• 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 "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 
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, VkShaderModule outShaderModules[],
95                              VkPipelineShaderStageCreateInfo* outStageInfo);
96 
97     void storeShadersInCache(const SkSL::String shaders[], const SkSL::Program::Inputs inputs[],
98                              bool isSkSL);
99 
100     bool createVkShaderModule(VkShaderStageFlagBits stage,
101                               const SkSL::String& sksl,
102                               VkShaderModule* shaderModule,
103                               VkPipelineShaderStageCreateInfo* stageInfo,
104                               const SkSL::Program::Settings& settings,
105                               Desc* desc,
106                               SkSL::String* outSPIRV,
107                               SkSL::Program::Inputs* outInputs);
108 
109     bool installVkShaderModule(VkShaderStageFlagBits stage,
110                                const GrGLSLShaderBuilder& builder,
111                                VkShaderModule* shaderModule,
112                                VkPipelineShaderStageCreateInfo* stageInfo,
113                                SkSL::String spirv,
114                                SkSL::Program::Inputs inputs);
115 
uniformHandler()116     GrGLSLUniformHandler* uniformHandler() override { return &fUniformHandler; }
uniformHandler()117     const GrGLSLUniformHandler* uniformHandler() const override { return &fUniformHandler; }
varyingHandler()118     GrGLSLVaryingHandler* varyingHandler() override { return &fVaryingHandler; }
119 
120     GrVkGpu* fGpu;
121     GrVkVaryingHandler fVaryingHandler;
122     GrVkUniformHandler fUniformHandler;
123 
124     typedef GrGLSLProgramBuilder INHERITED;
125 };
126 
127 #endif
128