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 #include "GrGLProgramBuilder.h" | 8 #include "GrGLProgramBuilder.h" |
9 #include "gl/GrGLProgram.h" | 9 #include "gl/GrGLProgram.h" |
10 #include "gl/GrGLSLPrettyPrint.h" | 10 #include "gl/GrGLSLPrettyPrint.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->gpu()->glInterface(), R, X) | 22 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->gpu()->glInterface(), R, X) |
23 | 23 |
24 // ES2 FS only guarantees mediump and lowp support | 24 // ES2 FS only guarantees mediump and lowp support |
25 static const GrGLShaderVar::Precision kDefaultFragmentPrecision = GrGLShaderVar:
:kMedium_Precision; | 25 static const GrGLShaderVar::Precision kDefaultFragmentPrecision = GrGLShaderVar:
:kMedium_Precision; |
26 | 26 |
27 ////////////////////////////////////////////////////////////////////////////// | 27 ////////////////////////////////////////////////////////////////////////////// |
28 | 28 |
29 const int GrGLProgramBuilder::kVarsPerBlock = 8; | 29 const int GrGLProgramBuilder::kVarsPerBlock = 8; |
30 | 30 |
31 GrGLProgram* GrGLProgramBuilder::CreateProgram(const GrOptDrawState& optState, | 31 GrGLProgram* GrGLProgramBuilder::CreateProgram(const GrOptDrawState& optState, |
32 const GrGLProgramDesc& desc, | |
33 GrGpu::DrawType drawType, | 32 GrGpu::DrawType drawType, |
34 GrGpuGL* gpu) { | 33 GrGpuGL* gpu) { |
35 // create a builder. This will be handed off to effects so they can use it
to add | 34 // create a builder. This will be handed off to effects so they can use it
to add |
36 // uniforms, varyings, textures, etc | 35 // uniforms, varyings, textures, etc |
37 SkAutoTDelete<GrGLProgramBuilder> builder(CreateProgramBuilder(desc, | 36 SkAutoTDelete<GrGLProgramBuilder> builder(CreateProgramBuilder(optState, |
38 optState, | |
39 drawType, | 37 drawType, |
40 optState.hasG
eometryProcessor(), | 38 optState.hasG
eometryProcessor(), |
41 gpu)); | 39 gpu)); |
42 | 40 |
43 GrGLProgramBuilder* pb = builder.get(); | 41 GrGLProgramBuilder* pb = builder.get(); |
44 const GrGLProgramDesc::KeyHeader& header = pb->header(); | 42 const GrGLProgramDescBuilder::GLKeyHeader& header = GrGLProgramDescBuilder::
GetHeader(pb->desc()); |
45 | 43 |
46 // emit code to read the dst copy texture, if necessary | 44 // emit code to read the dst copy texture, if necessary |
47 if (GrGLFragmentShaderBuilder::kNoDstRead_DstReadKey != header.fDstReadKey | 45 if (GrGLFragmentShaderBuilder::kNoDstRead_DstReadKey != header.fDstReadKey |
48 && !gpu->glCaps().fbFetchSupport()) { | 46 && !gpu->glCaps().fbFetchSupport()) { |
49 pb->fFS.emitCodeToReadDstTexture(); | 47 pb->fFS.emitCodeToReadDstTexture(); |
50 } | 48 } |
51 | 49 |
52 // get the initial color and coverage to feed into the first effect in each
effect chain | 50 // get the initial color and coverage to feed into the first effect in each
effect chain |
53 GrGLSLExpr4 inputColor, inputCoverage; | 51 GrGLSLExpr4 inputColor, inputCoverage; |
54 pb->setupUniformColorAndCoverageIfNeeded(&inputColor, &inputCoverage); | 52 pb->setupUniformColorAndCoverageIfNeeded(&inputColor, &inputCoverage); |
55 | 53 |
56 // if we have a vertex shader(we don't only if we are using NVPR or NVPR ES)
, then we may have | 54 // if we have a vertex shader(we don't only if we are using NVPR or NVPR ES)
, then we may have |
57 // to setup a few more things like builtin vertex attributes | 55 // to setup a few more things like builtin vertex attributes |
58 bool hasVertexShader = !(header.fUseNvpr && | 56 bool hasVertexShader = !(header.fUseNvpr && |
59 gpu->glPathRendering()->texturingMode() == | 57 gpu->glPathRendering()->texturingMode() == |
60 GrGLPathRendering::FixedFunction_TexturingMode); | 58 GrGLPathRendering::FixedFunction_TexturingMode); |
61 if (hasVertexShader) { | 59 if (hasVertexShader) { |
62 pb->fVS.setupLocalCoords(); | 60 pb->fVS.setupLocalCoords(); |
63 pb->fVS.transformGLToSkiaCoords(); | 61 pb->fVS.transformGLToSkiaCoords(); |
64 if (header.fEmitsPointSize) { | 62 if (header.fEmitsPointSize) { |
65 pb->fVS.codeAppend("gl_PointSize = 1.0;"); | 63 pb->fVS.codeAppend("gl_PointSize = 1.0;"); |
66 } | 64 } |
67 if (GrGLProgramDesc::kAttribute_ColorInput == header.fColorInput) { | 65 if (GrProgramDesc::kAttribute_ColorInput == header.fColorInput) { |
68 pb->fVS.setupBuiltinVertexAttribute("Color", &inputColor); | 66 pb->fVS.setupBuiltinVertexAttribute("Color", &inputColor); |
69 } | 67 } |
70 if (GrGLProgramDesc::kAttribute_ColorInput == header.fCoverageInput) { | 68 if (GrProgramDesc::kAttribute_ColorInput == header.fCoverageInput) { |
71 pb->fVS.setupBuiltinVertexAttribute("Coverage", &inputCoverage); | 69 pb->fVS.setupBuiltinVertexAttribute("Coverage", &inputCoverage); |
72 } | 70 } |
73 } | 71 } |
74 | 72 |
75 pb->emitAndInstallProcs(optState, &inputColor, &inputCoverage); | 73 pb->emitAndInstallProcs(optState, &inputColor, &inputCoverage); |
76 | 74 |
77 if (hasVertexShader) { | 75 if (hasVertexShader) { |
78 pb->fVS.transformSkiaToGLCoords(); | 76 pb->fVS.transformSkiaToGLCoords(); |
79 } | 77 } |
80 | 78 |
81 // write the secondary color output if necessary | 79 // write the secondary color output if necessary |
82 if (GrOptDrawState::kNone_SecondaryOutputType != header.fSecondaryOutputType
) { | 80 if (GrProgramDesc::kNone_SecondaryOutputType != header.fSecondaryOutputType)
{ |
83 pb->fFS.enableSecondaryOutput(inputColor, inputCoverage); | 81 pb->fFS.enableSecondaryOutput(inputColor, inputCoverage); |
84 } | 82 } |
85 | 83 |
86 pb->fFS.combineColorAndCoverage(inputColor, inputCoverage); | 84 pb->fFS.combineColorAndCoverage(inputColor, inputCoverage); |
87 | 85 |
88 return pb->finalize(); | 86 return pb->finalize(); |
89 } | 87 } |
90 | 88 |
91 GrGLProgramBuilder* | 89 GrGLProgramBuilder* |
92 GrGLProgramBuilder::CreateProgramBuilder(const GrGLProgramDesc& desc, | 90 GrGLProgramBuilder::CreateProgramBuilder(const GrOptDrawState& optState, |
93 const GrOptDrawState& optState, | |
94 GrGpu::DrawType drawType, | 91 GrGpu::DrawType drawType, |
95 bool hasGeometryProcessor, | 92 bool hasGeometryProcessor, |
96 GrGpuGL* gpu) { | 93 GrGpuGL* gpu) { |
97 if (desc.getHeader().fUseNvpr) { | 94 const GrProgramDesc& desc = optState.programDesc(); |
| 95 if (GrGLProgramDescBuilder::GetHeader(desc).fUseNvpr) { |
98 SkASSERT(gpu->glCaps().pathRenderingSupport()); | 96 SkASSERT(gpu->glCaps().pathRenderingSupport()); |
| 97 SkASSERT(GrProgramDesc::kAttribute_ColorInput != desc.header().fColorInp
ut); |
| 98 SkASSERT(GrProgramDesc::kAttribute_ColorInput != desc.header().fCoverage
Input); |
99 SkASSERT(!hasGeometryProcessor); | 99 SkASSERT(!hasGeometryProcessor); |
100 if (gpu->glPathRendering()->texturingMode() == | 100 if (gpu->glPathRendering()->texturingMode() == |
101 GrGLPathRendering::FixedFunction_TexturingMode) { | 101 GrGLPathRendering::FixedFunction_TexturingMode) { |
102 return SkNEW_ARGS(GrGLLegacyNvprProgramBuilder, (gpu, optState, desc
)); | 102 return SkNEW_ARGS(GrGLLegacyNvprProgramBuilder, (gpu, optState)); |
103 } else { | 103 } else { |
104 return SkNEW_ARGS(GrGLNvprProgramBuilder, (gpu, optState, desc)); | 104 return SkNEW_ARGS(GrGLNvprProgramBuilder, (gpu, optState)); |
105 } | 105 } |
106 } else { | 106 } else { |
107 return SkNEW_ARGS(GrGLProgramBuilder, (gpu, optState, desc)); | 107 return SkNEW_ARGS(GrGLProgramBuilder, (gpu, optState)); |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 ///////////////////////////////////////////////////////////////////////////// | 111 ///////////////////////////////////////////////////////////////////////////// |
112 | 112 |
113 GrGLProgramBuilder::GrGLProgramBuilder(GrGpuGL* gpu, | 113 GrGLProgramBuilder::GrGLProgramBuilder(GrGpuGL* gpu, const GrOptDrawState& optSt
ate) |
114 const GrOptDrawState& optState, | |
115 const GrGLProgramDesc& desc) | |
116 : fVS(this) | 114 : fVS(this) |
117 , fGS(this) | 115 , fGS(this) |
118 , fFS(this, desc) | 116 , fFS(this, optState.programDesc().header().fFragPosKey) |
119 , fOutOfStage(true) | 117 , fOutOfStage(true) |
120 , fStageIndex(-1) | 118 , fStageIndex(-1) |
121 , fGeometryProcessor(NULL) | 119 , fGeometryProcessor(NULL) |
122 , fOptState(optState) | 120 , fOptState(optState) |
123 , fDesc(desc) | 121 , fDesc(optState.programDesc()) |
124 , fGpu(gpu) | 122 , fGpu(gpu) |
125 , fUniforms(kVarsPerBlock) { | 123 , fUniforms(kVarsPerBlock) { |
126 } | 124 } |
127 | 125 |
128 void GrGLProgramBuilder::addVarying(const char* name, | 126 void GrGLProgramBuilder::addVarying(const char* name, |
129 GrGLVarying* varying, | 127 GrGLVarying* varying, |
130 GrGLShaderVar::Precision fsPrecision) { | 128 GrGLShaderVar::Precision fsPrecision) { |
131 SkASSERT(varying); | 129 SkASSERT(varying); |
132 if (varying->vsVarying()) { | 130 if (varying->vsVarying()) { |
133 fVS.addVarying(name, varying); | 131 fVS.addVarying(name, varying); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 } | 193 } |
196 } | 194 } |
197 } | 195 } |
198 | 196 |
199 const GrGLContextInfo& GrGLProgramBuilder::ctxInfo() const { | 197 const GrGLContextInfo& GrGLProgramBuilder::ctxInfo() const { |
200 return fGpu->ctxInfo(); | 198 return fGpu->ctxInfo(); |
201 } | 199 } |
202 | 200 |
203 void GrGLProgramBuilder::setupUniformColorAndCoverageIfNeeded(GrGLSLExpr4* input
Color, | 201 void GrGLProgramBuilder::setupUniformColorAndCoverageIfNeeded(GrGLSLExpr4* input
Color, |
204 GrGLSLExpr4* input
Coverage) { | 202 GrGLSLExpr4* input
Coverage) { |
205 const GrGLProgramDesc::KeyHeader& header = this->header(); | 203 const GrProgramDesc::KeyHeader& header = this->header(); |
206 if (GrGLProgramDesc::kUniform_ColorInput == header.fColorInput) { | 204 if (GrProgramDesc::kUniform_ColorInput == header.fColorInput) { |
207 const char* name; | 205 const char* name; |
208 fUniformHandles.fColorUni = | 206 fUniformHandles.fColorUni = |
209 this->addUniform(GrGLProgramBuilder::kFragment_Visibility, | 207 this->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
210 kVec4f_GrSLType, | 208 kVec4f_GrSLType, |
211 "Color", | 209 "Color", |
212 &name); | 210 &name); |
213 *inputColor = GrGLSLExpr4(name); | 211 *inputColor = GrGLSLExpr4(name); |
214 } else if (GrGLProgramDesc::kAllOnes_ColorInput == header.fColorInput) { | 212 } else if (GrProgramDesc::kAllOnes_ColorInput == header.fColorInput) { |
215 *inputColor = GrGLSLExpr4(1); | 213 *inputColor = GrGLSLExpr4(1); |
216 } | 214 } |
217 if (GrGLProgramDesc::kUniform_ColorInput == header.fCoverageInput) { | 215 if (GrProgramDesc::kUniform_ColorInput == header.fCoverageInput) { |
218 const char* name; | 216 const char* name; |
219 fUniformHandles.fCoverageUni = | 217 fUniformHandles.fCoverageUni = |
220 this->addUniform(GrGLProgramBuilder::kFragment_Visibility, | 218 this->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
221 kVec4f_GrSLType, | 219 kVec4f_GrSLType, |
222 "Coverage", | 220 "Coverage", |
223 &name); | 221 &name); |
224 *inputCoverage = GrGLSLExpr4(name); | 222 *inputCoverage = GrGLSLExpr4(name); |
225 } else if (GrGLProgramDesc::kAllOnes_ColorInput == header.fCoverageInput) { | 223 } else if (GrProgramDesc::kAllOnes_ColorInput == header.fCoverageInput) { |
226 *inputCoverage = GrGLSLExpr4(1); | 224 *inputCoverage = GrGLSLExpr4(1); |
227 } | 225 } |
228 } | 226 } |
229 | 227 |
230 void GrGLProgramBuilder::emitAndInstallProcs(const GrOptDrawState& optState, | 228 void GrGLProgramBuilder::emitAndInstallProcs(const GrOptDrawState& optState, |
231 GrGLSLExpr4* inputColor, | 229 GrGLSLExpr4* inputColor, |
232 GrGLSLExpr4* inputCoverage) { | 230 GrGLSLExpr4* inputCoverage) { |
233 fFragmentProcessors.reset(SkNEW(GrGLInstalledFragProcs)); | 231 fFragmentProcessors.reset(SkNEW(GrGLInstalledFragProcs)); |
234 int numProcs = optState.numFragmentStages(); | 232 int numProcs = optState.numFragmentStages(); |
235 this->emitAndInstallFragProcs(0, optState.numColorStages(), inputColor); | 233 this->emitAndInstallFragProcs(0, optState.numColorStages(), inputColor); |
236 if (optState.hasGeometryProcessor()) { | 234 if (optState.hasGeometryProcessor()) { |
237 const GrGeometryProcessor& gp = *optState.getGeometryProcessor(); | 235 const GrGeometryProcessor& gp = *optState.getGeometryProcessor(); |
238 fVS.emitAttributes(gp); | 236 fVS.emitAttributes(gp); |
239 ProcKeyProvider keyProvider(&fDesc, ProcKeyProvider::kGeometry_Processor
Type); | 237 ProcKeyProvider keyProvider(&fDesc, |
| 238 ProcKeyProvider::kGeometry_ProcessorType, |
| 239 GrGLProgramDescBuilder::kProcessorKeyOffsets
AndLengthOffset); |
240 GrGLSLExpr4 output; | 240 GrGLSLExpr4 output; |
241 this->emitAndInstallProc<GrGeometryProcessor>(gp, 0, keyProvider, *input
Coverage, &output); | 241 this->emitAndInstallProc<GrGeometryProcessor>(gp, 0, keyProvider, *input
Coverage, &output); |
242 *inputCoverage = output; | 242 *inputCoverage = output; |
243 } | 243 } |
244 this->emitAndInstallFragProcs(optState.numColorStages(), numProcs, inputCov
erage); | 244 this->emitAndInstallFragProcs(optState.numColorStages(), numProcs, inputCov
erage); |
245 } | 245 } |
246 | 246 |
247 void GrGLProgramBuilder::emitAndInstallFragProcs(int procOffset, int numProcs, G
rGLSLExpr4* inOut) { | 247 void GrGLProgramBuilder::emitAndInstallFragProcs(int procOffset, int numProcs, G
rGLSLExpr4* inOut) { |
248 ProcKeyProvider keyProvider(&fDesc, ProcKeyProvider::kFragment_ProcessorType
); | 248 ProcKeyProvider keyProvider(&fDesc, |
| 249 ProcKeyProvider::kFragment_ProcessorType, |
| 250 GrGLProgramDescBuilder::kProcessorKeyOffsetsAndL
engthOffset); |
249 for (int e = procOffset; e < numProcs; ++e) { | 251 for (int e = procOffset; e < numProcs; ++e) { |
250 GrGLSLExpr4 output; | 252 GrGLSLExpr4 output; |
251 const GrFragmentStage& stage = fOptState.getFragmentStage(e); | 253 const GrFragmentStage& stage = fOptState.getFragmentStage(e); |
252 this->emitAndInstallProc<GrFragmentStage>(stage, e, keyProvider, *inOut,
&output); | 254 this->emitAndInstallProc<GrFragmentStage>(stage, e, keyProvider, *inOut,
&output); |
253 *inOut = output; | 255 *inOut = output; |
254 } | 256 } |
255 } | 257 } |
256 | 258 |
257 // TODO Processors cannot output zeros because an empty string is all 1s | 259 // TODO Processors cannot output zeros because an empty string is all 1s |
258 // the fix is to allow effects to take the GrGLSLExpr4 directly | 260 // the fix is to allow effects to take the GrGLSLExpr4 directly |
259 template <class Proc> | 261 template <class Proc> |
260 void GrGLProgramBuilder::emitAndInstallProc(const Proc& proc, | 262 void GrGLProgramBuilder::emitAndInstallProc(const Proc& proc, |
261 int index, | 263 int index, |
262 const ProcKeyProvider keyProvider, | 264 const ProcKeyProvider& keyProvider, |
263 const GrGLSLExpr4& input, | 265 const GrGLSLExpr4& input, |
264 GrGLSLExpr4* output) { | 266 GrGLSLExpr4* output) { |
265 // Program builders have a bit of state we need to clear with each effect | 267 // Program builders have a bit of state we need to clear with each effect |
266 AutoStageAdvance adv(this); | 268 AutoStageAdvance adv(this); |
267 | 269 |
268 // create var to hold stage result | 270 // create var to hold stage result |
269 SkString outColorName; | 271 SkString outColorName; |
270 this->nameVariable(&outColorName, '\0', "output"); | 272 this->nameVariable(&outColorName, '\0', "output"); |
271 fFS.codeAppendf("vec4 %s;", outColorName.c_str()); | 273 fFS.codeAppendf("vec4 %s;", outColorName.c_str()); |
272 *output = outColorName; | 274 *output = outColorName; |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 if (0 == programID) { | 414 if (0 == programID) { |
413 return NULL; | 415 return NULL; |
414 } | 416 } |
415 | 417 |
416 // compile shaders and bind attributes / uniforms | 418 // compile shaders and bind attributes / uniforms |
417 SkTDArray<GrGLuint> shadersToDelete; | 419 SkTDArray<GrGLuint> shadersToDelete; |
418 if (!fFS.compileAndAttachShaders(programID, &shadersToDelete)) { | 420 if (!fFS.compileAndAttachShaders(programID, &shadersToDelete)) { |
419 this->cleanupProgram(programID, shadersToDelete); | 421 this->cleanupProgram(programID, shadersToDelete); |
420 return NULL; | 422 return NULL; |
421 } | 423 } |
422 if (!(this->header().fUseNvpr && | 424 if (!(GrGLProgramDescBuilder::GetHeader(fDesc).fUseNvpr && |
423 fGpu->glPathRendering()->texturingMode() == | 425 fGpu->glPathRendering()->texturingMode() == |
424 GrGLPathRendering::FixedFunction_TexturingMode)) { | 426 GrGLPathRendering::FixedFunction_TexturingMode)) { |
425 if (!fVS.compileAndAttachShaders(programID, &shadersToDelete)) { | 427 if (!fVS.compileAndAttachShaders(programID, &shadersToDelete)) { |
426 this->cleanupProgram(programID, shadersToDelete); | 428 this->cleanupProgram(programID, shadersToDelete); |
427 return NULL; | 429 return NULL; |
428 } | 430 } |
429 fVS.bindVertexAttributes(programID); | 431 fVS.bindVertexAttributes(programID); |
430 } | 432 } |
431 bool usingBindUniform = fGpu->glInterface()->fFunctions.fBindUniformLocation
!= NULL; | 433 bool usingBindUniform = fGpu->glInterface()->fFunctions.fBindUniformLocation
!= NULL; |
432 if (usingBindUniform) { | 434 if (usingBindUniform) { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 } | 511 } |
510 | 512 |
511 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 513 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
512 | 514 |
513 GrGLInstalledFragProcs::~GrGLInstalledFragProcs() { | 515 GrGLInstalledFragProcs::~GrGLInstalledFragProcs() { |
514 int numProcs = fProcs.count(); | 516 int numProcs = fProcs.count(); |
515 for (int e = 0; e < numProcs; ++e) { | 517 for (int e = 0; e < numProcs; ++e) { |
516 SkDELETE(fProcs[e]); | 518 SkDELETE(fProcs[e]); |
517 } | 519 } |
518 } | 520 } |
OLD | NEW |