egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 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 "glsl/GrGLSLProgramBuilder.h" |
| 9 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 10 | #include "GrCaps.h" |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 11 | #include "GrPipeline.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 12 | #include "GrShaderCaps.h" |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 13 | #include "GrTexturePriv.h" |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 15 | #include "glsl/GrGLSLGeometryProcessor.h" |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 16 | #include "glsl/GrGLSLVarying.h" |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 17 | #include "glsl/GrGLSLXferProcessor.h" |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 18 | #include "SkSLCompiler.h" |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 19 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 20 | const int GrGLSLProgramBuilder::kVarsPerBlock = 8; |
| 21 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 22 | GrGLSLProgramBuilder::GrGLSLProgramBuilder(const GrPipeline& pipeline, |
| 23 | const GrPrimitiveProcessor& primProc, |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 24 | GrProgramDesc* desc) |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 25 | : fVS(this) |
| 26 | , fGS(this) |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 27 | , fFS(this) |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 28 | , fStageIndex(-1) |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 29 | , fPipeline(pipeline) |
| 30 | , fPrimProc(primProc) |
| 31 | , fDesc(desc) |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 32 | , fGeometryProcessor(nullptr) |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 33 | , fXferProcessor(nullptr) |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 34 | , fNumVertexSamplers(0) |
| 35 | , fNumGeometrySamplers(0) |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 36 | , fNumFragmentSamplers(0) { |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void GrGLSLProgramBuilder::addFeature(GrShaderFlags shaders, |
| 40 | uint32_t featureBit, |
| 41 | const char* extensionName) { |
| 42 | if (shaders & kVertex_GrShaderFlag) { |
| 43 | fVS.addFeature(featureBit, extensionName); |
| 44 | } |
| 45 | if (shaders & kGeometry_GrShaderFlag) { |
csmartdalton | 276cc41 | 2016-11-21 11:55:00 -0700 | [diff] [blame] | 46 | SkASSERT(this->primitiveProcessor().willUseGeoShader()); |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 47 | fGS.addFeature(featureBit, extensionName); |
| 48 | } |
| 49 | if (shaders & kFragment_GrShaderFlag) { |
| 50 | fFS.addFeature(featureBit, extensionName); |
| 51 | } |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 54 | bool GrGLSLProgramBuilder::emitAndInstallProcs() { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 55 | // First we loop over all of the installed processors and collect coord transforms. These will |
| 56 | // be sent to the GrGLSLPrimitiveProcessor in its emitCode function |
| 57 | const GrPrimitiveProcessor& primProc = this->primitiveProcessor(); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 58 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 59 | SkString inputColor; |
| 60 | SkString inputCoverage; |
| 61 | this->emitAndInstallPrimProc(primProc, &inputColor, &inputCoverage); |
| 62 | this->emitAndInstallFragProcs(&inputColor, &inputCoverage); |
| 63 | this->emitAndInstallXferProc(inputColor, inputCoverage); |
Brian Salomon | 42c456f | 2017-03-06 11:29:48 -0500 | [diff] [blame] | 64 | this->emitFSOutputSwizzle(this->pipeline().getXferProcessor().hasSecondaryOutput()); |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 65 | |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 66 | return this->checkSamplerCounts(); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void GrGLSLProgramBuilder::emitAndInstallPrimProc(const GrPrimitiveProcessor& proc, |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 70 | SkString* outputColor, |
| 71 | SkString* outputCoverage) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 72 | // Program builders have a bit of state we need to clear with each effect |
| 73 | AutoStageAdvance adv(this); |
| 74 | this->nameExpression(outputColor, "outputColor"); |
| 75 | this->nameExpression(outputCoverage, "outputCoverage"); |
| 76 | |
csmartdalton | 936f81b | 2017-02-13 15:45:35 -0700 | [diff] [blame] | 77 | SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid()); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 78 | GrShaderFlags rtAdjustVisibility; |
csmartdalton | 936f81b | 2017-02-13 15:45:35 -0700 | [diff] [blame] | 79 | if (proc.willUseGeoShader()) { |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 80 | rtAdjustVisibility = kGeometry_GrShaderFlag; |
| 81 | } else { |
| 82 | rtAdjustVisibility = kVertex_GrShaderFlag; |
csmartdalton | 936f81b | 2017-02-13 15:45:35 -0700 | [diff] [blame] | 83 | } |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 84 | fUniformHandles.fRTAdjustmentUni = this->uniformHandler()->addUniform( |
| 85 | rtAdjustVisibility, |
| 86 | kFloat4_GrSLType, |
| 87 | SkSL::Compiler::RTADJUST_NAME); |
csmartdalton | 936f81b | 2017-02-13 15:45:35 -0700 | [diff] [blame] | 88 | const char* rtAdjustName = |
| 89 | this->uniformHandler()->getUniformCStr(fUniformHandles.fRTAdjustmentUni); |
| 90 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 91 | // Enclose custom code in a block to avoid namespace conflicts |
| 92 | SkString openBrace; |
| 93 | openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name()); |
| 94 | fFS.codeAppend(openBrace.c_str()); |
| 95 | fVS.codeAppendf("// Primitive Processor %s\n", proc.name()); |
| 96 | |
| 97 | SkASSERT(!fGeometryProcessor); |
Robert Phillips | 369e8b7 | 2017-08-01 16:13:04 -0400 | [diff] [blame] | 98 | fGeometryProcessor.reset(proc.createGLSLInstance(*this->shaderCaps())); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 99 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 100 | SkSTArray<4, SamplerHandle> texSamplers(proc.numTextureSamplers()); |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 101 | SkSTArray<2, TexelBufferHandle> texelBuffers(proc.numBuffers()); |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 102 | this->emitSamplers(proc, &texSamplers, &texelBuffers); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 103 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 104 | GrGLSLPrimitiveProcessor::FPCoordTransformHandler transformHandler(fPipeline, |
| 105 | &fTransformedCoordVars); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 106 | GrGLSLGeometryProcessor::EmitArgs args(&fVS, |
csmartdalton | 276cc41 | 2016-11-21 11:55:00 -0700 | [diff] [blame] | 107 | proc.willUseGeoShader() ? &fGS : nullptr, |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 108 | &fFS, |
| 109 | this->varyingHandler(), |
| 110 | this->uniformHandler(), |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 111 | this->shaderCaps(), |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 112 | proc, |
| 113 | outputColor->c_str(), |
| 114 | outputCoverage->c_str(), |
csmartdalton | 936f81b | 2017-02-13 15:45:35 -0700 | [diff] [blame] | 115 | rtAdjustName, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 116 | texSamplers.begin(), |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 117 | texelBuffers.begin(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 118 | &transformHandler); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 119 | fGeometryProcessor->emitCode(args); |
| 120 | |
| 121 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 122 | // asks for dst color, then the emit code needs to follow suit |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 123 | SkDEBUGCODE(verify(proc);) |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 124 | |
| 125 | fFS.codeAppend("}"); |
| 126 | } |
| 127 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 128 | void GrGLSLProgramBuilder::emitAndInstallFragProcs(SkString* color, SkString* coverage) { |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 129 | int transformedCoordVarsIdx = 0; |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 130 | SkString** inOut = &color; |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame^] | 131 | SkSTArray<8, std::unique_ptr<GrGLSLFragmentProcessor>> glslFragmentProcessors; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 132 | for (int i = 0; i < this->pipeline().numFragmentProcessors(); ++i) { |
| 133 | if (i == this->pipeline().numColorFragmentProcessors()) { |
| 134 | inOut = &coverage; |
| 135 | } |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 136 | SkString output; |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 137 | const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i); |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame^] | 138 | output = this->emitAndInstallFragProc(fp, i, transformedCoordVarsIdx, **inOut, output, |
| 139 | &glslFragmentProcessors); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 140 | GrFragmentProcessor::Iter iter(&fp); |
| 141 | while (const GrFragmentProcessor* fp = iter.next()) { |
| 142 | transformedCoordVarsIdx += fp->numCoordTransforms(); |
| 143 | } |
| 144 | **inOut = output; |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 145 | } |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame^] | 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 | } |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // TODO Processors cannot output zeros because an empty string is all 1s |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 154 | // the fix is to allow effects to take the SkString directly |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame^] | 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) { |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 162 | SkASSERT(input.size()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 163 | // Program builders have a bit of state we need to clear with each effect |
| 164 | AutoStageAdvance adv(this); |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 165 | this->nameExpression(&output, "output"); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 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 | |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 174 | SkSTArray<4, SamplerHandle> textureSamplerArray(fp.numTextureSamplers()); |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 175 | SkSTArray<2, TexelBufferHandle> texelBufferArray(fp.numBuffers()); |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 176 | GrFragmentProcessor::Iter iter(&fp); |
| 177 | while (const GrFragmentProcessor* subFP = iter.next()) { |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 178 | this->emitSamplers(*subFP, &textureSamplerArray, &texelBufferArray); |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 179 | } |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 180 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 181 | const GrShaderVar* coordVars = fTransformedCoordVars.begin() + transformedCoordVarsIdx; |
| 182 | GrGLSLFragmentProcessor::TransformedCoordVars coords(&fp, coordVars); |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 183 | GrGLSLFragmentProcessor::TextureSamplers textureSamplers(&fp, textureSamplerArray.begin()); |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 184 | GrGLSLFragmentProcessor::TexelBuffers texelBuffers(&fp, texelBufferArray.begin()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 185 | GrGLSLFragmentProcessor::EmitArgs args(&fFS, |
| 186 | this->uniformHandler(), |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 187 | this->shaderCaps(), |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 188 | fp, |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 189 | output.c_str(), |
| 190 | input.c_str(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 191 | coords, |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 192 | textureSamplers, |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 193 | texelBuffers); |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 194 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 195 | fragProc->emitCode(args); |
| 196 | |
| 197 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 198 | // asks for dst color, then the emit code needs to follow suit |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 199 | SkDEBUGCODE(verify(fp);) |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame^] | 200 | glslFragmentProcessors->emplace_back(fragProc); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 201 | |
| 202 | fFS.codeAppend("}"); |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 203 | return output; |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 206 | void GrGLSLProgramBuilder::emitAndInstallXferProc(const SkString& colorIn, |
| 207 | const SkString& coverageIn) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 208 | // Program builders have a bit of state we need to clear with each effect |
| 209 | AutoStageAdvance adv(this); |
| 210 | |
| 211 | SkASSERT(!fXferProcessor); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 212 | const GrXferProcessor& xp = fPipeline.getXferProcessor(); |
Robert Phillips | 369e8b7 | 2017-08-01 16:13:04 -0400 | [diff] [blame] | 213 | fXferProcessor.reset(xp.createGLSLInstance()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 214 | |
| 215 | // Enable dual source secondary output if we have one |
| 216 | if (xp.hasSecondaryOutput()) { |
| 217 | fFS.enableSecondaryOutput(); |
| 218 | } |
| 219 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 220 | if (this->shaderCaps()->mustDeclareFragmentShaderOutput()) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 221 | fFS.enableCustomOutput(); |
| 222 | } |
| 223 | |
| 224 | SkString openBrace; |
| 225 | openBrace.printf("{ // Xfer Processor: %s\n", xp.name()); |
| 226 | fFS.codeAppend(openBrace.c_str()); |
| 227 | |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 228 | SamplerHandle dstTextureSamplerHandle; |
| 229 | GrSurfaceOrigin dstTextureOrigin = kTopLeft_GrSurfaceOrigin; |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 230 | |
| 231 | if (GrTexture* dstTexture = fPipeline.peekDstTexture()) { |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 232 | // GrProcessor::TextureSampler sampler(dstTexture); |
| 233 | SkString name("DstTextureSampler"); |
| 234 | dstTextureSamplerHandle = |
| 235 | this->emitSampler(dstTexture->texturePriv().samplerType(), dstTexture->config(), |
| 236 | "DstTextureSampler", kFragment_GrShaderFlag); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 237 | dstTextureOrigin = fPipeline.dstTextureProxy()->origin(); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 238 | SkASSERT(kTextureExternalSampler_GrSLType != dstTexture->texturePriv().samplerType()); |
| 239 | } |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 240 | |
| 241 | GrGLSLXferProcessor::EmitArgs args(&fFS, |
| 242 | this->uniformHandler(), |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 243 | this->shaderCaps(), |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 244 | xp, |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 245 | colorIn.size() ? colorIn.c_str() : "float4(1)", |
| 246 | coverageIn.size() ? coverageIn.c_str() : "float4(1)", |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 247 | fFS.getPrimaryColorOutputName(), |
| 248 | fFS.getSecondaryColorOutputName(), |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 249 | dstTextureSamplerHandle, |
| 250 | dstTextureOrigin); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 251 | fXferProcessor->emitCode(args); |
| 252 | |
| 253 | // We have to check that effects and the code they emit are consistent, ie if an effect |
| 254 | // asks for dst color, then the emit code needs to follow suit |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 255 | SkDEBUGCODE(verify(xp);) |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 256 | fFS.codeAppend("}"); |
| 257 | } |
| 258 | |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 259 | void GrGLSLProgramBuilder::emitSamplers( |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 260 | const GrResourceIOProcessor& processor, |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 261 | SkTArray<SamplerHandle>* outTexSamplerHandles, |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 262 | SkTArray<TexelBufferHandle>* outTexelBufferHandles) { |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 263 | SkString name; |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 264 | int numTextureSamplers = processor.numTextureSamplers(); |
| 265 | for (int t = 0; t < numTextureSamplers; ++t) { |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 266 | const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(t); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 267 | name.printf("TextureSampler_%d", outTexSamplerHandles->count()); |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 268 | GrSLType samplerType = sampler.peekTexture()->texturePriv().samplerType(); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 269 | outTexSamplerHandles->emplace_back(this->emitSampler( |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 270 | samplerType, sampler.peekTexture()->config(), name.c_str(), sampler.visibility())); |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 271 | } |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 272 | if (int numBuffers = processor.numBuffers()) { |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 273 | SkASSERT(this->shaderCaps()->texelBufferSupport()); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 274 | GrShaderFlags texelBufferVisibility = kNone_GrShaderFlags; |
| 275 | |
| 276 | for (int b = 0; b < numBuffers; ++b) { |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 277 | const GrResourceIOProcessor::BufferAccess& access = processor.bufferAccess(b); |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 278 | name.printf("TexelBuffer_%d", outTexelBufferHandles->count()); |
| 279 | outTexelBufferHandles->emplace_back( |
| 280 | this->emitTexelBuffer(access.texelConfig(), name.c_str(), access.visibility())); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 281 | texelBufferVisibility |= access.visibility(); |
| 282 | } |
| 283 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 284 | if (const char* extension = this->shaderCaps()->texelBufferExtensionString()) { |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 285 | this->addFeature(texelBufferVisibility, |
| 286 | 1 << GrGLSLShaderBuilder::kTexelBuffer_GLSLPrivateFeature, |
| 287 | extension); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 292 | void GrGLSLProgramBuilder::updateSamplerCounts(GrShaderFlags visibility) { |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 293 | if (visibility & kVertex_GrShaderFlag) { |
| 294 | ++fNumVertexSamplers; |
| 295 | } |
| 296 | if (visibility & kGeometry_GrShaderFlag) { |
| 297 | SkASSERT(this->primitiveProcessor().willUseGeoShader()); |
| 298 | ++fNumGeometrySamplers; |
| 299 | } |
| 300 | if (visibility & kFragment_GrShaderFlag) { |
| 301 | ++fNumFragmentSamplers; |
| 302 | } |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | GrGLSLProgramBuilder::SamplerHandle GrGLSLProgramBuilder::emitSampler(GrSLType samplerType, |
| 306 | GrPixelConfig config, |
| 307 | const char* name, |
| 308 | GrShaderFlags visibility) { |
| 309 | this->updateSamplerCounts(visibility); |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 310 | GrSLPrecision precision = GrSLSamplerPrecision(config); |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 311 | GrSwizzle swizzle = this->shaderCaps()->configTextureSwizzle(config); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 312 | return this->uniformHandler()->addSampler(visibility, swizzle, samplerType, precision, name); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 313 | } |
| 314 | |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 315 | GrGLSLProgramBuilder::TexelBufferHandle GrGLSLProgramBuilder::emitTexelBuffer( |
| 316 | GrPixelConfig config, const char* name, GrShaderFlags visibility) { |
| 317 | this->updateSamplerCounts(visibility); |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 318 | GrSLPrecision precision = GrSLSamplerPrecision(config); |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 319 | return this->uniformHandler()->addTexelBuffer(visibility, precision, name); |
| 320 | } |
| 321 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 322 | void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) { |
| 323 | // Swizzle the fragment shader outputs if necessary. |
| 324 | GrSwizzle swizzle; |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 325 | swizzle.setFromKey(this->desc()->header().fOutputSwizzle); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 326 | if (swizzle != GrSwizzle::RGBA()) { |
| 327 | fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(), |
| 328 | fFS.getPrimaryColorOutputName(), |
| 329 | swizzle.c_str()); |
| 330 | if (hasSecondaryOutput) { |
| 331 | fFS.codeAppendf("%s = %s.%s;", fFS.getSecondaryColorOutputName(), |
| 332 | fFS.getSecondaryColorOutputName(), |
| 333 | swizzle.c_str()); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 338 | bool GrGLSLProgramBuilder::checkSamplerCounts() { |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 339 | const GrShaderCaps& shaderCaps = *this->shaderCaps(); |
| 340 | if (fNumVertexSamplers > shaderCaps.maxVertexSamplers()) { |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 341 | GrCapsDebugf(this->caps(), "Program would use too many vertex samplers\n"); |
| 342 | return false; |
| 343 | } |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 344 | if (fNumGeometrySamplers > shaderCaps.maxGeometrySamplers()) { |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 345 | GrCapsDebugf(this->caps(), "Program would use too many geometry samplers\n"); |
| 346 | return false; |
| 347 | } |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 348 | if (fNumFragmentSamplers > shaderCaps.maxFragmentSamplers()) { |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 349 | GrCapsDebugf(this->caps(), "Program would use too many fragment samplers\n"); |
| 350 | return false; |
| 351 | } |
| 352 | // If the same sampler is used in two different shaders, it counts as two combined samplers. |
| 353 | int numCombinedSamplers = fNumVertexSamplers + fNumGeometrySamplers + fNumFragmentSamplers; |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 354 | if (numCombinedSamplers > shaderCaps.maxCombinedSamplers()) { |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 355 | GrCapsDebugf(this->caps(), "Program would use too many combined samplers\n"); |
| 356 | return false; |
| 357 | } |
| 358 | return true; |
| 359 | } |
| 360 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 361 | #ifdef SK_DEBUG |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 362 | void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) { |
| 366 | SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor()); |
| 367 | } |
| 368 | |
| 369 | void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) { |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 370 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 371 | #endif |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 372 | |
| 373 | void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) { |
| 374 | if ('\0' == prefix) { |
| 375 | *out = name; |
| 376 | } else { |
| 377 | out->printf("%c%s", prefix, name); |
| 378 | } |
| 379 | if (mangle) { |
| 380 | if (out->endsWith('_')) { |
| 381 | // Names containing "__" are reserved. |
| 382 | out->append("x"); |
| 383 | } |
| 384 | out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str()); |
| 385 | } |
| 386 | } |
| 387 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 388 | void GrGLSLProgramBuilder::nameExpression(SkString* output, const char* baseName) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 389 | // create var to hold stage result. If we already have a valid output name, just use that |
| 390 | // otherwise create a new mangled one. This name is only valid if we are reordering stages |
| 391 | // and have to tell stage exactly where to put its output. |
| 392 | SkString outName; |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 393 | if (output->size()) { |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 394 | outName = output->c_str(); |
| 395 | } else { |
| 396 | this->nameVariable(&outName, '\0', baseName); |
| 397 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 398 | fFS.codeAppendf("half4 %s;", outName.c_str()); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 399 | *output = outName; |
| 400 | } |
| 401 | |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 402 | void GrGLSLProgramBuilder::appendUniformDecls(GrShaderFlags visibility, SkString* out) const { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 403 | this->uniformHandler()->appendUniformDecls(visibility, out); |
| 404 | } |
| 405 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 406 | void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 407 | SkASSERT(!fUniformHandles.fRTHeightUni.isValid()); |
| 408 | GrGLSLUniformHandler* uniformHandler = this->uniformHandler(); |
| 409 | fUniformHandles.fRTHeightUni = |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 410 | uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 411 | kHalf_GrSLType, kDefault_GrSLPrecision, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 412 | name, false, 0, nullptr); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 413 | } |
| 414 | |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 415 | void GrGLSLProgramBuilder::finalizeShaders() { |
| 416 | this->varyingHandler()->finalize(); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 417 | fVS.finalize(kVertex_GrShaderFlag); |
csmartdalton | 276cc41 | 2016-11-21 11:55:00 -0700 | [diff] [blame] | 418 | if (this->primitiveProcessor().willUseGeoShader()) { |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 419 | SkASSERT(this->shaderCaps()->geometryShaderSupport()); |
csmartdalton | 276cc41 | 2016-11-21 11:55:00 -0700 | [diff] [blame] | 420 | fGS.finalize(kGeometry_GrShaderFlag); |
| 421 | } |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 422 | fFS.finalize(kFragment_GrShaderFlag); |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 423 | } |