blob: 900b6eee6b7044940bf7eae68e9548c2e8a1e27b [file] [log] [blame]
egdaniel8dcdedc2015-11-11 06:27:20 -08001/*
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
egdanielfa896322016-01-13 12:19:30 -080010#include "GrPipeline.h"
11#include "glsl/GrGLSLFragmentProcessor.h"
12#include "glsl/GrGLSLGeometryProcessor.h"
13#include "glsl/GrGLSLXferProcessor.h"
14
egdaniel8dcdedc2015-11-11 06:27:20 -080015const int GrGLSLProgramBuilder::kVarsPerBlock = 8;
16
17GrGLSLProgramBuilder::GrGLSLProgramBuilder(const DrawArgs& args)
18 : fVS(this)
19 , fGS(this)
20 , fFS(this, args.fDesc->header().fFragPosKey)
21 , fStageIndex(-1)
egdanielfa896322016-01-13 12:19:30 -080022 , fArgs(args)
23 , fGeometryProcessor(nullptr)
24 , fXferProcessor(nullptr) {
25}
26
27bool GrGLSLProgramBuilder::emitAndInstallProcs(GrGLSLExpr4* inputColor,
28 GrGLSLExpr4* inputCoverage,
29 int maxTextures) {
30 // First we loop over all of the installed processors and collect coord transforms. These will
31 // be sent to the GrGLSLPrimitiveProcessor in its emitCode function
32 const GrPrimitiveProcessor& primProc = this->primitiveProcessor();
33 int totalTextures = primProc.numTextures();
34
35 for (int i = 0; i < this->pipeline().numFragmentProcessors(); i++) {
36 const GrFragmentProcessor& processor = this->pipeline().getFragmentProcessor(i);
37
38 if (!primProc.hasTransformedLocalCoords()) {
39 SkTArray<const GrCoordTransform*, true>& procCoords = fCoordTransforms.push_back();
40 processor.gatherCoordTransforms(&procCoords);
41 }
42
43 totalTextures += processor.numTextures();
44 if (totalTextures >= maxTextures) {
45 GrCapsDebugf(this->caps(), "Program would use too many texture units\n");
46 return false;
47 }
48 }
49
50 this->emitAndInstallPrimProc(primProc, inputColor, inputCoverage);
51
52 int numProcs = this->pipeline().numFragmentProcessors();
53 this->emitAndInstallFragProcs(0, this->pipeline().numColorFragmentProcessors(), inputColor);
54 this->emitAndInstallFragProcs(this->pipeline().numColorFragmentProcessors(), numProcs,
55 inputCoverage);
ethannicholas22793252016-01-30 09:59:10 -080056 if (primProc.getPixelLocalStorageState() !=
57 GrPixelLocalStorageState::kDraw_GrPixelLocalStorageState) {
58 this->emitAndInstallXferProc(this->pipeline().getXferProcessor(), *inputColor,
59 *inputCoverage, this->pipeline().ignoresCoverage(),
60 primProc.getPixelLocalStorageState());
61 this->emitFSOutputSwizzle(this->pipeline().getXferProcessor().hasSecondaryOutput());
62 }
egdanielfa896322016-01-13 12:19:30 -080063 return true;
64}
65
66void GrGLSLProgramBuilder::emitAndInstallPrimProc(const GrPrimitiveProcessor& proc,
67 GrGLSLExpr4* outputColor,
68 GrGLSLExpr4* outputCoverage) {
69 // Program builders have a bit of state we need to clear with each effect
70 AutoStageAdvance adv(this);
71 this->nameExpression(outputColor, "outputColor");
72 this->nameExpression(outputCoverage, "outputCoverage");
73
74 // Enclose custom code in a block to avoid namespace conflicts
75 SkString openBrace;
76 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, proc.name());
77 fFS.codeAppend(openBrace.c_str());
78 fVS.codeAppendf("// Primitive Processor %s\n", proc.name());
79
80 SkASSERT(!fGeometryProcessor);
81 fGeometryProcessor = proc.createGLSLInstance(*this->glslCaps());
82
83 SkSTArray<4, GrGLSLTextureSampler> samplers(proc.numTextures());
84 this->emitSamplers(proc, &samplers);
85
86 GrGLSLGeometryProcessor::EmitArgs args(&fVS,
87 &fFS,
88 this->varyingHandler(),
89 this->uniformHandler(),
90 this->glslCaps(),
91 proc,
92 outputColor->c_str(),
93 outputCoverage->c_str(),
94 samplers,
95 fCoordTransforms,
96 &fOutCoords);
97 fGeometryProcessor->emitCode(args);
98
99 // We have to check that effects and the code they emit are consistent, ie if an effect
100 // asks for dst color, then the emit code needs to follow suit
101 verify(proc);
102
103 fFS.codeAppend("}");
104}
105
106void GrGLSLProgramBuilder::emitAndInstallFragProcs(int procOffset,
107 int numProcs,
108 GrGLSLExpr4* inOut) {
109 for (int i = procOffset; i < numProcs; ++i) {
110 GrGLSLExpr4 output;
111 const GrFragmentProcessor& fp = this->pipeline().getFragmentProcessor(i);
112 this->emitAndInstallFragProc(fp, i, *inOut, &output);
113 *inOut = output;
114 }
115}
116
117// TODO Processors cannot output zeros because an empty string is all 1s
118// the fix is to allow effects to take the GrGLSLExpr4 directly
119void GrGLSLProgramBuilder::emitAndInstallFragProc(const GrFragmentProcessor& fp,
120 int index,
121 const GrGLSLExpr4& input,
122 GrGLSLExpr4* output) {
123 // Program builders have a bit of state we need to clear with each effect
124 AutoStageAdvance adv(this);
125 this->nameExpression(output, "output");
126
127 // Enclose custom code in a block to avoid namespace conflicts
128 SkString openBrace;
129 openBrace.printf("{ // Stage %d, %s\n", fStageIndex, fp.name());
130 fFS.codeAppend(openBrace.c_str());
131
132 GrGLSLFragmentProcessor* fragProc = fp.createGLSLInstance();
133
134 SkSTArray<4, GrGLSLTextureSampler> samplers(fp.numTextures());
135 this->emitSamplers(fp, &samplers);
136
137 GrGLSLFragmentProcessor::EmitArgs args(&fFS,
138 this->uniformHandler(),
139 this->glslCaps(),
140 fp,
141 output->c_str(),
142 input.isOnes() ? nullptr : input.c_str(),
143 fOutCoords[index],
144 samplers);
145 fragProc->emitCode(args);
146
147 // We have to check that effects and the code they emit are consistent, ie if an effect
148 // asks for dst color, then the emit code needs to follow suit
149 verify(fp);
150 fFragmentProcessors.push_back(fragProc);
151
152 fFS.codeAppend("}");
153}
154
155void GrGLSLProgramBuilder::emitAndInstallXferProc(const GrXferProcessor& xp,
156 const GrGLSLExpr4& colorIn,
157 const GrGLSLExpr4& coverageIn,
ethannicholas22793252016-01-30 09:59:10 -0800158 bool ignoresCoverage,
159 GrPixelLocalStorageState plsState) {
egdanielfa896322016-01-13 12:19:30 -0800160 // Program builders have a bit of state we need to clear with each effect
161 AutoStageAdvance adv(this);
162
163 SkASSERT(!fXferProcessor);
164 fXferProcessor = xp.createGLSLInstance();
165
166 // Enable dual source secondary output if we have one
167 if (xp.hasSecondaryOutput()) {
168 fFS.enableSecondaryOutput();
169 }
170
171 if (this->glslCaps()->mustDeclareFragmentShaderOutput()) {
172 fFS.enableCustomOutput();
173 }
174
175 SkString openBrace;
176 openBrace.printf("{ // Xfer Processor: %s\n", xp.name());
177 fFS.codeAppend(openBrace.c_str());
178
179 SkSTArray<4, GrGLSLTextureSampler> samplers(xp.numTextures());
180 this->emitSamplers(xp, &samplers);
181
ethannicholas22793252016-01-30 09:59:10 -0800182 bool usePLSDstRead = (plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState);
egdanielfa896322016-01-13 12:19:30 -0800183 GrGLSLXferProcessor::EmitArgs args(&fFS,
184 this->uniformHandler(),
185 this->glslCaps(),
186 xp, colorIn.c_str(),
187 ignoresCoverage ? nullptr : coverageIn.c_str(),
188 fFS.getPrimaryColorOutputName(),
189 fFS.getSecondaryColorOutputName(),
ethannicholas22793252016-01-30 09:59:10 -0800190 samplers,
191 usePLSDstRead);
egdanielfa896322016-01-13 12:19:30 -0800192 fXferProcessor->emitCode(args);
193
194 // We have to check that effects and the code they emit are consistent, ie if an effect
195 // asks for dst color, then the emit code needs to follow suit
196 verify(xp);
197 fFS.codeAppend("}");
198}
199
200void GrGLSLProgramBuilder::emitFSOutputSwizzle(bool hasSecondaryOutput) {
201 // Swizzle the fragment shader outputs if necessary.
202 GrSwizzle swizzle;
203 swizzle.setFromKey(this->desc().header().fOutputSwizzle);
204 if (swizzle != GrSwizzle::RGBA()) {
205 fFS.codeAppendf("%s = %s.%s;", fFS.getPrimaryColorOutputName(),
206 fFS.getPrimaryColorOutputName(),
207 swizzle.c_str());
208 if (hasSecondaryOutput) {
209 fFS.codeAppendf("%s = %s.%s;", fFS.getSecondaryColorOutputName(),
210 fFS.getSecondaryColorOutputName(),
211 swizzle.c_str());
212 }
213 }
214}
215
216void GrGLSLProgramBuilder::verify(const GrPrimitiveProcessor& gp) {
217 SkASSERT(fFS.hasReadFragmentPosition() == gp.willReadFragmentPosition());
218}
219
220void GrGLSLProgramBuilder::verify(const GrXferProcessor& xp) {
221 SkASSERT(fFS.hasReadDstColor() == xp.willReadDstColor());
222}
223
224void GrGLSLProgramBuilder::verify(const GrFragmentProcessor& fp) {
225 SkASSERT(fFS.hasReadFragmentPosition() == fp.willReadFragmentPosition());
egdaniel8dcdedc2015-11-11 06:27:20 -0800226}
227
228void GrGLSLProgramBuilder::nameVariable(SkString* out, char prefix, const char* name, bool mangle) {
229 if ('\0' == prefix) {
230 *out = name;
231 } else {
232 out->printf("%c%s", prefix, name);
233 }
234 if (mangle) {
235 if (out->endsWith('_')) {
236 // Names containing "__" are reserved.
237 out->append("x");
238 }
239 out->appendf("_Stage%d%s", fStageIndex, fFS.getMangleString().c_str());
240 }
241}
242
egdanielfa896322016-01-13 12:19:30 -0800243void GrGLSLProgramBuilder::nameExpression(GrGLSLExpr4* output, const char* baseName) {
244 // create var to hold stage result. If we already have a valid output name, just use that
245 // otherwise create a new mangled one. This name is only valid if we are reordering stages
246 // and have to tell stage exactly where to put its output.
247 SkString outName;
248 if (output->isValid()) {
249 outName = output->c_str();
250 } else {
251 this->nameVariable(&outName, '\0', baseName);
252 }
253 fFS.codeAppendf("vec4 %s;", outName.c_str());
254 *output = outName;
255}
256
egdaniel8dcdedc2015-11-11 06:27:20 -0800257void GrGLSLProgramBuilder::appendUniformDecls(ShaderVisibility visibility,
258 SkString* out) const {
egdaniel7ea439b2015-12-03 09:20:44 -0800259 this->uniformHandler()->appendUniformDecls(visibility, out);
260}
261
262void GrGLSLProgramBuilder::addRTAdjustmentUniform(GrSLPrecision precision,
263 const char* name,
264 const char** outName) {
265 SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid());
266 fUniformHandles.fRTAdjustmentUni =
267 this->uniformHandler()->addUniform(GrGLSLUniformHandler::kVertex_Visibility,
268 kVec4f_GrSLType,
269 precision,
270 name,
271 outName);
272}
273
274void GrGLSLProgramBuilder::addRTHeightUniform(const char* name, const char** outName) {
275 SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
276 GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
277 fUniformHandles.fRTHeightUni =
278 uniformHandler->internalAddUniformArray(GrGLSLUniformHandler::kFragment_Visibility,
279 kFloat_GrSLType, kDefault_GrSLPrecision,
280 name, false, 0, outName);
egdaniel8dcdedc2015-11-11 06:27:20 -0800281}
282
egdanielfa896322016-01-13 12:19:30 -0800283void GrGLSLProgramBuilder::cleanupFragmentProcessors() {
284 for (int i = 0; i < fFragmentProcessors.count(); ++i) {
285 delete fFragmentProcessors[i];
286 }
287}
288