| 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.fUseFragShaderOnly; | 56 bool hasVertexShader = !header.fUseFragShaderOnly; |
| 59 if (hasVertexShader) { | 57 if (hasVertexShader) { |
| 60 pb->fVS.setupLocalCoords(); | 58 pb->fVS.setupLocalCoords(); |
| 61 pb->fVS.transformGLToSkiaCoords(); | 59 pb->fVS.transformGLToSkiaCoords(); |
| 62 if (header.fEmitsPointSize) { | 60 if (header.fEmitsPointSize) { |
| 63 pb->fVS.codeAppend("gl_PointSize = 1.0;"); | 61 pb->fVS.codeAppend("gl_PointSize = 1.0;"); |
| 64 } | 62 } |
| 65 if (GrGLProgramDesc::kAttribute_ColorInput == header.fColorInput) { | 63 if (GrProgramDesc::kAttribute_ColorInput == header.fColorInput) { |
| 66 pb->fVS.setupBuiltinVertexAttribute("Color", &inputColor); | 64 pb->fVS.setupBuiltinVertexAttribute("Color", &inputColor); |
| 67 } | 65 } |
| 68 if (GrGLProgramDesc::kAttribute_ColorInput == header.fCoverageInput) { | 66 if (GrProgramDesc::kAttribute_ColorInput == header.fCoverageInput) { |
| 69 pb->fVS.setupBuiltinVertexAttribute("Coverage", &inputCoverage); | 67 pb->fVS.setupBuiltinVertexAttribute("Coverage", &inputCoverage); |
| 70 } | 68 } |
| 71 } | 69 } |
| 72 | 70 |
| 73 pb->emitAndInstallProcs(optState, &inputColor, &inputCoverage); | 71 pb->emitAndInstallProcs(optState, &inputColor, &inputCoverage); |
| 74 | 72 |
| 75 if (hasVertexShader) { | 73 if (hasVertexShader) { |
| 76 pb->fVS.transformSkiaToGLCoords(); | 74 pb->fVS.transformSkiaToGLCoords(); |
| 77 } | 75 } |
| 78 | 76 |
| 79 // write the secondary color output if necessary | 77 // write the secondary color output if necessary |
| 80 if (GrOptDrawState::kNone_SecondaryOutputType != header.fSecondaryOutputType
) { | 78 if (GrProgramDesc::kNone_SecondaryOutputType != header.fSecondaryOutputType)
{ |
| 81 pb->fFS.enableSecondaryOutput(inputColor, inputCoverage); | 79 pb->fFS.enableSecondaryOutput(inputColor, inputCoverage); |
| 82 } | 80 } |
| 83 | 81 |
| 84 pb->fFS.combineColorAndCoverage(inputColor, inputCoverage); | 82 pb->fFS.combineColorAndCoverage(inputColor, inputCoverage); |
| 85 | 83 |
| 86 return pb->finalize(); | 84 return pb->finalize(); |
| 87 } | 85 } |
| 88 | 86 |
| 89 GrGLProgramBuilder* | 87 GrGLProgramBuilder* |
| 90 GrGLProgramBuilder::CreateProgramBuilder(const GrGLProgramDesc& desc, | 88 GrGLProgramBuilder::CreateProgramBuilder(const GrOptDrawState& optState, |
| 91 const GrOptDrawState& optState, | |
| 92 GrGpu::DrawType drawType, | 89 GrGpu::DrawType drawType, |
| 93 bool hasGeometryProcessor, | 90 bool hasGeometryProcessor, |
| 94 GrGpuGL* gpu) { | 91 GrGpuGL* gpu) { |
| 95 if (desc.getHeader().fUseFragShaderOnly) { | 92 const GrProgramDesc& desc = optState.desc(); |
| 93 if (GrGLProgramDescBuilder::GetHeader(desc).fUseFragShaderOnly) { |
| 96 SkASSERT(gpu->glCaps().pathRenderingSupport()); | 94 SkASSERT(gpu->glCaps().pathRenderingSupport()); |
| 97 SkASSERT(gpu->glPathRendering()->texturingMode() == | 95 SkASSERT(gpu->glPathRendering()->texturingMode() == |
| 98 GrGLPathRendering::FixedFunction_TexturingMode); | 96 GrGLPathRendering::FixedFunction_TexturingMode); |
| 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 return SkNEW_ARGS(GrGLLegacyNvprProgramBuilder, (gpu, optState, desc)); | 100 return SkNEW_ARGS(GrGLLegacyNvprProgramBuilder, (gpu, optState)); |
| 101 } else if (GrGpu::IsPathRenderingDrawType(drawType)) { | 101 } else if (GrGpu::IsPathRenderingDrawType(drawType)) { |
| 102 SkASSERT(gpu->glCaps().pathRenderingSupport()); | 102 SkASSERT(gpu->glCaps().pathRenderingSupport()); |
| 103 SkASSERT(gpu->glPathRendering()->texturingMode() == | 103 SkASSERT(gpu->glPathRendering()->texturingMode() == |
| 104 GrGLPathRendering::SeparableShaders_TexturingMode); | 104 GrGLPathRendering::SeparableShaders_TexturingMode); |
| 105 SkASSERT(!hasGeometryProcessor); | 105 SkASSERT(!hasGeometryProcessor); |
| 106 return SkNEW_ARGS(GrGLNvprProgramBuilder, (gpu, optState, desc)); | 106 return SkNEW_ARGS(GrGLNvprProgramBuilder, (gpu, optState)); |
| 107 } else { | 107 } else { |
| 108 return SkNEW_ARGS(GrGLProgramBuilder, (gpu, optState, desc)); | 108 return SkNEW_ARGS(GrGLProgramBuilder, (gpu, optState)); |
| 109 } | 109 } |
| 110 } | 110 } |
| 111 | 111 |
| 112 ///////////////////////////////////////////////////////////////////////////// | 112 ///////////////////////////////////////////////////////////////////////////// |
| 113 | 113 |
| 114 GrGLProgramBuilder::GrGLProgramBuilder(GrGpuGL* gpu, | 114 GrGLProgramBuilder::GrGLProgramBuilder(GrGpuGL* gpu, const GrOptDrawState& optSt
ate) |
| 115 const GrOptDrawState& optState, | |
| 116 const GrGLProgramDesc& desc) | |
| 117 : fVS(this) | 115 : fVS(this) |
| 118 , fGS(this) | 116 , fGS(this) |
| 119 , fFS(this, desc) | 117 , fFS(this, optState.desc().header().fFragPosKey) |
| 120 , fOutOfStage(true) | 118 , fOutOfStage(true) |
| 121 , fStageIndex(-1) | 119 , fStageIndex(-1) |
| 122 , fGeometryProcessor(NULL) | 120 , fGeometryProcessor(NULL) |
| 123 , fOptState(optState) | 121 , fOptState(optState) |
| 124 , fDesc(desc) | 122 , fDesc(optState.desc()) |
| 125 , fGpu(gpu) | 123 , fGpu(gpu) |
| 126 , fUniforms(kVarsPerBlock) { | 124 , fUniforms(kVarsPerBlock) { |
| 127 } | 125 } |
| 128 | 126 |
| 129 void GrGLProgramBuilder::addVarying(GrSLType type, | 127 void GrGLProgramBuilder::addVarying(GrSLType type, |
| 130 const char* name, | 128 const char* name, |
| 131 const char** vsOutName, | 129 const char** vsOutName, |
| 132 const char** fsInName, | 130 const char** fsInName, |
| 133 GrGLShaderVar::Precision fsPrecision) { | 131 GrGLShaderVar::Precision fsPrecision) { |
| 134 SkString* fsInputName = fVS.addVarying(type, name, vsOutName); | 132 SkString* fsInputName = fVS.addVarying(type, name, vsOutName); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 } | 188 } |
| 191 } | 189 } |
| 192 } | 190 } |
| 193 | 191 |
| 194 const GrGLContextInfo& GrGLProgramBuilder::ctxInfo() const { | 192 const GrGLContextInfo& GrGLProgramBuilder::ctxInfo() const { |
| 195 return fGpu->ctxInfo(); | 193 return fGpu->ctxInfo(); |
| 196 } | 194 } |
| 197 | 195 |
| 198 void GrGLProgramBuilder::setupUniformColorAndCoverageIfNeeded(GrGLSLExpr4* input
Color, | 196 void GrGLProgramBuilder::setupUniformColorAndCoverageIfNeeded(GrGLSLExpr4* input
Color, |
| 199 GrGLSLExpr4* input
Coverage) { | 197 GrGLSLExpr4* input
Coverage) { |
| 200 const GrGLProgramDesc::KeyHeader& header = this->header(); | 198 const GrProgramDesc::KeyHeader& header = this->header(); |
| 201 if (GrGLProgramDesc::kUniform_ColorInput == header.fColorInput) { | 199 if (GrProgramDesc::kUniform_ColorInput == header.fColorInput) { |
| 202 const char* name; | 200 const char* name; |
| 203 fUniformHandles.fColorUni = | 201 fUniformHandles.fColorUni = |
| 204 this->addUniform(GrGLProgramBuilder::kFragment_Visibility, | 202 this->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 205 kVec4f_GrSLType, | 203 kVec4f_GrSLType, |
| 206 "Color", | 204 "Color", |
| 207 &name); | 205 &name); |
| 208 *inputColor = GrGLSLExpr4(name); | 206 *inputColor = GrGLSLExpr4(name); |
| 209 } else if (GrGLProgramDesc::kAllOnes_ColorInput == header.fColorInput) { | 207 } else if (GrProgramDesc::kAllOnes_ColorInput == header.fColorInput) { |
| 210 *inputColor = GrGLSLExpr4(1); | 208 *inputColor = GrGLSLExpr4(1); |
| 211 } | 209 } |
| 212 if (GrGLProgramDesc::kUniform_ColorInput == header.fCoverageInput) { | 210 if (GrProgramDesc::kUniform_ColorInput == header.fCoverageInput) { |
| 213 const char* name; | 211 const char* name; |
| 214 fUniformHandles.fCoverageUni = | 212 fUniformHandles.fCoverageUni = |
| 215 this->addUniform(GrGLProgramBuilder::kFragment_Visibility, | 213 this->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 216 kVec4f_GrSLType, | 214 kVec4f_GrSLType, |
| 217 "Coverage", | 215 "Coverage", |
| 218 &name); | 216 &name); |
| 219 *inputCoverage = GrGLSLExpr4(name); | 217 *inputCoverage = GrGLSLExpr4(name); |
| 220 } else if (GrGLProgramDesc::kAllOnes_ColorInput == header.fCoverageInput) { | 218 } else if (GrProgramDesc::kAllOnes_ColorInput == header.fCoverageInput) { |
| 221 *inputCoverage = GrGLSLExpr4(1); | 219 *inputCoverage = GrGLSLExpr4(1); |
| 222 } | 220 } |
| 223 } | 221 } |
| 224 | 222 |
| 225 void GrGLProgramBuilder::emitAndInstallProcs(const GrOptDrawState& optState, | 223 void GrGLProgramBuilder::emitAndInstallProcs(const GrOptDrawState& optState, |
| 226 GrGLSLExpr4* inputColor, | 224 GrGLSLExpr4* inputColor, |
| 227 GrGLSLExpr4* inputCoverage) { | 225 GrGLSLExpr4* inputCoverage) { |
| 228 fFragmentProcessors.reset(SkNEW(GrGLInstalledFragProcs)); | 226 fFragmentProcessors.reset(SkNEW(GrGLInstalledFragProcs)); |
| 229 int numProcs = optState.numFragmentStages(); | 227 int numProcs = optState.numFragmentStages(); |
| 230 this->emitAndInstallFragProcs(0, optState.numColorStages(), inputColor); | 228 this->emitAndInstallFragProcs(0, optState.numColorStages(), inputColor); |
| 231 if (optState.hasGeometryProcessor()) { | 229 if (optState.hasGeometryProcessor()) { |
| 232 const GrGeometryProcessor& gp = *optState.getGeometryProcessor(); | 230 const GrGeometryProcessor& gp = *optState.getGeometryProcessor(); |
| 233 fVS.emitAttributes(gp); | 231 fVS.emitAttributes(gp); |
| 234 ProcKeyProvider keyProvider(&fDesc, ProcKeyProvider::kGeometry_Processor
Type); | 232 ProcKeyProvider keyProvider(&fDesc, |
| 233 ProcKeyProvider::kGeometry_ProcessorType, |
| 234 GrGLProgramDescBuilder::kEffectKeyOffsetsAnd
LengthOffset); |
| 235 GrGLSLExpr4 output; | 235 GrGLSLExpr4 output; |
| 236 this->emitAndInstallProc<GrGeometryProcessor>(gp, 0, keyProvider, *input
Coverage, &output); | 236 this->emitAndInstallProc<GrGeometryProcessor>(gp, 0, keyProvider, *input
Coverage, &output); |
| 237 *inputCoverage = output; | 237 *inputCoverage = output; |
| 238 } | 238 } |
| 239 this->emitAndInstallFragProcs(optState.numColorStages(), numProcs, inputCov
erage); | 239 this->emitAndInstallFragProcs(optState.numColorStages(), numProcs, inputCov
erage); |
| 240 } | 240 } |
| 241 | 241 |
| 242 void GrGLProgramBuilder::emitAndInstallFragProcs(int procOffset, int numProcs, G
rGLSLExpr4* inOut) { | 242 void GrGLProgramBuilder::emitAndInstallFragProcs(int procOffset, int numProcs, G
rGLSLExpr4* inOut) { |
| 243 ProcKeyProvider keyProvider(&fDesc, ProcKeyProvider::kFragment_ProcessorType
); | 243 ProcKeyProvider keyProvider(&fDesc, |
| 244 ProcKeyProvider::kFragment_ProcessorType, |
| 245 GrGLProgramDescBuilder::kEffectKeyOffsetsAndLeng
thOffset); |
| 244 for (int e = procOffset; e < numProcs; ++e) { | 246 for (int e = procOffset; e < numProcs; ++e) { |
| 245 GrGLSLExpr4 output; | 247 GrGLSLExpr4 output; |
| 246 const GrFragmentStage& stage = fOptState.getFragmentStage(e); | 248 const GrFragmentStage& stage = fOptState.getFragmentStage(e); |
| 247 this->emitAndInstallProc<GrFragmentStage>(stage, e, keyProvider, *inOut,
&output); | 249 this->emitAndInstallProc<GrFragmentStage>(stage, e, keyProvider, *inOut,
&output); |
| 248 *inOut = output; | 250 *inOut = output; |
| 249 } | 251 } |
| 250 } | 252 } |
| 251 | 253 |
| 252 // TODO Processors cannot output zeros because an empty string is all 1s | 254 // TODO Processors cannot output zeros because an empty string is all 1s |
| 253 // the fix is to allow effects to take the GrGLSLExpr4 directly | 255 // the fix is to allow effects to take the GrGLSLExpr4 directly |
| 254 template <class Proc> | 256 template <class Proc> |
| 255 void GrGLProgramBuilder::emitAndInstallProc(const Proc& proc, | 257 void GrGLProgramBuilder::emitAndInstallProc(const Proc& proc, |
| 256 int index, | 258 int index, |
| 257 const ProcKeyProvider keyProvider, | 259 const ProcKeyProvider& keyProvider, |
| 258 const GrGLSLExpr4& input, | 260 const GrGLSLExpr4& input, |
| 259 GrGLSLExpr4* output) { | 261 GrGLSLExpr4* output) { |
| 260 // Program builders have a bit of state we need to clear with each effect | 262 // Program builders have a bit of state we need to clear with each effect |
| 261 AutoStageAdvance adv(this); | 263 AutoStageAdvance adv(this); |
| 262 | 264 |
| 263 // create var to hold stage result | 265 // create var to hold stage result |
| 264 SkString outColorName; | 266 SkString outColorName; |
| 265 this->nameVariable(&outColorName, '\0', "output"); | 267 this->nameVariable(&outColorName, '\0', "output"); |
| 266 fFS.codeAppendf("vec4 %s;", outColorName.c_str()); | 268 fFS.codeAppendf("vec4 %s;", outColorName.c_str()); |
| 267 *output = outColorName; | 269 *output = outColorName; |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 if (0 == programID) { | 410 if (0 == programID) { |
| 409 return NULL; | 411 return NULL; |
| 410 } | 412 } |
| 411 | 413 |
| 412 // compile shaders and bind attributes / uniforms | 414 // compile shaders and bind attributes / uniforms |
| 413 SkTDArray<GrGLuint> shadersToDelete; | 415 SkTDArray<GrGLuint> shadersToDelete; |
| 414 if (!fFS.compileAndAttachShaders(programID, &shadersToDelete)) { | 416 if (!fFS.compileAndAttachShaders(programID, &shadersToDelete)) { |
| 415 this->cleanupProgram(programID, shadersToDelete); | 417 this->cleanupProgram(programID, shadersToDelete); |
| 416 return NULL; | 418 return NULL; |
| 417 } | 419 } |
| 418 if (!this->header().fUseFragShaderOnly) { | 420 if (!GrGLProgramDescBuilder::GetHeader(fDesc).fUseFragShaderOnly) { |
| 419 if (!fVS.compileAndAttachShaders(programID, &shadersToDelete)) { | 421 if (!fVS.compileAndAttachShaders(programID, &shadersToDelete)) { |
| 420 this->cleanupProgram(programID, shadersToDelete); | 422 this->cleanupProgram(programID, shadersToDelete); |
| 421 return NULL; | 423 return NULL; |
| 422 } | 424 } |
| 423 fVS.bindVertexAttributes(programID); | 425 fVS.bindVertexAttributes(programID); |
| 424 } | 426 } |
| 425 bool usingBindUniform = fGpu->glInterface()->fFunctions.fBindUniformLocation
!= NULL; | 427 bool usingBindUniform = fGpu->glInterface()->fFunctions.fBindUniformLocation
!= NULL; |
| 426 if (usingBindUniform) { | 428 if (usingBindUniform) { |
| 427 this->bindUniformLocations(programID); | 429 this->bindUniformLocations(programID); |
| 428 } | 430 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 } | 505 } |
| 504 | 506 |
| 505 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 507 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 506 | 508 |
| 507 GrGLInstalledFragProcs::~GrGLInstalledFragProcs() { | 509 GrGLInstalledFragProcs::~GrGLInstalledFragProcs() { |
| 508 int numProcs = fProcs.count(); | 510 int numProcs = fProcs.count(); |
| 509 for (int e = 0; e < numProcs; ++e) { | 511 for (int e = 0; e < numProcs; ++e) { |
| 510 SkDELETE(fProcs[e]); | 512 SkDELETE(fProcs[e]); |
| 511 } | 513 } |
| 512 } | 514 } |
| OLD | NEW |