blob: 8b8a9549deb80d5ead149f91b6385664c576a17e [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
junov@google.comf93e7172011-03-31 21:26:24 +000019namespace {
20
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +000021enum {
22 /// Used to mark a StageUniLocation field that should be bound
23 /// to a uniform during getUniformLocationsAndInitCache().
24 kUseUniform = 2000
25};
26
junov@google.comf93e7172011-03-31 21:26:24 +000027} // namespace
28
rileya@google.come38160c2012-07-03 18:03:04 +000029#define PRINT_SHADERS 0
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000030
bsalomon@google.com85b505b2011-11-07 14:56:51 +000031typedef GrGLProgram::ProgramDesc::StageDesc StageDesc;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000032
bsalomon@google.com341767c2012-05-11 20:47:39 +000033#define VIEW_MATRIX_NAME "uViewM"
junov@google.comf93e7172011-03-31 21:26:24 +000034
35#define POS_ATTR_NAME "aPosition"
36#define COL_ATTR_NAME "aColor"
bsalomon@google.coma3108262011-10-10 14:08:47 +000037#define COV_ATTR_NAME "aCoverage"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000038#define EDGE_ATTR_NAME "aEdge"
bsalomon@google.com4be283f2011-04-19 21:15:09 +000039#define COL_UNI_NAME "uColor"
bsalomon@google.com2401ae82012-01-17 21:03:05 +000040#define COV_UNI_NAME "uCoverage"
Scroggo97c88c22011-05-11 14:05:25 +000041#define COL_FILTER_UNI_NAME "uColorFilter"
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +000042#define COL_MATRIX_UNI_NAME "uColorMatrix"
43#define COL_MATRIX_VEC_UNI_NAME "uColorMatrixVec"
junov@google.comf93e7172011-03-31 21:26:24 +000044
bsalomon@google.com4fa66942011-09-20 19:06:12 +000045namespace {
46inline void tex_attr_name(int coordIdx, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +000047 *s = "aTexCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +000048 s->appendS32(coordIdx);
junov@google.comf93e7172011-03-31 21:26:24 +000049}
50
bsalomon@google.com4fa66942011-09-20 19:06:12 +000051inline const char* float_vector_type_str(int count) {
tomhudson@google.com168e6342012-04-18 17:49:20 +000052 return GrGLShaderVar::TypeString(GrSLFloatVectorType(count));
junov@google.comf93e7172011-03-31 21:26:24 +000053}
54
bsalomon@google.com4fa66942011-09-20 19:06:12 +000055inline const char* vector_all_coords(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +000056 static const char* ALL[] = {"ERROR", "", ".xy", ".xyz", ".xyzw"};
57 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ALL));
58 return ALL[count];
59}
60
bsalomon@google.com4fa66942011-09-20 19:06:12 +000061inline const char* all_ones_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +000062 static const char* ONESVEC[] = {"ERROR", "1.0", "vec2(1,1)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +000063 "vec3(1,1,1)", "vec4(1,1,1,1)"};
64 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ONESVEC));
65 return ONESVEC[count];
66}
67
bsalomon@google.com4fa66942011-09-20 19:06:12 +000068inline const char* all_zeros_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +000069 static const char* ZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +000070 "vec3(0,0,0)", "vec4(0,0,0,0)"};
71 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ZEROSVEC));
72 return ZEROSVEC[count];
73}
74
bsalomon@google.com4fa66942011-09-20 19:06:12 +000075inline const char* declared_color_output_name() { return "fsColorOut"; }
76inline const char* dual_source_output_name() { return "dualSourceOut"; }
bsalomon@google.com271cffc2011-05-20 14:13:56 +000077
bsalomon@google.com4fa66942011-09-20 19:06:12 +000078inline void tex_matrix_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +000079 *s = "uTexM";
bsalomon@google.comfc296292011-05-06 13:53:47 +000080 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +000081}
82
bsalomon@google.com4fa66942011-09-20 19:06:12 +000083inline void sampler_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +000084 *s = "uSampler";
bsalomon@google.comfc296292011-05-06 13:53:47 +000085 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +000086}
87
bsalomon@google.com4fa66942011-09-20 19:06:12 +000088inline void tex_domain_name(int stage, GrStringBuilder* s) {
junov@google.com6acc9b32011-05-16 18:32:07 +000089 *s = "uTexDom";
90 s->appendS32(stage);
91}
bsalomon@google.com4fa66942011-09-20 19:06:12 +000092}
junov@google.com6acc9b32011-05-16 18:32:07 +000093
junov@google.comf93e7172011-03-31 21:26:24 +000094GrGLProgram::GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +000095}
96
97GrGLProgram::~GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +000098}
99
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000100void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000101 GrBlendCoeff* dstCoeff) const {
102 switch (fProgramDesc.fDualSrcOutput) {
103 case ProgramDesc::kNone_DualSrcOutput:
104 break;
105 // the prog will write a coverage value to the secondary
106 // output and the dst is blended by one minus that value.
107 case ProgramDesc::kCoverage_DualSrcOutput:
108 case ProgramDesc::kCoverageISA_DualSrcOutput:
109 case ProgramDesc::kCoverageISC_DualSrcOutput:
bsalomon@google.com47059542012-06-06 20:51:20 +0000110 *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000111 break;
112 default:
113 GrCrash("Unexpected dual source blend output");
114 break;
115 }
116}
117
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000118// assigns modulation of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000119// vars can be vec4s or floats (or one of each)
120// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000121// if either var is "" then assign to the other var
122// if both are "" then assign all ones
123static inline void modulate_helper(const char* outputVar,
124 const char* var0,
125 const char* var1,
126 GrStringBuilder* code) {
127 GrAssert(NULL != outputVar);
128 GrAssert(NULL != var0);
129 GrAssert(NULL != var1);
130 GrAssert(NULL != code);
131
132 bool has0 = '\0' != *var0;
133 bool has1 = '\0' != *var1;
134
135 if (!has0 && !has1) {
136 code->appendf("\t%s = %s;\n", outputVar, all_ones_vec(4));
137 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000138 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000139 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000140 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000141 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000142 code->appendf("\t%s = vec4(%s * %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000143 }
144}
145
146// assigns addition of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000147// vars can be vec4s or floats (or one of each)
148// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000149// if either var is "" then assign to the other var
150// if both are "" then assign all zeros
151static inline void add_helper(const char* outputVar,
152 const char* var0,
153 const char* var1,
154 GrStringBuilder* code) {
155 GrAssert(NULL != outputVar);
156 GrAssert(NULL != var0);
157 GrAssert(NULL != var1);
158 GrAssert(NULL != code);
159
160 bool has0 = '\0' != *var0;
161 bool has1 = '\0' != *var1;
162
163 if (!has0 && !has1) {
164 code->appendf("\t%s = %s;\n", outputVar, all_zeros_vec(4));
165 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000166 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000167 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000168 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000169 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000170 code->appendf("\t%s = vec4(%s + %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000171 }
172}
173
174// given two blend coeffecients determine whether the src
175// and/or dst computation can be omitted.
176static inline void needBlendInputs(SkXfermode::Coeff srcCoeff,
177 SkXfermode::Coeff dstCoeff,
178 bool* needSrcValue,
179 bool* needDstValue) {
180 if (SkXfermode::kZero_Coeff == srcCoeff) {
181 switch (dstCoeff) {
182 // these all read the src
183 case SkXfermode::kSC_Coeff:
184 case SkXfermode::kISC_Coeff:
185 case SkXfermode::kSA_Coeff:
186 case SkXfermode::kISA_Coeff:
187 *needSrcValue = true;
188 break;
189 default:
190 *needSrcValue = false;
191 break;
192 }
193 } else {
194 *needSrcValue = true;
195 }
196 if (SkXfermode::kZero_Coeff == dstCoeff) {
197 switch (srcCoeff) {
198 // these all read the dst
199 case SkXfermode::kDC_Coeff:
200 case SkXfermode::kIDC_Coeff:
201 case SkXfermode::kDA_Coeff:
202 case SkXfermode::kIDA_Coeff:
203 *needDstValue = true;
204 break;
205 default:
206 *needDstValue = false;
207 break;
208 }
209 } else {
210 *needDstValue = true;
Scroggo97c88c22011-05-11 14:05:25 +0000211 }
212}
213
214/**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000215 * Create a blend_coeff * value string to be used in shader code. Sets empty
216 * string if result is trivially zero.
217 */
218static void blendTermString(GrStringBuilder* str, SkXfermode::Coeff coeff,
219 const char* src, const char* dst,
220 const char* value) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000221 switch (coeff) {
222 case SkXfermode::kZero_Coeff: /** 0 */
223 *str = "";
224 break;
225 case SkXfermode::kOne_Coeff: /** 1 */
226 *str = value;
227 break;
228 case SkXfermode::kSC_Coeff:
229 str->printf("(%s * %s)", src, value);
230 break;
231 case SkXfermode::kISC_Coeff:
232 str->printf("((%s - %s) * %s)", all_ones_vec(4), src, value);
233 break;
234 case SkXfermode::kDC_Coeff:
235 str->printf("(%s * %s)", dst, value);
236 break;
237 case SkXfermode::kIDC_Coeff:
238 str->printf("((%s - %s) * %s)", all_ones_vec(4), dst, value);
239 break;
240 case SkXfermode::kSA_Coeff: /** src alpha */
241 str->printf("(%s.a * %s)", src, value);
242 break;
243 case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */
244 str->printf("((1.0 - %s.a) * %s)", src, value);
245 break;
246 case SkXfermode::kDA_Coeff: /** dst alpha */
247 str->printf("(%s.a * %s)", dst, value);
248 break;
249 case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */
250 str->printf("((1.0 - %s.a) * %s)", dst, value);
251 break;
252 default:
253 GrCrash("Unexpected xfer coeff.");
254 break;
255 }
256}
257/**
Scroggo97c88c22011-05-11 14:05:25 +0000258 * Adds a line to the fragment shader code which modifies the color by
259 * the specified color filter.
260 */
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000261static void addColorFilter(GrStringBuilder* fsCode, const char * outputVar,
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000262 SkXfermode::Coeff uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000263 SkXfermode::Coeff colorCoeff,
264 const char* inColor) {
265 GrStringBuilder colorStr, constStr;
266 blendTermString(&colorStr, colorCoeff, COL_FILTER_UNI_NAME,
267 inColor, inColor);
268 blendTermString(&constStr, uniformCoeff, COL_FILTER_UNI_NAME,
269 inColor, COL_FILTER_UNI_NAME);
270
271 add_helper(outputVar, colorStr.c_str(), constStr.c_str(), fsCode);
Scroggo97c88c22011-05-11 14:05:25 +0000272}
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000273/**
274 * Adds code to the fragment shader code which modifies the color by
275 * the specified color matrix.
276 */
277static void addColorMatrix(GrStringBuilder* fsCode, const char * outputVar,
278 const char* inColor) {
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000279 fsCode->appendf("\t%s = %s * vec4(%s.rgb / %s.a, %s.a) + %s;\n", outputVar, COL_MATRIX_UNI_NAME, inColor, inColor, inColor, COL_MATRIX_VEC_UNI_NAME);
280 fsCode->appendf("\t%s.rgb *= %s.a;\n", outputVar, outputVar);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000281}
Scroggo97c88c22011-05-11 14:05:25 +0000282
bsalomon@google.com96399942012-02-13 14:39:16 +0000283void GrGLProgram::genEdgeCoverage(const GrGLContextInfo& gl,
bsalomon@google.com66105672011-09-15 15:12:00 +0000284 GrVertexLayout layout,
285 CachedData* programData,
286 GrStringBuilder* coverageVar,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000287 GrGLShaderBuilder* segments) const {
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000288 if (layout & GrDrawTarget::kEdge_VertexLayoutBit) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000289 const char *vsName, *fsName;
tomhudson@google.com23cb2292012-05-30 18:26:03 +0000290 segments->addVarying(kVec4f_GrSLType, "Edge", &vsName, &fsName);
tomhudson@google.com168e6342012-04-18 17:49:20 +0000291 segments->fVSAttrs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000292 GrGLShaderVar::kAttribute_TypeModifier, EDGE_ATTR_NAME);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000293 segments->fVSCode.appendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName);
bsalomon@google.com93c96602012-04-27 13:05:21 +0000294 switch (fProgramDesc.fVertexEdgeType) {
295 case GrDrawState::kHairLine_EdgeType:
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000296 segments->fFSCode.appendf("\tfloat edgeAlpha = abs(dot(vec3(gl_FragCoord.xy,1), %s.xyz));\n", fsName);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000297 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
bsalomon@google.com93c96602012-04-27 13:05:21 +0000298 break;
299 case GrDrawState::kQuad_EdgeType:
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000300 segments->fFSCode.append("\tfloat edgeAlpha;\n");
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000301 // keep the derivative instructions outside the conditional
302 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
303 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
304 segments->fFSCode.appendf("\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName);
305 // today we know z and w are in device space. We could use derivatives
306 segments->fFSCode.appendf("\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName, fsName);
307 segments->fFSCode.append ("\t} else {\n");
308 segments->fFSCode.appendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
309 "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
310 fsName, fsName);
311 segments->fFSCode.appendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000312 segments->fFSCode.append("\t\tedgeAlpha = clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n"
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000313 "\t}\n");
bsalomon@google.com96399942012-02-13 14:39:16 +0000314 if (kES2_GrGLBinding == gl.binding()) {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000315 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
316 }
bsalomon@google.com93c96602012-04-27 13:05:21 +0000317 break;
318 case GrDrawState::kHairQuad_EdgeType:
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000319 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
320 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000321 segments->fFSCode.appendf("\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
322 "\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
323 fsName, fsName);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000324 segments->fFSCode.appendf("\tfloat edgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000325 segments->fFSCode.append("\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n");
326 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
bsalomon@google.com96399942012-02-13 14:39:16 +0000327 if (kES2_GrGLBinding == gl.binding()) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000328 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
329 }
bsalomon@google.com93c96602012-04-27 13:05:21 +0000330 break;
331 case GrDrawState::kCircle_EdgeType:
332 segments->fFSCode.append("\tfloat edgeAlpha;\n");
333 segments->fFSCode.appendf("\tfloat d = distance(gl_FragCoord.xy, %s.xy);\n", fsName);
334 segments->fFSCode.appendf("\tfloat outerAlpha = smoothstep(d - 0.5, d + 0.5, %s.z);\n", fsName);
335 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);
336 segments->fFSCode.append("\tedgeAlpha = outerAlpha * innerAlpha;\n");
337 break;
338 default:
339 GrCrash("Unknown Edge Type!");
340 break;
bsalomon@google.com66105672011-09-15 15:12:00 +0000341 }
bsalomon@google.com66105672011-09-15 15:12:00 +0000342 *coverageVar = "edgeAlpha";
343 } else {
344 coverageVar->reset();
345 }
346}
347
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000348namespace {
349
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000350void genInputColor(GrGLProgram::ProgramDesc::ColorInput colorInput,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000351 GrGLProgram::CachedData* programData,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000352 GrGLShaderBuilder* segments,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000353 GrStringBuilder* inColor) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000354 switch (colorInput) {
355 case GrGLProgram::ProgramDesc::kAttribute_ColorInput: {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000356 segments->fVSAttrs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000357 GrGLShaderVar::kAttribute_TypeModifier,
358 COL_ATTR_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000359 const char *vsName, *fsName;
tomhudson@google.com23cb2292012-05-30 18:26:03 +0000360 segments->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000361 segments->fVSCode.appendf("\t%s = " COL_ATTR_NAME ";\n", vsName);
362 *inColor = fsName;
363 } break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000364 case GrGLProgram::ProgramDesc::kUniform_ColorInput:
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000365 segments->addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
366 kVec4f_GrSLType, COL_UNI_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000367 programData->fUniLocations.fColorUni = kUseUniform;
368 *inColor = COL_UNI_NAME;
369 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000370 case GrGLProgram::ProgramDesc::kTransBlack_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000371 GrAssert(!"needComputedColor should be false.");
372 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000373 case GrGLProgram::ProgramDesc::kSolidWhite_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000374 break;
375 default:
376 GrCrash("Unknown color type.");
377 break;
378 }
379}
380
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000381void genAttributeCoverage(GrGLShaderBuilder* segments,
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000382 GrStringBuilder* inOutCoverage) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000383 segments->fVSAttrs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000384 GrGLShaderVar::kAttribute_TypeModifier,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000385 COV_ATTR_NAME);
386 const char *vsName, *fsName;
tomhudson@google.com23cb2292012-05-30 18:26:03 +0000387 segments->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000388 segments->fVSCode.appendf("\t%s = " COV_ATTR_NAME ";\n", vsName);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000389 if (inOutCoverage->size()) {
390 segments->fFSCode.appendf("\tvec4 attrCoverage = %s * %s;\n",
391 fsName, inOutCoverage->c_str());
392 *inOutCoverage = "attrCoverage";
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000393 } else {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000394 *inOutCoverage = fsName;
395 }
396}
397
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000398void genUniformCoverage(GrGLShaderBuilder* segments,
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000399 GrGLProgram::CachedData* programData,
400 GrStringBuilder* inOutCoverage) {
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000401 segments->addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
402 kVec4f_GrSLType, COV_UNI_NAME);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000403 programData->fUniLocations.fCoverageUni = kUseUniform;
404 if (inOutCoverage->size()) {
405 segments->fFSCode.appendf("\tvec4 uniCoverage = %s * %s;\n",
406 COV_UNI_NAME, inOutCoverage->c_str());
407 *inOutCoverage = "uniCoverage";
408 } else {
409 *inOutCoverage = COV_UNI_NAME;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000410 }
411}
412
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000413}
414
bsalomon@google.com96399942012-02-13 14:39:16 +0000415void GrGLProgram::genGeometryShader(const GrGLContextInfo& gl,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000416 GrGLShaderBuilder* segments) const {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000417#if GR_GL_EXPERIMENTAL_GS
418 if (fProgramDesc.fExperimentalGS) {
bsalomon@google.com96399942012-02-13 14:39:16 +0000419 GrAssert(gl.glslGeneration() >= k150_GrGLSLGeneration);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000420 segments->fGSHeader.append("layout(triangles) in;\n"
421 "layout(triangle_strip, max_vertices = 6) out;\n");
422 segments->fGSCode.append("void main() {\n"
423 "\tfor (int i = 0; i < 3; ++i) {\n"
424 "\t\tgl_Position = gl_in[i].gl_Position;\n");
425 if (this->fProgramDesc.fEmitsPointSize) {
426 segments->fGSCode.append("\t\tgl_PointSize = 1.0;\n");
427 }
428 GrAssert(segments->fGSInputs.count() == segments->fGSOutputs.count());
429 int count = segments->fGSInputs.count();
430 for (int i = 0; i < count; ++i) {
431 segments->fGSCode.appendf("\t\t%s = %s[i];\n",
432 segments->fGSOutputs[i].getName().c_str(),
433 segments->fGSInputs[i].getName().c_str());
434 }
435 segments->fGSCode.append("\t\tEmitVertex();\n"
436 "\t}\n"
437 "\tEndPrimitive();\n"
438 "}\n");
439 }
440#endif
441}
442
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000443const char* GrGLProgram::adjustInColor(const GrStringBuilder& inColor) const {
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000444 if (inColor.size()) {
445 return inColor.c_str();
446 } else {
447 if (ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput) {
448 return all_ones_vec(4);
449 } else {
450 return all_zeros_vec(4);
451 }
452 }
453}
454
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000455// If this destructor is in the header file, we must include GrGLProgramStage
456// instead of just forward-declaring it.
457GrGLProgram::CachedData::~CachedData() {
458 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
459 delete fCustomStage[i];
460 }
461}
462
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000463
bsalomon@google.com96399942012-02-13 14:39:16 +0000464bool GrGLProgram::genProgram(const GrGLContextInfo& gl,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000465 GrCustomStage** customStages,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000466 GrGLProgram::CachedData* programData) const {
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000467 GrGLShaderBuilder segments;
junov@google.comf93e7172011-03-31 21:26:24 +0000468 const uint32_t& layout = fProgramDesc.fVertexLayout;
469
bsalomon@google.com91961302011-05-09 18:39:58 +0000470 programData->fUniLocations.reset();
junov@google.comf93e7172011-03-31 21:26:24 +0000471
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000472#if GR_GL_EXPERIMENTAL_GS
473 segments.fUsesGS = fProgramDesc.fExperimentalGS;
474#endif
475
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000476 SkXfermode::Coeff colorCoeff, uniformCoeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000477 bool applyColorMatrix = SkToBool(fProgramDesc.fColorMatrixEnabled);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000478 // The rest of transfer mode color filters have not been implemented
479 if (fProgramDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) {
480 GR_DEBUGCODE(bool success =)
tomhudson@google.com0d831722011-06-02 15:37:14 +0000481 SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode>
482 (fProgramDesc.fColorFilterXfermode),
483 &uniformCoeff, &colorCoeff);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000484 GR_DEBUGASSERT(success);
485 } else {
486 colorCoeff = SkXfermode::kOne_Coeff;
487 uniformCoeff = SkXfermode::kZero_Coeff;
488 }
489
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000490 // no need to do the color filter / matrix at all if coverage is 0. The
491 // output color is scaled by the coverage. All the dual source outputs are
492 // scaled by the coverage as well.
493 if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fCoverageInput) {
494 colorCoeff = SkXfermode::kZero_Coeff;
495 uniformCoeff = SkXfermode::kZero_Coeff;
496 applyColorMatrix = false;
497 }
498
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000499 // If we know the final color is going to be all zeros then we can
500 // simplify the color filter coeffecients. needComputedColor will then
501 // come out false below.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000502 if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fColorInput) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000503 colorCoeff = SkXfermode::kZero_Coeff;
504 if (SkXfermode::kDC_Coeff == uniformCoeff ||
505 SkXfermode::kDA_Coeff == uniformCoeff) {
506 uniformCoeff = SkXfermode::kZero_Coeff;
507 } else if (SkXfermode::kIDC_Coeff == uniformCoeff ||
508 SkXfermode::kIDA_Coeff == uniformCoeff) {
509 uniformCoeff = SkXfermode::kOne_Coeff;
510 }
511 }
512
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000513 bool needColorFilterUniform;
514 bool needComputedColor;
515 needBlendInputs(uniformCoeff, colorCoeff,
516 &needColorFilterUniform, &needComputedColor);
517
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000518 // the dual source output has no canonical var name, have to
519 // declare an output, which is incompatible with gl_FragColor/gl_FragData.
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000520 bool dualSourceOutputWritten = false;
bsalomon@google.com96399942012-02-13 14:39:16 +0000521 segments.fHeader.printf(GrGetGLSLVersionDecl(gl.binding(),
522 gl.glslGeneration()));
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000523
524 GrGLShaderVar colorOutput;
bsalomon@google.com96399942012-02-13 14:39:16 +0000525 bool isColorDeclared = GrGLSLSetupFSColorOuput(gl.glslGeneration(),
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000526 declared_color_output_name(),
527 &colorOutput);
528 if (isColorDeclared) {
529 segments.fFSOutputs.push_back(colorOutput);
530 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000531
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000532 segments.addUniform(GrGLShaderBuilder::kVertex_VariableLifetime,
533 kMat33f_GrSLType, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000534 programData->fUniLocations.fViewMatrixUni = kUseUniform;
bsalomon@google.com341767c2012-05-11 20:47:39 +0000535
tomhudson@google.com168e6342012-04-18 17:49:20 +0000536 segments.fVSAttrs.push_back().set(kVec2f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000537 GrGLShaderVar::kAttribute_TypeModifier, POS_ATTR_NAME);
junov@google.comf93e7172011-03-31 21:26:24 +0000538
bsalomon@google.com91961302011-05-09 18:39:58 +0000539 segments.fVSCode.append(
540 "void main() {\n"
541 "\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3("POS_ATTR_NAME", 1);\n"
542 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n");
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000543
bsalomon@google.com91961302011-05-09 18:39:58 +0000544 // incoming color to current stage being processed.
545 GrStringBuilder inColor;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000546
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000547 if (needComputedColor) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000548 genInputColor((ProgramDesc::ColorInput) fProgramDesc.fColorInput,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000549 programData, &segments, &inColor);
junov@google.comf93e7172011-03-31 21:26:24 +0000550 }
551
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000552 // we output point size in the GS if present
553 if (fProgramDesc.fEmitsPointSize && !segments.fUsesGS){
bsalomon@google.com91961302011-05-09 18:39:58 +0000554 segments.fVSCode.append("\tgl_PointSize = 1.0;\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000555 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000556
bsalomon@google.com91961302011-05-09 18:39:58 +0000557 segments.fFSCode.append("void main() {\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000558
559 // add texture coordinates that are used to the list of vertex attr decls
tomhudson@google.com93813632011-10-27 20:21:16 +0000560 GrStringBuilder texCoordAttrs[GrDrawState::kMaxTexCoords];
561 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000562 if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000563 tex_attr_name(t, texCoordAttrs + t);
tomhudson@google.com168e6342012-04-18 17:49:20 +0000564 segments.fVSAttrs.push_back().set(kVec2f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000565 GrGLShaderVar::kAttribute_TypeModifier,
566 texCoordAttrs[t].c_str());
junov@google.comf93e7172011-03-31 21:26:24 +0000567 }
568 }
569
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000570 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000571 // We need to convert generic effect representations to GL-specific
572 // backends so they can be accesseed in genStageCode() and in subsequent,
573 // uses of programData, but it's safest to do so below when we're *sure*
574 // we need them.
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000575 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000576 programData->fCustomStage[s] = NULL;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000577 }
578
579 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000580 // compute the final color
Scroggo97c88c22011-05-11 14:05:25 +0000581
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000582 // if we have color stages string them together, feeding the output color
583 // of each to the next and generating code for each stage.
584 if (needComputedColor) {
585 GrStringBuilder outColor;
586 for (int s = 0; s < fProgramDesc.fFirstCoverageStage; ++s) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000587 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000588 // create var to hold stage result
589 outColor = "color";
590 outColor.appendS32(s);
591 segments.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str());
592
593 const char* inCoords;
594 // figure out what our input coords are
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000595 int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout);
596 if (tcIdx < 0) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000597 inCoords = POS_ATTR_NAME;
Scroggo97c88c22011-05-11 14:05:25 +0000598 } else {
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000599 // must have input tex coordinates if stage is enabled.
Scroggo97c88c22011-05-11 14:05:25 +0000600 GrAssert(texCoordAttrs[tcIdx].size());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000601 inCoords = texCoordAttrs[tcIdx].c_str();
junov@google.comf93e7172011-03-31 21:26:24 +0000602 }
603
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000604 if (NULL != customStages[s]) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000605 const GrProgramStageFactory& factory =
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000606 customStages[s]->getFactory();
607 programData->fCustomStage[s] =
bsalomon@google.comb505a122012-05-31 18:40:36 +0000608 factory.createGLInstance(*customStages[s]);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000609 }
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000610 this->genStageCode(gl,
611 s,
612 fProgramDesc.fStages[s],
613 inColor.size() ? inColor.c_str() : NULL,
614 outColor.c_str(),
615 inCoords,
616 &segments,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000617 &programData->fUniLocations.fStages[s],
618 programData->fCustomStage[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000619 inColor = outColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000620 }
621 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000622 }
Scroggo97c88c22011-05-11 14:05:25 +0000623
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000624 // if have all ones or zeros for the "dst" input to the color filter then we
625 // may be able to make additional optimizations.
626 if (needColorFilterUniform && needComputedColor && !inColor.size()) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000627 GrAssert(ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000628 bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff ||
629 SkXfermode::kIDA_Coeff == uniformCoeff;
630 if (uniformCoeffIsZero) {
631 uniformCoeff = SkXfermode::kZero_Coeff;
632 bool bogus;
633 needBlendInputs(SkXfermode::kZero_Coeff, colorCoeff,
634 &needColorFilterUniform, &bogus);
635 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000636 }
637 if (needColorFilterUniform) {
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000638 segments.addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
639 kVec4f_GrSLType, COL_FILTER_UNI_NAME);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000640 programData->fUniLocations.fColorFilterUni = kUseUniform;
641 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000642 bool wroteFragColorZero = false;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000643 if (SkXfermode::kZero_Coeff == uniformCoeff &&
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000644 SkXfermode::kZero_Coeff == colorCoeff &&
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000645 !applyColorMatrix) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000646 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000647 colorOutput.getName().c_str(),
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000648 all_zeros_vec(4));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000649 wroteFragColorZero = true;
650 } else if (SkXfermode::kDst_Mode != fProgramDesc.fColorFilterXfermode) {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000651 segments.fFSCode.append("\tvec4 filteredColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000652 const char* color = adjustInColor(inColor);
tomhudson@google.com0d831722011-06-02 15:37:14 +0000653 addColorFilter(&segments.fFSCode, "filteredColor", uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000654 colorCoeff, color);
655 inColor = "filteredColor";
656 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000657 if (applyColorMatrix) {
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000658 segments.addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
659 kMat44f_GrSLType, COL_MATRIX_UNI_NAME);
660 segments.addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
661 kVec4f_GrSLType, COL_MATRIX_VEC_UNI_NAME);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000662 programData->fUniLocations.fColorMatrixUni = kUseUniform;
663 programData->fUniLocations.fColorMatrixVecUni = kUseUniform;
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000664 segments.fFSCode.append("\tvec4 matrixedColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000665 const char* color = adjustInColor(inColor);
666 addColorMatrix(&segments.fFSCode, "matrixedColor", color);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000667 inColor = "matrixedColor";
668 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000669
670 ///////////////////////////////////////////////////////////////////////////
671 // compute the partial coverage (coverage stages and edge aa)
672
673 GrStringBuilder inCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000674 bool coverageIsZero = ProgramDesc::kTransBlack_ColorInput ==
675 fProgramDesc.fCoverageInput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000676 // we don't need to compute coverage at all if we know the final shader
677 // output will be zero and we don't have a dual src blend output.
678 if (!wroteFragColorZero ||
679 ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000680
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000681 if (!coverageIsZero) {
682 this->genEdgeCoverage(gl,
683 layout,
684 programData,
685 &inCoverage,
686 &segments);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000687
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000688 switch (fProgramDesc.fCoverageInput) {
689 case ProgramDesc::kSolidWhite_ColorInput:
690 // empty string implies solid white
691 break;
692 case ProgramDesc::kAttribute_ColorInput:
693 genAttributeCoverage(&segments, &inCoverage);
694 break;
695 case ProgramDesc::kUniform_ColorInput:
696 genUniformCoverage(&segments, programData, &inCoverage);
697 break;
698 default:
699 GrCrash("Unexpected input coverage.");
700 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000701
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000702 GrStringBuilder outCoverage;
703 const int& startStage = fProgramDesc.fFirstCoverageStage;
704 for (int s = startStage; s < GrDrawState::kNumStages; ++s) {
705 if (fProgramDesc.fStages[s].isEnabled()) {
706 // create var to hold stage output
707 outCoverage = "coverage";
708 outCoverage.appendS32(s);
709 segments.fFSCode.appendf("\tvec4 %s;\n",
710 outCoverage.c_str());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000711
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000712 const char* inCoords;
713 // figure out what our input coords are
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000714 int tcIdx =
715 GrDrawTarget::VertexTexCoordsForStage(s, layout);
716 if (tcIdx < 0) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000717 inCoords = POS_ATTR_NAME;
718 } else {
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000719 // must have input tex coordinates if stage is
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000720 // enabled.
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000721 GrAssert(texCoordAttrs[tcIdx].size());
722 inCoords = texCoordAttrs[tcIdx].c_str();
723 }
724
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000725 if (NULL != customStages[s]) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000726 const GrProgramStageFactory& factory =
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000727 customStages[s]->getFactory();
728 programData->fCustomStage[s] =
bsalomon@google.comb505a122012-05-31 18:40:36 +0000729 factory.createGLInstance(*customStages[s]);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000730 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000731 this->genStageCode(gl, s,
732 fProgramDesc.fStages[s],
733 inCoverage.size() ? inCoverage.c_str() : NULL,
734 outCoverage.c_str(),
735 inCoords,
736 &segments,
737 &programData->fUniLocations.fStages[s],
738 programData->fCustomStage[s]);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000739 inCoverage = outCoverage;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000740 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000741 }
742 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000743 if (ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000744 segments.fFSOutputs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000745 GrGLShaderVar::kOut_TypeModifier,
746 dual_source_output_name());
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000747 bool outputIsZero = coverageIsZero;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000748 GrStringBuilder coeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000749 if (!outputIsZero &&
750 ProgramDesc::kCoverage_DualSrcOutput !=
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000751 fProgramDesc.fDualSrcOutput && !wroteFragColorZero) {
752 if (!inColor.size()) {
753 outputIsZero = true;
754 } else {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000755 if (fProgramDesc.fDualSrcOutput ==
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000756 ProgramDesc::kCoverageISA_DualSrcOutput) {
757 coeff.printf("(1 - %s.a)", inColor.c_str());
758 } else {
759 coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str());
760 }
761 }
762 }
763 if (outputIsZero) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000764 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000765 dual_source_output_name(),
766 all_zeros_vec(4));
767 } else {
768 modulate_helper(dual_source_output_name(),
769 coeff.c_str(),
770 inCoverage.c_str(),
771 &segments.fFSCode);
772 }
773 dualSourceOutputWritten = true;
774 }
junov@google.comf93e7172011-03-31 21:26:24 +0000775 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000776
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000777 ///////////////////////////////////////////////////////////////////////////
778 // combine color and coverage as frag color
779
780 if (!wroteFragColorZero) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000781 if (coverageIsZero) {
782 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000783 colorOutput.getName().c_str(),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000784 all_zeros_vec(4));
785 } else {
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000786 modulate_helper(colorOutput.getName().c_str(),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000787 inColor.c_str(),
788 inCoverage.c_str(),
789 &segments.fFSCode);
790 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000791 if (ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig ==
792 fProgramDesc.fOutputConfig) {
793 segments.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",
794 colorOutput.getName().c_str(),
795 colorOutput.getName().c_str(),
796 colorOutput.getName().c_str(),
797 colorOutput.getName().c_str(),
798 colorOutput.getName().c_str());
799 } else if (ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig ==
800 fProgramDesc.fOutputConfig) {
801 segments.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",
802 colorOutput.getName().c_str(),
803 colorOutput.getName().c_str(),
804 colorOutput.getName().c_str(),
805 colorOutput.getName().c_str(),
806 colorOutput.getName().c_str());
bsalomon@google.comc4364992011-11-07 15:54:49 +0000807 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000808 }
809
bsalomon@google.com91961302011-05-09 18:39:58 +0000810 segments.fVSCode.append("}\n");
811 segments.fFSCode.append("}\n");
812
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000813 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000814 // insert GS
815#if GR_DEBUG
bsalomon@google.com96399942012-02-13 14:39:16 +0000816 this->genGeometryShader(gl, &segments);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000817#endif
818
819 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000820 // compile and setup attribs and unis
821
bsalomon@google.com96399942012-02-13 14:39:16 +0000822 if (!CompileShaders(gl, segments, programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000823 return false;
824 }
825
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000826 if (!this->bindOutputsAttribsAndLinkProgram(gl, texCoordAttrs,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000827 isColorDeclared,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000828 dualSourceOutputWritten,
829 programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000830 return false;
831 }
832
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000833 this->getUniformLocationsAndInitCache(gl, programData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000834
835 return true;
836}
837
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000838namespace {
839
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000840inline void expand_decls(const VarArray& vars,
bsalomon@google.com96399942012-02-13 14:39:16 +0000841 const GrGLContextInfo& gl,
842 GrStringBuilder* string) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000843 const int count = vars.count();
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000844 for (int i = 0; i < count; ++i) {
bsalomon@google.com96399942012-02-13 14:39:16 +0000845 vars[i].appendDecl(gl, string);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000846 }
847}
848
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000849inline void print_shader(int stringCnt,
850 const char** strings,
851 int* stringLengths) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000852 for (int i = 0; i < stringCnt; ++i) {
853 if (NULL == stringLengths || stringLengths[i] < 0) {
854 GrPrintf(strings[i]);
855 } else {
856 GrPrintf("%.*s", stringLengths[i], strings[i]);
857 }
858 }
859}
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000860
861typedef SkTArray<const char*, true> StrArray;
862#define PREALLOC_STR_ARRAY(N) SkSTArray<(N), const char*, true>
863
864typedef SkTArray<int, true> LengthArray;
865#define PREALLOC_LENGTH_ARRAY(N) SkSTArray<(N), int, true>
866
867// these shouldn't relocate
868typedef GrTAllocator<GrStringBuilder> TempArray;
869#define PREALLOC_TEMP_ARRAY(N) GrSTAllocator<(N), GrStringBuilder>
870
871inline void append_string(const GrStringBuilder& str,
872 StrArray* strings,
873 LengthArray* lengths) {
874 int length = (int) str.size();
875 if (length) {
876 strings->push_back(str.c_str());
877 lengths->push_back(length);
878 }
879 GrAssert(strings->count() == lengths->count());
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000880}
881
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000882inline void append_decls(const VarArray& vars,
bsalomon@google.com96399942012-02-13 14:39:16 +0000883 const GrGLContextInfo& gl,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000884 StrArray* strings,
885 LengthArray* lengths,
bsalomon@google.com96399942012-02-13 14:39:16 +0000886 TempArray* temp) {
887 expand_decls(vars, gl, &temp->push_back());
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000888 append_string(temp->back(), strings, lengths);
889}
890
891}
892
bsalomon@google.com96399942012-02-13 14:39:16 +0000893bool GrGLProgram::CompileShaders(const GrGLContextInfo& gl,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000894 const GrGLShaderBuilder& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +0000895 CachedData* programData) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000896 enum { kPreAllocStringCnt = 8 };
junov@google.comf93e7172011-03-31 21:26:24 +0000897
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000898 PREALLOC_STR_ARRAY(kPreAllocStringCnt) strs;
899 PREALLOC_LENGTH_ARRAY(kPreAllocStringCnt) lengths;
900 PREALLOC_TEMP_ARRAY(kPreAllocStringCnt) temps;
901
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000902 GrStringBuilder unis;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000903 GrStringBuilder inputs;
904 GrStringBuilder outputs;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000905
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000906 append_string(segments.fHeader, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +0000907 append_decls(segments.fVSUnis, gl, &strs, &lengths, &temps);
908 append_decls(segments.fVSAttrs, gl, &strs, &lengths, &temps);
909 append_decls(segments.fVSOutputs, gl, &strs, &lengths, &temps);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000910 append_string(segments.fVSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +0000911
912#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000913 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000914 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000915#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000916
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000917 programData->fVShaderID =
918 CompileShader(gl, GR_GL_VERTEX_SHADER, strs.count(),
919 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +0000920
bsalomon@google.com91961302011-05-09 18:39:58 +0000921 if (!programData->fVShaderID) {
922 return false;
923 }
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000924 if (segments.fUsesGS) {
925 strs.reset();
926 lengths.reset();
927 temps.reset();
928 append_string(segments.fHeader, &strs, &lengths);
929 append_string(segments.fGSHeader, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +0000930 append_decls(segments.fGSInputs, gl, &strs, &lengths, &temps);
931 append_decls(segments.fGSOutputs, gl, &strs, &lengths, &temps);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000932 append_string(segments.fGSCode, &strs, &lengths);
933#if PRINT_SHADERS
934 print_shader(strs.count(), &strs[0], &lengths[0]);
935 GrPrintf("\n");
936#endif
937 programData->fGShaderID =
938 CompileShader(gl, GR_GL_GEOMETRY_SHADER, strs.count(),
939 &strs[0], &lengths[0]);
940 } else {
941 programData->fGShaderID = 0;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000942 }
junov@google.comf93e7172011-03-31 21:26:24 +0000943
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000944 strs.reset();
945 lengths.reset();
946 temps.reset();
947
948 append_string(segments.fHeader, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +0000949 GrStringBuilder precisionStr(GrGetGLSLShaderPrecisionDecl(gl.binding()));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000950 append_string(precisionStr, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +0000951 append_decls(segments.fFSUnis, gl, &strs, &lengths, &temps);
952 append_decls(segments.fFSInputs, gl, &strs, &lengths, &temps);
bsalomon@google.com373a6632011-10-19 20:43:20 +0000953 // We shouldn't have declared outputs on 1.10
bsalomon@google.com96399942012-02-13 14:39:16 +0000954 GrAssert(k110_GrGLSLGeneration != gl.glslGeneration() ||
tomhudson@google.com086e5352011-12-08 14:44:10 +0000955 segments.fFSOutputs.empty());
bsalomon@google.com96399942012-02-13 14:39:16 +0000956 append_decls(segments.fFSOutputs, gl, &strs, &lengths, &temps);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000957 append_string(segments.fFSFunctions, &strs, &lengths);
958 append_string(segments.fFSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +0000959
960#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000961 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000962 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000963#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000964
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000965 programData->fFShaderID =
966 CompileShader(gl, GR_GL_FRAGMENT_SHADER, strs.count(),
967 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +0000968
bsalomon@google.com91961302011-05-09 18:39:58 +0000969 if (!programData->fFShaderID) {
970 return false;
junov@google.comf93e7172011-03-31 21:26:24 +0000971 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000972
bsalomon@google.com91961302011-05-09 18:39:58 +0000973 return true;
junov@google.comf93e7172011-03-31 21:26:24 +0000974}
975
bsalomon@google.com96399942012-02-13 14:39:16 +0000976#define GL_CALL(X) GR_GL_CALL(gl.interface(), X)
977#define GL_CALL_RET(R, X) GR_GL_CALL_RET(gl.interface(), R, X)
978
979GrGLuint GrGLProgram::CompileShader(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000980 GrGLenum type,
981 int stringCnt,
982 const char** strings,
983 int* stringLengths) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000984 SK_TRACE_EVENT1("GrGLProgram::CompileShader",
985 "stringCount", SkStringPrintf("%i", stringCnt).c_str());
986
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000987 GrGLuint shader;
bsalomon@google.com96399942012-02-13 14:39:16 +0000988 GL_CALL_RET(shader, CreateShader(type));
junov@google.comf93e7172011-03-31 21:26:24 +0000989 if (0 == shader) {
990 return 0;
991 }
992
993 GrGLint compiled = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +0000994 GL_CALL(ShaderSource(shader, stringCnt, strings, stringLengths));
995 GL_CALL(CompileShader(shader));
996 GL_CALL(GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled));
junov@google.comf93e7172011-03-31 21:26:24 +0000997
998 if (!compiled) {
999 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001000 GL_CALL(GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001001 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
junov@google.comf93e7172011-03-31 21:26:24 +00001002 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001003 // retrieve length even though we don't need it to workaround
1004 // bug in chrome cmd buffer param validation.
1005 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001006 GL_CALL(GetShaderInfoLog(shader, infoLen+1,
1007 &length, (char*)log.get()));
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001008 print_shader(stringCnt, strings, stringLengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001009 GrPrintf("\n%s", log.get());
1010 }
1011 GrAssert(!"Shader compilation failed!");
bsalomon@google.com96399942012-02-13 14:39:16 +00001012 GL_CALL(DeleteShader(shader));
junov@google.comf93e7172011-03-31 21:26:24 +00001013 return 0;
1014 }
1015 return shader;
1016}
1017
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001018bool GrGLProgram::bindOutputsAttribsAndLinkProgram(
bsalomon@google.com96399942012-02-13 14:39:16 +00001019 const GrGLContextInfo& gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001020 GrStringBuilder texCoordAttrNames[],
1021 bool bindColorOut,
tomhudson@google.com0d831722011-06-02 15:37:14 +00001022 bool bindDualSrcOut,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001023 CachedData* programData) const {
bsalomon@google.com96399942012-02-13 14:39:16 +00001024 GL_CALL_RET(programData->fProgramID, CreateProgram());
bsalomon@google.com91961302011-05-09 18:39:58 +00001025 if (!programData->fProgramID) {
1026 return false;
1027 }
1028 const GrGLint& progID = programData->fProgramID;
1029
bsalomon@google.com96399942012-02-13 14:39:16 +00001030 GL_CALL(AttachShader(progID, programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001031 if (programData->fGShaderID) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001032 GL_CALL(AttachShader(progID, programData->fGShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001033 }
bsalomon@google.com96399942012-02-13 14:39:16 +00001034 GL_CALL(AttachShader(progID, programData->fFShaderID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001035
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001036 if (bindColorOut) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001037 GL_CALL(BindFragDataLocation(programData->fProgramID,
1038 0, declared_color_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001039 }
1040 if (bindDualSrcOut) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001041 GL_CALL(BindFragDataLocationIndexed(programData->fProgramID,
1042 0, 1, dual_source_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001043 }
1044
bsalomon@google.com91961302011-05-09 18:39:58 +00001045 // Bind the attrib locations to same values for all shaders
bsalomon@google.com96399942012-02-13 14:39:16 +00001046 GL_CALL(BindAttribLocation(progID, PositionAttributeIdx(), POS_ATTR_NAME));
tomhudson@google.com93813632011-10-27 20:21:16 +00001047 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001048 if (texCoordAttrNames[t].size()) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001049 GL_CALL(BindAttribLocation(progID,
1050 TexCoordAttributeIdx(t),
1051 texCoordAttrNames[t].c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001052 }
1053 }
1054
bsalomon@google.com96399942012-02-13 14:39:16 +00001055 GL_CALL(BindAttribLocation(progID, ColorAttributeIdx(), COL_ATTR_NAME));
1056 GL_CALL(BindAttribLocation(progID, CoverageAttributeIdx(), COV_ATTR_NAME));
1057 GL_CALL(BindAttribLocation(progID, EdgeAttributeIdx(), EDGE_ATTR_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001058
bsalomon@google.com96399942012-02-13 14:39:16 +00001059 GL_CALL(LinkProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001060
1061 GrGLint linked = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001062 GL_CALL(GetProgramiv(progID, GR_GL_LINK_STATUS, &linked));
bsalomon@google.com91961302011-05-09 18:39:58 +00001063 if (!linked) {
1064 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001065 GL_CALL(GetProgramiv(progID, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001066 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
bsalomon@google.com91961302011-05-09 18:39:58 +00001067 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001068 // retrieve length even though we don't need it to workaround
1069 // bug in chrome cmd buffer param validation.
1070 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001071 GL_CALL(GetProgramInfoLog(progID,
1072 infoLen+1,
1073 &length,
1074 (char*)log.get()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001075 GrPrintf((char*)log.get());
1076 }
1077 GrAssert(!"Error linking program");
bsalomon@google.com96399942012-02-13 14:39:16 +00001078 GL_CALL(DeleteProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001079 programData->fProgramID = 0;
1080 return false;
1081 }
1082 return true;
1083}
1084
bsalomon@google.com96399942012-02-13 14:39:16 +00001085void GrGLProgram::getUniformLocationsAndInitCache(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001086 CachedData* programData) const {
bsalomon@google.com91961302011-05-09 18:39:58 +00001087 const GrGLint& progID = programData->fProgramID;
1088
1089 if (kUseUniform == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001090 GL_CALL_RET(programData->fUniLocations.fViewMatrixUni,
1091 GetUniformLocation(progID, VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001092 GrAssert(kUnusedUniform != programData->fUniLocations.fViewMatrixUni);
1093 }
1094 if (kUseUniform == programData->fUniLocations.fColorUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001095 GL_CALL_RET(programData->fUniLocations.fColorUni,
1096 GetUniformLocation(progID, COL_UNI_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001097 GrAssert(kUnusedUniform != programData->fUniLocations.fColorUni);
1098 }
Scroggo97c88c22011-05-11 14:05:25 +00001099 if (kUseUniform == programData->fUniLocations.fColorFilterUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001100 GL_CALL_RET(programData->fUniLocations.fColorFilterUni,
1101 GetUniformLocation(progID, COL_FILTER_UNI_NAME));
Scroggo97c88c22011-05-11 14:05:25 +00001102 GrAssert(kUnusedUniform != programData->fUniLocations.fColorFilterUni);
1103 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001104
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001105 if (kUseUniform == programData->fUniLocations.fColorMatrixUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001106 GL_CALL_RET(programData->fUniLocations.fColorMatrixUni,
1107 GetUniformLocation(progID, COL_MATRIX_UNI_NAME));
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001108 }
1109
1110 if (kUseUniform == programData->fUniLocations.fColorMatrixVecUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001111 GL_CALL_RET(programData->fUniLocations.fColorMatrixVecUni,
1112 GetUniformLocation(progID, COL_MATRIX_VEC_UNI_NAME));
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001113 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001114 if (kUseUniform == programData->fUniLocations.fCoverageUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001115 GL_CALL_RET(programData->fUniLocations.fCoverageUni,
1116 GetUniformLocation(progID, COV_UNI_NAME));
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001117 GrAssert(kUnusedUniform != programData->fUniLocations.fCoverageUni);
1118 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001119
tomhudson@google.com93813632011-10-27 20:21:16 +00001120 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001121 StageUniLocations& locations = programData->fUniLocations.fStages[s];
tomhudson@google.com0d831722011-06-02 15:37:14 +00001122 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001123 if (kUseUniform == locations.fTextureMatrixUni) {
1124 GrStringBuilder texMName;
1125 tex_matrix_name(s, &texMName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001126 GL_CALL_RET(locations.fTextureMatrixUni,
1127 GetUniformLocation(progID, texMName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001128 GrAssert(kUnusedUniform != locations.fTextureMatrixUni);
1129 }
1130
1131 if (kUseUniform == locations.fSamplerUni) {
1132 GrStringBuilder samplerName;
1133 sampler_name(s, &samplerName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001134 GL_CALL_RET(locations.fSamplerUni,
1135 GetUniformLocation(progID,samplerName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001136 GrAssert(kUnusedUniform != locations.fSamplerUni);
1137 }
1138
junov@google.com6acc9b32011-05-16 18:32:07 +00001139 if (kUseUniform == locations.fTexDomUni) {
1140 GrStringBuilder texDomName;
1141 tex_domain_name(s, &texDomName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001142 GL_CALL_RET(locations.fTexDomUni,
1143 GetUniformLocation(progID, texDomName.c_str()));
junov@google.com6acc9b32011-05-16 18:32:07 +00001144 GrAssert(kUnusedUniform != locations.fTexDomUni);
1145 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001146
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001147 if (NULL != programData->fCustomStage[s]) {
1148 programData->fCustomStage[s]->
1149 initUniforms(gl.interface(), progID);
1150 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001151 }
1152 }
bsalomon@google.com96399942012-02-13 14:39:16 +00001153 GL_CALL(UseProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001154
1155 // init sampler unis and set bogus values for state tracking
tomhudson@google.com93813632011-10-27 20:21:16 +00001156 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001157 if (kUnusedUniform != programData->fUniLocations.fStages[s].fSamplerUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001158 GL_CALL(Uniform1i(programData->fUniLocations.fStages[s].fSamplerUni, s));
bsalomon@google.com91961302011-05-09 18:39:58 +00001159 }
1160 programData->fTextureMatrices[s] = GrMatrix::InvalidMatrix();
tomhudson@google.com24878f72012-03-22 14:44:46 +00001161 programData->fTextureDomain[s].setEmpty();
bsalomon@google.com890e3b52012-06-01 19:01:37 +00001162 // this is arbitrary, just initialize to something
1163 programData->fTextureOrientation[s] =
1164 GrGLTexture::kBottomUp_Orientation;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001165 // Must not reset fStageOverride[] here.
bsalomon@google.com91961302011-05-09 18:39:58 +00001166 }
1167 programData->fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com4c883782012-06-04 19:05:11 +00001168 programData->fViewportSize.set(-1, -1);
bsalomon@google.com91961302011-05-09 18:39:58 +00001169 programData->fColor = GrColor_ILLEGAL;
Scroggo97c88c22011-05-11 14:05:25 +00001170 programData->fColorFilterColor = GrColor_ILLEGAL;
bsalomon@google.com91961302011-05-09 18:39:58 +00001171}
1172
junov@google.comf93e7172011-03-31 21:26:24 +00001173//============================================================================
1174// Stage code generation
1175//============================================================================
1176
bsalomon@google.com96399942012-02-13 14:39:16 +00001177void GrGLProgram::genStageCode(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001178 int stageNum,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001179 const GrGLProgram::StageDesc& desc,
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001180 const char* fsInColor, // NULL means no incoming color
1181 const char* fsOutColor,
1182 const char* vsInCoord,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +00001183 GrGLShaderBuilder* segments,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001184 StageUniLocations* locations,
1185 GrGLProgramStage* customStage) const {
junov@google.comf93e7172011-03-31 21:26:24 +00001186
bsalomon@google.com74b98712011-11-11 19:46:16 +00001187 GrAssert(stageNum >= 0 && stageNum <= GrDrawState::kNumStages);
1188 GrAssert((desc.fInConfigFlags & StageDesc::kInConfigBitMask) ==
1189 desc.fInConfigFlags);
junov@google.comf93e7172011-03-31 21:26:24 +00001190
tomhudson@google.com23cb2292012-05-30 18:26:03 +00001191 /// Vertex Shader Stuff
1192
junov@google.comf93e7172011-03-31 21:26:24 +00001193 // decide whether we need a matrix to transform texture coords
1194 // and whether the varying needs a perspective coord.
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001195 const char* matName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001196 if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) {
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001197 segments->fVaryingDims = segments->fCoordDims;
junov@google.comf93e7172011-03-31 21:26:24 +00001198 } else {
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001199 GrStringBuilder texMatName;
1200 tex_matrix_name(stageNum, &texMatName);
1201 const GrGLShaderVar* mat = &segments->addUniform(
1202 GrGLShaderBuilder::kVertex_VariableLifetime, kMat33f_GrSLType,
1203 texMatName.c_str());
1204 // Can't use texMatName.c_str() because it's on the stack!
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001205 matName = mat->getName().c_str();
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001206 locations->fTextureMatrixUni = kUseUniform;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001207
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001208 if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) {
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001209 segments->fVaryingDims = segments->fCoordDims;
junov@google.comf93e7172011-03-31 21:26:24 +00001210 } else {
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001211 segments->fVaryingDims = segments->fCoordDims + 1;
junov@google.comf93e7172011-03-31 21:26:24 +00001212 }
1213 }
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001214 GrAssert(segments->fVaryingDims > 0);
junov@google.comf93e7172011-03-31 21:26:24 +00001215
tomhudson@google.com898e7b52012-06-01 20:42:15 +00001216 // Must setup variables after computing segments->fVaryingDims
1217 if (NULL != customStage) {
1218 customStage->setupVariables(segments, stageNum);
1219 }
1220
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001221 GrStringBuilder samplerName;
1222 sampler_name(stageNum, &samplerName);
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001223 // const GrGLShaderVar* sampler = &
1224 segments->addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
1225 kSampler2D_GrSLType, samplerName.c_str());
bsalomon@google.com91961302011-05-09 18:39:58 +00001226 locations->fSamplerUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +00001227
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001228 const char *varyingVSName, *varyingFSName;
tomhudson@google.com23cb2292012-05-30 18:26:03 +00001229 segments->addVarying(GrSLFloatVectorType(segments->fVaryingDims),
1230 "Stage",
1231 stageNum,
1232 &varyingVSName,
1233 &varyingFSName);
junov@google.comf93e7172011-03-31 21:26:24 +00001234
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001235 if (!matName) {
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001236 GrAssert(segments->fVaryingDims == segments->fCoordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001237 segments->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord);
junov@google.comf93e7172011-03-31 21:26:24 +00001238 } else {
bsalomon@google.com91961302011-05-09 18:39:58 +00001239 // varying = texMatrix * texCoord
1240 segments->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n",
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001241 varyingVSName, matName, vsInCoord,
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001242 vector_all_coords(segments->fVaryingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001243 }
1244
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001245 // GrGLShaderVar* kernel = NULL;
1246 // const char* imageIncrementName = NULL;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001247 if (NULL != customStage) {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001248 segments->fVSCode.appendf("\t{ // stage %d %s\n",
1249 stageNum, customStage->name());
tomhudson@google.com6a820b62012-05-24 15:10:14 +00001250 customStage->emitVS(segments, varyingVSName);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001251 segments->fVSCode.appendf("\t}\n");
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001252 }
1253
junov@google.comf93e7172011-03-31 21:26:24 +00001254 /// Fragment Shader Stuff
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001255
tomhudson@google.com5440f062012-06-01 15:55:50 +00001256 segments->fSampleCoords = varyingFSName;
1257
1258 GrGLShaderBuilder::SamplerMode sampleMode =
1259 GrGLShaderBuilder::kExplicitDivide_SamplerMode;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001260 if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit |
1261 StageDesc::kNoPerspective_OptFlagBit)) {
tomhudson@google.com5440f062012-06-01 15:55:50 +00001262 sampleMode = GrGLShaderBuilder::kDefault_SamplerMode;
bsalomon@google.comd2ae1fa2012-06-04 20:06:02 +00001263 } else if (NULL == customStage) {
tomhudson@google.com5440f062012-06-01 15:55:50 +00001264 sampleMode = GrGLShaderBuilder::kProj_SamplerMode;
junov@google.comf93e7172011-03-31 21:26:24 +00001265 }
tomhudson@google.com5440f062012-06-01 15:55:50 +00001266 segments->setupTextureAccess(sampleMode, stageNum);
junov@google.comf93e7172011-03-31 21:26:24 +00001267
tomhudson@google.com898e7b52012-06-01 20:42:15 +00001268 segments->computeSwizzle(desc.fInConfigFlags);
1269 segments->computeModulate(fsInColor);
junov@google.comf93e7172011-03-31 21:26:24 +00001270
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001271 static const uint32_t kMulByAlphaMask =
1272 (StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
1273 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag);
1274
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001275 if (desc.fOptFlags & StageDesc::kCustomTextureDomain_OptFlagBit) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001276 GrStringBuilder texDomainName;
1277 tex_domain_name(stageNum, &texDomainName);
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001278 // const GrGLShaderVar* texDomain = &
1279 segments->addUniform(
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001280 GrGLShaderBuilder::kFragment_VariableLifetime,
1281 kVec4f_GrSLType, texDomainName.c_str());
junov@google.com6acc9b32011-05-16 18:32:07 +00001282 GrStringBuilder coordVar("clampCoord");
1283 segments->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n",
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001284 float_vector_type_str(segments->fCoordDims),
junov@google.com6acc9b32011-05-16 18:32:07 +00001285 coordVar.c_str(),
tomhudson@google.com040c41a2012-05-18 14:57:40 +00001286 segments->fSampleCoords.c_str(),
junov@google.com6acc9b32011-05-16 18:32:07 +00001287 texDomainName.c_str(),
1288 texDomainName.c_str());
tomhudson@google.com040c41a2012-05-18 14:57:40 +00001289 segments->fSampleCoords = coordVar;
junov@google.com6acc9b32011-05-16 18:32:07 +00001290 locations->fTexDomUni = kUseUniform;
1291 }
1292
tomhudson@google.com6a820b62012-05-24 15:10:14 +00001293 // NOTE: GrGLProgramStages are now responsible for fetching
1294 if (NULL == customStage) {
bsalomon@google.comd2ae1fa2012-06-04 20:06:02 +00001295 if (desc.fInConfigFlags & kMulByAlphaMask) {
1296 // only one of the mul by alpha flags should be set
1297 GrAssert(GrIsPow2(kMulByAlphaMask & desc.fInConfigFlags));
1298 GrAssert(!(desc.fInConfigFlags &
1299 StageDesc::kSmearAlpha_InConfigFlag));
1300 GrAssert(!(desc.fInConfigFlags &
1301 StageDesc::kSmearRed_InConfigFlag));
1302 segments->fFSCode.appendf("\t%s = %s(%s, %s)%s;\n",
1303 fsOutColor,
1304 segments->fTexFunc.c_str(),
1305 samplerName.c_str(),
1306 segments->fSampleCoords.c_str(),
1307 segments->fSwizzle.c_str());
1308 if (desc.fInConfigFlags &
1309 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag) {
1310 segments->fFSCode.appendf("\t%s = vec4(ceil(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
1311 fsOutColor, fsOutColor, fsOutColor,
1312 fsOutColor, segments->fModulate.c_str());
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001313 } else {
bsalomon@google.comd2ae1fa2012-06-04 20:06:02 +00001314 segments->fFSCode.appendf("\t%s = vec4(floor(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
1315 fsOutColor, fsOutColor, fsOutColor,
1316 fsOutColor, segments->fModulate.c_str());
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001317 }
bsalomon@google.comd2ae1fa2012-06-04 20:06:02 +00001318 } else {
1319 segments->emitDefaultFetch(fsOutColor, samplerName.c_str());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001320 }
junov@google.comf93e7172011-03-31 21:26:24 +00001321 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001322
1323 if (NULL != customStage) {
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001324 // Enclose custom code in a block to avoid namespace conflicts
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001325 segments->fFSCode.appendf("\t{ // stage %d %s \n",
1326 stageNum, customStage->name());
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001327 customStage->emitFS(segments, fsOutColor, fsInColor,
1328 samplerName.c_str());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001329 segments->fFSCode.appendf("\t}\n");
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001330 }
junov@google.comf93e7172011-03-31 21:26:24 +00001331}
tomhudson@google.com5960d002011-10-19 20:21:48 +00001332
tomhudson@google.com5960d002011-10-19 20:21:48 +00001333