1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 
8 #ifndef GrVkPipelineStateBuilder_DEFINED
9 #define GrVkPipelineStateBuilder_DEFINED
10 
11 #include "include/gpu/vk/GrVkTypes.h"
12 #include "src/gpu/GrPipeline.h"
13 #include "src/gpu/GrProgramDesc.h"
14 #include "src/gpu/glsl/GrGLSLProgramBuilder.h"
15 #include "src/gpu/vk/GrVkPipelineState.h"
16 #include "src/gpu/vk/GrVkUniformHandler.h"
17 #include "src/gpu/vk/GrVkVaryingHandler.h"
18 #include "src/sksl/SkSLCompiler.h"
19 
20 class GrVkGpu;
21 class GrVkRenderPass;
22 class SkReader32;
23 
24 class GrVkPipelineStateBuilder : public GrGLSLProgramBuilder {
25 public:
26     /**
27      * For Vulkan we want to cache the entire VkPipeline for reuse of draws. The Desc here holds all
28      * the information needed to differentiate one pipeline from another.
29      *
30      * The GrProgramDesc contains all the information need to create the actual shaders for the
31      * pipeline.
32      *
33      * For Vulkan we need to add to the GrProgramDesc to include the rest of the state on the
34      * pipline. This includes stencil settings, blending information, render pass format, draw face
35      * information, and primitive type. Note that some state is set dynamically on the pipeline for
36      * each draw  and thus is not included in this descriptor. This includes the viewport, scissor,
37      * and blend constant.
38      */
39     class Desc : public GrProgramDesc {
40     public:
41         static bool Build(Desc*,
42                           GrRenderTarget*,
43                           const GrProgramInfo&,
44                           const GrStencilSettings&,
45                           GrPrimitiveType primitiveType,
46                           GrVkGpu* gpu);
47 
shaderKeyLength()48         size_t shaderKeyLength() const { return fShaderKeyLength; }
49 
50     private:
51         size_t fShaderKeyLength;
52 
53         typedef GrProgramDesc INHERITED;
54     };
55 
56     /** Generates a pipeline state.
57     *
58     * The GrVkPipelineState implements what is specified in the GrPipeline and GrPrimitiveProcessor
59     * as input. After successful generation, the builder result objects are available to be used.
60     * This function may modify the program key by setting the surface origin key to 0 (unspecified)
61     * if it turns out the program does not care about the surface origin.
62     * @return true if generation was successful.
63     */
64     static GrVkPipelineState* CreatePipelineState(GrVkGpu*,
65                                                   GrRenderTarget*,
66                                                   const GrProgramInfo&,
67                                                   const GrStencilSettings&,
68                                                   GrPrimitiveType,
69                                                   Desc*,
70                                                   VkRenderPass compatibleRenderPass);
71 
72     const GrCaps* caps() const override;
73 
gpu()74     GrVkGpu* gpu() const { return fGpu; }
75 
76     void finalizeFragmentOutputColor(GrShaderVar& outputColor) override;
77     void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) override;
78 
79 private:
80     GrVkPipelineStateBuilder(GrVkGpu*, GrRenderTarget*, const GrProgramInfo&, GrProgramDesc*);
81 
82     GrVkPipelineState* finalize(const GrStencilSettings&,
83                                 GrPrimitiveType primitiveType,
84                                 VkRenderPass compatibleRenderPass,
85                                 Desc*);
86 
87     // returns number of shader stages
88     int loadShadersFromCache(SkReader32* cached, VkShaderModule outShaderModules[],
89                              VkPipelineShaderStageCreateInfo* outStageInfo);
90 
91     void storeShadersInCache(const SkSL::String shaders[], const SkSL::Program::Inputs inputs[],
92                              bool isSkSL);
93 
94     bool createVkShaderModule(VkShaderStageFlagBits stage,
95                               const SkSL::String& sksl,
96                               VkShaderModule* shaderModule,
97                               VkPipelineShaderStageCreateInfo* stageInfo,
98                               const SkSL::Program::Settings& settings,
99                               Desc* desc,
100                               SkSL::String* outSPIRV,
101                               SkSL::Program::Inputs* outInputs);
102 
103     bool installVkShaderModule(VkShaderStageFlagBits stage,
104                                const GrGLSLShaderBuilder& builder,
105                                VkShaderModule* shaderModule,
106                                VkPipelineShaderStageCreateInfo* stageInfo,
107                                SkSL::String spirv,
108                                SkSL::Program::Inputs inputs);
109 
uniformHandler()110     GrGLSLUniformHandler* uniformHandler() override { return &fUniformHandler; }
uniformHandler()111     const GrGLSLUniformHandler* uniformHandler() const override { return &fUniformHandler; }
varyingHandler()112     GrGLSLVaryingHandler* varyingHandler() override { return &fVaryingHandler; }
113 
114     GrVkGpu* fGpu;
115     GrVkVaryingHandler fVaryingHandler;
116     GrVkUniformHandler fUniformHandler;
117 
118     typedef GrGLSLProgramBuilder INHERITED;
119 };
120 
121 #endif
122