blob: 6c22f46e02d070f943675e993602791da2a94641 [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
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000029#define PRINT_SHADERS 0
30
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 normalized_texel_size_name(int stage, GrStringBuilder* s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000084 *s = "uTexelSize";
bsalomon@google.comfc296292011-05-06 13:53:47 +000085 s->appendS32(stage);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000086}
87
bsalomon@google.com4fa66942011-09-20 19:06:12 +000088inline void sampler_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +000089 *s = "uSampler";
bsalomon@google.comfc296292011-05-06 13:53:47 +000090 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +000091}
92
bsalomon@google.com4fa66942011-09-20 19:06:12 +000093inline void radial2_param_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +000094 *s = "uRadial2Params";
bsalomon@google.comfc296292011-05-06 13:53:47 +000095 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +000096}
97
bsalomon@google.com4fa66942011-09-20 19:06:12 +000098inline void tex_domain_name(int stage, GrStringBuilder* s) {
junov@google.com6acc9b32011-05-16 18:32:07 +000099 *s = "uTexDom";
100 s->appendS32(stage);
101}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000102}
junov@google.com6acc9b32011-05-16 18:32:07 +0000103
junov@google.comf93e7172011-03-31 21:26:24 +0000104GrGLProgram::GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000105}
106
107GrGLProgram::~GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000108}
109
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000110void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000111 GrBlendCoeff* dstCoeff) const {
112 switch (fProgramDesc.fDualSrcOutput) {
113 case ProgramDesc::kNone_DualSrcOutput:
114 break;
115 // the prog will write a coverage value to the secondary
116 // output and the dst is blended by one minus that value.
117 case ProgramDesc::kCoverage_DualSrcOutput:
118 case ProgramDesc::kCoverageISA_DualSrcOutput:
119 case ProgramDesc::kCoverageISC_DualSrcOutput:
120 *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_BlendCoeff;
121 break;
122 default:
123 GrCrash("Unexpected dual source blend output");
124 break;
125 }
126}
127
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000128// assigns modulation of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000129// vars can be vec4s or floats (or one of each)
130// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000131// if either var is "" then assign to the other var
132// if both are "" then assign all ones
133static inline void modulate_helper(const char* outputVar,
134 const char* var0,
135 const char* var1,
136 GrStringBuilder* code) {
137 GrAssert(NULL != outputVar);
138 GrAssert(NULL != var0);
139 GrAssert(NULL != var1);
140 GrAssert(NULL != code);
141
142 bool has0 = '\0' != *var0;
143 bool has1 = '\0' != *var1;
144
145 if (!has0 && !has1) {
146 code->appendf("\t%s = %s;\n", outputVar, all_ones_vec(4));
147 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000148 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000149 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000150 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000151 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000152 code->appendf("\t%s = vec4(%s * %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000153 }
154}
155
156// assigns addition of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000157// vars can be vec4s or floats (or one of each)
158// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000159// if either var is "" then assign to the other var
160// if both are "" then assign all zeros
161static inline void add_helper(const char* outputVar,
162 const char* var0,
163 const char* var1,
164 GrStringBuilder* code) {
165 GrAssert(NULL != outputVar);
166 GrAssert(NULL != var0);
167 GrAssert(NULL != var1);
168 GrAssert(NULL != code);
169
170 bool has0 = '\0' != *var0;
171 bool has1 = '\0' != *var1;
172
173 if (!has0 && !has1) {
174 code->appendf("\t%s = %s;\n", outputVar, all_zeros_vec(4));
175 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000176 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000177 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000178 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000179 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000180 code->appendf("\t%s = vec4(%s + %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000181 }
182}
183
184// given two blend coeffecients determine whether the src
185// and/or dst computation can be omitted.
186static inline void needBlendInputs(SkXfermode::Coeff srcCoeff,
187 SkXfermode::Coeff dstCoeff,
188 bool* needSrcValue,
189 bool* needDstValue) {
190 if (SkXfermode::kZero_Coeff == srcCoeff) {
191 switch (dstCoeff) {
192 // these all read the src
193 case SkXfermode::kSC_Coeff:
194 case SkXfermode::kISC_Coeff:
195 case SkXfermode::kSA_Coeff:
196 case SkXfermode::kISA_Coeff:
197 *needSrcValue = true;
198 break;
199 default:
200 *needSrcValue = false;
201 break;
202 }
203 } else {
204 *needSrcValue = true;
205 }
206 if (SkXfermode::kZero_Coeff == dstCoeff) {
207 switch (srcCoeff) {
208 // these all read the dst
209 case SkXfermode::kDC_Coeff:
210 case SkXfermode::kIDC_Coeff:
211 case SkXfermode::kDA_Coeff:
212 case SkXfermode::kIDA_Coeff:
213 *needDstValue = true;
214 break;
215 default:
216 *needDstValue = false;
217 break;
218 }
219 } else {
220 *needDstValue = true;
Scroggo97c88c22011-05-11 14:05:25 +0000221 }
222}
223
224/**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000225 * Create a blend_coeff * value string to be used in shader code. Sets empty
226 * string if result is trivially zero.
227 */
228static void blendTermString(GrStringBuilder* str, SkXfermode::Coeff coeff,
229 const char* src, const char* dst,
230 const char* value) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000231 switch (coeff) {
232 case SkXfermode::kZero_Coeff: /** 0 */
233 *str = "";
234 break;
235 case SkXfermode::kOne_Coeff: /** 1 */
236 *str = value;
237 break;
238 case SkXfermode::kSC_Coeff:
239 str->printf("(%s * %s)", src, value);
240 break;
241 case SkXfermode::kISC_Coeff:
242 str->printf("((%s - %s) * %s)", all_ones_vec(4), src, value);
243 break;
244 case SkXfermode::kDC_Coeff:
245 str->printf("(%s * %s)", dst, value);
246 break;
247 case SkXfermode::kIDC_Coeff:
248 str->printf("((%s - %s) * %s)", all_ones_vec(4), dst, value);
249 break;
250 case SkXfermode::kSA_Coeff: /** src alpha */
251 str->printf("(%s.a * %s)", src, value);
252 break;
253 case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */
254 str->printf("((1.0 - %s.a) * %s)", src, value);
255 break;
256 case SkXfermode::kDA_Coeff: /** dst alpha */
257 str->printf("(%s.a * %s)", dst, value);
258 break;
259 case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */
260 str->printf("((1.0 - %s.a) * %s)", dst, value);
261 break;
262 default:
263 GrCrash("Unexpected xfer coeff.");
264 break;
265 }
266}
267/**
Scroggo97c88c22011-05-11 14:05:25 +0000268 * Adds a line to the fragment shader code which modifies the color by
269 * the specified color filter.
270 */
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000271static void addColorFilter(GrStringBuilder* fsCode, const char * outputVar,
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000272 SkXfermode::Coeff uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000273 SkXfermode::Coeff colorCoeff,
274 const char* inColor) {
275 GrStringBuilder colorStr, constStr;
276 blendTermString(&colorStr, colorCoeff, COL_FILTER_UNI_NAME,
277 inColor, inColor);
278 blendTermString(&constStr, uniformCoeff, COL_FILTER_UNI_NAME,
279 inColor, COL_FILTER_UNI_NAME);
280
281 add_helper(outputVar, colorStr.c_str(), constStr.c_str(), fsCode);
Scroggo97c88c22011-05-11 14:05:25 +0000282}
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000283/**
284 * Adds code to the fragment shader code which modifies the color by
285 * the specified color matrix.
286 */
287static void addColorMatrix(GrStringBuilder* fsCode, const char * outputVar,
288 const char* inColor) {
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000289 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);
290 fsCode->appendf("\t%s.rgb *= %s.a;\n", outputVar, outputVar);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000291}
Scroggo97c88c22011-05-11 14:05:25 +0000292
bsalomon@google.com96399942012-02-13 14:39:16 +0000293void GrGLProgram::genEdgeCoverage(const GrGLContextInfo& gl,
bsalomon@google.com66105672011-09-15 15:12:00 +0000294 GrVertexLayout layout,
295 CachedData* programData,
296 GrStringBuilder* coverageVar,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000297 GrGLShaderBuilder* segments) const {
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000298 if (layout & GrDrawTarget::kEdge_VertexLayoutBit) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000299 const char *vsName, *fsName;
tomhudson@google.com23cb2292012-05-30 18:26:03 +0000300 segments->addVarying(kVec4f_GrSLType, "Edge", &vsName, &fsName);
tomhudson@google.com168e6342012-04-18 17:49:20 +0000301 segments->fVSAttrs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000302 GrGLShaderVar::kAttribute_TypeModifier, EDGE_ATTR_NAME);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000303 segments->fVSCode.appendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName);
bsalomon@google.com93c96602012-04-27 13:05:21 +0000304 switch (fProgramDesc.fVertexEdgeType) {
305 case GrDrawState::kHairLine_EdgeType:
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000306 segments->fFSCode.appendf("\tfloat edgeAlpha = abs(dot(vec3(gl_FragCoord.xy,1), %s.xyz));\n", fsName);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000307 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
bsalomon@google.com93c96602012-04-27 13:05:21 +0000308 break;
309 case GrDrawState::kQuad_EdgeType:
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000310 segments->fFSCode.append("\tfloat edgeAlpha;\n");
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000311 // keep the derivative instructions outside the conditional
312 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
313 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
314 segments->fFSCode.appendf("\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName);
315 // today we know z and w are in device space. We could use derivatives
316 segments->fFSCode.appendf("\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName, fsName);
317 segments->fFSCode.append ("\t} else {\n");
318 segments->fFSCode.appendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
319 "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
320 fsName, fsName);
321 segments->fFSCode.appendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000322 segments->fFSCode.append("\t\tedgeAlpha = clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n"
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000323 "\t}\n");
bsalomon@google.com96399942012-02-13 14:39:16 +0000324 if (kES2_GrGLBinding == gl.binding()) {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000325 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
326 }
bsalomon@google.com93c96602012-04-27 13:05:21 +0000327 break;
328 case GrDrawState::kHairQuad_EdgeType:
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000329 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
330 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000331 segments->fFSCode.appendf("\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
332 "\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
333 fsName, fsName);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000334 segments->fFSCode.appendf("\tfloat edgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000335 segments->fFSCode.append("\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n");
336 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
bsalomon@google.com96399942012-02-13 14:39:16 +0000337 if (kES2_GrGLBinding == gl.binding()) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000338 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
339 }
bsalomon@google.com93c96602012-04-27 13:05:21 +0000340 break;
341 case GrDrawState::kCircle_EdgeType:
342 segments->fFSCode.append("\tfloat edgeAlpha;\n");
343 segments->fFSCode.appendf("\tfloat d = distance(gl_FragCoord.xy, %s.xy);\n", fsName);
344 segments->fFSCode.appendf("\tfloat outerAlpha = smoothstep(d - 0.5, d + 0.5, %s.z);\n", fsName);
345 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);
346 segments->fFSCode.append("\tedgeAlpha = outerAlpha * innerAlpha;\n");
347 break;
348 default:
349 GrCrash("Unknown Edge Type!");
350 break;
bsalomon@google.com66105672011-09-15 15:12:00 +0000351 }
bsalomon@google.com66105672011-09-15 15:12:00 +0000352 *coverageVar = "edgeAlpha";
353 } else {
354 coverageVar->reset();
355 }
356}
357
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000358namespace {
359
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000360void genInputColor(GrGLProgram::ProgramDesc::ColorInput colorInput,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000361 GrGLProgram::CachedData* programData,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000362 GrGLShaderBuilder* segments,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000363 GrStringBuilder* inColor) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000364 switch (colorInput) {
365 case GrGLProgram::ProgramDesc::kAttribute_ColorInput: {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000366 segments->fVSAttrs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000367 GrGLShaderVar::kAttribute_TypeModifier,
368 COL_ATTR_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000369 const char *vsName, *fsName;
tomhudson@google.com23cb2292012-05-30 18:26:03 +0000370 segments->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000371 segments->fVSCode.appendf("\t%s = " COL_ATTR_NAME ";\n", vsName);
372 *inColor = fsName;
373 } break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000374 case GrGLProgram::ProgramDesc::kUniform_ColorInput:
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000375 segments->addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
376 kVec4f_GrSLType, COL_UNI_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000377 programData->fUniLocations.fColorUni = kUseUniform;
378 *inColor = COL_UNI_NAME;
379 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000380 case GrGLProgram::ProgramDesc::kTransBlack_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000381 GrAssert(!"needComputedColor should be false.");
382 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000383 case GrGLProgram::ProgramDesc::kSolidWhite_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000384 break;
385 default:
386 GrCrash("Unknown color type.");
387 break;
388 }
389}
390
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000391void genAttributeCoverage(GrGLShaderBuilder* segments,
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000392 GrStringBuilder* inOutCoverage) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000393 segments->fVSAttrs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000394 GrGLShaderVar::kAttribute_TypeModifier,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000395 COV_ATTR_NAME);
396 const char *vsName, *fsName;
tomhudson@google.com23cb2292012-05-30 18:26:03 +0000397 segments->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000398 segments->fVSCode.appendf("\t%s = " COV_ATTR_NAME ";\n", vsName);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000399 if (inOutCoverage->size()) {
400 segments->fFSCode.appendf("\tvec4 attrCoverage = %s * %s;\n",
401 fsName, inOutCoverage->c_str());
402 *inOutCoverage = "attrCoverage";
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000403 } else {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000404 *inOutCoverage = fsName;
405 }
406}
407
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000408void genUniformCoverage(GrGLShaderBuilder* segments,
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000409 GrGLProgram::CachedData* programData,
410 GrStringBuilder* inOutCoverage) {
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000411 segments->addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
412 kVec4f_GrSLType, COV_UNI_NAME);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000413 programData->fUniLocations.fCoverageUni = kUseUniform;
414 if (inOutCoverage->size()) {
415 segments->fFSCode.appendf("\tvec4 uniCoverage = %s * %s;\n",
416 COV_UNI_NAME, inOutCoverage->c_str());
417 *inOutCoverage = "uniCoverage";
418 } else {
419 *inOutCoverage = COV_UNI_NAME;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000420 }
421}
422
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000423}
424
bsalomon@google.com96399942012-02-13 14:39:16 +0000425void GrGLProgram::genGeometryShader(const GrGLContextInfo& gl,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000426 GrGLShaderBuilder* segments) const {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000427#if GR_GL_EXPERIMENTAL_GS
428 if (fProgramDesc.fExperimentalGS) {
bsalomon@google.com96399942012-02-13 14:39:16 +0000429 GrAssert(gl.glslGeneration() >= k150_GrGLSLGeneration);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000430 segments->fGSHeader.append("layout(triangles) in;\n"
431 "layout(triangle_strip, max_vertices = 6) out;\n");
432 segments->fGSCode.append("void main() {\n"
433 "\tfor (int i = 0; i < 3; ++i) {\n"
434 "\t\tgl_Position = gl_in[i].gl_Position;\n");
435 if (this->fProgramDesc.fEmitsPointSize) {
436 segments->fGSCode.append("\t\tgl_PointSize = 1.0;\n");
437 }
438 GrAssert(segments->fGSInputs.count() == segments->fGSOutputs.count());
439 int count = segments->fGSInputs.count();
440 for (int i = 0; i < count; ++i) {
441 segments->fGSCode.appendf("\t\t%s = %s[i];\n",
442 segments->fGSOutputs[i].getName().c_str(),
443 segments->fGSInputs[i].getName().c_str());
444 }
445 segments->fGSCode.append("\t\tEmitVertex();\n"
446 "\t}\n"
447 "\tEndPrimitive();\n"
448 "}\n");
449 }
450#endif
451}
452
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000453const char* GrGLProgram::adjustInColor(const GrStringBuilder& inColor) const {
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000454 if (inColor.size()) {
455 return inColor.c_str();
456 } else {
457 if (ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput) {
458 return all_ones_vec(4);
459 } else {
460 return all_zeros_vec(4);
461 }
462 }
463}
464
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000465// If this destructor is in the header file, we must include GrGLProgramStage
466// instead of just forward-declaring it.
467GrGLProgram::CachedData::~CachedData() {
468 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
469 delete fCustomStage[i];
470 }
471}
472
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000473
bsalomon@google.com96399942012-02-13 14:39:16 +0000474bool GrGLProgram::genProgram(const GrGLContextInfo& gl,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000475 GrCustomStage** customStages,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000476 GrGLProgram::CachedData* programData) const {
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000477 GrGLShaderBuilder segments;
junov@google.comf93e7172011-03-31 21:26:24 +0000478 const uint32_t& layout = fProgramDesc.fVertexLayout;
479
bsalomon@google.com91961302011-05-09 18:39:58 +0000480 programData->fUniLocations.reset();
junov@google.comf93e7172011-03-31 21:26:24 +0000481
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000482#if GR_GL_EXPERIMENTAL_GS
483 segments.fUsesGS = fProgramDesc.fExperimentalGS;
484#endif
485
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000486 SkXfermode::Coeff colorCoeff, uniformCoeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000487 bool applyColorMatrix = SkToBool(fProgramDesc.fColorMatrixEnabled);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000488 // The rest of transfer mode color filters have not been implemented
489 if (fProgramDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) {
490 GR_DEBUGCODE(bool success =)
tomhudson@google.com0d831722011-06-02 15:37:14 +0000491 SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode>
492 (fProgramDesc.fColorFilterXfermode),
493 &uniformCoeff, &colorCoeff);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000494 GR_DEBUGASSERT(success);
495 } else {
496 colorCoeff = SkXfermode::kOne_Coeff;
497 uniformCoeff = SkXfermode::kZero_Coeff;
498 }
499
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000500 // no need to do the color filter / matrix at all if coverage is 0. The
501 // output color is scaled by the coverage. All the dual source outputs are
502 // scaled by the coverage as well.
503 if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fCoverageInput) {
504 colorCoeff = SkXfermode::kZero_Coeff;
505 uniformCoeff = SkXfermode::kZero_Coeff;
506 applyColorMatrix = false;
507 }
508
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000509 // If we know the final color is going to be all zeros then we can
510 // simplify the color filter coeffecients. needComputedColor will then
511 // come out false below.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000512 if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fColorInput) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000513 colorCoeff = SkXfermode::kZero_Coeff;
514 if (SkXfermode::kDC_Coeff == uniformCoeff ||
515 SkXfermode::kDA_Coeff == uniformCoeff) {
516 uniformCoeff = SkXfermode::kZero_Coeff;
517 } else if (SkXfermode::kIDC_Coeff == uniformCoeff ||
518 SkXfermode::kIDA_Coeff == uniformCoeff) {
519 uniformCoeff = SkXfermode::kOne_Coeff;
520 }
521 }
522
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000523 bool needColorFilterUniform;
524 bool needComputedColor;
525 needBlendInputs(uniformCoeff, colorCoeff,
526 &needColorFilterUniform, &needComputedColor);
527
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000528 // the dual source output has no canonical var name, have to
529 // declare an output, which is incompatible with gl_FragColor/gl_FragData.
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000530 bool dualSourceOutputWritten = false;
bsalomon@google.com96399942012-02-13 14:39:16 +0000531 segments.fHeader.printf(GrGetGLSLVersionDecl(gl.binding(),
532 gl.glslGeneration()));
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000533
534 GrGLShaderVar colorOutput;
bsalomon@google.com96399942012-02-13 14:39:16 +0000535 bool isColorDeclared = GrGLSLSetupFSColorOuput(gl.glslGeneration(),
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000536 declared_color_output_name(),
537 &colorOutput);
538 if (isColorDeclared) {
539 segments.fFSOutputs.push_back(colorOutput);
540 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000541
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000542 segments.addUniform(GrGLShaderBuilder::kVertex_VariableLifetime,
543 kMat33f_GrSLType, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000544 programData->fUniLocations.fViewMatrixUni = kUseUniform;
bsalomon@google.com341767c2012-05-11 20:47:39 +0000545
tomhudson@google.com168e6342012-04-18 17:49:20 +0000546 segments.fVSAttrs.push_back().set(kVec2f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000547 GrGLShaderVar::kAttribute_TypeModifier, POS_ATTR_NAME);
junov@google.comf93e7172011-03-31 21:26:24 +0000548
bsalomon@google.com91961302011-05-09 18:39:58 +0000549 segments.fVSCode.append(
550 "void main() {\n"
551 "\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3("POS_ATTR_NAME", 1);\n"
552 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n");
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000553
bsalomon@google.com91961302011-05-09 18:39:58 +0000554 // incoming color to current stage being processed.
555 GrStringBuilder inColor;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000556
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000557 if (needComputedColor) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000558 genInputColor((ProgramDesc::ColorInput) fProgramDesc.fColorInput,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000559 programData, &segments, &inColor);
junov@google.comf93e7172011-03-31 21:26:24 +0000560 }
561
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000562 // we output point size in the GS if present
563 if (fProgramDesc.fEmitsPointSize && !segments.fUsesGS){
bsalomon@google.com91961302011-05-09 18:39:58 +0000564 segments.fVSCode.append("\tgl_PointSize = 1.0;\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000565 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000566
bsalomon@google.com91961302011-05-09 18:39:58 +0000567 segments.fFSCode.append("void main() {\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000568
569 // add texture coordinates that are used to the list of vertex attr decls
tomhudson@google.com93813632011-10-27 20:21:16 +0000570 GrStringBuilder texCoordAttrs[GrDrawState::kMaxTexCoords];
571 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000572 if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000573 tex_attr_name(t, texCoordAttrs + t);
tomhudson@google.com168e6342012-04-18 17:49:20 +0000574 segments.fVSAttrs.push_back().set(kVec2f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000575 GrGLShaderVar::kAttribute_TypeModifier,
576 texCoordAttrs[t].c_str());
junov@google.comf93e7172011-03-31 21:26:24 +0000577 }
578 }
579
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000580 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000581 // We need to convert generic effect representations to GL-specific
582 // backends so they can be accesseed in genStageCode() and in subsequent,
583 // uses of programData, but it's safest to do so below when we're *sure*
584 // we need them.
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000585 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000586 programData->fCustomStage[s] = NULL;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000587 }
588
589 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000590 // compute the final color
Scroggo97c88c22011-05-11 14:05:25 +0000591
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000592 // if we have color stages string them together, feeding the output color
593 // of each to the next and generating code for each stage.
594 if (needComputedColor) {
595 GrStringBuilder outColor;
596 for (int s = 0; s < fProgramDesc.fFirstCoverageStage; ++s) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000597 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000598 // create var to hold stage result
599 outColor = "color";
600 outColor.appendS32(s);
601 segments.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str());
602
603 const char* inCoords;
604 // figure out what our input coords are
605 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) &
606 layout) {
607 inCoords = POS_ATTR_NAME;
Scroggo97c88c22011-05-11 14:05:25 +0000608 } else {
609 int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout);
610 // we better have input tex coordinates if stage is enabled.
611 GrAssert(tcIdx >= 0);
612 GrAssert(texCoordAttrs[tcIdx].size());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000613 inCoords = texCoordAttrs[tcIdx].c_str();
junov@google.comf93e7172011-03-31 21:26:24 +0000614 }
615
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000616 if (NULL != customStages[s]) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000617 const GrProgramStageFactory& factory =
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000618 customStages[s]->getFactory();
619 programData->fCustomStage[s] =
bsalomon@google.comb505a122012-05-31 18:40:36 +0000620 factory.createGLInstance(*customStages[s]);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000621 }
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000622 this->genStageCode(gl,
623 s,
624 fProgramDesc.fStages[s],
625 inColor.size() ? inColor.c_str() : NULL,
626 outColor.c_str(),
627 inCoords,
628 &segments,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000629 &programData->fUniLocations.fStages[s],
630 programData->fCustomStage[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000631 inColor = outColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000632 }
633 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000634 }
Scroggo97c88c22011-05-11 14:05:25 +0000635
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000636 // if have all ones or zeros for the "dst" input to the color filter then we
637 // may be able to make additional optimizations.
638 if (needColorFilterUniform && needComputedColor && !inColor.size()) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000639 GrAssert(ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000640 bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff ||
641 SkXfermode::kIDA_Coeff == uniformCoeff;
642 if (uniformCoeffIsZero) {
643 uniformCoeff = SkXfermode::kZero_Coeff;
644 bool bogus;
645 needBlendInputs(SkXfermode::kZero_Coeff, colorCoeff,
646 &needColorFilterUniform, &bogus);
647 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000648 }
649 if (needColorFilterUniform) {
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000650 segments.addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
651 kVec4f_GrSLType, COL_FILTER_UNI_NAME);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000652 programData->fUniLocations.fColorFilterUni = kUseUniform;
653 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000654 bool wroteFragColorZero = false;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000655 if (SkXfermode::kZero_Coeff == uniformCoeff &&
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000656 SkXfermode::kZero_Coeff == colorCoeff &&
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000657 !applyColorMatrix) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000658 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000659 colorOutput.getName().c_str(),
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000660 all_zeros_vec(4));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000661 wroteFragColorZero = true;
662 } else if (SkXfermode::kDst_Mode != fProgramDesc.fColorFilterXfermode) {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000663 segments.fFSCode.append("\tvec4 filteredColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000664 const char* color = adjustInColor(inColor);
tomhudson@google.com0d831722011-06-02 15:37:14 +0000665 addColorFilter(&segments.fFSCode, "filteredColor", uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000666 colorCoeff, color);
667 inColor = "filteredColor";
668 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000669 if (applyColorMatrix) {
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000670 segments.addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
671 kMat44f_GrSLType, COL_MATRIX_UNI_NAME);
672 segments.addUniform(GrGLShaderBuilder::kFragment_VariableLifetime,
673 kVec4f_GrSLType, COL_MATRIX_VEC_UNI_NAME);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000674 programData->fUniLocations.fColorMatrixUni = kUseUniform;
675 programData->fUniLocations.fColorMatrixVecUni = kUseUniform;
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000676 segments.fFSCode.append("\tvec4 matrixedColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000677 const char* color = adjustInColor(inColor);
678 addColorMatrix(&segments.fFSCode, "matrixedColor", color);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000679 inColor = "matrixedColor";
680 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000681
682 ///////////////////////////////////////////////////////////////////////////
683 // compute the partial coverage (coverage stages and edge aa)
684
685 GrStringBuilder inCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000686 bool coverageIsZero = ProgramDesc::kTransBlack_ColorInput ==
687 fProgramDesc.fCoverageInput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000688 // we don't need to compute coverage at all if we know the final shader
689 // output will be zero and we don't have a dual src blend output.
690 if (!wroteFragColorZero ||
691 ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000692
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000693 if (!coverageIsZero) {
694 this->genEdgeCoverage(gl,
695 layout,
696 programData,
697 &inCoverage,
698 &segments);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000699
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000700 switch (fProgramDesc.fCoverageInput) {
701 case ProgramDesc::kSolidWhite_ColorInput:
702 // empty string implies solid white
703 break;
704 case ProgramDesc::kAttribute_ColorInput:
705 genAttributeCoverage(&segments, &inCoverage);
706 break;
707 case ProgramDesc::kUniform_ColorInput:
708 genUniformCoverage(&segments, programData, &inCoverage);
709 break;
710 default:
711 GrCrash("Unexpected input coverage.");
712 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000713
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000714 GrStringBuilder outCoverage;
715 const int& startStage = fProgramDesc.fFirstCoverageStage;
716 for (int s = startStage; s < GrDrawState::kNumStages; ++s) {
717 if (fProgramDesc.fStages[s].isEnabled()) {
718 // create var to hold stage output
719 outCoverage = "coverage";
720 outCoverage.appendS32(s);
721 segments.fFSCode.appendf("\tvec4 %s;\n",
722 outCoverage.c_str());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000723
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000724 const char* inCoords;
725 // figure out what our input coords are
726 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) &
727 layout) {
728 inCoords = POS_ATTR_NAME;
729 } else {
730 int tcIdx =
731 GrDrawTarget::VertexTexCoordsForStage(s, layout);
732 // we better have input tex coordinates if stage is
733 // enabled.
734 GrAssert(tcIdx >= 0);
735 GrAssert(texCoordAttrs[tcIdx].size());
736 inCoords = texCoordAttrs[tcIdx].c_str();
737 }
738
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000739 if (NULL != customStages[s]) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000740 const GrProgramStageFactory& factory =
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000741 customStages[s]->getFactory();
742 programData->fCustomStage[s] =
bsalomon@google.comb505a122012-05-31 18:40:36 +0000743 factory.createGLInstance(*customStages[s]);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000744 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000745 this->genStageCode(gl, s,
746 fProgramDesc.fStages[s],
747 inCoverage.size() ? inCoverage.c_str() : NULL,
748 outCoverage.c_str(),
749 inCoords,
750 &segments,
751 &programData->fUniLocations.fStages[s],
752 programData->fCustomStage[s]);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000753 inCoverage = outCoverage;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000754 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000755 }
756 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000757 if (ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000758 segments.fFSOutputs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000759 GrGLShaderVar::kOut_TypeModifier,
760 dual_source_output_name());
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000761 bool outputIsZero = coverageIsZero;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000762 GrStringBuilder coeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000763 if (!outputIsZero &&
764 ProgramDesc::kCoverage_DualSrcOutput !=
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000765 fProgramDesc.fDualSrcOutput && !wroteFragColorZero) {
766 if (!inColor.size()) {
767 outputIsZero = true;
768 } else {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000769 if (fProgramDesc.fDualSrcOutput ==
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000770 ProgramDesc::kCoverageISA_DualSrcOutput) {
771 coeff.printf("(1 - %s.a)", inColor.c_str());
772 } else {
773 coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str());
774 }
775 }
776 }
777 if (outputIsZero) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000778 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000779 dual_source_output_name(),
780 all_zeros_vec(4));
781 } else {
782 modulate_helper(dual_source_output_name(),
783 coeff.c_str(),
784 inCoverage.c_str(),
785 &segments.fFSCode);
786 }
787 dualSourceOutputWritten = true;
788 }
junov@google.comf93e7172011-03-31 21:26:24 +0000789 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000790
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000791 ///////////////////////////////////////////////////////////////////////////
792 // combine color and coverage as frag color
793
794 if (!wroteFragColorZero) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000795 if (coverageIsZero) {
796 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000797 colorOutput.getName().c_str(),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000798 all_zeros_vec(4));
799 } else {
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000800 modulate_helper(colorOutput.getName().c_str(),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000801 inColor.c_str(),
802 inCoverage.c_str(),
803 &segments.fFSCode);
804 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000805 if (ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig ==
806 fProgramDesc.fOutputConfig) {
807 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",
808 colorOutput.getName().c_str(),
809 colorOutput.getName().c_str(),
810 colorOutput.getName().c_str(),
811 colorOutput.getName().c_str(),
812 colorOutput.getName().c_str());
813 } else if (ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig ==
814 fProgramDesc.fOutputConfig) {
815 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",
816 colorOutput.getName().c_str(),
817 colorOutput.getName().c_str(),
818 colorOutput.getName().c_str(),
819 colorOutput.getName().c_str(),
820 colorOutput.getName().c_str());
bsalomon@google.comc4364992011-11-07 15:54:49 +0000821 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000822 }
823
bsalomon@google.com91961302011-05-09 18:39:58 +0000824 segments.fVSCode.append("}\n");
825 segments.fFSCode.append("}\n");
826
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000827 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000828 // insert GS
829#if GR_DEBUG
bsalomon@google.com96399942012-02-13 14:39:16 +0000830 this->genGeometryShader(gl, &segments);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000831#endif
832
833 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000834 // compile and setup attribs and unis
835
bsalomon@google.com96399942012-02-13 14:39:16 +0000836 if (!CompileShaders(gl, segments, programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000837 return false;
838 }
839
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000840 if (!this->bindOutputsAttribsAndLinkProgram(gl, texCoordAttrs,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000841 isColorDeclared,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000842 dualSourceOutputWritten,
843 programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000844 return false;
845 }
846
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000847 this->getUniformLocationsAndInitCache(gl, programData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000848
849 return true;
850}
851
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000852namespace {
853
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000854inline void expand_decls(const VarArray& vars,
bsalomon@google.com96399942012-02-13 14:39:16 +0000855 const GrGLContextInfo& gl,
856 GrStringBuilder* string) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000857 const int count = vars.count();
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000858 for (int i = 0; i < count; ++i) {
bsalomon@google.com96399942012-02-13 14:39:16 +0000859 vars[i].appendDecl(gl, string);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000860 }
861}
862
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000863inline void print_shader(int stringCnt,
864 const char** strings,
865 int* stringLengths) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000866 for (int i = 0; i < stringCnt; ++i) {
867 if (NULL == stringLengths || stringLengths[i] < 0) {
868 GrPrintf(strings[i]);
869 } else {
870 GrPrintf("%.*s", stringLengths[i], strings[i]);
871 }
872 }
873}
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000874
875typedef SkTArray<const char*, true> StrArray;
876#define PREALLOC_STR_ARRAY(N) SkSTArray<(N), const char*, true>
877
878typedef SkTArray<int, true> LengthArray;
879#define PREALLOC_LENGTH_ARRAY(N) SkSTArray<(N), int, true>
880
881// these shouldn't relocate
882typedef GrTAllocator<GrStringBuilder> TempArray;
883#define PREALLOC_TEMP_ARRAY(N) GrSTAllocator<(N), GrStringBuilder>
884
885inline void append_string(const GrStringBuilder& str,
886 StrArray* strings,
887 LengthArray* lengths) {
888 int length = (int) str.size();
889 if (length) {
890 strings->push_back(str.c_str());
891 lengths->push_back(length);
892 }
893 GrAssert(strings->count() == lengths->count());
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000894}
895
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000896inline void append_decls(const VarArray& vars,
bsalomon@google.com96399942012-02-13 14:39:16 +0000897 const GrGLContextInfo& gl,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000898 StrArray* strings,
899 LengthArray* lengths,
bsalomon@google.com96399942012-02-13 14:39:16 +0000900 TempArray* temp) {
901 expand_decls(vars, gl, &temp->push_back());
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000902 append_string(temp->back(), strings, lengths);
903}
904
905}
906
bsalomon@google.com96399942012-02-13 14:39:16 +0000907bool GrGLProgram::CompileShaders(const GrGLContextInfo& gl,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000908 const GrGLShaderBuilder& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +0000909 CachedData* programData) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000910 enum { kPreAllocStringCnt = 8 };
junov@google.comf93e7172011-03-31 21:26:24 +0000911
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000912 PREALLOC_STR_ARRAY(kPreAllocStringCnt) strs;
913 PREALLOC_LENGTH_ARRAY(kPreAllocStringCnt) lengths;
914 PREALLOC_TEMP_ARRAY(kPreAllocStringCnt) temps;
915
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000916 GrStringBuilder unis;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000917 GrStringBuilder inputs;
918 GrStringBuilder outputs;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000919
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000920 append_string(segments.fHeader, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +0000921 append_decls(segments.fVSUnis, gl, &strs, &lengths, &temps);
922 append_decls(segments.fVSAttrs, gl, &strs, &lengths, &temps);
923 append_decls(segments.fVSOutputs, gl, &strs, &lengths, &temps);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000924 append_string(segments.fVSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +0000925
926#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000927 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000928 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000929#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000930
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000931 programData->fVShaderID =
932 CompileShader(gl, GR_GL_VERTEX_SHADER, strs.count(),
933 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +0000934
bsalomon@google.com91961302011-05-09 18:39:58 +0000935 if (!programData->fVShaderID) {
936 return false;
937 }
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000938 if (segments.fUsesGS) {
939 strs.reset();
940 lengths.reset();
941 temps.reset();
942 append_string(segments.fHeader, &strs, &lengths);
943 append_string(segments.fGSHeader, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +0000944 append_decls(segments.fGSInputs, gl, &strs, &lengths, &temps);
945 append_decls(segments.fGSOutputs, gl, &strs, &lengths, &temps);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000946 append_string(segments.fGSCode, &strs, &lengths);
947#if PRINT_SHADERS
948 print_shader(strs.count(), &strs[0], &lengths[0]);
949 GrPrintf("\n");
950#endif
951 programData->fGShaderID =
952 CompileShader(gl, GR_GL_GEOMETRY_SHADER, strs.count(),
953 &strs[0], &lengths[0]);
954 } else {
955 programData->fGShaderID = 0;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000956 }
junov@google.comf93e7172011-03-31 21:26:24 +0000957
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000958 strs.reset();
959 lengths.reset();
960 temps.reset();
961
962 append_string(segments.fHeader, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +0000963 GrStringBuilder precisionStr(GrGetGLSLShaderPrecisionDecl(gl.binding()));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000964 append_string(precisionStr, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +0000965 append_decls(segments.fFSUnis, gl, &strs, &lengths, &temps);
966 append_decls(segments.fFSInputs, gl, &strs, &lengths, &temps);
bsalomon@google.com373a6632011-10-19 20:43:20 +0000967 // We shouldn't have declared outputs on 1.10
bsalomon@google.com96399942012-02-13 14:39:16 +0000968 GrAssert(k110_GrGLSLGeneration != gl.glslGeneration() ||
tomhudson@google.com086e5352011-12-08 14:44:10 +0000969 segments.fFSOutputs.empty());
bsalomon@google.com96399942012-02-13 14:39:16 +0000970 append_decls(segments.fFSOutputs, gl, &strs, &lengths, &temps);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000971 append_string(segments.fFSFunctions, &strs, &lengths);
972 append_string(segments.fFSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +0000973
974#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000975 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000976 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000977#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000978
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000979 programData->fFShaderID =
980 CompileShader(gl, GR_GL_FRAGMENT_SHADER, strs.count(),
981 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +0000982
bsalomon@google.com91961302011-05-09 18:39:58 +0000983 if (!programData->fFShaderID) {
984 return false;
junov@google.comf93e7172011-03-31 21:26:24 +0000985 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000986
bsalomon@google.com91961302011-05-09 18:39:58 +0000987 return true;
junov@google.comf93e7172011-03-31 21:26:24 +0000988}
989
bsalomon@google.com96399942012-02-13 14:39:16 +0000990#define GL_CALL(X) GR_GL_CALL(gl.interface(), X)
991#define GL_CALL_RET(R, X) GR_GL_CALL_RET(gl.interface(), R, X)
992
993GrGLuint GrGLProgram::CompileShader(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000994 GrGLenum type,
995 int stringCnt,
996 const char** strings,
997 int* stringLengths) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000998 SK_TRACE_EVENT1("GrGLProgram::CompileShader",
999 "stringCount", SkStringPrintf("%i", stringCnt).c_str());
1000
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001001 GrGLuint shader;
bsalomon@google.com96399942012-02-13 14:39:16 +00001002 GL_CALL_RET(shader, CreateShader(type));
junov@google.comf93e7172011-03-31 21:26:24 +00001003 if (0 == shader) {
1004 return 0;
1005 }
1006
1007 GrGLint compiled = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001008 GL_CALL(ShaderSource(shader, stringCnt, strings, stringLengths));
1009 GL_CALL(CompileShader(shader));
1010 GL_CALL(GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled));
junov@google.comf93e7172011-03-31 21:26:24 +00001011
1012 if (!compiled) {
1013 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001014 GL_CALL(GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001015 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
junov@google.comf93e7172011-03-31 21:26:24 +00001016 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001017 // retrieve length even though we don't need it to workaround
1018 // bug in chrome cmd buffer param validation.
1019 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001020 GL_CALL(GetShaderInfoLog(shader, infoLen+1,
1021 &length, (char*)log.get()));
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001022 print_shader(stringCnt, strings, stringLengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001023 GrPrintf("\n%s", log.get());
1024 }
1025 GrAssert(!"Shader compilation failed!");
bsalomon@google.com96399942012-02-13 14:39:16 +00001026 GL_CALL(DeleteShader(shader));
junov@google.comf93e7172011-03-31 21:26:24 +00001027 return 0;
1028 }
1029 return shader;
1030}
1031
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001032bool GrGLProgram::bindOutputsAttribsAndLinkProgram(
bsalomon@google.com96399942012-02-13 14:39:16 +00001033 const GrGLContextInfo& gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001034 GrStringBuilder texCoordAttrNames[],
1035 bool bindColorOut,
tomhudson@google.com0d831722011-06-02 15:37:14 +00001036 bool bindDualSrcOut,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001037 CachedData* programData) const {
bsalomon@google.com96399942012-02-13 14:39:16 +00001038 GL_CALL_RET(programData->fProgramID, CreateProgram());
bsalomon@google.com91961302011-05-09 18:39:58 +00001039 if (!programData->fProgramID) {
1040 return false;
1041 }
1042 const GrGLint& progID = programData->fProgramID;
1043
bsalomon@google.com96399942012-02-13 14:39:16 +00001044 GL_CALL(AttachShader(progID, programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001045 if (programData->fGShaderID) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001046 GL_CALL(AttachShader(progID, programData->fGShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001047 }
bsalomon@google.com96399942012-02-13 14:39:16 +00001048 GL_CALL(AttachShader(progID, programData->fFShaderID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001049
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001050 if (bindColorOut) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001051 GL_CALL(BindFragDataLocation(programData->fProgramID,
1052 0, declared_color_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001053 }
1054 if (bindDualSrcOut) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001055 GL_CALL(BindFragDataLocationIndexed(programData->fProgramID,
1056 0, 1, dual_source_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001057 }
1058
bsalomon@google.com91961302011-05-09 18:39:58 +00001059 // Bind the attrib locations to same values for all shaders
bsalomon@google.com96399942012-02-13 14:39:16 +00001060 GL_CALL(BindAttribLocation(progID, PositionAttributeIdx(), POS_ATTR_NAME));
tomhudson@google.com93813632011-10-27 20:21:16 +00001061 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001062 if (texCoordAttrNames[t].size()) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001063 GL_CALL(BindAttribLocation(progID,
1064 TexCoordAttributeIdx(t),
1065 texCoordAttrNames[t].c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001066 }
1067 }
1068
bsalomon@google.com96399942012-02-13 14:39:16 +00001069 GL_CALL(BindAttribLocation(progID, ColorAttributeIdx(), COL_ATTR_NAME));
1070 GL_CALL(BindAttribLocation(progID, CoverageAttributeIdx(), COV_ATTR_NAME));
1071 GL_CALL(BindAttribLocation(progID, EdgeAttributeIdx(), EDGE_ATTR_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001072
bsalomon@google.com96399942012-02-13 14:39:16 +00001073 GL_CALL(LinkProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001074
1075 GrGLint linked = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001076 GL_CALL(GetProgramiv(progID, GR_GL_LINK_STATUS, &linked));
bsalomon@google.com91961302011-05-09 18:39:58 +00001077 if (!linked) {
1078 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001079 GL_CALL(GetProgramiv(progID, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001080 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
bsalomon@google.com91961302011-05-09 18:39:58 +00001081 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001082 // retrieve length even though we don't need it to workaround
1083 // bug in chrome cmd buffer param validation.
1084 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001085 GL_CALL(GetProgramInfoLog(progID,
1086 infoLen+1,
1087 &length,
1088 (char*)log.get()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001089 GrPrintf((char*)log.get());
1090 }
1091 GrAssert(!"Error linking program");
bsalomon@google.com96399942012-02-13 14:39:16 +00001092 GL_CALL(DeleteProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001093 programData->fProgramID = 0;
1094 return false;
1095 }
1096 return true;
1097}
1098
bsalomon@google.com96399942012-02-13 14:39:16 +00001099void GrGLProgram::getUniformLocationsAndInitCache(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001100 CachedData* programData) const {
bsalomon@google.com91961302011-05-09 18:39:58 +00001101 const GrGLint& progID = programData->fProgramID;
1102
1103 if (kUseUniform == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001104 GL_CALL_RET(programData->fUniLocations.fViewMatrixUni,
1105 GetUniformLocation(progID, VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001106 GrAssert(kUnusedUniform != programData->fUniLocations.fViewMatrixUni);
1107 }
1108 if (kUseUniform == programData->fUniLocations.fColorUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001109 GL_CALL_RET(programData->fUniLocations.fColorUni,
1110 GetUniformLocation(progID, COL_UNI_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001111 GrAssert(kUnusedUniform != programData->fUniLocations.fColorUni);
1112 }
Scroggo97c88c22011-05-11 14:05:25 +00001113 if (kUseUniform == programData->fUniLocations.fColorFilterUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001114 GL_CALL_RET(programData->fUniLocations.fColorFilterUni,
1115 GetUniformLocation(progID, COL_FILTER_UNI_NAME));
Scroggo97c88c22011-05-11 14:05:25 +00001116 GrAssert(kUnusedUniform != programData->fUniLocations.fColorFilterUni);
1117 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001118
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001119 if (kUseUniform == programData->fUniLocations.fColorMatrixUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001120 GL_CALL_RET(programData->fUniLocations.fColorMatrixUni,
1121 GetUniformLocation(progID, COL_MATRIX_UNI_NAME));
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001122 }
1123
1124 if (kUseUniform == programData->fUniLocations.fColorMatrixVecUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001125 GL_CALL_RET(programData->fUniLocations.fColorMatrixVecUni,
1126 GetUniformLocation(progID, COL_MATRIX_VEC_UNI_NAME));
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001127 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001128 if (kUseUniform == programData->fUniLocations.fCoverageUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001129 GL_CALL_RET(programData->fUniLocations.fCoverageUni,
1130 GetUniformLocation(progID, COV_UNI_NAME));
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001131 GrAssert(kUnusedUniform != programData->fUniLocations.fCoverageUni);
1132 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001133
tomhudson@google.com93813632011-10-27 20:21:16 +00001134 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001135 StageUniLocations& locations = programData->fUniLocations.fStages[s];
tomhudson@google.com0d831722011-06-02 15:37:14 +00001136 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001137 if (kUseUniform == locations.fTextureMatrixUni) {
1138 GrStringBuilder texMName;
1139 tex_matrix_name(s, &texMName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001140 GL_CALL_RET(locations.fTextureMatrixUni,
1141 GetUniformLocation(progID, texMName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001142 GrAssert(kUnusedUniform != locations.fTextureMatrixUni);
1143 }
1144
1145 if (kUseUniform == locations.fSamplerUni) {
1146 GrStringBuilder samplerName;
1147 sampler_name(s, &samplerName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001148 GL_CALL_RET(locations.fSamplerUni,
1149 GetUniformLocation(progID,samplerName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001150 GrAssert(kUnusedUniform != locations.fSamplerUni);
1151 }
1152
1153 if (kUseUniform == locations.fNormalizedTexelSizeUni) {
1154 GrStringBuilder texelSizeName;
1155 normalized_texel_size_name(s, &texelSizeName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001156 GL_CALL_RET(locations.fNormalizedTexelSizeUni,
1157 GetUniformLocation(progID, texelSizeName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001158 GrAssert(kUnusedUniform != locations.fNormalizedTexelSizeUni);
1159 }
1160
junov@google.com6acc9b32011-05-16 18:32:07 +00001161 if (kUseUniform == locations.fTexDomUni) {
1162 GrStringBuilder texDomName;
1163 tex_domain_name(s, &texDomName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001164 GL_CALL_RET(locations.fTexDomUni,
1165 GetUniformLocation(progID, texDomName.c_str()));
junov@google.com6acc9b32011-05-16 18:32:07 +00001166 GrAssert(kUnusedUniform != locations.fTexDomUni);
1167 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001168
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001169 if (NULL != programData->fCustomStage[s]) {
1170 programData->fCustomStage[s]->
1171 initUniforms(gl.interface(), progID);
1172 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001173 }
1174 }
bsalomon@google.com96399942012-02-13 14:39:16 +00001175 GL_CALL(UseProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001176
1177 // init sampler unis and set bogus values for state tracking
tomhudson@google.com93813632011-10-27 20:21:16 +00001178 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001179 if (kUnusedUniform != programData->fUniLocations.fStages[s].fSamplerUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001180 GL_CALL(Uniform1i(programData->fUniLocations.fStages[s].fSamplerUni, s));
bsalomon@google.com91961302011-05-09 18:39:58 +00001181 }
1182 programData->fTextureMatrices[s] = GrMatrix::InvalidMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +00001183 programData->fTextureWidth[s] = -1;
1184 programData->fTextureHeight[s] = -1;
tomhudson@google.com24878f72012-03-22 14:44:46 +00001185 programData->fTextureDomain[s].setEmpty();
bsalomon@google.com890e3b52012-06-01 19:01:37 +00001186 // this is arbitrary, just initialize to something
1187 programData->fTextureOrientation[s] =
1188 GrGLTexture::kBottomUp_Orientation;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001189 // Must not reset fStageOverride[] here.
bsalomon@google.com91961302011-05-09 18:39:58 +00001190 }
1191 programData->fViewMatrix = GrMatrix::InvalidMatrix();
1192 programData->fColor = GrColor_ILLEGAL;
Scroggo97c88c22011-05-11 14:05:25 +00001193 programData->fColorFilterColor = GrColor_ILLEGAL;
bsalomon@google.com91961302011-05-09 18:39:58 +00001194}
1195
junov@google.comf93e7172011-03-31 21:26:24 +00001196//============================================================================
1197// Stage code generation
1198//============================================================================
1199
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001200namespace {
1201
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001202void gen2x2FS(int stageNum,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +00001203 GrGLShaderBuilder* segments,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001204 GrGLProgram::StageUniLocations* locations,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001205 const char* samplerName,
1206 const char* texelSizeName,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001207 const char* fsOutColor,
tomhudson@google.com52598142012-05-24 17:44:30 +00001208 GrStringBuilder& texFunc) {
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001209 locations->fNormalizedTexelSizeUni = kUseUniform;
tomhudson@google.com040c41a2012-05-18 14:57:40 +00001210 if (segments->fComplexCoord) {
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001211 // assign the coord to a var rather than compute 4x.
1212 GrStringBuilder coordVar("tCoord");
1213 coordVar.appendS32(stageNum);
1214 segments->fFSCode.appendf("\t%s %s = %s;\n",
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001215 float_vector_type_str(segments->fCoordDims),
tomhudson@google.com040c41a2012-05-18 14:57:40 +00001216 coordVar.c_str(), segments->fSampleCoords.c_str());
1217 segments->fSampleCoords = coordVar;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001218 }
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001219 GrAssert(2 == segments->fCoordDims);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001220 GrStringBuilder accumVar("accum");
1221 accumVar.appendS32(stageNum);
tomhudson@google.com52598142012-05-24 17:44:30 +00001222 segments->fFSCode.appendf("\tvec4 %s = %s(%s, %s + vec2(-%s.x,-%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, segments->fSampleCoords.c_str(), texelSizeName, texelSizeName, segments->fSwizzle.c_str());
1223 segments->fFSCode.appendf("\t%s += %s(%s, %s + vec2(+%s.x,-%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, segments->fSampleCoords.c_str(), texelSizeName, texelSizeName, segments->fSwizzle.c_str());
1224 segments->fFSCode.appendf("\t%s += %s(%s, %s + vec2(-%s.x,+%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, segments->fSampleCoords.c_str(), texelSizeName, texelSizeName, segments->fSwizzle.c_str());
1225 segments->fFSCode.appendf("\t%s += %s(%s, %s + vec2(+%s.x,+%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, segments->fSampleCoords.c_str(), texelSizeName, texelSizeName, segments->fSwizzle.c_str());
1226 segments->fFSCode.appendf("\t%s = .25 * %s%s;\n", fsOutColor, accumVar.c_str(), segments->fModulate.c_str());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001227
1228}
1229
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001230}
1231
bsalomon@google.com96399942012-02-13 14:39:16 +00001232void GrGLProgram::genStageCode(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001233 int stageNum,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001234 const GrGLProgram::StageDesc& desc,
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001235 const char* fsInColor, // NULL means no incoming color
1236 const char* fsOutColor,
1237 const char* vsInCoord,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +00001238 GrGLShaderBuilder* segments,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001239 StageUniLocations* locations,
1240 GrGLProgramStage* customStage) const {
junov@google.comf93e7172011-03-31 21:26:24 +00001241
bsalomon@google.com74b98712011-11-11 19:46:16 +00001242 GrAssert(stageNum >= 0 && stageNum <= GrDrawState::kNumStages);
1243 GrAssert((desc.fInConfigFlags & StageDesc::kInConfigBitMask) ==
1244 desc.fInConfigFlags);
junov@google.comf93e7172011-03-31 21:26:24 +00001245
tomhudson@google.com23cb2292012-05-30 18:26:03 +00001246 /// Vertex Shader Stuff
1247
junov@google.comf93e7172011-03-31 21:26:24 +00001248 // decide whether we need a matrix to transform texture coords
1249 // and whether the varying needs a perspective coord.
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001250 const char* matName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001251 if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) {
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001252 segments->fVaryingDims = segments->fCoordDims;
junov@google.comf93e7172011-03-31 21:26:24 +00001253 } else {
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001254 GrStringBuilder texMatName;
1255 tex_matrix_name(stageNum, &texMatName);
1256 const GrGLShaderVar* mat = &segments->addUniform(
1257 GrGLShaderBuilder::kVertex_VariableLifetime, kMat33f_GrSLType,
1258 texMatName.c_str());
1259 // Can't use texMatName.c_str() because it's on the stack!
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001260 matName = mat->getName().c_str();
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001261 locations->fTextureMatrixUni = kUseUniform;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001262
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001263 if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) {
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001264 segments->fVaryingDims = segments->fCoordDims;
junov@google.comf93e7172011-03-31 21:26:24 +00001265 } else {
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001266 segments->fVaryingDims = segments->fCoordDims + 1;
junov@google.comf93e7172011-03-31 21:26:24 +00001267 }
1268 }
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001269 GrAssert(segments->fVaryingDims > 0);
junov@google.comf93e7172011-03-31 21:26:24 +00001270
tomhudson@google.com898e7b52012-06-01 20:42:15 +00001271 // Must setup variables after computing segments->fVaryingDims
1272 if (NULL != customStage) {
1273 customStage->setupVariables(segments, stageNum);
1274 }
1275
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001276 GrStringBuilder samplerName;
1277 sampler_name(stageNum, &samplerName);
1278 const GrGLShaderVar* sampler = &segments->addUniform(
1279 GrGLShaderBuilder::kFragment_VariableLifetime, kSampler2D_GrSLType,
1280 samplerName.c_str());
bsalomon@google.com91961302011-05-09 18:39:58 +00001281 locations->fSamplerUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +00001282
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001283 const char* texelSizeName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001284 if (StageDesc::k2x2_FetchMode == desc.fFetchMode) {
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001285 GrStringBuilder ntsName;
1286 normalized_texel_size_name(stageNum, &ntsName);
1287 texelSizeName = segments->addUniform(
1288 GrGLShaderBuilder::kFragment_VariableLifetime,
1289 kVec2f_GrSLType, ntsName.c_str()).getName().c_str();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001290 }
1291
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001292 const char *varyingVSName, *varyingFSName;
tomhudson@google.com23cb2292012-05-30 18:26:03 +00001293 segments->addVarying(GrSLFloatVectorType(segments->fVaryingDims),
1294 "Stage",
1295 stageNum,
1296 &varyingVSName,
1297 &varyingFSName);
junov@google.comf93e7172011-03-31 21:26:24 +00001298
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001299 if (!matName) {
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001300 GrAssert(segments->fVaryingDims == segments->fCoordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001301 segments->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord);
junov@google.comf93e7172011-03-31 21:26:24 +00001302 } else {
bsalomon@google.com91961302011-05-09 18:39:58 +00001303 // varying = texMatrix * texCoord
1304 segments->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n",
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001305 varyingVSName, matName, vsInCoord,
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001306 vector_all_coords(segments->fVaryingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001307 }
1308
tomhudson@google.comda668982011-12-07 15:06:29 +00001309 GrGLShaderVar* kernel = NULL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001310 const char* imageIncrementName = NULL;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001311 if (NULL != customStage) {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001312 segments->fVSCode.appendf("\t{ // stage %d %s\n",
1313 stageNum, customStage->name());
tomhudson@google.com6a820b62012-05-24 15:10:14 +00001314 customStage->emitVS(segments, varyingVSName);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001315 segments->fVSCode.appendf("\t}\n");
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001316 }
1317
junov@google.comf93e7172011-03-31 21:26:24 +00001318 /// Fragment Shader Stuff
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001319
tomhudson@google.com5440f062012-06-01 15:55:50 +00001320 segments->fSampleCoords = varyingFSName;
1321
1322 GrGLShaderBuilder::SamplerMode sampleMode =
1323 GrGLShaderBuilder::kExplicitDivide_SamplerMode;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001324 if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit |
1325 StageDesc::kNoPerspective_OptFlagBit)) {
tomhudson@google.com5440f062012-06-01 15:55:50 +00001326 sampleMode = GrGLShaderBuilder::kDefault_SamplerMode;
tomhudson@google.com898e7b52012-06-01 20:42:15 +00001327 } else if (NULL == customStage &&
tomhudson@google.com5440f062012-06-01 15:55:50 +00001328 StageDesc::kSingle_FetchMode == desc.fFetchMode) {
1329 sampleMode = GrGLShaderBuilder::kProj_SamplerMode;
junov@google.comf93e7172011-03-31 21:26:24 +00001330 }
tomhudson@google.com5440f062012-06-01 15:55:50 +00001331 segments->setupTextureAccess(sampleMode, stageNum);
junov@google.comf93e7172011-03-31 21:26:24 +00001332
tomhudson@google.com898e7b52012-06-01 20:42:15 +00001333 segments->computeSwizzle(desc.fInConfigFlags);
1334 segments->computeModulate(fsInColor);
junov@google.comf93e7172011-03-31 21:26:24 +00001335
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001336 static const uint32_t kMulByAlphaMask =
1337 (StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
1338 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag);
1339
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001340 if (desc.fOptFlags & StageDesc::kCustomTextureDomain_OptFlagBit) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001341 GrStringBuilder texDomainName;
1342 tex_domain_name(stageNum, &texDomainName);
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001343 const GrGLShaderVar* texDomain =
1344 &segments->addUniform(
1345 GrGLShaderBuilder::kFragment_VariableLifetime,
1346 kVec4f_GrSLType, texDomainName.c_str());
junov@google.com6acc9b32011-05-16 18:32:07 +00001347 GrStringBuilder coordVar("clampCoord");
1348 segments->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n",
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001349 float_vector_type_str(segments->fCoordDims),
junov@google.com6acc9b32011-05-16 18:32:07 +00001350 coordVar.c_str(),
tomhudson@google.com040c41a2012-05-18 14:57:40 +00001351 segments->fSampleCoords.c_str(),
junov@google.com6acc9b32011-05-16 18:32:07 +00001352 texDomainName.c_str(),
1353 texDomainName.c_str());
tomhudson@google.com040c41a2012-05-18 14:57:40 +00001354 segments->fSampleCoords = coordVar;
junov@google.com6acc9b32011-05-16 18:32:07 +00001355 locations->fTexDomUni = kUseUniform;
1356 }
1357
tomhudson@google.com6a820b62012-05-24 15:10:14 +00001358 // NOTE: GrGLProgramStages are now responsible for fetching
1359 if (NULL == customStage) {
1360 switch (desc.fFetchMode) {
1361 case StageDesc::k2x2_FetchMode:
1362 GrAssert(!(desc.fInConfigFlags & kMulByAlphaMask));
1363 gen2x2FS(stageNum, segments, locations,
tomhudson@google.com5440f062012-06-01 15:55:50 +00001364 samplerName.c_str(), texelSizeName, fsOutColor,
1365 segments->fTexFunc);
tomhudson@google.com6a820b62012-05-24 15:10:14 +00001366 break;
tomhudson@google.com6a820b62012-05-24 15:10:14 +00001367 default:
1368 if (desc.fInConfigFlags & kMulByAlphaMask) {
1369 // only one of the mul by alpha flags should be set
1370 GrAssert(GrIsPow2(kMulByAlphaMask & desc.fInConfigFlags));
1371 GrAssert(!(desc.fInConfigFlags &
1372 StageDesc::kSmearAlpha_InConfigFlag));
1373 GrAssert(!(desc.fInConfigFlags &
1374 StageDesc::kSmearRed_InConfigFlag));
1375 segments->fFSCode.appendf("\t%s = %s(%s, %s)%s;\n",
tomhudson@google.com5440f062012-06-01 15:55:50 +00001376 fsOutColor,
1377 segments->fTexFunc.c_str(),
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001378 samplerName.c_str(),
tomhudson@google.com6a820b62012-05-24 15:10:14 +00001379 segments->fSampleCoords.c_str(),
tomhudson@google.com52598142012-05-24 17:44:30 +00001380 segments->fSwizzle.c_str());
tomhudson@google.com6a820b62012-05-24 15:10:14 +00001381 if (desc.fInConfigFlags &
1382 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag) {
1383 segments->fFSCode.appendf("\t%s = vec4(ceil(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
1384 fsOutColor, fsOutColor, fsOutColor,
tomhudson@google.com52598142012-05-24 17:44:30 +00001385 fsOutColor, segments->fModulate.c_str());
tomhudson@google.com6a820b62012-05-24 15:10:14 +00001386 } else {
1387 segments->fFSCode.appendf("\t%s = vec4(floor(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
1388 fsOutColor, fsOutColor, fsOutColor,
tomhudson@google.com52598142012-05-24 17:44:30 +00001389 fsOutColor, segments->fModulate.c_str());
tomhudson@google.com6a820b62012-05-24 15:10:14 +00001390 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001391 } else {
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001392 segments->emitDefaultFetch(fsOutColor, samplerName.c_str());
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001393 }
bsalomon@google.com74b98712011-11-11 19:46:16 +00001394 }
junov@google.comf93e7172011-03-31 21:26:24 +00001395 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001396
1397 if (NULL != customStage) {
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001398 // Enclose custom code in a block to avoid namespace conflicts
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001399 segments->fFSCode.appendf("\t{ // stage %d %s \n",
1400 stageNum, customStage->name());
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001401 customStage->emitFS(segments, fsOutColor, fsInColor,
1402 samplerName.c_str());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001403 segments->fFSCode.appendf("\t}\n");
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001404 }
junov@google.comf93e7172011-03-31 21:26:24 +00001405}
tomhudson@google.com5960d002011-10-19 20:21:48 +00001406
tomhudson@google.com5960d002011-10-19 20:21:48 +00001407