blob: f4044fcabe588077973fef1222f8a8ce4dc8a244 [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.com9ba4fa62012-07-16 17:36:28 +000035GrGLProgram* GrGpuGL::ProgramCache::getProgram(const ProgramDesc& desc, GrCustomStage** stages) {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000036 Entry newEntry;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000037 newEntry.fKey.setKeyData(desc.asKey());
38
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000039 Entry* entry = fHashCache.find(newEntry.fKey);
40 if (NULL == entry) {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000041 newEntry.fProgram.reset(GrGLProgram::Create(fGL, desc, stages));
42 if (NULL == newEntry.fProgram.get()) {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000043 return NULL;
44 }
45 if (fCount < kMaxEntries) {
46 entry = fEntries + fCount;
47 ++fCount;
48 } else {
49 GrAssert(kMaxEntries == fCount);
50 entry = fEntries;
51 for (int i = 1; i < kMaxEntries; ++i) {
52 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
53 entry = fEntries + i;
junov@google.comf93e7172011-03-31 21:26:24 +000054 }
junov@google.comf93e7172011-03-31 21:26:24 +000055 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000056 fHashCache.remove(entry->fKey, entry);
junov@google.comf93e7172011-03-31 21:26:24 +000057 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000058 *entry = newEntry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000059 fHashCache.insert(entry->fKey, entry);
junov@google.comf93e7172011-03-31 21:26:24 +000060 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000061
62 entry->fLRUStamp = fCurrLRUStamp;
63 if (GR_UINT32_MAX == fCurrLRUStamp) {
64 // wrap around! just trash our LRU, one time hit.
65 for (int i = 0; i < fCount; ++i) {
66 fEntries[i].fLRUStamp = 0;
67 }
68 }
69 ++fCurrLRUStamp;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000070 return entry->fProgram;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000071}
junov@google.comf93e7172011-03-31 21:26:24 +000072
bsalomon@google.com1e257a52011-07-06 19:52:16 +000073////////////////////////////////////////////////////////////////////////////////
74
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000075void GrGpuGL::abandonResources(){
76 INHERITED::abandonResources();
77 fProgramCache->abandon();
78 fHWProgramID = 0;
79}
80
81////////////////////////////////////////////////////////////////////////////////
82
bsalomon@google.com0b77d682011-08-19 13:28:54 +000083#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
84
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000085void GrGpuGL::flushViewMatrix(DrawType type) {
bsalomon@google.com4c883782012-06-04 19:05:11 +000086 const GrGLRenderTarget* rt = static_cast<const GrGLRenderTarget*>(this->getDrawState().getRenderTarget());
87 SkISize viewportSize;
88 const GrGLIRect& viewport = rt->getViewport();
89 viewportSize.set(viewport.fWidth, viewport.fHeight);
junov@google.comf93e7172011-03-31 21:26:24 +000090
bsalomon@google.com4c883782012-06-04 19:05:11 +000091 const GrMatrix& vm = this->getDrawState().getViewMatrix();
92
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000093 if (kStencilPath_DrawType == type) {
bsalomon@google.com05a718c2012-06-29 14:01:53 +000094 if (fHWPathMatrixState.fViewMatrix != vm ||
95 fHWPathMatrixState.fRTSize != viewportSize) {
96 // rescale the coords from skia's "device" coords to GL's normalized coords,
97 // and perform a y-flip.
98 GrMatrix m;
99 m.setScale(GrIntToScalar(2) / rt->width(), GrIntToScalar(-2) / rt->height());
robertphillips@google.com59f46b82012-07-10 17:30:58 +0000100 m.postTranslate(-GR_Scalar1, GR_Scalar1);
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000101 m.preConcat(vm);
102
103 // GL wants a column-major 4x4.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000104 GrGLfloat mv[] = {
105 // col 0
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000106 GrScalarToFloat(m[GrMatrix::kMScaleX]),
107 GrScalarToFloat(m[GrMatrix::kMSkewY]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000108 0,
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000109 GrScalarToFloat(m[GrMatrix::kMPersp0]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000110
111 // col 1
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000112 GrScalarToFloat(m[GrMatrix::kMSkewX]),
113 GrScalarToFloat(m[GrMatrix::kMScaleY]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000114 0,
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000115 GrScalarToFloat(m[GrMatrix::kMPersp1]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000116
117 // col 2
118 0, 0, 0, 0,
119
120 // col3
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000121 GrScalarToFloat(m[GrMatrix::kMTransX]),
122 GrScalarToFloat(m[GrMatrix::kMTransY]),
123 0.0f,
124 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000125 };
126 GL_CALL(MatrixMode(GR_GL_PROJECTION));
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000127 GL_CALL(LoadMatrixf(mv));
128 fHWPathMatrixState.fViewMatrix = vm;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000129 fHWPathMatrixState.fRTSize = viewportSize;
130 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000131 } else if (!fCurrentProgram->fViewMatrix.cheapEqualTo(vm) ||
132 fCurrentProgram->fViewportSize != viewportSize) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000133 GrMatrix m;
134 m.setAll(
bsalomon@google.com4c883782012-06-04 19:05:11 +0000135 GrIntToScalar(2) / viewportSize.fWidth, 0, -GR_Scalar1,
136 0,-GrIntToScalar(2) / viewportSize.fHeight, GR_Scalar1,
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000137 0, 0, GrMatrix::I()[8]);
138 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000139
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000140 // ES doesn't allow you to pass true to the transpose param,
141 // so do our own transpose
142 GrGLfloat mt[] = {
143 GrScalarToFloat(m[GrMatrix::kMScaleX]),
144 GrScalarToFloat(m[GrMatrix::kMSkewY]),
145 GrScalarToFloat(m[GrMatrix::kMPersp0]),
146 GrScalarToFloat(m[GrMatrix::kMSkewX]),
147 GrScalarToFloat(m[GrMatrix::kMScaleY]),
148 GrScalarToFloat(m[GrMatrix::kMPersp1]),
149 GrScalarToFloat(m[GrMatrix::kMTransX]),
150 GrScalarToFloat(m[GrMatrix::kMTransY]),
151 GrScalarToFloat(m[GrMatrix::kMPersp2])
152 };
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000153 fCurrentProgram->fUniformManager.setMatrix3f(fCurrentProgram->fUniforms.fViewMatrixUni, mt);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000154 fCurrentProgram->fViewMatrix = vm;
155 fCurrentProgram->fViewportSize = viewportSize;
bsalomon@google.com91961302011-05-09 18:39:58 +0000156 }
junov@google.comf93e7172011-03-31 21:26:24 +0000157}
158
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000159///////////////////////////////////////////////////////////////////////////////
junov@google.com6acc9b32011-05-16 18:32:07 +0000160
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000161// helpers for texture matrices
junov@google.com6acc9b32011-05-16 18:32:07 +0000162
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000163void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000164 GrMatrix* matrix) {
165 GrAssert(NULL != texture);
166 GrAssert(NULL != matrix);
167 GrGLTexture::Orientation orientation = texture->orientation();
168 if (GrGLTexture::kBottomUp_Orientation == orientation) {
169 GrMatrix invY;
170 invY.setAll(GR_Scalar1, 0, 0,
171 0, -GR_Scalar1, GR_Scalar1,
172 0, 0, GrMatrix::I()[8]);
173 matrix->postConcat(invY);
174 } else {
175 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
junov@google.com6acc9b32011-05-16 18:32:07 +0000176 }
177}
178
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000179bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
180 const GrSamplerState& sampler) {
181 GrAssert(NULL != texture);
182 if (!sampler.getMatrix().isIdentity()) {
183 return false;
184 }
185 GrGLTexture::Orientation orientation = texture->orientation();
186 if (GrGLTexture::kBottomUp_Orientation == orientation) {
187 return false;
188 } else {
189 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
190 }
191 return true;
192}
193
194///////////////////////////////////////////////////////////////////////////////
195
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000196void GrGpuGL::flushTextureMatrix(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000197 const GrDrawState& drawState = this->getDrawState();
198 const GrGLTexture* texture =
199 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000200 if (NULL != texture) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000201
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000202 bool orientationChange = fCurrentProgram->fTextureOrientation[s] !=
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000203 texture->orientation();
204
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000205 UniformHandle matrixUni = fCurrentProgram->fUniforms.fStages[s].fTextureMatrixUni;
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000206
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000207 const GrMatrix& hwMatrix = fCurrentProgram->fTextureMatrices[s];
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000208 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000209
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000210 if (kInvalidUniformHandle != matrixUni &&
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000211 (orientationChange || !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000212
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000213 GrMatrix m = samplerMatrix;
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000214 AdjustTextureMatrix(texture, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000215
bsalomon@google.com91961302011-05-09 18:39:58 +0000216 // ES doesn't allow you to pass true to the transpose param,
217 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000218 GrGLfloat mt[] = {
219 GrScalarToFloat(m[GrMatrix::kMScaleX]),
220 GrScalarToFloat(m[GrMatrix::kMSkewY]),
221 GrScalarToFloat(m[GrMatrix::kMPersp0]),
222 GrScalarToFloat(m[GrMatrix::kMSkewX]),
223 GrScalarToFloat(m[GrMatrix::kMScaleY]),
224 GrScalarToFloat(m[GrMatrix::kMPersp1]),
225 GrScalarToFloat(m[GrMatrix::kMTransX]),
226 GrScalarToFloat(m[GrMatrix::kMTransY]),
227 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000228 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000229
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000230 fCurrentProgram->fUniformManager.setMatrix3f(matrixUni, mt);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000231 fCurrentProgram->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000232 }
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000233
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000234 fCurrentProgram->fTextureOrientation[s] = texture->orientation();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000235 }
junov@google.comf93e7172011-03-31 21:26:24 +0000236}
237
junov@google.comf93e7172011-03-31 21:26:24 +0000238
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000239void GrGpuGL::flushColorMatrix() {
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000240 UniformHandle matrixUni = fCurrentProgram->fUniforms.fColorMatrixUni;
241 UniformHandle vecUni = fCurrentProgram->fUniforms.fColorMatrixVecUni;
242 if (kInvalidUniformHandle != matrixUni && kInvalidUniformHandle != vecUni) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000243 const float* m = this->getDrawState().getColorMatrix();
244 GrGLfloat mt[] = {
245 m[0], m[5], m[10], m[15],
246 m[1], m[6], m[11], m[16],
247 m[2], m[7], m[12], m[17],
248 m[3], m[8], m[13], m[18],
249 };
250 static float scale = 1.0f / 255.0f;
251 GrGLfloat vec[] = {
252 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
253 };
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000254 fCurrentProgram->fUniformManager.setMatrix4f(matrixUni, mt);
255 fCurrentProgram->fUniformManager.set4fv(vecUni, 0, 1, vec);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000256 }
257}
258
Scroggo01b87ec2011-05-11 18:05:38 +0000259static const float ONE_OVER_255 = 1.f / 255.f;
260
261#define GR_COLOR_TO_VEC4(color) {\
262 GrColorUnpackR(color) * ONE_OVER_255,\
263 GrColorUnpackG(color) * ONE_OVER_255,\
264 GrColorUnpackB(color) * ONE_OVER_255,\
265 GrColorUnpackA(color) * ONE_OVER_255 \
266}
267
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000268void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000269 const ProgramDesc& desc = fCurrentProgram->getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000270 const GrDrawState& drawState = this->getDrawState();
271
bsalomon@google.come79c8152012-03-29 19:07:12 +0000272 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000273 // color will be specified per-vertex as an attribute
274 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000275 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000276 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000277 switch (desc.fColorInput) {
278 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000279 if (fHWConstAttribColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000280 // OpenGL ES only supports the float varieties of
281 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000282 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000283 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
284 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000285 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000286 }
287 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000288 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000289 if (fCurrentProgram->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000290 // OpenGL ES doesn't support unsigned byte varieties of
291 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000292 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000293 GrAssert(kInvalidUniformHandle != fCurrentProgram->fUniforms.fColorUni);
294 fCurrentProgram->fUniformManager.set4fv(fCurrentProgram->fUniforms.fColorUni,
295 0, 1, c);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000296 fCurrentProgram->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000297 }
298 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000299 case ProgramDesc::kSolidWhite_ColorInput:
300 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000301 break;
302 default:
303 GrCrash("Unknown color type.");
304 }
305 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000306 UniformHandle filterColorUni = fCurrentProgram->fUniforms.fColorFilterUni;
307 if (kInvalidUniformHandle != filterColorUni &&
308 fCurrentProgram->fColorFilterColor != drawState.getColorFilterColor()) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000309 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000310 fCurrentProgram->fUniformManager.set4fv(filterColorUni, 0, 1, c);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000311 fCurrentProgram->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000312 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000313}
314
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000315void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000316 const ProgramDesc& desc = fCurrentProgram->getDesc();
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000317 // const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000318
319
bsalomon@google.come79c8152012-03-29 19:07:12 +0000320 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000321 // coverage will be specified per-vertex as an attribute
322 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000323 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000324 } else {
325 switch (desc.fCoverageInput) {
326 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000327 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000328 // OpenGL ES only supports the float varieties of
329 // glVertexAttrib
330 float c[] = GR_COLOR_TO_VEC4(coverage);
331 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
332 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000333 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000334 }
335 break;
336 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000337 if (fCurrentProgram->fCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000338 // OpenGL ES doesn't support unsigned byte varieties of
339 // glUniform
340 float c[] = GR_COLOR_TO_VEC4(coverage);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000341 GrAssert(kInvalidUniformHandle != fCurrentProgram->fUniforms.fCoverageUni);
342 fCurrentProgram->fUniformManager.set4fv(fCurrentProgram->fUniforms.fCoverageUni,
343 0, 1, c);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000344 fCurrentProgram->fCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000345 }
346 break;
347 case ProgramDesc::kSolidWhite_ColorInput:
348 case ProgramDesc::kTransBlack_ColorInput:
349 break;
350 default:
351 GrCrash("Unknown coverage type.");
352 }
353 }
354}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000355
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000356bool GrGpuGL::flushGraphicsState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000357 const GrDrawState& drawState = this->getDrawState();
358
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000359 // GrGpu::setupClipAndFlushState should have already checked this
360 // and bailed if not true.
361 GrAssert(NULL != drawState.getRenderTarget());
362
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000363 if (kStencilPath_DrawType != type) {
364 this->flushMiscFixedFunctionState();
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000365
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000366 GrBlendCoeff srcCoeff;
367 GrBlendCoeff dstCoeff;
368 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
369 if (kSkipDraw_BlendOptFlag & blendOpts) {
370 return false;
371 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000372
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000373 GrCustomStage* customStages [GrDrawState::kNumStages];
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000374 GrGLProgram::Desc desc;
375 this->buildProgram(kDrawPoints_DrawType == type, blendOpts, dstCoeff, customStages, &desc);
376
377 fCurrentProgram.reset(fProgramCache->getProgram(desc, customStages));
378 if (NULL == fCurrentProgram.get()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000379 GrAssert(!"Failed to create program!");
380 return false;
381 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000382 fCurrentProgram.get()->ref();
junov@google.comf93e7172011-03-31 21:26:24 +0000383
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000384 if (fHWProgramID != fCurrentProgram->fProgramID) {
385 GL_CALL(UseProgram(fCurrentProgram->fProgramID));
386 fHWProgramID = fCurrentProgram->fProgramID;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000387 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000388 fCurrentProgram->overrideBlend(&srcCoeff, &dstCoeff);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000389 this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000390
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000391 GrColor color;
392 GrColor coverage;
393 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
394 color = 0;
395 coverage = 0;
396 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
397 color = 0xffffffff;
398 coverage = drawState.getCoverage();
399 } else {
400 color = drawState.getColor();
401 coverage = drawState.getCoverage();
402 }
403 this->flushColor(color);
404 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000405
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000406 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
407 if (this->isStageEnabled(s)) {
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000408#if GR_DEBUG
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000409 // check for circular rendering
410 GrAssert(NULL == drawState.getRenderTarget() ||
411 NULL == drawState.getTexture(s) ||
412 drawState.getTexture(s)->asRenderTarget() !=
413 drawState.getRenderTarget());
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000414#endif
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000415 this->flushBoundTextureAndParams(s);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000416
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000417 this->flushTextureMatrix(s);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000418
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000419 if (NULL != fCurrentProgram->fProgramStage[s]) {
420 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
421 const GrGLTexture* texture = static_cast<const GrGLTexture*>(
422 this->getDrawState().getTexture(s));
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000423 fCurrentProgram->fProgramStage[s]->setData(fCurrentProgram->fUniformManager,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000424 *sampler.getCustomStage(),
425 drawState.getRenderTarget(), s);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000426 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000427 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000428 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000429 this->flushColorMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000430 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000431 this->flushStencil(type);
432 this->flushViewMatrix(type);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000433 this->flushScissor();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000434 this->flushAAState(type);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000435
436 GrIRect* rect = NULL;
437 GrIRect clipBounds;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000438 if (drawState.isClipState()) {
bsalomon@google.com4c883782012-06-04 19:05:11 +0000439 fClip.getConservativeBounds().roundOut(&clipBounds);
440 rect = &clipBounds;
441 }
442 // This must come after textures are flushed because a texture may need
443 // to be msaa-resolved (which will modify bound FBO state).
444 this->flushRenderTarget(rect);
445
junov@google.comf93e7172011-03-31 21:26:24 +0000446 return true;
447}
448
bsalomon@google.com2717d562012-05-07 19:10:52 +0000449#if GR_TEXT_SCALAR_IS_USHORT
450 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
451 #define TEXT_COORDS_ARE_NORMALIZED 1
452#elif GR_TEXT_SCALAR_IS_FLOAT
453 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
454 #define TEXT_COORDS_ARE_NORMALIZED 0
455#elif GR_TEXT_SCALAR_IS_FIXED
456 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
457 #define TEXT_COORDS_ARE_NORMALIZED 0
458#else
459 #error "unknown GR_TEXT_SCALAR type"
460#endif
461
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000462void GrGpuGL::setupGeometry(int* startVertex,
bsalomon@google.com49209392012-06-05 15:13:46 +0000463 int* startIndex,
464 int vertexCount,
465 int indexCount) {
junov@google.comf93e7172011-03-31 21:26:24 +0000466
467 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000468 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000469 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000470 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000471
bsalomon@google.come79c8152012-03-29 19:07:12 +0000472 GrVertexLayout currLayout = this->getVertexLayout();
473
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000474 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000475 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000476 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000477 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000478 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000479 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000480 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000481 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000482 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000483 int oldEdgeOffset;
484
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000485 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
486 fHWGeometryState.fVertexLayout,
487 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000488 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000489 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000490 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000491 bool indexed = NULL != startIndex;
492
493 int extraVertexOffset;
494 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000495 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000496
497 GrGLenum scalarType;
498 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000499 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000500 scalarType = TEXT_COORDS_GL_TYPE;
501 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000502 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000503 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
504 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000505 texCoordNorm = false;
506 }
507
508 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
509 *startVertex = 0;
510 if (indexed) {
511 *startIndex += extraIndexOffset;
512 }
513
514 // all the Pointers must be set if any of these are true
515 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
516 vertexOffset != fHWGeometryState.fVertexOffset ||
517 newStride != oldStride;
518
519 // position and tex coord offsets change if above conditions are true
520 // or the type/normalization changed based on text vs nontext type coords.
521 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000522 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000523 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000524 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000525
526 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000527 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000528 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000529 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000530 fHWGeometryState.fVertexOffset = vertexOffset;
531 }
532
tomhudson@google.com93813632011-10-27 20:21:16 +0000533 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000534 if (newTexCoordOffsets[t] > 0) {
535 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000536 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000537 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000538 GL_CALL(EnableVertexAttribArray(idx));
539 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000540 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000541 } else if (posAndTexChange ||
542 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000543 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000544 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000545 }
546 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000547 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000548 }
549 }
550
551 if (newColorOffset > 0) {
552 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000553 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000554 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000555 GL_CALL(EnableVertexAttribArray(idx));
556 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000557 true, newStride, colorOffset));
558 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000559 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000560 true, newStride, colorOffset));
561 }
562 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000563 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000564 }
565
bsalomon@google.coma3108262011-10-10 14:08:47 +0000566 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000567 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000568 int idx = GrGLProgram::CoverageAttributeIdx();
569 if (oldCoverageOffset <= 0) {
570 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000571 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000572 true, newStride, coverageOffset));
573 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000574 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000575 true, newStride, coverageOffset));
576 }
577 } else if (oldCoverageOffset > 0) {
578 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
579 }
580
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000581 if (newEdgeOffset > 0) {
582 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
583 int idx = GrGLProgram::EdgeAttributeIdx();
584 if (oldEdgeOffset <= 0) {
585 GL_CALL(EnableVertexAttribArray(idx));
586 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
587 false, newStride, edgeOffset));
588 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
589 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
590 false, newStride, edgeOffset));
591 }
592 } else if (oldEdgeOffset > 0) {
593 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
594 }
595
bsalomon@google.come79c8152012-03-29 19:07:12 +0000596 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000597 fHWGeometryState.fArrayPtrsDirty = false;
598}
599
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000600namespace {
601
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000602void setup_custom_stage(GrGLProgram::Desc::StageDesc* stage,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000603 const GrSamplerState& sampler,
604 GrCustomStage** customStages,
605 GrGLProgram* program, int index) {
606 GrCustomStage* customStage = sampler.getCustomStage();
607 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000608 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000609 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000610 customStages[index] = customStage;
611 } else {
612 stage->fCustomStageKey = 0;
613 customStages[index] = NULL;
614 }
615}
616
617}
618
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000619void GrGpuGL::buildProgram(bool isPoints,
bsalomon@google.com49209392012-06-05 15:13:46 +0000620 BlendOptFlags blendOpts,
621 GrBlendCoeff dstCoeff,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000622 GrCustomStage** customStages,
623 ProgramDesc* desc) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000624 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000625
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000626 // This should already have been caught
627 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
628
629 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
630
631 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
632 kEmitCoverage_BlendOptFlag));
633
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000634 // The descriptor is used as a cache key. Thus when a field of the
635 // descriptor will not affect program generation (because of the vertex
636 // layout in use or other descriptor field settings) it should be set
637 // to a canonical value to avoid duplicate programs with different keys.
638
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000639 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000640 desc->fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000641
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000642 desc->fEmitsPointSize = isPoints;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000643
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000644 bool requiresAttributeColors =
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000645 !skipColor && SkToBool(desc->fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000646 bool requiresAttributeCoverage =
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000647 !skipCoverage && SkToBool(desc->fVertexLayout &
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000648 kCoverage_VertexLayoutBit);
649
650 // fColorInput/fCoverageInput records how colors are specified for the.
651 // program. So we strip the bits from the layout to avoid false negatives
652 // when searching for an existing program in the cache.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000653 desc->fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000654
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000655 desc->fColorFilterXfermode = skipColor ?
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000656 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000657 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000658
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000659 desc->fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000660
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000661 // no reason to do edge aa or look at per-vertex coverage if coverage is
662 // ignored
663 if (skipCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000664 desc->fVertexLayout &= ~(kEdge_VertexLayoutBit |
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000665 kCoverage_VertexLayoutBit);
666 }
667
668 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
669 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
670 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000671 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000672 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000673 desc->fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000674 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000675 desc->fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000676 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000677 desc->fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000678 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000679 desc->fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000680 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000681
682 bool covIsSolidWhite = !requiresAttributeCoverage &&
683 0xffffffff == drawState.getCoverage();
684
685 if (skipCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000686 desc->fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000687 } else if (covIsSolidWhite) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000688 desc->fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000689 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000690 desc->fCoverageInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000691 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000692 desc->fCoverageInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000693 }
junov@google.comf93e7172011-03-31 21:26:24 +0000694
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000695 int lastEnabledStage = -1;
696
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000697 if (!skipCoverage && (desc->fVertexLayout &
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000698 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000699 desc->fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000700 } else {
701 // use canonical value when not set to avoid cache misses
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000702 desc->fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000703 }
704
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000705 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000706 StageDesc& stage = desc->fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000707
708 stage.fOptFlags = 0;
709 stage.setEnabled(this->isStageEnabled(s));
710
711 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
712 skipCoverage;
713
714 if (!skip && stage.isEnabled()) {
715 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000716 const GrGLTexture* texture =
717 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000718 GrAssert(NULL != texture);
719 const GrSamplerState& sampler = drawState.getSampler(s);
720 // we matrix to invert when orientation is TopDown, so make sure
721 // we aren't in that case before flagging as identity.
722 if (TextureMatrixIsIdentity(texture, sampler)) {
723 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
724 } else if (!sampler.getMatrix().hasPerspective()) {
725 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
726 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000727
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000728 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000729 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000730 if (GrPixelConfigIsAlphaOnly(texture->config())) {
731 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000732 // the shader must smear the single channel after
733 // reading the texture
734 if (this->glCaps().textureRedSupport()) {
735 // we can use R8 textures so use kSmearRed
736 stage.fInConfigFlags |=
737 StageDesc::kSmearRed_InConfigFlag;
738 } else {
739 // we can use A8 textures so use kSmearAlpha
740 stage.fInConfigFlags |=
741 StageDesc::kSmearAlpha_InConfigFlag;
742 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000743 } else if (sampler.swapsRAndB()) {
744 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
745 }
746 }
747 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000748 // The shader generator assumes that color channels are bytes
749 // when rounding.
750 GrAssert(4 == GrBytesPerPixel(texture->config()));
751 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
752 fUnpremulConversion) {
753 stage.fInConfigFlags |=
754 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
755 } else {
756 stage.fInConfigFlags |=
757 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
758 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000759 }
760
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000761 setup_custom_stage(&stage, sampler, customStages, fCurrentProgram.get(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000762
junov@google.comf93e7172011-03-31 21:26:24 +0000763 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000764 stage.fOptFlags = 0;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000765 stage.fInConfigFlags = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000766 stage.fCustomStageKey = 0;
767 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000768 }
769 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000770
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000771 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000772 // The shader generator assumes that color channels are bytes
773 // when rounding.
774 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
775 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000776 desc->fOutputConfig =
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000777 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
778 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000779 desc->fOutputConfig =
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000780 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
781 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000782 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000783 desc->fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000784 }
785
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000786 desc->fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000787
788 // currently the experimental GS will only work with triangle prims
789 // (and it doesn't do anything other than pass through values from
790 // the VS to the FS anyway).
791#if 0 && GR_GL_EXPERIMENTAL_GS
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000792 desc->fExperimentalGS = this->getCaps().fGeometryShaderSupport;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000793#endif
794
bsalomon@google.coma3108262011-10-10 14:08:47 +0000795 // we want to avoid generating programs with different "first cov stage"
796 // values when they would compute the same result.
797 // We set field in the desc to kNumStages when either there are no
798 // coverage stages or the distinction between coverage and color is
799 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000800 int firstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000801 desc->fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000802 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000803 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000804 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000805 }
806
807 // other coverage inputs
808 if (!hasCoverage) {
809 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000810 requiresAttributeCoverage ||
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000811 (desc->fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000812 }
813
814 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000815 // color filter is applied between color/coverage computation
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000816 if (SkXfermode::kDst_Mode != desc->fColorFilterXfermode) {
817 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000818 }
819
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000820 if (this->getCaps().fDualSourceBlendingSupport &&
821 !(blendOpts & (kEmitCoverage_BlendOptFlag |
822 kCoverageAsAlpha_BlendOptFlag))) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000823 if (kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000824 // write the coverage value to second color
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000825 desc->fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
826 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000827 } else if (kSA_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000828 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
829 // cover
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000830 desc->fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
831 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000832 } else if (kSC_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000833 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
834 // cover
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000835 desc->fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
836 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000837 }
838 }
839 }
junov@google.comf93e7172011-03-31 21:26:24 +0000840}