• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "GrPipeline.h"
12 #include "GrProgramDesc.h"
13 #include "GrVkPipelineState.h"
14 #include "GrVkUniformHandler.h"
15 #include "GrVkVaryingHandler.h"
16 #include "SkSLCompiler.h"
17 #include "glsl/GrGLSLProgramBuilder.h"
18 #include "vk/GrVkTypes.h"
19 
20 class GrVkGpu;
21 class GrVkRenderPass;
22 
23 class GrVkPipelineStateBuilder : public GrGLSLProgramBuilder {
24 public:
25     /**
26      * For Vulkan we want to cache the entire VkPipeline for reuse of draws. The Desc here holds all
27      * the information needed to differentiate one pipeline from another.
28      *
29      * The GrProgramDesc contains all the information need to create the actual shaders for the
30      * pipeline.
31      *
32      * For Vulkan we need to add to the GrProgramDesc to include the rest of the state on the
33      * pipline. This includes stencil settings, blending information, render pass format, draw face
34      * information, and primitive type. Note that some state is set dynamically on the pipeline for
35      * each draw  and thus is not included in this descriptor. This includes the viewport, scissor,
36      * and blend constant.
37      */
38     class Desc : public GrProgramDesc {
39     public:
40         static bool Build(Desc*,
41                           GrRenderTarget*,
42                           const GrPrimitiveProcessor&,
43                           const GrPipeline&,
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*, GrSurfaceOrigin,
66                                                   const GrPrimitiveProcessor&,
67                                                   const GrTextureProxy* const primProcProxies[],
68                                                   const GrPipeline&,
69                                                   const GrStencilSettings&,
70                                                   GrPrimitiveType,
71                                                   Desc*,
72                                                   VkRenderPass compatibleRenderPass);
73 
74     const GrCaps* caps() const override;
75 
gpu()76     GrVkGpu* gpu() const { return fGpu; }
77 
78     void finalizeFragmentOutputColor(GrShaderVar& outputColor) override;
79     void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) override;
80 
81 private:
82     GrVkPipelineStateBuilder(GrVkGpu*, GrRenderTarget*, GrSurfaceOrigin,
83                              const GrPipeline&,
84                              const GrPrimitiveProcessor&,
85                              const GrTextureProxy* const primProcProxies[],
86                              GrProgramDesc*);
87 
88     GrVkPipelineState* finalize(const GrStencilSettings&,
89                                 GrPrimitiveType primitiveType,
90                                 VkRenderPass compatibleRenderPass,
91                                 Desc*);
92 
93     // returns number of shader stages
94     int loadShadersFromCache(const SkData& cached,
95                              VkShaderModule* outVertShaderModule,
96                              VkShaderModule* outFragShaderModule,
97                              VkShaderModule* outGeomShaderModule,
98                              VkPipelineShaderStageCreateInfo* outStageInfo);
99 
100     void storeShadersInCache(const SkSL::String& vert,
101                              const SkSL::Program::Inputs& vertInputs,
102                              const SkSL::String& frag,
103                              const SkSL::Program::Inputs& fragInputs,
104                              const SkSL::String& geom,
105                              const SkSL::Program::Inputs& geomInputs);
106 
107     bool createVkShaderModule(VkShaderStageFlagBits stage,
108                               const GrGLSLShaderBuilder& builder,
109                               VkShaderModule* shaderModule,
110                               VkPipelineShaderStageCreateInfo* stageInfo,
111                               const SkSL::Program::Settings& settings,
112                               Desc* desc,
113                               SkSL::String* outSPIRV,
114                               SkSL::Program::Inputs* outInputs);
115 
116     bool installVkShaderModule(VkShaderStageFlagBits stage,
117                                const GrGLSLShaderBuilder& builder,
118                                VkShaderModule* shaderModule,
119                                VkPipelineShaderStageCreateInfo* stageInfo,
120                                SkSL::String spirv,
121                                SkSL::Program::Inputs inputs);
122 
uniformHandler()123     GrGLSLUniformHandler* uniformHandler() override { return &fUniformHandler; }
uniformHandler()124     const GrGLSLUniformHandler* uniformHandler() const override { return &fUniformHandler; }
varyingHandler()125     GrGLSLVaryingHandler* varyingHandler() override { return &fVaryingHandler; }
126 
127     GrVkGpu* fGpu;
128     GrVkVaryingHandler fVaryingHandler;
129     GrVkUniformHandler fUniformHandler;
130 
131     typedef GrGLSLProgramBuilder INHERITED;
132 };
133 
134 #endif
135