OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrGLProgramBuilder_DEFINED | 8 #ifndef GrGLProgramBuilder_DEFINED |
9 #define GrGLProgramBuilder_DEFINED | 9 #define GrGLProgramBuilder_DEFINED |
10 | 10 |
11 #include "GrAllocator.h" | 11 #include "GrAllocator.h" |
12 #include "GrBackendEffectFactory.h" | 12 #include "GrBackendProcessorFactory.h" |
13 #include "GrColor.h" | 13 #include "GrColor.h" |
14 #include "GrEffect.h" | 14 #include "GrProcessor.h" |
15 #include "GrGLFragmentShaderBuilder.h" | 15 #include "GrGLFragmentShaderBuilder.h" |
16 #include "GrGLGeometryShaderBuilder.h" | 16 #include "GrGLGeometryShaderBuilder.h" |
17 #include "GrGLVertexShaderBuilder.h" | 17 #include "GrGLVertexShaderBuilder.h" |
18 #include "SkTypes.h" | 18 #include "SkTypes.h" |
| 19 #include "gl/GrGLProcessor.h" |
19 #include "gl/GrGLProgramDesc.h" | 20 #include "gl/GrGLProgramDesc.h" |
20 #include "gl/GrGLProgramEffects.h" | 21 #include "gl/GrGLProgramEffects.h" |
21 #include "gl/GrGLSL.h" | 22 #include "gl/GrGLSL.h" |
22 #include "gl/GrGLProgramDataManager.h" | 23 #include "gl/GrGLProgramDataManager.h" |
23 | 24 |
24 #include <stdarg.h> | 25 #include <stdarg.h> |
25 | 26 |
26 class GrGLContextInfo; | 27 class GrGLContextInfo; |
27 class GrEffectStage; | 28 class GrProcessorStage; |
28 class GrGLProgramDesc; | 29 class GrGLProgramDesc; |
29 | 30 |
30 /** | 31 /** |
31 Contains all the incremental state of a shader as it is being built,as well as
helpers to | 32 Contains all the incremental state of a shader as it is being built,as well as
helpers to |
32 manipulate that state. | 33 manipulate that state. |
33 */ | 34 */ |
34 class GrGLProgramBuilder { | 35 class GrGLProgramBuilder { |
35 public: | 36 public: |
36 enum ShaderVisibility { | 37 enum ShaderVisibility { |
37 kVertex_Visibility = 0x1, | 38 kVertex_Visibility = 0x1, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 typedef GrTAllocator<SeparableVaryingInfo> SeparableVaryingInfoArray; | 79 typedef GrTAllocator<SeparableVaryingInfo> SeparableVaryingInfoArray; |
79 | 80 |
80 /** Generates a shader program. | 81 /** Generates a shader program. |
81 * | 82 * |
82 * The program implements what is specified in the stages given as input. | 83 * The program implements what is specified in the stages given as input. |
83 * After successful generation, the builder result objects are available | 84 * After successful generation, the builder result objects are available |
84 * to be used. | 85 * to be used. |
85 * @return true if generation was successful. | 86 * @return true if generation was successful. |
86 */ | 87 */ |
87 | 88 |
88 bool genProgram(const GrEffectStage* inGeometryProcessor, | 89 bool genProgram(const GrGeometryStage* inGeometryProcessor, |
89 const GrEffectStage* inColorStages[], | 90 const GrFragmentStage* inColorStages[], |
90 const GrEffectStage* inCoverageStages[]); | 91 const GrFragmentStage* inCoverageStages[]); |
91 | 92 |
92 GrGLProgramEffects* getGeometryProcessor() const { | 93 GrGLProgramEffects* getGeometryProcessor() const { |
93 SkASSERT(fProgramID); return fGeometryProcessor.get(); | 94 SkASSERT(fProgramID); return fGeometryProcessor.get(); |
94 } | 95 } |
95 GrGLProgramEffects* getColorEffects() const { SkASSERT(fProgramID); return f
ColorEffects.get(); } | 96 GrGLProgramEffects* getColorEffects() const { SkASSERT(fProgramID); return f
ColorEffects.get(); } |
96 GrGLProgramEffects* getCoverageEffects() const { SkASSERT(fProgramID); retur
n fCoverageEffects.get(); } | 97 GrGLProgramEffects* getCoverageEffects() const { SkASSERT(fProgramID); retur
n fCoverageEffects.get(); } |
97 const BuiltinUniformHandles& getBuiltinUniformHandles() const { | 98 const BuiltinUniformHandles& getBuiltinUniformHandles() const { |
98 SkASSERT(fProgramID); | 99 SkASSERT(fProgramID); |
99 return fUniformHandles; | 100 return fUniformHandles; |
100 } | 101 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 GrGLFragmentShaderBuilder* getFragmentShaderBuilder() { return &fFS; } | 143 GrGLFragmentShaderBuilder* getFragmentShaderBuilder() { return &fFS; } |
143 GrGpuGL* gpu() const { return fGpu; } | 144 GrGpuGL* gpu() const { return fGpu; } |
144 | 145 |
145 protected: | 146 protected: |
146 typedef GrTAllocator<GrGLShaderVar> VarArray; | 147 typedef GrTAllocator<GrGLShaderVar> VarArray; |
147 GrGLProgramBuilder(GrGpuGL*, const GrGLProgramDesc&); | 148 GrGLProgramBuilder(GrGpuGL*, const GrGLProgramDesc&); |
148 | 149 |
149 const GrGLProgramDesc& desc() const { return fDesc; } | 150 const GrGLProgramDesc& desc() const { return fDesc; } |
150 | 151 |
151 // Helper for emitEffects(). | 152 // Helper for emitEffects(). |
152 void createAndEmitEffects(const GrEffectStage* effectStages[], | 153 void createAndEmitEffects(const GrFragmentStage* effectStages[], |
153 int effectCnt, | 154 int effectCnt, |
154 const GrGLProgramDesc::EffectKeyProvider&, | 155 const GrGLProgramDesc::EffectKeyProvider&, |
155 GrGLSLExpr4* inOutFSColor); | 156 GrGLSLExpr4* inOutFSColor); |
156 | 157 |
157 /* | 158 /* |
158 * A helper function called to emit the geometry processor as well as indivi
dual coverage | 159 * A helper function called to emit the geometry processor as well as indivi
dual coverage |
159 * and color stages. this will call into subclasses emit effect | 160 * and color stages. this will call into subclasses emit effect |
160 */ | 161 */ |
161 void emitEffect(const GrEffectStage& effectStage, | 162 void emitEffect(const GrProcessorStage& effectStage, |
162 int effectIndex, | 163 int effectIndex, |
163 const GrGLProgramDesc::EffectKeyProvider& keyProvider, | 164 const GrGLProgramDesc::EffectKeyProvider& keyProvider, |
164 GrGLSLExpr4* inColor, | 165 GrGLSLExpr4* inColor, |
165 GrGLSLExpr4* outColor); | 166 GrGLSLExpr4* outColor); |
166 | 167 |
167 /** | 168 /** |
168 * Helper for emitEffect() in subclasses. Emits uniforms for an effect's tex
ture accesses and | 169 * Helper for emitEffect() in subclasses. Emits uniforms for an effect's tex
ture accesses and |
169 * appends the necessary data to the TextureSamplerArray* object so effects
can add texture | 170 * appends the necessary data to the TextureSamplerArray* object so effects
can add texture |
170 * lookups to their code. This method is only meant to be called during the
construction phase. | 171 * lookups to their code. This method is only meant to be called during the
construction phase. |
171 */ | 172 */ |
172 void emitSamplers(const GrEffect& effect, GrGLEffect::TextureSamplerArray* o
utSamplers); | 173 void emitSamplers(const GrProcessor& effect, |
| 174 GrGLProcessor::TextureSamplerArray* outSamplers); |
173 | 175 |
174 // Generates a name for a variable. The generated string will be name prefix
ed by the prefix | 176 // Generates a name for a variable. The generated string will be name prefix
ed by the prefix |
175 // char (unless the prefix is '\0'). It also mangles the name to be stage-sp
ecific if we're | 177 // char (unless the prefix is '\0'). It also mangles the name to be stage-sp
ecific if we're |
176 // generating stage code. | 178 // generating stage code. |
177 void nameVariable(SkString* out, char prefix, const char* name); | 179 void nameVariable(SkString* out, char prefix, const char* name); |
178 | 180 |
179 virtual bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>
* shaderIds) const; | 181 virtual bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>
* shaderIds) const; |
180 | 182 |
181 virtual void bindProgramLocations(GrGLuint programId); | 183 virtual void bindProgramLocations(GrGLuint programId); |
182 void resolveProgramLocations(GrGLuint programId); | 184 void resolveProgramLocations(GrGLuint programId); |
183 | 185 |
184 void appendDecls(const VarArray&, SkString*) const; | 186 void appendDecls(const VarArray&, SkString*) const; |
185 void appendUniformDecls(ShaderVisibility, SkString*) const; | 187 void appendUniformDecls(ShaderVisibility, SkString*) const; |
186 | 188 |
187 class CodeStage : SkNoncopyable { | 189 class CodeStage : SkNoncopyable { |
188 public: | 190 public: |
189 CodeStage() : fNextIndex(0), fCurrentIndex(-1), fEffectStage(NULL) {} | 191 CodeStage() : fNextIndex(0), fCurrentIndex(-1), fEffectStage(NULL) {} |
190 | 192 |
191 bool inStageCode() const { | 193 bool inStageCode() const { |
192 this->validate(); | 194 this->validate(); |
193 return SkToBool(fEffectStage); | 195 return SkToBool(fEffectStage); |
194 } | 196 } |
195 | 197 |
196 const GrEffectStage* effectStage() const { | 198 const GrProcessorStage* effectStage() const { |
197 this->validate(); | 199 this->validate(); |
198 return fEffectStage; | 200 return fEffectStage; |
199 } | 201 } |
200 | 202 |
201 int stageIndex() const { | 203 int stageIndex() const { |
202 this->validate(); | 204 this->validate(); |
203 return fCurrentIndex; | 205 return fCurrentIndex; |
204 } | 206 } |
205 | 207 |
206 class AutoStageRestore : SkNoncopyable { | 208 class AutoStageRestore : SkNoncopyable { |
207 public: | 209 public: |
208 AutoStageRestore(CodeStage* codeStage, const GrEffectStage* newStage
) { | 210 AutoStageRestore(CodeStage* codeStage, const GrProcessorStage* newSt
age) { |
209 SkASSERT(codeStage); | 211 SkASSERT(codeStage); |
210 fSavedIndex = codeStage->fCurrentIndex; | 212 fSavedIndex = codeStage->fCurrentIndex; |
211 fSavedEffectStage = codeStage->fEffectStage; | 213 fSavedEffectStage = codeStage->fEffectStage; |
212 | 214 |
213 if (NULL == newStage) { | 215 if (NULL == newStage) { |
214 codeStage->fCurrentIndex = -1; | 216 codeStage->fCurrentIndex = -1; |
215 } else { | 217 } else { |
216 codeStage->fCurrentIndex = codeStage->fNextIndex++; | 218 codeStage->fCurrentIndex = codeStage->fNextIndex++; |
217 } | 219 } |
218 codeStage->fEffectStage = newStage; | 220 codeStage->fEffectStage = newStage; |
219 | 221 |
220 fCodeStage = codeStage; | 222 fCodeStage = codeStage; |
221 } | 223 } |
222 ~AutoStageRestore() { | 224 ~AutoStageRestore() { |
223 fCodeStage->fCurrentIndex = fSavedIndex; | 225 fCodeStage->fCurrentIndex = fSavedIndex; |
224 fCodeStage->fEffectStage = fSavedEffectStage; | 226 fCodeStage->fEffectStage = fSavedEffectStage; |
225 } | 227 } |
226 private: | 228 private: |
227 CodeStage* fCodeStage; | 229 CodeStage* fCodeStage; |
228 int fSavedIndex; | 230 int fSavedIndex; |
229 const GrEffectStage* fSavedEffectStage; | 231 const GrProcessorStage* fSavedEffectStage; |
230 }; | 232 }; |
231 private: | 233 private: |
232 void validate() const { SkASSERT((NULL == fEffectStage) == (-1 == fCurre
ntIndex)); } | 234 void validate() const { SkASSERT((NULL == fEffectStage) == (-1 == fCurre
ntIndex)); } |
233 int fNextIndex; | 235 int fNextIndex; |
234 int fCurrentIndex; | 236 int fCurrentIndex; |
235 const GrEffectStage* fEffectStage; | 237 const GrProcessorStage* fEffectStage; |
236 }; | 238 }; |
237 | 239 |
| 240 class GrGLProcessorEmitterInterface { |
| 241 public: |
| 242 virtual ~GrGLProcessorEmitterInterface() {} |
| 243 virtual GrGLProcessor* createGLInstance() = 0; |
| 244 virtual void emit(const GrProcessorKey& key, |
| 245 const char* outColor, |
| 246 const char* inColor, |
| 247 const GrGLProcessor::TransformedCoordsArray& coords, |
| 248 const GrGLProcessor::TextureSamplerArray& samplers) =
0; |
| 249 }; |
| 250 |
| 251 class GrGLFragmentProcessorEmitter : public GrGLProcessorEmitterInterface { |
| 252 public: |
| 253 GrGLFragmentProcessorEmitter(GrGLProgramBuilder* builder) |
| 254 : fBuilder(builder) |
| 255 , fFragmentProcessor(NULL) |
| 256 , fGLFragmentProcessor(NULL) {} |
| 257 virtual ~GrGLFragmentProcessorEmitter() {} |
| 258 void set(const GrFragmentProcessor* fp) { |
| 259 SkASSERT(NULL == fFragmentProcessor); |
| 260 fFragmentProcessor = fp; |
| 261 } |
| 262 virtual GrGLProcessor* createGLInstance() { |
| 263 SkASSERT(fFragmentProcessor); |
| 264 SkASSERT(NULL == fGLFragmentProcessor); |
| 265 fGLFragmentProcessor = |
| 266 fFragmentProcessor->getFactory().createGLInstance(*fFragment
Processor); |
| 267 return fGLFragmentProcessor; |
| 268 } |
| 269 virtual void emit(const GrProcessorKey& key, |
| 270 const char* outColor, |
| 271 const char* inColor, |
| 272 const GrGLProcessor::TransformedCoordsArray& coords, |
| 273 const GrGLProcessor::TextureSamplerArray& samplers) { |
| 274 SkASSERT(fFragmentProcessor); |
| 275 SkASSERT(fGLFragmentProcessor); |
| 276 fGLFragmentProcessor->emitCode(fBuilder, *fFragmentProcessor, key, o
utColor, inColor, |
| 277 coords, samplers); |
| 278 // this will not leak because it hasa already been used by createGLI
nstance |
| 279 fGLFragmentProcessor = NULL; |
| 280 fFragmentProcessor = NULL; |
| 281 } |
| 282 private: |
| 283 GrGLProgramBuilder* fBuilder; |
| 284 const GrFragmentProcessor* fFragmentProcessor; |
| 285 GrGLFragmentProcessor* fGLFragmentProcessor; |
| 286 }; |
| 287 |
| 288 GrGLProcessorEmitterInterface* fEffectEmitter; |
238 CodeStage fCodeStage; | 289 CodeStage fCodeStage; |
239 SkAutoTUnref<GrGLProgramEffects> fGeometryProcessor; | 290 SkAutoTUnref<GrGLProgramEffects> fGeometryProcessor; |
240 SkAutoTUnref<GrGLProgramEffects> fColorEffects; | 291 SkAutoTUnref<GrGLProgramEffects> fColorEffects; |
241 SkAutoTUnref<GrGLProgramEffects> fCoverageEffects; | 292 SkAutoTUnref<GrGLProgramEffects> fCoverageEffects; |
242 BuiltinUniformHandles fUniformHandles; | 293 BuiltinUniformHandles fUniformHandles; |
243 bool fFragOnly; | 294 bool fFragOnly; |
244 int fTexCoordSetCnt; | 295 int fTexCoordSetCnt; |
245 GrGLuint fProgramID; | 296 GrGLuint fProgramID; |
246 GrGLFragmentShaderBuilder fFS; | 297 GrGLFragmentShaderBuilder fFS; |
247 SeparableVaryingInfoArray fSeparableVaryingInfos; | 298 SeparableVaryingInfoArray fSeparableVaryingInfos; |
248 | 299 |
249 private: | 300 private: |
250 virtual void createAndEmitEffects(const GrEffectStage* geometryProcessor, | 301 virtual void createAndEmitEffects(const GrGeometryStage* geometryProcessor, |
251 const GrEffectStage* colorStages[], | 302 const GrFragmentStage* colorStages[], |
252 const GrEffectStage* coverageStages[], | 303 const GrFragmentStage* coverageStages[], |
253 GrGLSLExpr4* inputColor, | 304 GrGLSLExpr4* inputColor, |
254 GrGLSLExpr4* inputCoverage) = 0; | 305 GrGLSLExpr4* inputCoverage) = 0; |
255 /* | 306 /* |
256 * Subclasses override emitEffect below to emit data and code for a specific
single effect | 307 * Subclasses override emitEffect below to emit data and code for a specific
single effect |
257 */ | 308 */ |
258 virtual void emitEffect(const GrEffectStage&, | 309 virtual void emitEffect(const GrProcessorStage&, |
259 const GrEffectKey&, | 310 const GrProcessorKey&, |
260 const char* outColor, | 311 const char* outColor, |
261 const char* inColor, | 312 const char* inColor, |
262 int stageIndex) = 0; | 313 int stageIndex) = 0; |
263 | 314 |
264 /* | 315 /* |
265 * Because we have fragment only builders, and those builders need to implem
ent a subclass | 316 * Because we have fragment only builders, and those builders need to implem
ent a subclass |
266 * of program effects, we have to have base classes overload the program eff
ects here | 317 * of program effects, we have to have base classes overload the program eff
ects here |
267 */ | 318 */ |
268 virtual GrGLProgramEffects* getProgramEffects() = 0; | 319 virtual GrGLProgramEffects* getProgramEffects() = 0; |
269 | 320 |
270 /** | 321 /** |
271 * Compiles all the shaders, links them into a program, and writes the progr
am id to the output | 322 * Compiles all the shaders, links them into a program, and writes the progr
am id to the output |
272 * struct. | 323 * struct. |
273 **/ | 324 **/ |
274 bool finish(); | 325 bool finish(); |
275 | 326 |
| 327 GrGLFragmentProcessorEmitter fGrProcessorEmitter; |
| 328 |
276 const GrGLProgramDesc& fDesc; | 329 const GrGLProgramDesc& fDesc; |
277 GrGpuGL* fGpu; | 330 GrGpuGL* fGpu; |
278 UniformInfoArray fUniforms; | 331 UniformInfoArray fUniforms; |
279 | 332 |
280 friend class GrGLShaderBuilder; | 333 friend class GrGLShaderBuilder; |
281 friend class GrGLVertexShaderBuilder; | 334 friend class GrGLVertexShaderBuilder; |
282 friend class GrGLFragmentShaderBuilder; | 335 friend class GrGLFragmentShaderBuilder; |
283 friend class GrGLGeometryShaderBuilder; | 336 friend class GrGLGeometryShaderBuilder; |
284 }; | 337 }; |
285 | 338 |
286 #endif | 339 #endif |
OLD | NEW |