blob: fc80a7893973ad499004af91ebbe0942dcb696f4 [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"
bsalomon@google.coma469c282012-10-24 18:28:34 +000011#include "GrEffect.h"
bsalomon@google.comc7818882013-03-20 19:19:53 +000012#include "GrDrawEffect.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +000013#include "GrGLEffect.h"
bsalomon@google.com34cccde2013-01-04 18:34:30 +000014#include "GrGpuGL.h"
bsalomon@google.com4fa66942011-09-20 19:06:12 +000015#include "GrGLShaderVar.h"
bsalomon@google.com396e61f2012-10-25 19:00:29 +000016#include "GrBackendEffectFactory.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000017#include "SkTrace.h"
Scroggo97c88c22011-05-11 14:05:25 +000018#include "SkXfermode.h"
19
humper@google.com7af56be2013-01-14 18:49:19 +000020#include "SkRTConf.h"
21
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000022SK_DEFINE_INST_COUNT(GrGLProgram)
23
robertphillips@google.com6177e692013-02-28 20:16:25 +000024#define GL_CALL(X) GR_GL_CALL(fContext.interface(), X)
25#define GL_CALL_RET(R, X) GR_GL_CALL_RET(fContext.interface(), R, X)
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000026
jvanverth@google.com65eb4d52013-03-19 18:51:02 +000027SK_CONF_DECLARE(bool, c_PrintShaders, "gpu.printShaders", false,
28 "Print the source code for all shaders generated.");
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000029
junov@google.comf93e7172011-03-31 21:26:24 +000030#define COL_ATTR_NAME "aColor"
bsalomon@google.coma3108262011-10-10 14:08:47 +000031#define COV_ATTR_NAME "aCoverage"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000032#define EDGE_ATTR_NAME "aEdge"
junov@google.comf93e7172011-03-31 21:26:24 +000033
bsalomon@google.com4fa66942011-09-20 19:06:12 +000034namespace {
bsalomon@google.com4fa66942011-09-20 19:06:12 +000035inline const char* declared_color_output_name() { return "fsColorOut"; }
36inline const char* dual_source_output_name() { return "dualSourceOut"; }
bsalomon@google.com4fa66942011-09-20 19:06:12 +000037}
junov@google.com6acc9b32011-05-16 18:32:07 +000038
jvanverth@google.com9b855c72013-03-01 18:21:22 +000039const GrGLProgram::AttribLayout GrGLProgram::kAttribLayouts[kGrVertexAttribTypeCount] = {
40 {1, GR_GL_FLOAT, false}, // kFloat_GrVertexAttribType
41 {2, GR_GL_FLOAT, false}, // kVec2f_GrVertexAttribType
42 {3, GR_GL_FLOAT, false}, // kVec3f_GrVertexAttribType
43 {4, GR_GL_FLOAT, false}, // kVec4f_GrVertexAttribType
44 {4, GR_GL_UNSIGNED_BYTE, true}, // kVec4ub_GrVertexAttribType
45};
46
bsalomon@google.com91207482013-02-12 21:45:24 +000047void GrGLProgram::BuildDesc(const GrDrawState& drawState,
48 bool isPoints,
49 GrDrawState::BlendOptFlags blendOpts,
50 GrBlendCoeff srcCoeff,
51 GrBlendCoeff dstCoeff,
52 const GrGpuGL* gpu,
53 Desc* desc) {
54
55 // This should already have been caught
56 GrAssert(!(GrDrawState::kSkipDraw_BlendOptFlag & blendOpts));
57
58 bool skipCoverage = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag);
59
60 bool skipColor = SkToBool(blendOpts & (GrDrawState::kEmitTransBlack_BlendOptFlag |
61 GrDrawState::kEmitCoverage_BlendOptFlag));
62
63 // The descriptor is used as a cache key. Thus when a field of the
jvanverth@google.com9b855c72013-03-01 18:21:22 +000064 // descriptor will not affect program generation (because of the attribute
65 // bindings in use or other descriptor field settings) it should be set
bsalomon@google.com91207482013-02-12 21:45:24 +000066 // to a canonical value to avoid duplicate programs with different keys.
67
68 // Must initialize all fields or cache will have false negatives!
jvanverth@google.com9b855c72013-03-01 18:21:22 +000069 desc->fAttribBindings = drawState.getAttribBindings();
bsalomon@google.com91207482013-02-12 21:45:24 +000070
71 desc->fEmitsPointSize = isPoints;
72
jvanverth@google.com65eb4d52013-03-19 18:51:02 +000073 bool requiresAttributeColors =
74 !skipColor && SkToBool(desc->fAttribBindings & GrDrawState::kColor_AttribBindingsBit);
75 bool requiresAttributeCoverage =
76 !skipCoverage && SkToBool(desc->fAttribBindings & GrDrawState::kCoverage_AttribBindingsBit);
bsalomon@google.com91207482013-02-12 21:45:24 +000077
78 // fColorInput/fCoverageInput records how colors are specified for the program So we strip the
jvanverth@google.com9b855c72013-03-01 18:21:22 +000079 // bits from the bindings to avoid false negatives when searching for an existing program in the
bsalomon@google.com91207482013-02-12 21:45:24 +000080 // cache.
jvanverth@google.com65eb4d52013-03-19 18:51:02 +000081 desc->fAttribBindings &=
82 ~(GrDrawState::kColor_AttribBindingsBit | GrDrawState::kCoverage_AttribBindingsBit);
bsalomon@google.com91207482013-02-12 21:45:24 +000083
84 desc->fColorFilterXfermode = skipColor ?
85 SkXfermode::kDst_Mode :
86 drawState.getColorFilterMode();
87
88 // no reason to do edge aa or look at per-vertex coverage if coverage is ignored
89 if (skipCoverage) {
bsalomon@google.com4647f902013-03-26 14:45:27 +000090 desc->fAttribBindings &= ~(GrDrawState::kCoverage_AttribBindingsBit);
bsalomon@google.com91207482013-02-12 21:45:24 +000091 }
92
93 bool colorIsTransBlack = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag);
94 bool colorIsSolidWhite = (blendOpts & GrDrawState::kEmitCoverage_BlendOptFlag) ||
95 (!requiresAttributeColors && 0xffffffff == drawState.getColor());
96 if (colorIsTransBlack) {
97 desc->fColorInput = Desc::kTransBlack_ColorInput;
98 } else if (colorIsSolidWhite) {
99 desc->fColorInput = Desc::kSolidWhite_ColorInput;
100 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
101 desc->fColorInput = Desc::kUniform_ColorInput;
102 } else {
103 desc->fColorInput = Desc::kAttribute_ColorInput;
104 }
105
106 bool covIsSolidWhite = !requiresAttributeCoverage && 0xffffffff == drawState.getCoverage();
107
108 if (skipCoverage) {
109 desc->fCoverageInput = Desc::kTransBlack_ColorInput;
110 } else if (covIsSolidWhite) {
111 desc->fCoverageInput = Desc::kSolidWhite_ColorInput;
112 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
113 desc->fCoverageInput = Desc::kUniform_ColorInput;
114 } else {
115 desc->fCoverageInput = Desc::kAttribute_ColorInput;
116 }
117
118 int lastEnabledStage = -1;
119
bsalomon@google.com4647f902013-03-26 14:45:27 +0000120 if (!skipCoverage) {
bsalomon@google.com91207482013-02-12 21:45:24 +0000121 desc->fDiscardIfOutsideEdge = drawState.getStencil().doesWrite();
122 } else {
123 // Use canonical values when edge-aa is not enabled to avoid program cache misses.
bsalomon@google.com91207482013-02-12 21:45:24 +0000124 desc->fDiscardIfOutsideEdge = false;
125 }
126
127 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
128
129 bool skip = s < drawState.getFirstCoverageStage() ? skipColor : skipCoverage;
130 if (!skip && drawState.isStageEnabled(s)) {
131 lastEnabledStage = s;
132 const GrEffectRef& effect = *drawState.getStage(s).getEffect();
133 const GrBackendEffectFactory& factory = effect->getFactory();
bsalomon@google.comc7818882013-03-20 19:19:53 +0000134 bool explicitLocalCoords = (drawState.getAttribBindings() &
135 GrDrawState::kLocalCoords_AttribBindingsBit);
136 GrDrawEffect drawEffect(drawState.getStage(s), explicitLocalCoords);
137 desc->fEffectKeys[s] = factory.glEffectKey(drawEffect, gpu->glCaps());
bsalomon@google.com91207482013-02-12 21:45:24 +0000138 } else {
139 desc->fEffectKeys[s] = 0;
140 }
141 }
142
143 desc->fDualSrcOutput = Desc::kNone_DualSrcOutput;
144
145 // Currently the experimental GS will only work with triangle prims (and it doesn't do anything
146 // other than pass through values from the VS to the FS anyway).
147#if GR_GL_EXPERIMENTAL_GS
148#if 0
bsalomon@google.combcce8922013-03-25 15:38:39 +0000149 desc->fExperimentalGS = gpu->caps().geometryShaderSupport();
bsalomon@google.com91207482013-02-12 21:45:24 +0000150#else
151 desc->fExperimentalGS = false;
152#endif
153#endif
154
155 // We want to avoid generating programs with different "first cov stage" values when they would
156 // compute the same result. We set field in the desc to kNumStages when either there are no
157 // coverage stages or the distinction between coverage and color is immaterial.
158 int firstCoverageStage = GrDrawState::kNumStages;
159 desc->fFirstCoverageStage = GrDrawState::kNumStages;
160 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
161 if (hasCoverage) {
162 firstCoverageStage = drawState.getFirstCoverageStage();
163 }
164
165 // other coverage inputs
166 if (!hasCoverage) {
bsalomon@google.com4647f902013-03-26 14:45:27 +0000167 hasCoverage = requiresAttributeCoverage;
bsalomon@google.com91207482013-02-12 21:45:24 +0000168 }
169
170 if (hasCoverage) {
171 // color filter is applied between color/coverage computation
172 if (SkXfermode::kDst_Mode != desc->fColorFilterXfermode) {
173 desc->fFirstCoverageStage = firstCoverageStage;
174 }
175
bsalomon@google.combcce8922013-03-25 15:38:39 +0000176 if (gpu->caps()->dualSourceBlendingSupport() &&
bsalomon@google.com91207482013-02-12 21:45:24 +0000177 !(blendOpts & (GrDrawState::kEmitCoverage_BlendOptFlag |
178 GrDrawState::kCoverageAsAlpha_BlendOptFlag))) {
179 if (kZero_GrBlendCoeff == dstCoeff) {
180 // write the coverage value to second color
181 desc->fDualSrcOutput = Desc::kCoverage_DualSrcOutput;
182 desc->fFirstCoverageStage = firstCoverageStage;
183 } else if (kSA_GrBlendCoeff == dstCoeff) {
184 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
185 desc->fDualSrcOutput = Desc::kCoverageISA_DualSrcOutput;
186 desc->fFirstCoverageStage = firstCoverageStage;
187 } else if (kSC_GrBlendCoeff == dstCoeff) {
188 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
189 desc->fDualSrcOutput = Desc::kCoverageISC_DualSrcOutput;
190 desc->fFirstCoverageStage = firstCoverageStage;
191 }
192 }
193 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000194
195 desc->fPositionAttributeIndex = drawState.getAttribIndex(GrDrawState::kPosition_AttribIndex);
196 if (requiresAttributeColors) {
197 desc->fColorAttributeIndex = drawState.getAttribIndex(GrDrawState::kColor_AttribIndex);
198 } else {
199 desc->fColorAttributeIndex = GrDrawState::kColorOverrideAttribIndexValue;
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000200 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000201 if (requiresAttributeCoverage) {
202 desc->fCoverageAttributeIndex = drawState.getAttribIndex(GrDrawState::kCoverage_AttribIndex);
203 } else {
204 desc->fCoverageAttributeIndex = GrDrawState::kCoverageOverrideAttribIndexValue;
205 }
bsalomon@google.comc7818882013-03-20 19:19:53 +0000206 if (desc->fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit) {
207 desc->fLocalCoordsAttributeIndex = drawState.getAttribIndex(GrDrawState::kLocalCoords_AttribIndex);
robertphillips@google.com37ebe3f2013-03-04 20:03:44 +0000208 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000209
210#if GR_DEBUG
211 // verify valid vertex attribute state
212 const GrVertexAttrib* vertexAttribs = drawState.getVertexAttribs();
213 GrAssert(desc->fPositionAttributeIndex < GrDrawState::kVertexAttribCnt);
214 GrAssert(kAttribLayouts[vertexAttribs[desc->fPositionAttributeIndex].fType].fCount == 2);
215 if (requiresAttributeColors) {
216 GrAssert(desc->fColorAttributeIndex < GrDrawState::kVertexAttribCnt);
217 GrAssert(kAttribLayouts[vertexAttribs[desc->fColorAttributeIndex].fType].fCount == 4);
218 }
219 if (requiresAttributeCoverage) {
220 GrAssert(desc->fCoverageAttributeIndex < GrDrawState::kVertexAttribCnt);
221 GrAssert(kAttribLayouts[vertexAttribs[desc->fCoverageAttributeIndex].fType].fCount == 4);
222 }
bsalomon@google.comc7818882013-03-20 19:19:53 +0000223 if (desc->fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit) {
224 GrAssert(desc->fLocalCoordsAttributeIndex < GrDrawState::kVertexAttribCnt);
225 GrAssert(kAttribLayouts[vertexAttribs[desc->fLocalCoordsAttributeIndex].fType].fCount == 2);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000226 }
227#endif
bsalomon@google.com91207482013-02-12 21:45:24 +0000228}
229
robertphillips@google.com6177e692013-02-28 20:16:25 +0000230GrGLProgram* GrGLProgram::Create(const GrGLContext& gl,
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000231 const Desc& desc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000232 const GrEffectStage* stages[]) {
233 GrGLProgram* program = SkNEW_ARGS(GrGLProgram, (gl, desc, stages));
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000234 if (!program->succeeded()) {
235 delete program;
236 program = NULL;
237 }
238 return program;
239}
240
robertphillips@google.com6177e692013-02-28 20:16:25 +0000241GrGLProgram::GrGLProgram(const GrGLContext& gl,
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000242 const Desc& desc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000243 const GrEffectStage* stages[])
robertphillips@google.com6177e692013-02-28 20:16:25 +0000244: fContext(gl)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000245, fUniformManager(gl) {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000246 fDesc = desc;
247 fVShaderID = 0;
248 fGShaderID = 0;
249 fFShaderID = 0;
250 fProgramID = 0;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000251
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000252 fColor = GrColor_ILLEGAL;
253 fColorFilterColor = GrColor_ILLEGAL;
254
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000255 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com46fba0d2012-10-25 21:42:05 +0000256 fEffects[s] = NULL;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000257 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000258
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000259 this->genProgram(stages);
junov@google.comf93e7172011-03-31 21:26:24 +0000260}
261
262GrGLProgram::~GrGLProgram() {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000263 if (fVShaderID) {
264 GL_CALL(DeleteShader(fVShaderID));
265 }
266 if (fGShaderID) {
267 GL_CALL(DeleteShader(fGShaderID));
268 }
269 if (fFShaderID) {
270 GL_CALL(DeleteShader(fFShaderID));
271 }
272 if (fProgramID) {
273 GL_CALL(DeleteProgram(fProgramID));
274 }
275
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000276 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
bsalomon@google.com46fba0d2012-10-25 21:42:05 +0000277 delete fEffects[i];
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000278 }
junov@google.comf93e7172011-03-31 21:26:24 +0000279}
280
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000281void GrGLProgram::abandon() {
282 fVShaderID = 0;
283 fGShaderID = 0;
284 fFShaderID = 0;
285 fProgramID = 0;
286}
287
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000288void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000289 GrBlendCoeff* dstCoeff) const {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000290 switch (fDesc.fDualSrcOutput) {
291 case Desc::kNone_DualSrcOutput:
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000292 break;
293 // the prog will write a coverage value to the secondary
294 // output and the dst is blended by one minus that value.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000295 case Desc::kCoverage_DualSrcOutput:
296 case Desc::kCoverageISA_DualSrcOutput:
297 case Desc::kCoverageISC_DualSrcOutput:
bsalomon@google.com47059542012-06-06 20:51:20 +0000298 *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000299 break;
300 default:
301 GrCrash("Unexpected dual source blend output");
302 break;
303 }
304}
305
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000306namespace {
307
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000308// given two blend coeffecients determine whether the src
309// and/or dst computation can be omitted.
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000310inline void need_blend_inputs(SkXfermode::Coeff srcCoeff,
311 SkXfermode::Coeff dstCoeff,
312 bool* needSrcValue,
313 bool* needDstValue) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000314 if (SkXfermode::kZero_Coeff == srcCoeff) {
315 switch (dstCoeff) {
316 // these all read the src
317 case SkXfermode::kSC_Coeff:
318 case SkXfermode::kISC_Coeff:
319 case SkXfermode::kSA_Coeff:
320 case SkXfermode::kISA_Coeff:
321 *needSrcValue = true;
322 break;
323 default:
324 *needSrcValue = false;
325 break;
326 }
327 } else {
328 *needSrcValue = true;
329 }
330 if (SkXfermode::kZero_Coeff == dstCoeff) {
331 switch (srcCoeff) {
332 // these all read the dst
333 case SkXfermode::kDC_Coeff:
334 case SkXfermode::kIDC_Coeff:
335 case SkXfermode::kDA_Coeff:
336 case SkXfermode::kIDA_Coeff:
337 *needDstValue = true;
338 break;
339 default:
340 *needDstValue = false;
341 break;
342 }
343 } else {
344 *needDstValue = true;
Scroggo97c88c22011-05-11 14:05:25 +0000345 }
346}
347
348/**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000349 * Create a blend_coeff * value string to be used in shader code. Sets empty
350 * string if result is trivially zero.
351 */
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000352inline void blend_term_string(SkString* str, SkXfermode::Coeff coeff,
353 const char* src, const char* dst,
354 const char* value) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000355 switch (coeff) {
356 case SkXfermode::kZero_Coeff: /** 0 */
357 *str = "";
358 break;
359 case SkXfermode::kOne_Coeff: /** 1 */
360 *str = value;
361 break;
362 case SkXfermode::kSC_Coeff:
363 str->printf("(%s * %s)", src, value);
364 break;
365 case SkXfermode::kISC_Coeff:
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000366 str->printf("((%s - %s) * %s)", GrGLSLOnesVecf(4), src, value);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000367 break;
368 case SkXfermode::kDC_Coeff:
369 str->printf("(%s * %s)", dst, value);
370 break;
371 case SkXfermode::kIDC_Coeff:
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000372 str->printf("((%s - %s) * %s)", GrGLSLOnesVecf(4), dst, value);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000373 break;
374 case SkXfermode::kSA_Coeff: /** src alpha */
375 str->printf("(%s.a * %s)", src, value);
376 break;
377 case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */
378 str->printf("((1.0 - %s.a) * %s)", src, value);
379 break;
380 case SkXfermode::kDA_Coeff: /** dst alpha */
381 str->printf("(%s.a * %s)", dst, value);
382 break;
383 case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */
384 str->printf("((1.0 - %s.a) * %s)", dst, value);
385 break;
386 default:
387 GrCrash("Unexpected xfer coeff.");
388 break;
389 }
390}
391/**
Scroggo97c88c22011-05-11 14:05:25 +0000392 * Adds a line to the fragment shader code which modifies the color by
393 * the specified color filter.
394 */
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000395void add_color_filter(GrGLShaderBuilder* builder,
396 const char * outputVar,
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000397 SkXfermode::Coeff uniformCoeff,
398 SkXfermode::Coeff colorCoeff,
399 const char* filterColor,
400 const char* inColor) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000401 SkString colorStr, constStr;
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000402 blend_term_string(&colorStr, colorCoeff, filterColor, inColor, inColor);
403 blend_term_string(&constStr, uniformCoeff, filterColor, inColor, filterColor);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000404
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000405 SkString sum;
406 GrGLSLAdd4f(&sum, colorStr.c_str(), constStr.c_str());
407 builder->fsCodeAppendf("\t%s = %s;\n", outputVar, sum.c_str());
Scroggo97c88c22011-05-11 14:05:25 +0000408}
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000409}
Scroggo97c88c22011-05-11 14:05:25 +0000410
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000411void GrGLProgram::genInputColor(GrGLShaderBuilder* builder, SkString* inColor) {
412 switch (fDesc.fColorInput) {
413 case GrGLProgram::Desc::kAttribute_ColorInput: {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000414 builder->addAttribute(kVec4f_GrSLType, COL_ATTR_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000415 const char *vsName, *fsName;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000416 builder->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000417 builder->vsCodeAppendf("\t%s = " COL_ATTR_NAME ";\n", vsName);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000418 *inColor = fsName;
419 } break;
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000420 case GrGLProgram::Desc::kUniform_ColorInput: {
421 const char* name;
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000422 fUniformHandles.fColorUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
423 kVec4f_GrSLType, "Color", &name);
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000424 *inColor = name;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000425 break;
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000426 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000427 case GrGLProgram::Desc::kTransBlack_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000428 GrAssert(!"needComputedColor should be false.");
429 break;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000430 case GrGLProgram::Desc::kSolidWhite_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000431 break;
432 default:
433 GrCrash("Unknown color type.");
434 break;
435 }
436}
437
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000438void GrGLProgram::genUniformCoverage(GrGLShaderBuilder* builder, SkString* inOutCoverage) {
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000439 const char* covUniName;
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000440 fUniformHandles.fCoverageUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
441 kVec4f_GrSLType, "Coverage", &covUniName);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000442 if (inOutCoverage->size()) {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000443 builder->fsCodeAppendf("\tvec4 uniCoverage = %s * %s;\n",
444 covUniName, inOutCoverage->c_str());
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000445 *inOutCoverage = "uniCoverage";
446 } else {
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000447 *inOutCoverage = covUniName;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000448 }
449}
450
451namespace {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000452void gen_attribute_coverage(GrGLShaderBuilder* builder,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000453 SkString* inOutCoverage) {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000454 builder->addAttribute(kVec4f_GrSLType, COV_ATTR_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000455 const char *vsName, *fsName;
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000456 builder->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName);
457 builder->vsCodeAppendf("\t%s = " COV_ATTR_NAME ";\n", vsName);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000458 if (inOutCoverage->size()) {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000459 builder->fsCodeAppendf("\tvec4 attrCoverage = %s * %s;\n", fsName, inOutCoverage->c_str());
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000460 *inOutCoverage = "attrCoverage";
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000461 } else {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000462 *inOutCoverage = fsName;
463 }
464}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000465}
466
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000467void GrGLProgram::genGeometryShader(GrGLShaderBuilder* builder) const {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000468#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000469 // TODO: The builder should add all this glue code.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000470 if (fDesc.fExperimentalGS) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000471 GrAssert(fContext.info().glslGeneration() >= k150_GrGLSLGeneration);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000472 builder->fGSHeader.append("layout(triangles) in;\n"
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000473 "layout(triangle_strip, max_vertices = 6) out;\n");
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000474 builder->gsCodeAppend("\tfor (int i = 0; i < 3; ++i) {\n"
475 "\t\tgl_Position = gl_in[i].gl_Position;\n");
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000476 if (fDesc.fEmitsPointSize) {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000477 builder->gsCodeAppend("\t\tgl_PointSize = 1.0;\n");
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000478 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000479 GrAssert(builder->fGSInputs.count() == builder->fGSOutputs.count());
480 int count = builder->fGSInputs.count();
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000481 for (int i = 0; i < count; ++i) {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000482 builder->gsCodeAppendf("\t\t%s = %s[i];\n",
483 builder->fGSOutputs[i].getName().c_str(),
484 builder->fGSInputs[i].getName().c_str());
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000485 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000486 builder->gsCodeAppend("\t\tEmitVertex();\n"
487 "\t}\n"
488 "\tEndPrimitive();\n");
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000489 }
490#endif
491}
492
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000493const char* GrGLProgram::adjustInColor(const SkString& inColor) const {
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000494 if (inColor.size()) {
495 return inColor.c_str();
496 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000497 if (Desc::kSolidWhite_ColorInput == fDesc.fColorInput) {
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000498 return GrGLSLOnesVecf(4);
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000499 } else {
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000500 return GrGLSLZerosVecf(4);
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000501 }
502 }
503}
504
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000505namespace {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000506// prints a shader using params similar to glShaderSource
507void print_shader(GrGLint stringCnt,
508 const GrGLchar** strings,
509 GrGLint* stringLengths) {
510 for (int i = 0; i < stringCnt; ++i) {
511 if (NULL == stringLengths || stringLengths[i] < 0) {
512 GrPrintf(strings[i]);
513 } else {
514 GrPrintf("%.*s", stringLengths[i], strings[i]);
515 }
516 }
517}
518
519// Compiles a GL shader, returns shader ID or 0 if failed params have same meaning as glShaderSource
robertphillips@google.com6177e692013-02-28 20:16:25 +0000520GrGLuint compile_shader(const GrGLContext& gl,
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000521 GrGLenum type,
522 int stringCnt,
523 const char** strings,
524 int* stringLengths) {
525 SK_TRACE_EVENT1("GrGLProgram::CompileShader",
526 "stringCount", SkStringPrintf("%i", stringCnt).c_str());
527
528 GrGLuint shader;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000529 GR_GL_CALL_RET(gl.interface(), shader, CreateShader(type));
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000530 if (0 == shader) {
531 return 0;
532 }
533
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000534 const GrGLInterface* gli = gl.interface();
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000535 GrGLint compiled = GR_GL_INIT_ZERO;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000536 GR_GL_CALL(gli, ShaderSource(shader, stringCnt, strings, stringLengths));
537 GR_GL_CALL(gli, CompileShader(shader));
538 GR_GL_CALL(gli, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled));
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000539
540 if (!compiled) {
541 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000542 GR_GL_CALL(gli, GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000543 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
544 if (infoLen > 0) {
545 // retrieve length even though we don't need it to workaround bug in chrome cmd buffer
546 // param validation.
547 GrGLsizei length = GR_GL_INIT_ZERO;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000548 GR_GL_CALL(gli, GetShaderInfoLog(shader, infoLen+1,
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000549 &length, (char*)log.get()));
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000550 print_shader(stringCnt, strings, stringLengths);
551 GrPrintf("\n%s", log.get());
552 }
553 GrAssert(!"Shader compilation failed!");
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000554 GR_GL_CALL(gli, DeleteShader(shader));
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000555 return 0;
556 }
557 return shader;
558}
559
560// helper version of above for when shader is already flattened into a single SkString
robertphillips@google.com6177e692013-02-28 20:16:25 +0000561GrGLuint compile_shader(const GrGLContext& gl, GrGLenum type, const SkString& shader) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000562 const GrGLchar* str = shader.c_str();
563 int length = shader.size();
564 return compile_shader(gl, type, 1, &str, &length);
565}
566
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000567}
568
569// compiles all the shaders from builder and stores the shader IDs
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000570bool GrGLProgram::compileShaders(const GrGLShaderBuilder& builder) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000571
572 SkString shader;
573
574 builder.getShader(GrGLShaderBuilder::kVertex_ShaderType, &shader);
humper@google.com7af56be2013-01-14 18:49:19 +0000575 if (c_PrintShaders) {
576 GrPrintf(shader.c_str());
577 GrPrintf("\n");
578 }
skia.committer@gmail.com7fc0e0a2013-01-15 02:01:40 +0000579
robertphillips@google.com6177e692013-02-28 20:16:25 +0000580 if (!(fVShaderID = compile_shader(fContext, GR_GL_VERTEX_SHADER, shader))) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000581 return false;
582 }
583
584 if (builder.fUsesGS) {
585 builder.getShader(GrGLShaderBuilder::kGeometry_ShaderType, &shader);
humper@google.com7af56be2013-01-14 18:49:19 +0000586 if (c_PrintShaders) {
587 GrPrintf(shader.c_str());
588 GrPrintf("\n");
589 }
robertphillips@google.com6177e692013-02-28 20:16:25 +0000590 if (!(fGShaderID = compile_shader(fContext, GR_GL_GEOMETRY_SHADER, shader))) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000591 return false;
592 }
593 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000594 fGShaderID = 0;
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000595 }
596
597 builder.getShader(GrGLShaderBuilder::kFragment_ShaderType, &shader);
humper@google.com7af56be2013-01-14 18:49:19 +0000598 if (c_PrintShaders) {
599 GrPrintf(shader.c_str());
600 GrPrintf("\n");
601 }
robertphillips@google.com6177e692013-02-28 20:16:25 +0000602 if (!(fFShaderID = compile_shader(fContext, GR_GL_FRAGMENT_SHADER, shader))) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000603 return false;
604 }
605
606 return true;
607}
608
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000609bool GrGLProgram::genProgram(const GrEffectStage* stages[]) {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000610 GrAssert(0 == fProgramID);
611
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000612 const GrAttribBindings& attribBindings = fDesc.fAttribBindings;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000613 bool hasExplicitLocalCoords =
614 SkToBool(attribBindings & GrDrawState::kLocalCoords_AttribBindingsBit);
615 GrGLShaderBuilder builder(fContext.info(), fUniformManager, hasExplicitLocalCoords);
junov@google.comf93e7172011-03-31 21:26:24 +0000616
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000617#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000618 builder.fUsesGS = fDesc.fExperimentalGS;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000619#endif
620
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000621 SkXfermode::Coeff colorCoeff, uniformCoeff;
622 // The rest of transfer mode color filters have not been implemented
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000623 if (fDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000624 GR_DEBUGCODE(bool success =)
tomhudson@google.com0d831722011-06-02 15:37:14 +0000625 SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode>
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000626 (fDesc.fColorFilterXfermode),
tomhudson@google.com0d831722011-06-02 15:37:14 +0000627 &uniformCoeff, &colorCoeff);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000628 GR_DEBUGASSERT(success);
629 } else {
630 colorCoeff = SkXfermode::kOne_Coeff;
631 uniformCoeff = SkXfermode::kZero_Coeff;
632 }
633
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000634 // no need to do the color filter if coverage is 0. The output color is scaled by the coverage.
635 // All the dual source outputs are scaled by the coverage as well.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000636 if (Desc::kTransBlack_ColorInput == fDesc.fCoverageInput) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000637 colorCoeff = SkXfermode::kZero_Coeff;
638 uniformCoeff = SkXfermode::kZero_Coeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000639 }
640
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000641 // If we know the final color is going to be all zeros then we can
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000642 // simplify the color filter coefficients. needComputedColor will then
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000643 // come out false below.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000644 if (Desc::kTransBlack_ColorInput == fDesc.fColorInput) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000645 colorCoeff = SkXfermode::kZero_Coeff;
646 if (SkXfermode::kDC_Coeff == uniformCoeff ||
647 SkXfermode::kDA_Coeff == uniformCoeff) {
648 uniformCoeff = SkXfermode::kZero_Coeff;
649 } else if (SkXfermode::kIDC_Coeff == uniformCoeff ||
650 SkXfermode::kIDA_Coeff == uniformCoeff) {
651 uniformCoeff = SkXfermode::kOne_Coeff;
652 }
653 }
654
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000655 bool needColorFilterUniform;
656 bool needComputedColor;
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000657 need_blend_inputs(uniformCoeff, colorCoeff,
658 &needColorFilterUniform, &needComputedColor);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000659
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000660 // the dual source output has no canonical var name, have to
661 // declare an output, which is incompatible with gl_FragColor/gl_FragData.
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000662 bool dualSourceOutputWritten = false;
robertphillips@google.com6177e692013-02-28 20:16:25 +0000663 builder.fHeader.append(GrGetGLSLVersionDecl(fContext.info().binding(),
664 fContext.info().glslGeneration()));
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000665
666 GrGLShaderVar colorOutput;
robertphillips@google.com6177e692013-02-28 20:16:25 +0000667 bool isColorDeclared = GrGLSLSetupFSColorOuput(fContext.info().glslGeneration(),
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000668 declared_color_output_name(),
669 &colorOutput);
670 if (isColorDeclared) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000671 builder.fFSOutputs.push_back(colorOutput);
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000672 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000673
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000674 const char* viewMName;
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000675 fUniformHandles.fViewMatrixUni = builder.addUniform(GrGLShaderBuilder::kVertex_ShaderType,
676 kMat33f_GrSLType, "ViewM", &viewMName);
bsalomon@google.com341767c2012-05-11 20:47:39 +0000677
skia.committer@gmail.come862d162012-10-31 02:01:18 +0000678
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000679 builder.vsCodeAppendf("\tvec3 pos3 = %s * vec3(%s, 1);\n"
680 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n",
681 viewMName, builder.positionAttribute().getName().c_str());
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000682
bsalomon@google.com91961302011-05-09 18:39:58 +0000683 // incoming color to current stage being processed.
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000684 SkString inColor;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000685
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000686 if (needComputedColor) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000687 this->genInputColor(&builder, &inColor);
junov@google.comf93e7172011-03-31 21:26:24 +0000688 }
689
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000690 // we output point size in the GS if present
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000691 if (fDesc.fEmitsPointSize && !builder.fUsesGS){
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000692 builder.vsCodeAppend("\tgl_PointSize = 1.0;\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000693 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000694
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000695 ///////////////////////////////////////////////////////////////////////////
696 // compute the final color
Scroggo97c88c22011-05-11 14:05:25 +0000697
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000698 // if we have color stages string them together, feeding the output color
699 // of each to the next and generating code for each stage.
700 if (needComputedColor) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000701 SkString outColor;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000702 for (int s = 0; s < fDesc.fFirstCoverageStage; ++s) {
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000703 if (GrGLEffect::kNoEffectKey != fDesc.fEffectKeys[s]) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000704 // create var to hold stage result
705 outColor = "color";
706 outColor.appendS32(s);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000707 builder.fsCodeAppendf("\tvec4 %s;\n", outColor.c_str());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000708
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000709 builder.setCurrentStage(s);
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000710 fEffects[s] = builder.createAndEmitGLEffect(*stages[s],
711 fDesc.fEffectKeys[s],
712 inColor.size() ? inColor.c_str() : NULL,
713 outColor.c_str(),
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000714 &fUniformHandles.fSamplerUnis[s]);
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000715 builder.setNonStage();
junov@google.comf93e7172011-03-31 21:26:24 +0000716 inColor = outColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000717 }
718 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000719 }
Scroggo97c88c22011-05-11 14:05:25 +0000720
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000721 // if have all ones or zeros for the "dst" input to the color filter then we
722 // may be able to make additional optimizations.
723 if (needColorFilterUniform && needComputedColor && !inColor.size()) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000724 GrAssert(Desc::kSolidWhite_ColorInput == fDesc.fColorInput);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000725 bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff ||
726 SkXfermode::kIDA_Coeff == uniformCoeff;
727 if (uniformCoeffIsZero) {
728 uniformCoeff = SkXfermode::kZero_Coeff;
729 bool bogus;
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000730 need_blend_inputs(SkXfermode::kZero_Coeff, colorCoeff,
731 &needColorFilterUniform, &bogus);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000732 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000733 }
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000734 const char* colorFilterColorUniName = NULL;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000735 if (needColorFilterUniform) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000736 fUniformHandles.fColorFilterUni = builder.addUniform(
737 GrGLShaderBuilder::kFragment_ShaderType,
738 kVec4f_GrSLType, "FilterColor",
739 &colorFilterColorUniName);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000740 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000741 bool wroteFragColorZero = false;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000742 if (SkXfermode::kZero_Coeff == uniformCoeff &&
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000743 SkXfermode::kZero_Coeff == colorCoeff) {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000744 builder.fsCodeAppendf("\t%s = %s;\n", colorOutput.getName().c_str(), GrGLSLZerosVecf(4));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000745 wroteFragColorZero = true;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000746 } else if (SkXfermode::kDst_Mode != fDesc.fColorFilterXfermode) {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000747 builder.fsCodeAppend("\tvec4 filteredColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000748 const char* color = adjustInColor(inColor);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000749 add_color_filter(&builder, "filteredColor", uniformCoeff,
750 colorCoeff, colorFilterColorUniName, color);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000751 inColor = "filteredColor";
752 }
753
754 ///////////////////////////////////////////////////////////////////////////
755 // compute the partial coverage (coverage stages and edge aa)
756
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000757 SkString inCoverage;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000758 bool coverageIsZero = Desc::kTransBlack_ColorInput == fDesc.fCoverageInput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000759 // we don't need to compute coverage at all if we know the final shader
760 // output will be zero and we don't have a dual src blend output.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000761 if (!wroteFragColorZero || Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000762
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000763 if (!coverageIsZero) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000764 switch (fDesc.fCoverageInput) {
765 case Desc::kSolidWhite_ColorInput:
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000766 // empty string implies solid white
767 break;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000768 case Desc::kAttribute_ColorInput:
769 gen_attribute_coverage(&builder, &inCoverage);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000770 break;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000771 case Desc::kUniform_ColorInput:
772 this->genUniformCoverage(&builder, &inCoverage);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000773 break;
774 default:
775 GrCrash("Unexpected input coverage.");
776 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000777
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000778 SkString outCoverage;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000779 const int& startStage = fDesc.fFirstCoverageStage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000780 for (int s = startStage; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000781 if (fDesc.fEffectKeys[s]) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000782 // create var to hold stage output
783 outCoverage = "coverage";
784 outCoverage.appendS32(s);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000785 builder.fsCodeAppendf("\tvec4 %s;\n", outCoverage.c_str());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000786
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000787 builder.setCurrentStage(s);
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000788 fEffects[s] = builder.createAndEmitGLEffect(
789 *stages[s],
790 fDesc.fEffectKeys[s],
791 inCoverage.size() ? inCoverage.c_str() : NULL,
792 outCoverage.c_str(),
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000793 &fUniformHandles.fSamplerUnis[s]);
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000794 builder.setNonStage();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000795 inCoverage = outCoverage;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000796 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000797 }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000798
799 // discard if coverage is zero
800 if (fDesc.fDiscardIfOutsideEdge && !outCoverage.isEmpty()) {
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000801 builder.fsCodeAppendf(
802 "\tif (all(lessThanEqual(%s, vec4(0.0)))) {\n\t\tdiscard;\n\t}\n",
803 outCoverage.c_str());
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000804 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000805 }
bsalomon@google.com777c3aa2012-07-25 20:58:20 +0000806
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000807 if (Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) {
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000808 builder.fFSOutputs.push_back().set(kVec4f_GrSLType,
809 GrGLShaderVar::kOut_TypeModifier,
810 dual_source_output_name());
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000811 bool outputIsZero = coverageIsZero;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000812 SkString coeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000813 if (!outputIsZero &&
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000814 Desc::kCoverage_DualSrcOutput != fDesc.fDualSrcOutput && !wroteFragColorZero) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000815 if (!inColor.size()) {
816 outputIsZero = true;
817 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000818 if (Desc::kCoverageISA_DualSrcOutput == fDesc.fDualSrcOutput) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000819 coeff.printf("(1 - %s.a)", inColor.c_str());
820 } else {
821 coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str());
822 }
823 }
824 }
825 if (outputIsZero) {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000826 builder.fsCodeAppendf("\t%s = %s;\n", dual_source_output_name(), GrGLSLZerosVecf(4));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000827 } else {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000828 SkString modulate;
829 GrGLSLModulate4f(&modulate, coeff.c_str(), inCoverage.c_str());
830 builder.fsCodeAppendf("\t%s = %s;\n", dual_source_output_name(), modulate.c_str());
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000831 }
832 dualSourceOutputWritten = true;
833 }
junov@google.comf93e7172011-03-31 21:26:24 +0000834 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000835
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000836 ///////////////////////////////////////////////////////////////////////////
837 // combine color and coverage as frag color
838
839 if (!wroteFragColorZero) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000840 if (coverageIsZero) {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000841 builder.fsCodeAppendf("\t%s = %s;\n", colorOutput.getName().c_str(), GrGLSLZerosVecf(4));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000842 } else {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000843 SkString modulate;
844 GrGLSLModulate4f(&modulate, inColor.c_str(), inCoverage.c_str());
845 builder.fsCodeAppendf("\t%s = %s;\n", colorOutput.getName().c_str(), modulate.c_str());
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000846 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000847 }
848
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000849 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000850 // insert GS
851#if GR_DEBUG
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000852 this->genGeometryShader(&builder);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000853#endif
854
855 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000856 // compile and setup attribs and unis
857
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000858 if (!this->compileShaders(builder)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000859 return false;
860 }
861
bsalomon@google.com17504f52012-10-30 12:34:25 +0000862 if (!this->bindOutputsAttribsAndLinkProgram(builder,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000863 isColorDeclared,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000864 dualSourceOutputWritten)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000865 return false;
866 }
867
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000868 builder.finished(fProgramID);
869 this->initSamplerUniforms();
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000870 fUniformHandles.fRTHeightUni = builder.getRTHeightUniform();
bsalomon@google.com91961302011-05-09 18:39:58 +0000871
872 return true;
873}
874
bsalomon@google.com17504f52012-10-30 12:34:25 +0000875bool GrGLProgram::bindOutputsAttribsAndLinkProgram(const GrGLShaderBuilder& builder,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000876 bool bindColorOut,
877 bool bindDualSrcOut) {
878 GL_CALL_RET(fProgramID, CreateProgram());
879 if (!fProgramID) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000880 return false;
881 }
bsalomon@google.com91961302011-05-09 18:39:58 +0000882
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000883 GL_CALL(AttachShader(fProgramID, fVShaderID));
884 if (fGShaderID) {
885 GL_CALL(AttachShader(fProgramID, fGShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000886 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000887 GL_CALL(AttachShader(fProgramID, fFShaderID));
bsalomon@google.com91961302011-05-09 18:39:58 +0000888
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000889 if (bindColorOut) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000890 GL_CALL(BindFragDataLocation(fProgramID, 0, declared_color_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000891 }
892 if (bindDualSrcOut) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000893 GL_CALL(BindFragDataLocationIndexed(fProgramID, 0, 1, dual_source_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000894 }
895
bsalomon@google.com91961302011-05-09 18:39:58 +0000896 // Bind the attrib locations to same values for all shaders
bsalomon@google.com17504f52012-10-30 12:34:25 +0000897 GL_CALL(BindAttribLocation(fProgramID,
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000898 fDesc.fPositionAttributeIndex,
bsalomon@google.com17504f52012-10-30 12:34:25 +0000899 builder.positionAttribute().c_str()));
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000900 GL_CALL(BindAttribLocation(fProgramID, fDesc.fColorAttributeIndex, COL_ATTR_NAME));
901 GL_CALL(BindAttribLocation(fProgramID, fDesc.fCoverageAttributeIndex, COV_ATTR_NAME));
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000902
bsalomon@google.comc7818882013-03-20 19:19:53 +0000903 if (fDesc.fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit) {
904 GL_CALL(BindAttribLocation(fProgramID,
905 fDesc.fLocalCoordsAttributeIndex,
906 builder.localCoordsAttribute().c_str()));
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000907 }
skia.committer@gmail.comf140f182013-03-02 07:01:56 +0000908
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000909 const GrGLShaderBuilder::AttributePair* attribEnd = builder.getEffectAttributes().end();
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000910 for (const GrGLShaderBuilder::AttributePair* attrib = builder.getEffectAttributes().begin();
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000911 attrib != attribEnd;
912 ++attrib) {
913 GL_CALL(BindAttribLocation(fProgramID, attrib->fIndex, attrib->fName.c_str()));
914 }
915
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000916 GL_CALL(LinkProgram(fProgramID));
bsalomon@google.com91961302011-05-09 18:39:58 +0000917
918 GrGLint linked = GR_GL_INIT_ZERO;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000919 GL_CALL(GetProgramiv(fProgramID, GR_GL_LINK_STATUS, &linked));
bsalomon@google.com91961302011-05-09 18:39:58 +0000920 if (!linked) {
921 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000922 GL_CALL(GetProgramiv(fProgramID, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000923 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
bsalomon@google.com91961302011-05-09 18:39:58 +0000924 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +0000925 // retrieve length even though we don't need it to workaround
926 // bug in chrome cmd buffer param validation.
927 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000928 GL_CALL(GetProgramInfoLog(fProgramID,
bsalomon@google.com96399942012-02-13 14:39:16 +0000929 infoLen+1,
930 &length,
931 (char*)log.get()));
bsalomon@google.com91961302011-05-09 18:39:58 +0000932 GrPrintf((char*)log.get());
933 }
934 GrAssert(!"Error linking program");
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000935 GL_CALL(DeleteProgram(fProgramID));
936 fProgramID = 0;
bsalomon@google.com91961302011-05-09 18:39:58 +0000937 return false;
938 }
939 return true;
940}
941
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000942void GrGLProgram::initSamplerUniforms() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000943 GL_CALL(UseProgram(fProgramID));
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000944 // We simply bind the uniforms to successive texture units beginning at 0. setData() assumes this
945 // behavior.
946 GrGLint texUnitIdx = 0;
tomhudson@google.com93813632011-10-27 20:21:16 +0000947 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000948 int numSamplers = fUniformHandles.fSamplerUnis[s].count();
949 for (int u = 0; u < numSamplers; ++u) {
950 UniformHandle handle = fUniformHandles.fSamplerUnis[s][u];
951 if (GrGLUniformManager::kInvalidUniformHandle != handle) {
952 fUniformManager.setSampler(handle, texUnitIdx);
953 ++texUnitIdx;
bsalomon@google.com0982d352012-07-31 15:33:25 +0000954 }
bsalomon@google.com91961302011-05-09 18:39:58 +0000955 }
bsalomon@google.com91961302011-05-09 18:39:58 +0000956 }
bsalomon@google.com91961302011-05-09 18:39:58 +0000957}
958
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000959///////////////////////////////////////////////////////////////////////////////
junov@google.comf93e7172011-03-31 21:26:24 +0000960
bsalomon@google.com91207482013-02-12 21:45:24 +0000961void GrGLProgram::setData(GrGpuGL* gpu,
962 GrColor color,
963 GrColor coverage,
964 SharedGLState* sharedState) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000965 const GrDrawState& drawState = gpu->getDrawState();
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000966
bsalomon@google.com91207482013-02-12 21:45:24 +0000967 this->setColor(drawState, color, sharedState);
968 this->setCoverage(drawState, coverage, sharedState);
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000969 this->setMatrixAndRenderTargetHeight(drawState);
bsalomon@google.com91207482013-02-12 21:45:24 +0000970
971 // Setup the SkXfermode::Mode-based colorfilter uniform if necessary
972 if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fColorFilterUni &&
973 fColorFilterColor != drawState.getColorFilterColor()) {
974 GrGLfloat c[4];
975 GrColorToRGBAFloat(drawState.getColorFilterColor(), c);
976 fUniformManager.set4fv(fUniformHandles.fColorFilterUni, 0, 1, c);
977 fColorFilterColor = drawState.getColorFilterColor();
978 }
979
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000980 GrGLint texUnitIdx = 0;
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000981 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com46fba0d2012-10-25 21:42:05 +0000982 if (NULL != fEffects[s]) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000983 const GrEffectStage& stage = drawState.getStage(s);
984 GrAssert(NULL != stage.getEffect());
bsalomon@google.comc7818882013-03-20 19:19:53 +0000985
986 bool explicitLocalCoords =
987 (fDesc.fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit);
988 GrDrawEffect drawEffect(stage, explicitLocalCoords);
989 fEffects[s]->setData(fUniformManager, drawEffect);
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000990 int numSamplers = fUniformHandles.fSamplerUnis[s].count();
991 for (int u = 0; u < numSamplers; ++u) {
992 UniformHandle handle = fUniformHandles.fSamplerUnis[s][u];
993 if (GrGLUniformManager::kInvalidUniformHandle != handle) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000994 const GrTextureAccess& access = (*stage.getEffect())->textureAccess(u);
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000995 GrGLTexture* texture = static_cast<GrGLTexture*>(access.getTexture());
996 gpu->bindTexture(texUnitIdx, access.getParams(), texture);
997 ++texUnitIdx;
998 }
999 }
bsalomon@google.com4285acc2012-10-22 14:11:24 +00001000 }
1001 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +00001002}
bsalomon@google.com91207482013-02-12 21:45:24 +00001003
1004void GrGLProgram::setColor(const GrDrawState& drawState,
1005 GrColor color,
1006 SharedGLState* sharedState) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001007 if (!(drawState.getAttribBindings() & GrDrawState::kColor_AttribBindingsBit)) {
bsalomon@google.com91207482013-02-12 21:45:24 +00001008 switch (fDesc.fColorInput) {
1009 case GrGLProgram::Desc::kAttribute_ColorInput:
1010 if (sharedState->fConstAttribColor != color) {
1011 // OpenGL ES only supports the float varieties of glVertexAttrib
1012 GrGLfloat c[4];
1013 GrColorToRGBAFloat(color, c);
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001014 GL_CALL(VertexAttrib4fv(fDesc.fColorAttributeIndex, c));
bsalomon@google.com91207482013-02-12 21:45:24 +00001015 sharedState->fConstAttribColor = color;
1016 }
1017 break;
1018 case GrGLProgram::Desc::kUniform_ColorInput:
1019 if (fColor != color) {
1020 // OpenGL ES doesn't support unsigned byte varieties of glUniform
1021 GrGLfloat c[4];
1022 GrColorToRGBAFloat(color, c);
1023 GrAssert(GrGLUniformManager::kInvalidUniformHandle !=
1024 fUniformHandles.fColorUni);
1025 fUniformManager.set4fv(fUniformHandles.fColorUni, 0, 1, c);
1026 fColor = color;
1027 }
1028 break;
1029 case GrGLProgram::Desc::kSolidWhite_ColorInput:
1030 case GrGLProgram::Desc::kTransBlack_ColorInput:
1031 break;
1032 default:
1033 GrCrash("Unknown color type.");
1034 }
1035 }
1036}
1037
1038void GrGLProgram::setCoverage(const GrDrawState& drawState,
1039 GrColor coverage,
1040 SharedGLState* sharedState) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001041 if (!(drawState.getAttribBindings() & GrDrawState::kCoverage_AttribBindingsBit)) {
bsalomon@google.com91207482013-02-12 21:45:24 +00001042 switch (fDesc.fCoverageInput) {
1043 case Desc::kAttribute_ColorInput:
1044 if (sharedState->fConstAttribCoverage != coverage) {
1045 // OpenGL ES only supports the float varieties of glVertexAttrib
1046 GrGLfloat c[4];
1047 GrColorToRGBAFloat(coverage, c);
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001048 GL_CALL(VertexAttrib4fv(fDesc.fCoverageAttributeIndex, c));
bsalomon@google.com91207482013-02-12 21:45:24 +00001049 sharedState->fConstAttribCoverage = coverage;
1050 }
1051 break;
1052 case Desc::kUniform_ColorInput:
1053 if (fCoverage != coverage) {
1054 // OpenGL ES doesn't support unsigned byte varieties of glUniform
1055 GrGLfloat c[4];
1056 GrColorToRGBAFloat(coverage, c);
1057 GrAssert(GrGLUniformManager::kInvalidUniformHandle !=
1058 fUniformHandles.fCoverageUni);
1059 fUniformManager.set4fv(fUniformHandles.fCoverageUni, 0, 1, c);
1060 fCoverage = coverage;
1061 }
1062 break;
1063 case Desc::kSolidWhite_ColorInput:
1064 case Desc::kTransBlack_ColorInput:
1065 break;
1066 default:
1067 GrCrash("Unknown coverage type.");
1068 }
1069 }
1070}
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +00001071
1072void GrGLProgram::setMatrixAndRenderTargetHeight(const GrDrawState& drawState) {
1073 const GrRenderTarget* rt = drawState.getRenderTarget();
1074 SkISize size;
1075 size.set(rt->width(), rt->height());
1076
1077 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
1078 if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fRTHeightUni &&
1079 fMatrixState.fRenderTargetSize.fHeight != size.fHeight) {
1080 fUniformManager.set1f(fUniformHandles.fRTHeightUni, SkIntToScalar(size.fHeight));
1081 }
1082
1083 if (fMatrixState.fRenderTargetOrigin != rt->origin() ||
1084 !fMatrixState.fViewMatrix.cheapEqualTo(drawState.getViewMatrix()) ||
1085 fMatrixState.fRenderTargetSize != size) {
1086 SkMatrix m;
1087 if (kBottomLeft_GrSurfaceOrigin == rt->origin()) {
1088 m.setAll(
1089 SkIntToScalar(2) / size.fWidth, 0, -SK_Scalar1,
1090 0,-SkIntToScalar(2) / size.fHeight, SK_Scalar1,
1091 0, 0, SkMatrix::I()[8]);
1092 } else {
1093 m.setAll(
1094 SkIntToScalar(2) / size.fWidth, 0, -SK_Scalar1,
1095 0, SkIntToScalar(2) / size.fHeight,-SK_Scalar1,
1096 0, 0, SkMatrix::I()[8]);
1097 }
1098 m.setConcat(m, drawState.getViewMatrix());
1099
1100 // ES doesn't allow you to pass true to the transpose param so we do our own transpose.
1101 GrGLfloat mt[] = {
1102 SkScalarToFloat(m[SkMatrix::kMScaleX]),
1103 SkScalarToFloat(m[SkMatrix::kMSkewY]),
1104 SkScalarToFloat(m[SkMatrix::kMPersp0]),
1105 SkScalarToFloat(m[SkMatrix::kMSkewX]),
1106 SkScalarToFloat(m[SkMatrix::kMScaleY]),
1107 SkScalarToFloat(m[SkMatrix::kMPersp1]),
1108 SkScalarToFloat(m[SkMatrix::kMTransX]),
1109 SkScalarToFloat(m[SkMatrix::kMTransY]),
1110 SkScalarToFloat(m[SkMatrix::kMPersp2])
1111 };
1112 fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, mt);
1113 fMatrixState.fViewMatrix = drawState.getViewMatrix();
1114 fMatrixState.fRenderTargetSize = size;
1115 fMatrixState.fRenderTargetOrigin = rt->origin();
1116 }
1117}