blob: afbe809e9db94ae32421aca55187a3454c53c99d [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
40void GrGpuGL::ProgramCache::invalidateViewMatrices() {
41 for (int i = 0; i < fCount; ++i) {
42 // set to illegal matrix
43 fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +000044 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000045}
junov@google.comf93e7172011-03-31 21:26:24 +000046
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000047GrGLProgram::CachedData* GrGpuGL::ProgramCache::getProgramData(
48 const GrGLProgram& desc,
49 GrCustomStage** stages) {
50 Entry newEntry;
51 newEntry.fKey.setKeyData(desc.keyData());
junov@google.comf7c00f62011-08-18 18:15:16 +000052
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000053 Entry* entry = fHashCache.find(newEntry.fKey);
54 if (NULL == entry) {
55 if (!desc.genProgram(fGL, stages, &newEntry.fProgramData)) {
56 return NULL;
57 }
58 if (fCount < kMaxEntries) {
59 entry = fEntries + fCount;
60 ++fCount;
61 } else {
62 GrAssert(kMaxEntries == fCount);
63 entry = fEntries;
64 for (int i = 1; i < kMaxEntries; ++i) {
65 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
66 entry = fEntries + i;
junov@google.comf93e7172011-03-31 21:26:24 +000067 }
junov@google.comf93e7172011-03-31 21:26:24 +000068 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000069 fHashCache.remove(entry->fKey, entry);
70 GrGpuGL::DeleteProgram(fGL.interface(),
71 &entry->fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000072 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000073 entry->copyAndTakeOwnership(newEntry);
74 fHashCache.insert(entry->fKey, entry);
junov@google.comf93e7172011-03-31 21:26:24 +000075 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000076
77 entry->fLRUStamp = fCurrLRUStamp;
78 if (GR_UINT32_MAX == fCurrLRUStamp) {
79 // wrap around! just trash our LRU, one time hit.
80 for (int i = 0; i < fCount; ++i) {
81 fEntries[i].fLRUStamp = 0;
82 }
83 }
84 ++fCurrLRUStamp;
85 return &entry->fProgramData;
86}
junov@google.comf93e7172011-03-31 21:26:24 +000087
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000088void GrGpuGL::DeleteProgram(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +000089 CachedData* programData) {
90 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000091 if (programData->fGShaderID) {
92 GR_GL_CALL(gl, DeleteShader(programData->fGShaderID));
93 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000094 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
95 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +000096 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
97}
98
bsalomon@google.com1e257a52011-07-06 19:52:16 +000099////////////////////////////////////////////////////////////////////////////////
100
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000101void GrGpuGL::abandonResources(){
102 INHERITED::abandonResources();
103 fProgramCache->abandon();
104 fHWProgramID = 0;
105}
106
107////////////////////////////////////////////////////////////////////////////////
108
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000109#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
110
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000111void GrGpuGL::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000112 const GrMatrix& vm = this->getDrawState().getViewMatrix();
bsalomon@google.com341767c2012-05-11 20:47:39 +0000113 if (!fProgramData->fViewMatrix.cheapEqualTo(vm)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000114
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000115 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
116 GrAssert(NULL != rt);
117 GrMatrix m;
118 m.setAll(
119 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
120 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
121 0, 0, GrMatrix::I()[8]);
122 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000123
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000124 // ES doesn't allow you to pass true to the transpose param,
125 // so do our own transpose
126 GrGLfloat mt[] = {
127 GrScalarToFloat(m[GrMatrix::kMScaleX]),
128 GrScalarToFloat(m[GrMatrix::kMSkewY]),
129 GrScalarToFloat(m[GrMatrix::kMPersp0]),
130 GrScalarToFloat(m[GrMatrix::kMSkewX]),
131 GrScalarToFloat(m[GrMatrix::kMScaleY]),
132 GrScalarToFloat(m[GrMatrix::kMPersp1]),
133 GrScalarToFloat(m[GrMatrix::kMTransX]),
134 GrScalarToFloat(m[GrMatrix::kMTransY]),
135 GrScalarToFloat(m[GrMatrix::kMPersp2])
136 };
137
bsalomon@google.com341767c2012-05-11 20:47:39 +0000138 GrAssert(GrGLProgram::kUnusedUniform !=
139 fProgramData->fUniLocations.fViewMatrixUni);
140 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
141 1, false, mt));
142 fProgramData->fViewMatrix = vm;
bsalomon@google.com91961302011-05-09 18:39:58 +0000143 }
junov@google.comf93e7172011-03-31 21:26:24 +0000144}
145
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000146///////////////////////////////////////////////////////////////////////////////
junov@google.com6acc9b32011-05-16 18:32:07 +0000147
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000148// helpers for texture matrices
junov@google.com6acc9b32011-05-16 18:32:07 +0000149
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000150void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
151 GrSamplerState::SampleMode mode,
152 GrMatrix* matrix) {
153 GrAssert(NULL != texture);
154 GrAssert(NULL != matrix);
155 GrGLTexture::Orientation orientation = texture->orientation();
156 if (GrGLTexture::kBottomUp_Orientation == orientation) {
157 GrMatrix invY;
158 invY.setAll(GR_Scalar1, 0, 0,
159 0, -GR_Scalar1, GR_Scalar1,
160 0, 0, GrMatrix::I()[8]);
161 matrix->postConcat(invY);
162 } else {
163 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
junov@google.com6acc9b32011-05-16 18:32:07 +0000164 }
165}
166
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000167bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
168 const GrSamplerState& sampler) {
169 GrAssert(NULL != texture);
170 if (!sampler.getMatrix().isIdentity()) {
171 return false;
172 }
173 GrGLTexture::Orientation orientation = texture->orientation();
174 if (GrGLTexture::kBottomUp_Orientation == orientation) {
175 return false;
176 } else {
177 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
178 }
179 return true;
180}
181
182///////////////////////////////////////////////////////////////////////////////
183
184void GrGpuGL::flushTextureMatrixAndDomain(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000185 const GrDrawState& drawState = this->getDrawState();
186 const GrGLTexture* texture =
187 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000188 if (NULL != texture) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000189
190 bool orientationChange = fProgramData->fTextureOrientation[s] !=
191 texture->orientation();
192
193 const GrGLint& matrixUni =
194 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
195
bsalomon@google.com341767c2012-05-11 20:47:39 +0000196 const GrMatrix& hwMatrix = fProgramData->fTextureMatrices[s];
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000197 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000198
199 if (GrGLProgram::kUnusedUniform != matrixUni &&
200 (orientationChange || !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000201
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000202 GrMatrix m = samplerMatrix;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000203 GrSamplerState::SampleMode mode =
204 drawState.getSampler(s).getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000205 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000206
bsalomon@google.com91961302011-05-09 18:39:58 +0000207 // ES doesn't allow you to pass true to the transpose param,
208 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000209 GrGLfloat mt[] = {
210 GrScalarToFloat(m[GrMatrix::kMScaleX]),
211 GrScalarToFloat(m[GrMatrix::kMSkewY]),
212 GrScalarToFloat(m[GrMatrix::kMPersp0]),
213 GrScalarToFloat(m[GrMatrix::kMSkewX]),
214 GrScalarToFloat(m[GrMatrix::kMScaleY]),
215 GrScalarToFloat(m[GrMatrix::kMPersp1]),
216 GrScalarToFloat(m[GrMatrix::kMTransX]),
217 GrScalarToFloat(m[GrMatrix::kMTransY]),
218 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000219 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000220
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000221 GL_CALL(UniformMatrix3fv(matrixUni, 1, false, mt));
bsalomon@google.com341767c2012-05-11 20:47:39 +0000222 fProgramData->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000223 }
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000224
225 const GrGLint& domUni =
226 fProgramData->fUniLocations.fStages[s].fTexDomUni;
227 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
228 if (GrGLProgram::kUnusedUniform != domUni &&
229 (orientationChange ||fProgramData->fTextureDomain[s] != texDom)) {
230
231 fProgramData->fTextureDomain[s] = texDom;
232
233 float values[4] = {
234 GrScalarToFloat(texDom.left()),
235 GrScalarToFloat(texDom.top()),
236 GrScalarToFloat(texDom.right()),
237 GrScalarToFloat(texDom.bottom())
238 };
239
240 // vertical flip if necessary
241 if (GrGLTexture::kBottomUp_Orientation == texture->orientation()) {
242 values[1] = 1.0f - values[1];
243 values[3] = 1.0f - values[3];
244 // The top and bottom were just flipped, so correct the ordering
245 // of elements so that values = (l, t, r, b).
246 SkTSwap(values[1], values[3]);
247 }
248 GL_CALL(Uniform4fv(domUni, 1, values));
249 }
250 fProgramData->fTextureOrientation[s] = texture->orientation();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000251 }
junov@google.comf93e7172011-03-31 21:26:24 +0000252}
253
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000254void GrGpuGL::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000255
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000256 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000257 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.com91961302011-05-09 18:39:58 +0000258 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000259 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
260 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
261 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000262
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000263 GrScalar centerX1 = sampler.getRadial2CenterX1();
264 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000265
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000266 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000267
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000268 // when were in the degenerate (linear) case the second
269 // value will be INF but the program doesn't read it. (We
270 // use the same 6 uniforms even though we don't need them
271 // all in the linear case just to keep the code complexity
272 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000273 float values[6] = {
274 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000275 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000276 GrScalarToFloat(centerX1),
277 GrScalarToFloat(radius0),
278 GrScalarToFloat(GrMul(radius0, radius0)),
279 sampler.isRadial2PosRoot() ? 1.f : -1.f
280 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000281 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000282 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
283 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
284 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
285 }
286}
287
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000288void GrGpuGL::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000289 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000290 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000291 const GrGLTexture* texture =
292 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000293 if (texture->width() != fProgramData->fTextureWidth[s] ||
294 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000295
bsalomon@google.com99621082011-11-15 16:47:16 +0000296 float texelSize[] = {1.f / texture->width(),
297 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000298 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000299 fProgramData->fTextureWidth[s] = texture->width();
300 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000301 }
302 }
junov@google.comf93e7172011-03-31 21:26:24 +0000303}
304
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000305void GrGpuGL::flushColorMatrix() {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000306 const ProgramDesc& desc = fCurrentProgram.getDesc();
307 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
308 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
309 if (GrGLProgram::kUnusedUniform != matrixUni
310 && GrGLProgram::kUnusedUniform != vecUni) {
311 const float* m = this->getDrawState().getColorMatrix();
312 GrGLfloat mt[] = {
313 m[0], m[5], m[10], m[15],
314 m[1], m[6], m[11], m[16],
315 m[2], m[7], m[12], m[17],
316 m[3], m[8], m[13], m[18],
317 };
318 static float scale = 1.0f / 255.0f;
319 GrGLfloat vec[] = {
320 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
321 };
322 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
323 GL_CALL(Uniform4fv(vecUni, 1, vec));
324 }
325}
326
Scroggo01b87ec2011-05-11 18:05:38 +0000327static const float ONE_OVER_255 = 1.f / 255.f;
328
329#define GR_COLOR_TO_VEC4(color) {\
330 GrColorUnpackR(color) * ONE_OVER_255,\
331 GrColorUnpackG(color) * ONE_OVER_255,\
332 GrColorUnpackB(color) * ONE_OVER_255,\
333 GrColorUnpackA(color) * ONE_OVER_255 \
334}
335
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000336void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000337 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000338 const GrDrawState& drawState = this->getDrawState();
339
bsalomon@google.come79c8152012-03-29 19:07:12 +0000340 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000341 // color will be specified per-vertex as an attribute
342 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000343 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000344 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000345 switch (desc.fColorInput) {
346 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000347 if (fHWConstAttribColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000348 // OpenGL ES only supports the float varieties of
349 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000350 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000351 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
352 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000353 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000354 }
355 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000356 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000357 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000358 // OpenGL ES doesn't support unsigned byte varieties of
359 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000360 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000361 GrAssert(GrGLProgram::kUnusedUniform !=
362 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000363 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
364 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000365 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000366 }
367 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000368 case ProgramDesc::kSolidWhite_ColorInput:
369 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000370 break;
371 default:
372 GrCrash("Unknown color type.");
373 }
374 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000375 if (fProgramData->fUniLocations.fColorFilterUni
376 != GrGLProgram::kUnusedUniform
377 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000378 != drawState.getColorFilterColor()) {
379 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000380 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000381 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000382 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000383}
384
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000385void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000386 const ProgramDesc& desc = fCurrentProgram.getDesc();
387 const GrDrawState& drawState = this->getDrawState();
388
389
bsalomon@google.come79c8152012-03-29 19:07:12 +0000390 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000391 // coverage will be specified per-vertex as an attribute
392 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000393 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000394 } else {
395 switch (desc.fCoverageInput) {
396 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000397 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000398 // OpenGL ES only supports the float varieties of
399 // glVertexAttrib
400 float c[] = GR_COLOR_TO_VEC4(coverage);
401 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
402 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000403 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000404 }
405 break;
406 case ProgramDesc::kUniform_ColorInput:
407 if (fProgramData->fCoverage != coverage) {
408 // OpenGL ES doesn't support unsigned byte varieties of
409 // glUniform
410 float c[] = GR_COLOR_TO_VEC4(coverage);
411 GrAssert(GrGLProgram::kUnusedUniform !=
412 fProgramData->fUniLocations.fCoverageUni);
413 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
414 1, c));
415 fProgramData->fCoverage = coverage;
416 }
417 break;
418 case ProgramDesc::kSolidWhite_ColorInput:
419 case ProgramDesc::kTransBlack_ColorInput:
420 break;
421 default:
422 GrCrash("Unknown coverage type.");
423 }
424 }
425}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000426
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000427bool GrGpuGL::flushGraphicsState(GrPrimitiveType type) {
junov@google.comf93e7172011-03-31 21:26:24 +0000428 if (!flushGLStateCommon(type)) {
429 return false;
430 }
431
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000432 const GrDrawState& drawState = this->getDrawState();
433
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000434 GrBlendCoeff srcCoeff;
435 GrBlendCoeff dstCoeff;
436 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
437 if (kSkipDraw_BlendOptFlag & blendOpts) {
438 return false;
439 }
440
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000441 GrCustomStage* customStages [GrDrawState::kNumStages];
442 this->buildProgram(type, blendOpts, dstCoeff, customStages);
443 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
444 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000445 if (NULL == fProgramData) {
446 GrAssert(!"Failed to create program!");
447 return false;
448 }
junov@google.comf93e7172011-03-31 21:26:24 +0000449
450 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000451 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000452 fHWProgramID = fProgramData->fProgramID;
453 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000454 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
455 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000456
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000457 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000458 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000459 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
460 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000461 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000462 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
463 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000464 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000465 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000466 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000467 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000468 }
469 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000470 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000471
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000472 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000473
tomhudson@google.com93813632011-10-27 20:21:16 +0000474 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000475 if (this->isStageEnabled(s)) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000476 this->flushTextureMatrixAndDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000477
bsalomon@google.com40d92932011-12-13 18:40:47 +0000478 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000479
bsalomon@google.com40d92932011-12-13 18:40:47 +0000480 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000481
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000482 if (NULL != fProgramData->fCustomStage[s]) {
483 const GrSamplerState& sampler =
484 this->getDrawState().getSampler(s);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000485 const GrGLTexture* texture =
486 static_cast<const GrGLTexture*>(
487 this->getDrawState().getTexture(s));
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000488 fProgramData->fCustomStage[s]->setData(
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000489 this->glInterface(), *texture,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000490 *sampler.getCustomStage(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000491 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000492 }
junov@google.comf93e7172011-03-31 21:26:24 +0000493 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000494 this->flushColorMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000495 return true;
496}
497
bsalomon@google.com2717d562012-05-07 19:10:52 +0000498#if GR_TEXT_SCALAR_IS_USHORT
499 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
500 #define TEXT_COORDS_ARE_NORMALIZED 1
501#elif GR_TEXT_SCALAR_IS_FLOAT
502 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
503 #define TEXT_COORDS_ARE_NORMALIZED 0
504#elif GR_TEXT_SCALAR_IS_FIXED
505 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
506 #define TEXT_COORDS_ARE_NORMALIZED 0
507#else
508 #error "unknown GR_TEXT_SCALAR type"
509#endif
510
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000511void GrGpuGL::setupGeometry(int* startVertex,
junov@google.comf93e7172011-03-31 21:26:24 +0000512 int* startIndex,
513 int vertexCount,
514 int indexCount) {
515
516 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000517 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000518 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000519 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000520
bsalomon@google.come79c8152012-03-29 19:07:12 +0000521 GrVertexLayout currLayout = this->getVertexLayout();
522
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000523 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000524 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000525 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000526 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000527 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000528 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000529 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000530 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000531 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000532 int oldEdgeOffset;
533
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000534 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
535 fHWGeometryState.fVertexLayout,
536 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000537 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000538 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000539 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000540 bool indexed = NULL != startIndex;
541
542 int extraVertexOffset;
543 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000544 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000545
546 GrGLenum scalarType;
547 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000548 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000549 scalarType = TEXT_COORDS_GL_TYPE;
550 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000551 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000552 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
553 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000554 texCoordNorm = false;
555 }
556
557 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
558 *startVertex = 0;
559 if (indexed) {
560 *startIndex += extraIndexOffset;
561 }
562
563 // all the Pointers must be set if any of these are true
564 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
565 vertexOffset != fHWGeometryState.fVertexOffset ||
566 newStride != oldStride;
567
568 // position and tex coord offsets change if above conditions are true
569 // or the type/normalization changed based on text vs nontext type coords.
570 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000571 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000572 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000573 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000574
575 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000576 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000577 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000578 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000579 fHWGeometryState.fVertexOffset = vertexOffset;
580 }
581
tomhudson@google.com93813632011-10-27 20:21:16 +0000582 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000583 if (newTexCoordOffsets[t] > 0) {
584 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000585 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000586 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000587 GL_CALL(EnableVertexAttribArray(idx));
588 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000589 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000590 } else if (posAndTexChange ||
591 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000592 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000593 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000594 }
595 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000596 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000597 }
598 }
599
600 if (newColorOffset > 0) {
601 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000602 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000603 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000604 GL_CALL(EnableVertexAttribArray(idx));
605 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000606 true, newStride, colorOffset));
607 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000608 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000609 true, newStride, colorOffset));
610 }
611 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000612 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000613 }
614
bsalomon@google.coma3108262011-10-10 14:08:47 +0000615 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000616 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000617 int idx = GrGLProgram::CoverageAttributeIdx();
618 if (oldCoverageOffset <= 0) {
619 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000620 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000621 true, newStride, coverageOffset));
622 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000623 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000624 true, newStride, coverageOffset));
625 }
626 } else if (oldCoverageOffset > 0) {
627 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
628 }
629
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000630 if (newEdgeOffset > 0) {
631 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
632 int idx = GrGLProgram::EdgeAttributeIdx();
633 if (oldEdgeOffset <= 0) {
634 GL_CALL(EnableVertexAttribArray(idx));
635 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
636 false, newStride, edgeOffset));
637 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
638 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
639 false, newStride, edgeOffset));
640 }
641 } else if (oldEdgeOffset > 0) {
642 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
643 }
644
bsalomon@google.come79c8152012-03-29 19:07:12 +0000645 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000646 fHWGeometryState.fArrayPtrsDirty = false;
647}
648
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000649namespace {
650
651void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
652 const GrSamplerState& sampler,
653 GrCustomStage** customStages,
654 GrGLProgram* program, int index) {
655 GrCustomStage* customStage = sampler.getCustomStage();
656 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000657 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000658 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000659 customStages[index] = customStage;
660 } else {
661 stage->fCustomStageKey = 0;
662 customStages[index] = NULL;
663 }
664}
665
666}
667
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000668void GrGpuGL::buildProgram(GrPrimitiveType type,
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000669 BlendOptFlags blendOpts,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000670 GrBlendCoeff dstCoeff,
671 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000672 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000673 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000674
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000675 // This should already have been caught
676 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
677
678 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
679
680 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
681 kEmitCoverage_BlendOptFlag));
682
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000683 // The descriptor is used as a cache key. Thus when a field of the
684 // descriptor will not affect program generation (because of the vertex
685 // layout in use or other descriptor field settings) it should be set
686 // to a canonical value to avoid duplicate programs with different keys.
687
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000688 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000689 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000690
691 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
692
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000693 bool requiresAttributeColors =
694 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000695 bool requiresAttributeCoverage =
696 !skipCoverage && SkToBool(desc.fVertexLayout &
697 kCoverage_VertexLayoutBit);
698
699 // fColorInput/fCoverageInput records how colors are specified for the.
700 // program. So we strip the bits from the layout to avoid false negatives
701 // when searching for an existing program in the cache.
702 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000703
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000704 desc.fColorFilterXfermode = skipColor ?
705 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000706 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000707
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000708 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
709
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000710 // no reason to do edge aa or look at per-vertex coverage if coverage is
711 // ignored
712 if (skipCoverage) {
713 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
714 kCoverage_VertexLayoutBit);
715 }
716
717 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
718 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
719 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000720 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000721 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000722 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000723 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000724 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000725 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000726 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000727 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000728 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000729 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000730
731 bool covIsSolidWhite = !requiresAttributeCoverage &&
732 0xffffffff == drawState.getCoverage();
733
734 if (skipCoverage) {
735 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
736 } else if (covIsSolidWhite) {
737 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
738 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
739 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
740 } else {
741 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
742 }
junov@google.comf93e7172011-03-31 21:26:24 +0000743
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000744 int lastEnabledStage = -1;
745
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000746 if (!skipCoverage && (desc.fVertexLayout &
747 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000748 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000749 } else {
750 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000751 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000752 }
753
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000754 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000755 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000756
757 stage.fOptFlags = 0;
758 stage.setEnabled(this->isStageEnabled(s));
759
760 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
761 skipCoverage;
762
763 if (!skip && stage.isEnabled()) {
764 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000765 const GrGLTexture* texture =
766 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000767 GrAssert(NULL != texture);
768 const GrSamplerState& sampler = drawState.getSampler(s);
769 // we matrix to invert when orientation is TopDown, so make sure
770 // we aren't in that case before flagging as identity.
771 if (TextureMatrixIsIdentity(texture, sampler)) {
772 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
773 } else if (!sampler.getMatrix().hasPerspective()) {
774 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
775 }
776 switch (sampler.getSampleMode()) {
777 case GrSamplerState::kNormal_SampleMode:
778 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
779 break;
780 case GrSamplerState::kRadial_SampleMode:
781 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
782 break;
783 case GrSamplerState::kRadial2_SampleMode:
784 if (sampler.radial2IsDegenerate()) {
785 stage.fCoordMapping =
786 StageDesc::kRadial2GradientDegenerate_CoordMapping;
787 } else {
788 stage.fCoordMapping =
789 StageDesc::kRadial2Gradient_CoordMapping;
790 }
791 break;
792 case GrSamplerState::kSweep_SampleMode:
793 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
794 break;
795 default:
796 GrCrash("Unexpected sample mode!");
797 break;
798 }
799
800 switch (sampler.getFilter()) {
801 // these both can use a regular texture2D()
802 case GrSamplerState::kNearest_Filter:
803 case GrSamplerState::kBilinear_Filter:
804 stage.fFetchMode = StageDesc::kSingle_FetchMode;
805 break;
806 // performs 4 texture2D()s
807 case GrSamplerState::k4x4Downsample_Filter:
808 stage.fFetchMode = StageDesc::k2x2_FetchMode;
809 break;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000810 default:
811 GrCrash("Unexpected filter!");
812 break;
813 }
814
815 if (sampler.hasTextureDomain()) {
816 GrAssert(GrSamplerState::kClamp_WrapMode ==
817 sampler.getWrapX() &&
818 GrSamplerState::kClamp_WrapMode ==
819 sampler.getWrapY());
820 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
821 }
822
823 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000824 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000825 if (GrPixelConfigIsAlphaOnly(texture->config())) {
826 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000827 // the shader must smear the single channel after
828 // reading the texture
829 if (this->glCaps().textureRedSupport()) {
830 // we can use R8 textures so use kSmearRed
831 stage.fInConfigFlags |=
832 StageDesc::kSmearRed_InConfigFlag;
833 } else {
834 // we can use A8 textures so use kSmearAlpha
835 stage.fInConfigFlags |=
836 StageDesc::kSmearAlpha_InConfigFlag;
837 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000838 } else if (sampler.swapsRAndB()) {
839 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
840 }
841 }
842 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000843 // The shader generator assumes that color channels are bytes
844 // when rounding.
845 GrAssert(4 == GrBytesPerPixel(texture->config()));
846 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
847 fUnpremulConversion) {
848 stage.fInConfigFlags |=
849 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
850 } else {
851 stage.fInConfigFlags |=
852 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
853 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000854 }
855
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000856 setup_custom_stage(&stage, sampler, customStages,
857 &fCurrentProgram, s);
858
junov@google.comf93e7172011-03-31 21:26:24 +0000859 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000860 stage.fOptFlags = 0;
861 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
862 stage.fInConfigFlags = 0;
863 stage.fFetchMode = (StageDesc::FetchMode) 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000864 stage.fCustomStageKey = 0;
865 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000866 }
867 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000868
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000869 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000870 // The shader generator assumes that color channels are bytes
871 // when rounding.
872 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
873 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
874 desc.fOutputConfig =
875 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
876 } else {
877 desc.fOutputConfig =
878 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
879 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000880 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000881 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000882 }
883
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000884 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000885
886 // currently the experimental GS will only work with triangle prims
887 // (and it doesn't do anything other than pass through values from
888 // the VS to the FS anyway).
889#if 0 && GR_GL_EXPERIMENTAL_GS
890 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
891#endif
892
bsalomon@google.coma3108262011-10-10 14:08:47 +0000893 // we want to avoid generating programs with different "first cov stage"
894 // values when they would compute the same result.
895 // We set field in the desc to kNumStages when either there are no
896 // coverage stages or the distinction between coverage and color is
897 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000898 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +0000899 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000900 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000901 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000902 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000903 }
904
905 // other coverage inputs
906 if (!hasCoverage) {
907 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000908 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +0000909 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
910 }
911
912 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000913 // color filter is applied between color/coverage computation
914 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000915 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000916 }
917
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000918 if (this->getCaps().fDualSourceBlendingSupport &&
919 !(blendOpts & (kEmitCoverage_BlendOptFlag |
920 kCoverageAsAlpha_BlendOptFlag))) {
921 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000922 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000923 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000924 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000925 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000926 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
927 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000928 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000929 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000930 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000931 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
932 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000933 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000934 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000935 }
936 }
937 }
junov@google.comf93e7172011-03-31 21:26:24 +0000938}