| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "glsl/GrGLSLProgramBuilder.h" | 8 #include "glsl/GrGLSLProgramBuilder.h" |
| 9 | 9 |
| 10 #include "GrPipeline.h" | 10 #include "GrPipeline.h" |
| 11 #include "glsl/GrGLSLFragmentProcessor.h" | 11 #include "glsl/GrGLSLFragmentProcessor.h" |
| 12 #include "glsl/GrGLSLGeometryProcessor.h" | 12 #include "glsl/GrGLSLGeometryProcessor.h" |
| 13 #include "glsl/GrGLSLVarying.h" | 13 #include "glsl/GrGLSLVarying.h" |
| 14 #include "glsl/GrGLSLXferProcessor.h" | 14 #include "glsl/GrGLSLXferProcessor.h" |
| 15 | 15 |
| 16 const int GrGLSLProgramBuilder::kVarsPerBlock = 8; | 16 const int GrGLSLProgramBuilder::kVarsPerBlock = 8; |
| 17 | 17 |
| 18 GrGLSLProgramBuilder::GrGLSLProgramBuilder(const GrPipeline& pipeline, | 18 GrGLSLProgramBuilder::GrGLSLProgramBuilder(const GrPipeline& pipeline, |
| 19 const GrPrimitiveProcessor& primProc, | 19 const GrPrimitiveProcessor& primProc, |
| 20 const GrProgramDesc& desc) | 20 const GrProgramDesc& desc) |
| 21 : fVS(this) | 21 : fVS(this) |
| 22 , fGS(this) | 22 , fGS(this) |
| 23 , fFS(this) | 23 , fFS(this) |
| 24 , fStageIndex(-1) | 24 , fStageIndex(-1) |
| 25 , fPipeline(pipeline) | 25 , fPipeline(pipeline) |
| 26 , fPrimProc(primProc) | 26 , fPrimProc(primProc) |
| 27 , fDesc(desc) | 27 , fDesc(desc) |
| 28 , fGeometryProcessor(nullptr) | 28 , fGeometryProcessor(nullptr) |
| 29 , fXferProcessor(nullptr) | 29 , fXferProcessor(nullptr) |
| 30 , fSamplerUniforms(4) | |
| 31 , fNumVertexSamplers(0) | 30 , fNumVertexSamplers(0) |
| 32 , fNumGeometrySamplers(0) | 31 , fNumGeometrySamplers(0) |
| 33 , fNumFragmentSamplers(0) { | 32 , fNumFragmentSamplers(0) { |
| 34 } | 33 } |
| 35 | 34 |
| 36 void GrGLSLProgramBuilder::addFeature(GrShaderFlags shaders, | 35 void GrGLSLProgramBuilder::addFeature(GrShaderFlags shaders, |
| 37 uint32_t featureBit, | 36 uint32_t featureBit, |
| 38 const char* extensionName) { | 37 const char* extensionName) { |
| 39 if (shaders & kVertex_GrShaderFlag) { | 38 if (shaders & kVertex_GrShaderFlag) { |
| 40 fVS.addFeature(featureBit, extensionName); | 39 fVS.addFeature(featureBit, extensionName); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 | 89 |
| 91 // Enclose custom code in a block to avoid namespace conflicts | 90 // Enclose custom code in a block to avoid namespace conflicts |
| 92 SkString openBrace; | 91 SkString openBrace; |
| 93 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); | 92 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); |
| 94 fFS.codeAppend(openBrace.c_str()); | 93 fFS.codeAppend(openBrace.c_str()); |
| 95 fVS.codeAppendf("// Primitive Processor %s\n", proc.name()); | 94 fVS.codeAppendf("// Primitive Processor %s\n", proc.name()); |
| 96 | 95 |
| 97 SkASSERT(!fGeometryProcessor); | 96 SkASSERT(!fGeometryProcessor); |
| 98 fGeometryProcessor = proc.createGLSLInstance(*this->glslCaps()); | 97 fGeometryProcessor = proc.createGLSLInstance(*this->glslCaps()); |
| 99 | 98 |
| 100 SkSTArray<4, GrGLSLSampler> texSamplers(proc.numTextures()); | 99 SkSTArray<4, SamplerHandle> texSamplers(proc.numTextures()); |
| 101 SkSTArray<2, GrGLSLSampler> bufferSamplers(proc.numBuffers()); | 100 SkSTArray<2, SamplerHandle> bufferSamplers(proc.numBuffers()); |
| 102 this->emitSamplers(proc, &texSamplers, &bufferSamplers); | 101 this->emitSamplers(proc, &texSamplers, &bufferSamplers); |
| 103 | 102 |
| 104 GrGLSLGeometryProcessor::EmitArgs args(&fVS, | 103 GrGLSLGeometryProcessor::EmitArgs args(&fVS, |
| 105 &fFS, | 104 &fFS, |
| 106 this->varyingHandler(), | 105 this->varyingHandler(), |
| 107 this->uniformHandler(), | 106 this->uniformHandler(), |
| 108 this->glslCaps(), | 107 this->glslCaps(), |
| 109 proc, | 108 proc, |
| 110 outputColor->c_str(), | 109 outputColor->c_str(), |
| 111 outputCoverage->c_str(), | 110 outputCoverage->c_str(), |
| 112 texSamplers, | 111 texSamplers.begin(), |
| 113 bufferSamplers, | 112 bufferSamplers.begin(), |
| 114 fCoordTransforms, | 113 fCoordTransforms, |
| 115 &fOutCoords); | 114 &fOutCoords); |
| 116 fGeometryProcessor->emitCode(args); | 115 fGeometryProcessor->emitCode(args); |
| 117 | 116 |
| 118 // We have to check that effects and the code they emit are consistent, ie i
f an effect | 117 // We have to check that effects and the code they emit are consistent, ie i
f an effect |
| 119 // asks for dst color, then the emit code needs to follow suit | 118 // asks for dst color, then the emit code needs to follow suit |
| 120 SkDEBUGCODE(verify(proc);) | 119 SkDEBUGCODE(verify(proc);) |
| 121 | 120 |
| 122 fFS.codeAppend("}"); | 121 fFS.codeAppend("}"); |
| 123 } | 122 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 143 AutoStageAdvance adv(this); | 142 AutoStageAdvance adv(this); |
| 144 this->nameExpression(output, "output"); | 143 this->nameExpression(output, "output"); |
| 145 | 144 |
| 146 // Enclose custom code in a block to avoid namespace conflicts | 145 // Enclose custom code in a block to avoid namespace conflicts |
| 147 SkString openBrace; | 146 SkString openBrace; |
| 148 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name()); | 147 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name()); |
| 149 fFS.codeAppend(openBrace.c_str()); | 148 fFS.codeAppend(openBrace.c_str()); |
| 150 | 149 |
| 151 GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance(); | 150 GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance(); |
| 152 | 151 |
| 153 SkSTArray<4, GrGLSLSampler> texSamplers(fp.numTextures()); | 152 SkSTArray<4, SamplerHandle> texSamplers(fp.numTextures()); |
| 154 SkSTArray<2, GrGLSLSampler> bufferSamplers(fp.numBuffers()); | 153 SkSTArray<2, SamplerHandle> bufferSamplers(fp.numBuffers()); |
| 155 this->emitSamplers(fp, &texSamplers, &bufferSamplers); | 154 this->emitSamplers(fp, &texSamplers, &bufferSamplers); |
| 156 | 155 |
| 157 GrGLSLFragmentProcessor::EmitArgs args(&fFS, | 156 GrGLSLFragmentProcessor::EmitArgs args(&fFS, |
| 158 this->uniformHandler(), | 157 this->uniformHandler(), |
| 159 this->glslCaps(), | 158 this->glslCaps(), |
| 160 fp, | 159 fp, |
| 161 output->c_str(), | 160 output->c_str(), |
| 162 input.isOnes() ? nullptr : input.c_st
r(), | 161 input.isOnes() ? nullptr : input.c_st
r(), |
| 163 fOutCoords[index], | 162 fOutCoords[index], |
| 164 texSamplers, | 163 texSamplers.begin(), |
| 165 bufferSamplers); | 164 bufferSamplers.begin()); |
| 166 fragProc->emitCode(args); | 165 fragProc->emitCode(args); |
| 167 | 166 |
| 168 // We have to check that effects and the code they emit are consistent, ie i
f an effect | 167 // We have to check that effects and the code they emit are consistent, ie i
f an effect |
| 169 // asks for dst color, then the emit code needs to follow suit | 168 // asks for dst color, then the emit code needs to follow suit |
| 170 SkDEBUGCODE(verify(fp);) | 169 SkDEBUGCODE(verify(fp);) |
| 171 fFragmentProcessors.push_back(fragProc); | 170 fFragmentProcessors.push_back(fragProc); |
| 172 | 171 |
| 173 fFS.codeAppend("}"); | 172 fFS.codeAppend("}"); |
| 174 } | 173 } |
| 175 | 174 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 190 } | 189 } |
| 191 | 190 |
| 192 if (this->glslCaps()->mustDeclareFragmentShaderOutput()) { | 191 if (this->glslCaps()->mustDeclareFragmentShaderOutput()) { |
| 193 fFS.enableCustomOutput(); | 192 fFS.enableCustomOutput(); |
| 194 } | 193 } |
| 195 | 194 |
| 196 SkString openBrace; | 195 SkString openBrace; |
| 197 openBrace.printf("{ // Xfer Processor: %s\n", xp.name()); | 196 openBrace.printf("{ // Xfer Processor: %s\n", xp.name()); |
| 198 fFS.codeAppend(openBrace.c_str()); | 197 fFS.codeAppend(openBrace.c_str()); |
| 199 | 198 |
| 200 SkSTArray<4, GrGLSLSampler> texSamplers(xp.numTextures()); | 199 SkSTArray<4, SamplerHandle> texSamplers(xp.numTextures()); |
| 201 SkSTArray<2, GrGLSLSampler> bufferSamplers(xp.numBuffers()); | 200 SkSTArray<2, SamplerHandle> bufferSamplers(xp.numBuffers()); |
| 202 this->emitSamplers(xp, &texSamplers, &bufferSamplers); | 201 this->emitSamplers(xp, &texSamplers, &bufferSamplers); |
| 203 | 202 |
| 204 bool usePLSDstRead = (plsState == GrPixelLocalStorageState::kFinish_GrPixelL
ocalStorageState); | 203 bool usePLSDstRead = (plsState == GrPixelLocalStorageState::kFinish_GrPixelL
ocalStorageState); |
| 205 GrGLSLXferProcessor::EmitArgs args(&fFS, | 204 GrGLSLXferProcessor::EmitArgs args(&fFS, |
| 206 this->uniformHandler(), | 205 this->uniformHandler(), |
| 207 this->glslCaps(), | 206 this->glslCaps(), |
| 208 xp, colorIn.c_str(), | 207 xp, colorIn.c_str(), |
| 209 ignoresCoverage ? nullptr : coverageIn.c_
str(), | 208 ignoresCoverage ? nullptr : coverageIn.c_
str(), |
| 210 fFS.getPrimaryColorOutputName(), | 209 fFS.getPrimaryColorOutputName(), |
| 211 fFS.getSecondaryColorOutputName(), | 210 fFS.getSecondaryColorOutputName(), |
| 212 texSamplers, | 211 texSamplers.begin(), |
| 213 bufferSamplers, | 212 bufferSamplers.begin(), |
| 214 usePLSDstRead); | 213 usePLSDstRead); |
| 215 fXferProcessor->emitCode(args); | 214 fXferProcessor->emitCode(args); |
| 216 | 215 |
| 217 // We have to check that effects and the code they emit are consistent, ie i
f an effect | 216 // We have to check that effects and the code they emit are consistent, ie i
f an effect |
| 218 // asks for dst color, then the emit code needs to follow suit | 217 // asks for dst color, then the emit code needs to follow suit |
| 219 SkDEBUGCODE(verify(xp);) | 218 SkDEBUGCODE(verify(xp);) |
| 220 fFS.codeAppend("}"); | 219 fFS.codeAppend("}"); |
| 221 } | 220 } |
| 222 | 221 |
| 223 void GrGLSLProgramBuilder::emitSamplers(const GrProcessor& processor, | 222 void GrGLSLProgramBuilder::emitSamplers(const GrProcessor& processor, |
| 224 GrGLSLSampler::SamplerArray* outTexSampl
ers, | 223 SkTArray<SamplerHandle>* outTexSamplers, |
| 225 GrGLSLSampler::SamplerArray* outBufferSa
mplers) { | 224 SkTArray<SamplerHandle>* outBufferSample
rs) { |
| 226 SkString name; | 225 SkString name; |
| 227 int numTextures = processor.numTextures(); | 226 int numTextures = processor.numTextures(); |
| 228 for (int t = 0; t < numTextures; ++t) { | 227 for (int t = 0; t < numTextures; ++t) { |
| 229 const GrTextureAccess& access = processor.textureAccess(t); | 228 const GrTextureAccess& access = processor.textureAccess(t); |
| 230 GrSLType samplerType = access.getTexture()->samplerType(); | 229 GrSLType samplerType = access.getTexture()->samplerType(); |
| 231 if (kSamplerExternal_GrSLType == samplerType) { | 230 if (kSamplerExternal_GrSLType == samplerType) { |
| 232 const char* externalFeatureString = this->glslCaps()->externalTextur
eExtensionString(); | 231 const char* externalFeatureString = this->glslCaps()->externalTextur
eExtensionString(); |
| 233 // We shouldn't ever create a GrGLTexture that requires external sam
pler type | 232 // We shouldn't ever create a GrGLTexture that requires external sam
pler type |
| 234 SkASSERT(externalFeatureString); | 233 SkASSERT(externalFeatureString); |
| 235 this->addFeature(access.getVisibility(), | 234 this->addFeature(access.getVisibility(), |
| (...skipping 22 matching lines...) Expand all Loading... |
| 258 1 << GrGLSLShaderBuilder::kTexelBuffer_GLSLPrivateF
eature, | 257 1 << GrGLSLShaderBuilder::kTexelBuffer_GLSLPrivateF
eature, |
| 259 extension); | 258 extension); |
| 260 } | 259 } |
| 261 } | 260 } |
| 262 } | 261 } |
| 263 | 262 |
| 264 void GrGLSLProgramBuilder::emitSampler(GrSLType samplerType, | 263 void GrGLSLProgramBuilder::emitSampler(GrSLType samplerType, |
| 265 GrPixelConfig config, | 264 GrPixelConfig config, |
| 266 const char* name, | 265 const char* name, |
| 267 GrShaderFlags visibility, | 266 GrShaderFlags visibility, |
| 268 GrGLSLSampler::SamplerArray* outSamplers)
{ | 267 SkTArray<SamplerHandle>* outSamplers) { |
| 269 if (visibility & kVertex_GrShaderFlag) { | 268 if (visibility & kVertex_GrShaderFlag) { |
| 270 ++fNumVertexSamplers; | 269 ++fNumVertexSamplers; |
| 271 } | 270 } |
| 272 if (visibility & kGeometry_GrShaderFlag) { | 271 if (visibility & kGeometry_GrShaderFlag) { |
| 273 SkASSERT(this->primitiveProcessor().willUseGeoShader()); | 272 SkASSERT(this->primitiveProcessor().willUseGeoShader()); |
| 274 ++fNumGeometrySamplers; | 273 ++fNumGeometrySamplers; |
| 275 } | 274 } |
| 276 if (visibility & kFragment_GrShaderFlag) { | 275 if (visibility & kFragment_GrShaderFlag) { |
| 277 ++fNumFragmentSamplers; | 276 ++fNumFragmentSamplers; |
| 278 } | 277 } |
| 279 GrSLPrecision precision = this->glslCaps()->samplerPrecision(config, visibil
ity); | 278 GrSLPrecision precision = this->glslCaps()->samplerPrecision(config, visibil
ity); |
| 280 UniformHandle u = this->uniformHandler()->addUniform(visibility, samplerType
, precision, name); | 279 SamplerHandle handle = this->uniformHandler()->addSampler(visibility, |
| 281 fSamplerUniforms.push_back(u); | 280 config, |
| 282 outSamplers->emplace_back(u, config); | 281 samplerType, |
| 282 precision, |
| 283 name); |
| 284 outSamplers->emplace_back(handle); |
| 283 } | 285 } |
| 284 | 286 |
| 285 void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) { | 287 void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) { |
| 286 // Swizzle the fragment shader outputs if necessary. | 288 // Swizzle the fragment shader outputs if necessary. |
| 287 GrSwizzle swizzle; | 289 GrSwizzle swizzle; |
| 288 swizzle.setFromKey(this->desc().header().fOutputSwizzle); | 290 swizzle.setFromKey(this->desc().header().fOutputSwizzle); |
| 289 if (swizzle != GrSwizzle::RGBA()) { | 291 if (swizzle != GrSwizzle::RGBA()) { |
| 290 fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(), | 292 fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(), |
| 291 fFS.getPrimaryColorOutputName(), | 293 fFS.getPrimaryColorOutputName(), |
| 292 swizzle.c_str()); | 294 swizzle.c_str()); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 this->nameVariable(&outName, '\0', baseName); | 364 this->nameVariable(&outName, '\0', baseName); |
| 363 } | 365 } |
| 364 fFS.codeAppendf("vec4 %s;", outName.c_str()); | 366 fFS.codeAppendf("vec4 %s;", outName.c_str()); |
| 365 *output = outName; | 367 *output = outName; |
| 366 } | 368 } |
| 367 | 369 |
| 368 void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString
* out) const { | 370 void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString
* out) const { |
| 369 this->uniformHandler()->appendUniformDecls(visibility, out); | 371 this->uniformHandler()->appendUniformDecls(visibility, out); |
| 370 } | 372 } |
| 371 | 373 |
| 374 const GrGLSLSampler& GrGLSLProgramBuilder::getSampler(SamplerHandle handle) cons
t { |
| 375 return this->uniformHandler()->getSampler(handle); |
| 376 } |
| 377 |
| 372 void GrGLSLProgramBuilder::addRTAdjustmentUniform(GrSLPrecision precision, | 378 void GrGLSLProgramBuilder::addRTAdjustmentUniform(GrSLPrecision precision, |
| 373 const char* name, | 379 const char* name, |
| 374 const char** outName) { | 380 const char** outName) { |
| 375 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid()); | 381 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid()); |
| 376 fUniformHandles.fRTAdjustmentUni = | 382 fUniformHandles.fRTAdjustmentUni = |
| 377 this->uniformHandler()->addUniform(kVertex_GrShaderFlag, | 383 this->uniformHandler()->addUniform(kVertex_GrShaderFlag, |
| 378 kVec4f_GrSLType, | 384 kVec4f_GrSLType, |
| 379 precision, | 385 precision, |
| 380 name, | 386 name, |
| 381 outName); | 387 outName); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 395 delete fFragmentProcessors[i]; | 401 delete fFragmentProcessors[i]; |
| 396 } | 402 } |
| 397 } | 403 } |
| 398 | 404 |
| 399 void GrGLSLProgramBuilder::finalizeShaders() { | 405 void GrGLSLProgramBuilder::finalizeShaders() { |
| 400 this->varyingHandler()->finalize(); | 406 this->varyingHandler()->finalize(); |
| 401 fVS.finalize(kVertex_GrShaderFlag); | 407 fVS.finalize(kVertex_GrShaderFlag); |
| 402 fFS.finalize(kFragment_GrShaderFlag); | 408 fFS.finalize(kFragment_GrShaderFlag); |
| 403 | 409 |
| 404 } | 410 } |
| OLD | NEW |