blob: f8d71ffbf45e944759d9a06efeb05e29cd94ca8d [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));
tomhudson@google.com50e4ce02012-06-19 15:27:50 +000089 GR_DEBUGCODE(programData->fVShaderID = 0);
90 GR_DEBUGCODE(programData->fGShaderID = 0);
91 GR_DEBUGCODE(programData->fFShaderID = 0);
92 GR_DEBUGCODE(programData->fProgramID = 0);
junov@google.comf93e7172011-03-31 21:26:24 +000093}
94
bsalomon@google.com1e257a52011-07-06 19:52:16 +000095////////////////////////////////////////////////////////////////////////////////
96
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000097void GrGpuGL::abandonResources(){
98 INHERITED::abandonResources();
99 fProgramCache->abandon();
100 fHWProgramID = 0;
101}
102
103////////////////////////////////////////////////////////////////////////////////
104
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000105#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
106
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000107void GrGpuGL::flushViewMatrix() {
bsalomon@google.com4c883782012-06-04 19:05:11 +0000108 const GrGLRenderTarget* rt = static_cast<const GrGLRenderTarget*>(this->getDrawState().getRenderTarget());
109 SkISize viewportSize;
110 const GrGLIRect& viewport = rt->getViewport();
111 viewportSize.set(viewport.fWidth, viewport.fHeight);
junov@google.comf93e7172011-03-31 21:26:24 +0000112
bsalomon@google.com4c883782012-06-04 19:05:11 +0000113 const GrMatrix& vm = this->getDrawState().getViewMatrix();
114
115 if (!fProgramData->fViewMatrix.cheapEqualTo(vm) ||
116 fProgramData->fViewportSize != viewportSize) {
117
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000118 GrMatrix m;
119 m.setAll(
bsalomon@google.com4c883782012-06-04 19:05:11 +0000120 GrIntToScalar(2) / viewportSize.fWidth, 0, -GR_Scalar1,
121 0,-GrIntToScalar(2) / viewportSize.fHeight, GR_Scalar1,
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000122 0, 0, GrMatrix::I()[8]);
123 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000124
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000125 // ES doesn't allow you to pass true to the transpose param,
126 // so do our own transpose
127 GrGLfloat mt[] = {
128 GrScalarToFloat(m[GrMatrix::kMScaleX]),
129 GrScalarToFloat(m[GrMatrix::kMSkewY]),
130 GrScalarToFloat(m[GrMatrix::kMPersp0]),
131 GrScalarToFloat(m[GrMatrix::kMSkewX]),
132 GrScalarToFloat(m[GrMatrix::kMScaleY]),
133 GrScalarToFloat(m[GrMatrix::kMPersp1]),
134 GrScalarToFloat(m[GrMatrix::kMTransX]),
135 GrScalarToFloat(m[GrMatrix::kMTransY]),
136 GrScalarToFloat(m[GrMatrix::kMPersp2])
137 };
138
bsalomon@google.com341767c2012-05-11 20:47:39 +0000139 GrAssert(GrGLProgram::kUnusedUniform !=
140 fProgramData->fUniLocations.fViewMatrixUni);
141 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
142 1, false, mt));
143 fProgramData->fViewMatrix = vm;
bsalomon@google.com4c883782012-06-04 19:05:11 +0000144 fProgramData->fViewportSize = viewportSize;
bsalomon@google.com91961302011-05-09 18:39:58 +0000145 }
junov@google.comf93e7172011-03-31 21:26:24 +0000146}
147
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000148///////////////////////////////////////////////////////////////////////////////
junov@google.com6acc9b32011-05-16 18:32:07 +0000149
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000150// helpers for texture matrices
junov@google.com6acc9b32011-05-16 18:32:07 +0000151
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000152void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000153 GrMatrix* matrix) {
154 GrAssert(NULL != texture);
155 GrAssert(NULL != matrix);
156 GrGLTexture::Orientation orientation = texture->orientation();
157 if (GrGLTexture::kBottomUp_Orientation == orientation) {
158 GrMatrix invY;
159 invY.setAll(GR_Scalar1, 0, 0,
160 0, -GR_Scalar1, GR_Scalar1,
161 0, 0, GrMatrix::I()[8]);
162 matrix->postConcat(invY);
163 } else {
164 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
junov@google.com6acc9b32011-05-16 18:32:07 +0000165 }
166}
167
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000168bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
169 const GrSamplerState& sampler) {
170 GrAssert(NULL != texture);
171 if (!sampler.getMatrix().isIdentity()) {
172 return false;
173 }
174 GrGLTexture::Orientation orientation = texture->orientation();
175 if (GrGLTexture::kBottomUp_Orientation == orientation) {
176 return false;
177 } else {
178 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
179 }
180 return true;
181}
182
183///////////////////////////////////////////////////////////////////////////////
184
185void GrGpuGL::flushTextureMatrixAndDomain(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000186 const GrDrawState& drawState = this->getDrawState();
187 const GrGLTexture* texture =
188 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000189 if (NULL != texture) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000190
191 bool orientationChange = fProgramData->fTextureOrientation[s] !=
192 texture->orientation();
193
194 const GrGLint& matrixUni =
195 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
196
bsalomon@google.com341767c2012-05-11 20:47:39 +0000197 const GrMatrix& hwMatrix = fProgramData->fTextureMatrices[s];
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000198 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000199
200 if (GrGLProgram::kUnusedUniform != matrixUni &&
201 (orientationChange || !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000202
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000203 GrMatrix m = samplerMatrix;
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000204 AdjustTextureMatrix(texture, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000205
bsalomon@google.com91961302011-05-09 18:39:58 +0000206 // ES doesn't allow you to pass true to the transpose param,
207 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000208 GrGLfloat mt[] = {
209 GrScalarToFloat(m[GrMatrix::kMScaleX]),
210 GrScalarToFloat(m[GrMatrix::kMSkewY]),
211 GrScalarToFloat(m[GrMatrix::kMPersp0]),
212 GrScalarToFloat(m[GrMatrix::kMSkewX]),
213 GrScalarToFloat(m[GrMatrix::kMScaleY]),
214 GrScalarToFloat(m[GrMatrix::kMPersp1]),
215 GrScalarToFloat(m[GrMatrix::kMTransX]),
216 GrScalarToFloat(m[GrMatrix::kMTransY]),
217 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000218 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000219
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000220 GL_CALL(UniformMatrix3fv(matrixUni, 1, false, mt));
bsalomon@google.com341767c2012-05-11 20:47:39 +0000221 fProgramData->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000222 }
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000223
224 const GrGLint& domUni =
225 fProgramData->fUniLocations.fStages[s].fTexDomUni;
226 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
227 if (GrGLProgram::kUnusedUniform != domUni &&
228 (orientationChange ||fProgramData->fTextureDomain[s] != texDom)) {
229
230 fProgramData->fTextureDomain[s] = texDom;
231
232 float values[4] = {
233 GrScalarToFloat(texDom.left()),
234 GrScalarToFloat(texDom.top()),
235 GrScalarToFloat(texDom.right()),
236 GrScalarToFloat(texDom.bottom())
237 };
238
239 // vertical flip if necessary
240 if (GrGLTexture::kBottomUp_Orientation == texture->orientation()) {
241 values[1] = 1.0f - values[1];
242 values[3] = 1.0f - values[3];
243 // The top and bottom were just flipped, so correct the ordering
244 // of elements so that values = (l, t, r, b).
245 SkTSwap(values[1], values[3]);
246 }
247 GL_CALL(Uniform4fv(domUni, 1, values));
248 }
249 fProgramData->fTextureOrientation[s] = texture->orientation();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000250 }
junov@google.comf93e7172011-03-31 21:26:24 +0000251}
252
junov@google.comf93e7172011-03-31 21:26:24 +0000253
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000254void GrGpuGL::flushColorMatrix() {
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000255 // const ProgramDesc& desc = fCurrentProgram.getDesc();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000256 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
257 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
258 if (GrGLProgram::kUnusedUniform != matrixUni
259 && GrGLProgram::kUnusedUniform != vecUni) {
260 const float* m = this->getDrawState().getColorMatrix();
261 GrGLfloat mt[] = {
262 m[0], m[5], m[10], m[15],
263 m[1], m[6], m[11], m[16],
264 m[2], m[7], m[12], m[17],
265 m[3], m[8], m[13], m[18],
266 };
267 static float scale = 1.0f / 255.0f;
268 GrGLfloat vec[] = {
269 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
270 };
271 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
272 GL_CALL(Uniform4fv(vecUni, 1, vec));
273 }
274}
275
Scroggo01b87ec2011-05-11 18:05:38 +0000276static const float ONE_OVER_255 = 1.f / 255.f;
277
278#define GR_COLOR_TO_VEC4(color) {\
279 GrColorUnpackR(color) * ONE_OVER_255,\
280 GrColorUnpackG(color) * ONE_OVER_255,\
281 GrColorUnpackB(color) * ONE_OVER_255,\
282 GrColorUnpackA(color) * ONE_OVER_255 \
283}
284
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000285void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000286 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000287 const GrDrawState& drawState = this->getDrawState();
288
bsalomon@google.come79c8152012-03-29 19:07:12 +0000289 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000290 // color will be specified per-vertex as an attribute
291 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000292 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000293 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000294 switch (desc.fColorInput) {
295 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000296 if (fHWConstAttribColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000297 // OpenGL ES only supports the float varieties of
298 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000299 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000300 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
301 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000302 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000303 }
304 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000305 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000306 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000307 // OpenGL ES doesn't support unsigned byte varieties of
308 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000309 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000310 GrAssert(GrGLProgram::kUnusedUniform !=
311 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000312 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
313 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000314 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000315 }
316 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000317 case ProgramDesc::kSolidWhite_ColorInput:
318 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000319 break;
320 default:
321 GrCrash("Unknown color type.");
322 }
323 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000324 if (fProgramData->fUniLocations.fColorFilterUni
325 != GrGLProgram::kUnusedUniform
326 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000327 != drawState.getColorFilterColor()) {
328 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000329 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000330 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000331 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000332}
333
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000334void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000335 const ProgramDesc& desc = fCurrentProgram.getDesc();
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000336 // const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000337
338
bsalomon@google.come79c8152012-03-29 19:07:12 +0000339 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000340 // coverage will be specified per-vertex as an attribute
341 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000342 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000343 } else {
344 switch (desc.fCoverageInput) {
345 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000346 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000347 // OpenGL ES only supports the float varieties of
348 // glVertexAttrib
349 float c[] = GR_COLOR_TO_VEC4(coverage);
350 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
351 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000352 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000353 }
354 break;
355 case ProgramDesc::kUniform_ColorInput:
356 if (fProgramData->fCoverage != coverage) {
357 // OpenGL ES doesn't support unsigned byte varieties of
358 // glUniform
359 float c[] = GR_COLOR_TO_VEC4(coverage);
360 GrAssert(GrGLProgram::kUnusedUniform !=
361 fProgramData->fUniLocations.fCoverageUni);
362 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
363 1, c));
364 fProgramData->fCoverage = coverage;
365 }
366 break;
367 case ProgramDesc::kSolidWhite_ColorInput:
368 case ProgramDesc::kTransBlack_ColorInput:
369 break;
370 default:
371 GrCrash("Unknown coverage type.");
372 }
373 }
374}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000375
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000376bool GrGpuGL::flushGraphicsState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000377 const GrDrawState& drawState = this->getDrawState();
378
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000379 // GrGpu::setupClipAndFlushState should have already checked this
380 // and bailed if not true.
381 GrAssert(NULL != drawState.getRenderTarget());
382
bsalomon@google.com5a94efc2012-06-08 14:13:02 +0000383 this->flushMiscFixedFunctionState();
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000384 this->flushStencil();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000385 this->flushAAState(kDrawLines_DrawType == type);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000386
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000387 GrBlendCoeff srcCoeff;
388 GrBlendCoeff dstCoeff;
389 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
390 if (kSkipDraw_BlendOptFlag & blendOpts) {
391 return false;
392 }
393
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000394 GrCustomStage* customStages [GrDrawState::kNumStages];
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000395 this->buildProgram(kDrawPoints_DrawType == type,
396 blendOpts, dstCoeff, customStages);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000397 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
398 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000399 if (NULL == fProgramData) {
400 GrAssert(!"Failed to create program!");
401 return false;
402 }
junov@google.comf93e7172011-03-31 21:26:24 +0000403
404 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000405 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000406 fHWProgramID = fProgramData->fProgramID;
407 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000408 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000409 this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000410
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000411 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000412 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000413 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
414 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000415 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000416 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
417 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000418 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000419 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000420 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000421 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000422 }
423 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000424 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000425
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000426 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000427
tomhudson@google.com93813632011-10-27 20:21:16 +0000428 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000429 if (this->isStageEnabled(s)) {
bsalomon@google.com4c883782012-06-04 19:05:11 +0000430
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000431
432#if GR_DEBUG
433 // check for circular rendering
434 GrAssert(NULL == drawState.getRenderTarget() ||
435 NULL == drawState.getTexture(s) ||
436 drawState.getTexture(s)->asRenderTarget() !=
437 drawState.getRenderTarget());
438#endif
439
bsalomon@google.com4c883782012-06-04 19:05:11 +0000440 this->flushBoundTextureAndParams(s);
441
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000442 this->flushTextureMatrixAndDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000443
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000444 if (NULL != fProgramData->fCustomStage[s]) {
445 const GrSamplerState& sampler =
446 this->getDrawState().getSampler(s);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000447 const GrGLTexture* texture =
448 static_cast<const GrGLTexture*>(
449 this->getDrawState().getTexture(s));
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000450 fProgramData->fCustomStage[s]->setData(
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000451 this->glInterface(), *texture,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000452 *sampler.getCustomStage(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000453 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000454 }
junov@google.comf93e7172011-03-31 21:26:24 +0000455 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000456 this->flushColorMatrix();
bsalomon@google.com4c883782012-06-04 19:05:11 +0000457
458 GrIRect* rect = NULL;
459 GrIRect clipBounds;
460 if (drawState.isClipState() &&
461 fClip.hasConservativeBounds()) {
462 fClip.getConservativeBounds().roundOut(&clipBounds);
463 rect = &clipBounds;
464 }
465 // This must come after textures are flushed because a texture may need
466 // to be msaa-resolved (which will modify bound FBO state).
467 this->flushRenderTarget(rect);
468
junov@google.comf93e7172011-03-31 21:26:24 +0000469 return true;
470}
471
bsalomon@google.com2717d562012-05-07 19:10:52 +0000472#if GR_TEXT_SCALAR_IS_USHORT
473 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
474 #define TEXT_COORDS_ARE_NORMALIZED 1
475#elif GR_TEXT_SCALAR_IS_FLOAT
476 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
477 #define TEXT_COORDS_ARE_NORMALIZED 0
478#elif GR_TEXT_SCALAR_IS_FIXED
479 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
480 #define TEXT_COORDS_ARE_NORMALIZED 0
481#else
482 #error "unknown GR_TEXT_SCALAR type"
483#endif
484
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000485void GrGpuGL::setupGeometry(int* startVertex,
bsalomon@google.com49209392012-06-05 15:13:46 +0000486 int* startIndex,
487 int vertexCount,
488 int indexCount) {
junov@google.comf93e7172011-03-31 21:26:24 +0000489
490 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000491 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000492 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000493 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000494
bsalomon@google.come79c8152012-03-29 19:07:12 +0000495 GrVertexLayout currLayout = this->getVertexLayout();
496
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000497 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000498 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000499 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000500 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000501 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000502 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000503 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000504 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000505 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000506 int oldEdgeOffset;
507
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000508 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
509 fHWGeometryState.fVertexLayout,
510 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000511 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000512 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000513 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000514 bool indexed = NULL != startIndex;
515
516 int extraVertexOffset;
517 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000518 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000519
520 GrGLenum scalarType;
521 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000522 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000523 scalarType = TEXT_COORDS_GL_TYPE;
524 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000525 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000526 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
527 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000528 texCoordNorm = false;
529 }
530
531 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
532 *startVertex = 0;
533 if (indexed) {
534 *startIndex += extraIndexOffset;
535 }
536
537 // all the Pointers must be set if any of these are true
538 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
539 vertexOffset != fHWGeometryState.fVertexOffset ||
540 newStride != oldStride;
541
542 // position and tex coord offsets change if above conditions are true
543 // or the type/normalization changed based on text vs nontext type coords.
544 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000545 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000546 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000547 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000548
549 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000550 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000551 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000552 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000553 fHWGeometryState.fVertexOffset = vertexOffset;
554 }
555
tomhudson@google.com93813632011-10-27 20:21:16 +0000556 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000557 if (newTexCoordOffsets[t] > 0) {
558 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000559 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000560 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000561 GL_CALL(EnableVertexAttribArray(idx));
562 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000563 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000564 } else if (posAndTexChange ||
565 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000566 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000567 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000568 }
569 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000570 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000571 }
572 }
573
574 if (newColorOffset > 0) {
575 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000576 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000577 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000578 GL_CALL(EnableVertexAttribArray(idx));
579 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000580 true, newStride, colorOffset));
581 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000582 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000583 true, newStride, colorOffset));
584 }
585 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000586 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000587 }
588
bsalomon@google.coma3108262011-10-10 14:08:47 +0000589 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000590 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000591 int idx = GrGLProgram::CoverageAttributeIdx();
592 if (oldCoverageOffset <= 0) {
593 GL_CALL(EnableVertexAttribArray(idx));
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 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000597 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000598 true, newStride, coverageOffset));
599 }
600 } else if (oldCoverageOffset > 0) {
601 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
602 }
603
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000604 if (newEdgeOffset > 0) {
605 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
606 int idx = GrGLProgram::EdgeAttributeIdx();
607 if (oldEdgeOffset <= 0) {
608 GL_CALL(EnableVertexAttribArray(idx));
609 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
610 false, newStride, edgeOffset));
611 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
612 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
613 false, newStride, edgeOffset));
614 }
615 } else if (oldEdgeOffset > 0) {
616 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
617 }
618
bsalomon@google.come79c8152012-03-29 19:07:12 +0000619 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000620 fHWGeometryState.fArrayPtrsDirty = false;
621}
622
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000623namespace {
624
625void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
626 const GrSamplerState& sampler,
627 GrCustomStage** customStages,
628 GrGLProgram* program, int index) {
629 GrCustomStage* customStage = sampler.getCustomStage();
630 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000631 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000632 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000633 customStages[index] = customStage;
634 } else {
635 stage->fCustomStageKey = 0;
636 customStages[index] = NULL;
637 }
638}
639
640}
641
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000642void GrGpuGL::buildProgram(bool isPoints,
bsalomon@google.com49209392012-06-05 15:13:46 +0000643 BlendOptFlags blendOpts,
644 GrBlendCoeff dstCoeff,
645 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000646 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000647 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000648
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000649 // This should already have been caught
650 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
651
652 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
653
654 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
655 kEmitCoverage_BlendOptFlag));
656
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000657 // The descriptor is used as a cache key. Thus when a field of the
658 // descriptor will not affect program generation (because of the vertex
659 // layout in use or other descriptor field settings) it should be set
660 // to a canonical value to avoid duplicate programs with different keys.
661
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000662 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000663 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000664
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000665 desc.fEmitsPointSize = isPoints;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000666
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000667 bool requiresAttributeColors =
668 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000669 bool requiresAttributeCoverage =
670 !skipCoverage && SkToBool(desc.fVertexLayout &
671 kCoverage_VertexLayoutBit);
672
673 // fColorInput/fCoverageInput records how colors are specified for the.
674 // program. So we strip the bits from the layout to avoid false negatives
675 // when searching for an existing program in the cache.
676 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000677
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000678 desc.fColorFilterXfermode = skipColor ?
679 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000680 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000681
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000682 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
683
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000684 // no reason to do edge aa or look at per-vertex coverage if coverage is
685 // ignored
686 if (skipCoverage) {
687 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
688 kCoverage_VertexLayoutBit);
689 }
690
691 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
692 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
693 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000694 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000695 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000696 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000697 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000698 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000699 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000700 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000701 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000702 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000703 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000704
705 bool covIsSolidWhite = !requiresAttributeCoverage &&
706 0xffffffff == drawState.getCoverage();
707
708 if (skipCoverage) {
709 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
710 } else if (covIsSolidWhite) {
711 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
712 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
713 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
714 } else {
715 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
716 }
junov@google.comf93e7172011-03-31 21:26:24 +0000717
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000718 int lastEnabledStage = -1;
719
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000720 if (!skipCoverage && (desc.fVertexLayout &
721 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000722 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000723 } else {
724 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000725 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000726 }
727
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000728 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000729 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000730
731 stage.fOptFlags = 0;
732 stage.setEnabled(this->isStageEnabled(s));
733
734 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
735 skipCoverage;
736
737 if (!skip && stage.isEnabled()) {
738 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000739 const GrGLTexture* texture =
740 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000741 GrAssert(NULL != texture);
742 const GrSamplerState& sampler = drawState.getSampler(s);
743 // we matrix to invert when orientation is TopDown, so make sure
744 // we aren't in that case before flagging as identity.
745 if (TextureMatrixIsIdentity(texture, sampler)) {
746 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
747 } else if (!sampler.getMatrix().hasPerspective()) {
748 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
749 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000750
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000751 if (sampler.hasTextureDomain()) {
752 GrAssert(GrSamplerState::kClamp_WrapMode ==
753 sampler.getWrapX() &&
754 GrSamplerState::kClamp_WrapMode ==
755 sampler.getWrapY());
756 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
757 }
758
759 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000760 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000761 if (GrPixelConfigIsAlphaOnly(texture->config())) {
762 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000763 // the shader must smear the single channel after
764 // reading the texture
765 if (this->glCaps().textureRedSupport()) {
766 // we can use R8 textures so use kSmearRed
767 stage.fInConfigFlags |=
768 StageDesc::kSmearRed_InConfigFlag;
769 } else {
770 // we can use A8 textures so use kSmearAlpha
771 stage.fInConfigFlags |=
772 StageDesc::kSmearAlpha_InConfigFlag;
773 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000774 } else if (sampler.swapsRAndB()) {
775 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
776 }
777 }
778 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000779 // The shader generator assumes that color channels are bytes
780 // when rounding.
781 GrAssert(4 == GrBytesPerPixel(texture->config()));
782 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
783 fUnpremulConversion) {
784 stage.fInConfigFlags |=
785 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
786 } else {
787 stage.fInConfigFlags |=
788 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
789 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000790 }
791
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000792 setup_custom_stage(&stage, sampler, customStages,
793 &fCurrentProgram, s);
794
junov@google.comf93e7172011-03-31 21:26:24 +0000795 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000796 stage.fOptFlags = 0;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000797 stage.fInConfigFlags = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000798 stage.fCustomStageKey = 0;
799 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000800 }
801 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000802
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000803 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000804 // The shader generator assumes that color channels are bytes
805 // when rounding.
806 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
807 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
808 desc.fOutputConfig =
809 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
810 } else {
811 desc.fOutputConfig =
812 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
813 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000814 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000815 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000816 }
817
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000818 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000819
820 // currently the experimental GS will only work with triangle prims
821 // (and it doesn't do anything other than pass through values from
822 // the VS to the FS anyway).
823#if 0 && GR_GL_EXPERIMENTAL_GS
824 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
825#endif
826
bsalomon@google.coma3108262011-10-10 14:08:47 +0000827 // we want to avoid generating programs with different "first cov stage"
828 // values when they would compute the same result.
829 // We set field in the desc to kNumStages when either there are no
830 // coverage stages or the distinction between coverage and color is
831 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000832 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +0000833 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000834 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000835 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000836 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000837 }
838
839 // other coverage inputs
840 if (!hasCoverage) {
841 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000842 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +0000843 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
844 }
845
846 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000847 // color filter is applied between color/coverage computation
848 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000849 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000850 }
851
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000852 if (this->getCaps().fDualSourceBlendingSupport &&
853 !(blendOpts & (kEmitCoverage_BlendOptFlag |
854 kCoverageAsAlpha_BlendOptFlag))) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000855 if (kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000856 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000857 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000858 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000859 } else if (kSA_GrBlendCoeff == 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::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000863 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000864 } else if (kSC_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000865 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
866 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000867 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000868 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000869 }
870 }
871 }
junov@google.comf93e7172011-03-31 21:26:24 +0000872}