blob: a38ae2dc98038afdfb7394f764da78955c3b2bb5 [file] [log] [blame]
junov@google.comf93e7172011-03-31 21:26:24 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
junov@google.comf93e7172011-03-31 21:26:24 +00006 */
7
bsalomon@google.com5739d2c2012-05-31 15:07:19 +00008#include "GrGpuGL.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000010#include "GrCustomStage.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000011#include "GrGLProgramStage.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000012#include "GrGpuVertex.h"
junov@google.comf93e7172011-03-31 21:26:24 +000013
junov@google.comf93e7172011-03-31 21:26:24 +000014#define SKIP_CACHE_CHECK true
15#define GR_UINT32_MAX static_cast<uint32_t>(-1)
16
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000017void GrGpuGL::ProgramCache::Entry::copyAndTakeOwnership(Entry& entry) {
18 fProgramData.copyAndTakeOwnership(entry.fProgramData);
19 fKey = entry.fKey; // ownership transfer
20 fLRUStamp = entry.fLRUStamp;
21}
junov@google.comf93e7172011-03-31 21:26:24 +000022
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000023GrGpuGL::ProgramCache::ProgramCache(const GrGLContextInfo& gl)
24 : fCount(0)
25 , fCurrLRUStamp(0)
26 , fGL(gl) {
27}
junov@google.comf93e7172011-03-31 21:26:24 +000028
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000029GrGpuGL::ProgramCache::~ProgramCache() {
30 for (int i = 0; i < fCount; ++i) {
31 GrGpuGL::DeleteProgram(fGL.interface(),
32 &fEntries[i].fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000033 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000034}
junov@google.comf93e7172011-03-31 21:26:24 +000035
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000036void GrGpuGL::ProgramCache::abandon() {
37 fCount = 0;
38}
39
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000040GrGLProgram::CachedData* GrGpuGL::ProgramCache::getProgramData(
41 const GrGLProgram& desc,
42 GrCustomStage** stages) {
43 Entry newEntry;
44 newEntry.fKey.setKeyData(desc.keyData());
junov@google.comf7c00f62011-08-18 18:15:16 +000045
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000046 Entry* entry = fHashCache.find(newEntry.fKey);
47 if (NULL == entry) {
48 if (!desc.genProgram(fGL, stages, &newEntry.fProgramData)) {
49 return NULL;
50 }
51 if (fCount < kMaxEntries) {
52 entry = fEntries + fCount;
53 ++fCount;
54 } else {
55 GrAssert(kMaxEntries == fCount);
56 entry = fEntries;
57 for (int i = 1; i < kMaxEntries; ++i) {
58 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
59 entry = fEntries + i;
junov@google.comf93e7172011-03-31 21:26:24 +000060 }
junov@google.comf93e7172011-03-31 21:26:24 +000061 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000062 fHashCache.remove(entry->fKey, entry);
63 GrGpuGL::DeleteProgram(fGL.interface(),
64 &entry->fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000065 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000066 entry->copyAndTakeOwnership(newEntry);
67 fHashCache.insert(entry->fKey, entry);
junov@google.comf93e7172011-03-31 21:26:24 +000068 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000069
70 entry->fLRUStamp = fCurrLRUStamp;
71 if (GR_UINT32_MAX == fCurrLRUStamp) {
72 // wrap around! just trash our LRU, one time hit.
73 for (int i = 0; i < fCount; ++i) {
74 fEntries[i].fLRUStamp = 0;
75 }
76 }
77 ++fCurrLRUStamp;
78 return &entry->fProgramData;
79}
junov@google.comf93e7172011-03-31 21:26:24 +000080
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000081void GrGpuGL::DeleteProgram(const GrGLInterface* gl,
bsalomon@google.com49209392012-06-05 15:13:46 +000082 CachedData* programData) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000083 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000084 if (programData->fGShaderID) {
85 GR_GL_CALL(gl, DeleteShader(programData->fGShaderID));
86 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000087 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
88 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +000089 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
90}
91
bsalomon@google.com1e257a52011-07-06 19:52:16 +000092////////////////////////////////////////////////////////////////////////////////
93
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000094void GrGpuGL::abandonResources(){
95 INHERITED::abandonResources();
96 fProgramCache->abandon();
97 fHWProgramID = 0;
98}
99
100////////////////////////////////////////////////////////////////////////////////
101
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000102#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
103
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000104void GrGpuGL::flushViewMatrix() {
bsalomon@google.com4c883782012-06-04 19:05:11 +0000105 const GrGLRenderTarget* rt = static_cast<const GrGLRenderTarget*>(this->getDrawState().getRenderTarget());
106 SkISize viewportSize;
107 const GrGLIRect& viewport = rt->getViewport();
108 viewportSize.set(viewport.fWidth, viewport.fHeight);
junov@google.comf93e7172011-03-31 21:26:24 +0000109
bsalomon@google.com4c883782012-06-04 19:05:11 +0000110 const GrMatrix& vm = this->getDrawState().getViewMatrix();
111
112 if (!fProgramData->fViewMatrix.cheapEqualTo(vm) ||
113 fProgramData->fViewportSize != viewportSize) {
114
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000115 GrMatrix m;
116 m.setAll(
bsalomon@google.com4c883782012-06-04 19:05:11 +0000117 GrIntToScalar(2) / viewportSize.fWidth, 0, -GR_Scalar1,
118 0,-GrIntToScalar(2) / viewportSize.fHeight, GR_Scalar1,
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000119 0, 0, GrMatrix::I()[8]);
120 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000121
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000122 // ES doesn't allow you to pass true to the transpose param,
123 // so do our own transpose
124 GrGLfloat mt[] = {
125 GrScalarToFloat(m[GrMatrix::kMScaleX]),
126 GrScalarToFloat(m[GrMatrix::kMSkewY]),
127 GrScalarToFloat(m[GrMatrix::kMPersp0]),
128 GrScalarToFloat(m[GrMatrix::kMSkewX]),
129 GrScalarToFloat(m[GrMatrix::kMScaleY]),
130 GrScalarToFloat(m[GrMatrix::kMPersp1]),
131 GrScalarToFloat(m[GrMatrix::kMTransX]),
132 GrScalarToFloat(m[GrMatrix::kMTransY]),
133 GrScalarToFloat(m[GrMatrix::kMPersp2])
134 };
135
bsalomon@google.com341767c2012-05-11 20:47:39 +0000136 GrAssert(GrGLProgram::kUnusedUniform !=
137 fProgramData->fUniLocations.fViewMatrixUni);
138 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
139 1, false, mt));
140 fProgramData->fViewMatrix = vm;
bsalomon@google.com4c883782012-06-04 19:05:11 +0000141 fProgramData->fViewportSize = viewportSize;
bsalomon@google.com91961302011-05-09 18:39:58 +0000142 }
junov@google.comf93e7172011-03-31 21:26:24 +0000143}
144
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000145///////////////////////////////////////////////////////////////////////////////
junov@google.com6acc9b32011-05-16 18:32:07 +0000146
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000147// helpers for texture matrices
junov@google.com6acc9b32011-05-16 18:32:07 +0000148
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000149void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000150 GrMatrix* matrix) {
151 GrAssert(NULL != texture);
152 GrAssert(NULL != matrix);
153 GrGLTexture::Orientation orientation = texture->orientation();
154 if (GrGLTexture::kBottomUp_Orientation == orientation) {
155 GrMatrix invY;
156 invY.setAll(GR_Scalar1, 0, 0,
157 0, -GR_Scalar1, GR_Scalar1,
158 0, 0, GrMatrix::I()[8]);
159 matrix->postConcat(invY);
160 } else {
161 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
junov@google.com6acc9b32011-05-16 18:32:07 +0000162 }
163}
164
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000165bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
166 const GrSamplerState& sampler) {
167 GrAssert(NULL != texture);
168 if (!sampler.getMatrix().isIdentity()) {
169 return false;
170 }
171 GrGLTexture::Orientation orientation = texture->orientation();
172 if (GrGLTexture::kBottomUp_Orientation == orientation) {
173 return false;
174 } else {
175 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
176 }
177 return true;
178}
179
180///////////////////////////////////////////////////////////////////////////////
181
182void GrGpuGL::flushTextureMatrixAndDomain(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000183 const GrDrawState& drawState = this->getDrawState();
184 const GrGLTexture* texture =
185 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000186 if (NULL != texture) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000187
188 bool orientationChange = fProgramData->fTextureOrientation[s] !=
189 texture->orientation();
190
191 const GrGLint& matrixUni =
192 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
193
bsalomon@google.com341767c2012-05-11 20:47:39 +0000194 const GrMatrix& hwMatrix = fProgramData->fTextureMatrices[s];
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000195 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000196
197 if (GrGLProgram::kUnusedUniform != matrixUni &&
198 (orientationChange || !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000199
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000200 GrMatrix m = samplerMatrix;
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000201 AdjustTextureMatrix(texture, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000202
bsalomon@google.com91961302011-05-09 18:39:58 +0000203 // ES doesn't allow you to pass true to the transpose param,
204 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000205 GrGLfloat mt[] = {
206 GrScalarToFloat(m[GrMatrix::kMScaleX]),
207 GrScalarToFloat(m[GrMatrix::kMSkewY]),
208 GrScalarToFloat(m[GrMatrix::kMPersp0]),
209 GrScalarToFloat(m[GrMatrix::kMSkewX]),
210 GrScalarToFloat(m[GrMatrix::kMScaleY]),
211 GrScalarToFloat(m[GrMatrix::kMPersp1]),
212 GrScalarToFloat(m[GrMatrix::kMTransX]),
213 GrScalarToFloat(m[GrMatrix::kMTransY]),
214 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000215 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000216
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000217 GL_CALL(UniformMatrix3fv(matrixUni, 1, false, mt));
bsalomon@google.com341767c2012-05-11 20:47:39 +0000218 fProgramData->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000219 }
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000220
221 const GrGLint& domUni =
222 fProgramData->fUniLocations.fStages[s].fTexDomUni;
223 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
224 if (GrGLProgram::kUnusedUniform != domUni &&
225 (orientationChange ||fProgramData->fTextureDomain[s] != texDom)) {
226
227 fProgramData->fTextureDomain[s] = texDom;
228
229 float values[4] = {
230 GrScalarToFloat(texDom.left()),
231 GrScalarToFloat(texDom.top()),
232 GrScalarToFloat(texDom.right()),
233 GrScalarToFloat(texDom.bottom())
234 };
235
236 // vertical flip if necessary
237 if (GrGLTexture::kBottomUp_Orientation == texture->orientation()) {
238 values[1] = 1.0f - values[1];
239 values[3] = 1.0f - values[3];
240 // The top and bottom were just flipped, so correct the ordering
241 // of elements so that values = (l, t, r, b).
242 SkTSwap(values[1], values[3]);
243 }
244 GL_CALL(Uniform4fv(domUni, 1, values));
245 }
246 fProgramData->fTextureOrientation[s] = texture->orientation();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000247 }
junov@google.comf93e7172011-03-31 21:26:24 +0000248}
249
junov@google.comf93e7172011-03-31 21:26:24 +0000250
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000251void GrGpuGL::flushColorMatrix() {
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000252 // const ProgramDesc& desc = fCurrentProgram.getDesc();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000253 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
254 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
255 if (GrGLProgram::kUnusedUniform != matrixUni
256 && GrGLProgram::kUnusedUniform != vecUni) {
257 const float* m = this->getDrawState().getColorMatrix();
258 GrGLfloat mt[] = {
259 m[0], m[5], m[10], m[15],
260 m[1], m[6], m[11], m[16],
261 m[2], m[7], m[12], m[17],
262 m[3], m[8], m[13], m[18],
263 };
264 static float scale = 1.0f / 255.0f;
265 GrGLfloat vec[] = {
266 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
267 };
268 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
269 GL_CALL(Uniform4fv(vecUni, 1, vec));
270 }
271}
272
Scroggo01b87ec2011-05-11 18:05:38 +0000273static const float ONE_OVER_255 = 1.f / 255.f;
274
275#define GR_COLOR_TO_VEC4(color) {\
276 GrColorUnpackR(color) * ONE_OVER_255,\
277 GrColorUnpackG(color) * ONE_OVER_255,\
278 GrColorUnpackB(color) * ONE_OVER_255,\
279 GrColorUnpackA(color) * ONE_OVER_255 \
280}
281
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000282void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000283 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000284 const GrDrawState& drawState = this->getDrawState();
285
bsalomon@google.come79c8152012-03-29 19:07:12 +0000286 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000287 // color will be specified per-vertex as an attribute
288 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000289 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000290 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000291 switch (desc.fColorInput) {
292 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000293 if (fHWConstAttribColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000294 // OpenGL ES only supports the float varieties of
295 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000296 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000297 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
298 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000299 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000300 }
301 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000302 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000303 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000304 // OpenGL ES doesn't support unsigned byte varieties of
305 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000306 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000307 GrAssert(GrGLProgram::kUnusedUniform !=
308 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000309 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
310 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000311 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000312 }
313 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000314 case ProgramDesc::kSolidWhite_ColorInput:
315 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000316 break;
317 default:
318 GrCrash("Unknown color type.");
319 }
320 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000321 if (fProgramData->fUniLocations.fColorFilterUni
322 != GrGLProgram::kUnusedUniform
323 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000324 != drawState.getColorFilterColor()) {
325 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000326 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000327 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000328 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000329}
330
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000331void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000332 const ProgramDesc& desc = fCurrentProgram.getDesc();
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000333 // const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000334
335
bsalomon@google.come79c8152012-03-29 19:07:12 +0000336 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000337 // coverage will be specified per-vertex as an attribute
338 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000339 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000340 } else {
341 switch (desc.fCoverageInput) {
342 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000343 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000344 // OpenGL ES only supports the float varieties of
345 // glVertexAttrib
346 float c[] = GR_COLOR_TO_VEC4(coverage);
347 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
348 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000349 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000350 }
351 break;
352 case ProgramDesc::kUniform_ColorInput:
353 if (fProgramData->fCoverage != coverage) {
354 // OpenGL ES doesn't support unsigned byte varieties of
355 // glUniform
356 float c[] = GR_COLOR_TO_VEC4(coverage);
357 GrAssert(GrGLProgram::kUnusedUniform !=
358 fProgramData->fUniLocations.fCoverageUni);
359 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
360 1, c));
361 fProgramData->fCoverage = coverage;
362 }
363 break;
364 case ProgramDesc::kSolidWhite_ColorInput:
365 case ProgramDesc::kTransBlack_ColorInput:
366 break;
367 default:
368 GrCrash("Unknown coverage type.");
369 }
370 }
371}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000372
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000373bool GrGpuGL::flushGraphicsState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000374 const GrDrawState& drawState = this->getDrawState();
375
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000376 // GrGpu::setupClipAndFlushState should have already checked this
377 // and bailed if not true.
378 GrAssert(NULL != drawState.getRenderTarget());
379
bsalomon@google.com5a94efc2012-06-08 14:13:02 +0000380 this->flushMiscFixedFunctionState();
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000381 this->flushStencil();
382 this->flushAAState(type);
383
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000384 GrBlendCoeff srcCoeff;
385 GrBlendCoeff dstCoeff;
386 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
387 if (kSkipDraw_BlendOptFlag & blendOpts) {
388 return false;
389 }
390
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000391 GrCustomStage* customStages [GrDrawState::kNumStages];
392 this->buildProgram(type, blendOpts, dstCoeff, customStages);
393 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
394 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000395 if (NULL == fProgramData) {
396 GrAssert(!"Failed to create program!");
397 return false;
398 }
junov@google.comf93e7172011-03-31 21:26:24 +0000399
400 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000401 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000402 fHWProgramID = fProgramData->fProgramID;
403 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000404 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
405 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000406
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000407 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000408 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000409 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
410 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000411 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000412 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
413 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000414 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000415 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000416 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000417 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000418 }
419 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000420 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000421
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000422 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000423
tomhudson@google.com93813632011-10-27 20:21:16 +0000424 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000425 if (this->isStageEnabled(s)) {
bsalomon@google.com4c883782012-06-04 19:05:11 +0000426
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000427
428#if GR_DEBUG
429 // check for circular rendering
430 GrAssert(NULL == drawState.getRenderTarget() ||
431 NULL == drawState.getTexture(s) ||
432 drawState.getTexture(s)->asRenderTarget() !=
433 drawState.getRenderTarget());
434#endif
435
bsalomon@google.com4c883782012-06-04 19:05:11 +0000436 this->flushBoundTextureAndParams(s);
437
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000438 this->flushTextureMatrixAndDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000439
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000440 if (NULL != fProgramData->fCustomStage[s]) {
441 const GrSamplerState& sampler =
442 this->getDrawState().getSampler(s);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000443 const GrGLTexture* texture =
444 static_cast<const GrGLTexture*>(
445 this->getDrawState().getTexture(s));
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000446 fProgramData->fCustomStage[s]->setData(
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000447 this->glInterface(), *texture,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000448 *sampler.getCustomStage(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000449 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000450 }
junov@google.comf93e7172011-03-31 21:26:24 +0000451 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000452 this->flushColorMatrix();
bsalomon@google.com4c883782012-06-04 19:05:11 +0000453
454 GrIRect* rect = NULL;
455 GrIRect clipBounds;
456 if (drawState.isClipState() &&
457 fClip.hasConservativeBounds()) {
458 fClip.getConservativeBounds().roundOut(&clipBounds);
459 rect = &clipBounds;
460 }
461 // This must come after textures are flushed because a texture may need
462 // to be msaa-resolved (which will modify bound FBO state).
463 this->flushRenderTarget(rect);
464
junov@google.comf93e7172011-03-31 21:26:24 +0000465 return true;
466}
467
bsalomon@google.com2717d562012-05-07 19:10:52 +0000468#if GR_TEXT_SCALAR_IS_USHORT
469 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
470 #define TEXT_COORDS_ARE_NORMALIZED 1
471#elif GR_TEXT_SCALAR_IS_FLOAT
472 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
473 #define TEXT_COORDS_ARE_NORMALIZED 0
474#elif GR_TEXT_SCALAR_IS_FIXED
475 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
476 #define TEXT_COORDS_ARE_NORMALIZED 0
477#else
478 #error "unknown GR_TEXT_SCALAR type"
479#endif
480
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000481void GrGpuGL::setupGeometry(int* startVertex,
bsalomon@google.com49209392012-06-05 15:13:46 +0000482 int* startIndex,
483 int vertexCount,
484 int indexCount) {
junov@google.comf93e7172011-03-31 21:26:24 +0000485
486 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000487 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000488 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000489 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000490
bsalomon@google.come79c8152012-03-29 19:07:12 +0000491 GrVertexLayout currLayout = this->getVertexLayout();
492
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000493 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000494 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000495 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000496 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000497 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000498 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000499 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000500 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000501 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000502 int oldEdgeOffset;
503
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000504 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
505 fHWGeometryState.fVertexLayout,
506 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000507 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000508 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000509 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000510 bool indexed = NULL != startIndex;
511
512 int extraVertexOffset;
513 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000514 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000515
516 GrGLenum scalarType;
517 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000518 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000519 scalarType = TEXT_COORDS_GL_TYPE;
520 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000521 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000522 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
523 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000524 texCoordNorm = false;
525 }
526
527 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
528 *startVertex = 0;
529 if (indexed) {
530 *startIndex += extraIndexOffset;
531 }
532
533 // all the Pointers must be set if any of these are true
534 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
535 vertexOffset != fHWGeometryState.fVertexOffset ||
536 newStride != oldStride;
537
538 // position and tex coord offsets change if above conditions are true
539 // or the type/normalization changed based on text vs nontext type coords.
540 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000541 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000542 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000543 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000544
545 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000546 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000547 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000548 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000549 fHWGeometryState.fVertexOffset = vertexOffset;
550 }
551
tomhudson@google.com93813632011-10-27 20:21:16 +0000552 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000553 if (newTexCoordOffsets[t] > 0) {
554 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000555 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000556 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000557 GL_CALL(EnableVertexAttribArray(idx));
558 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000559 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000560 } else if (posAndTexChange ||
561 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000562 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000563 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000564 }
565 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000566 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000567 }
568 }
569
570 if (newColorOffset > 0) {
571 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000572 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000573 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000574 GL_CALL(EnableVertexAttribArray(idx));
575 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000576 true, newStride, colorOffset));
577 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000578 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000579 true, newStride, colorOffset));
580 }
581 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000582 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000583 }
584
bsalomon@google.coma3108262011-10-10 14:08:47 +0000585 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000586 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000587 int idx = GrGLProgram::CoverageAttributeIdx();
588 if (oldCoverageOffset <= 0) {
589 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000590 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000591 true, newStride, coverageOffset));
592 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000593 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000594 true, newStride, coverageOffset));
595 }
596 } else if (oldCoverageOffset > 0) {
597 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
598 }
599
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000600 if (newEdgeOffset > 0) {
601 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
602 int idx = GrGLProgram::EdgeAttributeIdx();
603 if (oldEdgeOffset <= 0) {
604 GL_CALL(EnableVertexAttribArray(idx));
605 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
606 false, newStride, edgeOffset));
607 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
608 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
609 false, newStride, edgeOffset));
610 }
611 } else if (oldEdgeOffset > 0) {
612 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
613 }
614
bsalomon@google.come79c8152012-03-29 19:07:12 +0000615 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000616 fHWGeometryState.fArrayPtrsDirty = false;
617}
618
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000619namespace {
620
621void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
622 const GrSamplerState& sampler,
623 GrCustomStage** customStages,
624 GrGLProgram* program, int index) {
625 GrCustomStage* customStage = sampler.getCustomStage();
626 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000627 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000628 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000629 customStages[index] = customStage;
630 } else {
631 stage->fCustomStageKey = 0;
632 customStages[index] = NULL;
633 }
634}
635
636}
637
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000638void GrGpuGL::buildProgram(GrPrimitiveType type,
bsalomon@google.com49209392012-06-05 15:13:46 +0000639 BlendOptFlags blendOpts,
640 GrBlendCoeff dstCoeff,
641 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000642 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000643 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000644
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000645 // This should already have been caught
646 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
647
648 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
649
650 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
651 kEmitCoverage_BlendOptFlag));
652
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000653 // The descriptor is used as a cache key. Thus when a field of the
654 // descriptor will not affect program generation (because of the vertex
655 // layout in use or other descriptor field settings) it should be set
656 // to a canonical value to avoid duplicate programs with different keys.
657
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000658 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000659 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000660
bsalomon@google.com47059542012-06-06 20:51:20 +0000661 desc.fEmitsPointSize = kPoints_GrPrimitiveType == type;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000662
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000663 bool requiresAttributeColors =
664 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000665 bool requiresAttributeCoverage =
666 !skipCoverage && SkToBool(desc.fVertexLayout &
667 kCoverage_VertexLayoutBit);
668
669 // fColorInput/fCoverageInput records how colors are specified for the.
670 // program. So we strip the bits from the layout to avoid false negatives
671 // when searching for an existing program in the cache.
672 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000673
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000674 desc.fColorFilterXfermode = skipColor ?
675 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000676 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000677
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000678 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
679
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000680 // no reason to do edge aa or look at per-vertex coverage if coverage is
681 // ignored
682 if (skipCoverage) {
683 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
684 kCoverage_VertexLayoutBit);
685 }
686
687 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
688 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
689 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000690 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000691 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000692 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000693 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000694 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000695 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000696 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000697 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000698 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000699 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000700
701 bool covIsSolidWhite = !requiresAttributeCoverage &&
702 0xffffffff == drawState.getCoverage();
703
704 if (skipCoverage) {
705 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
706 } else if (covIsSolidWhite) {
707 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
708 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
709 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
710 } else {
711 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
712 }
junov@google.comf93e7172011-03-31 21:26:24 +0000713
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000714 int lastEnabledStage = -1;
715
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000716 if (!skipCoverage && (desc.fVertexLayout &
717 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000718 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000719 } else {
720 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000721 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000722 }
723
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000724 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000725 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000726
727 stage.fOptFlags = 0;
728 stage.setEnabled(this->isStageEnabled(s));
729
730 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
731 skipCoverage;
732
733 if (!skip && stage.isEnabled()) {
734 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000735 const GrGLTexture* texture =
736 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000737 GrAssert(NULL != texture);
738 const GrSamplerState& sampler = drawState.getSampler(s);
739 // we matrix to invert when orientation is TopDown, so make sure
740 // we aren't in that case before flagging as identity.
741 if (TextureMatrixIsIdentity(texture, sampler)) {
742 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
743 } else if (!sampler.getMatrix().hasPerspective()) {
744 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
745 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000746
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000747 if (sampler.hasTextureDomain()) {
748 GrAssert(GrSamplerState::kClamp_WrapMode ==
749 sampler.getWrapX() &&
750 GrSamplerState::kClamp_WrapMode ==
751 sampler.getWrapY());
752 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
753 }
754
755 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000756 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000757 if (GrPixelConfigIsAlphaOnly(texture->config())) {
758 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000759 // the shader must smear the single channel after
760 // reading the texture
761 if (this->glCaps().textureRedSupport()) {
762 // we can use R8 textures so use kSmearRed
763 stage.fInConfigFlags |=
764 StageDesc::kSmearRed_InConfigFlag;
765 } else {
766 // we can use A8 textures so use kSmearAlpha
767 stage.fInConfigFlags |=
768 StageDesc::kSmearAlpha_InConfigFlag;
769 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000770 } else if (sampler.swapsRAndB()) {
771 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
772 }
773 }
774 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000775 // The shader generator assumes that color channels are bytes
776 // when rounding.
777 GrAssert(4 == GrBytesPerPixel(texture->config()));
778 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
779 fUnpremulConversion) {
780 stage.fInConfigFlags |=
781 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
782 } else {
783 stage.fInConfigFlags |=
784 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
785 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000786 }
787
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000788 setup_custom_stage(&stage, sampler, customStages,
789 &fCurrentProgram, s);
790
junov@google.comf93e7172011-03-31 21:26:24 +0000791 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000792 stage.fOptFlags = 0;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000793 stage.fInConfigFlags = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000794 stage.fCustomStageKey = 0;
795 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000796 }
797 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000798
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000799 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000800 // The shader generator assumes that color channels are bytes
801 // when rounding.
802 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
803 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
804 desc.fOutputConfig =
805 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
806 } else {
807 desc.fOutputConfig =
808 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
809 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000810 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000811 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000812 }
813
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000814 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000815
816 // currently the experimental GS will only work with triangle prims
817 // (and it doesn't do anything other than pass through values from
818 // the VS to the FS anyway).
819#if 0 && GR_GL_EXPERIMENTAL_GS
820 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
821#endif
822
bsalomon@google.coma3108262011-10-10 14:08:47 +0000823 // we want to avoid generating programs with different "first cov stage"
824 // values when they would compute the same result.
825 // We set field in the desc to kNumStages when either there are no
826 // coverage stages or the distinction between coverage and color is
827 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000828 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +0000829 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000830 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000831 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000832 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000833 }
834
835 // other coverage inputs
836 if (!hasCoverage) {
837 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000838 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +0000839 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
840 }
841
842 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000843 // color filter is applied between color/coverage computation
844 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000845 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000846 }
847
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000848 if (this->getCaps().fDualSourceBlendingSupport &&
849 !(blendOpts & (kEmitCoverage_BlendOptFlag |
850 kCoverageAsAlpha_BlendOptFlag))) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000851 if (kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000852 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000853 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000854 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000855 } else if (kSA_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000856 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
857 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000858 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000859 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000860 } else if (kSC_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000861 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
862 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000863 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000864 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000865 }
866 }
867 }
junov@google.comf93e7172011-03-31 21:26:24 +0000868}