blob: 55455604a015fd3171542c9e055d762613df84d6 [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 {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000046inline void tex_attr_name(int coordIdx, SkString* 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.comf0a104e2012-07-10 17:51:07 +000078inline void tex_matrix_name(int stage, SkString* 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.comf0a104e2012-07-10 17:51:07 +000083inline void sampler_name(int stage, SkString* 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.comf0a104e2012-07-10 17:51:07 +000088inline void tex_domain_name(int stage, SkString* 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,
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000126 SkString* code) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000127 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,
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000154 SkString* code) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000155 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 */
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000218static void blendTermString(SkString* str, SkXfermode::Coeff coeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000219 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.comf0a104e2012-07-10 17:51:07 +0000261static void addColorFilter(SkString* 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) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000265 SkString colorStr, constStr;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000266 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 */
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000277static void addColorMatrix(SkString* fsCode, const char * outputVar,
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000278 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,
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000286 SkString* 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,
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000353 SkString* 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:
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000365 segments->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000366 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.comf0a104e2012-07-10 17:51:07 +0000382 SkString* 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,
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000400 SkString* inOutCoverage) {
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000401 segments->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
tomhudson@google.com242ed6f2012-05-30 17:38:57 +0000402 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
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000443const char* GrGLProgram::adjustInColor(const SkString& 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.comad5e9372012-07-11 18:11:27 +0000463namespace {
464#define GL_CALL(X) GR_GL_CALL(gl.interface(), X)
465#define GL_CALL_RET(R, X) GR_GL_CALL_RET(gl.interface(), R, X)
466
467// prints a shader using params similar to glShaderSource
468void print_shader(GrGLint stringCnt,
469 const GrGLchar** strings,
470 GrGLint* stringLengths) {
471 for (int i = 0; i < stringCnt; ++i) {
472 if (NULL == stringLengths || stringLengths[i] < 0) {
473 GrPrintf(strings[i]);
474 } else {
475 GrPrintf("%.*s", stringLengths[i], strings[i]);
476 }
477 }
478}
479
480// Compiles a GL shader, returns shader ID or 0 if failed params have same meaning as glShaderSource
481GrGLuint compile_shader(const GrGLContextInfo& gl,
482 GrGLenum type,
483 int stringCnt,
484 const char** strings,
485 int* stringLengths) {
486 SK_TRACE_EVENT1("GrGLProgram::CompileShader",
487 "stringCount", SkStringPrintf("%i", stringCnt).c_str());
488
489 GrGLuint shader;
490 GL_CALL_RET(shader, CreateShader(type));
491 if (0 == shader) {
492 return 0;
493 }
494
495 GrGLint compiled = GR_GL_INIT_ZERO;
496 GL_CALL(ShaderSource(shader, stringCnt, strings, stringLengths));
497 GL_CALL(CompileShader(shader));
498 GL_CALL(GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled));
499
500 if (!compiled) {
501 GrGLint infoLen = GR_GL_INIT_ZERO;
502 GL_CALL(GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen));
503 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
504 if (infoLen > 0) {
505 // retrieve length even though we don't need it to workaround bug in chrome cmd buffer
506 // param validation.
507 GrGLsizei length = GR_GL_INIT_ZERO;
508 GL_CALL(GetShaderInfoLog(shader, infoLen+1,
509 &length, (char*)log.get()));
510 print_shader(stringCnt, strings, stringLengths);
511 GrPrintf("\n%s", log.get());
512 }
513 GrAssert(!"Shader compilation failed!");
514 GL_CALL(DeleteShader(shader));
515 return 0;
516 }
517 return shader;
518}
519
520// helper version of above for when shader is already flattened into a single SkString
521GrGLuint compile_shader(const GrGLContextInfo& gl, GrGLenum type, const SkString& shader) {
522 const GrGLchar* str = shader.c_str();
523 int length = shader.size();
524 return compile_shader(gl, type, 1, &str, &length);
525}
526
527// compiles all the shaders from builder and stores the shader IDs in programData.
528bool compile_shaders(const GrGLContextInfo& gl,
529 const GrGLShaderBuilder& builder,
530 GrGLProgram::CachedData* programData) {
531
532 SkString shader;
533
534 builder.getShader(GrGLShaderBuilder::kVertex_ShaderType, &shader);
535#if PRINT_SHADERS
536 GrPrintf(shader.c_str());
537 GrPrintf("\n");
538#endif
539 if (!(programData->fVShaderID = compile_shader(gl, GR_GL_VERTEX_SHADER, shader))) {
540 return false;
541 }
542
543 if (builder.fUsesGS) {
544 builder.getShader(GrGLShaderBuilder::kGeometry_ShaderType, &shader);
545#if PRINT_SHADERS
546 GrPrintf(shader.c_str());
547 GrPrintf("\n");
548#endif
549 if (!(programData->fVShaderID = compile_shader(gl, GR_GL_GEOMETRY_SHADER, shader))) {
550 return false;
551 }
552 } else {
553 programData->fGShaderID = 0;
554 }
555
556 builder.getShader(GrGLShaderBuilder::kFragment_ShaderType, &shader);
557#if PRINT_SHADERS
558 GrPrintf(shader.c_str());
559 GrPrintf("\n");
560#endif
561 if (!(programData->fFShaderID = compile_shader(gl, GR_GL_FRAGMENT_SHADER, shader))) {
562 return false;
563 }
564
565 return true;
566}
567
568}
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000569
bsalomon@google.com96399942012-02-13 14:39:16 +0000570bool GrGLProgram::genProgram(const GrGLContextInfo& gl,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000571 GrCustomStage** customStages,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000572 GrGLProgram::CachedData* programData) const {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000573 GrGLShaderBuilder builder(gl);
junov@google.comf93e7172011-03-31 21:26:24 +0000574 const uint32_t& layout = fProgramDesc.fVertexLayout;
575
bsalomon@google.com91961302011-05-09 18:39:58 +0000576 programData->fUniLocations.reset();
junov@google.comf93e7172011-03-31 21:26:24 +0000577
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000578#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000579 builder.fUsesGS = fProgramDesc.fExperimentalGS;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000580#endif
581
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000582 SkXfermode::Coeff colorCoeff, uniformCoeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000583 bool applyColorMatrix = SkToBool(fProgramDesc.fColorMatrixEnabled);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000584 // The rest of transfer mode color filters have not been implemented
585 if (fProgramDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) {
586 GR_DEBUGCODE(bool success =)
tomhudson@google.com0d831722011-06-02 15:37:14 +0000587 SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode>
588 (fProgramDesc.fColorFilterXfermode),
589 &uniformCoeff, &colorCoeff);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000590 GR_DEBUGASSERT(success);
591 } else {
592 colorCoeff = SkXfermode::kOne_Coeff;
593 uniformCoeff = SkXfermode::kZero_Coeff;
594 }
595
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000596 // no need to do the color filter / matrix at all if coverage is 0. The
597 // output color is scaled by the coverage. All the dual source outputs are
598 // scaled by the coverage as well.
599 if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fCoverageInput) {
600 colorCoeff = SkXfermode::kZero_Coeff;
601 uniformCoeff = SkXfermode::kZero_Coeff;
602 applyColorMatrix = false;
603 }
604
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000605 // If we know the final color is going to be all zeros then we can
606 // simplify the color filter coeffecients. needComputedColor will then
607 // come out false below.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000608 if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fColorInput) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000609 colorCoeff = SkXfermode::kZero_Coeff;
610 if (SkXfermode::kDC_Coeff == uniformCoeff ||
611 SkXfermode::kDA_Coeff == uniformCoeff) {
612 uniformCoeff = SkXfermode::kZero_Coeff;
613 } else if (SkXfermode::kIDC_Coeff == uniformCoeff ||
614 SkXfermode::kIDA_Coeff == uniformCoeff) {
615 uniformCoeff = SkXfermode::kOne_Coeff;
616 }
617 }
618
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000619 bool needColorFilterUniform;
620 bool needComputedColor;
621 needBlendInputs(uniformCoeff, colorCoeff,
622 &needColorFilterUniform, &needComputedColor);
623
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000624 // the dual source output has no canonical var name, have to
625 // declare an output, which is incompatible with gl_FragColor/gl_FragData.
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000626 bool dualSourceOutputWritten = false;
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000627 builder.fHeader.append(GrGetGLSLVersionDecl(gl.binding(),
628 gl.glslGeneration()));
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000629
630 GrGLShaderVar colorOutput;
bsalomon@google.com96399942012-02-13 14:39:16 +0000631 bool isColorDeclared = GrGLSLSetupFSColorOuput(gl.glslGeneration(),
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000632 declared_color_output_name(),
633 &colorOutput);
634 if (isColorDeclared) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000635 builder.fFSOutputs.push_back(colorOutput);
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000636 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000637
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000638 builder.addUniform(GrGLShaderBuilder::kVertex_ShaderType,
639 kMat33f_GrSLType, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000640 programData->fUniLocations.fViewMatrixUni = kUseUniform;
bsalomon@google.com341767c2012-05-11 20:47:39 +0000641
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000642 builder.fVSAttrs.push_back().set(kVec2f_GrSLType,
643 GrGLShaderVar::kAttribute_TypeModifier,
644 POS_ATTR_NAME);
junov@google.comf93e7172011-03-31 21:26:24 +0000645
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000646 builder.fVSCode.append("void main() {\n"
647 "\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3("POS_ATTR_NAME", 1);\n"
648 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n");
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000649
bsalomon@google.com91961302011-05-09 18:39:58 +0000650 // incoming color to current stage being processed.
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000651 SkString inColor;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000652
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000653 if (needComputedColor) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000654 genInputColor((ProgramDesc::ColorInput) fProgramDesc.fColorInput,
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000655 programData, &builder, &inColor);
junov@google.comf93e7172011-03-31 21:26:24 +0000656 }
657
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000658 // we output point size in the GS if present
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000659 if (fProgramDesc.fEmitsPointSize && !builder.fUsesGS){
660 builder.fVSCode.append("\tgl_PointSize = 1.0;\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000661 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000662
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000663 builder.fFSCode.append("void main() {\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000664
665 // add texture coordinates that are used to the list of vertex attr decls
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000666 SkString texCoordAttrs[GrDrawState::kMaxTexCoords];
tomhudson@google.com93813632011-10-27 20:21:16 +0000667 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000668 if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000669 tex_attr_name(t, texCoordAttrs + t);
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000670 builder.fVSAttrs.push_back().set(kVec2f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000671 GrGLShaderVar::kAttribute_TypeModifier,
672 texCoordAttrs[t].c_str());
junov@google.comf93e7172011-03-31 21:26:24 +0000673 }
674 }
675
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000676 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000677 // We need to convert generic effect representations to GL-specific
678 // backends so they can be accesseed in genStageCode() and in subsequent,
679 // uses of programData, but it's safest to do so below when we're *sure*
680 // we need them.
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000681 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000682 programData->fCustomStage[s] = NULL;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000683 }
684
685 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000686 // compute the final color
Scroggo97c88c22011-05-11 14:05:25 +0000687
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000688 // if we have color stages string them together, feeding the output color
689 // of each to the next and generating code for each stage.
690 if (needComputedColor) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000691 SkString outColor;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000692 for (int s = 0; s < fProgramDesc.fFirstCoverageStage; ++s) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000693 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000694 // create var to hold stage result
695 outColor = "color";
696 outColor.appendS32(s);
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000697 builder.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000698
699 const char* inCoords;
700 // figure out what our input coords are
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000701 int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout);
702 if (tcIdx < 0) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000703 inCoords = POS_ATTR_NAME;
Scroggo97c88c22011-05-11 14:05:25 +0000704 } else {
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000705 // must have input tex coordinates if stage is enabled.
Scroggo97c88c22011-05-11 14:05:25 +0000706 GrAssert(texCoordAttrs[tcIdx].size());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000707 inCoords = texCoordAttrs[tcIdx].c_str();
junov@google.comf93e7172011-03-31 21:26:24 +0000708 }
709
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000710 if (NULL != customStages[s]) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000711 const GrProgramStageFactory& factory =
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000712 customStages[s]->getFactory();
713 programData->fCustomStage[s] =
bsalomon@google.comb505a122012-05-31 18:40:36 +0000714 factory.createGLInstance(*customStages[s]);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000715 }
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000716 this->genStageCode(gl,
717 s,
718 fProgramDesc.fStages[s],
719 inColor.size() ? inColor.c_str() : NULL,
720 outColor.c_str(),
721 inCoords,
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000722 &builder,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000723 &programData->fUniLocations.fStages[s],
724 programData->fCustomStage[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000725 inColor = outColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000726 }
727 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000728 }
Scroggo97c88c22011-05-11 14:05:25 +0000729
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000730 // if have all ones or zeros for the "dst" input to the color filter then we
731 // may be able to make additional optimizations.
732 if (needColorFilterUniform && needComputedColor && !inColor.size()) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000733 GrAssert(ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000734 bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff ||
735 SkXfermode::kIDA_Coeff == uniformCoeff;
736 if (uniformCoeffIsZero) {
737 uniformCoeff = SkXfermode::kZero_Coeff;
738 bool bogus;
739 needBlendInputs(SkXfermode::kZero_Coeff, colorCoeff,
740 &needColorFilterUniform, &bogus);
741 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000742 }
743 if (needColorFilterUniform) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000744 builder.addUniform(GrGLShaderBuilder::kFragment_ShaderType,
745 kVec4f_GrSLType, COL_FILTER_UNI_NAME);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000746 programData->fUniLocations.fColorFilterUni = kUseUniform;
747 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000748 bool wroteFragColorZero = false;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000749 if (SkXfermode::kZero_Coeff == uniformCoeff &&
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000750 SkXfermode::kZero_Coeff == colorCoeff &&
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000751 !applyColorMatrix) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000752 builder.fFSCode.appendf("\t%s = %s;\n",
753 colorOutput.getName().c_str(),
754 all_zeros_vec(4));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000755 wroteFragColorZero = true;
756 } else if (SkXfermode::kDst_Mode != fProgramDesc.fColorFilterXfermode) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000757 builder.fFSCode.append("\tvec4 filteredColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000758 const char* color = adjustInColor(inColor);
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000759 addColorFilter(&builder.fFSCode, "filteredColor", uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000760 colorCoeff, color);
761 inColor = "filteredColor";
762 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000763 if (applyColorMatrix) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000764 builder.addUniform(GrGLShaderBuilder::kFragment_ShaderType,
765 kMat44f_GrSLType, COL_MATRIX_UNI_NAME);
766 builder.addUniform(GrGLShaderBuilder::kFragment_ShaderType,
767 kVec4f_GrSLType, COL_MATRIX_VEC_UNI_NAME);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000768 programData->fUniLocations.fColorMatrixUni = kUseUniform;
769 programData->fUniLocations.fColorMatrixVecUni = kUseUniform;
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000770 builder.fFSCode.append("\tvec4 matrixedColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000771 const char* color = adjustInColor(inColor);
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000772 addColorMatrix(&builder.fFSCode, "matrixedColor", color);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000773 inColor = "matrixedColor";
774 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000775
776 ///////////////////////////////////////////////////////////////////////////
777 // compute the partial coverage (coverage stages and edge aa)
778
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000779 SkString inCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000780 bool coverageIsZero = ProgramDesc::kTransBlack_ColorInput ==
781 fProgramDesc.fCoverageInput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000782 // we don't need to compute coverage at all if we know the final shader
783 // output will be zero and we don't have a dual src blend output.
784 if (!wroteFragColorZero ||
785 ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000786
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000787 if (!coverageIsZero) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000788 this->genEdgeCoverage(gl, layout, programData, &inCoverage, &builder);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000789
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000790 switch (fProgramDesc.fCoverageInput) {
791 case ProgramDesc::kSolidWhite_ColorInput:
792 // empty string implies solid white
793 break;
794 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000795 genAttributeCoverage(&builder, &inCoverage);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000796 break;
797 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000798 genUniformCoverage(&builder, programData, &inCoverage);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000799 break;
800 default:
801 GrCrash("Unexpected input coverage.");
802 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000803
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000804 SkString outCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000805 const int& startStage = fProgramDesc.fFirstCoverageStage;
806 for (int s = startStage; s < GrDrawState::kNumStages; ++s) {
807 if (fProgramDesc.fStages[s].isEnabled()) {
808 // create var to hold stage output
809 outCoverage = "coverage";
810 outCoverage.appendS32(s);
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000811 builder.fFSCode.appendf("\tvec4 %s;\n",
812 outCoverage.c_str());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000813
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000814 const char* inCoords;
815 // figure out what our input coords are
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000816 int tcIdx =
817 GrDrawTarget::VertexTexCoordsForStage(s, layout);
818 if (tcIdx < 0) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000819 inCoords = POS_ATTR_NAME;
820 } else {
tomhudson@google.comb213ed82012-06-25 15:22:12 +0000821 // must have input tex coordinates if stage is
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000822 // enabled.
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000823 GrAssert(texCoordAttrs[tcIdx].size());
824 inCoords = texCoordAttrs[tcIdx].c_str();
825 }
826
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000827 if (NULL != customStages[s]) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000828 const GrProgramStageFactory& factory =
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000829 customStages[s]->getFactory();
830 programData->fCustomStage[s] =
bsalomon@google.comb505a122012-05-31 18:40:36 +0000831 factory.createGLInstance(*customStages[s]);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000832 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000833 this->genStageCode(gl, s,
834 fProgramDesc.fStages[s],
835 inCoverage.size() ? inCoverage.c_str() : NULL,
836 outCoverage.c_str(),
837 inCoords,
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000838 &builder,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000839 &programData->fUniLocations.fStages[s],
840 programData->fCustomStage[s]);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000841 inCoverage = outCoverage;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000842 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000843 }
844 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000845 if (ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000846 builder.fFSOutputs.push_back().set(kVec4f_GrSLType,
847 GrGLShaderVar::kOut_TypeModifier,
848 dual_source_output_name());
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000849 bool outputIsZero = coverageIsZero;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000850 SkString coeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000851 if (!outputIsZero &&
852 ProgramDesc::kCoverage_DualSrcOutput !=
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000853 fProgramDesc.fDualSrcOutput && !wroteFragColorZero) {
854 if (!inColor.size()) {
855 outputIsZero = true;
856 } else {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000857 if (fProgramDesc.fDualSrcOutput ==
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000858 ProgramDesc::kCoverageISA_DualSrcOutput) {
859 coeff.printf("(1 - %s.a)", inColor.c_str());
860 } else {
861 coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str());
862 }
863 }
864 }
865 if (outputIsZero) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000866 builder.fFSCode.appendf("\t%s = %s;\n",
867 dual_source_output_name(),
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000868 all_zeros_vec(4));
869 } else {
870 modulate_helper(dual_source_output_name(),
871 coeff.c_str(),
872 inCoverage.c_str(),
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000873 &builder.fFSCode);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000874 }
875 dualSourceOutputWritten = true;
876 }
junov@google.comf93e7172011-03-31 21:26:24 +0000877 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000878
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000879 ///////////////////////////////////////////////////////////////////////////
880 // combine color and coverage as frag color
881
882 if (!wroteFragColorZero) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000883 if (coverageIsZero) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000884 builder.fFSCode.appendf("\t%s = %s;\n",
885 colorOutput.getName().c_str(),
886 all_zeros_vec(4));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000887 } else {
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000888 modulate_helper(colorOutput.getName().c_str(),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000889 inColor.c_str(),
890 inCoverage.c_str(),
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000891 &builder.fFSCode);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000892 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000893 if (ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig ==
894 fProgramDesc.fOutputConfig) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000895 builder.fFSCode.appendf("\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.rgb / %s.a * 255.0)/255.0, %s.a);\n",
896 colorOutput.getName().c_str(),
897 colorOutput.getName().c_str(),
898 colorOutput.getName().c_str(),
899 colorOutput.getName().c_str(),
900 colorOutput.getName().c_str());
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000901 } else if (ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig ==
902 fProgramDesc.fOutputConfig) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000903 builder.fFSCode.appendf("\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.rgb / %s.a * 255.0)/255.0, %s.a);\n",
904 colorOutput.getName().c_str(),
905 colorOutput.getName().c_str(),
906 colorOutput.getName().c_str(),
907 colorOutput.getName().c_str(),
908 colorOutput.getName().c_str());
bsalomon@google.comc4364992011-11-07 15:54:49 +0000909 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000910 }
911
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000912 builder.fVSCode.append("}\n");
913 builder.fFSCode.append("}\n");
bsalomon@google.com91961302011-05-09 18:39:58 +0000914
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000915 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000916 // insert GS
917#if GR_DEBUG
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000918 this->genGeometryShader(gl, &builder);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000919#endif
920
921 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000922 // compile and setup attribs and unis
923
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000924 if (!compile_shaders(gl, builder, programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000925 return false;
926 }
927
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000928 if (!this->bindOutputsAttribsAndLinkProgram(gl, texCoordAttrs,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000929 isColorDeclared,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000930 dualSourceOutputWritten,
931 programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000932 return false;
933 }
934
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000935 this->getUniformLocationsAndInitCache(gl, programData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000936
937 return true;
938}
939
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000940bool GrGLProgram::bindOutputsAttribsAndLinkProgram(const GrGLContextInfo& gl,
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000941 SkString texCoordAttrNames[],
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000942 bool bindColorOut,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000943 bool bindDualSrcOut,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000944 CachedData* programData) const {
bsalomon@google.com96399942012-02-13 14:39:16 +0000945 GL_CALL_RET(programData->fProgramID, CreateProgram());
bsalomon@google.com91961302011-05-09 18:39:58 +0000946 if (!programData->fProgramID) {
947 return false;
948 }
949 const GrGLint& progID = programData->fProgramID;
950
bsalomon@google.com96399942012-02-13 14:39:16 +0000951 GL_CALL(AttachShader(progID, programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000952 if (programData->fGShaderID) {
bsalomon@google.com96399942012-02-13 14:39:16 +0000953 GL_CALL(AttachShader(progID, programData->fGShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000954 }
bsalomon@google.com96399942012-02-13 14:39:16 +0000955 GL_CALL(AttachShader(progID, programData->fFShaderID));
bsalomon@google.com91961302011-05-09 18:39:58 +0000956
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000957 if (bindColorOut) {
bsalomon@google.com96399942012-02-13 14:39:16 +0000958 GL_CALL(BindFragDataLocation(programData->fProgramID,
959 0, declared_color_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000960 }
961 if (bindDualSrcOut) {
bsalomon@google.com96399942012-02-13 14:39:16 +0000962 GL_CALL(BindFragDataLocationIndexed(programData->fProgramID,
963 0, 1, dual_source_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000964 }
965
bsalomon@google.com91961302011-05-09 18:39:58 +0000966 // Bind the attrib locations to same values for all shaders
bsalomon@google.com96399942012-02-13 14:39:16 +0000967 GL_CALL(BindAttribLocation(progID, PositionAttributeIdx(), POS_ATTR_NAME));
tomhudson@google.com93813632011-10-27 20:21:16 +0000968 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000969 if (texCoordAttrNames[t].size()) {
bsalomon@google.com96399942012-02-13 14:39:16 +0000970 GL_CALL(BindAttribLocation(progID,
971 TexCoordAttributeIdx(t),
972 texCoordAttrNames[t].c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +0000973 }
974 }
975
bsalomon@google.com96399942012-02-13 14:39:16 +0000976 GL_CALL(BindAttribLocation(progID, ColorAttributeIdx(), COL_ATTR_NAME));
977 GL_CALL(BindAttribLocation(progID, CoverageAttributeIdx(), COV_ATTR_NAME));
978 GL_CALL(BindAttribLocation(progID, EdgeAttributeIdx(), EDGE_ATTR_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +0000979
bsalomon@google.com96399942012-02-13 14:39:16 +0000980 GL_CALL(LinkProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +0000981
982 GrGLint linked = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +0000983 GL_CALL(GetProgramiv(progID, GR_GL_LINK_STATUS, &linked));
bsalomon@google.com91961302011-05-09 18:39:58 +0000984 if (!linked) {
985 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +0000986 GL_CALL(GetProgramiv(progID, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000987 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
bsalomon@google.com91961302011-05-09 18:39:58 +0000988 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +0000989 // retrieve length even though we don't need it to workaround
990 // bug in chrome cmd buffer param validation.
991 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +0000992 GL_CALL(GetProgramInfoLog(progID,
993 infoLen+1,
994 &length,
995 (char*)log.get()));
bsalomon@google.com91961302011-05-09 18:39:58 +0000996 GrPrintf((char*)log.get());
997 }
998 GrAssert(!"Error linking program");
bsalomon@google.com96399942012-02-13 14:39:16 +0000999 GL_CALL(DeleteProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001000 programData->fProgramID = 0;
1001 return false;
1002 }
1003 return true;
1004}
1005
bsalomon@google.com96399942012-02-13 14:39:16 +00001006void GrGLProgram::getUniformLocationsAndInitCache(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001007 CachedData* programData) const {
bsalomon@google.com91961302011-05-09 18:39:58 +00001008 const GrGLint& progID = programData->fProgramID;
1009
1010 if (kUseUniform == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001011 GL_CALL_RET(programData->fUniLocations.fViewMatrixUni,
1012 GetUniformLocation(progID, VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001013 GrAssert(kUnusedUniform != programData->fUniLocations.fViewMatrixUni);
1014 }
1015 if (kUseUniform == programData->fUniLocations.fColorUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001016 GL_CALL_RET(programData->fUniLocations.fColorUni,
1017 GetUniformLocation(progID, COL_UNI_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001018 GrAssert(kUnusedUniform != programData->fUniLocations.fColorUni);
1019 }
Scroggo97c88c22011-05-11 14:05:25 +00001020 if (kUseUniform == programData->fUniLocations.fColorFilterUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001021 GL_CALL_RET(programData->fUniLocations.fColorFilterUni,
1022 GetUniformLocation(progID, COL_FILTER_UNI_NAME));
Scroggo97c88c22011-05-11 14:05:25 +00001023 GrAssert(kUnusedUniform != programData->fUniLocations.fColorFilterUni);
1024 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001025
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001026 if (kUseUniform == programData->fUniLocations.fColorMatrixUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001027 GL_CALL_RET(programData->fUniLocations.fColorMatrixUni,
1028 GetUniformLocation(progID, COL_MATRIX_UNI_NAME));
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001029 }
1030
1031 if (kUseUniform == programData->fUniLocations.fColorMatrixVecUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001032 GL_CALL_RET(programData->fUniLocations.fColorMatrixVecUni,
1033 GetUniformLocation(progID, COL_MATRIX_VEC_UNI_NAME));
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001034 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001035 if (kUseUniform == programData->fUniLocations.fCoverageUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001036 GL_CALL_RET(programData->fUniLocations.fCoverageUni,
1037 GetUniformLocation(progID, COV_UNI_NAME));
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001038 GrAssert(kUnusedUniform != programData->fUniLocations.fCoverageUni);
1039 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001040
tomhudson@google.com93813632011-10-27 20:21:16 +00001041 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001042 StageUniLocations& locations = programData->fUniLocations.fStages[s];
tomhudson@google.com0d831722011-06-02 15:37:14 +00001043 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001044 if (kUseUniform == locations.fTextureMatrixUni) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +00001045 SkString texMName;
bsalomon@google.com91961302011-05-09 18:39:58 +00001046 tex_matrix_name(s, &texMName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001047 GL_CALL_RET(locations.fTextureMatrixUni,
1048 GetUniformLocation(progID, texMName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001049 GrAssert(kUnusedUniform != locations.fTextureMatrixUni);
1050 }
1051
1052 if (kUseUniform == locations.fSamplerUni) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +00001053 SkString samplerName;
bsalomon@google.com91961302011-05-09 18:39:58 +00001054 sampler_name(s, &samplerName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001055 GL_CALL_RET(locations.fSamplerUni,
1056 GetUniformLocation(progID,samplerName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001057 GrAssert(kUnusedUniform != locations.fSamplerUni);
1058 }
1059
junov@google.com6acc9b32011-05-16 18:32:07 +00001060 if (kUseUniform == locations.fTexDomUni) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +00001061 SkString texDomName;
junov@google.com6acc9b32011-05-16 18:32:07 +00001062 tex_domain_name(s, &texDomName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001063 GL_CALL_RET(locations.fTexDomUni,
1064 GetUniformLocation(progID, texDomName.c_str()));
junov@google.com6acc9b32011-05-16 18:32:07 +00001065 GrAssert(kUnusedUniform != locations.fTexDomUni);
1066 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001067
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001068 if (NULL != programData->fCustomStage[s]) {
1069 programData->fCustomStage[s]->
1070 initUniforms(gl.interface(), progID);
1071 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001072 }
1073 }
bsalomon@google.com96399942012-02-13 14:39:16 +00001074 GL_CALL(UseProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001075
1076 // init sampler unis and set bogus values for state tracking
tomhudson@google.com93813632011-10-27 20:21:16 +00001077 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001078 if (kUnusedUniform != programData->fUniLocations.fStages[s].fSamplerUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001079 GL_CALL(Uniform1i(programData->fUniLocations.fStages[s].fSamplerUni, s));
bsalomon@google.com91961302011-05-09 18:39:58 +00001080 }
1081 programData->fTextureMatrices[s] = GrMatrix::InvalidMatrix();
tomhudson@google.com24878f72012-03-22 14:44:46 +00001082 programData->fTextureDomain[s].setEmpty();
bsalomon@google.com890e3b52012-06-01 19:01:37 +00001083 // this is arbitrary, just initialize to something
1084 programData->fTextureOrientation[s] =
1085 GrGLTexture::kBottomUp_Orientation;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001086 // Must not reset fStageOverride[] here.
bsalomon@google.com91961302011-05-09 18:39:58 +00001087 }
1088 programData->fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com4c883782012-06-04 19:05:11 +00001089 programData->fViewportSize.set(-1, -1);
bsalomon@google.com91961302011-05-09 18:39:58 +00001090 programData->fColor = GrColor_ILLEGAL;
Scroggo97c88c22011-05-11 14:05:25 +00001091 programData->fColorFilterColor = GrColor_ILLEGAL;
bsalomon@google.com91961302011-05-09 18:39:58 +00001092}
1093
bsalomon@google.comeb715c82012-07-11 15:03:31 +00001094///////////////////////////////////////////////////////////////////////////////
junov@google.comf93e7172011-03-31 21:26:24 +00001095// Stage code generation
junov@google.comf93e7172011-03-31 21:26:24 +00001096
bsalomon@google.com96399942012-02-13 14:39:16 +00001097void GrGLProgram::genStageCode(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001098 int stageNum,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001099 const GrGLProgram::StageDesc& desc,
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001100 const char* fsInColor, // NULL means no incoming color
1101 const char* fsOutColor,
1102 const char* vsInCoord,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +00001103 GrGLShaderBuilder* segments,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001104 StageUniLocations* locations,
1105 GrGLProgramStage* customStage) const {
junov@google.comf93e7172011-03-31 21:26:24 +00001106
bsalomon@google.com74b98712011-11-11 19:46:16 +00001107 GrAssert(stageNum >= 0 && stageNum <= GrDrawState::kNumStages);
1108 GrAssert((desc.fInConfigFlags & StageDesc::kInConfigBitMask) ==
1109 desc.fInConfigFlags);
junov@google.comf93e7172011-03-31 21:26:24 +00001110
tomhudson@google.com23cb2292012-05-30 18:26:03 +00001111 /// Vertex Shader Stuff
1112
bsalomon@google.comeb715c82012-07-11 15:03:31 +00001113 // decide whether we need a matrix to transform texture coords and whether the varying needs a
1114 // perspective coord.
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001115 const char* matName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001116 if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) {
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001117 segments->fVaryingDims = segments->fCoordDims;
junov@google.comf93e7172011-03-31 21:26:24 +00001118 } else {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +00001119 SkString texMatName;
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001120 tex_matrix_name(stageNum, &texMatName);
bsalomon@google.comeb715c82012-07-11 15:03:31 +00001121 const GrGLShaderVar* mat = &segments->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
1122 kMat33f_GrSLType, texMatName.c_str());
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001123 // Can't use texMatName.c_str() because it's on the stack!
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001124 matName = mat->getName().c_str();
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001125 locations->fTextureMatrixUni = kUseUniform;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001126
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001127 if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) {
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001128 segments->fVaryingDims = segments->fCoordDims;
junov@google.comf93e7172011-03-31 21:26:24 +00001129 } else {
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001130 segments->fVaryingDims = segments->fCoordDims + 1;
junov@google.comf93e7172011-03-31 21:26:24 +00001131 }
1132 }
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001133 GrAssert(segments->fVaryingDims > 0);
junov@google.comf93e7172011-03-31 21:26:24 +00001134
tomhudson@google.com898e7b52012-06-01 20:42:15 +00001135 // Must setup variables after computing segments->fVaryingDims
1136 if (NULL != customStage) {
1137 customStage->setupVariables(segments, stageNum);
1138 }
1139
bsalomon@google.comf0a104e2012-07-10 17:51:07 +00001140 SkString samplerName;
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001141 sampler_name(stageNum, &samplerName);
bsalomon@google.comeb715c82012-07-11 15:03:31 +00001142 segments->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
1143 kSampler2D_GrSLType, samplerName.c_str());
bsalomon@google.com91961302011-05-09 18:39:58 +00001144 locations->fSamplerUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +00001145
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001146 const char *varyingVSName, *varyingFSName;
tomhudson@google.com23cb2292012-05-30 18:26:03 +00001147 segments->addVarying(GrSLFloatVectorType(segments->fVaryingDims),
1148 "Stage",
1149 stageNum,
1150 &varyingVSName,
1151 &varyingFSName);
junov@google.comf93e7172011-03-31 21:26:24 +00001152
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001153 if (!matName) {
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001154 GrAssert(segments->fVaryingDims == segments->fCoordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001155 segments->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord);
junov@google.comf93e7172011-03-31 21:26:24 +00001156 } else {
bsalomon@google.com91961302011-05-09 18:39:58 +00001157 // varying = texMatrix * texCoord
1158 segments->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n",
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001159 varyingVSName, matName, vsInCoord,
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001160 vector_all_coords(segments->fVaryingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001161 }
1162
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001163 if (NULL != customStage) {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001164 segments->fVSCode.appendf("\t{ // stage %d %s\n",
1165 stageNum, customStage->name());
tomhudson@google.com6a820b62012-05-24 15:10:14 +00001166 customStage->emitVS(segments, varyingVSName);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001167 segments->fVSCode.appendf("\t}\n");
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001168 }
1169
junov@google.comf93e7172011-03-31 21:26:24 +00001170 /// Fragment Shader Stuff
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001171
tomhudson@google.com5440f062012-06-01 15:55:50 +00001172 segments->fSampleCoords = varyingFSName;
1173
1174 GrGLShaderBuilder::SamplerMode sampleMode =
1175 GrGLShaderBuilder::kExplicitDivide_SamplerMode;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001176 if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit |
1177 StageDesc::kNoPerspective_OptFlagBit)) {
tomhudson@google.com5440f062012-06-01 15:55:50 +00001178 sampleMode = GrGLShaderBuilder::kDefault_SamplerMode;
bsalomon@google.comd2ae1fa2012-06-04 20:06:02 +00001179 } else if (NULL == customStage) {
tomhudson@google.com5440f062012-06-01 15:55:50 +00001180 sampleMode = GrGLShaderBuilder::kProj_SamplerMode;
junov@google.comf93e7172011-03-31 21:26:24 +00001181 }
tomhudson@google.com5440f062012-06-01 15:55:50 +00001182 segments->setupTextureAccess(sampleMode, stageNum);
junov@google.comf93e7172011-03-31 21:26:24 +00001183
tomhudson@google.com898e7b52012-06-01 20:42:15 +00001184 segments->computeSwizzle(desc.fInConfigFlags);
1185 segments->computeModulate(fsInColor);
junov@google.comf93e7172011-03-31 21:26:24 +00001186
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001187 static const uint32_t kMulByAlphaMask =
1188 (StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
1189 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag);
1190
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001191 if (desc.fOptFlags & StageDesc::kCustomTextureDomain_OptFlagBit) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +00001192 SkString texDomainName;
junov@google.com6acc9b32011-05-16 18:32:07 +00001193 tex_domain_name(stageNum, &texDomainName);
bsalomon@google.comeb715c82012-07-11 15:03:31 +00001194 segments->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
1195 kVec4f_GrSLType, texDomainName.c_str());
bsalomon@google.comf0a104e2012-07-10 17:51:07 +00001196 SkString coordVar("clampCoord");
junov@google.com6acc9b32011-05-16 18:32:07 +00001197 segments->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n",
tomhudson@google.com9c639a42012-05-14 19:58:06 +00001198 float_vector_type_str(segments->fCoordDims),
junov@google.com6acc9b32011-05-16 18:32:07 +00001199 coordVar.c_str(),
tomhudson@google.com040c41a2012-05-18 14:57:40 +00001200 segments->fSampleCoords.c_str(),
junov@google.com6acc9b32011-05-16 18:32:07 +00001201 texDomainName.c_str(),
1202 texDomainName.c_str());
tomhudson@google.com040c41a2012-05-18 14:57:40 +00001203 segments->fSampleCoords = coordVar;
junov@google.com6acc9b32011-05-16 18:32:07 +00001204 locations->fTexDomUni = kUseUniform;
1205 }
1206
tomhudson@google.com6a820b62012-05-24 15:10:14 +00001207 // NOTE: GrGLProgramStages are now responsible for fetching
1208 if (NULL == customStage) {
bsalomon@google.comd2ae1fa2012-06-04 20:06:02 +00001209 if (desc.fInConfigFlags & kMulByAlphaMask) {
1210 // only one of the mul by alpha flags should be set
1211 GrAssert(GrIsPow2(kMulByAlphaMask & desc.fInConfigFlags));
1212 GrAssert(!(desc.fInConfigFlags &
1213 StageDesc::kSmearAlpha_InConfigFlag));
1214 GrAssert(!(desc.fInConfigFlags &
1215 StageDesc::kSmearRed_InConfigFlag));
1216 segments->fFSCode.appendf("\t%s = %s(%s, %s)%s;\n",
1217 fsOutColor,
1218 segments->fTexFunc.c_str(),
1219 samplerName.c_str(),
1220 segments->fSampleCoords.c_str(),
1221 segments->fSwizzle.c_str());
1222 if (desc.fInConfigFlags &
1223 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag) {
1224 segments->fFSCode.appendf("\t%s = vec4(ceil(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
1225 fsOutColor, fsOutColor, fsOutColor,
1226 fsOutColor, segments->fModulate.c_str());
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001227 } else {
bsalomon@google.comd2ae1fa2012-06-04 20:06:02 +00001228 segments->fFSCode.appendf("\t%s = vec4(floor(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
1229 fsOutColor, fsOutColor, fsOutColor,
1230 fsOutColor, segments->fModulate.c_str());
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001231 }
bsalomon@google.comd2ae1fa2012-06-04 20:06:02 +00001232 } else {
1233 segments->emitDefaultFetch(fsOutColor, samplerName.c_str());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001234 }
junov@google.comf93e7172011-03-31 21:26:24 +00001235 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001236
1237 if (NULL != customStage) {
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001238 // Enclose custom code in a block to avoid namespace conflicts
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001239 segments->fFSCode.appendf("\t{ // stage %d %s \n",
1240 stageNum, customStage->name());
tomhudson@google.com242ed6f2012-05-30 17:38:57 +00001241 customStage->emitFS(segments, fsOutColor, fsInColor,
1242 samplerName.c_str());
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001243 segments->fFSCode.appendf("\t}\n");
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001244 }
junov@google.comf93e7172011-03-31 21:26:24 +00001245}