| 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
| 9 | |
| 10 | #include "src/gpu/GrCaps.h" |
| 11 | #include "src/gpu/GrPipeline.h" |
| 12 | #include "src/gpu/GrRenderTarget.h" |
| 13 | #include "src/gpu/GrShaderCaps.h" |
| 14 | #include "src/gpu/GrTexturePriv.h" |
| 15 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 16 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 17 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 18 | #include "src/gpu/glsl/GrGLSLXferProcessor.h" |
| 19 | #include "src/sksl/SkSLCompiler.h" |
| 20 | |
| 21 | const int GrGLSLProgramBuilder::kVarsPerBlock = 8; |
| 22 | |
| 23 | GrGLSLProgramBuilder::GrGLSLProgramBuilder(GrRenderTarget* renderTarget, |
| 24 | const GrProgramDesc& desc, |
| 25 | const GrProgramInfo& programInfo) |
| 26 | : fVS(this) |
| 27 | , fGS(this) |
| 28 | , fFS(this) |
| 29 | , fStageIndex(-1) |
| 30 | , fRenderTarget(renderTarget) |
| 31 | , fDesc(desc) |
| 32 | , fProgramInfo(programInfo) |
| 33 | , fGeometryProcessor(nullptr) |
| 34 | , fXferProcessor(nullptr) |
| 35 | , fNumFragmentSamplers(0) {} |
| 36 | |
| 37 | void GrGLSLProgramBuilder::addFeature(GrShaderFlags shaders, |
| 38 | uint32_t featureBit, |
| 39 | const char* extensionName) { |
| 40 | if (shaders & kVertex_GrShaderFlag) { |
| 41 | fVS.addFeature(featureBit, extensionName); |
| 42 | } |
| 43 | if (shaders & kGeometry_GrShaderFlag) { |
| 44 | SkASSERT(this->primitiveProcessor().willUseGeoShader()); |
| 45 | fGS.addFeature(featureBit, extensionName); |
| 46 | } |
| 47 | if (shaders & kFragment_GrShaderFlag) { |
| 48 | fFS.addFeature(featureBit, extensionName); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | bool GrGLSLProgramBuilder::emitAndInstallProcs() { |
| 53 | // First we loop over all of the installed processors and collect coord transforms. These will |
| 54 | // be sent to the GrGLSLPrimitiveProcessor in its emitCode function |
| 55 | SkString inputColor; |
| 56 | SkString inputCoverage; |
| 57 | this->emitAndInstallPrimProc(&inputColor, &inputCoverage); |
| 58 | this->emitAndInstallFragProcs(&inputColor, &inputCoverage); |
| 59 | this->emitAndInstallXferProc(inputColor, inputCoverage); |
| 60 | |
| 61 | return this->checkSamplerCounts(); |
| 62 | } |
| 63 | |
| 64 | void GrGLSLProgramBuilder::emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage) { |
| 65 | const GrPrimitiveProcessor& proc = this->primitiveProcessor(); |
| 66 | |
| 67 | // Program builders have a bit of state we need to clear with each effect |
| 68 | AutoStageAdvance adv(this); |
| 69 | this->nameExpression(outputColor, "outputColor" ); |
| 70 | this->nameExpression(outputCoverage, "outputCoverage" ); |
| 71 | |
| 72 | SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid()); |
| 73 | GrShaderFlags rtAdjustVisibility; |
| 74 | if (proc.willUseGeoShader()) { |
| 75 | rtAdjustVisibility = kGeometry_GrShaderFlag; |
| 76 | } else if (proc.willUseTessellationShaders()) { |
| 77 | rtAdjustVisibility = kTessEvaluation_GrShaderFlag; |
| 78 | } else { |
| 79 | rtAdjustVisibility = kVertex_GrShaderFlag; |
| 80 | } |
| 81 | fUniformHandles.fRTAdjustmentUni = this->uniformHandler()->addUniform( |
| 82 | nullptr, rtAdjustVisibility, kFloat4_GrSLType, SkSL::Compiler::RTADJUST_NAME); |
| 83 | const char* rtAdjustName = |
| 84 | this->uniformHandler()->getUniformCStr(fUniformHandles.fRTAdjustmentUni); |
| 85 | |
| 86 | // Enclose custom code in a block to avoid namespace conflicts |
| 87 | SkString openBrace; |
| 88 | openBrace.printf("{ // Stage %d, %s\n" , fStageIndex, proc.name()); |
| 89 | fFS.codeAppend(openBrace.c_str()); |
| 90 | fVS.codeAppendf("// Primitive Processor %s\n" , proc.name()); |
| 91 | |
| 92 | SkASSERT(!fGeometryProcessor); |
| 93 | fGeometryProcessor.reset(proc.createGLSLInstance(*this->shaderCaps())); |
| 94 | |
| 95 | SkAutoSTMalloc<4, SamplerHandle> texSamplers(proc.numTextureSamplers()); |
| 96 | for (int i = 0; i < proc.numTextureSamplers(); ++i) { |
| 97 | SkString name; |
| 98 | name.printf("TextureSampler_%d" , i); |
| 99 | const auto& sampler = proc.textureSampler(i); |
| 100 | texSamplers[i] = this->emitSampler(proc.textureSampler(i).backendFormat(), |
| 101 | sampler.samplerState(), |
| 102 | sampler.swizzle(), |
| 103 | name.c_str()); |
| 104 | } |
| 105 | |
| 106 | GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(this->pipeline(), |
| 107 | &fTransformedCoordVars); |
| 108 | GrGLSLGeometryProcessor::EmitArgs args(&fVS, |
| 109 | proc.willUseGeoShader() ? &fGS : nullptr, |
| 110 | &fFS, |
| 111 | this->varyingHandler(), |
| 112 | this->uniformHandler(), |
| 113 | this->shaderCaps(), |
| 114 | proc, |
| 115 | outputColor->c_str(), |
| 116 | outputCoverage->c_str(), |
| 117 | rtAdjustName, |
| 118 | texSamplers.get(), |
| 119 | &transformHandler); |
| 120 | fGeometryProcessor->emitCode(args); |
| 121 | |
| 122 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 123 | // asks for dst color, then the emit code needs to follow suit |
| 124 | SkDEBUGCODE(verify(proc);) |
| 125 | |
| 126 | fFS.codeAppend("}" ); |
| 127 | } |
| 128 | |
| 129 | void GrGLSLProgramBuilder::emitAndInstallFragProcs(SkString* color, SkString* coverage) { |
| 130 | int transformedCoordVarsIdx = 0; |
| 131 | SkString** inOut = &color; |
| 132 | SkSTArray<8, std::unique_ptr<GrGLSLFragmentProcessor>> glslFragmentProcessors; |
| 133 | for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) { |
| 134 | if (i == this->pipeline().numColorFragmentProcessors()) { |
| 135 | inOut = &coverage; |
| 136 | } |
| 137 | SkString output; |
| 138 | const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i); |
| 139 | output = this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, output, |
| 140 | &glslFragmentProcessors); |
| 141 | for (const auto& subFP : GrFragmentProcessor::FPCRange(fp)) { |
| 142 | transformedCoordVarsIdx += subFP.numCoordTransforms(); |
| 143 | } |
| 144 | **inOut = output; |
| 145 | } |
| 146 | fFragmentProcessorCnt = glslFragmentProcessors.count(); |
| 147 | fFragmentProcessors.reset(new std::unique_ptr<GrGLSLFragmentProcessor>[fFragmentProcessorCnt]); |
| 148 | for (int i = 0; i < fFragmentProcessorCnt; ++i) { |
| 149 | fFragmentProcessors[i] = std::move(glslFragmentProcessors[i]); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // TODO Processors cannot output zeros because an empty string is all 1s |
| 154 | // the fix is to allow effects to take the SkString directly |
| 155 | SkString GrGLSLProgramBuilder::emitAndInstallFragProc( |
| 156 | const GrFragmentProcessor& fp, |
| 157 | int index, |
| 158 | int transformedCoordVarsIdx, |
| 159 | const SkString& input, |
| 160 | SkString output, |
| 161 | SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>* glslFragmentProcessors) { |
| 162 | SkASSERT(input.size()); |
| 163 | // Program builders have a bit of state we need to clear with each effect |
| 164 | AutoStageAdvance adv(this); |
| 165 | this->nameExpression(&output, "output" ); |
| 166 | |
| 167 | // Enclose custom code in a block to avoid namespace conflicts |
| 168 | SkString openBrace; |
| 169 | openBrace.printf("{ // Stage %d, %s\n" , fStageIndex, fp.name()); |
| 170 | fFS.codeAppend(openBrace.c_str()); |
| 171 | |
| 172 | GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance(); |
| 173 | |
| 174 | SkSTArray<4, SamplerHandle> texSamplers; |
| 175 | int samplerIdx = 0; |
| 176 | for (const auto& subFP : GrFragmentProcessor::FPCRange(fp)) { |
| 177 | for (int i = 0; i < subFP.numTextureSamplers(); ++i) { |
| 178 | SkString name; |
| 179 | name.printf("TextureSampler_%d" , samplerIdx++); |
| 180 | const auto& sampler = subFP.textureSampler(i); |
| 181 | texSamplers.emplace_back(this->emitSampler(sampler.view().proxy()->backendFormat(), |
| 182 | sampler.samplerState(), |
| 183 | sampler.view().swizzle(), |
| 184 | name.c_str())); |
| 185 | } |
| 186 | } |
| 187 | const GrGLSLPrimitiveProcessor::TransformVar* coordVars = fTransformedCoordVars.begin() + |
| 188 | transformedCoordVarsIdx; |
| 189 | GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars); |
| 190 | GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, texSamplers.begin()); |
| 191 | GrGLSLFragmentProcessor::EmitArgs args(&fFS, |
| 192 | this->uniformHandler(), |
| 193 | this->shaderCaps(), |
| 194 | fp, |
| 195 | output.c_str(), |
| 196 | input.c_str(), |
| 197 | coords, |
| 198 | textureSamplers); |
| 199 | |
| 200 | fragProc->emitCode(args); |
| 201 | |
| 202 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 203 | // asks for dst color, then the emit code needs to follow suit |
| 204 | SkDEBUGCODE(verify(fp);) |
| 205 | glslFragmentProcessors->emplace_back(fragProc); |
| 206 | |
| 207 | fFS.codeAppend("}" ); |
| 208 | return output; |
| 209 | } |
| 210 | |
| 211 | void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn, |
| 212 | const SkString& coverageIn) { |
| 213 | // Program builders have a bit of state we need to clear with each effect |
| 214 | AutoStageAdvance adv(this); |
| 215 | |
| 216 | SkASSERT(!fXferProcessor); |
| 217 | const GrXferProcessor& xp = this->pipeline().getXferProcessor(); |
| 218 | fXferProcessor.reset(xp.createGLSLInstance()); |
| 219 | |
| 220 | // Enable dual source secondary output if we have one |
| 221 | if (xp.hasSecondaryOutput()) { |
| 222 | fFS.enableSecondaryOutput(); |
| 223 | } |
| 224 | |
| 225 | if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) { |
| 226 | fFS.enableCustomOutput(); |
| 227 | } |
| 228 | |
| 229 | SkString openBrace; |
| 230 | openBrace.printf("{ // Xfer Processor: %s\n" , xp.name()); |
| 231 | fFS.codeAppend(openBrace.c_str()); |
| 232 | |
| 233 | SamplerHandle dstTextureSamplerHandle; |
| 234 | GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin; |
| 235 | |
| 236 | const GrSurfaceProxyView& dstView = this->pipeline().dstProxyView(); |
| 237 | if (GrTextureProxy* dstTextureProxy = dstView.asTextureProxy()) { |
| 238 | // GrProcessor::TextureSampler sampler(dstTexture); |
| 239 | const GrSwizzle& swizzle = dstView.swizzle(); |
| 240 | dstTextureSamplerHandle = this->emitSampler(dstTextureProxy->backendFormat(), |
| 241 | GrSamplerState(), swizzle, "DstTextureSampler" ); |
| 242 | dstTextureOrigin = dstView.origin(); |
| 243 | SkASSERT(dstTextureProxy->textureType() != GrTextureType::kExternal); |
| 244 | } |
| 245 | |
| 246 | SkString finalInColor = colorIn.size() ? colorIn : SkString("float4(1)" ); |
| 247 | |
| 248 | GrGLSLXferProcessor::EmitArgs args(&fFS, |
| 249 | this->uniformHandler(), |
| 250 | this->shaderCaps(), |
| 251 | xp, |
| 252 | finalInColor.c_str(), |
| 253 | coverageIn.size() ? coverageIn.c_str() : "float4(1)" , |
| 254 | fFS.getPrimaryColorOutputName(), |
| 255 | fFS.getSecondaryColorOutputName(), |
| 256 | dstTextureSamplerHandle, |
| 257 | dstTextureOrigin, |
| 258 | this->pipeline().writeSwizzle()); |
| 259 | fXferProcessor->emitCode(args); |
| 260 | |
| 261 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 262 | // asks for dst color, then the emit code needs to follow suit |
| 263 | SkDEBUGCODE(verify(xp);) |
| 264 | fFS.codeAppend("}" ); |
| 265 | } |
| 266 | |
| 267 | GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler( |
| 268 | const GrBackendFormat& backendFormat, GrSamplerState state, const GrSwizzle& swizzle, |
| 269 | const char* name) { |
| 270 | ++fNumFragmentSamplers; |
| 271 | return this->uniformHandler()->addSampler(backendFormat, state, swizzle, name, |
| 272 | this->shaderCaps()); |
| 273 | } |
| 274 | |
| 275 | bool GrGLSLProgramBuilder::checkSamplerCounts() { |
| 276 | const GrShaderCaps& shaderCaps = *this->shaderCaps(); |
| 277 | if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) { |
| 278 | GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n" ); |
| 279 | return false; |
| 280 | } |
| 281 | return true; |
| 282 | } |
| 283 | |
| 284 | #ifdef SK_DEBUG |
| 285 | void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) { |
| 286 | SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly); |
| 287 | SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == gp.requestedFeatures()); |
| 288 | } |
| 289 | |
| 290 | void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) { |
| 291 | SkASSERT(!fFS.fHasReadDstColorThisStage_DebugOnly); |
| 292 | SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == fp.requestedFeatures()); |
| 293 | } |
| 294 | |
| 295 | void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) { |
| 296 | SkASSERT(xp.willReadDstColor() == fFS.fHasReadDstColorThisStage_DebugOnly); |
| 297 | SkASSERT(fFS.fUsedProcessorFeaturesThisStage_DebugOnly == xp.requestedFeatures()); |
| 298 | } |
| 299 | #endif |
| 300 | |
| 301 | void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) { |
| 302 | if ('\0' == prefix) { |
| 303 | *out = name; |
| 304 | } else { |
| 305 | out->printf("%c%s" , prefix, name); |
| 306 | } |
| 307 | if (mangle) { |
| 308 | if (out->endsWith('_')) { |
| 309 | // Names containing "__" are reserved. |
| 310 | out->append("x" ); |
| 311 | } |
| 312 | out->appendf("_Stage%d%s" , fStageIndex, fFS.getMangleString().c_str()); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) { |
| 317 | // create var to hold stage result. If we already have a valid output name, just use that |
| 318 | // otherwise create a new mangled one. This name is only valid if we are reordering stages |
| 319 | // and have to tell stage exactly where to put its output. |
| 320 | SkString outName; |
| 321 | if (output->size()) { |
| 322 | outName = output->c_str(); |
| 323 | } else { |
| 324 | this->nameVariable(&outName, '\0', baseName); |
| 325 | } |
| 326 | fFS.codeAppendf("half4 %s;" , outName.c_str()); |
| 327 | *output = outName; |
| 328 | } |
| 329 | |
| 330 | void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const { |
| 331 | this->uniformHandler()->appendUniformDecls(visibility, out); |
| 332 | } |
| 333 | |
| 334 | void GrGLSLProgramBuilder::addRTWidthUniform(const char* name) { |
| 335 | SkASSERT(!fUniformHandles.fRTWidthUni.isValid()); |
| 336 | GrGLSLUniformHandler* uniformHandler = this->uniformHandler(); |
| 337 | fUniformHandles.fRTWidthUni = |
| 338 | uniformHandler->internalAddUniformArray(nullptr, kFragment_GrShaderFlag, kHalf_GrSLType, |
| 339 | name, false, 0, nullptr); |
| 340 | } |
| 341 | |
| 342 | void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) { |
| 343 | SkASSERT(!fUniformHandles.fRTHeightUni.isValid()); |
| 344 | GrGLSLUniformHandler* uniformHandler = this->uniformHandler(); |
| 345 | fUniformHandles.fRTHeightUni = |
| 346 | uniformHandler->internalAddUniformArray(nullptr, kFragment_GrShaderFlag, kHalf_GrSLType, |
| 347 | name, false, 0, nullptr); |
| 348 | } |
| 349 | |
| 350 | void GrGLSLProgramBuilder::finalizeShaders() { |
| 351 | this->varyingHandler()->finalize(); |
| 352 | fVS.finalize(kVertex_GrShaderFlag); |
| 353 | if (this->primitiveProcessor().willUseGeoShader()) { |
| 354 | SkASSERT(this->shaderCaps()->geometryShaderSupport()); |
| 355 | fGS.finalize(kGeometry_GrShaderFlag); |
| 356 | } |
| 357 | fFS.finalize(kFragment_GrShaderFlag); |
| 358 | } |
| 359 | |