blob: 97f9b6d817d967f13d9cc878281629865e83d63c [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.com64aef2b2012-06-11 15:36:13 +0000373bool GrGpuGL::flushGraphicsState(DrawType 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();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000382 this->flushAAState(kDrawLines_DrawType == type);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000383
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];
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000392 this->buildProgram(kDrawPoints_DrawType == type,
393 blendOpts, dstCoeff, customStages);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000394 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
395 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000396 if (NULL == fProgramData) {
397 GrAssert(!"Failed to create program!");
398 return false;
399 }
junov@google.comf93e7172011-03-31 21:26:24 +0000400
401 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000402 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000403 fHWProgramID = fProgramData->fProgramID;
404 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000405 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000406 this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000407
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000408 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000409 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000410 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
411 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000412 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000413 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
414 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000415 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000416 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000417 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000418 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000419 }
420 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000421 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000422
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000423 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000424
tomhudson@google.com93813632011-10-27 20:21:16 +0000425 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000426 if (this->isStageEnabled(s)) {
bsalomon@google.com4c883782012-06-04 19:05:11 +0000427
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000428
429#if GR_DEBUG
430 // check for circular rendering
431 GrAssert(NULL == drawState.getRenderTarget() ||
432 NULL == drawState.getTexture(s) ||
433 drawState.getTexture(s)->asRenderTarget() !=
434 drawState.getRenderTarget());
435#endif
436
bsalomon@google.com4c883782012-06-04 19:05:11 +0000437 this->flushBoundTextureAndParams(s);
438
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000439 this->flushTextureMatrixAndDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000440
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000441 if (NULL != fProgramData->fCustomStage[s]) {
442 const GrSamplerState& sampler =
443 this->getDrawState().getSampler(s);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000444 const GrGLTexture* texture =
445 static_cast<const GrGLTexture*>(
446 this->getDrawState().getTexture(s));
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000447 fProgramData->fCustomStage[s]->setData(
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000448 this->glInterface(), *texture,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000449 *sampler.getCustomStage(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000450 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000451 }
junov@google.comf93e7172011-03-31 21:26:24 +0000452 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000453 this->flushColorMatrix();
bsalomon@google.com4c883782012-06-04 19:05:11 +0000454
455 GrIRect* rect = NULL;
456 GrIRect clipBounds;
457 if (drawState.isClipState() &&
458 fClip.hasConservativeBounds()) {
459 fClip.getConservativeBounds().roundOut(&clipBounds);
460 rect = &clipBounds;
461 }
462 // This must come after textures are flushed because a texture may need
463 // to be msaa-resolved (which will modify bound FBO state).
464 this->flushRenderTarget(rect);
465
junov@google.comf93e7172011-03-31 21:26:24 +0000466 return true;
467}
468
bsalomon@google.com2717d562012-05-07 19:10:52 +0000469#if GR_TEXT_SCALAR_IS_USHORT
470 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
471 #define TEXT_COORDS_ARE_NORMALIZED 1
472#elif GR_TEXT_SCALAR_IS_FLOAT
473 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
474 #define TEXT_COORDS_ARE_NORMALIZED 0
475#elif GR_TEXT_SCALAR_IS_FIXED
476 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
477 #define TEXT_COORDS_ARE_NORMALIZED 0
478#else
479 #error "unknown GR_TEXT_SCALAR type"
480#endif
481
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000482void GrGpuGL::setupGeometry(int* startVertex,
bsalomon@google.com49209392012-06-05 15:13:46 +0000483 int* startIndex,
484 int vertexCount,
485 int indexCount) {
junov@google.comf93e7172011-03-31 21:26:24 +0000486
487 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000488 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000489 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000490 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000491
bsalomon@google.come79c8152012-03-29 19:07:12 +0000492 GrVertexLayout currLayout = this->getVertexLayout();
493
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000494 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000495 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000496 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000497 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000498 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000499 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000500 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000501 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000502 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000503 int oldEdgeOffset;
504
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000505 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
506 fHWGeometryState.fVertexLayout,
507 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000508 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000509 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000510 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000511 bool indexed = NULL != startIndex;
512
513 int extraVertexOffset;
514 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000515 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000516
517 GrGLenum scalarType;
518 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000519 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000520 scalarType = TEXT_COORDS_GL_TYPE;
521 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000522 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000523 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
524 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000525 texCoordNorm = false;
526 }
527
528 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
529 *startVertex = 0;
530 if (indexed) {
531 *startIndex += extraIndexOffset;
532 }
533
534 // all the Pointers must be set if any of these are true
535 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
536 vertexOffset != fHWGeometryState.fVertexOffset ||
537 newStride != oldStride;
538
539 // position and tex coord offsets change if above conditions are true
540 // or the type/normalization changed based on text vs nontext type coords.
541 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000542 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000543 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000544 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000545
546 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000547 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000548 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000549 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000550 fHWGeometryState.fVertexOffset = vertexOffset;
551 }
552
tomhudson@google.com93813632011-10-27 20:21:16 +0000553 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000554 if (newTexCoordOffsets[t] > 0) {
555 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000556 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000557 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000558 GL_CALL(EnableVertexAttribArray(idx));
559 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000560 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000561 } else if (posAndTexChange ||
562 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000563 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000564 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000565 }
566 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000567 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000568 }
569 }
570
571 if (newColorOffset > 0) {
572 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000573 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000574 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000575 GL_CALL(EnableVertexAttribArray(idx));
576 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000577 true, newStride, colorOffset));
578 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000579 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000580 true, newStride, colorOffset));
581 }
582 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000583 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000584 }
585
bsalomon@google.coma3108262011-10-10 14:08:47 +0000586 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000587 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000588 int idx = GrGLProgram::CoverageAttributeIdx();
589 if (oldCoverageOffset <= 0) {
590 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000591 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000592 true, newStride, coverageOffset));
593 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000594 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000595 true, newStride, coverageOffset));
596 }
597 } else if (oldCoverageOffset > 0) {
598 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
599 }
600
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000601 if (newEdgeOffset > 0) {
602 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
603 int idx = GrGLProgram::EdgeAttributeIdx();
604 if (oldEdgeOffset <= 0) {
605 GL_CALL(EnableVertexAttribArray(idx));
606 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
607 false, newStride, edgeOffset));
608 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
609 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
610 false, newStride, edgeOffset));
611 }
612 } else if (oldEdgeOffset > 0) {
613 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
614 }
615
bsalomon@google.come79c8152012-03-29 19:07:12 +0000616 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000617 fHWGeometryState.fArrayPtrsDirty = false;
618}
619
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000620namespace {
621
622void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
623 const GrSamplerState& sampler,
624 GrCustomStage** customStages,
625 GrGLProgram* program, int index) {
626 GrCustomStage* customStage = sampler.getCustomStage();
627 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000628 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000629 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000630 customStages[index] = customStage;
631 } else {
632 stage->fCustomStageKey = 0;
633 customStages[index] = NULL;
634 }
635}
636
637}
638
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000639void GrGpuGL::buildProgram(bool isPoints,
bsalomon@google.com49209392012-06-05 15:13:46 +0000640 BlendOptFlags blendOpts,
641 GrBlendCoeff dstCoeff,
642 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000643 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000644 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000645
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000646 // This should already have been caught
647 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
648
649 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
650
651 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
652 kEmitCoverage_BlendOptFlag));
653
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000654 // The descriptor is used as a cache key. Thus when a field of the
655 // descriptor will not affect program generation (because of the vertex
656 // layout in use or other descriptor field settings) it should be set
657 // to a canonical value to avoid duplicate programs with different keys.
658
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000659 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000660 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000661
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000662 desc.fEmitsPointSize = isPoints;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000663
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000664 bool requiresAttributeColors =
665 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000666 bool requiresAttributeCoverage =
667 !skipCoverage && SkToBool(desc.fVertexLayout &
668 kCoverage_VertexLayoutBit);
669
670 // fColorInput/fCoverageInput records how colors are specified for the.
671 // program. So we strip the bits from the layout to avoid false negatives
672 // when searching for an existing program in the cache.
673 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000674
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000675 desc.fColorFilterXfermode = skipColor ?
676 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000677 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000678
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000679 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
680
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000681 // no reason to do edge aa or look at per-vertex coverage if coverage is
682 // ignored
683 if (skipCoverage) {
684 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
685 kCoverage_VertexLayoutBit);
686 }
687
688 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
689 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
690 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000691 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000692 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000693 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000694 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000695 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000696 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000697 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000698 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000699 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000700 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000701
702 bool covIsSolidWhite = !requiresAttributeCoverage &&
703 0xffffffff == drawState.getCoverage();
704
705 if (skipCoverage) {
706 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
707 } else if (covIsSolidWhite) {
708 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
709 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
710 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
711 } else {
712 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
713 }
junov@google.comf93e7172011-03-31 21:26:24 +0000714
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000715 int lastEnabledStage = -1;
716
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000717 if (!skipCoverage && (desc.fVertexLayout &
718 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000719 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000720 } else {
721 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000722 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000723 }
724
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000725 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000726 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000727
728 stage.fOptFlags = 0;
729 stage.setEnabled(this->isStageEnabled(s));
730
731 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
732 skipCoverage;
733
734 if (!skip && stage.isEnabled()) {
735 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000736 const GrGLTexture* texture =
737 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000738 GrAssert(NULL != texture);
739 const GrSamplerState& sampler = drawState.getSampler(s);
740 // we matrix to invert when orientation is TopDown, so make sure
741 // we aren't in that case before flagging as identity.
742 if (TextureMatrixIsIdentity(texture, sampler)) {
743 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
744 } else if (!sampler.getMatrix().hasPerspective()) {
745 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
746 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000747
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000748 if (sampler.hasTextureDomain()) {
749 GrAssert(GrSamplerState::kClamp_WrapMode ==
750 sampler.getWrapX() &&
751 GrSamplerState::kClamp_WrapMode ==
752 sampler.getWrapY());
753 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
754 }
755
756 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000757 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000758 if (GrPixelConfigIsAlphaOnly(texture->config())) {
759 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000760 // the shader must smear the single channel after
761 // reading the texture
762 if (this->glCaps().textureRedSupport()) {
763 // we can use R8 textures so use kSmearRed
764 stage.fInConfigFlags |=
765 StageDesc::kSmearRed_InConfigFlag;
766 } else {
767 // we can use A8 textures so use kSmearAlpha
768 stage.fInConfigFlags |=
769 StageDesc::kSmearAlpha_InConfigFlag;
770 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000771 } else if (sampler.swapsRAndB()) {
772 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
773 }
774 }
775 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000776 // The shader generator assumes that color channels are bytes
777 // when rounding.
778 GrAssert(4 == GrBytesPerPixel(texture->config()));
779 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
780 fUnpremulConversion) {
781 stage.fInConfigFlags |=
782 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
783 } else {
784 stage.fInConfigFlags |=
785 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
786 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000787 }
788
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000789 setup_custom_stage(&stage, sampler, customStages,
790 &fCurrentProgram, s);
791
junov@google.comf93e7172011-03-31 21:26:24 +0000792 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000793 stage.fOptFlags = 0;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000794 stage.fInConfigFlags = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000795 stage.fCustomStageKey = 0;
796 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000797 }
798 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000799
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000800 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000801 // The shader generator assumes that color channels are bytes
802 // when rounding.
803 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
804 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
805 desc.fOutputConfig =
806 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
807 } else {
808 desc.fOutputConfig =
809 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
810 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000811 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000812 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000813 }
814
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000815 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000816
817 // currently the experimental GS will only work with triangle prims
818 // (and it doesn't do anything other than pass through values from
819 // the VS to the FS anyway).
820#if 0 && GR_GL_EXPERIMENTAL_GS
821 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
822#endif
823
bsalomon@google.coma3108262011-10-10 14:08:47 +0000824 // we want to avoid generating programs with different "first cov stage"
825 // values when they would compute the same result.
826 // We set field in the desc to kNumStages when either there are no
827 // coverage stages or the distinction between coverage and color is
828 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000829 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +0000830 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000831 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000832 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000833 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000834 }
835
836 // other coverage inputs
837 if (!hasCoverage) {
838 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000839 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +0000840 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
841 }
842
843 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000844 // color filter is applied between color/coverage computation
845 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000846 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000847 }
848
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000849 if (this->getCaps().fDualSourceBlendingSupport &&
850 !(blendOpts & (kEmitCoverage_BlendOptFlag |
851 kCoverageAsAlpha_BlendOptFlag))) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000852 if (kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000853 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000854 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000855 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000856 } else if (kSA_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000857 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
858 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000859 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000860 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000861 } else if (kSC_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000862 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
863 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000864 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000865 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000866 }
867 }
868 }
junov@google.comf93e7172011-03-31 21:26:24 +0000869}