blob: e57933161444a46f6444b221233f094f8cc703d5 [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
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000010#include "GrCustomStage.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000011#include "GrGLProgramStage.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,
36 const GrCustomStage** 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.com890e3b52012-06-01 19:01:37 +0000164void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000165 GrMatrix* matrix) {
166 GrAssert(NULL != texture);
167 GrAssert(NULL != matrix);
168 GrGLTexture::Orientation orientation = texture->orientation();
169 if (GrGLTexture::kBottomUp_Orientation == orientation) {
170 GrMatrix invY;
171 invY.setAll(GR_Scalar1, 0, 0,
172 0, -GR_Scalar1, GR_Scalar1,
173 0, 0, GrMatrix::I()[8]);
174 matrix->postConcat(invY);
175 } else {
176 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
junov@google.com6acc9b32011-05-16 18:32:07 +0000177 }
178}
179
bsalomon@google.com288d9542012-10-17 12:53:54 +0000180int GrGpuGL::TextureMatrixOptFlags(const GrGLTexture* texture,
181 const GrSamplerState& sampler) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000182 GrAssert(NULL != texture);
bsalomon@google.com288d9542012-10-17 12:53:54 +0000183 GrMatrix matrix;
184 sampler.getTotalMatrix(&matrix);
185
186 bool canBeIndentity = GrGLTexture::kTopDown_Orientation == texture->orientation();
187
188 if (canBeIndentity && matrix.isIdentity()) {
189 return GrGLProgram::StageDesc::kIdentityMatrix_OptFlagBit;
190 } else if (!matrix.hasPerspective()) {
191 return GrGLProgram::StageDesc::kNoPerspective_OptFlagBit;
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000192 }
bsalomon@google.com288d9542012-10-17 12:53:54 +0000193 return 0;
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000194}
195
196///////////////////////////////////////////////////////////////////////////////
197
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000198void GrGpuGL::flushTextureMatrix(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000199 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000200
201 // FIXME: Still assuming only a single texture per custom stage
202 const GrCustomStage* stage = drawState.getSampler(s).getCustomStage();
203 const GrGLTexture* texture = static_cast<const GrGLTexture*>(stage->texture(0));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000204 if (NULL != texture) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000205
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000206 bool orientationChange = fCurrentProgram->fTextureOrientation[s] !=
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000207 texture->orientation();
208
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000209 UniformHandle matrixUni = fCurrentProgram->fUniforms.fStages[s].fTextureMatrixUni;
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000210
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000211 const GrMatrix& hwMatrix = fCurrentProgram->fTextureMatrices[s];
bsalomon@google.com288d9542012-10-17 12:53:54 +0000212 GrMatrix samplerMatrix;
213 drawState.getSampler(s).getTotalMatrix(&samplerMatrix);
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000214
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000215 if (kInvalidUniformHandle != matrixUni &&
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000216 (orientationChange || !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000217
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000218 GrMatrix m = samplerMatrix;
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000219 AdjustTextureMatrix(texture, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000220
bsalomon@google.com91961302011-05-09 18:39:58 +0000221 // ES doesn't allow you to pass true to the transpose param,
222 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000223 GrGLfloat mt[] = {
224 GrScalarToFloat(m[GrMatrix::kMScaleX]),
225 GrScalarToFloat(m[GrMatrix::kMSkewY]),
226 GrScalarToFloat(m[GrMatrix::kMPersp0]),
227 GrScalarToFloat(m[GrMatrix::kMSkewX]),
228 GrScalarToFloat(m[GrMatrix::kMScaleY]),
229 GrScalarToFloat(m[GrMatrix::kMPersp1]),
230 GrScalarToFloat(m[GrMatrix::kMTransX]),
231 GrScalarToFloat(m[GrMatrix::kMTransY]),
232 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000233 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000234
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000235 fCurrentProgram->fUniformManager.setMatrix3f(matrixUni, mt);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000236 fCurrentProgram->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000237 }
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000238
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000239 fCurrentProgram->fTextureOrientation[s] = texture->orientation();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000240 }
junov@google.comf93e7172011-03-31 21:26:24 +0000241}
242
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000243void GrGpuGL::flushColorMatrix() {
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000244 UniformHandle matrixUni = fCurrentProgram->fUniforms.fColorMatrixUni;
245 UniformHandle vecUni = fCurrentProgram->fUniforms.fColorMatrixVecUni;
246 if (kInvalidUniformHandle != matrixUni && kInvalidUniformHandle != vecUni) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000247 const float* m = this->getDrawState().getColorMatrix();
248 GrGLfloat mt[] = {
249 m[0], m[5], m[10], m[15],
250 m[1], m[6], m[11], m[16],
251 m[2], m[7], m[12], m[17],
252 m[3], m[8], m[13], m[18],
253 };
254 static float scale = 1.0f / 255.0f;
255 GrGLfloat vec[] = {
256 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
257 };
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000258 fCurrentProgram->fUniformManager.setMatrix4f(matrixUni, mt);
259 fCurrentProgram->fUniformManager.set4fv(vecUni, 0, 1, vec);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000260 }
261}
262
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000263void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000264 const ProgramDesc& desc = fCurrentProgram->getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000265 const GrDrawState& drawState = this->getDrawState();
266
bsalomon@google.come79c8152012-03-29 19:07:12 +0000267 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000268 // color will be specified per-vertex as an attribute
269 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000270 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000271 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000272 switch (desc.fColorInput) {
273 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000274 if (fHWConstAttribColor != color) {
bsalomon@google.com75347472012-09-17 17:23:21 +0000275 // OpenGL ES only supports the float varieties of glVertexAttrib
276 GrGLfloat c[4];
277 GrColorToRGBAFloat(color, c);
278 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(), c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000279 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000280 }
281 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000282 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000283 if (fCurrentProgram->fColor != color) {
bsalomon@google.com75347472012-09-17 17:23:21 +0000284 // OpenGL ES doesn't support unsigned byte varieties of glUniform
285 GrGLfloat c[4];
286 GrColorToRGBAFloat(color, c);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000287 GrAssert(kInvalidUniformHandle != fCurrentProgram->fUniforms.fColorUni);
288 fCurrentProgram->fUniformManager.set4fv(fCurrentProgram->fUniforms.fColorUni,
289 0, 1, c);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000290 fCurrentProgram->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000291 }
292 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000293 case ProgramDesc::kSolidWhite_ColorInput:
294 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000295 break;
296 default:
297 GrCrash("Unknown color type.");
298 }
299 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000300 UniformHandle filterColorUni = fCurrentProgram->fUniforms.fColorFilterUni;
301 if (kInvalidUniformHandle != filterColorUni &&
302 fCurrentProgram->fColorFilterColor != drawState.getColorFilterColor()) {
bsalomon@google.com75347472012-09-17 17:23:21 +0000303 GrGLfloat c[4];
304 GrColorToRGBAFloat(drawState.getColorFilterColor(), c);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000305 fCurrentProgram->fUniformManager.set4fv(filterColorUni, 0, 1, c);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000306 fCurrentProgram->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000307 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000308}
309
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000310void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000311 const ProgramDesc& desc = fCurrentProgram->getDesc();
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000312 // const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000313
314
bsalomon@google.come79c8152012-03-29 19:07:12 +0000315 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000316 // coverage will be specified per-vertex as an attribute
317 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000318 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000319 } else {
320 switch (desc.fCoverageInput) {
321 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000322 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000323 // OpenGL ES only supports the float varieties of
324 // glVertexAttrib
bsalomon@google.com75347472012-09-17 17:23:21 +0000325 GrGLfloat c[4];
326 GrColorToRGBAFloat(coverage, c);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000327 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000328 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000329 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000330 }
331 break;
332 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000333 if (fCurrentProgram->fCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000334 // OpenGL ES doesn't support unsigned byte varieties of
335 // glUniform
bsalomon@google.com75347472012-09-17 17:23:21 +0000336 GrGLfloat c[4];
337 GrColorToRGBAFloat(coverage, c);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000338 GrAssert(kInvalidUniformHandle != fCurrentProgram->fUniforms.fCoverageUni);
339 fCurrentProgram->fUniformManager.set4fv(fCurrentProgram->fUniforms.fCoverageUni,
340 0, 1, c);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000341 fCurrentProgram->fCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000342 }
343 break;
344 case ProgramDesc::kSolidWhite_ColorInput:
345 case ProgramDesc::kTransBlack_ColorInput:
346 break;
347 default:
348 GrCrash("Unknown coverage type.");
349 }
350 }
351}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000352
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000353bool GrGpuGL::flushGraphicsState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000354 const GrDrawState& drawState = this->getDrawState();
355
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000356 // GrGpu::setupClipAndFlushState should have already checked this
357 // and bailed if not true.
358 GrAssert(NULL != drawState.getRenderTarget());
359
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000360 if (kStencilPath_DrawType != type) {
361 this->flushMiscFixedFunctionState();
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000362
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000363 GrBlendCoeff srcCoeff;
364 GrBlendCoeff dstCoeff;
365 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
366 if (kSkipDraw_BlendOptFlag & blendOpts) {
367 return false;
368 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000369
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000370 const GrCustomStage* customStages [GrDrawState::kNumStages];
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000371 GrGLProgram::Desc desc;
372 this->buildProgram(kDrawPoints_DrawType == type, blendOpts, dstCoeff, customStages, &desc);
373
374 fCurrentProgram.reset(fProgramCache->getProgram(desc, customStages));
375 if (NULL == fCurrentProgram.get()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000376 GrAssert(!"Failed to create program!");
377 return false;
378 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000379 fCurrentProgram.get()->ref();
junov@google.comf93e7172011-03-31 21:26:24 +0000380
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000381 if (fHWProgramID != fCurrentProgram->fProgramID) {
382 GL_CALL(UseProgram(fCurrentProgram->fProgramID));
383 fHWProgramID = fCurrentProgram->fProgramID;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000384 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000385 fCurrentProgram->overrideBlend(&srcCoeff, &dstCoeff);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000386 this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000387
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000388 GrColor color;
389 GrColor coverage;
390 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
391 color = 0;
392 coverage = 0;
393 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
394 color = 0xffffffff;
395 coverage = drawState.getCoverage();
396 } else {
397 color = drawState.getColor();
398 coverage = drawState.getCoverage();
399 }
400 this->flushColor(color);
401 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000402
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000403 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
404 if (this->isStageEnabled(s)) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000405 this->flushBoundTextureAndParams(s);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000406
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000407 this->flushTextureMatrix(s);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000408
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000409 if (NULL != fCurrentProgram->fProgramStage[s]) {
410 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000411 fCurrentProgram->fProgramStage[s]->setData(fCurrentProgram->fUniformManager,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000412 *sampler.getCustomStage(),
413 drawState.getRenderTarget(), s);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000414 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000415 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000416 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000417 this->flushColorMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000418 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000419 this->flushStencil(type);
420 this->flushViewMatrix(type);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000421 this->flushScissor();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000422 this->flushAAState(type);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000423
robertphillips@google.com7b112892012-07-31 15:18:21 +0000424 GrIRect* devRect = NULL;
425 GrIRect devClipBounds;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000426 if (drawState.isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000427 fClip->getConservativeBounds(drawState.getRenderTarget(),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000428 &devClipBounds);
429 devRect = &devClipBounds;
bsalomon@google.com4c883782012-06-04 19:05:11 +0000430 }
431 // This must come after textures are flushed because a texture may need
432 // to be msaa-resolved (which will modify bound FBO state).
robertphillips@google.com7b112892012-07-31 15:18:21 +0000433 this->flushRenderTarget(devRect);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000434
junov@google.comf93e7172011-03-31 21:26:24 +0000435 return true;
436}
437
bsalomon@google.com2717d562012-05-07 19:10:52 +0000438#if GR_TEXT_SCALAR_IS_USHORT
439 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
440 #define TEXT_COORDS_ARE_NORMALIZED 1
441#elif GR_TEXT_SCALAR_IS_FLOAT
442 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
443 #define TEXT_COORDS_ARE_NORMALIZED 0
444#elif GR_TEXT_SCALAR_IS_FIXED
445 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
446 #define TEXT_COORDS_ARE_NORMALIZED 0
447#else
448 #error "unknown GR_TEXT_SCALAR type"
449#endif
450
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000451void GrGpuGL::setupGeometry(int* startVertex,
bsalomon@google.com49209392012-06-05 15:13:46 +0000452 int* startIndex,
453 int vertexCount,
454 int indexCount) {
junov@google.comf93e7172011-03-31 21:26:24 +0000455
456 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000457 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000458 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000459 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000460
bsalomon@google.come79c8152012-03-29 19:07:12 +0000461 GrVertexLayout currLayout = this->getVertexLayout();
462
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000463 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000464 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000465 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000466 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000467 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000468 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000469 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000470 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000471 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000472 int oldEdgeOffset;
473
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000474 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
475 fHWGeometryState.fVertexLayout,
476 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000477 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000478 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000479 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000480 bool indexed = NULL != startIndex;
481
482 int extraVertexOffset;
483 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000484 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000485
486 GrGLenum scalarType;
487 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000488 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000489 scalarType = TEXT_COORDS_GL_TYPE;
490 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000491 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000492 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
493 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000494 texCoordNorm = false;
495 }
496
497 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
498 *startVertex = 0;
499 if (indexed) {
500 *startIndex += extraIndexOffset;
501 }
502
503 // all the Pointers must be set if any of these are true
504 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
505 vertexOffset != fHWGeometryState.fVertexOffset ||
506 newStride != oldStride;
507
508 // position and tex coord offsets change if above conditions are true
509 // or the type/normalization changed based on text vs nontext type coords.
510 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000511 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000512 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000513 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000514
515 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000516 int idx = GrGLProgram::PositionAttributeIdx();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000517 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000518 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000519 fHWGeometryState.fVertexOffset = vertexOffset;
520 }
521
tomhudson@google.com93813632011-10-27 20:21:16 +0000522 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000523 if (newTexCoordOffsets[t] > 0) {
524 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000525 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000526 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000527 GL_CALL(EnableVertexAttribArray(idx));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000528 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000529 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000530 } else if (posAndTexChange ||
531 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000532 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000533 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000534 }
535 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000536 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000537 }
538 }
539
540 if (newColorOffset > 0) {
541 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000542 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000543 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000544 GL_CALL(EnableVertexAttribArray(idx));
545 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000546 true, newStride, colorOffset));
547 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000548 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000549 true, newStride, colorOffset));
550 }
551 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000552 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000553 }
554
bsalomon@google.coma3108262011-10-10 14:08:47 +0000555 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000556 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000557 int idx = GrGLProgram::CoverageAttributeIdx();
558 if (oldCoverageOffset <= 0) {
559 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000560 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000561 true, newStride, coverageOffset));
562 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000563 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000564 true, newStride, coverageOffset));
565 }
566 } else if (oldCoverageOffset > 0) {
567 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
568 }
569
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000570 if (newEdgeOffset > 0) {
571 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
572 int idx = GrGLProgram::EdgeAttributeIdx();
573 if (oldEdgeOffset <= 0) {
574 GL_CALL(EnableVertexAttribArray(idx));
575 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
576 false, newStride, edgeOffset));
577 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
578 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
579 false, newStride, edgeOffset));
580 }
581 } else if (oldEdgeOffset > 0) {
582 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
583 }
584
bsalomon@google.come79c8152012-03-29 19:07:12 +0000585 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000586 fHWGeometryState.fArrayPtrsDirty = false;
587}
588
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000589namespace {
590
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000591void setup_custom_stage(GrGLProgram::Desc::StageDesc* stage,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000592 const GrSamplerState& sampler,
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000593 const GrGLCaps& caps,
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000594 const GrCustomStage** customStages,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000595 GrGLProgram* program, int index) {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000596 const GrCustomStage* customStage = sampler.getCustomStage();
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000597 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000598 const GrProgramStageFactory& factory = customStage->getFactory();
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000599 stage->fCustomStageKey = factory.glStageKey(*customStage, caps);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000600 customStages[index] = customStage;
601 } else {
602 stage->fCustomStageKey = 0;
603 customStages[index] = NULL;
604 }
605}
606
607}
608
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000609void GrGpuGL::buildProgram(bool isPoints,
bsalomon@google.com49209392012-06-05 15:13:46 +0000610 BlendOptFlags blendOpts,
611 GrBlendCoeff dstCoeff,
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000612 const GrCustomStage** customStages,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000613 ProgramDesc* desc) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000614 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000615
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000616 // This should already have been caught
617 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
618
619 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
620
621 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
622 kEmitCoverage_BlendOptFlag));
623
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000624 // The descriptor is used as a cache key. Thus when a field of the
625 // descriptor will not affect program generation (because of the vertex
626 // layout in use or other descriptor field settings) it should be set
627 // to a canonical value to avoid duplicate programs with different keys.
628
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000629 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000630 desc->fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000631
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000632 desc->fEmitsPointSize = isPoints;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000633
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000634 bool requiresAttributeColors = !skipColor &&
635 SkToBool(desc->fVertexLayout & kColor_VertexLayoutBit);
636 bool requiresAttributeCoverage = !skipCoverage &&
637 SkToBool(desc->fVertexLayout & kCoverage_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000638
639 // fColorInput/fCoverageInput records how colors are specified for the.
640 // program. So we strip the bits from the layout to avoid false negatives
641 // when searching for an existing program in the cache.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000642 desc->fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000643
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000644 desc->fColorFilterXfermode = skipColor ?
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000645 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000646 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000647
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000648 desc->fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000649
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000650 // no reason to do edge aa or look at per-vertex coverage if coverage is
651 // ignored
652 if (skipCoverage) {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000653 desc->fVertexLayout &= ~(kEdge_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000654 }
655
656 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
657 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000658 (!requiresAttributeColors && 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000659 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000660 desc->fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000661 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000662 desc->fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000663 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000664 desc->fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000665 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000666 desc->fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000667 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000668
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000669 bool covIsSolidWhite = !requiresAttributeCoverage && 0xffffffff == drawState.getCoverage();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000670
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000671 if (skipCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000672 desc->fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000673 } else if (covIsSolidWhite) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000674 desc->fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000675 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000676 desc->fCoverageInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000677 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000678 desc->fCoverageInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000679 }
junov@google.comf93e7172011-03-31 21:26:24 +0000680
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000681 int lastEnabledStage = -1;
682
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000683 if (!skipCoverage && (desc->fVertexLayout &GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000684 desc->fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000685 } else {
686 // use canonical value when not set to avoid cache misses
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000687 desc->fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000688 }
689
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000690 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000691 StageDesc& stage = desc->fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000692
693 stage.fOptFlags = 0;
694 stage.setEnabled(this->isStageEnabled(s));
695
696 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000697 skipCoverage;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000698
699 if (!skip && stage.isEnabled()) {
700 lastEnabledStage = s;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000701 const GrSamplerState& sampler = drawState.getSampler(s);
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000702 // FIXME: Still assuming one texture per custom stage
703 const GrCustomStage* customStage = drawState.getSampler(s).getCustomStage();
704 const GrGLTexture* texture = static_cast<const GrGLTexture*>(customStage->texture(0));
bsalomon@google.com288d9542012-10-17 12:53:54 +0000705 GrMatrix samplerMatrix;
706 sampler.getTotalMatrix(&samplerMatrix);
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000707 if (NULL != texture) {
708 // We call this helper function rather then simply checking the client-specified
709 // texture matrix. This is because we may have to concat a y-inversion to account
710 // for texture orientation.
bsalomon@google.com288d9542012-10-17 12:53:54 +0000711 stage.fOptFlags |= TextureMatrixOptFlags(texture, sampler);
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000712 }
713
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000714 setup_custom_stage(&stage, sampler, this->glCaps(), customStages,
715 fCurrentProgram.get(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000716
junov@google.comf93e7172011-03-31 21:26:24 +0000717 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000718 stage.fOptFlags = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000719 stage.fCustomStageKey = 0;
720 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000721 }
722 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000723
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000724 desc->fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000725
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000726 // Currently the experimental GS will only work with triangle prims (and it doesn't do anything
727 // other than pass through values fromthe VS to the FS anyway).
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000728#if 0 && GR_GL_EXPERIMENTAL_GS
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000729 desc->fExperimentalGS = this->getCaps().fGeometryShaderSupport;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000730#endif
731
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000732 // We want to avoid generating programs with different "first cov stage" values when they would
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000733 // 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 +0000734 // coverage stages or the distinction between coverage and color is immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000735 int firstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000736 desc->fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000737 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000738 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000739 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000740 }
741
742 // other coverage inputs
743 if (!hasCoverage) {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000744 hasCoverage = requiresAttributeCoverage ||
745 (desc->fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000746 }
747
748 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000749 // color filter is applied between color/coverage computation
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000750 if (SkXfermode::kDst_Mode != desc->fColorFilterXfermode) {
751 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000752 }
753
bsalomon@google.comf6601872012-08-28 21:11:35 +0000754 if (this->getCaps().dualSourceBlendingSupport() &&
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000755 !(blendOpts & (kEmitCoverage_BlendOptFlag | kCoverageAsAlpha_BlendOptFlag))) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000756 if (kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000757 // write the coverage value to second color
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000758 desc->fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
759 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000760 } else if (kSA_GrBlendCoeff == dstCoeff) {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000761 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000762 desc->fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
763 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000764 } else if (kSC_GrBlendCoeff == dstCoeff) {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000765 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000766 desc->fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
767 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000768 }
769 }
770 }
junov@google.comf93e7172011-03-31 21:26:24 +0000771}