• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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 GrGLSLProgramBuilder_DEFINED
9 #define GrGLSLProgramBuilder_DEFINED
10 
11 #include "src/gpu/GrCaps.h"
12 #include "src/gpu/GrFragmentProcessor.h"
13 #include "src/gpu/GrGeometryProcessor.h"
14 #include "src/gpu/GrProgramInfo.h"
15 #include "src/gpu/GrXferProcessor.h"
16 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
17 #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
18 #include "src/gpu/glsl/GrGLSLUniformHandler.h"
19 #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
20 #include "src/sksl/SkSLCompiler.h"
21 
22 #include <vector>
23 
24 class GrProgramDesc;
25 class GrRenderTarget;
26 class GrShaderVar;
27 class GrGLSLVaryingHandler;
28 class SkString;
29 class GrShaderCaps;
30 
31 class GrGLSLProgramBuilder {
32 public:
33     using UniformHandle      = GrGLSLUniformHandler::UniformHandle;
34     using SamplerHandle      = GrGLSLUniformHandler::SamplerHandle;
35 
36     virtual ~GrGLSLProgramBuilder();
37 
38     virtual const GrCaps* caps() const = 0;
shaderCaps()39     const GrShaderCaps* shaderCaps() const { return this->caps()->shaderCaps(); }
40 
origin()41     GrSurfaceOrigin origin() const { return fProgramInfo.origin(); }
pipeline()42     const GrPipeline& pipeline() const { return fProgramInfo.pipeline(); }
geometryProcessor()43     const GrGeometryProcessor& geometryProcessor() const { return fProgramInfo.geomProc(); }
snapVerticesToPixelCenters()44     bool snapVerticesToPixelCenters() const {
45         return fProgramInfo.pipeline().snapVerticesToPixelCenters();
46     }
hasPointSize()47     bool hasPointSize() const { return fProgramInfo.primitiveType() == GrPrimitiveType::kPoints; }
48     virtual SkSL::Compiler* shaderCompiler() const = 0;
49 
desc()50     const GrProgramDesc& desc() const { return fDesc; }
51 
52     void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
53 
samplerVariable(SamplerHandle handle)54     const char* samplerVariable(SamplerHandle handle) const {
55         return this->uniformHandler()->samplerVariable(handle);
56     }
57 
samplerSwizzle(SamplerHandle handle)58     GrSwizzle samplerSwizzle(SamplerHandle handle) const {
59         return this->uniformHandler()->samplerSwizzle(handle);
60     }
61 
inputSamplerVariable(SamplerHandle handle)62     const char* inputSamplerVariable(SamplerHandle handle) const {
63         return this->uniformHandler()->inputSamplerVariable(handle);
64     }
65 
inputSamplerSwizzle(SamplerHandle handle)66     GrSwizzle inputSamplerSwizzle(SamplerHandle handle) const {
67         return this->uniformHandler()->inputSamplerSwizzle(handle);
68     }
69 
70     // Used to add a uniform for render target flip (used for dFdy, sk_Clockwise, and sk_FragCoord)
71     // without mangling the name of the uniform inside of a stage.
72     void addRTFlipUniform(const char* name);
73 
74     // Generates a name for a variable. The generated string will be name prefixed by the prefix
75     // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless
76     // explicitly asked not to. `nameVariable` can also be used to generate names for functions or
77     // other types of symbols where unique names are important.
78     SkString nameVariable(char prefix, const char* name, bool mangle = true);
79 
80     /**
81      * If the FP's coords are unused or all uses have been lifted to interpolated varyings then
82      * don't put coords in the FP's function signature or call sites.
83      */
84     bool fragmentProcessorHasCoordsParam(const GrFragmentProcessor*);
85 
86     virtual GrGLSLUniformHandler* uniformHandler() = 0;
87     virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
88     virtual GrGLSLVaryingHandler* varyingHandler() = 0;
89 
90     // Used for backend customization of the output color and secondary color variables from the
91     // fragment processor. Only used if the outputs are explicitly declared in the shaders
finalizeFragmentOutputColor(GrShaderVar & outputColor)92     virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {}
finalizeFragmentSecondaryColor(GrShaderVar & outputColor)93     virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {}
94 
95     // number of each input/output type in a single allocation block, used by many builders
96     static const int kVarsPerBlock;
97 
98     GrGLSLVertexBuilder          fVS;
99     GrGLSLFragmentShaderBuilder  fFS;
100 
101     const GrProgramDesc&         fDesc;
102     const GrProgramInfo&         fProgramInfo;
103 
104     GrGLSLBuiltinUniformHandles  fUniformHandles;
105 
106     std::unique_ptr<GrGeometryProcessor::ProgramImpl>               fGPImpl;
107     std::unique_ptr<GrXferProcessor::ProgramImpl>                   fXPImpl;
108     std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>>  fFPImpls;
109 
110     SamplerHandle fDstTextureSamplerHandle;
111     GrSurfaceOrigin fDstTextureOrigin;
112 
113 protected:
114     explicit GrGLSLProgramBuilder(const GrProgramDesc&, const GrProgramInfo&);
115 
116     void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
117 
118     bool emitAndInstallProcs();
119 
120     void finalizeShaders();
121 
fragColorIsInOut()122     bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); }
123 
124 private:
125     // advanceStage is called by program creator between each processor's emit code.  It increments
126     // the stage index for variable name mangling, and also ensures verification variables in the
127     // fragment shader are cleared.
advanceStage()128     void advanceStage() {
129         fStageIndex++;
130         SkDEBUGCODE(fFS.debugOnly_resetPerStageVerification();)
131         fFS.nextStage();
132     }
133 
134     SkString getMangleSuffix() const;
135 
136     // Generates a possibly mangled name for a stage variable and writes it to the fragment shader.
137     void nameExpression(SkString*, const char* baseName);
138 
139     bool emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage);
140     bool emitAndInstallDstTexture();
141     /** Adds the root FPs */
142     bool emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut);
143     /** Adds a single root FP tree. */
144     SkString emitFragProc(const GrFragmentProcessor&,
145                           GrFragmentProcessor::ProgramImpl&,
146                           const SkString& input,
147                           SkString output);
148     /** Recursive step to write out children FPs' functions before parent's. */
149     void writeChildFPFunctions(const GrFragmentProcessor& fp,
150                                GrFragmentProcessor::ProgramImpl& impl);
151     /** Adds the SkSL function that implements an FP assuming its children are already written. */
152     void writeFPFunction(const GrFragmentProcessor& fp, GrFragmentProcessor::ProgramImpl& impl);
153     bool emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn);
154     SamplerHandle emitSampler(const GrBackendFormat&, GrSamplerState, const GrSwizzle&,
155                               const char* name);
156     SamplerHandle emitInputSampler(const GrSwizzle& swizzle, const char* name);
157     bool checkSamplerCounts();
158 
159 #ifdef SK_DEBUG
160     void verify(const GrGeometryProcessor&);
161     void verify(const GrFragmentProcessor&);
162     void verify(const GrXferProcessor&);
163 #endif
164 
165     // This is used to check that we don't excede the allowable number of resources in a shader.
166     int fNumFragmentSamplers;
167 
168     GrGeometryProcessor::ProgramImpl::FPCoordsMap fFPCoordsMap;
169 
170     /**
171      * Each root processor has an stage index. The GP is stage 0. The first root FP is stage 1,
172      * the second root FP is stage 2, etc. The XP's stage index is last and its value depends on
173      * how many root FPs there are. Names are mangled by appending _S<stage-index>.
174      */
175     int fStageIndex = -1;
176 
177     /**
178      * When emitting FP stages we track the children FPs as "substages" and do additional name
179      * mangling based on where in the FP hierarchy we are. The first FP is stage index 1. It's first
180      * child would be substage 0 of stage 1. If that FP also has three children then its third child
181      * would be substage 2 of stubstage 0 of stage 1 and would be mangled as "_S1_c0_c2".
182      */
183     SkTArray<int> fSubstageIndices;
184 };
185 
186 #endif
187