blob: 1f3216f3efe642d2dd8b127299b78d455fa1de1b [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.com898e7b52012-06-01 20:42:15 +000010#include "effects/GrGradientEffects.h"
11
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000012#include "GrCustomStage.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000013#include "GrGLProgramStage.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000014#include "GrGpuVertex.h"
junov@google.comf93e7172011-03-31 21:26:24 +000015
junov@google.comf93e7172011-03-31 21:26:24 +000016#define SKIP_CACHE_CHECK true
17#define GR_UINT32_MAX static_cast<uint32_t>(-1)
18
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000019void GrGpuGL::ProgramCache::Entry::copyAndTakeOwnership(Entry& entry) {
20 fProgramData.copyAndTakeOwnership(entry.fProgramData);
21 fKey = entry.fKey; // ownership transfer
22 fLRUStamp = entry.fLRUStamp;
23}
junov@google.comf93e7172011-03-31 21:26:24 +000024
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000025GrGpuGL::ProgramCache::ProgramCache(const GrGLContextInfo& gl)
26 : fCount(0)
27 , fCurrLRUStamp(0)
28 , fGL(gl) {
29}
junov@google.comf93e7172011-03-31 21:26:24 +000030
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000031GrGpuGL::ProgramCache::~ProgramCache() {
32 for (int i = 0; i < fCount; ++i) {
33 GrGpuGL::DeleteProgram(fGL.interface(),
34 &fEntries[i].fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000035 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000036}
junov@google.comf93e7172011-03-31 21:26:24 +000037
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000038void GrGpuGL::ProgramCache::abandon() {
39 fCount = 0;
40}
41
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000042GrGLProgram::CachedData* GrGpuGL::ProgramCache::getProgramData(
43 const GrGLProgram& desc,
44 GrCustomStage** stages) {
45 Entry newEntry;
46 newEntry.fKey.setKeyData(desc.keyData());
junov@google.comf7c00f62011-08-18 18:15:16 +000047
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000048 Entry* entry = fHashCache.find(newEntry.fKey);
49 if (NULL == entry) {
50 if (!desc.genProgram(fGL, stages, &newEntry.fProgramData)) {
51 return NULL;
52 }
53 if (fCount < kMaxEntries) {
54 entry = fEntries + fCount;
55 ++fCount;
56 } else {
57 GrAssert(kMaxEntries == fCount);
58 entry = fEntries;
59 for (int i = 1; i < kMaxEntries; ++i) {
60 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
61 entry = fEntries + i;
junov@google.comf93e7172011-03-31 21:26:24 +000062 }
junov@google.comf93e7172011-03-31 21:26:24 +000063 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000064 fHashCache.remove(entry->fKey, entry);
65 GrGpuGL::DeleteProgram(fGL.interface(),
66 &entry->fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000067 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000068 entry->copyAndTakeOwnership(newEntry);
69 fHashCache.insert(entry->fKey, entry);
junov@google.comf93e7172011-03-31 21:26:24 +000070 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000071
72 entry->fLRUStamp = fCurrLRUStamp;
73 if (GR_UINT32_MAX == fCurrLRUStamp) {
74 // wrap around! just trash our LRU, one time hit.
75 for (int i = 0; i < fCount; ++i) {
76 fEntries[i].fLRUStamp = 0;
77 }
78 }
79 ++fCurrLRUStamp;
80 return &entry->fProgramData;
81}
junov@google.comf93e7172011-03-31 21:26:24 +000082
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000083void GrGpuGL::DeleteProgram(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +000084 CachedData* programData) {
85 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000086 if (programData->fGShaderID) {
87 GR_GL_CALL(gl, DeleteShader(programData->fGShaderID));
88 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000089 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
90 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +000091 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
92}
93
bsalomon@google.com1e257a52011-07-06 19:52:16 +000094////////////////////////////////////////////////////////////////////////////////
95
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000096void GrGpuGL::abandonResources(){
97 INHERITED::abandonResources();
98 fProgramCache->abandon();
99 fHWProgramID = 0;
100}
101
102////////////////////////////////////////////////////////////////////////////////
103
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000104#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
105
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000106void GrGpuGL::flushViewMatrix() {
bsalomon@google.com4c883782012-06-04 19:05:11 +0000107 const GrGLRenderTarget* rt = static_cast<const GrGLRenderTarget*>(this->getDrawState().getRenderTarget());
108 SkISize viewportSize;
109 const GrGLIRect& viewport = rt->getViewport();
110 viewportSize.set(viewport.fWidth, viewport.fHeight);
junov@google.comf93e7172011-03-31 21:26:24 +0000111
bsalomon@google.com4c883782012-06-04 19:05:11 +0000112 const GrMatrix& vm = this->getDrawState().getViewMatrix();
113
114 if (!fProgramData->fViewMatrix.cheapEqualTo(vm) ||
115 fProgramData->fViewportSize != viewportSize) {
116
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000117 GrMatrix m;
118 m.setAll(
bsalomon@google.com4c883782012-06-04 19:05:11 +0000119 GrIntToScalar(2) / viewportSize.fWidth, 0, -GR_Scalar1,
120 0,-GrIntToScalar(2) / viewportSize.fHeight, GR_Scalar1,
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000121 0, 0, GrMatrix::I()[8]);
122 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000123
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000124 // ES doesn't allow you to pass true to the transpose param,
125 // so do our own transpose
126 GrGLfloat mt[] = {
127 GrScalarToFloat(m[GrMatrix::kMScaleX]),
128 GrScalarToFloat(m[GrMatrix::kMSkewY]),
129 GrScalarToFloat(m[GrMatrix::kMPersp0]),
130 GrScalarToFloat(m[GrMatrix::kMSkewX]),
131 GrScalarToFloat(m[GrMatrix::kMScaleY]),
132 GrScalarToFloat(m[GrMatrix::kMPersp1]),
133 GrScalarToFloat(m[GrMatrix::kMTransX]),
134 GrScalarToFloat(m[GrMatrix::kMTransY]),
135 GrScalarToFloat(m[GrMatrix::kMPersp2])
136 };
137
bsalomon@google.com341767c2012-05-11 20:47:39 +0000138 GrAssert(GrGLProgram::kUnusedUniform !=
139 fProgramData->fUniLocations.fViewMatrixUni);
140 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
141 1, false, mt));
142 fProgramData->fViewMatrix = vm;
bsalomon@google.com4c883782012-06-04 19:05:11 +0000143 fProgramData->fViewportSize = viewportSize;
bsalomon@google.com91961302011-05-09 18:39:58 +0000144 }
junov@google.comf93e7172011-03-31 21:26:24 +0000145}
146
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000147///////////////////////////////////////////////////////////////////////////////
junov@google.com6acc9b32011-05-16 18:32:07 +0000148
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000149// helpers for texture matrices
junov@google.com6acc9b32011-05-16 18:32:07 +0000150
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000151void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000152 GrMatrix* matrix) {
153 GrAssert(NULL != texture);
154 GrAssert(NULL != matrix);
155 GrGLTexture::Orientation orientation = texture->orientation();
156 if (GrGLTexture::kBottomUp_Orientation == orientation) {
157 GrMatrix invY;
158 invY.setAll(GR_Scalar1, 0, 0,
159 0, -GR_Scalar1, GR_Scalar1,
160 0, 0, GrMatrix::I()[8]);
161 matrix->postConcat(invY);
162 } else {
163 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
junov@google.com6acc9b32011-05-16 18:32:07 +0000164 }
165}
166
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000167bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
168 const GrSamplerState& sampler) {
169 GrAssert(NULL != texture);
170 if (!sampler.getMatrix().isIdentity()) {
171 return false;
172 }
173 GrGLTexture::Orientation orientation = texture->orientation();
174 if (GrGLTexture::kBottomUp_Orientation == orientation) {
175 return false;
176 } else {
177 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
178 }
179 return true;
180}
181
182///////////////////////////////////////////////////////////////////////////////
183
184void GrGpuGL::flushTextureMatrixAndDomain(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000185 const GrDrawState& drawState = this->getDrawState();
186 const GrGLTexture* texture =
187 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000188 if (NULL != texture) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000189
190 bool orientationChange = fProgramData->fTextureOrientation[s] !=
191 texture->orientation();
192
193 const GrGLint& matrixUni =
194 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
195
bsalomon@google.com341767c2012-05-11 20:47:39 +0000196 const GrMatrix& hwMatrix = fProgramData->fTextureMatrices[s];
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000197 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000198
199 if (GrGLProgram::kUnusedUniform != matrixUni &&
200 (orientationChange || !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000201
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000202 GrMatrix m = samplerMatrix;
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000203 AdjustTextureMatrix(texture, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000204
bsalomon@google.com91961302011-05-09 18:39:58 +0000205 // ES doesn't allow you to pass true to the transpose param,
206 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000207 GrGLfloat mt[] = {
208 GrScalarToFloat(m[GrMatrix::kMScaleX]),
209 GrScalarToFloat(m[GrMatrix::kMSkewY]),
210 GrScalarToFloat(m[GrMatrix::kMPersp0]),
211 GrScalarToFloat(m[GrMatrix::kMSkewX]),
212 GrScalarToFloat(m[GrMatrix::kMScaleY]),
213 GrScalarToFloat(m[GrMatrix::kMPersp1]),
214 GrScalarToFloat(m[GrMatrix::kMTransX]),
215 GrScalarToFloat(m[GrMatrix::kMTransY]),
216 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000217 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000218
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000219 GL_CALL(UniformMatrix3fv(matrixUni, 1, false, mt));
bsalomon@google.com341767c2012-05-11 20:47:39 +0000220 fProgramData->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000221 }
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000222
223 const GrGLint& domUni =
224 fProgramData->fUniLocations.fStages[s].fTexDomUni;
225 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
226 if (GrGLProgram::kUnusedUniform != domUni &&
227 (orientationChange ||fProgramData->fTextureDomain[s] != texDom)) {
228
229 fProgramData->fTextureDomain[s] = texDom;
230
231 float values[4] = {
232 GrScalarToFloat(texDom.left()),
233 GrScalarToFloat(texDom.top()),
234 GrScalarToFloat(texDom.right()),
235 GrScalarToFloat(texDom.bottom())
236 };
237
238 // vertical flip if necessary
239 if (GrGLTexture::kBottomUp_Orientation == texture->orientation()) {
240 values[1] = 1.0f - values[1];
241 values[3] = 1.0f - values[3];
242 // The top and bottom were just flipped, so correct the ordering
243 // of elements so that values = (l, t, r, b).
244 SkTSwap(values[1], values[3]);
245 }
246 GL_CALL(Uniform4fv(domUni, 1, values));
247 }
248 fProgramData->fTextureOrientation[s] = texture->orientation();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000249 }
junov@google.comf93e7172011-03-31 21:26:24 +0000250}
251
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000252void GrGpuGL::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000253 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000254 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000255 const GrGLTexture* texture =
256 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000257 if (texture->width() != fProgramData->fTextureWidth[s] ||
258 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000259
bsalomon@google.com99621082011-11-15 16:47:16 +0000260 float texelSize[] = {1.f / texture->width(),
261 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000262 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000263 fProgramData->fTextureWidth[s] = texture->width();
264 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000265 }
266 }
junov@google.comf93e7172011-03-31 21:26:24 +0000267}
268
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000269void GrGpuGL::flushColorMatrix() {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000270 const ProgramDesc& desc = fCurrentProgram.getDesc();
271 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
272 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
273 if (GrGLProgram::kUnusedUniform != matrixUni
274 && GrGLProgram::kUnusedUniform != vecUni) {
275 const float* m = this->getDrawState().getColorMatrix();
276 GrGLfloat mt[] = {
277 m[0], m[5], m[10], m[15],
278 m[1], m[6], m[11], m[16],
279 m[2], m[7], m[12], m[17],
280 m[3], m[8], m[13], m[18],
281 };
282 static float scale = 1.0f / 255.0f;
283 GrGLfloat vec[] = {
284 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
285 };
286 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
287 GL_CALL(Uniform4fv(vecUni, 1, vec));
288 }
289}
290
Scroggo01b87ec2011-05-11 18:05:38 +0000291static const float ONE_OVER_255 = 1.f / 255.f;
292
293#define GR_COLOR_TO_VEC4(color) {\
294 GrColorUnpackR(color) * ONE_OVER_255,\
295 GrColorUnpackG(color) * ONE_OVER_255,\
296 GrColorUnpackB(color) * ONE_OVER_255,\
297 GrColorUnpackA(color) * ONE_OVER_255 \
298}
299
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000300void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000301 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000302 const GrDrawState& drawState = this->getDrawState();
303
bsalomon@google.come79c8152012-03-29 19:07:12 +0000304 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000305 // color will be specified per-vertex as an attribute
306 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000307 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000308 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000309 switch (desc.fColorInput) {
310 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000311 if (fHWConstAttribColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000312 // OpenGL ES only supports the float varieties of
313 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000314 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000315 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
316 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000317 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000318 }
319 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000320 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000321 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000322 // OpenGL ES doesn't support unsigned byte varieties of
323 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000324 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000325 GrAssert(GrGLProgram::kUnusedUniform !=
326 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000327 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
328 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000329 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000330 }
331 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000332 case ProgramDesc::kSolidWhite_ColorInput:
333 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000334 break;
335 default:
336 GrCrash("Unknown color type.");
337 }
338 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000339 if (fProgramData->fUniLocations.fColorFilterUni
340 != GrGLProgram::kUnusedUniform
341 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000342 != drawState.getColorFilterColor()) {
343 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000344 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000345 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000346 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000347}
348
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000349void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000350 const ProgramDesc& desc = fCurrentProgram.getDesc();
351 const GrDrawState& drawState = this->getDrawState();
352
353
bsalomon@google.come79c8152012-03-29 19:07:12 +0000354 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000355 // coverage will be specified per-vertex as an attribute
356 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000357 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000358 } else {
359 switch (desc.fCoverageInput) {
360 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000361 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000362 // OpenGL ES only supports the float varieties of
363 // glVertexAttrib
364 float c[] = GR_COLOR_TO_VEC4(coverage);
365 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
366 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000367 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000368 }
369 break;
370 case ProgramDesc::kUniform_ColorInput:
371 if (fProgramData->fCoverage != coverage) {
372 // OpenGL ES doesn't support unsigned byte varieties of
373 // glUniform
374 float c[] = GR_COLOR_TO_VEC4(coverage);
375 GrAssert(GrGLProgram::kUnusedUniform !=
376 fProgramData->fUniLocations.fCoverageUni);
377 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
378 1, c));
379 fProgramData->fCoverage = coverage;
380 }
381 break;
382 case ProgramDesc::kSolidWhite_ColorInput:
383 case ProgramDesc::kTransBlack_ColorInput:
384 break;
385 default:
386 GrCrash("Unknown coverage type.");
387 }
388 }
389}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000390
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000391bool GrGpuGL::flushGraphicsState(GrPrimitiveType type) {
junov@google.comf93e7172011-03-31 21:26:24 +0000392 if (!flushGLStateCommon(type)) {
393 return false;
394 }
395
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000396 const GrDrawState& drawState = this->getDrawState();
397
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000398 GrBlendCoeff srcCoeff;
399 GrBlendCoeff dstCoeff;
400 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
401 if (kSkipDraw_BlendOptFlag & blendOpts) {
402 return false;
403 }
404
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000405 GrCustomStage* customStages [GrDrawState::kNumStages];
406 this->buildProgram(type, blendOpts, dstCoeff, customStages);
407 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
408 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000409 if (NULL == fProgramData) {
410 GrAssert(!"Failed to create program!");
411 return false;
412 }
junov@google.comf93e7172011-03-31 21:26:24 +0000413
414 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000415 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000416 fHWProgramID = fProgramData->fProgramID;
417 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000418 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
419 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000420
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000421 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000422 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000423 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
424 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000425 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000426 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
427 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000428 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000429 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000430 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000431 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000432 }
433 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000434 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000435
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000436 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000437
tomhudson@google.com93813632011-10-27 20:21:16 +0000438 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000439 if (this->isStageEnabled(s)) {
bsalomon@google.com4c883782012-06-04 19:05:11 +0000440
441 this->flushBoundTextureAndParams(s);
442
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000443 this->flushTextureMatrixAndDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000444
bsalomon@google.com40d92932011-12-13 18:40:47 +0000445 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000446
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000447 if (NULL != fProgramData->fCustomStage[s]) {
448 const GrSamplerState& sampler =
449 this->getDrawState().getSampler(s);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000450 const GrGLTexture* texture =
451 static_cast<const GrGLTexture*>(
452 this->getDrawState().getTexture(s));
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000453 fProgramData->fCustomStage[s]->setData(
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000454 this->glInterface(), *texture,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000455 *sampler.getCustomStage(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000456 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000457 }
junov@google.comf93e7172011-03-31 21:26:24 +0000458 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000459 this->flushColorMatrix();
bsalomon@google.com4c883782012-06-04 19:05:11 +0000460
461 GrIRect* rect = NULL;
462 GrIRect clipBounds;
463 if (drawState.isClipState() &&
464 fClip.hasConservativeBounds()) {
465 fClip.getConservativeBounds().roundOut(&clipBounds);
466 rect = &clipBounds;
467 }
468 // This must come after textures are flushed because a texture may need
469 // to be msaa-resolved (which will modify bound FBO state).
470 this->flushRenderTarget(rect);
471
junov@google.comf93e7172011-03-31 21:26:24 +0000472 return true;
473}
474
bsalomon@google.com2717d562012-05-07 19:10:52 +0000475#if GR_TEXT_SCALAR_IS_USHORT
476 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
477 #define TEXT_COORDS_ARE_NORMALIZED 1
478#elif GR_TEXT_SCALAR_IS_FLOAT
479 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
480 #define TEXT_COORDS_ARE_NORMALIZED 0
481#elif GR_TEXT_SCALAR_IS_FIXED
482 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
483 #define TEXT_COORDS_ARE_NORMALIZED 0
484#else
485 #error "unknown GR_TEXT_SCALAR type"
486#endif
487
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000488void GrGpuGL::setupGeometry(int* startVertex,
junov@google.comf93e7172011-03-31 21:26:24 +0000489 int* startIndex,
490 int vertexCount,
491 int indexCount) {
492
493 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000494 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000495 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000496 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000497
bsalomon@google.come79c8152012-03-29 19:07:12 +0000498 GrVertexLayout currLayout = this->getVertexLayout();
499
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000500 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000501 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000502 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000503 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000504 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000505 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000506 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000507 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000508 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000509 int oldEdgeOffset;
510
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000511 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
512 fHWGeometryState.fVertexLayout,
513 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000514 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000515 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000516 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000517 bool indexed = NULL != startIndex;
518
519 int extraVertexOffset;
520 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000521 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000522
523 GrGLenum scalarType;
524 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000525 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000526 scalarType = TEXT_COORDS_GL_TYPE;
527 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000528 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000529 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
530 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000531 texCoordNorm = false;
532 }
533
534 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
535 *startVertex = 0;
536 if (indexed) {
537 *startIndex += extraIndexOffset;
538 }
539
540 // all the Pointers must be set if any of these are true
541 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
542 vertexOffset != fHWGeometryState.fVertexOffset ||
543 newStride != oldStride;
544
545 // position and tex coord offsets change if above conditions are true
546 // or the type/normalization changed based on text vs nontext type coords.
547 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000548 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000549 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000550 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000551
552 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000553 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000554 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000555 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000556 fHWGeometryState.fVertexOffset = vertexOffset;
557 }
558
tomhudson@google.com93813632011-10-27 20:21:16 +0000559 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000560 if (newTexCoordOffsets[t] > 0) {
561 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000562 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000563 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000564 GL_CALL(EnableVertexAttribArray(idx));
565 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000566 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000567 } else if (posAndTexChange ||
568 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000569 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000570 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000571 }
572 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000573 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000574 }
575 }
576
577 if (newColorOffset > 0) {
578 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000579 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000580 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000581 GL_CALL(EnableVertexAttribArray(idx));
582 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000583 true, newStride, colorOffset));
584 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000585 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000586 true, newStride, colorOffset));
587 }
588 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000589 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000590 }
591
bsalomon@google.coma3108262011-10-10 14:08:47 +0000592 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000593 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000594 int idx = GrGLProgram::CoverageAttributeIdx();
595 if (oldCoverageOffset <= 0) {
596 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000597 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000598 true, newStride, coverageOffset));
599 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000600 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000601 true, newStride, coverageOffset));
602 }
603 } else if (oldCoverageOffset > 0) {
604 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
605 }
606
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000607 if (newEdgeOffset > 0) {
608 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
609 int idx = GrGLProgram::EdgeAttributeIdx();
610 if (oldEdgeOffset <= 0) {
611 GL_CALL(EnableVertexAttribArray(idx));
612 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
613 false, newStride, edgeOffset));
614 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
615 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
616 false, newStride, edgeOffset));
617 }
618 } else if (oldEdgeOffset > 0) {
619 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
620 }
621
bsalomon@google.come79c8152012-03-29 19:07:12 +0000622 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000623 fHWGeometryState.fArrayPtrsDirty = false;
624}
625
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000626namespace {
627
628void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
629 const GrSamplerState& sampler,
630 GrCustomStage** customStages,
631 GrGLProgram* program, int index) {
632 GrCustomStage* customStage = sampler.getCustomStage();
633 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000634 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000635 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000636 customStages[index] = customStage;
637 } else {
638 stage->fCustomStageKey = 0;
639 customStages[index] = NULL;
640 }
641}
642
643}
644
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000645void GrGpuGL::buildProgram(GrPrimitiveType type,
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000646 BlendOptFlags blendOpts,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000647 GrBlendCoeff dstCoeff,
648 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000649 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000650 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000651
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000652 // This should already have been caught
653 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
654
655 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
656
657 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
658 kEmitCoverage_BlendOptFlag));
659
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000660 // The descriptor is used as a cache key. Thus when a field of the
661 // descriptor will not affect program generation (because of the vertex
662 // layout in use or other descriptor field settings) it should be set
663 // to a canonical value to avoid duplicate programs with different keys.
664
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000665 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000666 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000667
668 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
669
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000670 bool requiresAttributeColors =
671 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000672 bool requiresAttributeCoverage =
673 !skipCoverage && SkToBool(desc.fVertexLayout &
674 kCoverage_VertexLayoutBit);
675
676 // fColorInput/fCoverageInput records how colors are specified for the.
677 // program. So we strip the bits from the layout to avoid false negatives
678 // when searching for an existing program in the cache.
679 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000680
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000681 desc.fColorFilterXfermode = skipColor ?
682 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000683 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000684
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000685 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
686
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000687 // no reason to do edge aa or look at per-vertex coverage if coverage is
688 // ignored
689 if (skipCoverage) {
690 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
691 kCoverage_VertexLayoutBit);
692 }
693
694 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
695 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
696 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000697 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000698 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000699 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000700 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000701 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000702 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000703 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000704 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000705 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000706 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000707
708 bool covIsSolidWhite = !requiresAttributeCoverage &&
709 0xffffffff == drawState.getCoverage();
710
711 if (skipCoverage) {
712 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
713 } else if (covIsSolidWhite) {
714 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
715 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
716 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
717 } else {
718 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
719 }
junov@google.comf93e7172011-03-31 21:26:24 +0000720
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000721 int lastEnabledStage = -1;
722
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000723 if (!skipCoverage && (desc.fVertexLayout &
724 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000725 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000726 } else {
727 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000728 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000729 }
730
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000731 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000732 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000733
734 stage.fOptFlags = 0;
735 stage.setEnabled(this->isStageEnabled(s));
736
737 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
738 skipCoverage;
739
740 if (!skip && stage.isEnabled()) {
741 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000742 const GrGLTexture* texture =
743 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000744 GrAssert(NULL != texture);
745 const GrSamplerState& sampler = drawState.getSampler(s);
746 // we matrix to invert when orientation is TopDown, so make sure
747 // we aren't in that case before flagging as identity.
748 if (TextureMatrixIsIdentity(texture, sampler)) {
749 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
750 } else if (!sampler.getMatrix().hasPerspective()) {
751 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
752 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000753
754 switch (sampler.getFilter()) {
755 // these both can use a regular texture2D()
756 case GrSamplerState::kNearest_Filter:
757 case GrSamplerState::kBilinear_Filter:
758 stage.fFetchMode = StageDesc::kSingle_FetchMode;
759 break;
760 // performs 4 texture2D()s
761 case GrSamplerState::k4x4Downsample_Filter:
762 stage.fFetchMode = StageDesc::k2x2_FetchMode;
763 break;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000764 default:
765 GrCrash("Unexpected filter!");
766 break;
767 }
768
769 if (sampler.hasTextureDomain()) {
770 GrAssert(GrSamplerState::kClamp_WrapMode ==
771 sampler.getWrapX() &&
772 GrSamplerState::kClamp_WrapMode ==
773 sampler.getWrapY());
774 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
775 }
776
777 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000778 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000779 if (GrPixelConfigIsAlphaOnly(texture->config())) {
780 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000781 // the shader must smear the single channel after
782 // reading the texture
783 if (this->glCaps().textureRedSupport()) {
784 // we can use R8 textures so use kSmearRed
785 stage.fInConfigFlags |=
786 StageDesc::kSmearRed_InConfigFlag;
787 } else {
788 // we can use A8 textures so use kSmearAlpha
789 stage.fInConfigFlags |=
790 StageDesc::kSmearAlpha_InConfigFlag;
791 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000792 } else if (sampler.swapsRAndB()) {
793 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
794 }
795 }
796 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000797 // The shader generator assumes that color channels are bytes
798 // when rounding.
799 GrAssert(4 == GrBytesPerPixel(texture->config()));
800 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
801 fUnpremulConversion) {
802 stage.fInConfigFlags |=
803 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
804 } else {
805 stage.fInConfigFlags |=
806 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
807 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000808 }
809
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000810 setup_custom_stage(&stage, sampler, customStages,
811 &fCurrentProgram, s);
812
junov@google.comf93e7172011-03-31 21:26:24 +0000813 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000814 stage.fOptFlags = 0;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000815 stage.fInConfigFlags = 0;
816 stage.fFetchMode = (StageDesc::FetchMode) 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000817 stage.fCustomStageKey = 0;
818 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000819 }
820 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000821
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000822 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000823 // The shader generator assumes that color channels are bytes
824 // when rounding.
825 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
826 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
827 desc.fOutputConfig =
828 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
829 } else {
830 desc.fOutputConfig =
831 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
832 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000833 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000834 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000835 }
836
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000837 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000838
839 // currently the experimental GS will only work with triangle prims
840 // (and it doesn't do anything other than pass through values from
841 // the VS to the FS anyway).
842#if 0 && GR_GL_EXPERIMENTAL_GS
843 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
844#endif
845
bsalomon@google.coma3108262011-10-10 14:08:47 +0000846 // we want to avoid generating programs with different "first cov stage"
847 // values when they would compute the same result.
848 // We set field in the desc to kNumStages when either there are no
849 // coverage stages or the distinction between coverage and color is
850 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000851 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +0000852 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000853 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000854 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000855 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000856 }
857
858 // other coverage inputs
859 if (!hasCoverage) {
860 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000861 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +0000862 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
863 }
864
865 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000866 // color filter is applied between color/coverage computation
867 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000868 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000869 }
870
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000871 if (this->getCaps().fDualSourceBlendingSupport &&
872 !(blendOpts & (kEmitCoverage_BlendOptFlag |
873 kCoverageAsAlpha_BlendOptFlag))) {
874 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000875 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000876 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000877 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000878 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000879 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
880 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000881 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000882 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000883 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000884 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
885 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000886 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000887 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000888 }
889 }
890 }
junov@google.comf93e7172011-03-31 21:26:24 +0000891}