blob: 581a22a68f2c2ffd2a0e20e70efdfb981e090b28 [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
380 this->flushStencil();
381 this->flushAAState(type);
382
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000383 GrBlendCoeff srcCoeff;
384 GrBlendCoeff dstCoeff;
385 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
386 if (kSkipDraw_BlendOptFlag & blendOpts) {
387 return false;
388 }
389
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000390 GrCustomStage* customStages [GrDrawState::kNumStages];
391 this->buildProgram(type, blendOpts, dstCoeff, customStages);
392 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
393 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000394 if (NULL == fProgramData) {
395 GrAssert(!"Failed to create program!");
396 return false;
397 }
junov@google.comf93e7172011-03-31 21:26:24 +0000398
399 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000400 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000401 fHWProgramID = fProgramData->fProgramID;
402 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000403 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
404 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000405
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000406 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000407 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000408 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
409 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000410 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000411 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
412 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000413 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000414 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000415 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000416 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000417 }
418 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000419 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000420
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000421 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000422
tomhudson@google.com93813632011-10-27 20:21:16 +0000423 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000424 if (this->isStageEnabled(s)) {
bsalomon@google.com4c883782012-06-04 19:05:11 +0000425
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000426
427#if GR_DEBUG
428 // check for circular rendering
429 GrAssert(NULL == drawState.getRenderTarget() ||
430 NULL == drawState.getTexture(s) ||
431 drawState.getTexture(s)->asRenderTarget() !=
432 drawState.getRenderTarget());
433#endif
434
bsalomon@google.com4c883782012-06-04 19:05:11 +0000435 this->flushBoundTextureAndParams(s);
436
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000437 this->flushTextureMatrixAndDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000438
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000439 if (NULL != fProgramData->fCustomStage[s]) {
440 const GrSamplerState& sampler =
441 this->getDrawState().getSampler(s);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000442 const GrGLTexture* texture =
443 static_cast<const GrGLTexture*>(
444 this->getDrawState().getTexture(s));
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000445 fProgramData->fCustomStage[s]->setData(
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000446 this->glInterface(), *texture,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000447 *sampler.getCustomStage(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000448 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000449 }
junov@google.comf93e7172011-03-31 21:26:24 +0000450 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000451 this->flushColorMatrix();
bsalomon@google.com4c883782012-06-04 19:05:11 +0000452
453 GrIRect* rect = NULL;
454 GrIRect clipBounds;
455 if (drawState.isClipState() &&
456 fClip.hasConservativeBounds()) {
457 fClip.getConservativeBounds().roundOut(&clipBounds);
458 rect = &clipBounds;
459 }
460 // This must come after textures are flushed because a texture may need
461 // to be msaa-resolved (which will modify bound FBO state).
462 this->flushRenderTarget(rect);
463
junov@google.comf93e7172011-03-31 21:26:24 +0000464 return true;
465}
466
bsalomon@google.com2717d562012-05-07 19:10:52 +0000467#if GR_TEXT_SCALAR_IS_USHORT
468 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
469 #define TEXT_COORDS_ARE_NORMALIZED 1
470#elif GR_TEXT_SCALAR_IS_FLOAT
471 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
472 #define TEXT_COORDS_ARE_NORMALIZED 0
473#elif GR_TEXT_SCALAR_IS_FIXED
474 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
475 #define TEXT_COORDS_ARE_NORMALIZED 0
476#else
477 #error "unknown GR_TEXT_SCALAR type"
478#endif
479
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000480void GrGpuGL::setupGeometry(int* startVertex,
bsalomon@google.com49209392012-06-05 15:13:46 +0000481 int* startIndex,
482 int vertexCount,
483 int indexCount) {
junov@google.comf93e7172011-03-31 21:26:24 +0000484
485 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000486 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000487 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000488 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000489
bsalomon@google.come79c8152012-03-29 19:07:12 +0000490 GrVertexLayout currLayout = this->getVertexLayout();
491
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000492 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000493 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000494 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000495 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000496 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000497 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000498 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000499 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000500 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000501 int oldEdgeOffset;
502
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000503 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
504 fHWGeometryState.fVertexLayout,
505 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000506 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000507 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000508 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000509 bool indexed = NULL != startIndex;
510
511 int extraVertexOffset;
512 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000513 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000514
515 GrGLenum scalarType;
516 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000517 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000518 scalarType = TEXT_COORDS_GL_TYPE;
519 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000520 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000521 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
522 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000523 texCoordNorm = false;
524 }
525
526 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
527 *startVertex = 0;
528 if (indexed) {
529 *startIndex += extraIndexOffset;
530 }
531
532 // all the Pointers must be set if any of these are true
533 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
534 vertexOffset != fHWGeometryState.fVertexOffset ||
535 newStride != oldStride;
536
537 // position and tex coord offsets change if above conditions are true
538 // or the type/normalization changed based on text vs nontext type coords.
539 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000540 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000541 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000542 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000543
544 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000545 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000546 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000547 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000548 fHWGeometryState.fVertexOffset = vertexOffset;
549 }
550
tomhudson@google.com93813632011-10-27 20:21:16 +0000551 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000552 if (newTexCoordOffsets[t] > 0) {
553 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000554 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000555 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000556 GL_CALL(EnableVertexAttribArray(idx));
557 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000558 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000559 } else if (posAndTexChange ||
560 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000561 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000562 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000563 }
564 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000565 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000566 }
567 }
568
569 if (newColorOffset > 0) {
570 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000571 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000572 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000573 GL_CALL(EnableVertexAttribArray(idx));
574 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000575 true, newStride, colorOffset));
576 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000577 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000578 true, newStride, colorOffset));
579 }
580 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000581 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000582 }
583
bsalomon@google.coma3108262011-10-10 14:08:47 +0000584 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000585 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000586 int idx = GrGLProgram::CoverageAttributeIdx();
587 if (oldCoverageOffset <= 0) {
588 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000589 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000590 true, newStride, coverageOffset));
591 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000592 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000593 true, newStride, coverageOffset));
594 }
595 } else if (oldCoverageOffset > 0) {
596 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
597 }
598
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000599 if (newEdgeOffset > 0) {
600 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
601 int idx = GrGLProgram::EdgeAttributeIdx();
602 if (oldEdgeOffset <= 0) {
603 GL_CALL(EnableVertexAttribArray(idx));
604 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
605 false, newStride, edgeOffset));
606 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
607 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
608 false, newStride, edgeOffset));
609 }
610 } else if (oldEdgeOffset > 0) {
611 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
612 }
613
bsalomon@google.come79c8152012-03-29 19:07:12 +0000614 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000615 fHWGeometryState.fArrayPtrsDirty = false;
616}
617
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000618namespace {
619
620void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
621 const GrSamplerState& sampler,
622 GrCustomStage** customStages,
623 GrGLProgram* program, int index) {
624 GrCustomStage* customStage = sampler.getCustomStage();
625 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000626 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000627 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000628 customStages[index] = customStage;
629 } else {
630 stage->fCustomStageKey = 0;
631 customStages[index] = NULL;
632 }
633}
634
635}
636
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000637void GrGpuGL::buildProgram(GrPrimitiveType type,
bsalomon@google.com49209392012-06-05 15:13:46 +0000638 BlendOptFlags blendOpts,
639 GrBlendCoeff dstCoeff,
640 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000641 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000642 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000643
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000644 // This should already have been caught
645 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
646
647 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
648
649 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
650 kEmitCoverage_BlendOptFlag));
651
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000652 // The descriptor is used as a cache key. Thus when a field of the
653 // descriptor will not affect program generation (because of the vertex
654 // layout in use or other descriptor field settings) it should be set
655 // to a canonical value to avoid duplicate programs with different keys.
656
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000657 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000658 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000659
660 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
661
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000662 bool requiresAttributeColors =
663 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000664 bool requiresAttributeCoverage =
665 !skipCoverage && SkToBool(desc.fVertexLayout &
666 kCoverage_VertexLayoutBit);
667
668 // fColorInput/fCoverageInput records how colors are specified for the.
669 // program. So we strip the bits from the layout to avoid false negatives
670 // when searching for an existing program in the cache.
671 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000672
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000673 desc.fColorFilterXfermode = skipColor ?
674 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000675 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000676
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000677 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
678
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000679 // no reason to do edge aa or look at per-vertex coverage if coverage is
680 // ignored
681 if (skipCoverage) {
682 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
683 kCoverage_VertexLayoutBit);
684 }
685
686 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
687 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
688 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000689 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000690 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000691 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000692 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000693 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000694 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000695 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000696 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000697 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000698 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000699
700 bool covIsSolidWhite = !requiresAttributeCoverage &&
701 0xffffffff == drawState.getCoverage();
702
703 if (skipCoverage) {
704 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
705 } else if (covIsSolidWhite) {
706 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
707 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
708 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
709 } else {
710 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
711 }
junov@google.comf93e7172011-03-31 21:26:24 +0000712
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000713 int lastEnabledStage = -1;
714
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000715 if (!skipCoverage && (desc.fVertexLayout &
716 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000717 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000718 } else {
719 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000720 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000721 }
722
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000723 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000724 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000725
726 stage.fOptFlags = 0;
727 stage.setEnabled(this->isStageEnabled(s));
728
729 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
730 skipCoverage;
731
732 if (!skip && stage.isEnabled()) {
733 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000734 const GrGLTexture* texture =
735 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000736 GrAssert(NULL != texture);
737 const GrSamplerState& sampler = drawState.getSampler(s);
738 // we matrix to invert when orientation is TopDown, so make sure
739 // we aren't in that case before flagging as identity.
740 if (TextureMatrixIsIdentity(texture, sampler)) {
741 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
742 } else if (!sampler.getMatrix().hasPerspective()) {
743 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
744 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000745
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000746 if (sampler.hasTextureDomain()) {
747 GrAssert(GrSamplerState::kClamp_WrapMode ==
748 sampler.getWrapX() &&
749 GrSamplerState::kClamp_WrapMode ==
750 sampler.getWrapY());
751 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
752 }
753
754 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000755 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000756 if (GrPixelConfigIsAlphaOnly(texture->config())) {
757 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000758 // the shader must smear the single channel after
759 // reading the texture
760 if (this->glCaps().textureRedSupport()) {
761 // we can use R8 textures so use kSmearRed
762 stage.fInConfigFlags |=
763 StageDesc::kSmearRed_InConfigFlag;
764 } else {
765 // we can use A8 textures so use kSmearAlpha
766 stage.fInConfigFlags |=
767 StageDesc::kSmearAlpha_InConfigFlag;
768 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000769 } else if (sampler.swapsRAndB()) {
770 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
771 }
772 }
773 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000774 // The shader generator assumes that color channels are bytes
775 // when rounding.
776 GrAssert(4 == GrBytesPerPixel(texture->config()));
777 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
778 fUnpremulConversion) {
779 stage.fInConfigFlags |=
780 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
781 } else {
782 stage.fInConfigFlags |=
783 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
784 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000785 }
786
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000787 setup_custom_stage(&stage, sampler, customStages,
788 &fCurrentProgram, s);
789
junov@google.comf93e7172011-03-31 21:26:24 +0000790 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000791 stage.fOptFlags = 0;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000792 stage.fInConfigFlags = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000793 stage.fCustomStageKey = 0;
794 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000795 }
796 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000797
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000798 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000799 // The shader generator assumes that color channels are bytes
800 // when rounding.
801 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
802 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
803 desc.fOutputConfig =
804 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
805 } else {
806 desc.fOutputConfig =
807 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
808 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000809 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000810 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000811 }
812
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000813 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000814
815 // currently the experimental GS will only work with triangle prims
816 // (and it doesn't do anything other than pass through values from
817 // the VS to the FS anyway).
818#if 0 && GR_GL_EXPERIMENTAL_GS
819 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
820#endif
821
bsalomon@google.coma3108262011-10-10 14:08:47 +0000822 // we want to avoid generating programs with different "first cov stage"
823 // values when they would compute the same result.
824 // We set field in the desc to kNumStages when either there are no
825 // coverage stages or the distinction between coverage and color is
826 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000827 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +0000828 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000829 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000830 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000831 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000832 }
833
834 // other coverage inputs
835 if (!hasCoverage) {
836 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000837 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +0000838 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
839 }
840
841 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000842 // color filter is applied between color/coverage computation
843 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000844 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000845 }
846
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000847 if (this->getCaps().fDualSourceBlendingSupport &&
848 !(blendOpts & (kEmitCoverage_BlendOptFlag |
849 kCoverageAsAlpha_BlendOptFlag))) {
850 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000851 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000852 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000853 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000854 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000855 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
856 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000857 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000858 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000859 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000860 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
861 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000862 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000863 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000864 }
865 }
866 }
junov@google.comf93e7172011-03-31 21:26:24 +0000867}