blob: bd406f4c0c1812b880f3e01e3918fdfb385496bf [file] [log] [blame]
junov@google.comf93e7172011-03-31 21:26:24 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 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.
junov@google.comf93e7172011-03-31 21:26:24 +00006 */
7
8#include "GrGLProgram.h"
9
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000010#include "GrAllocator.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000011#include "GrCustomStage.h"
12#include "GrGLProgramStage.h"
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000013#include "gl/GrGLShaderBuilder.h"
bsalomon@google.com4fa66942011-09-20 19:06:12 +000014#include "GrGLShaderVar.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000015#include "GrProgramStageFactory.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000016#include "SkTrace.h"
Scroggo97c88c22011-05-11 14:05:25 +000017#include "SkXfermode.h"
18
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000019SK_DEFINE_INST_COUNT(GrGLProgram)
20
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000021#define GL_CALL(X) GR_GL_CALL(fContextInfo.interface(), X)
22#define GL_CALL_RET(R, X) GR_GL_CALL_RET(fContextInfo.interface(), R, X)
23
rileya@google.come38160c2012-07-03 18:03:04 +000024#define PRINT_SHADERS 0
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000025
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000026typedef GrGLProgram::Desc::StageDesc StageDesc;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000027
junov@google.comf93e7172011-03-31 21:26:24 +000028#define POS_ATTR_NAME "aPosition"
29#define COL_ATTR_NAME "aColor"
bsalomon@google.coma3108262011-10-10 14:08:47 +000030#define COV_ATTR_NAME "aCoverage"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000031#define EDGE_ATTR_NAME "aEdge"
junov@google.comf93e7172011-03-31 21:26:24 +000032
bsalomon@google.com4fa66942011-09-20 19:06:12 +000033namespace {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000034inline void tex_attr_name(int coordIdx, SkString* s) {
junov@google.comf93e7172011-03-31 21:26:24 +000035 *s = "aTexCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +000036 s->appendS32(coordIdx);
junov@google.comf93e7172011-03-31 21:26:24 +000037}
38
bsalomon@google.com4fa66942011-09-20 19:06:12 +000039inline const char* float_vector_type_str(int count) {
tomhudson@google.com168e6342012-04-18 17:49:20 +000040 return GrGLShaderVar::TypeString(GrSLFloatVectorType(count));
junov@google.comf93e7172011-03-31 21:26:24 +000041}
42
bsalomon@google.com4fa66942011-09-20 19:06:12 +000043inline const char* vector_all_coords(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +000044 static const char* ALL[] = {"ERROR", "", ".xy", ".xyz", ".xyzw"};
45 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ALL));
46 return ALL[count];
47}
48
bsalomon@google.com4fa66942011-09-20 19:06:12 +000049inline const char* all_ones_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +000050 static const char* ONESVEC[] = {"ERROR", "1.0", "vec2(1,1)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +000051 "vec3(1,1,1)", "vec4(1,1,1,1)"};
52 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ONESVEC));
53 return ONESVEC[count];
54}
55
bsalomon@google.com4fa66942011-09-20 19:06:12 +000056inline const char* all_zeros_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +000057 static const char* ZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +000058 "vec3(0,0,0)", "vec4(0,0,0,0)"};
59 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ZEROSVEC));
60 return ZEROSVEC[count];
61}
62
bsalomon@google.com4fa66942011-09-20 19:06:12 +000063inline const char* declared_color_output_name() { return "fsColorOut"; }
64inline const char* dual_source_output_name() { return "dualSourceOut"; }
bsalomon@google.com271cffc2011-05-20 14:13:56 +000065
bsalomon@google.com4fa66942011-09-20 19:06:12 +000066}
junov@google.com6acc9b32011-05-16 18:32:07 +000067
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000068GrGLProgram* GrGLProgram::Create(const GrGLContextInfo& gl,
69 const Desc& desc,
bsalomon@google.comcddaf342012-07-30 13:09:05 +000070 const GrCustomStage** customStages) {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000071 GrGLProgram* program = SkNEW_ARGS(GrGLProgram, (gl, desc, customStages));
72 if (!program->succeeded()) {
73 delete program;
74 program = NULL;
75 }
76 return program;
77}
78
79GrGLProgram::GrGLProgram(const GrGLContextInfo& gl,
80 const Desc& desc,
bsalomon@google.comcddaf342012-07-30 13:09:05 +000081 const GrCustomStage** customStages)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000082: fContextInfo(gl)
83, fUniformManager(gl) {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000084 fDesc = desc;
85 fVShaderID = 0;
86 fGShaderID = 0;
87 fFShaderID = 0;
88 fProgramID = 0;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000089
90 fViewMatrix = GrMatrix::InvalidMatrix();
91 fViewportSize.set(-1, -1);
92 fColor = GrColor_ILLEGAL;
93 fColorFilterColor = GrColor_ILLEGAL;
94
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000095 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
96 fProgramStage[s] = NULL;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000097 fTextureMatrices[s] = GrMatrix::InvalidMatrix();
98 // this is arbitrary, just initialize to something
99 fTextureOrientation[s] = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000100 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000101
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000102 this->genProgram(customStages);
junov@google.comf93e7172011-03-31 21:26:24 +0000103}
104
105GrGLProgram::~GrGLProgram() {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000106 if (fVShaderID) {
107 GL_CALL(DeleteShader(fVShaderID));
108 }
109 if (fGShaderID) {
110 GL_CALL(DeleteShader(fGShaderID));
111 }
112 if (fFShaderID) {
113 GL_CALL(DeleteShader(fFShaderID));
114 }
115 if (fProgramID) {
116 GL_CALL(DeleteProgram(fProgramID));
117 }
118
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000119 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
120 delete fProgramStage[i];
121 }
junov@google.comf93e7172011-03-31 21:26:24 +0000122}
123
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000124void GrGLProgram::abandon() {
125 fVShaderID = 0;
126 fGShaderID = 0;
127 fFShaderID = 0;
128 fProgramID = 0;
129}
130
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000131void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000132 GrBlendCoeff* dstCoeff) const {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000133 switch (fDesc.fDualSrcOutput) {
134 case Desc::kNone_DualSrcOutput:
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000135 break;
136 // the prog will write a coverage value to the secondary
137 // output and the dst is blended by one minus that value.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000138 case Desc::kCoverage_DualSrcOutput:
139 case Desc::kCoverageISA_DualSrcOutput:
140 case Desc::kCoverageISC_DualSrcOutput:
bsalomon@google.com47059542012-06-06 20:51:20 +0000141 *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000142 break;
143 default:
144 GrCrash("Unexpected dual source blend output");
145 break;
146 }
147}
148
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000149// assigns modulation of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000150// vars can be vec4s or floats (or one of each)
151// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000152// if either var is "" then assign to the other var
153// if both are "" then assign all ones
154static inline void modulate_helper(const char* outputVar,
155 const char* var0,
156 const char* var1,
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000157 SkString* code) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000158 GrAssert(NULL != outputVar);
159 GrAssert(NULL != var0);
160 GrAssert(NULL != var1);
161 GrAssert(NULL != code);
162
163 bool has0 = '\0' != *var0;
164 bool has1 = '\0' != *var1;
165
166 if (!has0 && !has1) {
167 code->appendf("\t%s = %s;\n", outputVar, all_ones_vec(4));
168 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000169 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000170 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000171 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000172 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000173 code->appendf("\t%s = vec4(%s * %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000174 }
175}
176
177// assigns addition of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000178// vars can be vec4s or floats (or one of each)
179// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000180// if either var is "" then assign to the other var
181// if both are "" then assign all zeros
182static inline void add_helper(const char* outputVar,
183 const char* var0,
184 const char* var1,
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000185 SkString* code) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000186 GrAssert(NULL != outputVar);
187 GrAssert(NULL != var0);
188 GrAssert(NULL != var1);
189 GrAssert(NULL != code);
190
191 bool has0 = '\0' != *var0;
192 bool has1 = '\0' != *var1;
193
194 if (!has0 && !has1) {
195 code->appendf("\t%s = %s;\n", outputVar, all_zeros_vec(4));
196 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000197 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000198 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000199 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000200 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000201 code->appendf("\t%s = vec4(%s + %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000202 }
203}
204
205// given two blend coeffecients determine whether the src
206// and/or dst computation can be omitted.
207static inline void needBlendInputs(SkXfermode::Coeff srcCoeff,
208 SkXfermode::Coeff dstCoeff,
209 bool* needSrcValue,
210 bool* needDstValue) {
211 if (SkXfermode::kZero_Coeff == srcCoeff) {
212 switch (dstCoeff) {
213 // these all read the src
214 case SkXfermode::kSC_Coeff:
215 case SkXfermode::kISC_Coeff:
216 case SkXfermode::kSA_Coeff:
217 case SkXfermode::kISA_Coeff:
218 *needSrcValue = true;
219 break;
220 default:
221 *needSrcValue = false;
222 break;
223 }
224 } else {
225 *needSrcValue = true;
226 }
227 if (SkXfermode::kZero_Coeff == dstCoeff) {
228 switch (srcCoeff) {
229 // these all read the dst
230 case SkXfermode::kDC_Coeff:
231 case SkXfermode::kIDC_Coeff:
232 case SkXfermode::kDA_Coeff:
233 case SkXfermode::kIDA_Coeff:
234 *needDstValue = true;
235 break;
236 default:
237 *needDstValue = false;
238 break;
239 }
240 } else {
241 *needDstValue = true;
Scroggo97c88c22011-05-11 14:05:25 +0000242 }
243}
244
245/**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000246 * Create a blend_coeff * value string to be used in shader code. Sets empty
247 * string if result is trivially zero.
248 */
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000249static void blendTermString(SkString* str, SkXfermode::Coeff coeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000250 const char* src, const char* dst,
251 const char* value) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000252 switch (coeff) {
253 case SkXfermode::kZero_Coeff: /** 0 */
254 *str = "";
255 break;
256 case SkXfermode::kOne_Coeff: /** 1 */
257 *str = value;
258 break;
259 case SkXfermode::kSC_Coeff:
260 str->printf("(%s * %s)", src, value);
261 break;
262 case SkXfermode::kISC_Coeff:
263 str->printf("((%s - %s) * %s)", all_ones_vec(4), src, value);
264 break;
265 case SkXfermode::kDC_Coeff:
266 str->printf("(%s * %s)", dst, value);
267 break;
268 case SkXfermode::kIDC_Coeff:
269 str->printf("((%s - %s) * %s)", all_ones_vec(4), dst, value);
270 break;
271 case SkXfermode::kSA_Coeff: /** src alpha */
272 str->printf("(%s.a * %s)", src, value);
273 break;
274 case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */
275 str->printf("((1.0 - %s.a) * %s)", src, value);
276 break;
277 case SkXfermode::kDA_Coeff: /** dst alpha */
278 str->printf("(%s.a * %s)", dst, value);
279 break;
280 case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */
281 str->printf("((1.0 - %s.a) * %s)", dst, value);
282 break;
283 default:
284 GrCrash("Unexpected xfer coeff.");
285 break;
286 }
287}
288/**
Scroggo97c88c22011-05-11 14:05:25 +0000289 * Adds a line to the fragment shader code which modifies the color by
290 * the specified color filter.
291 */
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000292static void addColorFilter(SkString* fsCode, const char * outputVar,
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000293 SkXfermode::Coeff uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000294 SkXfermode::Coeff colorCoeff,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000295 const char* filterColor,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000296 const char* inColor) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000297 SkString colorStr, constStr;
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000298 blendTermString(&colorStr, colorCoeff, filterColor, inColor, inColor);
299 blendTermString(&constStr, uniformCoeff, filterColor, inColor, filterColor);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000300
301 add_helper(outputVar, colorStr.c_str(), constStr.c_str(), fsCode);
Scroggo97c88c22011-05-11 14:05:25 +0000302}
303
bsalomon@google.comd4726202012-08-03 14:34:46 +0000304bool GrGLProgram::genEdgeCoverage(SkString* coverageVar,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000305 GrGLShaderBuilder* segments) const {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000306 if (fDesc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000307 const char *vsName, *fsName;
tomhudson@google.com23cb2292012-05-30 18:26:03 +0000308 segments->addVarying(kVec4f_GrSLType, "Edge", &vsName, &fsName);
tomhudson@google.com168e6342012-04-18 17:49:20 +0000309 segments->fVSAttrs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000310 GrGLShaderVar::kAttribute_TypeModifier, EDGE_ATTR_NAME);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000311 segments->fVSCode.appendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000312 switch (fDesc.fVertexEdgeType) {
bsalomon@google.com93c96602012-04-27 13:05:21 +0000313 case GrDrawState::kHairLine_EdgeType:
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000314 segments->fFSCode.appendf("\tfloat edgeAlpha = abs(dot(vec3(gl_FragCoord.xy,1), %s.xyz));\n", fsName);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000315 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
bsalomon@google.com93c96602012-04-27 13:05:21 +0000316 break;
317 case GrDrawState::kQuad_EdgeType:
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000318 segments->fFSCode.append("\tfloat edgeAlpha;\n");
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000319 // keep the derivative instructions outside the conditional
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000320 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
321 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
322 segments->fFSCode.appendf("\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName);
323 // today we know z and w are in device space. We could use derivatives
324 segments->fFSCode.appendf("\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName, fsName);
325 segments->fFSCode.append ("\t} else {\n");
326 segments->fFSCode.appendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
327 "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
328 fsName, fsName);
329 segments->fFSCode.appendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000330 segments->fFSCode.append("\t\tedgeAlpha = clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n"
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000331 "\t}\n");
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000332 if (kES2_GrGLBinding == fContextInfo.binding()) {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000333 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
334 }
bsalomon@google.com93c96602012-04-27 13:05:21 +0000335 break;
336 case GrDrawState::kHairQuad_EdgeType:
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000337 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
338 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000339 segments->fFSCode.appendf("\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
340 "\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
341 fsName, fsName);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000342 segments->fFSCode.appendf("\tfloat edgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000343 segments->fFSCode.append("\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n");
344 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000345 if (kES2_GrGLBinding == fContextInfo.binding()) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000346 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
347 }
bsalomon@google.com93c96602012-04-27 13:05:21 +0000348 break;
349 case GrDrawState::kCircle_EdgeType:
350 segments->fFSCode.append("\tfloat edgeAlpha;\n");
351 segments->fFSCode.appendf("\tfloat d = distance(gl_FragCoord.xy, %s.xy);\n", fsName);
352 segments->fFSCode.appendf("\tfloat outerAlpha = smoothstep(d - 0.5, d + 0.5, %s.z);\n", fsName);
353 segments->fFSCode.appendf("\tfloat innerAlpha = %s.w == 0.0 ? 1.0 : smoothstep(%s.w - 0.5, %s.w + 0.5, d);\n", fsName, fsName, fsName);
354 segments->fFSCode.append("\tedgeAlpha = outerAlpha * innerAlpha;\n");
355 break;
356 default:
357 GrCrash("Unknown Edge Type!");
358 break;
bsalomon@google.com66105672011-09-15 15:12:00 +0000359 }
bsalomon@google.com66105672011-09-15 15:12:00 +0000360 *coverageVar = "edgeAlpha";
bsalomon@google.comd4726202012-08-03 14:34:46 +0000361 return true;
bsalomon@google.com66105672011-09-15 15:12:00 +0000362 } else {
363 coverageVar->reset();
bsalomon@google.comd4726202012-08-03 14:34:46 +0000364 return false;
bsalomon@google.com66105672011-09-15 15:12:00 +0000365 }
366}
367
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000368void GrGLProgram::genInputColor(GrGLShaderBuilder* builder, SkString* inColor) {
369 switch (fDesc.fColorInput) {
370 case GrGLProgram::Desc::kAttribute_ColorInput: {
371 builder->fVSAttrs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000372 GrGLShaderVar::kAttribute_TypeModifier,
373 COL_ATTR_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000374 const char *vsName, *fsName;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000375 builder->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName);
376 builder->fVSCode.appendf("\t%s = " COL_ATTR_NAME ";\n", vsName);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000377 *inColor = fsName;
378 } break;
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000379 case GrGLProgram::Desc::kUniform_ColorInput: {
380 const char* name;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000381 fUniforms.fColorUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000382 kVec4f_GrSLType, "Color", &name);
383 *inColor = name;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000384 break;
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000385 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000386 case GrGLProgram::Desc::kTransBlack_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000387 GrAssert(!"needComputedColor should be false.");
388 break;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000389 case GrGLProgram::Desc::kSolidWhite_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000390 break;
391 default:
392 GrCrash("Unknown color type.");
393 break;
394 }
395}
396
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000397void GrGLProgram::genUniformCoverage(GrGLShaderBuilder* builder, SkString* inOutCoverage) {
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000398 const char* covUniName;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000399 fUniforms.fCoverageUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000400 kVec4f_GrSLType, "Coverage", &covUniName);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000401 if (inOutCoverage->size()) {
402 builder->fFSCode.appendf("\tvec4 uniCoverage = %s * %s;\n",
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000403 covUniName, inOutCoverage->c_str());
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000404 *inOutCoverage = "uniCoverage";
405 } else {
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000406 *inOutCoverage = covUniName;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000407 }
408}
409
410namespace {
411void gen_attribute_coverage(GrGLShaderBuilder* segments,
412 SkString* inOutCoverage) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000413 segments->fVSAttrs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000414 GrGLShaderVar::kAttribute_TypeModifier,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000415 COV_ATTR_NAME);
416 const char *vsName, *fsName;
tomhudson@google.com23cb2292012-05-30 18:26:03 +0000417 segments->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000418 segments->fVSCode.appendf("\t%s = " COV_ATTR_NAME ";\n", vsName);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000419 if (inOutCoverage->size()) {
420 segments->fFSCode.appendf("\tvec4 attrCoverage = %s * %s;\n",
421 fsName, inOutCoverage->c_str());
422 *inOutCoverage = "attrCoverage";
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000423 } else {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000424 *inOutCoverage = fsName;
425 }
426}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000427}
428
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000429void GrGLProgram::genGeometryShader(GrGLShaderBuilder* segments) const {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000430#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000431 if (fDesc.fExperimentalGS) {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000432 GrAssert(fContextInfo.glslGeneration() >= k150_GrGLSLGeneration);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000433 segments->fGSHeader.append("layout(triangles) in;\n"
434 "layout(triangle_strip, max_vertices = 6) out;\n");
435 segments->fGSCode.append("void main() {\n"
436 "\tfor (int i = 0; i < 3; ++i) {\n"
437 "\t\tgl_Position = gl_in[i].gl_Position;\n");
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000438 if (fDesc.fEmitsPointSize) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000439 segments->fGSCode.append("\t\tgl_PointSize = 1.0;\n");
440 }
441 GrAssert(segments->fGSInputs.count() == segments->fGSOutputs.count());
442 int count = segments->fGSInputs.count();
443 for (int i = 0; i < count; ++i) {
444 segments->fGSCode.appendf("\t\t%s = %s[i];\n",
445 segments->fGSOutputs[i].getName().c_str(),
446 segments->fGSInputs[i].getName().c_str());
447 }
448 segments->fGSCode.append("\t\tEmitVertex();\n"
449 "\t}\n"
450 "\tEndPrimitive();\n"
451 "}\n");
452 }
453#endif
454}
455
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000456const char* GrGLProgram::adjustInColor(const SkString& inColor) const {
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000457 if (inColor.size()) {
458 return inColor.c_str();
459 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000460 if (Desc::kSolidWhite_ColorInput == fDesc.fColorInput) {
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000461 return all_ones_vec(4);
462 } else {
463 return all_zeros_vec(4);
464 }
465 }
466}
467
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000468namespace {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000469// prints a shader using params similar to glShaderSource
470void print_shader(GrGLint stringCnt,
471 const GrGLchar** strings,
472 GrGLint* stringLengths) {
473 for (int i = 0; i < stringCnt; ++i) {
474 if (NULL == stringLengths || stringLengths[i] < 0) {
475 GrPrintf(strings[i]);
476 } else {
477 GrPrintf("%.*s", stringLengths[i], strings[i]);
478 }
479 }
480}
481
482// Compiles a GL shader, returns shader ID or 0 if failed params have same meaning as glShaderSource
483GrGLuint compile_shader(const GrGLContextInfo& gl,
484 GrGLenum type,
485 int stringCnt,
486 const char** strings,
487 int* stringLengths) {
488 SK_TRACE_EVENT1("GrGLProgram::CompileShader",
489 "stringCount", SkStringPrintf("%i", stringCnt).c_str());
490
491 GrGLuint shader;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000492 GR_GL_CALL_RET(gl.interface(), shader, CreateShader(type));
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000493 if (0 == shader) {
494 return 0;
495 }
496
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000497 const GrGLInterface* gli = gl.interface();
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000498 GrGLint compiled = GR_GL_INIT_ZERO;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000499 GR_GL_CALL(gli, ShaderSource(shader, stringCnt, strings, stringLengths));
500 GR_GL_CALL(gli, CompileShader(shader));
501 GR_GL_CALL(gli, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled));
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000502
503 if (!compiled) {
504 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000505 GR_GL_CALL(gli, GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000506 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
507 if (infoLen > 0) {
508 // retrieve length even though we don't need it to workaround bug in chrome cmd buffer
509 // param validation.
510 GrGLsizei length = GR_GL_INIT_ZERO;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000511 GR_GL_CALL(gli, GetShaderInfoLog(shader, infoLen+1,
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000512 &length, (char*)log.get()));
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000513 print_shader(stringCnt, strings, stringLengths);
514 GrPrintf("\n%s", log.get());
515 }
516 GrAssert(!"Shader compilation failed!");
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000517 GR_GL_CALL(gli, DeleteShader(shader));
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000518 return 0;
519 }
520 return shader;
521}
522
523// helper version of above for when shader is already flattened into a single SkString
524GrGLuint compile_shader(const GrGLContextInfo& gl, GrGLenum type, const SkString& shader) {
525 const GrGLchar* str = shader.c_str();
526 int length = shader.size();
527 return compile_shader(gl, type, 1, &str, &length);
528}
529
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000530}
531
532// compiles all the shaders from builder and stores the shader IDs
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000533bool GrGLProgram::compileShaders(const GrGLShaderBuilder& builder) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000534
535 SkString shader;
536
537 builder.getShader(GrGLShaderBuilder::kVertex_ShaderType, &shader);
538#if PRINT_SHADERS
539 GrPrintf(shader.c_str());
540 GrPrintf("\n");
541#endif
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000542 if (!(fVShaderID = compile_shader(fContextInfo, GR_GL_VERTEX_SHADER, shader))) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000543 return false;
544 }
545
546 if (builder.fUsesGS) {
547 builder.getShader(GrGLShaderBuilder::kGeometry_ShaderType, &shader);
548#if PRINT_SHADERS
549 GrPrintf(shader.c_str());
550 GrPrintf("\n");
551#endif
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000552 if (!(fGShaderID = compile_shader(fContextInfo, GR_GL_GEOMETRY_SHADER, shader))) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000553 return false;
554 }
555 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000556 fGShaderID = 0;
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000557 }
558
559 builder.getShader(GrGLShaderBuilder::kFragment_ShaderType, &shader);
560#if PRINT_SHADERS
561 GrPrintf(shader.c_str());
562 GrPrintf("\n");
563#endif
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000564 if (!(fFShaderID = compile_shader(fContextInfo, GR_GL_FRAGMENT_SHADER, shader))) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000565 return false;
566 }
567
568 return true;
569}
570
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000571bool GrGLProgram::genProgram(const GrCustomStage** customStages) {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000572 GrAssert(0 == fProgramID);
573
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000574 GrGLShaderBuilder builder(fContextInfo, fUniformManager);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000575 const uint32_t& layout = fDesc.fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000576
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000577#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000578 builder.fUsesGS = fDesc.fExperimentalGS;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000579#endif
580
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000581 SkXfermode::Coeff colorCoeff, uniformCoeff;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000582 bool applyColorMatrix = SkToBool(fDesc.fColorMatrixEnabled);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000583 // The rest of transfer mode color filters have not been implemented
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000584 if (fDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000585 GR_DEBUGCODE(bool success =)
tomhudson@google.com0d831722011-06-02 15:37:14 +0000586 SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode>
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000587 (fDesc.fColorFilterXfermode),
tomhudson@google.com0d831722011-06-02 15:37:14 +0000588 &uniformCoeff, &colorCoeff);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000589 GR_DEBUGASSERT(success);
590 } else {
591 colorCoeff = SkXfermode::kOne_Coeff;
592 uniformCoeff = SkXfermode::kZero_Coeff;
593 }
594
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000595 // no need to do the color filter / matrix at all if coverage is 0. The
596 // output color is scaled by the coverage. All the dual source outputs are
597 // scaled by the coverage as well.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000598 if (Desc::kTransBlack_ColorInput == fDesc.fCoverageInput) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000599 colorCoeff = SkXfermode::kZero_Coeff;
600 uniformCoeff = SkXfermode::kZero_Coeff;
601 applyColorMatrix = false;
602 }
603
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000604 // If we know the final color is going to be all zeros then we can
605 // simplify the color filter coeffecients. needComputedColor will then
606 // come out false below.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000607 if (Desc::kTransBlack_ColorInput == fDesc.fColorInput) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000608 colorCoeff = SkXfermode::kZero_Coeff;
609 if (SkXfermode::kDC_Coeff == uniformCoeff ||
610 SkXfermode::kDA_Coeff == uniformCoeff) {
611 uniformCoeff = SkXfermode::kZero_Coeff;
612 } else if (SkXfermode::kIDC_Coeff == uniformCoeff ||
613 SkXfermode::kIDA_Coeff == uniformCoeff) {
614 uniformCoeff = SkXfermode::kOne_Coeff;
615 }
616 }
617
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000618 bool needColorFilterUniform;
619 bool needComputedColor;
620 needBlendInputs(uniformCoeff, colorCoeff,
621 &needColorFilterUniform, &needComputedColor);
622
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000623 // the dual source output has no canonical var name, have to
624 // declare an output, which is incompatible with gl_FragColor/gl_FragData.
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000625 bool dualSourceOutputWritten = false;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000626 builder.fHeader.append(GrGetGLSLVersionDecl(fContextInfo.binding(),
627 fContextInfo.glslGeneration()));
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000628
629 GrGLShaderVar colorOutput;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000630 bool isColorDeclared = GrGLSLSetupFSColorOuput(fContextInfo.glslGeneration(),
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000631 declared_color_output_name(),
632 &colorOutput);
633 if (isColorDeclared) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000634 builder.fFSOutputs.push_back(colorOutput);
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000635 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000636
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000637 const char* viewMName;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000638 fUniforms.fViewMatrixUni = builder.addUniform(GrGLShaderBuilder::kVertex_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000639 kMat33f_GrSLType, "ViewM", &viewMName);
bsalomon@google.com341767c2012-05-11 20:47:39 +0000640
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000641 builder.fVSAttrs.push_back().set(kVec2f_GrSLType,
642 GrGLShaderVar::kAttribute_TypeModifier,
643 POS_ATTR_NAME);
junov@google.comf93e7172011-03-31 21:26:24 +0000644
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000645 builder.fVSCode.appendf("void main() {\n"
646 "\tvec3 pos3 = %s * vec3("POS_ATTR_NAME", 1);\n"
647 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n",
648 viewMName);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000649
bsalomon@google.com91961302011-05-09 18:39:58 +0000650 // incoming color to current stage being processed.
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000651 SkString inColor;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000652
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000653 if (needComputedColor) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000654 this->genInputColor(&builder, &inColor);
junov@google.comf93e7172011-03-31 21:26:24 +0000655 }
656
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000657 // we output point size in the GS if present
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000658 if (fDesc.fEmitsPointSize && !builder.fUsesGS){
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000659 builder.fVSCode.append("\tgl_PointSize = 1.0;\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000660 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000661
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000662 builder.fFSCode.append("void main() {\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000663
664 // add texture coordinates that are used to the list of vertex attr decls
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000665 SkString texCoordAttrs[GrDrawState::kMaxTexCoords];
tomhudson@google.com93813632011-10-27 20:21:16 +0000666 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000667 if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000668 tex_attr_name(t, texCoordAttrs + t);
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000669 builder.fVSAttrs.push_back().set(kVec2f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000670 GrGLShaderVar::kAttribute_TypeModifier,
671 texCoordAttrs[t].c_str());
junov@google.comf93e7172011-03-31 21:26:24 +0000672 }
673 }
674
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000675 ///////////////////////////////////////////////////////////////////////////
676 // compute the final color
Scroggo97c88c22011-05-11 14:05:25 +0000677
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000678 // if we have color stages string them together, feeding the output color
679 // of each to the next and generating code for each stage.
680 if (needComputedColor) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000681 SkString outColor;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000682 for (int s = 0; s < fDesc.fFirstCoverageStage; ++s) {
683 if (fDesc.fStages[s].isEnabled()) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000684 // create var to hold stage result
685 outColor = "color";
686 outColor.appendS32(s);
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000687 builder.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000688
689 const char* inCoords;
690 // figure out what our input coords are
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000691 int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout);
692 if (tcIdx < 0) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000693 inCoords = POS_ATTR_NAME;
Scroggo97c88c22011-05-11 14:05:25 +0000694 } else {
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000695 // must have input tex coordinates if stage is enabled.
Scroggo97c88c22011-05-11 14:05:25 +0000696 GrAssert(texCoordAttrs[tcIdx].size());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000697 inCoords = texCoordAttrs[tcIdx].c_str();
junov@google.comf93e7172011-03-31 21:26:24 +0000698 }
699
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000700 if (NULL != customStages[s]) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000701 const GrProgramStageFactory& factory = customStages[s]->getFactory();
702 fProgramStage[s] = factory.createGLInstance(*customStages[s]);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000703 }
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000704 this->genStageCode(s,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000705 inColor.size() ? inColor.c_str() : NULL,
706 outColor.c_str(),
707 inCoords,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000708 &builder);
junov@google.comf93e7172011-03-31 21:26:24 +0000709 inColor = outColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000710 }
711 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000712 }
Scroggo97c88c22011-05-11 14:05:25 +0000713
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000714 // if have all ones or zeros for the "dst" input to the color filter then we
715 // may be able to make additional optimizations.
716 if (needColorFilterUniform && needComputedColor && !inColor.size()) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000717 GrAssert(Desc::kSolidWhite_ColorInput == fDesc.fColorInput);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000718 bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff ||
719 SkXfermode::kIDA_Coeff == uniformCoeff;
720 if (uniformCoeffIsZero) {
721 uniformCoeff = SkXfermode::kZero_Coeff;
722 bool bogus;
723 needBlendInputs(SkXfermode::kZero_Coeff, colorCoeff,
724 &needColorFilterUniform, &bogus);
725 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000726 }
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000727 const char* colorFilterColorUniName = NULL;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000728 if (needColorFilterUniform) {
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000729 fUniforms.fColorFilterUni = builder.addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000730 kVec4f_GrSLType, "FilterColor",
731 &colorFilterColorUniName);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000732 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000733 bool wroteFragColorZero = false;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000734 if (SkXfermode::kZero_Coeff == uniformCoeff &&
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000735 SkXfermode::kZero_Coeff == colorCoeff &&
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000736 !applyColorMatrix) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000737 builder.fFSCode.appendf("\t%s = %s;\n",
738 colorOutput.getName().c_str(),
739 all_zeros_vec(4));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000740 wroteFragColorZero = true;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000741 } else if (SkXfermode::kDst_Mode != fDesc.fColorFilterXfermode) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000742 builder.fFSCode.append("\tvec4 filteredColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000743 const char* color = adjustInColor(inColor);
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000744 addColorFilter(&builder.fFSCode, "filteredColor", uniformCoeff,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000745 colorCoeff, colorFilterColorUniName, color);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000746 inColor = "filteredColor";
747 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000748 if (applyColorMatrix) {
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000749 const char* colMatrixName;
750 const char* colMatrixVecName;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000751 fUniforms.fColorMatrixUni = builder.addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000752 kMat44f_GrSLType, "ColorMatrix",
753 &colMatrixName);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000754 fUniforms.fColorMatrixVecUni = builder.addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000755 kVec4f_GrSLType, "ColorMatrixVec",
756 &colMatrixVecName);
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000757 const char* color = adjustInColor(inColor);
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000758 builder.fFSCode.appendf("\tvec4 matrixedColor = %s * vec4(%s.rgb / %s.a, %s.a) + %s;\n",
759 colMatrixName, color, color, color, colMatrixVecName);
760 builder.fFSCode.append("\tmatrixedColor.rgb *= matrixedColor.a;\n");
761
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000762 inColor = "matrixedColor";
763 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000764
765 ///////////////////////////////////////////////////////////////////////////
766 // compute the partial coverage (coverage stages and edge aa)
767
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000768 SkString inCoverage;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000769 bool coverageIsZero = Desc::kTransBlack_ColorInput == fDesc.fCoverageInput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000770 // we don't need to compute coverage at all if we know the final shader
771 // output will be zero and we don't have a dual src blend output.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000772 if (!wroteFragColorZero || Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000773
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000774 if (!coverageIsZero) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000775 bool inCoverageIsScalar = this->genEdgeCoverage(&inCoverage, &builder);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000776
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000777 switch (fDesc.fCoverageInput) {
778 case Desc::kSolidWhite_ColorInput:
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000779 // empty string implies solid white
780 break;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000781 case Desc::kAttribute_ColorInput:
782 gen_attribute_coverage(&builder, &inCoverage);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000783 inCoverageIsScalar = false;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000784 break;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000785 case Desc::kUniform_ColorInput:
786 this->genUniformCoverage(&builder, &inCoverage);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000787 inCoverageIsScalar = false;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000788 break;
789 default:
790 GrCrash("Unexpected input coverage.");
791 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000792
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000793 SkString outCoverage;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000794 const int& startStage = fDesc.fFirstCoverageStage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000795 for (int s = startStage; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000796 if (fDesc.fStages[s].isEnabled()) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000797 // create var to hold stage output
798 outCoverage = "coverage";
799 outCoverage.appendS32(s);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000800 builder.fFSCode.appendf("\tvec4 %s;\n", outCoverage.c_str());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000801
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000802 const char* inCoords;
803 // figure out what our input coords are
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000804 int tcIdx =
805 GrDrawTarget::VertexTexCoordsForStage(s, layout);
806 if (tcIdx < 0) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000807 inCoords = POS_ATTR_NAME;
808 } else {
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000809 // must have input tex coordinates if stage is
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000810 // enabled.
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000811 GrAssert(texCoordAttrs[tcIdx].size());
812 inCoords = texCoordAttrs[tcIdx].c_str();
813 }
814
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000815 if (NULL != customStages[s]) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000816 const GrProgramStageFactory& factory = customStages[s]->getFactory();
817 fProgramStage[s] = factory.createGLInstance(*customStages[s]);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000818 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000819 // stages don't know how to deal with a scalar input. (Maybe they should. We
bsalomon@google.comd4726202012-08-03 14:34:46 +0000820 // could pass a GrGLShaderVar)
821 if (inCoverageIsScalar) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000822 builder.fFSCode.appendf("\tvec4 %s4 = vec4(%s);\n",
bsalomon@google.comd4726202012-08-03 14:34:46 +0000823 inCoverage.c_str(), inCoverage.c_str());
824 inCoverage.append("4");
825 }
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000826 this->genStageCode(s,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000827 inCoverage.size() ? inCoverage.c_str() : NULL,
828 outCoverage.c_str(),
829 inCoords,
830 &builder);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000831 inCoverage = outCoverage;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000832 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000833 }
834 }
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000835
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000836 if (Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000837 builder.fFSOutputs.push_back().set(kVec4f_GrSLType,
838 GrGLShaderVar::kOut_TypeModifier,
839 dual_source_output_name());
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000840 bool outputIsZero = coverageIsZero;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000841 SkString coeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000842 if (!outputIsZero &&
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000843 Desc::kCoverage_DualSrcOutput != fDesc.fDualSrcOutput && !wroteFragColorZero) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000844 if (!inColor.size()) {
845 outputIsZero = true;
846 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000847 if (Desc::kCoverageISA_DualSrcOutput == fDesc.fDualSrcOutput) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000848 coeff.printf("(1 - %s.a)", inColor.c_str());
849 } else {
850 coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str());
851 }
852 }
853 }
854 if (outputIsZero) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000855 builder.fFSCode.appendf("\t%s = %s;\n",
856 dual_source_output_name(),
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000857 all_zeros_vec(4));
858 } else {
859 modulate_helper(dual_source_output_name(),
860 coeff.c_str(),
861 inCoverage.c_str(),
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000862 &builder.fFSCode);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000863 }
864 dualSourceOutputWritten = true;
865 }
junov@google.comf93e7172011-03-31 21:26:24 +0000866 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000867
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000868 ///////////////////////////////////////////////////////////////////////////
869 // combine color and coverage as frag color
870
871 if (!wroteFragColorZero) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000872 if (coverageIsZero) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000873 builder.fFSCode.appendf("\t%s = %s;\n",
874 colorOutput.getName().c_str(),
875 all_zeros_vec(4));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000876 } else {
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000877 modulate_helper(colorOutput.getName().c_str(),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000878 inColor.c_str(),
879 inCoverage.c_str(),
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000880 &builder.fFSCode);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000881 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000882 if (Desc::kUnpremultiplied_RoundDown_OutputConfig == fDesc.fOutputConfig) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000883 builder.fFSCode.appendf("\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.rgb / %s.a * 255.0)/255.0, %s.a);\n",
884 colorOutput.getName().c_str(),
885 colorOutput.getName().c_str(),
886 colorOutput.getName().c_str(),
887 colorOutput.getName().c_str(),
888 colorOutput.getName().c_str());
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000889 } else if (Desc::kUnpremultiplied_RoundUp_OutputConfig == fDesc.fOutputConfig) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000890 builder.fFSCode.appendf("\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.rgb / %s.a * 255.0)/255.0, %s.a);\n",
891 colorOutput.getName().c_str(),
892 colorOutput.getName().c_str(),
893 colorOutput.getName().c_str(),
894 colorOutput.getName().c_str(),
895 colorOutput.getName().c_str());
bsalomon@google.comc4364992011-11-07 15:54:49 +0000896 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000897 }
898
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000899 builder.fVSCode.append("}\n");
900 builder.fFSCode.append("}\n");
bsalomon@google.com91961302011-05-09 18:39:58 +0000901
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000902 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000903 // insert GS
904#if GR_DEBUG
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000905 this->genGeometryShader(&builder);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000906#endif
907
908 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000909 // compile and setup attribs and unis
910
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000911 if (!this->compileShaders(builder)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000912 return false;
913 }
914
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000915 if (!this->bindOutputsAttribsAndLinkProgram(texCoordAttrs,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000916 isColorDeclared,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000917 dualSourceOutputWritten)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000918 return false;
919 }
920
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000921 builder.finished(fProgramID);
922 this->initSamplerUniforms();
bsalomon@google.com91961302011-05-09 18:39:58 +0000923
924 return true;
925}
926
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000927bool GrGLProgram::bindOutputsAttribsAndLinkProgram(SkString texCoordAttrNames[],
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000928 bool bindColorOut,
929 bool bindDualSrcOut) {
930 GL_CALL_RET(fProgramID, CreateProgram());
931 if (!fProgramID) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000932 return false;
933 }
bsalomon@google.com91961302011-05-09 18:39:58 +0000934
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000935 GL_CALL(AttachShader(fProgramID, fVShaderID));
936 if (fGShaderID) {
937 GL_CALL(AttachShader(fProgramID, fGShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000938 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000939 GL_CALL(AttachShader(fProgramID, fFShaderID));
bsalomon@google.com91961302011-05-09 18:39:58 +0000940
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000941 if (bindColorOut) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000942 GL_CALL(BindFragDataLocation(fProgramID, 0, declared_color_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000943 }
944 if (bindDualSrcOut) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000945 GL_CALL(BindFragDataLocationIndexed(fProgramID, 0, 1, dual_source_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000946 }
947
bsalomon@google.com91961302011-05-09 18:39:58 +0000948 // Bind the attrib locations to same values for all shaders
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000949 GL_CALL(BindAttribLocation(fProgramID, PositionAttributeIdx(), POS_ATTR_NAME));
tomhudson@google.com93813632011-10-27 20:21:16 +0000950 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000951 if (texCoordAttrNames[t].size()) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000952 GL_CALL(BindAttribLocation(fProgramID,
bsalomon@google.com96399942012-02-13 14:39:16 +0000953 TexCoordAttributeIdx(t),
954 texCoordAttrNames[t].c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +0000955 }
956 }
957
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000958 GL_CALL(BindAttribLocation(fProgramID, ColorAttributeIdx(), COL_ATTR_NAME));
959 GL_CALL(BindAttribLocation(fProgramID, CoverageAttributeIdx(), COV_ATTR_NAME));
960 GL_CALL(BindAttribLocation(fProgramID, EdgeAttributeIdx(), EDGE_ATTR_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +0000961
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000962 GL_CALL(LinkProgram(fProgramID));
bsalomon@google.com91961302011-05-09 18:39:58 +0000963
964 GrGLint linked = GR_GL_INIT_ZERO;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000965 GL_CALL(GetProgramiv(fProgramID, GR_GL_LINK_STATUS, &linked));
bsalomon@google.com91961302011-05-09 18:39:58 +0000966 if (!linked) {
967 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000968 GL_CALL(GetProgramiv(fProgramID, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000969 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
bsalomon@google.com91961302011-05-09 18:39:58 +0000970 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +0000971 // retrieve length even though we don't need it to workaround
972 // bug in chrome cmd buffer param validation.
973 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000974 GL_CALL(GetProgramInfoLog(fProgramID,
bsalomon@google.com96399942012-02-13 14:39:16 +0000975 infoLen+1,
976 &length,
977 (char*)log.get()));
bsalomon@google.com91961302011-05-09 18:39:58 +0000978 GrPrintf((char*)log.get());
979 }
980 GrAssert(!"Error linking program");
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000981 GL_CALL(DeleteProgram(fProgramID));
982 fProgramID = 0;
bsalomon@google.com91961302011-05-09 18:39:58 +0000983 return false;
984 }
985 return true;
986}
987
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000988void GrGLProgram::initSamplerUniforms() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000989 GL_CALL(UseProgram(fProgramID));
tomhudson@google.com93813632011-10-27 20:21:16 +0000990 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com0982d352012-07-31 15:33:25 +0000991 int count = fUniforms.fStages[s].fSamplerUniforms.count();
992 // FIXME: We're still always reserving one texture per stage. After GrTextureParams are
993 // expressed by the custom stage rather than the GrSamplerState we can move texture binding
994 // into GrGLProgram and it should be easier to fix this.
995 GrAssert(count <= 1);
996 for (int t = 0; t < count; ++t) {
997 UniformHandle uh = fUniforms.fStages[s].fSamplerUniforms[t];
998 if (GrGLUniformManager::kInvalidUniformHandle != uh) {
999 fUniformManager.setSampler(uh, s);
1000 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001001 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001002 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001003}
1004
bsalomon@google.comeb715c82012-07-11 15:03:31 +00001005///////////////////////////////////////////////////////////////////////////////
junov@google.comf93e7172011-03-31 21:26:24 +00001006// Stage code generation
junov@google.comf93e7172011-03-31 21:26:24 +00001007
bsalomon@google.comecb60aa2012-07-18 13:20:29 +00001008void GrGLProgram::genStageCode(int stageNum,
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001009 const char* fsInColor, // NULL means no incoming color
1010 const char* fsOutColor,
1011 const char* vsInCoord,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001012 GrGLShaderBuilder* builder) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001013 GrAssert(stageNum >= 0 && stageNum <= GrDrawState::kNumStages);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +00001014
1015 const GrGLProgram::StageDesc& desc = fDesc.fStages[stageNum];
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +00001016 StageUniforms& uniforms = fUniforms.fStages[stageNum];
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +00001017 GrGLProgramStage* customStage = fProgramStage[stageNum];
1018
1019 GrAssert((desc.fInConfigFlags & StageDesc::kInConfigBitMask) == desc.fInConfigFlags);
junov@google.comf93e7172011-03-31 21:26:24 +00001020
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001021 builder->setCurrentStage(stageNum);
1022
tomhudson@google.com23cb2292012-05-30 18:26:03 +00001023 /// Vertex Shader Stuff
1024
bsalomon@google.comeb715c82012-07-11 15:03:31 +00001025 // decide whether we need a matrix to transform texture coords and whether the varying needs a
1026 // perspective coord.
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001027 const char* matName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001028 if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) {
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001029 builder->fVaryingDims = builder->fCoordDims;
junov@google.comf93e7172011-03-31 21:26:24 +00001030 } else {
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001031 uniforms.fTextureMatrixUni = builder->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
1032 kMat33f_GrSLType, "TexM", &matName);
1033 const GrGLShaderVar& mat = builder->getUniformVariable(uniforms.fTextureMatrixUni);
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001034
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001035 if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) {
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001036 builder->fVaryingDims = builder->fCoordDims;
junov@google.comf93e7172011-03-31 21:26:24 +00001037 } else {
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001038 builder->fVaryingDims = builder->fCoordDims + 1;
junov@google.comf93e7172011-03-31 21:26:24 +00001039 }
1040 }
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001041 GrAssert(builder->fVaryingDims > 0);
junov@google.comf93e7172011-03-31 21:26:24 +00001042
tomhudson@google.com898e7b52012-06-01 20:42:15 +00001043 // Must setup variables after computing segments->fVaryingDims
1044 if (NULL != customStage) {
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001045 customStage->setupVariables(builder);
tomhudson@google.com898e7b52012-06-01 20:42:15 +00001046 }
1047
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001048 const char* samplerName;
bsalomon@google.com0982d352012-07-31 15:33:25 +00001049 uniforms.fSamplerUniforms.push_back(builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
1050 kSampler2D_GrSLType,
1051 "Sampler",
1052 &samplerName));
junov@google.comf93e7172011-03-31 21:26:24 +00001053
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001054 const char *varyingVSName, *varyingFSName;
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001055 builder->addVarying(GrSLFloatVectorType(builder->fVaryingDims),
1056 "Stage",
1057 &varyingVSName,
1058 &varyingFSName);
junov@google.comf93e7172011-03-31 21:26:24 +00001059
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001060 if (!matName) {
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001061 GrAssert(builder->fVaryingDims == builder->fCoordDims);
1062 builder->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord);
junov@google.comf93e7172011-03-31 21:26:24 +00001063 } else {
bsalomon@google.com91961302011-05-09 18:39:58 +00001064 // varying = texMatrix * texCoord
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001065 builder->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n",
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001066 varyingVSName, matName, vsInCoord,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001067 vector_all_coords(builder->fVaryingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001068 }
1069
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001070 if (NULL != customStage) {
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001071 builder->fVSCode.appendf("\t{ // stage %d %s\n",
1072 stageNum, customStage->name());
1073 customStage->emitVS(builder, varyingVSName);
1074 builder->fVSCode.appendf("\t}\n");
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001075 }
1076
junov@google.comf93e7172011-03-31 21:26:24 +00001077 /// Fragment Shader Stuff
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001078
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001079 builder->fSampleCoords = varyingFSName;
tomhudson@google.com5440f062012-06-01 15:55:50 +00001080
tomhudson@google.comde788232012-08-02 20:13:12 +00001081 builder->setupTextureAccess(stageNum);
junov@google.comf93e7172011-03-31 21:26:24 +00001082
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001083 builder->computeSwizzle(desc.fInConfigFlags);
1084 builder->computeModulate(fsInColor);
junov@google.comf93e7172011-03-31 21:26:24 +00001085
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001086 static const uint32_t kMulByAlphaMask =
1087 (StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
1088 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag);
1089
tomhudson@google.com6a820b62012-05-24 15:10:14 +00001090 // NOTE: GrGLProgramStages are now responsible for fetching
1091 if (NULL == customStage) {
bsalomon@google.comd2ae1fa2012-06-04 20:06:02 +00001092 if (desc.fInConfigFlags & kMulByAlphaMask) {
1093 // only one of the mul by alpha flags should be set
1094 GrAssert(GrIsPow2(kMulByAlphaMask & desc.fInConfigFlags));
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001095 GrAssert(!(desc.fInConfigFlags &
bsalomon@google.comd2ae1fa2012-06-04 20:06:02 +00001096 StageDesc::kSmearAlpha_InConfigFlag));
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001097 GrAssert(!(desc.fInConfigFlags &
bsalomon@google.comd2ae1fa2012-06-04 20:06:02 +00001098 StageDesc::kSmearRed_InConfigFlag));
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001099 builder->fFSCode.appendf("\t%s = %s(%s, %s)%s;\n",
1100 fsOutColor,
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001101 builder->fTexFunc.c_str(),
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001102 samplerName,
1103 builder->fSampleCoords.c_str(),
1104 builder->fSwizzle.c_str());
bsalomon@google.comd2ae1fa2012-06-04 20:06:02 +00001105 if (desc.fInConfigFlags &
1106 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag) {
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001107 builder->fFSCode.appendf("\t%s = vec4(ceil(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
1108 fsOutColor, fsOutColor, fsOutColor,
1109 fsOutColor, builder->fModulate.c_str());
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001110 } else {
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001111 builder->fFSCode.appendf("\t%s = vec4(floor(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
1112 fsOutColor, fsOutColor, fsOutColor,
1113 fsOutColor, builder->fModulate.c_str());
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001114 }
bsalomon@google.comd2ae1fa2012-06-04 20:06:02 +00001115 } else {
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001116 builder->emitDefaultFetch(fsOutColor, samplerName);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001117 }
junov@google.comf93e7172011-03-31 21:26:24 +00001118 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001119
1120 if (NULL != customStage) {
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001121 // Enclose custom code in a block to avoid namespace conflicts
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001122 builder->fFSCode.appendf("\t{ // stage %d %s \n",
1123 stageNum, customStage->name());
1124 customStage->emitFS(builder, fsOutColor, fsInColor,
1125 samplerName);
1126 builder->fFSCode.appendf("\t}\n");
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001127 }
bsalomon@google.com777c3aa2012-07-25 20:58:20 +00001128 builder->setNonStage();
junov@google.comf93e7172011-03-31 21:26:24 +00001129}