blob: d14b1e82618b011e02b40ddc3809920a3dd38020 [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
bsalomon@google.com5739d2c2012-05-31 15:07:19 +00008#include "GrGpuGL.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.coma469c282012-10-24 18:28:34 +000010#include "GrEffect.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +000011#include "GrGLEffect.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000012#include "GrGpuVertex.h"
junov@google.comf93e7172011-03-31 21:26:24 +000013
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000014typedef GrGLUniformManager::UniformHandle UniformHandle;
15static const UniformHandle kInvalidUniformHandle = GrGLUniformManager::kInvalidUniformHandle;
16
junov@google.comf93e7172011-03-31 21:26:24 +000017#define SKIP_CACHE_CHECK true
18#define GR_UINT32_MAX static_cast<uint32_t>(-1)
19
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000020GrGpuGL::ProgramCache::ProgramCache(const GrGLContextInfo& gl)
21 : fCount(0)
22 , fCurrLRUStamp(0)
23 , fGL(gl) {
24}
junov@google.comf93e7172011-03-31 21:26:24 +000025
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000026void GrGpuGL::ProgramCache::abandon() {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000027 for (int i = 0; i < fCount; ++i) {
28 GrAssert(NULL != fEntries[i].fProgram.get());
29 fEntries[i].fProgram->abandon();
30 fEntries[i].fProgram.reset(NULL);
31 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000032 fCount = 0;
33}
34
bsalomon@google.comcddaf342012-07-30 13:09:05 +000035GrGLProgram* GrGpuGL::ProgramCache::getProgram(const ProgramDesc& desc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000036 const GrEffectStage* stages[]) {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000037 Entry newEntry;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000038 newEntry.fKey.setKeyData(desc.asKey());
39
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000040 Entry* entry = fHashCache.find(newEntry.fKey);
41 if (NULL == entry) {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000042 newEntry.fProgram.reset(GrGLProgram::Create(fGL, desc, stages));
43 if (NULL == newEntry.fProgram.get()) {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000044 return NULL;
45 }
46 if (fCount < kMaxEntries) {
47 entry = fEntries + fCount;
48 ++fCount;
49 } else {
50 GrAssert(kMaxEntries == fCount);
51 entry = fEntries;
52 for (int i = 1; i < kMaxEntries; ++i) {
53 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
54 entry = fEntries + i;
junov@google.comf93e7172011-03-31 21:26:24 +000055 }
junov@google.comf93e7172011-03-31 21:26:24 +000056 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000057 fHashCache.remove(entry->fKey, entry);
junov@google.comf93e7172011-03-31 21:26:24 +000058 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000059 *entry = newEntry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000060 fHashCache.insert(entry->fKey, entry);
junov@google.comf93e7172011-03-31 21:26:24 +000061 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000062
63 entry->fLRUStamp = fCurrLRUStamp;
64 if (GR_UINT32_MAX == fCurrLRUStamp) {
65 // wrap around! just trash our LRU, one time hit.
66 for (int i = 0; i < fCount; ++i) {
67 fEntries[i].fLRUStamp = 0;
68 }
69 }
70 ++fCurrLRUStamp;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000071 return entry->fProgram;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000072}
junov@google.comf93e7172011-03-31 21:26:24 +000073
bsalomon@google.com1e257a52011-07-06 19:52:16 +000074////////////////////////////////////////////////////////////////////////////////
75
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000076void GrGpuGL::abandonResources(){
77 INHERITED::abandonResources();
78 fProgramCache->abandon();
79 fHWProgramID = 0;
80}
81
82////////////////////////////////////////////////////////////////////////////////
83
bsalomon@google.com0b77d682011-08-19 13:28:54 +000084#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
85
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000086void GrGpuGL::flushViewMatrix(DrawType type) {
bsalomon@google.com4c883782012-06-04 19:05:11 +000087 const GrGLRenderTarget* rt = static_cast<const GrGLRenderTarget*>(this->getDrawState().getRenderTarget());
88 SkISize viewportSize;
89 const GrGLIRect& viewport = rt->getViewport();
90 viewportSize.set(viewport.fWidth, viewport.fHeight);
junov@google.comf93e7172011-03-31 21:26:24 +000091
bsalomon@google.comb9086a02012-11-01 18:02:54 +000092 const SkMatrix& vm = this->getDrawState().getViewMatrix();
bsalomon@google.com4c883782012-06-04 19:05:11 +000093
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000094 if (kStencilPath_DrawType == type) {
bsalomon@google.com05a718c2012-06-29 14:01:53 +000095 if (fHWPathMatrixState.fViewMatrix != vm ||
96 fHWPathMatrixState.fRTSize != viewportSize) {
97 // rescale the coords from skia's "device" coords to GL's normalized coords,
98 // and perform a y-flip.
bsalomon@google.comb9086a02012-11-01 18:02:54 +000099 SkMatrix m;
bsalomon@google.com81712882012-11-01 17:12:34 +0000100 m.setScale(SkIntToScalar(2) / rt->width(), SkIntToScalar(-2) / rt->height());
101 m.postTranslate(-SK_Scalar1, SK_Scalar1);
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000102 m.preConcat(vm);
103
104 // GL wants a column-major 4x4.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000105 GrGLfloat mv[] = {
106 // col 0
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000107 SkScalarToFloat(m[SkMatrix::kMScaleX]),
108 SkScalarToFloat(m[SkMatrix::kMSkewY]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000109 0,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000110 SkScalarToFloat(m[SkMatrix::kMPersp0]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000111
112 // col 1
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000113 SkScalarToFloat(m[SkMatrix::kMSkewX]),
114 SkScalarToFloat(m[SkMatrix::kMScaleY]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000115 0,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000116 SkScalarToFloat(m[SkMatrix::kMPersp1]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000117
118 // col 2
119 0, 0, 0, 0,
120
121 // col3
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000122 SkScalarToFloat(m[SkMatrix::kMTransX]),
123 SkScalarToFloat(m[SkMatrix::kMTransY]),
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000124 0.0f,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000125 SkScalarToFloat(m[SkMatrix::kMPersp2])
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000126 };
127 GL_CALL(MatrixMode(GR_GL_PROJECTION));
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000128 GL_CALL(LoadMatrixf(mv));
129 fHWPathMatrixState.fViewMatrix = vm;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000130 fHWPathMatrixState.fRTSize = viewportSize;
131 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000132 } else if (!fCurrentProgram->fViewMatrix.cheapEqualTo(vm) ||
133 fCurrentProgram->fViewportSize != viewportSize) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000134 SkMatrix m;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000135 m.setAll(
bsalomon@google.com81712882012-11-01 17:12:34 +0000136 SkIntToScalar(2) / viewportSize.fWidth, 0, -SK_Scalar1,
137 0,-SkIntToScalar(2) / viewportSize.fHeight, SK_Scalar1,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000138 0, 0, SkMatrix::I()[8]);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000139 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000140
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000141 // ES doesn't allow you to pass true to the transpose param,
142 // so do our own transpose
143 GrGLfloat mt[] = {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000144 SkScalarToFloat(m[SkMatrix::kMScaleX]),
145 SkScalarToFloat(m[SkMatrix::kMSkewY]),
146 SkScalarToFloat(m[SkMatrix::kMPersp0]),
147 SkScalarToFloat(m[SkMatrix::kMSkewX]),
148 SkScalarToFloat(m[SkMatrix::kMScaleY]),
149 SkScalarToFloat(m[SkMatrix::kMPersp1]),
150 SkScalarToFloat(m[SkMatrix::kMTransX]),
151 SkScalarToFloat(m[SkMatrix::kMTransY]),
152 SkScalarToFloat(m[SkMatrix::kMPersp2])
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000153 };
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000154 fCurrentProgram->fUniformManager.setMatrix3f(fCurrentProgram->fUniforms.fViewMatrixUni, mt);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000155 fCurrentProgram->fViewMatrix = vm;
156 fCurrentProgram->fViewportSize = viewportSize;
bsalomon@google.com91961302011-05-09 18:39:58 +0000157 }
junov@google.comf93e7172011-03-31 21:26:24 +0000158}
159
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000160///////////////////////////////////////////////////////////////////////////////
junov@google.com6acc9b32011-05-16 18:32:07 +0000161
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000162// helpers for texture matrices
junov@google.com6acc9b32011-05-16 18:32:07 +0000163
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000164void GrGpuGL::AdjustTextureMatrix(const GrTexture* texture, SkMatrix* matrix) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000165 GrAssert(NULL != texture);
166 GrAssert(NULL != matrix);
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000167 if (GrSurface::kBottomLeft_Origin == texture->origin()) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000168 SkMatrix invY;
bsalomon@google.com81712882012-11-01 17:12:34 +0000169 invY.setAll(SK_Scalar1, 0, 0,
170 0, -SK_Scalar1, SK_Scalar1,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000171 0, 0, SkMatrix::I()[8]);
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000172 matrix->postConcat(invY);
junov@google.com6acc9b32011-05-16 18:32:07 +0000173 }
174}
175
bsalomon@google.com288d9542012-10-17 12:53:54 +0000176int GrGpuGL::TextureMatrixOptFlags(const GrGLTexture* texture,
bsalomon@google.com08283af2012-10-26 13:01:20 +0000177 const GrEffectStage& stage) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000178 GrAssert(NULL != texture);
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000179 SkMatrix matrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000180 stage.getTotalMatrix(&matrix);
bsalomon@google.com288d9542012-10-17 12:53:54 +0000181
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000182 bool canBeIndentity = GrSurface::kTopLeft_Origin == texture->origin();
bsalomon@google.com288d9542012-10-17 12:53:54 +0000183
184 if (canBeIndentity && matrix.isIdentity()) {
185 return GrGLProgram::StageDesc::kIdentityMatrix_OptFlagBit;
186 } else if (!matrix.hasPerspective()) {
187 return GrGLProgram::StageDesc::kNoPerspective_OptFlagBit;
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000188 }
bsalomon@google.com288d9542012-10-17 12:53:54 +0000189 return 0;
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000190}
191
192///////////////////////////////////////////////////////////////////////////////
193
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000194void GrGpuGL::flushTextureMatrix(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000195 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000196
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000197 // FIXME: Still assuming only a single texture per effect
bsalomon@google.com08283af2012-10-26 13:01:20 +0000198 const GrEffect* effect = drawState.getStage(s).getEffect();
bsalomon@google.com021fc732012-10-25 12:47:42 +0000199 if (0 == effect->numTextures()) {
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000200 return;
201 }
bsalomon@google.com021fc732012-10-25 12:47:42 +0000202 const GrGLTexture* texture = static_cast<const GrGLTexture*>(effect->texture(0));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000203 if (NULL != texture) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000204
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000205 bool originChange = fCurrentProgram->fTextureOrigin[s] != texture->origin();
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000206
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000207 UniformHandle matrixUni = fCurrentProgram->fUniforms.fStages[s].fTextureMatrixUni;
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000208
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000209 const SkMatrix& hwMatrix = fCurrentProgram->fTextureMatrices[s];
210 SkMatrix samplerMatrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000211 drawState.getStage(s).getTotalMatrix(&samplerMatrix);
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000212
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000213 if (kInvalidUniformHandle != matrixUni &&
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000214 (originChange || !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000215
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000216 SkMatrix m = samplerMatrix;
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000217 AdjustTextureMatrix(texture, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000218
bsalomon@google.com91961302011-05-09 18:39:58 +0000219 // ES doesn't allow you to pass true to the transpose param,
220 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000221 GrGLfloat mt[] = {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000222 SkScalarToFloat(m[SkMatrix::kMScaleX]),
223 SkScalarToFloat(m[SkMatrix::kMSkewY]),
224 SkScalarToFloat(m[SkMatrix::kMPersp0]),
225 SkScalarToFloat(m[SkMatrix::kMSkewX]),
226 SkScalarToFloat(m[SkMatrix::kMScaleY]),
227 SkScalarToFloat(m[SkMatrix::kMPersp1]),
228 SkScalarToFloat(m[SkMatrix::kMTransX]),
229 SkScalarToFloat(m[SkMatrix::kMTransY]),
230 SkScalarToFloat(m[SkMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000231 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000232
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000233 fCurrentProgram->fUniformManager.setMatrix3f(matrixUni, mt);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000234 fCurrentProgram->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000235 }
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000236
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000237 fCurrentProgram->fTextureOrigin[s] = texture->origin();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000238 }
junov@google.comf93e7172011-03-31 21:26:24 +0000239}
240
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000241void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000242 const ProgramDesc& desc = fCurrentProgram->getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000243 const GrDrawState& drawState = this->getDrawState();
244
bsalomon@google.come79c8152012-03-29 19:07:12 +0000245 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000246 // color will be specified per-vertex as an attribute
247 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000248 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000249 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000250 switch (desc.fColorInput) {
251 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000252 if (fHWConstAttribColor != color) {
bsalomon@google.com75347472012-09-17 17:23:21 +0000253 // OpenGL ES only supports the float varieties of glVertexAttrib
254 GrGLfloat c[4];
255 GrColorToRGBAFloat(color, c);
256 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(), c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000257 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000258 }
259 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000260 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000261 if (fCurrentProgram->fColor != color) {
bsalomon@google.com75347472012-09-17 17:23:21 +0000262 // OpenGL ES doesn't support unsigned byte varieties of glUniform
263 GrGLfloat c[4];
264 GrColorToRGBAFloat(color, c);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000265 GrAssert(kInvalidUniformHandle != fCurrentProgram->fUniforms.fColorUni);
266 fCurrentProgram->fUniformManager.set4fv(fCurrentProgram->fUniforms.fColorUni,
267 0, 1, c);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000268 fCurrentProgram->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000269 }
270 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000271 case ProgramDesc::kSolidWhite_ColorInput:
272 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000273 break;
274 default:
275 GrCrash("Unknown color type.");
276 }
277 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000278 UniformHandle filterColorUni = fCurrentProgram->fUniforms.fColorFilterUni;
279 if (kInvalidUniformHandle != filterColorUni &&
280 fCurrentProgram->fColorFilterColor != drawState.getColorFilterColor()) {
bsalomon@google.com75347472012-09-17 17:23:21 +0000281 GrGLfloat c[4];
282 GrColorToRGBAFloat(drawState.getColorFilterColor(), c);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000283 fCurrentProgram->fUniformManager.set4fv(filterColorUni, 0, 1, c);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000284 fCurrentProgram->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000285 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000286}
287
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000288void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000289 const ProgramDesc& desc = fCurrentProgram->getDesc();
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000290 // const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000291
292
bsalomon@google.come79c8152012-03-29 19:07:12 +0000293 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000294 // coverage will be specified per-vertex as an attribute
295 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000296 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000297 } else {
298 switch (desc.fCoverageInput) {
299 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000300 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000301 // OpenGL ES only supports the float varieties of
302 // glVertexAttrib
bsalomon@google.com75347472012-09-17 17:23:21 +0000303 GrGLfloat c[4];
304 GrColorToRGBAFloat(coverage, c);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000305 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000306 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000307 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000308 }
309 break;
310 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000311 if (fCurrentProgram->fCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000312 // OpenGL ES doesn't support unsigned byte varieties of
313 // glUniform
bsalomon@google.com75347472012-09-17 17:23:21 +0000314 GrGLfloat c[4];
315 GrColorToRGBAFloat(coverage, c);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000316 GrAssert(kInvalidUniformHandle != fCurrentProgram->fUniforms.fCoverageUni);
317 fCurrentProgram->fUniformManager.set4fv(fCurrentProgram->fUniforms.fCoverageUni,
318 0, 1, c);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000319 fCurrentProgram->fCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000320 }
321 break;
322 case ProgramDesc::kSolidWhite_ColorInput:
323 case ProgramDesc::kTransBlack_ColorInput:
324 break;
325 default:
326 GrCrash("Unknown coverage type.");
327 }
328 }
329}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000330
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000331bool GrGpuGL::flushGraphicsState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000332 const GrDrawState& drawState = this->getDrawState();
333
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000334 // GrGpu::setupClipAndFlushState should have already checked this
335 // and bailed if not true.
336 GrAssert(NULL != drawState.getRenderTarget());
337
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000338 if (kStencilPath_DrawType != type) {
339 this->flushMiscFixedFunctionState();
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000340
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000341 GrBlendCoeff srcCoeff;
342 GrBlendCoeff dstCoeff;
343 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
344 if (kSkipDraw_BlendOptFlag & blendOpts) {
345 return false;
346 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000347
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000348 const GrEffectStage* stages[GrDrawState::kNumStages];
349 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
350 stages[i] = drawState.isStageEnabled(i) ? &drawState.getStage(i) : NULL;
351 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000352 GrGLProgram::Desc desc;
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000353 this->buildProgram(kDrawPoints_DrawType == type, blendOpts, dstCoeff, &desc);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000354
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000355 fCurrentProgram.reset(fProgramCache->getProgram(desc, stages));
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000356 if (NULL == fCurrentProgram.get()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000357 GrAssert(!"Failed to create program!");
358 return false;
359 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000360 fCurrentProgram.get()->ref();
junov@google.comf93e7172011-03-31 21:26:24 +0000361
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000362 if (fHWProgramID != fCurrentProgram->fProgramID) {
363 GL_CALL(UseProgram(fCurrentProgram->fProgramID));
364 fHWProgramID = fCurrentProgram->fProgramID;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000365 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000366 fCurrentProgram->overrideBlend(&srcCoeff, &dstCoeff);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000367 this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000368
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000369 GrColor color;
370 GrColor coverage;
371 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
372 color = 0;
373 coverage = 0;
374 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
375 color = 0xffffffff;
376 coverage = drawState.getCoverage();
377 } else {
378 color = drawState.getColor();
379 coverage = drawState.getCoverage();
380 }
381 this->flushColor(color);
382 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000383
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000384 fCurrentProgram->setData(drawState);
385
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000386 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
387 if (this->isStageEnabled(s)) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000388 this->flushBoundTextureAndParams(s);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000389
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000390 this->flushTextureMatrix(s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000391 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000392 }
junov@google.comf93e7172011-03-31 21:26:24 +0000393 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000394 this->flushStencil(type);
395 this->flushViewMatrix(type);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000396 this->flushScissor();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000397 this->flushAAState(type);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000398
robertphillips@google.com7b112892012-07-31 15:18:21 +0000399 GrIRect* devRect = NULL;
400 GrIRect devClipBounds;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000401 if (drawState.isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000402 fClip->getConservativeBounds(drawState.getRenderTarget(),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000403 &devClipBounds);
404 devRect = &devClipBounds;
bsalomon@google.com4c883782012-06-04 19:05:11 +0000405 }
406 // This must come after textures are flushed because a texture may need
407 // to be msaa-resolved (which will modify bound FBO state).
robertphillips@google.com7b112892012-07-31 15:18:21 +0000408 this->flushRenderTarget(devRect);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000409
junov@google.comf93e7172011-03-31 21:26:24 +0000410 return true;
411}
412
bsalomon@google.com2717d562012-05-07 19:10:52 +0000413#if GR_TEXT_SCALAR_IS_USHORT
414 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
415 #define TEXT_COORDS_ARE_NORMALIZED 1
416#elif GR_TEXT_SCALAR_IS_FLOAT
417 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
418 #define TEXT_COORDS_ARE_NORMALIZED 0
419#elif GR_TEXT_SCALAR_IS_FIXED
420 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
421 #define TEXT_COORDS_ARE_NORMALIZED 0
422#else
423 #error "unknown GR_TEXT_SCALAR type"
424#endif
425
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000426void GrGpuGL::setupGeometry(int* startVertex,
bsalomon@google.com49209392012-06-05 15:13:46 +0000427 int* startIndex,
428 int vertexCount,
429 int indexCount) {
junov@google.comf93e7172011-03-31 21:26:24 +0000430
431 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000432 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000433 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000434 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000435
bsalomon@google.come79c8152012-03-29 19:07:12 +0000436 GrVertexLayout currLayout = this->getVertexLayout();
437
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000438 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000439 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000440 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000441 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000442 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000443 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000444 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000445 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000446 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000447 int oldEdgeOffset;
448
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000449 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
450 fHWGeometryState.fVertexLayout,
451 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000452 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000453 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000454 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000455 bool indexed = NULL != startIndex;
456
457 int extraVertexOffset;
458 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000459 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000460
461 GrGLenum scalarType;
462 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000463 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000464 scalarType = TEXT_COORDS_GL_TYPE;
465 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000466 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +0000467 GR_STATIC_ASSERT(SK_SCALAR_IS_FLOAT);
bsalomon@google.com2717d562012-05-07 19:10:52 +0000468 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000469 texCoordNorm = false;
470 }
471
472 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
473 *startVertex = 0;
474 if (indexed) {
475 *startIndex += extraIndexOffset;
476 }
477
478 // all the Pointers must be set if any of these are true
479 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
480 vertexOffset != fHWGeometryState.fVertexOffset ||
481 newStride != oldStride;
482
483 // position and tex coord offsets change if above conditions are true
484 // or the type/normalization changed based on text vs nontext type coords.
485 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000486 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000487 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000488 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000489
490 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000491 int idx = GrGLProgram::PositionAttributeIdx();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000492 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000493 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000494 fHWGeometryState.fVertexOffset = vertexOffset;
495 }
496
tomhudson@google.com93813632011-10-27 20:21:16 +0000497 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000498 if (newTexCoordOffsets[t] > 0) {
499 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000500 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000501 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000502 GL_CALL(EnableVertexAttribArray(idx));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000503 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000504 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000505 } else if (posAndTexChange ||
506 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000507 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000508 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000509 }
510 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000511 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000512 }
513 }
514
515 if (newColorOffset > 0) {
516 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000517 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000518 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000519 GL_CALL(EnableVertexAttribArray(idx));
520 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000521 true, newStride, colorOffset));
522 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000523 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000524 true, newStride, colorOffset));
525 }
526 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000527 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000528 }
529
bsalomon@google.coma3108262011-10-10 14:08:47 +0000530 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000531 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000532 int idx = GrGLProgram::CoverageAttributeIdx();
533 if (oldCoverageOffset <= 0) {
534 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000535 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000536 true, newStride, coverageOffset));
537 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000538 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000539 true, newStride, coverageOffset));
540 }
541 } else if (oldCoverageOffset > 0) {
542 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
543 }
544
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000545 if (newEdgeOffset > 0) {
546 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
547 int idx = GrGLProgram::EdgeAttributeIdx();
548 if (oldEdgeOffset <= 0) {
549 GL_CALL(EnableVertexAttribArray(idx));
550 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
551 false, newStride, edgeOffset));
552 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
553 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
554 false, newStride, edgeOffset));
555 }
556 } else if (oldEdgeOffset > 0) {
557 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
558 }
559
bsalomon@google.come79c8152012-03-29 19:07:12 +0000560 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000561 fHWGeometryState.fArrayPtrsDirty = false;
562}
563
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000564void GrGpuGL::buildProgram(bool isPoints,
bsalomon@google.com49209392012-06-05 15:13:46 +0000565 BlendOptFlags blendOpts,
566 GrBlendCoeff dstCoeff,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000567 ProgramDesc* desc) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000568 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000569
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000570 // This should already have been caught
571 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
572
573 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
574
575 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
576 kEmitCoverage_BlendOptFlag));
577
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000578 // The descriptor is used as a cache key. Thus when a field of the
579 // descriptor will not affect program generation (because of the vertex
580 // layout in use or other descriptor field settings) it should be set
581 // to a canonical value to avoid duplicate programs with different keys.
582
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000583 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000584 desc->fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000585
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000586 desc->fEmitsPointSize = isPoints;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000587
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000588 bool requiresAttributeColors = !skipColor &&
589 SkToBool(desc->fVertexLayout & kColor_VertexLayoutBit);
590 bool requiresAttributeCoverage = !skipCoverage &&
591 SkToBool(desc->fVertexLayout & kCoverage_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000592
593 // fColorInput/fCoverageInput records how colors are specified for the.
594 // program. So we strip the bits from the layout to avoid false negatives
595 // when searching for an existing program in the cache.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000596 desc->fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000597
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000598 desc->fColorFilterXfermode = skipColor ?
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000599 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000600 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000601
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000602 // no reason to do edge aa or look at per-vertex coverage if coverage is
603 // ignored
604 if (skipCoverage) {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000605 desc->fVertexLayout &= ~(kEdge_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000606 }
607
608 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
609 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000610 (!requiresAttributeColors && 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000611 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000612 desc->fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000613 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000614 desc->fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000615 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000616 desc->fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000617 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000618 desc->fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000619 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000620
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000621 bool covIsSolidWhite = !requiresAttributeCoverage && 0xffffffff == drawState.getCoverage();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000622
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000623 if (skipCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000624 desc->fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000625 } else if (covIsSolidWhite) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000626 desc->fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000627 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000628 desc->fCoverageInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000629 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000630 desc->fCoverageInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000631 }
junov@google.comf93e7172011-03-31 21:26:24 +0000632
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000633 int lastEnabledStage = -1;
634
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000635 if (!skipCoverage && (desc->fVertexLayout &GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000636 desc->fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000637 } else {
638 // use canonical value when not set to avoid cache misses
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000639 desc->fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000640 }
641
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000642 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000643 StageDesc& stageDesc = desc->fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000644
bsalomon@google.com08283af2012-10-26 13:01:20 +0000645 stageDesc.fOptFlags = 0;
646 stageDesc.setEnabled(this->isStageEnabled(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000647
648 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000649 skipCoverage;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000650
bsalomon@google.com08283af2012-10-26 13:01:20 +0000651 if (!skip && stageDesc.isEnabled()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000652 lastEnabledStage = s;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000653 const GrEffectStage& stage = drawState.getStage(s);
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000654 // FIXME: Still assuming one texture per effect
bsalomon@google.com08283af2012-10-26 13:01:20 +0000655 const GrEffect* effect = drawState.getStage(s).getEffect();
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000656
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000657 if (effect->numTextures() > 0) {
658 const GrGLTexture* texture = static_cast<const GrGLTexture*>(effect->texture(0));
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000659 SkMatrix samplerMatrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000660 stage.getTotalMatrix(&samplerMatrix);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000661 if (NULL != texture) {
662 // We call this helper function rather then simply checking the client-specified
663 // texture matrix. This is because we may have to concat a y-inversion to account
664 // for texture orientation.
bsalomon@google.com08283af2012-10-26 13:01:20 +0000665 stageDesc.fOptFlags |= TextureMatrixOptFlags(texture, stage);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000666 }
667 } else {
668 // Set identity to do the minimal amount of extra work for the no texture case.
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000669 // This will go away when effects manage their own texture matrix.
bsalomon@google.com08283af2012-10-26 13:01:20 +0000670 stageDesc.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000671 }
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000672 const GrBackendEffectFactory& factory = effect->getFactory();
673 stageDesc.fEffectKey = factory.glEffectKey(stage, this->glCaps());
junov@google.comf93e7172011-03-31 21:26:24 +0000674 } else {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000675 stageDesc.fOptFlags = 0;
676 stageDesc.fEffectKey = 0;
junov@google.comf93e7172011-03-31 21:26:24 +0000677 }
678 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000679
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000680 desc->fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000681
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000682 // Currently the experimental GS will only work with triangle prims (and it doesn't do anything
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000683 // other than pass through values from the VS to the FS anyway).
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000684#if 0 && GR_GL_EXPERIMENTAL_GS
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000685 desc->fExperimentalGS = this->getCaps().fGeometryShaderSupport;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000686#endif
687
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000688 // We want to avoid generating programs with different "first cov stage" values when they would
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000689 // compute the same result. We set field in the desc to kNumStages when either there are no
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000690 // coverage stages or the distinction between coverage and color is immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000691 int firstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000692 desc->fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000693 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000694 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000695 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000696 }
697
698 // other coverage inputs
699 if (!hasCoverage) {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000700 hasCoverage = requiresAttributeCoverage ||
701 (desc->fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000702 }
703
704 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000705 // color filter is applied between color/coverage computation
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000706 if (SkXfermode::kDst_Mode != desc->fColorFilterXfermode) {
707 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000708 }
709
bsalomon@google.comf6601872012-08-28 21:11:35 +0000710 if (this->getCaps().dualSourceBlendingSupport() &&
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000711 !(blendOpts & (kEmitCoverage_BlendOptFlag | kCoverageAsAlpha_BlendOptFlag))) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000712 if (kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000713 // write the coverage value to second color
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000714 desc->fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
715 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000716 } else if (kSA_GrBlendCoeff == dstCoeff) {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000717 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000718 desc->fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
719 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000720 } else if (kSC_GrBlendCoeff == dstCoeff) {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000721 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000722 desc->fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
723 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000724 }
725 }
726 }
junov@google.comf93e7172011-03-31 21:26:24 +0000727}