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 19 matching lines...) Expand all Loading... |
30 // create a builder. This will be handed off to effects so they can use it
to add | 30 // create a builder. This will be handed off to effects so they can use it
to add |
31 // uniforms, varyings, textures, etc | 31 // uniforms, varyings, textures, etc |
32 SkAutoTDelete<GrGLProgramBuilder> builder(CreateProgramBuilder(optState, | 32 SkAutoTDelete<GrGLProgramBuilder> builder(CreateProgramBuilder(optState, |
33 optState.hasG
eometryProcessor(), | 33 optState.hasG
eometryProcessor(), |
34 gpu)); | 34 gpu)); |
35 | 35 |
36 GrGLProgramBuilder* pb = builder.get(); | 36 GrGLProgramBuilder* pb = builder.get(); |
37 const GrGLProgramDescBuilder::GLKeyHeader& header = GrGLProgramDescBuilder::
GetHeader(pb->desc()); | 37 const GrGLProgramDescBuilder::GLKeyHeader& header = GrGLProgramDescBuilder::
GetHeader(pb->desc()); |
38 | 38 |
39 // emit code to read the dst copy texture, if necessary | 39 // emit code to read the dst copy texture, if necessary |
40 if (GrGLFragmentShaderBuilder::kNoDstRead_DstReadKey != header.fDstReadKey | 40 if (GrGLFragmentShaderBuilder::kNoDstRead_DstReadKey != header.fDstReadKey &
& |
41 && !gpu->glCaps().fbFetchSupport()) { | 41 !gpu->glCaps().fbFetchSupport()) { |
42 pb->fFS.emitCodeToReadDstTexture(); | 42 pb->fFS.emitCodeToReadDstTexture(); |
43 } | 43 } |
44 | 44 |
45 // get the initial color and coverage to feed into the first effect in each
effect chain | 45 // TODO: Once all stages can handle taking a float or vec4 and correctly han
dling them we can |
| 46 // seed correctly here |
46 GrGLSLExpr4 inputColor; | 47 GrGLSLExpr4 inputColor; |
47 GrGLSLExpr1 inputCoverage; | 48 GrGLSLExpr4 inputCoverage; |
48 pb->setupUniformColorAndCoverageIfNeeded(&inputColor, &inputCoverage); | |
49 | 49 |
50 // TODO: Once all stages can handle taking a float or vec4 and correctly han
dling them we can | 50 pb->emitAndInstallProcs(&inputColor, &inputCoverage); |
51 // remove this cast to a vec4. | |
52 GrGLSLExpr4 inputCoverageVec4; | |
53 if (inputCoverage.isValid()) { | |
54 inputCoverageVec4 = GrGLSLExpr4::VectorCast(inputCoverage); | |
55 } | |
56 | |
57 pb->emitAndInstallProcs(&inputColor, &inputCoverageVec4); | |
58 | 51 |
59 return pb->finalize(); | 52 return pb->finalize(); |
60 } | 53 } |
61 | 54 |
62 GrGLProgramBuilder* GrGLProgramBuilder::CreateProgramBuilder(const GrOptDrawStat
e& optState, | 55 GrGLProgramBuilder* GrGLProgramBuilder::CreateProgramBuilder(const GrOptDrawStat
e& optState, |
63 bool hasGeometryPro
cessor, | 56 bool hasGeometryPro
cessor, |
64 GrGpuGL* gpu) { | 57 GrGpuGL* gpu) { |
65 const GrProgramDesc& desc = optState.programDesc(); | 58 const GrProgramDesc& desc = optState.programDesc(); |
66 if (GrGLProgramDescBuilder::GetHeader(desc).fUseNvpr) { | 59 if (GrGLProgramDescBuilder::GetHeader(desc).fUseNvpr) { |
67 SkASSERT(gpu->glCaps().pathRenderingSupport()); | 60 SkASSERT(gpu->glCaps().pathRenderingSupport()); |
68 SkASSERT(GrProgramDesc::kAttribute_ColorInput != desc.header().fColorInp
ut); | |
69 SkASSERT(GrProgramDesc::kAttribute_ColorInput != desc.header().fCoverage
Input); | |
70 SkASSERT(!hasGeometryProcessor); | 61 SkASSERT(!hasGeometryProcessor); |
71 if (gpu->glPathRendering()->texturingMode() == | 62 if (gpu->glPathRendering()->texturingMode() == |
72 GrGLPathRendering::FixedFunction_TexturingMode) { | 63 GrGLPathRendering::FixedFunction_TexturingMode) { |
73 return SkNEW_ARGS(GrGLLegacyNvprProgramBuilder, (gpu, optState)); | 64 return SkNEW_ARGS(GrGLLegacyNvprProgramBuilder, (gpu, optState)); |
74 } else { | 65 } else { |
75 return SkNEW_ARGS(GrGLNvprProgramBuilder, (gpu, optState)); | 66 return SkNEW_ARGS(GrGLNvprProgramBuilder, (gpu, optState)); |
76 } | 67 } |
77 } else { | 68 } else { |
78 return SkNEW_ARGS(GrGLProgramBuilder, (gpu, optState)); | 69 return SkNEW_ARGS(GrGLProgramBuilder, (gpu, optState)); |
79 } | 70 } |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 fUniforms[i].fVariable.appendDecl(this->ctxInfo(), out); | 169 fUniforms[i].fVariable.appendDecl(this->ctxInfo(), out); |
179 out->append(";\n"); | 170 out->append(";\n"); |
180 } | 171 } |
181 } | 172 } |
182 } | 173 } |
183 | 174 |
184 const GrGLContextInfo& GrGLProgramBuilder::ctxInfo() const { | 175 const GrGLContextInfo& GrGLProgramBuilder::ctxInfo() const { |
185 return fGpu->ctxInfo(); | 176 return fGpu->ctxInfo(); |
186 } | 177 } |
187 | 178 |
188 void GrGLProgramBuilder::setupUniformColorAndCoverageIfNeeded(GrGLSLExpr4* input
Color, | |
189 GrGLSLExpr1* input
Coverage) { | |
190 const GrProgramDesc::KeyHeader& header = this->header(); | |
191 if (GrProgramDesc::kUniform_ColorInput == header.fColorInput) { | |
192 const char* name; | |
193 fUniformHandles.fColorUni = | |
194 this->addUniform(GrGLProgramBuilder::kFragment_Visibility, | |
195 kVec4f_GrSLType, kDefault_GrSLPrecision, | |
196 "Color", &name); | |
197 *inputColor = GrGLSLExpr4(name); | |
198 } else if (GrProgramDesc::kAllOnes_ColorInput == header.fColorInput) { | |
199 *inputColor = GrGLSLExpr4(1); | |
200 } | |
201 if (GrProgramDesc::kUniform_ColorInput == header.fCoverageInput) { | |
202 const char* name; | |
203 fUniformHandles.fCoverageUni = | |
204 this->addUniform(GrGLProgramBuilder::kFragment_Visibility, | |
205 kFloat_GrSLType, kDefault_GrSLPrecision, | |
206 "Coverage",&name); | |
207 *inputCoverage = GrGLSLExpr1(name); | |
208 } else if (GrProgramDesc::kAllOnes_ColorInput == header.fCoverageInput) { | |
209 *inputCoverage = GrGLSLExpr1(1); | |
210 } | |
211 } | |
212 | |
213 void GrGLProgramBuilder::emitAndInstallProcs(GrGLSLExpr4* inputColor, GrGLSLExpr
4* inputCoverage) { | 179 void GrGLProgramBuilder::emitAndInstallProcs(GrGLSLExpr4* inputColor, GrGLSLExpr
4* inputCoverage) { |
214 if (fOptState.hasGeometryProcessor()) { | 180 if (fOptState.hasGeometryProcessor()) { |
215 fVS.setupUniformViewMatrix(); | 181 fVS.setupUniformViewMatrix(); |
216 | 182 |
217 const GrProgramDesc::KeyHeader& header = this->header(); | |
218 fVS.codeAppend("gl_PointSize = 1.0;"); | 183 fVS.codeAppend("gl_PointSize = 1.0;"); |
219 | 184 |
220 // Setup position | 185 // Setup position |
221 // TODO it'd be possible to remove these from the vertexshader builder a
nd have them | 186 // TODO it'd be possible to remove these from the vertexshader builder a
nd have them |
222 // be outputs from the emit call. We don't do this because emitargs is
constant. It would | 187 // be outputs from the emit call. We don't do this because emitargs is
constant. It would |
223 // be easy to change this though | 188 // be easy to change this though |
224 fVS.codeAppendf("vec3 %s;", fVS.glPosition()); | 189 fVS.codeAppendf("vec3 %s;", fVS.glPosition()); |
225 fVS.codeAppendf("vec2 %s;", fVS.positionCoords()); | 190 fVS.codeAppendf("vec2 %s;", fVS.positionCoords()); |
226 fVS.codeAppendf("vec2 %s;", fVS.localCoords()); | 191 fVS.codeAppendf("vec2 %s;", fVS.localCoords()); |
227 | 192 |
228 const GrGeometryProcessor& gp = *fOptState.getGeometryProcessor(); | 193 const GrGeometryProcessor& gp = *fOptState.getGeometryProcessor(); |
229 fVS.emitAttributes(gp); | 194 fVS.emitAttributes(gp); |
230 GrGLSLExpr4 outputColor; | 195 } |
231 GrGLSLExpr4 outputCoverage; | |
232 this->emitAndInstallProc(gp, &outputColor, &outputCoverage); | |
233 | 196 |
234 // We may override color and coverage here if we have unform color or co
verage. This is | 197 const GrPrimitiveProcessor& primProc = *fOptState.getPrimitiveProcessor(); |
235 // obviously not ideal. | 198 this->emitAndInstallProc(primProc, inputColor, inputCoverage); |
236 // TODO lets the GP itself do the override | |
237 if (GrProgramDesc::kAttribute_ColorInput == header.fColorInput) { | |
238 *inputColor = outputColor; | |
239 } | |
240 | |
241 // We may have uniform coverage, if so we need to multiply the GPs outpu
t by the uniform | |
242 // coverage | |
243 if (GrProgramDesc::kUniform_ColorInput == header.fCoverageInput) { | |
244 fFS.codeAppendf("%s *= %s;", outputCoverage.c_str(), inputCoverage->
c_str()); | |
245 } | |
246 *inputCoverage = outputCoverage; | |
247 } | |
248 | 199 |
249 fFragmentProcessors.reset(SkNEW(GrGLInstalledFragProcs)); | 200 fFragmentProcessors.reset(SkNEW(GrGLInstalledFragProcs)); |
250 int numProcs = fOptState.numFragmentStages(); | 201 int numProcs = fOptState.numFragmentStages(); |
251 this->emitAndInstallFragProcs(0, fOptState.numColorStages(), inputColor); | 202 this->emitAndInstallFragProcs(0, fOptState.numColorStages(), inputColor); |
252 this->emitAndInstallFragProcs(fOptState.numColorStages(), numProcs, inputCo
verage); | 203 this->emitAndInstallFragProcs(fOptState.numColorStages(), numProcs, inputCo
verage); |
253 | 204 |
254 if (fOptState.hasGeometryProcessor()) { | 205 if (fOptState.hasGeometryProcessor()) { |
255 fVS.transformToNormalizedDeviceSpace(); | 206 fVS.transformToNormalizedDeviceSpace(); |
256 } | 207 } |
257 | 208 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 // Enclose custom code in a block to avoid namespace conflicts | 247 // Enclose custom code in a block to avoid namespace conflicts |
297 SkString openBrace; | 248 SkString openBrace; |
298 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); | 249 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); |
299 fFS.codeAppend(openBrace.c_str()); | 250 fFS.codeAppend(openBrace.c_str()); |
300 | 251 |
301 this->emitAndInstallProc(proc, output->c_str(), input.isOnes() ? NULL : inpu
t.c_str()); | 252 this->emitAndInstallProc(proc, output->c_str(), input.isOnes() ? NULL : inpu
t.c_str()); |
302 | 253 |
303 fFS.codeAppend("}"); | 254 fFS.codeAppend("}"); |
304 } | 255 } |
305 | 256 |
306 void GrGLProgramBuilder::emitAndInstallProc(const GrGeometryProcessor& proc, | 257 void GrGLProgramBuilder::emitAndInstallProc(const GrPrimitiveProcessor& proc, |
307 GrGLSLExpr4* outputColor, | 258 GrGLSLExpr4* outputColor, |
308 GrGLSLExpr4* outputCoverage) { | 259 GrGLSLExpr4* outputCoverage) { |
309 // Program builders have a bit of state we need to clear with each effect | 260 // Program builders have a bit of state we need to clear with each effect |
310 AutoStageAdvance adv(this); | 261 AutoStageAdvance adv(this); |
311 this->nameExpression(outputColor, "outputColor"); | 262 this->nameExpression(outputColor, "outputColor"); |
312 this->nameExpression(outputCoverage, "outputCoverage"); | 263 this->nameExpression(outputCoverage, "outputCoverage"); |
313 | 264 |
314 // Enclose custom code in a block to avoid namespace conflicts | 265 // Enclose custom code in a block to avoid namespace conflicts |
315 SkString openBrace; | 266 SkString openBrace; |
316 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); | 267 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); |
(...skipping 20 matching lines...) Expand all Loading... |
337 this->emitTransforms(fs, &coords, ifp); | 288 this->emitTransforms(fs, &coords, ifp); |
338 | 289 |
339 ifp->fGLProc->emitCode(this, fp, outColor, inColor, coords, samplers); | 290 ifp->fGLProc->emitCode(this, fp, outColor, inColor, coords, samplers); |
340 | 291 |
341 // We have to check that effects and the code they emit are consistent, ie i
f an effect | 292 // We have to check that effects and the code they emit are consistent, ie i
f an effect |
342 // asks for dst color, then the emit code needs to follow suit | 293 // asks for dst color, then the emit code needs to follow suit |
343 verify(fp); | 294 verify(fp); |
344 fFragmentProcessors->fProcs.push_back(ifp); | 295 fFragmentProcessors->fProcs.push_back(ifp); |
345 } | 296 } |
346 | 297 |
347 void GrGLProgramBuilder::emitAndInstallProc(const GrGeometryProcessor& gp, | 298 void GrGLProgramBuilder::emitAndInstallProc(const GrPrimitiveProcessor& gp, |
348 const char* outColor, | 299 const char* outColor, |
349 const char* outCoverage) { | 300 const char* outCoverage) { |
350 SkASSERT(!fGeometryProcessor); | 301 SkASSERT(!fGeometryProcessor); |
351 fGeometryProcessor = SkNEW(GrGLInstalledGeoProc); | 302 fGeometryProcessor = SkNEW(GrGLInstalledGeoProc); |
352 | 303 |
353 const GrBatchTracker& bt = fOptState.getBatchTracker(); | 304 const GrBatchTracker& bt = fOptState.getBatchTracker(); |
354 fGeometryProcessor->fGLProc.reset(gp.createGLInstance(bt)); | 305 fGeometryProcessor->fGLProc.reset(gp.createGLInstance(bt)); |
355 | 306 |
356 SkSTArray<4, GrGLProcessor::TextureSampler> samplers(gp.numTextures()); | 307 SkSTArray<4, GrGLProcessor::TextureSampler> samplers(gp.numTextures()); |
357 this->emitSamplers(gp, &samplers, fGeometryProcessor); | 308 this->emitSamplers(gp, &samplers, fGeometryProcessor); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 fFS.getPrimaryColorOutputName(), | 347 fFS.getPrimaryColorOutputName(), |
397 fFS.getSecondaryColorOutputName(), samplers
); | 348 fFS.getSecondaryColorOutputName(), samplers
); |
398 fXferProcessor->fGLProc->emitCode(args); | 349 fXferProcessor->fGLProc->emitCode(args); |
399 | 350 |
400 // We have to check that effects and the code they emit are consistent, ie i
f an effect | 351 // We have to check that effects and the code they emit are consistent, ie i
f an effect |
401 // asks for dst color, then the emit code needs to follow suit | 352 // asks for dst color, then the emit code needs to follow suit |
402 verify(xp); | 353 verify(xp); |
403 fFS.codeAppend("}"); | 354 fFS.codeAppend("}"); |
404 } | 355 } |
405 | 356 |
406 void GrGLProgramBuilder::verify(const GrGeometryProcessor& gp) { | 357 void GrGLProgramBuilder::verify(const GrPrimitiveProcessor& gp) { |
407 SkASSERT(fFS.hasReadFragmentPosition() == gp.willReadFragmentPosition()); | 358 SkASSERT(fFS.hasReadFragmentPosition() == gp.willReadFragmentPosition()); |
408 } | 359 } |
409 | 360 |
410 void GrGLProgramBuilder::verify(const GrXferProcessor& xp) { | 361 void GrGLProgramBuilder::verify(const GrXferProcessor& xp) { |
411 // TODO: Once will readDst is only xp enable this assert and remove it from
the | 362 // TODO: Once will readDst is only xp enable this assert and remove it from
the |
412 // FragmentProcessor verify() | 363 // FragmentProcessor verify() |
413 //SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor()); | 364 //SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor()); |
414 } | 365 } |
415 | 366 |
416 void GrGLProgramBuilder::verify(const GrFragmentProcessor& fp) { | 367 void GrGLProgramBuilder::verify(const GrFragmentProcessor& fp) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 GrGLProgram* GrGLProgramBuilder::finalize() { | 433 GrGLProgram* GrGLProgramBuilder::finalize() { |
483 // verify we can get a program id | 434 // verify we can get a program id |
484 GrGLuint programID; | 435 GrGLuint programID; |
485 GL_CALL_RET(programID, CreateProgram()); | 436 GL_CALL_RET(programID, CreateProgram()); |
486 if (0 == programID) { | 437 if (0 == programID) { |
487 return NULL; | 438 return NULL; |
488 } | 439 } |
489 | 440 |
490 // compile shaders and bind attributes / uniforms | 441 // compile shaders and bind attributes / uniforms |
491 SkTDArray<GrGLuint> shadersToDelete; | 442 SkTDArray<GrGLuint> shadersToDelete; |
492 if (!fFS.compileAndAttachShaders(programID, &shadersToDelete)) { | |
493 this->cleanupProgram(programID, shadersToDelete); | |
494 return NULL; | |
495 } | |
496 | |
497 if (!(GrGLProgramDescBuilder::GetHeader(fDesc).fUseNvpr && | 443 if (!(GrGLProgramDescBuilder::GetHeader(fDesc).fUseNvpr && |
498 fGpu->glPathRendering()->texturingMode() == | 444 fGpu->glPathRendering()->texturingMode() == |
499 GrGLPathRendering::FixedFunction_TexturingMode)) { | 445 GrGLPathRendering::FixedFunction_TexturingMode)) { |
500 if (!fVS.compileAndAttachShaders(programID, &shadersToDelete)) { | 446 if (!fVS.compileAndAttachShaders(programID, &shadersToDelete)) { |
501 this->cleanupProgram(programID, shadersToDelete); | 447 this->cleanupProgram(programID, shadersToDelete); |
502 return NULL; | 448 return NULL; |
503 } | 449 } |
504 | 450 |
505 // Non fixed function NVPR actually requires a vertex shader to compile | 451 // Non fixed function NVPR actually requires a vertex shader to compile |
506 if (fOptState.hasGeometryProcessor()) { | 452 if (fOptState.hasGeometryProcessor()) { |
507 fVS.bindVertexAttributes(programID); | 453 fVS.bindVertexAttributes(programID); |
508 } | 454 } |
509 } | 455 } |
| 456 |
| 457 if (!fFS.compileAndAttachShaders(programID, &shadersToDelete)) { |
| 458 this->cleanupProgram(programID, shadersToDelete); |
| 459 return NULL; |
| 460 } |
| 461 |
510 bool usingBindUniform = fGpu->glInterface()->fFunctions.fBindUniformLocation
!= NULL; | 462 bool usingBindUniform = fGpu->glInterface()->fFunctions.fBindUniformLocation
!= NULL; |
511 if (usingBindUniform) { | 463 if (usingBindUniform) { |
512 this->bindUniformLocations(programID); | 464 this->bindUniformLocations(programID); |
513 } | 465 } |
514 fFS.bindFragmentShaderLocations(programID); | 466 fFS.bindFragmentShaderLocations(programID); |
515 GL_CALL(LinkProgram(programID)); | 467 GL_CALL(LinkProgram(programID)); |
516 | 468 |
517 // Calling GetProgramiv is expensive in Chromium. Assume success in release
builds. | 469 // Calling GetProgramiv is expensive in Chromium. Assume success in release
builds. |
518 bool checkLinked = !fGpu->ctxInfo().isChromium(); | 470 bool checkLinked = !fGpu->ctxInfo().isChromium(); |
519 #ifdef SK_DEBUG | 471 #ifdef SK_DEBUG |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
588 } | 540 } |
589 | 541 |
590 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 542 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
591 | 543 |
592 GrGLInstalledFragProcs::~GrGLInstalledFragProcs() { | 544 GrGLInstalledFragProcs::~GrGLInstalledFragProcs() { |
593 int numProcs = fProcs.count(); | 545 int numProcs = fProcs.count(); |
594 for (int e = 0; e < numProcs; ++e) { | 546 for (int e = 0; e < numProcs; ++e) { |
595 SkDELETE(fProcs[e]); | 547 SkDELETE(fProcs[e]); |
596 } | 548 } |
597 } | 549 } |
OLD | NEW |