| 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" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 // Enclose custom code in a block to avoid namespace conflicts | 91 // Enclose custom code in a block to avoid namespace conflicts |
| 92 SkString openBrace; | 92 SkString openBrace; |
| 93 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); | 93 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); |
| 94 fFS.codeAppend(openBrace.c_str()); | 94 fFS.codeAppend(openBrace.c_str()); |
| 95 fVS.codeAppendf("// Primitive Processor %s\n", proc.name()); | 95 fVS.codeAppendf("// Primitive Processor %s\n", proc.name()); |
| 96 | 96 |
| 97 SkASSERT(!fGeometryProcessor); | 97 SkASSERT(!fGeometryProcessor); |
| 98 fGeometryProcessor = proc.createGLSLInstance(*this->glslCaps()); | 98 fGeometryProcessor = proc.createGLSLInstance(*this->glslCaps()); |
| 99 | 99 |
| 100 SkSTArray<4, GrGLSLSampler> texSamplers(proc.numTextures()); | 100 SkSTArray<4, GrGLSLSampler> texSamplers(proc.numTextures()); |
| 101 this->emitSamplers(proc, &texSamplers); | 101 SkSTArray<2, GrGLSLSampler> bufferSamplers(proc.numBuffers()); |
| 102 this->emitSamplers(proc, &texSamplers, &bufferSamplers); |
| 102 | 103 |
| 103 GrGLSLGeometryProcessor::EmitArgs args(&fVS, | 104 GrGLSLGeometryProcessor::EmitArgs args(&fVS, |
| 104 &fFS, | 105 &fFS, |
| 105 this->varyingHandler(), | 106 this->varyingHandler(), |
| 106 this->uniformHandler(), | 107 this->uniformHandler(), |
| 107 this->glslCaps(), | 108 this->glslCaps(), |
| 108 proc, | 109 proc, |
| 109 outputColor->c_str(), | 110 outputColor->c_str(), |
| 110 outputCoverage->c_str(), | 111 outputCoverage->c_str(), |
| 111 texSamplers, | 112 texSamplers, |
| 113 bufferSamplers, |
| 112 fCoordTransforms, | 114 fCoordTransforms, |
| 113 &fOutCoords); | 115 &fOutCoords); |
| 114 fGeometryProcessor->emitCode(args); | 116 fGeometryProcessor->emitCode(args); |
| 115 | 117 |
| 116 // We have to check that effects and the code they emit are consistent, ie i
f an effect | 118 // We have to check that effects and the code they emit are consistent, ie i
f an effect |
| 117 // asks for dst color, then the emit code needs to follow suit | 119 // asks for dst color, then the emit code needs to follow suit |
| 118 SkDEBUGCODE(verify(proc);) | 120 SkDEBUGCODE(verify(proc);) |
| 119 | 121 |
| 120 fFS.codeAppend("}"); | 122 fFS.codeAppend("}"); |
| 121 } | 123 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 142 this->nameExpression(output, "output"); | 144 this->nameExpression(output, "output"); |
| 143 | 145 |
| 144 // Enclose custom code in a block to avoid namespace conflicts | 146 // Enclose custom code in a block to avoid namespace conflicts |
| 145 SkString openBrace; | 147 SkString openBrace; |
| 146 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name()); | 148 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name()); |
| 147 fFS.codeAppend(openBrace.c_str()); | 149 fFS.codeAppend(openBrace.c_str()); |
| 148 | 150 |
| 149 GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance(); | 151 GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance(); |
| 150 | 152 |
| 151 SkSTArray<4, GrGLSLSampler> texSamplers(fp.numTextures()); | 153 SkSTArray<4, GrGLSLSampler> texSamplers(fp.numTextures()); |
| 152 this->emitSamplers(fp, &texSamplers); | 154 SkSTArray<2, GrGLSLSampler> bufferSamplers(fp.numBuffers()); |
| 155 this->emitSamplers(fp, &texSamplers, &bufferSamplers); |
| 153 | 156 |
| 154 GrGLSLFragmentProcessor::EmitArgs args(&fFS, | 157 GrGLSLFragmentProcessor::EmitArgs args(&fFS, |
| 155 this->uniformHandler(), | 158 this->uniformHandler(), |
| 156 this->glslCaps(), | 159 this->glslCaps(), |
| 157 fp, | 160 fp, |
| 158 output->c_str(), | 161 output->c_str(), |
| 159 input.isOnes() ? nullptr : input.c_st
r(), | 162 input.isOnes() ? nullptr : input.c_st
r(), |
| 160 fOutCoords[index], | 163 fOutCoords[index], |
| 161 texSamplers); | 164 texSamplers, |
| 165 bufferSamplers); |
| 162 fragProc->emitCode(args); | 166 fragProc->emitCode(args); |
| 163 | 167 |
| 164 // We have to check that effects and the code they emit are consistent, ie i
f an effect | 168 // We have to check that effects and the code they emit are consistent, ie i
f an effect |
| 165 // asks for dst color, then the emit code needs to follow suit | 169 // asks for dst color, then the emit code needs to follow suit |
| 166 SkDEBUGCODE(verify(fp);) | 170 SkDEBUGCODE(verify(fp);) |
| 167 fFragmentProcessors.push_back(fragProc); | 171 fFragmentProcessors.push_back(fragProc); |
| 168 | 172 |
| 169 fFS.codeAppend("}"); | 173 fFS.codeAppend("}"); |
| 170 } | 174 } |
| 171 | 175 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 187 | 191 |
| 188 if (this->glslCaps()->mustDeclareFragmentShaderOutput()) { | 192 if (this->glslCaps()->mustDeclareFragmentShaderOutput()) { |
| 189 fFS.enableCustomOutput(); | 193 fFS.enableCustomOutput(); |
| 190 } | 194 } |
| 191 | 195 |
| 192 SkString openBrace; | 196 SkString openBrace; |
| 193 openBrace.printf("{ // Xfer Processor: %s\n", xp.name()); | 197 openBrace.printf("{ // Xfer Processor: %s\n", xp.name()); |
| 194 fFS.codeAppend(openBrace.c_str()); | 198 fFS.codeAppend(openBrace.c_str()); |
| 195 | 199 |
| 196 SkSTArray<4, GrGLSLSampler> texSamplers(xp.numTextures()); | 200 SkSTArray<4, GrGLSLSampler> texSamplers(xp.numTextures()); |
| 197 this->emitSamplers(xp, &texSamplers); | 201 SkSTArray<2, GrGLSLSampler> bufferSamplers(xp.numBuffers()); |
| 202 this->emitSamplers(xp, &texSamplers, &bufferSamplers); |
| 198 | 203 |
| 199 bool usePLSDstRead = (plsState == GrPixelLocalStorageState::kFinish_GrPixelL
ocalStorageState); | 204 bool usePLSDstRead = (plsState == GrPixelLocalStorageState::kFinish_GrPixelL
ocalStorageState); |
| 200 GrGLSLXferProcessor::EmitArgs args(&fFS, | 205 GrGLSLXferProcessor::EmitArgs args(&fFS, |
| 201 this->uniformHandler(), | 206 this->uniformHandler(), |
| 202 this->glslCaps(), | 207 this->glslCaps(), |
| 203 xp, colorIn.c_str(), | 208 xp, colorIn.c_str(), |
| 204 ignoresCoverage ? nullptr : coverageIn.c_
str(), | 209 ignoresCoverage ? nullptr : coverageIn.c_
str(), |
| 205 fFS.getPrimaryColorOutputName(), | 210 fFS.getPrimaryColorOutputName(), |
| 206 fFS.getSecondaryColorOutputName(), | 211 fFS.getSecondaryColorOutputName(), |
| 207 texSamplers, | 212 texSamplers, |
| 213 bufferSamplers, |
| 208 usePLSDstRead); | 214 usePLSDstRead); |
| 209 fXferProcessor->emitCode(args); | 215 fXferProcessor->emitCode(args); |
| 210 | 216 |
| 211 // We have to check that effects and the code they emit are consistent, ie i
f an effect | 217 // We have to check that effects and the code they emit are consistent, ie i
f an effect |
| 212 // asks for dst color, then the emit code needs to follow suit | 218 // asks for dst color, then the emit code needs to follow suit |
| 213 SkDEBUGCODE(verify(xp);) | 219 SkDEBUGCODE(verify(xp);) |
| 214 fFS.codeAppend("}"); | 220 fFS.codeAppend("}"); |
| 215 } | 221 } |
| 216 | 222 |
| 217 void GrGLSLProgramBuilder::emitSamplers(const GrProcessor& processor, | 223 void GrGLSLProgramBuilder::emitSamplers(const GrProcessor& processor, |
| 218 GrGLSLSampler::SamplerArray* outTexSampl
ers) { | 224 GrGLSLSampler::SamplerArray* outTexSampl
ers, |
| 225 GrGLSLSampler::SamplerArray* outBufferSa
mplers) { |
| 226 SkString name; |
| 219 int numTextures = processor.numTextures(); | 227 int numTextures = processor.numTextures(); |
| 220 UniformHandle* localSamplerUniforms = fSamplerUniforms.push_back_n(numTextur
es); | |
| 221 SkString name; | |
| 222 for (int t = 0; t < numTextures; ++t) { | 228 for (int t = 0; t < numTextures; ++t) { |
| 223 const GrTextureAccess& access = processor.textureAccess(t); | 229 const GrTextureAccess& access = processor.textureAccess(t); |
| 224 GrShaderFlags visibility = access.getVisibility(); | |
| 225 if (visibility & kVertex_GrShaderFlag) { | |
| 226 ++fNumVertexSamplers; | |
| 227 } | |
| 228 if (visibility & kGeometry_GrShaderFlag) { | |
| 229 SkASSERT(this->primitiveProcessor().willUseGeoShader()); | |
| 230 ++fNumGeometrySamplers; | |
| 231 } | |
| 232 if (visibility & kFragment_GrShaderFlag) { | |
| 233 ++fNumFragmentSamplers; | |
| 234 } | |
| 235 GrSLType samplerType = access.getTexture()->samplerType(); | 230 GrSLType samplerType = access.getTexture()->samplerType(); |
| 236 if (kSamplerExternal_GrSLType == samplerType) { | 231 if (kSamplerExternal_GrSLType == samplerType) { |
| 237 const char* externalFeatureString = this->glslCaps()->externalTextur
eExtensionString(); | 232 const char* externalFeatureString = this->glslCaps()->externalTextur
eExtensionString(); |
| 238 // We shouldn't ever create a GrGLTexture that requires external sam
pler type | 233 // We shouldn't ever create a GrGLTexture that requires external sam
pler type |
| 239 SkASSERT(externalFeatureString); | 234 SkASSERT(externalFeatureString); |
| 240 this->addFeature(visibility, | 235 this->addFeature(access.getVisibility(), |
| 241 1 << GrGLSLShaderBuilder::kExternalTexture_GLSLPriv
ateFeature, | 236 1 << GrGLSLShaderBuilder::kExternalTexture_GLSLPriv
ateFeature, |
| 242 externalFeatureString); | 237 externalFeatureString); |
| 243 } | 238 } |
| 244 GrSLPrecision precision = this->glslCaps()->samplerPrecision(access.getT
exture()->config(), | 239 name.printf("TextureSampler%d", t); |
| 245 visibility)
; | 240 this->emitSampler(samplerType, access.getTexture()->config(), |
| 246 name.printf("Sampler%d", t); | 241 name.c_str(), access.getVisibility(), outTexSamplers); |
| 247 localSamplerUniforms[t] = this->uniformHandler()->addUniform(visibility, | 242 } |
| 248 samplerType
, | 243 |
| 249 precision, | 244 if (int numBuffers = processor.numBuffers()) { |
| 250 name.c_str(
)); | 245 SkASSERT(this->glslCaps()->texelBufferSupport()); |
| 251 outTexSamplers->emplace_back(localSamplerUniforms[t], access.getTexture(
)->config()); | 246 GrShaderFlags texelBufferVisibility = kNone_GrShaderFlags; |
| 247 |
| 248 for (int b = 0; b < numBuffers; ++b) { |
| 249 const GrBufferAccess& access = processor.bufferAccess(b); |
| 250 name.printf("BufferSampler%d", b); |
| 251 this->emitSampler(kSamplerBuffer_GrSLType, access.texelConfig(), nam
e.c_str(), |
| 252 access.visibility(), outBufferSamplers); |
| 253 texelBufferVisibility |= access.visibility(); |
| 254 } |
| 255 |
| 256 if (const char* extension = this->glslCaps()->texelBufferExtensionString
()) { |
| 257 this->addFeature(texelBufferVisibility, |
| 258 1 << GrGLSLShaderBuilder::kTexelBuffer_GLSLPrivateF
eature, |
| 259 extension); |
| 260 } |
| 252 } | 261 } |
| 253 } | 262 } |
| 254 | 263 |
| 264 void GrGLSLProgramBuilder::emitSampler(GrSLType samplerType, |
| 265 GrPixelConfig config, |
| 266 const char* name, |
| 267 GrShaderFlags visibility, |
| 268 GrGLSLSampler::SamplerArray* outSamplers)
{ |
| 269 if (visibility & kVertex_GrShaderFlag) { |
| 270 ++fNumVertexSamplers; |
| 271 } |
| 272 if (visibility & kGeometry_GrShaderFlag) { |
| 273 SkASSERT(this->primitiveProcessor().willUseGeoShader()); |
| 274 ++fNumGeometrySamplers; |
| 275 } |
| 276 if (visibility & kFragment_GrShaderFlag) { |
| 277 ++fNumFragmentSamplers; |
| 278 } |
| 279 GrSLPrecision precision = this->glslCaps()->samplerPrecision(config, visibil
ity); |
| 280 UniformHandle u = this->uniformHandler()->addUniform(visibility, samplerType
, precision, name); |
| 281 fSamplerUniforms.push_back(u); |
| 282 outSamplers->emplace_back(u, config); |
| 283 } |
| 284 |
| 255 void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) { | 285 void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) { |
| 256 // Swizzle the fragment shader outputs if necessary. | 286 // Swizzle the fragment shader outputs if necessary. |
| 257 GrSwizzle swizzle; | 287 GrSwizzle swizzle; |
| 258 swizzle.setFromKey(this->desc().header().fOutputSwizzle); | 288 swizzle.setFromKey(this->desc().header().fOutputSwizzle); |
| 259 if (swizzle != GrSwizzle::RGBA()) { | 289 if (swizzle != GrSwizzle::RGBA()) { |
| 260 fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(), | 290 fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(), |
| 261 fFS.getPrimaryColorOutputName(), | 291 fFS.getPrimaryColorOutputName(), |
| 262 swizzle.c_str()); | 292 swizzle.c_str()); |
| 263 if (hasSecondaryOutput) { | 293 if (hasSecondaryOutput) { |
| 264 fFS.codeAppendf("%s = %s.%s;", fFS.getSecondaryColorOutputName(), | 294 fFS.codeAppendf("%s = %s.%s;", fFS.getSecondaryColorOutputName(), |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 delete fFragmentProcessors[i]; | 395 delete fFragmentProcessors[i]; |
| 366 } | 396 } |
| 367 } | 397 } |
| 368 | 398 |
| 369 void GrGLSLProgramBuilder::finalizeShaders() { | 399 void GrGLSLProgramBuilder::finalizeShaders() { |
| 370 this->varyingHandler()->finalize(); | 400 this->varyingHandler()->finalize(); |
| 371 fVS.finalize(kVertex_GrShaderFlag); | 401 fVS.finalize(kVertex_GrShaderFlag); |
| 372 fFS.finalize(kFragment_GrShaderFlag); | 402 fFS.finalize(kFragment_GrShaderFlag); |
| 373 | 403 |
| 374 } | 404 } |
| OLD | NEW |