blob: 3ad352070a82985bf92035cb9037205927d7bb96 [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.coma3201942012-06-21 19:58:20 +0000457 this->flushScissor();
bsalomon@google.com4c883782012-06-04 19:05:11 +0000458
459 GrIRect* rect = NULL;
460 GrIRect clipBounds;
461 if (drawState.isClipState() &&
462 fClip.hasConservativeBounds()) {
463 fClip.getConservativeBounds().roundOut(&clipBounds);
464 rect = &clipBounds;
465 }
466 // This must come after textures are flushed because a texture may need
467 // to be msaa-resolved (which will modify bound FBO state).
468 this->flushRenderTarget(rect);
469
junov@google.comf93e7172011-03-31 21:26:24 +0000470 return true;
471}
472
bsalomon@google.com2717d562012-05-07 19:10:52 +0000473#if GR_TEXT_SCALAR_IS_USHORT
474 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
475 #define TEXT_COORDS_ARE_NORMALIZED 1
476#elif GR_TEXT_SCALAR_IS_FLOAT
477 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
478 #define TEXT_COORDS_ARE_NORMALIZED 0
479#elif GR_TEXT_SCALAR_IS_FIXED
480 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
481 #define TEXT_COORDS_ARE_NORMALIZED 0
482#else
483 #error "unknown GR_TEXT_SCALAR type"
484#endif
485
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000486void GrGpuGL::setupGeometry(int* startVertex,
bsalomon@google.com49209392012-06-05 15:13:46 +0000487 int* startIndex,
488 int vertexCount,
489 int indexCount) {
junov@google.comf93e7172011-03-31 21:26:24 +0000490
491 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000492 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000493 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000494 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000495
bsalomon@google.come79c8152012-03-29 19:07:12 +0000496 GrVertexLayout currLayout = this->getVertexLayout();
497
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000498 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000499 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000500 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000501 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000502 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000503 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000504 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000505 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000506 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000507 int oldEdgeOffset;
508
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000509 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
510 fHWGeometryState.fVertexLayout,
511 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000512 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000513 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000514 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000515 bool indexed = NULL != startIndex;
516
517 int extraVertexOffset;
518 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000519 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000520
521 GrGLenum scalarType;
522 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000523 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000524 scalarType = TEXT_COORDS_GL_TYPE;
525 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000526 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000527 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
528 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000529 texCoordNorm = false;
530 }
531
532 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
533 *startVertex = 0;
534 if (indexed) {
535 *startIndex += extraIndexOffset;
536 }
537
538 // all the Pointers must be set if any of these are true
539 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
540 vertexOffset != fHWGeometryState.fVertexOffset ||
541 newStride != oldStride;
542
543 // position and tex coord offsets change if above conditions are true
544 // or the type/normalization changed based on text vs nontext type coords.
545 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000546 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000547 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000548 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000549
550 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000551 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000552 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000553 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000554 fHWGeometryState.fVertexOffset = vertexOffset;
555 }
556
tomhudson@google.com93813632011-10-27 20:21:16 +0000557 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000558 if (newTexCoordOffsets[t] > 0) {
559 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000560 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000561 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000562 GL_CALL(EnableVertexAttribArray(idx));
563 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 } else if (posAndTexChange ||
566 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000567 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000568 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000569 }
570 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000571 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000572 }
573 }
574
575 if (newColorOffset > 0) {
576 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000577 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000578 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000579 GL_CALL(EnableVertexAttribArray(idx));
580 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000581 true, newStride, colorOffset));
582 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000583 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000584 true, newStride, colorOffset));
585 }
586 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000587 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000588 }
589
bsalomon@google.coma3108262011-10-10 14:08:47 +0000590 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000591 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000592 int idx = GrGLProgram::CoverageAttributeIdx();
593 if (oldCoverageOffset <= 0) {
594 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000595 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000596 true, newStride, coverageOffset));
597 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000598 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000599 true, newStride, coverageOffset));
600 }
601 } else if (oldCoverageOffset > 0) {
602 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
603 }
604
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000605 if (newEdgeOffset > 0) {
606 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
607 int idx = GrGLProgram::EdgeAttributeIdx();
608 if (oldEdgeOffset <= 0) {
609 GL_CALL(EnableVertexAttribArray(idx));
610 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
611 false, newStride, edgeOffset));
612 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
613 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
614 false, newStride, edgeOffset));
615 }
616 } else if (oldEdgeOffset > 0) {
617 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
618 }
619
bsalomon@google.come79c8152012-03-29 19:07:12 +0000620 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000621 fHWGeometryState.fArrayPtrsDirty = false;
622}
623
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000624namespace {
625
626void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
627 const GrSamplerState& sampler,
628 GrCustomStage** customStages,
629 GrGLProgram* program, int index) {
630 GrCustomStage* customStage = sampler.getCustomStage();
631 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000632 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000633 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000634 customStages[index] = customStage;
635 } else {
636 stage->fCustomStageKey = 0;
637 customStages[index] = NULL;
638 }
639}
640
641}
642
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000643void GrGpuGL::buildProgram(bool isPoints,
bsalomon@google.com49209392012-06-05 15:13:46 +0000644 BlendOptFlags blendOpts,
645 GrBlendCoeff dstCoeff,
646 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000647 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000648 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000649
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000650 // This should already have been caught
651 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
652
653 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
654
655 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
656 kEmitCoverage_BlendOptFlag));
657
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000658 // The descriptor is used as a cache key. Thus when a field of the
659 // descriptor will not affect program generation (because of the vertex
660 // layout in use or other descriptor field settings) it should be set
661 // to a canonical value to avoid duplicate programs with different keys.
662
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000663 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000664 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000665
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000666 desc.fEmitsPointSize = isPoints;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000667
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000668 bool requiresAttributeColors =
669 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000670 bool requiresAttributeCoverage =
671 !skipCoverage && SkToBool(desc.fVertexLayout &
672 kCoverage_VertexLayoutBit);
673
674 // fColorInput/fCoverageInput records how colors are specified for the.
675 // program. So we strip the bits from the layout to avoid false negatives
676 // when searching for an existing program in the cache.
677 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000678
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000679 desc.fColorFilterXfermode = skipColor ?
680 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000681 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000682
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000683 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
684
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000685 // no reason to do edge aa or look at per-vertex coverage if coverage is
686 // ignored
687 if (skipCoverage) {
688 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
689 kCoverage_VertexLayoutBit);
690 }
691
692 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
693 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
694 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000695 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000696 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000697 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000698 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000699 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000700 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000701 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000702 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000703 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000704 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000705
706 bool covIsSolidWhite = !requiresAttributeCoverage &&
707 0xffffffff == drawState.getCoverage();
708
709 if (skipCoverage) {
710 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
711 } else if (covIsSolidWhite) {
712 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
713 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
714 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
715 } else {
716 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
717 }
junov@google.comf93e7172011-03-31 21:26:24 +0000718
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000719 int lastEnabledStage = -1;
720
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000721 if (!skipCoverage && (desc.fVertexLayout &
722 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000723 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000724 } else {
725 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000726 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000727 }
728
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000729 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000730 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000731
732 stage.fOptFlags = 0;
733 stage.setEnabled(this->isStageEnabled(s));
734
735 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
736 skipCoverage;
737
738 if (!skip && stage.isEnabled()) {
739 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000740 const GrGLTexture* texture =
741 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000742 GrAssert(NULL != texture);
743 const GrSamplerState& sampler = drawState.getSampler(s);
744 // we matrix to invert when orientation is TopDown, so make sure
745 // we aren't in that case before flagging as identity.
746 if (TextureMatrixIsIdentity(texture, sampler)) {
747 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
748 } else if (!sampler.getMatrix().hasPerspective()) {
749 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
750 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000751
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000752 if (sampler.hasTextureDomain()) {
753 GrAssert(GrSamplerState::kClamp_WrapMode ==
754 sampler.getWrapX() &&
755 GrSamplerState::kClamp_WrapMode ==
756 sampler.getWrapY());
757 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
758 }
759
760 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000761 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000762 if (GrPixelConfigIsAlphaOnly(texture->config())) {
763 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000764 // the shader must smear the single channel after
765 // reading the texture
766 if (this->glCaps().textureRedSupport()) {
767 // we can use R8 textures so use kSmearRed
768 stage.fInConfigFlags |=
769 StageDesc::kSmearRed_InConfigFlag;
770 } else {
771 // we can use A8 textures so use kSmearAlpha
772 stage.fInConfigFlags |=
773 StageDesc::kSmearAlpha_InConfigFlag;
774 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000775 } else if (sampler.swapsRAndB()) {
776 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
777 }
778 }
779 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000780 // The shader generator assumes that color channels are bytes
781 // when rounding.
782 GrAssert(4 == GrBytesPerPixel(texture->config()));
783 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
784 fUnpremulConversion) {
785 stage.fInConfigFlags |=
786 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
787 } else {
788 stage.fInConfigFlags |=
789 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
790 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000791 }
792
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000793 setup_custom_stage(&stage, sampler, customStages,
794 &fCurrentProgram, s);
795
junov@google.comf93e7172011-03-31 21:26:24 +0000796 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000797 stage.fOptFlags = 0;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000798 stage.fInConfigFlags = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000799 stage.fCustomStageKey = 0;
800 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000801 }
802 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000803
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000804 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000805 // The shader generator assumes that color channels are bytes
806 // when rounding.
807 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
808 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
809 desc.fOutputConfig =
810 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
811 } else {
812 desc.fOutputConfig =
813 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
814 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000815 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000816 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000817 }
818
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000819 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000820
821 // currently the experimental GS will only work with triangle prims
822 // (and it doesn't do anything other than pass through values from
823 // the VS to the FS anyway).
824#if 0 && GR_GL_EXPERIMENTAL_GS
825 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
826#endif
827
bsalomon@google.coma3108262011-10-10 14:08:47 +0000828 // we want to avoid generating programs with different "first cov stage"
829 // values when they would compute the same result.
830 // We set field in the desc to kNumStages when either there are no
831 // coverage stages or the distinction between coverage and color is
832 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000833 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +0000834 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000835 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000836 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000837 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000838 }
839
840 // other coverage inputs
841 if (!hasCoverage) {
842 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000843 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +0000844 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
845 }
846
847 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000848 // color filter is applied between color/coverage computation
849 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000850 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000851 }
852
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000853 if (this->getCaps().fDualSourceBlendingSupport &&
854 !(blendOpts & (kEmitCoverage_BlendOptFlag |
855 kCoverageAsAlpha_BlendOptFlag))) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000856 if (kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000857 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000858 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000859 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000860 } else if (kSA_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000861 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
862 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000863 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000864 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000865 } else if (kSC_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000866 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
867 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000868 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000869 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000870 }
871 }
872 }
junov@google.comf93e7172011-03-31 21:26:24 +0000873}