blob: e79afe15e2000fe78e937df49402e2a6223dfff3 [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.com898e7b52012-06-01 20:42:15 +000010#include "effects/GrGradientEffects.h"
11
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000012#include "GrCustomStage.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000013#include "GrGLProgramStage.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000014#include "GrGpuVertex.h"
junov@google.comf93e7172011-03-31 21:26:24 +000015
junov@google.comf93e7172011-03-31 21:26:24 +000016#define SKIP_CACHE_CHECK true
17#define GR_UINT32_MAX static_cast<uint32_t>(-1)
18
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000019void GrGpuGL::ProgramCache::Entry::copyAndTakeOwnership(Entry& entry) {
20 fProgramData.copyAndTakeOwnership(entry.fProgramData);
21 fKey = entry.fKey; // ownership transfer
22 fLRUStamp = entry.fLRUStamp;
23}
junov@google.comf93e7172011-03-31 21:26:24 +000024
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000025GrGpuGL::ProgramCache::ProgramCache(const GrGLContextInfo& gl)
26 : fCount(0)
27 , fCurrLRUStamp(0)
28 , fGL(gl) {
29}
junov@google.comf93e7172011-03-31 21:26:24 +000030
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000031GrGpuGL::ProgramCache::~ProgramCache() {
32 for (int i = 0; i < fCount; ++i) {
33 GrGpuGL::DeleteProgram(fGL.interface(),
34 &fEntries[i].fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000035 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000036}
junov@google.comf93e7172011-03-31 21:26:24 +000037
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000038void GrGpuGL::ProgramCache::abandon() {
39 fCount = 0;
40}
41
42void GrGpuGL::ProgramCache::invalidateViewMatrices() {
43 for (int i = 0; i < fCount; ++i) {
44 // set to illegal matrix
45 fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +000046 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000047}
junov@google.comf93e7172011-03-31 21:26:24 +000048
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000049GrGLProgram::CachedData* GrGpuGL::ProgramCache::getProgramData(
50 const GrGLProgram& desc,
51 GrCustomStage** stages) {
52 Entry newEntry;
53 newEntry.fKey.setKeyData(desc.keyData());
junov@google.comf7c00f62011-08-18 18:15:16 +000054
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000055 Entry* entry = fHashCache.find(newEntry.fKey);
56 if (NULL == entry) {
57 if (!desc.genProgram(fGL, stages, &newEntry.fProgramData)) {
58 return NULL;
59 }
60 if (fCount < kMaxEntries) {
61 entry = fEntries + fCount;
62 ++fCount;
63 } else {
64 GrAssert(kMaxEntries == fCount);
65 entry = fEntries;
66 for (int i = 1; i < kMaxEntries; ++i) {
67 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
68 entry = fEntries + i;
junov@google.comf93e7172011-03-31 21:26:24 +000069 }
junov@google.comf93e7172011-03-31 21:26:24 +000070 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000071 fHashCache.remove(entry->fKey, entry);
72 GrGpuGL::DeleteProgram(fGL.interface(),
73 &entry->fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000074 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000075 entry->copyAndTakeOwnership(newEntry);
76 fHashCache.insert(entry->fKey, entry);
junov@google.comf93e7172011-03-31 21:26:24 +000077 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000078
79 entry->fLRUStamp = fCurrLRUStamp;
80 if (GR_UINT32_MAX == fCurrLRUStamp) {
81 // wrap around! just trash our LRU, one time hit.
82 for (int i = 0; i < fCount; ++i) {
83 fEntries[i].fLRUStamp = 0;
84 }
85 }
86 ++fCurrLRUStamp;
87 return &entry->fProgramData;
88}
junov@google.comf93e7172011-03-31 21:26:24 +000089
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000090void GrGpuGL::DeleteProgram(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +000091 CachedData* programData) {
92 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000093 if (programData->fGShaderID) {
94 GR_GL_CALL(gl, DeleteShader(programData->fGShaderID));
95 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000096 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
97 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +000098 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
99}
100
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000101////////////////////////////////////////////////////////////////////////////////
102
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000103void GrGpuGL::abandonResources(){
104 INHERITED::abandonResources();
105 fProgramCache->abandon();
106 fHWProgramID = 0;
107}
108
109////////////////////////////////////////////////////////////////////////////////
110
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000111#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
112
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000113void GrGpuGL::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000114 const GrMatrix& vm = this->getDrawState().getViewMatrix();
bsalomon@google.com341767c2012-05-11 20:47:39 +0000115 if (!fProgramData->fViewMatrix.cheapEqualTo(vm)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000116
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000117 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
118 GrAssert(NULL != rt);
119 GrMatrix m;
120 m.setAll(
121 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
122 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
123 0, 0, GrMatrix::I()[8]);
124 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000125
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000126 // ES doesn't allow you to pass true to the transpose param,
127 // so do our own transpose
128 GrGLfloat mt[] = {
129 GrScalarToFloat(m[GrMatrix::kMScaleX]),
130 GrScalarToFloat(m[GrMatrix::kMSkewY]),
131 GrScalarToFloat(m[GrMatrix::kMPersp0]),
132 GrScalarToFloat(m[GrMatrix::kMSkewX]),
133 GrScalarToFloat(m[GrMatrix::kMScaleY]),
134 GrScalarToFloat(m[GrMatrix::kMPersp1]),
135 GrScalarToFloat(m[GrMatrix::kMTransX]),
136 GrScalarToFloat(m[GrMatrix::kMTransY]),
137 GrScalarToFloat(m[GrMatrix::kMPersp2])
138 };
139
bsalomon@google.com341767c2012-05-11 20:47:39 +0000140 GrAssert(GrGLProgram::kUnusedUniform !=
141 fProgramData->fUniLocations.fViewMatrixUni);
142 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
143 1, false, mt));
144 fProgramData->fViewMatrix = vm;
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
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000253void GrGpuGL::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000254 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000255 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000256 const GrGLTexture* texture =
257 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000258 if (texture->width() != fProgramData->fTextureWidth[s] ||
259 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000260
bsalomon@google.com99621082011-11-15 16:47:16 +0000261 float texelSize[] = {1.f / texture->width(),
262 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000263 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000264 fProgramData->fTextureWidth[s] = texture->width();
265 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000266 }
267 }
junov@google.comf93e7172011-03-31 21:26:24 +0000268}
269
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000270void GrGpuGL::flushColorMatrix() {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000271 const ProgramDesc& desc = fCurrentProgram.getDesc();
272 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
273 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
274 if (GrGLProgram::kUnusedUniform != matrixUni
275 && GrGLProgram::kUnusedUniform != vecUni) {
276 const float* m = this->getDrawState().getColorMatrix();
277 GrGLfloat mt[] = {
278 m[0], m[5], m[10], m[15],
279 m[1], m[6], m[11], m[16],
280 m[2], m[7], m[12], m[17],
281 m[3], m[8], m[13], m[18],
282 };
283 static float scale = 1.0f / 255.0f;
284 GrGLfloat vec[] = {
285 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
286 };
287 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
288 GL_CALL(Uniform4fv(vecUni, 1, vec));
289 }
290}
291
Scroggo01b87ec2011-05-11 18:05:38 +0000292static const float ONE_OVER_255 = 1.f / 255.f;
293
294#define GR_COLOR_TO_VEC4(color) {\
295 GrColorUnpackR(color) * ONE_OVER_255,\
296 GrColorUnpackG(color) * ONE_OVER_255,\
297 GrColorUnpackB(color) * ONE_OVER_255,\
298 GrColorUnpackA(color) * ONE_OVER_255 \
299}
300
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000301void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000302 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000303 const GrDrawState& drawState = this->getDrawState();
304
bsalomon@google.come79c8152012-03-29 19:07:12 +0000305 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000306 // color will be specified per-vertex as an attribute
307 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000308 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000309 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000310 switch (desc.fColorInput) {
311 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000312 if (fHWConstAttribColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000313 // OpenGL ES only supports the float varieties of
314 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000315 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000316 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
317 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000318 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000319 }
320 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000321 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000322 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000323 // OpenGL ES doesn't support unsigned byte varieties of
324 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000325 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000326 GrAssert(GrGLProgram::kUnusedUniform !=
327 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000328 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
329 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000330 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000331 }
332 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000333 case ProgramDesc::kSolidWhite_ColorInput:
334 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000335 break;
336 default:
337 GrCrash("Unknown color type.");
338 }
339 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000340 if (fProgramData->fUniLocations.fColorFilterUni
341 != GrGLProgram::kUnusedUniform
342 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000343 != drawState.getColorFilterColor()) {
344 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000345 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000346 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000347 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000348}
349
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000350void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000351 const ProgramDesc& desc = fCurrentProgram.getDesc();
352 const GrDrawState& drawState = this->getDrawState();
353
354
bsalomon@google.come79c8152012-03-29 19:07:12 +0000355 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000356 // coverage will be specified per-vertex as an attribute
357 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000358 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000359 } else {
360 switch (desc.fCoverageInput) {
361 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000362 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000363 // OpenGL ES only supports the float varieties of
364 // glVertexAttrib
365 float c[] = GR_COLOR_TO_VEC4(coverage);
366 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
367 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000368 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000369 }
370 break;
371 case ProgramDesc::kUniform_ColorInput:
372 if (fProgramData->fCoverage != coverage) {
373 // OpenGL ES doesn't support unsigned byte varieties of
374 // glUniform
375 float c[] = GR_COLOR_TO_VEC4(coverage);
376 GrAssert(GrGLProgram::kUnusedUniform !=
377 fProgramData->fUniLocations.fCoverageUni);
378 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
379 1, c));
380 fProgramData->fCoverage = coverage;
381 }
382 break;
383 case ProgramDesc::kSolidWhite_ColorInput:
384 case ProgramDesc::kTransBlack_ColorInput:
385 break;
386 default:
387 GrCrash("Unknown coverage type.");
388 }
389 }
390}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000391
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000392bool GrGpuGL::flushGraphicsState(GrPrimitiveType type) {
junov@google.comf93e7172011-03-31 21:26:24 +0000393 if (!flushGLStateCommon(type)) {
394 return false;
395 }
396
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000397 const GrDrawState& drawState = this->getDrawState();
398
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000399 GrBlendCoeff srcCoeff;
400 GrBlendCoeff dstCoeff;
401 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
402 if (kSkipDraw_BlendOptFlag & blendOpts) {
403 return false;
404 }
405
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000406 GrCustomStage* customStages [GrDrawState::kNumStages];
407 this->buildProgram(type, blendOpts, dstCoeff, customStages);
408 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
409 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000410 if (NULL == fProgramData) {
411 GrAssert(!"Failed to create program!");
412 return false;
413 }
junov@google.comf93e7172011-03-31 21:26:24 +0000414
415 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000416 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000417 fHWProgramID = fProgramData->fProgramID;
418 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000419 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
420 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000421
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000422 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000423 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000424 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
425 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000426 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000427 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
428 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000429 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000430 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000431 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000432 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000433 }
434 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000435 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000436
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000437 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000438
tomhudson@google.com93813632011-10-27 20:21:16 +0000439 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000440 if (this->isStageEnabled(s)) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000441 this->flushTextureMatrixAndDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000442
bsalomon@google.com40d92932011-12-13 18:40:47 +0000443 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000444
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000445 if (NULL != fProgramData->fCustomStage[s]) {
446 const GrSamplerState& sampler =
447 this->getDrawState().getSampler(s);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000448 const GrGLTexture* texture =
449 static_cast<const GrGLTexture*>(
450 this->getDrawState().getTexture(s));
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000451 fProgramData->fCustomStage[s]->setData(
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000452 this->glInterface(), *texture,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000453 *sampler.getCustomStage(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000454 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000455 }
junov@google.comf93e7172011-03-31 21:26:24 +0000456 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000457 this->flushColorMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000458 return true;
459}
460
bsalomon@google.com2717d562012-05-07 19:10:52 +0000461#if GR_TEXT_SCALAR_IS_USHORT
462 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
463 #define TEXT_COORDS_ARE_NORMALIZED 1
464#elif GR_TEXT_SCALAR_IS_FLOAT
465 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
466 #define TEXT_COORDS_ARE_NORMALIZED 0
467#elif GR_TEXT_SCALAR_IS_FIXED
468 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
469 #define TEXT_COORDS_ARE_NORMALIZED 0
470#else
471 #error "unknown GR_TEXT_SCALAR type"
472#endif
473
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000474void GrGpuGL::setupGeometry(int* startVertex,
junov@google.comf93e7172011-03-31 21:26:24 +0000475 int* startIndex,
476 int vertexCount,
477 int indexCount) {
478
479 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000480 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000481 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000482 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000483
bsalomon@google.come79c8152012-03-29 19:07:12 +0000484 GrVertexLayout currLayout = this->getVertexLayout();
485
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000486 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000487 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000488 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000489 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000490 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000491 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000492 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000493 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000494 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000495 int oldEdgeOffset;
496
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000497 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
498 fHWGeometryState.fVertexLayout,
499 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000500 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000501 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000502 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000503 bool indexed = NULL != startIndex;
504
505 int extraVertexOffset;
506 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000507 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000508
509 GrGLenum scalarType;
510 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000511 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000512 scalarType = TEXT_COORDS_GL_TYPE;
513 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000514 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000515 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
516 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000517 texCoordNorm = false;
518 }
519
520 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
521 *startVertex = 0;
522 if (indexed) {
523 *startIndex += extraIndexOffset;
524 }
525
526 // all the Pointers must be set if any of these are true
527 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
528 vertexOffset != fHWGeometryState.fVertexOffset ||
529 newStride != oldStride;
530
531 // position and tex coord offsets change if above conditions are true
532 // or the type/normalization changed based on text vs nontext type coords.
533 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000534 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000535 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000536 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000537
538 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000539 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000540 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000541 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000542 fHWGeometryState.fVertexOffset = vertexOffset;
543 }
544
tomhudson@google.com93813632011-10-27 20:21:16 +0000545 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000546 if (newTexCoordOffsets[t] > 0) {
547 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000548 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000549 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000550 GL_CALL(EnableVertexAttribArray(idx));
551 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000552 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000553 } else if (posAndTexChange ||
554 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000555 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000556 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000557 }
558 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000559 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000560 }
561 }
562
563 if (newColorOffset > 0) {
564 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000565 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000566 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000567 GL_CALL(EnableVertexAttribArray(idx));
568 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000569 true, newStride, colorOffset));
570 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000571 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000572 true, newStride, colorOffset));
573 }
574 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000575 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000576 }
577
bsalomon@google.coma3108262011-10-10 14:08:47 +0000578 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000579 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000580 int idx = GrGLProgram::CoverageAttributeIdx();
581 if (oldCoverageOffset <= 0) {
582 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000583 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000584 true, newStride, coverageOffset));
585 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000586 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000587 true, newStride, coverageOffset));
588 }
589 } else if (oldCoverageOffset > 0) {
590 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
591 }
592
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000593 if (newEdgeOffset > 0) {
594 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
595 int idx = GrGLProgram::EdgeAttributeIdx();
596 if (oldEdgeOffset <= 0) {
597 GL_CALL(EnableVertexAttribArray(idx));
598 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
599 false, newStride, edgeOffset));
600 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
601 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
602 false, newStride, edgeOffset));
603 }
604 } else if (oldEdgeOffset > 0) {
605 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
606 }
607
bsalomon@google.come79c8152012-03-29 19:07:12 +0000608 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000609 fHWGeometryState.fArrayPtrsDirty = false;
610}
611
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000612namespace {
613
614void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
615 const GrSamplerState& sampler,
616 GrCustomStage** customStages,
617 GrGLProgram* program, int index) {
618 GrCustomStage* customStage = sampler.getCustomStage();
619 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000620 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000621 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000622 customStages[index] = customStage;
623 } else {
624 stage->fCustomStageKey = 0;
625 customStages[index] = NULL;
626 }
627}
628
629}
630
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000631void GrGpuGL::buildProgram(GrPrimitiveType type,
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000632 BlendOptFlags blendOpts,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000633 GrBlendCoeff dstCoeff,
634 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000635 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000636 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000637
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000638 // This should already have been caught
639 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
640
641 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
642
643 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
644 kEmitCoverage_BlendOptFlag));
645
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000646 // The descriptor is used as a cache key. Thus when a field of the
647 // descriptor will not affect program generation (because of the vertex
648 // layout in use or other descriptor field settings) it should be set
649 // to a canonical value to avoid duplicate programs with different keys.
650
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000651 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000652 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000653
654 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
655
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000656 bool requiresAttributeColors =
657 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000658 bool requiresAttributeCoverage =
659 !skipCoverage && SkToBool(desc.fVertexLayout &
660 kCoverage_VertexLayoutBit);
661
662 // fColorInput/fCoverageInput records how colors are specified for the.
663 // program. So we strip the bits from the layout to avoid false negatives
664 // when searching for an existing program in the cache.
665 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000666
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000667 desc.fColorFilterXfermode = skipColor ?
668 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000669 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000670
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000671 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
672
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000673 // no reason to do edge aa or look at per-vertex coverage if coverage is
674 // ignored
675 if (skipCoverage) {
676 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
677 kCoverage_VertexLayoutBit);
678 }
679
680 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
681 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
682 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000683 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000684 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000685 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000686 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000687 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000688 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000689 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000690 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000691 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000692 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000693
694 bool covIsSolidWhite = !requiresAttributeCoverage &&
695 0xffffffff == drawState.getCoverage();
696
697 if (skipCoverage) {
698 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
699 } else if (covIsSolidWhite) {
700 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
701 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
702 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
703 } else {
704 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
705 }
junov@google.comf93e7172011-03-31 21:26:24 +0000706
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000707 int lastEnabledStage = -1;
708
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000709 if (!skipCoverage && (desc.fVertexLayout &
710 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000711 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000712 } else {
713 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000714 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000715 }
716
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000717 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000718 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000719
720 stage.fOptFlags = 0;
721 stage.setEnabled(this->isStageEnabled(s));
722
723 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
724 skipCoverage;
725
726 if (!skip && stage.isEnabled()) {
727 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000728 const GrGLTexture* texture =
729 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000730 GrAssert(NULL != texture);
731 const GrSamplerState& sampler = drawState.getSampler(s);
732 // we matrix to invert when orientation is TopDown, so make sure
733 // we aren't in that case before flagging as identity.
734 if (TextureMatrixIsIdentity(texture, sampler)) {
735 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
736 } else if (!sampler.getMatrix().hasPerspective()) {
737 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
738 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000739
740 switch (sampler.getFilter()) {
741 // these both can use a regular texture2D()
742 case GrSamplerState::kNearest_Filter:
743 case GrSamplerState::kBilinear_Filter:
744 stage.fFetchMode = StageDesc::kSingle_FetchMode;
745 break;
746 // performs 4 texture2D()s
747 case GrSamplerState::k4x4Downsample_Filter:
748 stage.fFetchMode = StageDesc::k2x2_FetchMode;
749 break;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000750 default:
751 GrCrash("Unexpected filter!");
752 break;
753 }
754
755 if (sampler.hasTextureDomain()) {
756 GrAssert(GrSamplerState::kClamp_WrapMode ==
757 sampler.getWrapX() &&
758 GrSamplerState::kClamp_WrapMode ==
759 sampler.getWrapY());
760 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
761 }
762
763 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000764 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000765 if (GrPixelConfigIsAlphaOnly(texture->config())) {
766 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000767 // the shader must smear the single channel after
768 // reading the texture
769 if (this->glCaps().textureRedSupport()) {
770 // we can use R8 textures so use kSmearRed
771 stage.fInConfigFlags |=
772 StageDesc::kSmearRed_InConfigFlag;
773 } else {
774 // we can use A8 textures so use kSmearAlpha
775 stage.fInConfigFlags |=
776 StageDesc::kSmearAlpha_InConfigFlag;
777 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000778 } else if (sampler.swapsRAndB()) {
779 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
780 }
781 }
782 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000783 // The shader generator assumes that color channels are bytes
784 // when rounding.
785 GrAssert(4 == GrBytesPerPixel(texture->config()));
786 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
787 fUnpremulConversion) {
788 stage.fInConfigFlags |=
789 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
790 } else {
791 stage.fInConfigFlags |=
792 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
793 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000794 }
795
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000796 setup_custom_stage(&stage, sampler, customStages,
797 &fCurrentProgram, s);
798
junov@google.comf93e7172011-03-31 21:26:24 +0000799 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000800 stage.fOptFlags = 0;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000801 stage.fInConfigFlags = 0;
802 stage.fFetchMode = (StageDesc::FetchMode) 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000803 stage.fCustomStageKey = 0;
804 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000805 }
806 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000807
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000808 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000809 // The shader generator assumes that color channels are bytes
810 // when rounding.
811 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
812 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
813 desc.fOutputConfig =
814 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
815 } else {
816 desc.fOutputConfig =
817 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
818 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000819 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000820 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000821 }
822
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000823 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000824
825 // currently the experimental GS will only work with triangle prims
826 // (and it doesn't do anything other than pass through values from
827 // the VS to the FS anyway).
828#if 0 && GR_GL_EXPERIMENTAL_GS
829 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
830#endif
831
bsalomon@google.coma3108262011-10-10 14:08:47 +0000832 // we want to avoid generating programs with different "first cov stage"
833 // values when they would compute the same result.
834 // We set field in the desc to kNumStages when either there are no
835 // coverage stages or the distinction between coverage and color is
836 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000837 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +0000838 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000839 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000840 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000841 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000842 }
843
844 // other coverage inputs
845 if (!hasCoverage) {
846 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000847 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +0000848 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
849 }
850
851 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000852 // color filter is applied between color/coverage computation
853 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000854 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000855 }
856
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000857 if (this->getCaps().fDualSourceBlendingSupport &&
858 !(blendOpts & (kEmitCoverage_BlendOptFlag |
859 kCoverageAsAlpha_BlendOptFlag))) {
860 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000861 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000862 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000863 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000864 } else if (kSA_BlendCoeff == 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::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000868 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000869 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000870 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
871 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000872 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000873 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000874 }
875 }
876 }
junov@google.comf93e7172011-03-31 21:26:24 +0000877}