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/GrGeometryProcessor.h" |
13 | #include "src/gpu/GrProgramInfo.h" |
14 | #include "src/gpu/GrRenderTarget.h" |
15 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
16 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
17 | #include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h" |
18 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
19 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
20 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
21 | #include "src/gpu/glsl/GrGLSLXferProcessor.h" |
22 | |
23 | class GrProgramDesc; |
24 | class GrShaderVar; |
25 | class GrGLSLVaryingHandler; |
26 | class SkString; |
27 | class GrShaderCaps; |
28 | |
29 | class GrGLSLProgramBuilder { |
30 | public: |
31 | using UniformHandle = GrGLSLUniformHandler::UniformHandle; |
32 | using SamplerHandle = GrGLSLUniformHandler::SamplerHandle; |
33 | |
34 | virtual ~GrGLSLProgramBuilder() {} |
35 | |
36 | virtual const GrCaps* caps() const = 0; |
37 | const GrShaderCaps* shaderCaps() const { return this->caps()->shaderCaps(); } |
38 | |
39 | GrSurfaceOrigin origin() const { return fProgramInfo.origin(); } |
40 | const GrPipeline& pipeline() const { return fProgramInfo.pipeline(); } |
41 | const GrPrimitiveProcessor& primitiveProcessor() const { return fProgramInfo.primProc(); } |
42 | GrProcessor::CustomFeatures processorFeatures() const { |
43 | return fProgramInfo.requestedFeatures(); |
44 | } |
45 | bool snapVerticesToPixelCenters() const { |
46 | return fProgramInfo.pipeline().snapVerticesToPixelCenters(); |
47 | } |
48 | bool hasPointSize() const { return fProgramInfo.primitiveType() == GrPrimitiveType::kPoints; } |
49 | |
50 | // TODO: stop passing in the renderTarget for just the sampleLocations |
51 | int effectiveSampleCnt() { |
52 | SkASSERT(GrProcessor::CustomFeatures::kSampleLocations & fProgramInfo.requestedFeatures()); |
53 | return fRenderTarget->getSampleLocations().count(); |
54 | } |
55 | const SkTArray<SkPoint>& getSampleLocations() { |
56 | SkASSERT(GrProcessor::CustomFeatures::kSampleLocations & fProgramInfo.requestedFeatures()); |
57 | return fRenderTarget->getSampleLocations(); |
58 | } |
59 | |
60 | const GrProgramDesc& desc() const { return fDesc; } |
61 | |
62 | void appendUniformDecls(GrShaderFlags visibility, SkString*) const; |
63 | |
64 | const char* samplerVariable(SamplerHandle handle) const { |
65 | return this->uniformHandler()->samplerVariable(handle); |
66 | } |
67 | |
68 | GrSwizzle samplerSwizzle(SamplerHandle handle) const { |
69 | if (this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) { |
70 | return this->uniformHandler()->samplerSwizzle(handle); |
71 | } |
72 | return GrSwizzle::RGBA(); |
73 | } |
74 | |
75 | // Used to add a uniform for the RenderTarget width (used for sk_Width) without mangling |
76 | // the name of the uniform inside of a stage. |
77 | void addRTWidthUniform(const char* name); |
78 | |
79 | // Used to add a uniform for the RenderTarget height (used for sk_Height and frag position) |
80 | // without mangling the name of the uniform inside of a stage. |
81 | void addRTHeightUniform(const char* name); |
82 | |
83 | // Generates a name for a variable. The generated string will be name prefixed by the prefix |
84 | // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless |
85 | // explicitly asked not to. |
86 | void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true); |
87 | |
88 | virtual GrGLSLUniformHandler* uniformHandler() = 0; |
89 | virtual const GrGLSLUniformHandler* uniformHandler() const = 0; |
90 | virtual GrGLSLVaryingHandler* varyingHandler() = 0; |
91 | |
92 | // Used for backend customization of the output color and secondary color variables from the |
93 | // fragment processor. Only used if the outputs are explicitly declared in the shaders |
94 | virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {} |
95 | virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {} |
96 | |
97 | // number of each input/output type in a single allocation block, used by many builders |
98 | static const int kVarsPerBlock; |
99 | |
100 | GrGLSLVertexBuilder fVS; |
101 | GrGLSLGeometryBuilder fGS; |
102 | GrGLSLFragmentShaderBuilder fFS; |
103 | |
104 | int fStageIndex; |
105 | |
106 | GrRenderTarget* fRenderTarget; // TODO: remove this |
107 | const GrProgramDesc& fDesc; |
108 | const GrProgramInfo& fProgramInfo; |
109 | |
110 | GrGLSLBuiltinUniformHandles fUniformHandles; |
111 | |
112 | std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor; |
113 | std::unique_ptr<GrGLSLXferProcessor> fXferProcessor; |
114 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors; |
115 | |
116 | protected: |
117 | explicit GrGLSLProgramBuilder(GrRenderTarget*, const GrProgramDesc&, const GrProgramInfo&); |
118 | |
119 | void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName); |
120 | |
121 | bool emitAndInstallProcs(); |
122 | |
123 | void finalizeShaders(); |
124 | |
125 | bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); } |
126 | |
127 | private: |
128 | // reset is called by program creator between each processor's emit code. It increments the |
129 | // stage offset for variable name mangling, and also ensures verfication variables in the |
130 | // fragment shader are cleared. |
131 | void reset() { |
132 | this->addStage(); |
133 | SkDEBUGCODE(fFS.debugOnly_resetPerStageVerification();) |
134 | } |
135 | void addStage() { fStageIndex++; } |
136 | |
137 | class AutoStageAdvance { |
138 | public: |
139 | AutoStageAdvance(GrGLSLProgramBuilder* pb) |
140 | : fPB(pb) { |
141 | fPB->reset(); |
142 | // Each output to the fragment processor gets its own code section |
143 | fPB->fFS.nextStage(); |
144 | } |
145 | ~AutoStageAdvance() {} |
146 | private: |
147 | GrGLSLProgramBuilder* fPB; |
148 | }; |
149 | |
150 | // Generates a possibly mangled name for a stage variable and writes it to the fragment shader. |
151 | void nameExpression(SkString*, const char* baseName); |
152 | |
153 | void emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage); |
154 | void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut); |
155 | SkString emitFragProc(const GrFragmentProcessor&, |
156 | GrGLSLFragmentProcessor&, |
157 | int transformedCoordVarsIdx, |
158 | const SkString& input, |
159 | SkString output); |
160 | void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn); |
161 | SamplerHandle emitSampler(const GrBackendFormat&, GrSamplerState, const GrSwizzle&, |
162 | const char* name); |
163 | bool checkSamplerCounts(); |
164 | |
165 | #ifdef SK_DEBUG |
166 | void verify(const GrPrimitiveProcessor&); |
167 | void verify(const GrFragmentProcessor&); |
168 | void verify(const GrXferProcessor&); |
169 | #endif |
170 | |
171 | // These are used to check that we don't excede the allowable number of resources in a shader. |
172 | int fNumFragmentSamplers; |
173 | SkSTArray<4, GrShaderVar> fTransformedCoordVars; |
174 | }; |
175 | |
176 | #endif |
177 | |