blob: c230d4011816531b61c355e115233cb9aca84f2b [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) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000392 const GrDrawState& drawState = this->getDrawState();
393
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000394 // GrGpu::setupClipAndFlushState should have already checked this
395 // and bailed if not true.
396 GrAssert(NULL != drawState.getRenderTarget());
397
398 this->flushStencil();
399 this->flushAAState(type);
400
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000401 GrBlendCoeff srcCoeff;
402 GrBlendCoeff dstCoeff;
403 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
404 if (kSkipDraw_BlendOptFlag & blendOpts) {
405 return false;
406 }
407
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000408 GrCustomStage* customStages [GrDrawState::kNumStages];
409 this->buildProgram(type, blendOpts, dstCoeff, customStages);
410 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
411 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000412 if (NULL == fProgramData) {
413 GrAssert(!"Failed to create program!");
414 return false;
415 }
junov@google.comf93e7172011-03-31 21:26:24 +0000416
417 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000418 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000419 fHWProgramID = fProgramData->fProgramID;
420 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000421 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
422 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000423
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000424 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000425 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000426 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
427 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000428 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000429 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
430 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000431 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000432 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000433 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000434 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000435 }
436 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000437 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000438
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000439 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000440
tomhudson@google.com93813632011-10-27 20:21:16 +0000441 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000442 if (this->isStageEnabled(s)) {
bsalomon@google.com4c883782012-06-04 19:05:11 +0000443
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000444
445#if GR_DEBUG
446 // check for circular rendering
447 GrAssert(NULL == drawState.getRenderTarget() ||
448 NULL == drawState.getTexture(s) ||
449 drawState.getTexture(s)->asRenderTarget() !=
450 drawState.getRenderTarget());
451#endif
452
bsalomon@google.com4c883782012-06-04 19:05:11 +0000453 this->flushBoundTextureAndParams(s);
454
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000455 this->flushTextureMatrixAndDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000456
bsalomon@google.com40d92932011-12-13 18:40:47 +0000457 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000458
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000459 if (NULL != fProgramData->fCustomStage[s]) {
460 const GrSamplerState& sampler =
461 this->getDrawState().getSampler(s);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000462 const GrGLTexture* texture =
463 static_cast<const GrGLTexture*>(
464 this->getDrawState().getTexture(s));
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000465 fProgramData->fCustomStage[s]->setData(
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000466 this->glInterface(), *texture,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000467 *sampler.getCustomStage(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000468 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000469 }
junov@google.comf93e7172011-03-31 21:26:24 +0000470 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000471 this->flushColorMatrix();
bsalomon@google.com4c883782012-06-04 19:05:11 +0000472
473 GrIRect* rect = NULL;
474 GrIRect clipBounds;
475 if (drawState.isClipState() &&
476 fClip.hasConservativeBounds()) {
477 fClip.getConservativeBounds().roundOut(&clipBounds);
478 rect = &clipBounds;
479 }
480 // This must come after textures are flushed because a texture may need
481 // to be msaa-resolved (which will modify bound FBO state).
482 this->flushRenderTarget(rect);
483
junov@google.comf93e7172011-03-31 21:26:24 +0000484 return true;
485}
486
bsalomon@google.com2717d562012-05-07 19:10:52 +0000487#if GR_TEXT_SCALAR_IS_USHORT
488 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
489 #define TEXT_COORDS_ARE_NORMALIZED 1
490#elif GR_TEXT_SCALAR_IS_FLOAT
491 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
492 #define TEXT_COORDS_ARE_NORMALIZED 0
493#elif GR_TEXT_SCALAR_IS_FIXED
494 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
495 #define TEXT_COORDS_ARE_NORMALIZED 0
496#else
497 #error "unknown GR_TEXT_SCALAR type"
498#endif
499
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000500void GrGpuGL::setupGeometry(int* startVertex,
junov@google.comf93e7172011-03-31 21:26:24 +0000501 int* startIndex,
502 int vertexCount,
503 int indexCount) {
504
505 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000506 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000507 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000508 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000509
bsalomon@google.come79c8152012-03-29 19:07:12 +0000510 GrVertexLayout currLayout = this->getVertexLayout();
511
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000512 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000513 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000514 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000515 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000516 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000517 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000518 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000519 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000520 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000521 int oldEdgeOffset;
522
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000523 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
524 fHWGeometryState.fVertexLayout,
525 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000526 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000527 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000528 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000529 bool indexed = NULL != startIndex;
530
531 int extraVertexOffset;
532 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000533 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000534
535 GrGLenum scalarType;
536 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000537 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000538 scalarType = TEXT_COORDS_GL_TYPE;
539 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000540 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000541 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
542 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000543 texCoordNorm = false;
544 }
545
546 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
547 *startVertex = 0;
548 if (indexed) {
549 *startIndex += extraIndexOffset;
550 }
551
552 // all the Pointers must be set if any of these are true
553 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
554 vertexOffset != fHWGeometryState.fVertexOffset ||
555 newStride != oldStride;
556
557 // position and tex coord offsets change if above conditions are true
558 // or the type/normalization changed based on text vs nontext type coords.
559 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000560 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000561 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000562 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000563
564 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000565 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000566 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000567 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000568 fHWGeometryState.fVertexOffset = vertexOffset;
569 }
570
tomhudson@google.com93813632011-10-27 20:21:16 +0000571 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000572 if (newTexCoordOffsets[t] > 0) {
573 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000574 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000575 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000576 GL_CALL(EnableVertexAttribArray(idx));
577 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000578 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000579 } else if (posAndTexChange ||
580 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000581 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000582 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000583 }
584 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000585 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000586 }
587 }
588
589 if (newColorOffset > 0) {
590 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000591 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000592 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000593 GL_CALL(EnableVertexAttribArray(idx));
594 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000595 true, newStride, colorOffset));
596 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000597 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000598 true, newStride, colorOffset));
599 }
600 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000601 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000602 }
603
bsalomon@google.coma3108262011-10-10 14:08:47 +0000604 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000605 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000606 int idx = GrGLProgram::CoverageAttributeIdx();
607 if (oldCoverageOffset <= 0) {
608 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000609 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000610 true, newStride, coverageOffset));
611 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000612 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000613 true, newStride, coverageOffset));
614 }
615 } else if (oldCoverageOffset > 0) {
616 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
617 }
618
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000619 if (newEdgeOffset > 0) {
620 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
621 int idx = GrGLProgram::EdgeAttributeIdx();
622 if (oldEdgeOffset <= 0) {
623 GL_CALL(EnableVertexAttribArray(idx));
624 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
625 false, newStride, edgeOffset));
626 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
627 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
628 false, newStride, edgeOffset));
629 }
630 } else if (oldEdgeOffset > 0) {
631 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
632 }
633
bsalomon@google.come79c8152012-03-29 19:07:12 +0000634 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000635 fHWGeometryState.fArrayPtrsDirty = false;
636}
637
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000638namespace {
639
640void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
641 const GrSamplerState& sampler,
642 GrCustomStage** customStages,
643 GrGLProgram* program, int index) {
644 GrCustomStage* customStage = sampler.getCustomStage();
645 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000646 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000647 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000648 customStages[index] = customStage;
649 } else {
650 stage->fCustomStageKey = 0;
651 customStages[index] = NULL;
652 }
653}
654
655}
656
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000657void GrGpuGL::buildProgram(GrPrimitiveType type,
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000658 BlendOptFlags blendOpts,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000659 GrBlendCoeff dstCoeff,
660 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000661 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000662 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000663
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000664 // This should already have been caught
665 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
666
667 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
668
669 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
670 kEmitCoverage_BlendOptFlag));
671
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000672 // The descriptor is used as a cache key. Thus when a field of the
673 // descriptor will not affect program generation (because of the vertex
674 // layout in use or other descriptor field settings) it should be set
675 // to a canonical value to avoid duplicate programs with different keys.
676
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000677 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000678 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000679
680 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
681
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000682 bool requiresAttributeColors =
683 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000684 bool requiresAttributeCoverage =
685 !skipCoverage && SkToBool(desc.fVertexLayout &
686 kCoverage_VertexLayoutBit);
687
688 // fColorInput/fCoverageInput records how colors are specified for the.
689 // program. So we strip the bits from the layout to avoid false negatives
690 // when searching for an existing program in the cache.
691 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000692
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000693 desc.fColorFilterXfermode = skipColor ?
694 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000695 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000696
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000697 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
698
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000699 // no reason to do edge aa or look at per-vertex coverage if coverage is
700 // ignored
701 if (skipCoverage) {
702 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
703 kCoverage_VertexLayoutBit);
704 }
705
706 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
707 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
708 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000709 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000710 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000711 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000712 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000713 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000714 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000715 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000716 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000717 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000718 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000719
720 bool covIsSolidWhite = !requiresAttributeCoverage &&
721 0xffffffff == drawState.getCoverage();
722
723 if (skipCoverage) {
724 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
725 } else if (covIsSolidWhite) {
726 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
727 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
728 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
729 } else {
730 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
731 }
junov@google.comf93e7172011-03-31 21:26:24 +0000732
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000733 int lastEnabledStage = -1;
734
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000735 if (!skipCoverage && (desc.fVertexLayout &
736 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000737 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000738 } else {
739 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000740 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000741 }
742
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000743 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000744 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000745
746 stage.fOptFlags = 0;
747 stage.setEnabled(this->isStageEnabled(s));
748
749 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
750 skipCoverage;
751
752 if (!skip && stage.isEnabled()) {
753 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000754 const GrGLTexture* texture =
755 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000756 GrAssert(NULL != texture);
757 const GrSamplerState& sampler = drawState.getSampler(s);
758 // we matrix to invert when orientation is TopDown, so make sure
759 // we aren't in that case before flagging as identity.
760 if (TextureMatrixIsIdentity(texture, sampler)) {
761 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
762 } else if (!sampler.getMatrix().hasPerspective()) {
763 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
764 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000765
766 switch (sampler.getFilter()) {
767 // these both can use a regular texture2D()
768 case GrSamplerState::kNearest_Filter:
769 case GrSamplerState::kBilinear_Filter:
770 stage.fFetchMode = StageDesc::kSingle_FetchMode;
771 break;
772 // performs 4 texture2D()s
773 case GrSamplerState::k4x4Downsample_Filter:
774 stage.fFetchMode = StageDesc::k2x2_FetchMode;
775 break;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000776 default:
777 GrCrash("Unexpected filter!");
778 break;
779 }
780
781 if (sampler.hasTextureDomain()) {
782 GrAssert(GrSamplerState::kClamp_WrapMode ==
783 sampler.getWrapX() &&
784 GrSamplerState::kClamp_WrapMode ==
785 sampler.getWrapY());
786 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
787 }
788
789 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000790 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000791 if (GrPixelConfigIsAlphaOnly(texture->config())) {
792 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000793 // the shader must smear the single channel after
794 // reading the texture
795 if (this->glCaps().textureRedSupport()) {
796 // we can use R8 textures so use kSmearRed
797 stage.fInConfigFlags |=
798 StageDesc::kSmearRed_InConfigFlag;
799 } else {
800 // we can use A8 textures so use kSmearAlpha
801 stage.fInConfigFlags |=
802 StageDesc::kSmearAlpha_InConfigFlag;
803 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000804 } else if (sampler.swapsRAndB()) {
805 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
806 }
807 }
808 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000809 // The shader generator assumes that color channels are bytes
810 // when rounding.
811 GrAssert(4 == GrBytesPerPixel(texture->config()));
812 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
813 fUnpremulConversion) {
814 stage.fInConfigFlags |=
815 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
816 } else {
817 stage.fInConfigFlags |=
818 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
819 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000820 }
821
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000822 setup_custom_stage(&stage, sampler, customStages,
823 &fCurrentProgram, s);
824
junov@google.comf93e7172011-03-31 21:26:24 +0000825 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000826 stage.fOptFlags = 0;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000827 stage.fInConfigFlags = 0;
828 stage.fFetchMode = (StageDesc::FetchMode) 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000829 stage.fCustomStageKey = 0;
830 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000831 }
832 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000833
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000834 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000835 // The shader generator assumes that color channels are bytes
836 // when rounding.
837 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
838 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
839 desc.fOutputConfig =
840 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
841 } else {
842 desc.fOutputConfig =
843 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
844 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000845 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000846 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000847 }
848
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000849 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000850
851 // currently the experimental GS will only work with triangle prims
852 // (and it doesn't do anything other than pass through values from
853 // the VS to the FS anyway).
854#if 0 && GR_GL_EXPERIMENTAL_GS
855 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
856#endif
857
bsalomon@google.coma3108262011-10-10 14:08:47 +0000858 // we want to avoid generating programs with different "first cov stage"
859 // values when they would compute the same result.
860 // We set field in the desc to kNumStages when either there are no
861 // coverage stages or the distinction between coverage and color is
862 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000863 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +0000864 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000865 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000866 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000867 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000868 }
869
870 // other coverage inputs
871 if (!hasCoverage) {
872 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000873 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +0000874 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
875 }
876
877 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000878 // color filter is applied between color/coverage computation
879 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000880 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000881 }
882
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000883 if (this->getCaps().fDualSourceBlendingSupport &&
884 !(blendOpts & (kEmitCoverage_BlendOptFlag |
885 kCoverageAsAlpha_BlendOptFlag))) {
886 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000887 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000888 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000889 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000890 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000891 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
892 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000893 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000894 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000895 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000896 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
897 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000898 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000899 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000900 }
901 }
902 }
junov@google.comf93e7172011-03-31 21:26:24 +0000903}