blob: ba36abef3690872f88e46aa500bbe9fbec3f7d2c [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.coma469c282012-10-24 18:28:34 +000036 const GrEffect** 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.com4c883782012-06-04 19:05:11 +000092 const GrMatrix& vm = this->getDrawState().getViewMatrix();
93
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.
99 GrMatrix m;
100 m.setScale(GrIntToScalar(2) / rt->width(), GrIntToScalar(-2) / rt->height());
robertphillips@google.com59f46b82012-07-10 17:30:58 +0000101 m.postTranslate(-GR_Scalar1, GR_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.com05a718c2012-06-29 14:01:53 +0000107 GrScalarToFloat(m[GrMatrix::kMScaleX]),
108 GrScalarToFloat(m[GrMatrix::kMSkewY]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000109 0,
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000110 GrScalarToFloat(m[GrMatrix::kMPersp0]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000111
112 // col 1
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000113 GrScalarToFloat(m[GrMatrix::kMSkewX]),
114 GrScalarToFloat(m[GrMatrix::kMScaleY]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000115 0,
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000116 GrScalarToFloat(m[GrMatrix::kMPersp1]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000117
118 // col 2
119 0, 0, 0, 0,
120
121 // col3
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000122 GrScalarToFloat(m[GrMatrix::kMTransX]),
123 GrScalarToFloat(m[GrMatrix::kMTransY]),
124 0.0f,
125 GrScalarToFloat(m[GrMatrix::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.com8f9cbd62011-12-09 15:55:34 +0000134 GrMatrix m;
135 m.setAll(
bsalomon@google.com4c883782012-06-04 19:05:11 +0000136 GrIntToScalar(2) / viewportSize.fWidth, 0, -GR_Scalar1,
137 0,-GrIntToScalar(2) / viewportSize.fHeight, GR_Scalar1,
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000138 0, 0, GrMatrix::I()[8]);
139 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[] = {
144 GrScalarToFloat(m[GrMatrix::kMScaleX]),
145 GrScalarToFloat(m[GrMatrix::kMSkewY]),
146 GrScalarToFloat(m[GrMatrix::kMPersp0]),
147 GrScalarToFloat(m[GrMatrix::kMSkewX]),
148 GrScalarToFloat(m[GrMatrix::kMScaleY]),
149 GrScalarToFloat(m[GrMatrix::kMPersp1]),
150 GrScalarToFloat(m[GrMatrix::kMTransX]),
151 GrScalarToFloat(m[GrMatrix::kMTransY]),
152 GrScalarToFloat(m[GrMatrix::kMPersp2])
153 };
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.com2d0bade2012-10-26 19:01:17 +0000164void GrGpuGL::AdjustTextureMatrix(const GrTexture* texture, GrMatrix* 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.com890e3b52012-06-01 19:01:37 +0000168 GrMatrix invY;
169 invY.setAll(GR_Scalar1, 0, 0,
170 0, -GR_Scalar1, GR_Scalar1,
171 0, 0, GrMatrix::I()[8]);
172 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.com288d9542012-10-17 12:53:54 +0000179 GrMatrix 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.com9ba4fa62012-07-16 17:36:28 +0000209 const GrMatrix& hwMatrix = fCurrentProgram->fTextureMatrices[s];
bsalomon@google.com288d9542012-10-17 12:53:54 +0000210 GrMatrix 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.com8fe84b52012-03-26 15:24:27 +0000216 GrMatrix 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[] = {
222 GrScalarToFloat(m[GrMatrix::kMScaleX]),
223 GrScalarToFloat(m[GrMatrix::kMSkewY]),
224 GrScalarToFloat(m[GrMatrix::kMPersp0]),
225 GrScalarToFloat(m[GrMatrix::kMSkewX]),
226 GrScalarToFloat(m[GrMatrix::kMScaleY]),
227 GrScalarToFloat(m[GrMatrix::kMPersp1]),
228 GrScalarToFloat(m[GrMatrix::kMTransX]),
229 GrScalarToFloat(m[GrMatrix::kMTransY]),
230 GrScalarToFloat(m[GrMatrix::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.comf271cc72012-10-24 19:35:13 +0000348 const GrEffect* effects[GrDrawState::kNumStages];
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000349 GrGLProgram::Desc desc;
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000350 this->buildProgram(kDrawPoints_DrawType == type, blendOpts, dstCoeff, effects, &desc);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000351
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000352 fCurrentProgram.reset(fProgramCache->getProgram(desc, effects));
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000353 if (NULL == fCurrentProgram.get()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000354 GrAssert(!"Failed to create program!");
355 return false;
356 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000357 fCurrentProgram.get()->ref();
junov@google.comf93e7172011-03-31 21:26:24 +0000358
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000359 if (fHWProgramID != fCurrentProgram->fProgramID) {
360 GL_CALL(UseProgram(fCurrentProgram->fProgramID));
361 fHWProgramID = fCurrentProgram->fProgramID;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000362 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000363 fCurrentProgram->overrideBlend(&srcCoeff, &dstCoeff);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000364 this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000365
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000366 GrColor color;
367 GrColor coverage;
368 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
369 color = 0;
370 coverage = 0;
371 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
372 color = 0xffffffff;
373 coverage = drawState.getCoverage();
374 } else {
375 color = drawState.getColor();
376 coverage = drawState.getCoverage();
377 }
378 this->flushColor(color);
379 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000380
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000381 fCurrentProgram->setData(drawState);
382
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000383 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
384 if (this->isStageEnabled(s)) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000385 this->flushBoundTextureAndParams(s);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000386
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000387 this->flushTextureMatrix(s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000388 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000389 }
junov@google.comf93e7172011-03-31 21:26:24 +0000390 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000391 this->flushStencil(type);
392 this->flushViewMatrix(type);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000393 this->flushScissor();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000394 this->flushAAState(type);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000395
robertphillips@google.com7b112892012-07-31 15:18:21 +0000396 GrIRect* devRect = NULL;
397 GrIRect devClipBounds;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000398 if (drawState.isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000399 fClip->getConservativeBounds(drawState.getRenderTarget(),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000400 &devClipBounds);
401 devRect = &devClipBounds;
bsalomon@google.com4c883782012-06-04 19:05:11 +0000402 }
403 // This must come after textures are flushed because a texture may need
404 // to be msaa-resolved (which will modify bound FBO state).
robertphillips@google.com7b112892012-07-31 15:18:21 +0000405 this->flushRenderTarget(devRect);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000406
junov@google.comf93e7172011-03-31 21:26:24 +0000407 return true;
408}
409
bsalomon@google.com2717d562012-05-07 19:10:52 +0000410#if GR_TEXT_SCALAR_IS_USHORT
411 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
412 #define TEXT_COORDS_ARE_NORMALIZED 1
413#elif GR_TEXT_SCALAR_IS_FLOAT
414 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
415 #define TEXT_COORDS_ARE_NORMALIZED 0
416#elif GR_TEXT_SCALAR_IS_FIXED
417 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
418 #define TEXT_COORDS_ARE_NORMALIZED 0
419#else
420 #error "unknown GR_TEXT_SCALAR type"
421#endif
422
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000423void GrGpuGL::setupGeometry(int* startVertex,
bsalomon@google.com49209392012-06-05 15:13:46 +0000424 int* startIndex,
425 int vertexCount,
426 int indexCount) {
junov@google.comf93e7172011-03-31 21:26:24 +0000427
428 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000429 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000430 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000431 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000432
bsalomon@google.come79c8152012-03-29 19:07:12 +0000433 GrVertexLayout currLayout = this->getVertexLayout();
434
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000435 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000436 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000437 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000438 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000439 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000440 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000441 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000442 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000443 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000444 int oldEdgeOffset;
445
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000446 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
447 fHWGeometryState.fVertexLayout,
448 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000449 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000450 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000451 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000452 bool indexed = NULL != startIndex;
453
454 int extraVertexOffset;
455 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000456 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000457
458 GrGLenum scalarType;
459 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000460 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000461 scalarType = TEXT_COORDS_GL_TYPE;
462 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000463 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000464 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
465 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000466 texCoordNorm = false;
467 }
468
469 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
470 *startVertex = 0;
471 if (indexed) {
472 *startIndex += extraIndexOffset;
473 }
474
475 // all the Pointers must be set if any of these are true
476 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
477 vertexOffset != fHWGeometryState.fVertexOffset ||
478 newStride != oldStride;
479
480 // position and tex coord offsets change if above conditions are true
481 // or the type/normalization changed based on text vs nontext type coords.
482 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000483 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000484 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000485 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000486
487 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000488 int idx = GrGLProgram::PositionAttributeIdx();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000489 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000490 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000491 fHWGeometryState.fVertexOffset = vertexOffset;
492 }
493
tomhudson@google.com93813632011-10-27 20:21:16 +0000494 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000495 if (newTexCoordOffsets[t] > 0) {
496 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000497 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000498 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000499 GL_CALL(EnableVertexAttribArray(idx));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000500 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000501 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000502 } else if (posAndTexChange ||
503 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000504 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000505 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000506 }
507 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000508 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000509 }
510 }
511
512 if (newColorOffset > 0) {
513 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000514 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000515 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000516 GL_CALL(EnableVertexAttribArray(idx));
517 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000518 true, newStride, colorOffset));
519 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000520 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000521 true, newStride, colorOffset));
522 }
523 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000524 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000525 }
526
bsalomon@google.coma3108262011-10-10 14:08:47 +0000527 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000528 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000529 int idx = GrGLProgram::CoverageAttributeIdx();
530 if (oldCoverageOffset <= 0) {
531 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000532 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000533 true, newStride, coverageOffset));
534 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
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 }
538 } else if (oldCoverageOffset > 0) {
539 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
540 }
541
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000542 if (newEdgeOffset > 0) {
543 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
544 int idx = GrGLProgram::EdgeAttributeIdx();
545 if (oldEdgeOffset <= 0) {
546 GL_CALL(EnableVertexAttribArray(idx));
547 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
548 false, newStride, edgeOffset));
549 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
550 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
551 false, newStride, edgeOffset));
552 }
553 } else if (oldEdgeOffset > 0) {
554 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
555 }
556
bsalomon@google.come79c8152012-03-29 19:07:12 +0000557 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000558 fHWGeometryState.fArrayPtrsDirty = false;
559}
560
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000561namespace {
562
bsalomon@google.com08283af2012-10-26 13:01:20 +0000563void setup_effect(GrGLProgram::Desc::StageDesc* stageDesc,
564 const GrEffectStage& stage,
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000565 const GrGLCaps& caps,
566 const GrEffect** effects,
567 GrGLProgram* program, int index) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000568 const GrEffect* effect = stage.getEffect();
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000569 if (effect) {
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000570 const GrBackendEffectFactory& factory = effect->getFactory();
bsalomon@google.com08283af2012-10-26 13:01:20 +0000571 stageDesc->fEffectKey = factory.glEffectKey(*effect, caps);
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000572 effects[index] = effect;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000573 } else {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000574 stageDesc->fEffectKey = 0;
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000575 effects[index] = NULL;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000576 }
577}
578
579}
580
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000581void GrGpuGL::buildProgram(bool isPoints,
bsalomon@google.com49209392012-06-05 15:13:46 +0000582 BlendOptFlags blendOpts,
583 GrBlendCoeff dstCoeff,
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000584 const GrEffect** effects,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000585 ProgramDesc* desc) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000586 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000587
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000588 // This should already have been caught
589 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
590
591 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
592
593 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
594 kEmitCoverage_BlendOptFlag));
595
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000596 // The descriptor is used as a cache key. Thus when a field of the
597 // descriptor will not affect program generation (because of the vertex
598 // layout in use or other descriptor field settings) it should be set
599 // to a canonical value to avoid duplicate programs with different keys.
600
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000601 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000602 desc->fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000603
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000604 desc->fEmitsPointSize = isPoints;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000605
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000606 bool requiresAttributeColors = !skipColor &&
607 SkToBool(desc->fVertexLayout & kColor_VertexLayoutBit);
608 bool requiresAttributeCoverage = !skipCoverage &&
609 SkToBool(desc->fVertexLayout & kCoverage_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000610
611 // fColorInput/fCoverageInput records how colors are specified for the.
612 // program. So we strip the bits from the layout to avoid false negatives
613 // when searching for an existing program in the cache.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000614 desc->fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000615
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000616 desc->fColorFilterXfermode = skipColor ?
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000617 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000618 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000619
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000620 // no reason to do edge aa or look at per-vertex coverage if coverage is
621 // ignored
622 if (skipCoverage) {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000623 desc->fVertexLayout &= ~(kEdge_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000624 }
625
626 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
627 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000628 (!requiresAttributeColors && 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000629 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000630 desc->fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000631 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000632 desc->fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000633 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000634 desc->fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000635 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000636 desc->fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000637 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000638
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000639 bool covIsSolidWhite = !requiresAttributeCoverage && 0xffffffff == drawState.getCoverage();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000640
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000641 if (skipCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000642 desc->fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000643 } else if (covIsSolidWhite) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000644 desc->fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000645 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000646 desc->fCoverageInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000647 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000648 desc->fCoverageInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000649 }
junov@google.comf93e7172011-03-31 21:26:24 +0000650
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000651 int lastEnabledStage = -1;
652
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000653 if (!skipCoverage && (desc->fVertexLayout &GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000654 desc->fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000655 } else {
656 // use canonical value when not set to avoid cache misses
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000657 desc->fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000658 }
659
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000660 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000661 StageDesc& stageDesc = desc->fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000662
bsalomon@google.com08283af2012-10-26 13:01:20 +0000663 stageDesc.fOptFlags = 0;
664 stageDesc.setEnabled(this->isStageEnabled(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000665
666 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000667 skipCoverage;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000668
bsalomon@google.com08283af2012-10-26 13:01:20 +0000669 if (!skip && stageDesc.isEnabled()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000670 lastEnabledStage = s;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000671 const GrEffectStage& stage = drawState.getStage(s);
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000672 // FIXME: Still assuming one texture per effect
bsalomon@google.com08283af2012-10-26 13:01:20 +0000673 const GrEffect* effect = drawState.getStage(s).getEffect();
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000674
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000675 if (effect->numTextures() > 0) {
676 const GrGLTexture* texture = static_cast<const GrGLTexture*>(effect->texture(0));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000677 GrMatrix samplerMatrix;
bsalomon@google.com08283af2012-10-26 13:01:20 +0000678 stage.getTotalMatrix(&samplerMatrix);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000679 if (NULL != texture) {
680 // We call this helper function rather then simply checking the client-specified
681 // texture matrix. This is because we may have to concat a y-inversion to account
682 // for texture orientation.
bsalomon@google.com08283af2012-10-26 13:01:20 +0000683 stageDesc.fOptFlags |= TextureMatrixOptFlags(texture, stage);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000684 }
685 } else {
686 // Set identity to do the minimal amount of extra work for the no texture case.
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000687 // This will go away when effects manage their own texture matrix.
bsalomon@google.com08283af2012-10-26 13:01:20 +0000688 stageDesc.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000689 }
bsalomon@google.com08283af2012-10-26 13:01:20 +0000690 setup_effect(&stageDesc, stage, this->glCaps(), effects, fCurrentProgram.get(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000691
junov@google.comf93e7172011-03-31 21:26:24 +0000692 } else {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000693 stageDesc.fOptFlags = 0;
694 stageDesc.fEffectKey = 0;
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000695 effects[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000696 }
697 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000698
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000699 desc->fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000700
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000701 // Currently the experimental GS will only work with triangle prims (and it doesn't do anything
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000702 // other than pass through values from the VS to the FS anyway).
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000703#if 0 && GR_GL_EXPERIMENTAL_GS
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000704 desc->fExperimentalGS = this->getCaps().fGeometryShaderSupport;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000705#endif
706
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000707 // We want to avoid generating programs with different "first cov stage" values when they would
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000708 // 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 +0000709 // coverage stages or the distinction between coverage and color is immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000710 int firstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000711 desc->fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000712 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000713 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000714 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000715 }
716
717 // other coverage inputs
718 if (!hasCoverage) {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000719 hasCoverage = requiresAttributeCoverage ||
720 (desc->fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000721 }
722
723 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000724 // color filter is applied between color/coverage computation
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000725 if (SkXfermode::kDst_Mode != desc->fColorFilterXfermode) {
726 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000727 }
728
bsalomon@google.comf6601872012-08-28 21:11:35 +0000729 if (this->getCaps().dualSourceBlendingSupport() &&
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000730 !(blendOpts & (kEmitCoverage_BlendOptFlag | kCoverageAsAlpha_BlendOptFlag))) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000731 if (kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000732 // write the coverage value to second color
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000733 desc->fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
734 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000735 } else if (kSA_GrBlendCoeff == dstCoeff) {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000736 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000737 desc->fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
738 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000739 } else if (kSC_GrBlendCoeff == dstCoeff) {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000740 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000741 desc->fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
742 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000743 }
744 }
745 }
junov@google.comf93e7172011-03-31 21:26:24 +0000746}