blob: 736be931d9bda140e2125598330a37aae507a371 [file] [log] [blame]
junov@google.comf93e7172011-03-31 21:26:24 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
junov@google.comf93e7172011-03-31 21:26:24 +00006 */
7
bsalomon@google.com5739d2c2012-05-31 15:07:19 +00008#include "GrGpuGL.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000010#include "GrCustomStage.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000011#include "GrGLProgramStage.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000012#include "GrGpuVertex.h"
junov@google.comf93e7172011-03-31 21:26:24 +000013
junov@google.comf93e7172011-03-31 21:26:24 +000014#define SKIP_CACHE_CHECK true
15#define GR_UINT32_MAX static_cast<uint32_t>(-1)
16
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000017void GrGpuGL::ProgramCache::Entry::copyAndTakeOwnership(Entry& entry) {
18 fProgramData.copyAndTakeOwnership(entry.fProgramData);
19 fKey = entry.fKey; // ownership transfer
20 fLRUStamp = entry.fLRUStamp;
21}
junov@google.comf93e7172011-03-31 21:26:24 +000022
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000023GrGpuGL::ProgramCache::ProgramCache(const GrGLContextInfo& gl)
24 : fCount(0)
25 , fCurrLRUStamp(0)
26 , fGL(gl) {
27}
junov@google.comf93e7172011-03-31 21:26:24 +000028
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000029GrGpuGL::ProgramCache::~ProgramCache() {
30 for (int i = 0; i < fCount; ++i) {
31 GrGpuGL::DeleteProgram(fGL.interface(),
32 &fEntries[i].fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000033 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000034}
junov@google.comf93e7172011-03-31 21:26:24 +000035
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000036void GrGpuGL::ProgramCache::abandon() {
37 fCount = 0;
38}
39
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000040GrGLProgram::CachedData* GrGpuGL::ProgramCache::getProgramData(
41 const GrGLProgram& desc,
42 GrCustomStage** stages) {
43 Entry newEntry;
44 newEntry.fKey.setKeyData(desc.keyData());
junov@google.comf7c00f62011-08-18 18:15:16 +000045
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000046 Entry* entry = fHashCache.find(newEntry.fKey);
47 if (NULL == entry) {
48 if (!desc.genProgram(fGL, stages, &newEntry.fProgramData)) {
49 return NULL;
50 }
51 if (fCount < kMaxEntries) {
52 entry = fEntries + fCount;
53 ++fCount;
54 } else {
55 GrAssert(kMaxEntries == fCount);
56 entry = fEntries;
57 for (int i = 1; i < kMaxEntries; ++i) {
58 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
59 entry = fEntries + i;
junov@google.comf93e7172011-03-31 21:26:24 +000060 }
junov@google.comf93e7172011-03-31 21:26:24 +000061 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000062 fHashCache.remove(entry->fKey, entry);
63 GrGpuGL::DeleteProgram(fGL.interface(),
64 &entry->fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000065 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000066 entry->copyAndTakeOwnership(newEntry);
67 fHashCache.insert(entry->fKey, entry);
junov@google.comf93e7172011-03-31 21:26:24 +000068 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000069
70 entry->fLRUStamp = fCurrLRUStamp;
71 if (GR_UINT32_MAX == fCurrLRUStamp) {
72 // wrap around! just trash our LRU, one time hit.
73 for (int i = 0; i < fCount; ++i) {
74 fEntries[i].fLRUStamp = 0;
75 }
76 }
77 ++fCurrLRUStamp;
78 return &entry->fProgramData;
79}
junov@google.comf93e7172011-03-31 21:26:24 +000080
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000081void GrGpuGL::DeleteProgram(const GrGLInterface* gl,
bsalomon@google.com49209392012-06-05 15:13:46 +000082 CachedData* programData) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000083 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000084 if (programData->fGShaderID) {
85 GR_GL_CALL(gl, DeleteShader(programData->fGShaderID));
86 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000087 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
88 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
tomhudson@google.com50e4ce02012-06-19 15:27:50 +000089 GR_DEBUGCODE(programData->fVShaderID = 0);
90 GR_DEBUGCODE(programData->fGShaderID = 0);
91 GR_DEBUGCODE(programData->fFShaderID = 0);
92 GR_DEBUGCODE(programData->fProgramID = 0);
junov@google.comf93e7172011-03-31 21:26:24 +000093}
94
bsalomon@google.com1e257a52011-07-06 19:52:16 +000095////////////////////////////////////////////////////////////////////////////////
96
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000097void GrGpuGL::abandonResources(){
98 INHERITED::abandonResources();
99 fProgramCache->abandon();
100 fHWProgramID = 0;
101}
102
103////////////////////////////////////////////////////////////////////////////////
104
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000105#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
106
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000107void GrGpuGL::flushViewMatrix(DrawType type) {
bsalomon@google.com4c883782012-06-04 19:05:11 +0000108 const GrGLRenderTarget* rt = static_cast<const GrGLRenderTarget*>(this->getDrawState().getRenderTarget());
109 SkISize viewportSize;
110 const GrGLIRect& viewport = rt->getViewport();
111 viewportSize.set(viewport.fWidth, viewport.fHeight);
junov@google.comf93e7172011-03-31 21:26:24 +0000112
bsalomon@google.com4c883782012-06-04 19:05:11 +0000113 const GrMatrix& vm = this->getDrawState().getViewMatrix();
114
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000115 if (kStencilPath_DrawType == type) {
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000116 if (fHWPathMatrixState.fViewMatrix != vm ||
117 fHWPathMatrixState.fRTSize != viewportSize) {
118 // rescale the coords from skia's "device" coords to GL's normalized coords,
119 // and perform a y-flip.
120 GrMatrix m;
121 m.setScale(GrIntToScalar(2) / rt->width(), GrIntToScalar(-2) / rt->height());
robertphillips@google.com59f46b82012-07-10 17:30:58 +0000122 m.postTranslate(-GR_Scalar1, GR_Scalar1);
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000123 m.preConcat(vm);
124
125 // GL wants a column-major 4x4.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000126 GrGLfloat mv[] = {
127 // col 0
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000128 GrScalarToFloat(m[GrMatrix::kMScaleX]),
129 GrScalarToFloat(m[GrMatrix::kMSkewY]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000130 0,
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000131 GrScalarToFloat(m[GrMatrix::kMPersp0]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000132
133 // col 1
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000134 GrScalarToFloat(m[GrMatrix::kMSkewX]),
135 GrScalarToFloat(m[GrMatrix::kMScaleY]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000136 0,
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000137 GrScalarToFloat(m[GrMatrix::kMPersp1]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000138
139 // col 2
140 0, 0, 0, 0,
141
142 // col3
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000143 GrScalarToFloat(m[GrMatrix::kMTransX]),
144 GrScalarToFloat(m[GrMatrix::kMTransY]),
145 0.0f,
146 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000147 };
148 GL_CALL(MatrixMode(GR_GL_PROJECTION));
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000149 GL_CALL(LoadMatrixf(mv));
150 fHWPathMatrixState.fViewMatrix = vm;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000151 fHWPathMatrixState.fRTSize = viewportSize;
152 }
153 } else if (!fProgramData->fViewMatrix.cheapEqualTo(vm) ||
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000154 fProgramData->fViewportSize != viewportSize) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000155 GrMatrix m;
156 m.setAll(
bsalomon@google.com4c883782012-06-04 19:05:11 +0000157 GrIntToScalar(2) / viewportSize.fWidth, 0, -GR_Scalar1,
158 0,-GrIntToScalar(2) / viewportSize.fHeight, GR_Scalar1,
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000159 0, 0, GrMatrix::I()[8]);
160 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000161
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000162 // ES doesn't allow you to pass true to the transpose param,
163 // so do our own transpose
164 GrGLfloat mt[] = {
165 GrScalarToFloat(m[GrMatrix::kMScaleX]),
166 GrScalarToFloat(m[GrMatrix::kMSkewY]),
167 GrScalarToFloat(m[GrMatrix::kMPersp0]),
168 GrScalarToFloat(m[GrMatrix::kMSkewX]),
169 GrScalarToFloat(m[GrMatrix::kMScaleY]),
170 GrScalarToFloat(m[GrMatrix::kMPersp1]),
171 GrScalarToFloat(m[GrMatrix::kMTransX]),
172 GrScalarToFloat(m[GrMatrix::kMTransY]),
173 GrScalarToFloat(m[GrMatrix::kMPersp2])
174 };
175
bsalomon@google.com341767c2012-05-11 20:47:39 +0000176 GrAssert(GrGLProgram::kUnusedUniform !=
177 fProgramData->fUniLocations.fViewMatrixUni);
178 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
179 1, false, mt));
180 fProgramData->fViewMatrix = vm;
bsalomon@google.com4c883782012-06-04 19:05:11 +0000181 fProgramData->fViewportSize = viewportSize;
bsalomon@google.com91961302011-05-09 18:39:58 +0000182 }
junov@google.comf93e7172011-03-31 21:26:24 +0000183}
184
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000185///////////////////////////////////////////////////////////////////////////////
junov@google.com6acc9b32011-05-16 18:32:07 +0000186
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000187// helpers for texture matrices
junov@google.com6acc9b32011-05-16 18:32:07 +0000188
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000189void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000190 GrMatrix* matrix) {
191 GrAssert(NULL != texture);
192 GrAssert(NULL != matrix);
193 GrGLTexture::Orientation orientation = texture->orientation();
194 if (GrGLTexture::kBottomUp_Orientation == orientation) {
195 GrMatrix invY;
196 invY.setAll(GR_Scalar1, 0, 0,
197 0, -GR_Scalar1, GR_Scalar1,
198 0, 0, GrMatrix::I()[8]);
199 matrix->postConcat(invY);
200 } else {
201 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
junov@google.com6acc9b32011-05-16 18:32:07 +0000202 }
203}
204
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000205bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
206 const GrSamplerState& sampler) {
207 GrAssert(NULL != texture);
208 if (!sampler.getMatrix().isIdentity()) {
209 return false;
210 }
211 GrGLTexture::Orientation orientation = texture->orientation();
212 if (GrGLTexture::kBottomUp_Orientation == orientation) {
213 return false;
214 } else {
215 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
216 }
217 return true;
218}
219
220///////////////////////////////////////////////////////////////////////////////
221
222void GrGpuGL::flushTextureMatrixAndDomain(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000223 const GrDrawState& drawState = this->getDrawState();
224 const GrGLTexture* texture =
225 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000226 if (NULL != texture) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000227
228 bool orientationChange = fProgramData->fTextureOrientation[s] !=
229 texture->orientation();
230
231 const GrGLint& matrixUni =
232 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
233
bsalomon@google.com341767c2012-05-11 20:47:39 +0000234 const GrMatrix& hwMatrix = fProgramData->fTextureMatrices[s];
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000235 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000236
237 if (GrGLProgram::kUnusedUniform != matrixUni &&
238 (orientationChange || !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000239
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000240 GrMatrix m = samplerMatrix;
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000241 AdjustTextureMatrix(texture, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000242
bsalomon@google.com91961302011-05-09 18:39:58 +0000243 // ES doesn't allow you to pass true to the transpose param,
244 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000245 GrGLfloat mt[] = {
246 GrScalarToFloat(m[GrMatrix::kMScaleX]),
247 GrScalarToFloat(m[GrMatrix::kMSkewY]),
248 GrScalarToFloat(m[GrMatrix::kMPersp0]),
249 GrScalarToFloat(m[GrMatrix::kMSkewX]),
250 GrScalarToFloat(m[GrMatrix::kMScaleY]),
251 GrScalarToFloat(m[GrMatrix::kMPersp1]),
252 GrScalarToFloat(m[GrMatrix::kMTransX]),
253 GrScalarToFloat(m[GrMatrix::kMTransY]),
254 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000255 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000256
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000257 GL_CALL(UniformMatrix3fv(matrixUni, 1, false, mt));
bsalomon@google.com341767c2012-05-11 20:47:39 +0000258 fProgramData->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000259 }
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000260
261 const GrGLint& domUni =
262 fProgramData->fUniLocations.fStages[s].fTexDomUni;
263 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
264 if (GrGLProgram::kUnusedUniform != domUni &&
265 (orientationChange ||fProgramData->fTextureDomain[s] != texDom)) {
266
267 fProgramData->fTextureDomain[s] = texDom;
268
269 float values[4] = {
270 GrScalarToFloat(texDom.left()),
271 GrScalarToFloat(texDom.top()),
272 GrScalarToFloat(texDom.right()),
273 GrScalarToFloat(texDom.bottom())
274 };
275
276 // vertical flip if necessary
277 if (GrGLTexture::kBottomUp_Orientation == texture->orientation()) {
278 values[1] = 1.0f - values[1];
279 values[3] = 1.0f - values[3];
280 // The top and bottom were just flipped, so correct the ordering
281 // of elements so that values = (l, t, r, b).
282 SkTSwap(values[1], values[3]);
283 }
284 GL_CALL(Uniform4fv(domUni, 1, values));
285 }
286 fProgramData->fTextureOrientation[s] = texture->orientation();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000287 }
junov@google.comf93e7172011-03-31 21:26:24 +0000288}
289
junov@google.comf93e7172011-03-31 21:26:24 +0000290
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000291void GrGpuGL::flushColorMatrix() {
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000292 // const ProgramDesc& desc = fCurrentProgram.getDesc();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000293 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
294 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
295 if (GrGLProgram::kUnusedUniform != matrixUni
296 && GrGLProgram::kUnusedUniform != vecUni) {
297 const float* m = this->getDrawState().getColorMatrix();
298 GrGLfloat mt[] = {
299 m[0], m[5], m[10], m[15],
300 m[1], m[6], m[11], m[16],
301 m[2], m[7], m[12], m[17],
302 m[3], m[8], m[13], m[18],
303 };
304 static float scale = 1.0f / 255.0f;
305 GrGLfloat vec[] = {
306 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
307 };
308 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
309 GL_CALL(Uniform4fv(vecUni, 1, vec));
310 }
311}
312
Scroggo01b87ec2011-05-11 18:05:38 +0000313static const float ONE_OVER_255 = 1.f / 255.f;
314
315#define GR_COLOR_TO_VEC4(color) {\
316 GrColorUnpackR(color) * ONE_OVER_255,\
317 GrColorUnpackG(color) * ONE_OVER_255,\
318 GrColorUnpackB(color) * ONE_OVER_255,\
319 GrColorUnpackA(color) * ONE_OVER_255 \
320}
321
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000322void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000323 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000324 const GrDrawState& drawState = this->getDrawState();
325
bsalomon@google.come79c8152012-03-29 19:07:12 +0000326 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000327 // color will be specified per-vertex as an attribute
328 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000329 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000330 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000331 switch (desc.fColorInput) {
332 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000333 if (fHWConstAttribColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000334 // OpenGL ES only supports the float varieties of
335 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000336 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000337 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
338 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000339 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000340 }
341 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000342 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000343 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000344 // OpenGL ES doesn't support unsigned byte varieties of
345 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000346 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000347 GrAssert(GrGLProgram::kUnusedUniform !=
348 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000349 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
350 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000351 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000352 }
353 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000354 case ProgramDesc::kSolidWhite_ColorInput:
355 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000356 break;
357 default:
358 GrCrash("Unknown color type.");
359 }
360 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000361 if (fProgramData->fUniLocations.fColorFilterUni
362 != GrGLProgram::kUnusedUniform
363 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000364 != drawState.getColorFilterColor()) {
365 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000366 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000367 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000368 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000369}
370
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000371void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000372 const ProgramDesc& desc = fCurrentProgram.getDesc();
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000373 // const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000374
375
bsalomon@google.come79c8152012-03-29 19:07:12 +0000376 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000377 // coverage will be specified per-vertex as an attribute
378 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000379 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000380 } else {
381 switch (desc.fCoverageInput) {
382 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000383 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000384 // OpenGL ES only supports the float varieties of
385 // glVertexAttrib
386 float c[] = GR_COLOR_TO_VEC4(coverage);
387 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
388 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000389 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000390 }
391 break;
392 case ProgramDesc::kUniform_ColorInput:
393 if (fProgramData->fCoverage != coverage) {
394 // OpenGL ES doesn't support unsigned byte varieties of
395 // glUniform
396 float c[] = GR_COLOR_TO_VEC4(coverage);
397 GrAssert(GrGLProgram::kUnusedUniform !=
398 fProgramData->fUniLocations.fCoverageUni);
399 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
400 1, c));
401 fProgramData->fCoverage = coverage;
402 }
403 break;
404 case ProgramDesc::kSolidWhite_ColorInput:
405 case ProgramDesc::kTransBlack_ColorInput:
406 break;
407 default:
408 GrCrash("Unknown coverage type.");
409 }
410 }
411}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000412
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000413bool GrGpuGL::flushGraphicsState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000414 const GrDrawState& drawState = this->getDrawState();
415
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000416 // GrGpu::setupClipAndFlushState should have already checked this
417 // and bailed if not true.
418 GrAssert(NULL != drawState.getRenderTarget());
419
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000420 if (kStencilPath_DrawType != type) {
421 this->flushMiscFixedFunctionState();
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000422
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000423 GrBlendCoeff srcCoeff;
424 GrBlendCoeff dstCoeff;
425 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
426 if (kSkipDraw_BlendOptFlag & blendOpts) {
427 return false;
428 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000429
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000430 GrCustomStage* customStages [GrDrawState::kNumStages];
431 this->buildProgram(kDrawPoints_DrawType == type,
432 blendOpts, dstCoeff, customStages);
433 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
434 customStages);
435 if (NULL == fProgramData) {
436 GrAssert(!"Failed to create program!");
437 return false;
438 }
junov@google.comf93e7172011-03-31 21:26:24 +0000439
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000440 if (fHWProgramID != fProgramData->fProgramID) {
441 GL_CALL(UseProgram(fProgramData->fProgramID));
442 fHWProgramID = fProgramData->fProgramID;
443 }
444 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
445 this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000446
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000447 GrColor color;
448 GrColor coverage;
449 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
450 color = 0;
451 coverage = 0;
452 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
453 color = 0xffffffff;
454 coverage = drawState.getCoverage();
455 } else {
456 color = drawState.getColor();
457 coverage = drawState.getCoverage();
458 }
459 this->flushColor(color);
460 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000461
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000462 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
463 if (this->isStageEnabled(s)) {
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000464#if GR_DEBUG
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000465 // check for circular rendering
466 GrAssert(NULL == drawState.getRenderTarget() ||
467 NULL == drawState.getTexture(s) ||
468 drawState.getTexture(s)->asRenderTarget() !=
469 drawState.getRenderTarget());
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000470#endif
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000471 this->flushBoundTextureAndParams(s);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000472
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000473 this->flushTextureMatrixAndDomain(s);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000474
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000475 if (NULL != fProgramData->fCustomStage[s]) {
476 const GrSamplerState& sampler =
477 this->getDrawState().getSampler(s);
478 const GrGLTexture* texture =
479 static_cast<const GrGLTexture*>(
480 this->getDrawState().getTexture(s));
481 fProgramData->fCustomStage[s]->setData(
482 this->glInterface(), *texture,
483 *sampler.getCustomStage(), s);
484 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000485 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000486 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000487 this->flushColorMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000488 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000489 this->flushStencil(type);
490 this->flushViewMatrix(type);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000491 this->flushScissor();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000492 this->flushAAState(type);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000493
494 GrIRect* rect = NULL;
495 GrIRect clipBounds;
496 if (drawState.isClipState() &&
497 fClip.hasConservativeBounds()) {
498 fClip.getConservativeBounds().roundOut(&clipBounds);
499 rect = &clipBounds;
500 }
501 // This must come after textures are flushed because a texture may need
502 // to be msaa-resolved (which will modify bound FBO state).
503 this->flushRenderTarget(rect);
504
junov@google.comf93e7172011-03-31 21:26:24 +0000505 return true;
506}
507
bsalomon@google.com2717d562012-05-07 19:10:52 +0000508#if GR_TEXT_SCALAR_IS_USHORT
509 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
510 #define TEXT_COORDS_ARE_NORMALIZED 1
511#elif GR_TEXT_SCALAR_IS_FLOAT
512 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
513 #define TEXT_COORDS_ARE_NORMALIZED 0
514#elif GR_TEXT_SCALAR_IS_FIXED
515 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
516 #define TEXT_COORDS_ARE_NORMALIZED 0
517#else
518 #error "unknown GR_TEXT_SCALAR type"
519#endif
520
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000521void GrGpuGL::setupGeometry(int* startVertex,
bsalomon@google.com49209392012-06-05 15:13:46 +0000522 int* startIndex,
523 int vertexCount,
524 int indexCount) {
junov@google.comf93e7172011-03-31 21:26:24 +0000525
526 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000527 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000528 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000529 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000530
bsalomon@google.come79c8152012-03-29 19:07:12 +0000531 GrVertexLayout currLayout = this->getVertexLayout();
532
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000533 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000534 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000535 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000536 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000537 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000538 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000539 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000540 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000541 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000542 int oldEdgeOffset;
543
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000544 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
545 fHWGeometryState.fVertexLayout,
546 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000547 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000548 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000549 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000550 bool indexed = NULL != startIndex;
551
552 int extraVertexOffset;
553 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000554 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000555
556 GrGLenum scalarType;
557 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000558 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000559 scalarType = TEXT_COORDS_GL_TYPE;
560 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000561 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000562 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
563 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000564 texCoordNorm = false;
565 }
566
567 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
568 *startVertex = 0;
569 if (indexed) {
570 *startIndex += extraIndexOffset;
571 }
572
573 // all the Pointers must be set if any of these are true
574 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
575 vertexOffset != fHWGeometryState.fVertexOffset ||
576 newStride != oldStride;
577
578 // position and tex coord offsets change if above conditions are true
579 // or the type/normalization changed based on text vs nontext type coords.
580 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000581 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000582 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000583 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000584
585 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000586 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000587 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000588 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000589 fHWGeometryState.fVertexOffset = vertexOffset;
590 }
591
tomhudson@google.com93813632011-10-27 20:21:16 +0000592 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000593 if (newTexCoordOffsets[t] > 0) {
594 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000595 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000596 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000597 GL_CALL(EnableVertexAttribArray(idx));
598 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000599 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000600 } else if (posAndTexChange ||
601 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000602 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000603 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000604 }
605 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000606 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000607 }
608 }
609
610 if (newColorOffset > 0) {
611 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000612 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000613 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000614 GL_CALL(EnableVertexAttribArray(idx));
615 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000616 true, newStride, colorOffset));
617 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000618 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000619 true, newStride, colorOffset));
620 }
621 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000622 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000623 }
624
bsalomon@google.coma3108262011-10-10 14:08:47 +0000625 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000626 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000627 int idx = GrGLProgram::CoverageAttributeIdx();
628 if (oldCoverageOffset <= 0) {
629 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000630 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000631 true, newStride, coverageOffset));
632 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000633 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000634 true, newStride, coverageOffset));
635 }
636 } else if (oldCoverageOffset > 0) {
637 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
638 }
639
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000640 if (newEdgeOffset > 0) {
641 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
642 int idx = GrGLProgram::EdgeAttributeIdx();
643 if (oldEdgeOffset <= 0) {
644 GL_CALL(EnableVertexAttribArray(idx));
645 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
646 false, newStride, edgeOffset));
647 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
648 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
649 false, newStride, edgeOffset));
650 }
651 } else if (oldEdgeOffset > 0) {
652 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
653 }
654
bsalomon@google.come79c8152012-03-29 19:07:12 +0000655 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000656 fHWGeometryState.fArrayPtrsDirty = false;
657}
658
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000659namespace {
660
661void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
662 const GrSamplerState& sampler,
663 GrCustomStage** customStages,
664 GrGLProgram* program, int index) {
665 GrCustomStage* customStage = sampler.getCustomStage();
666 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000667 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000668 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000669 customStages[index] = customStage;
670 } else {
671 stage->fCustomStageKey = 0;
672 customStages[index] = NULL;
673 }
674}
675
676}
677
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000678void GrGpuGL::buildProgram(bool isPoints,
bsalomon@google.com49209392012-06-05 15:13:46 +0000679 BlendOptFlags blendOpts,
680 GrBlendCoeff dstCoeff,
681 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000682 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000683 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000684
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000685 // This should already have been caught
686 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
687
688 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
689
690 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
691 kEmitCoverage_BlendOptFlag));
692
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000693 // The descriptor is used as a cache key. Thus when a field of the
694 // descriptor will not affect program generation (because of the vertex
695 // layout in use or other descriptor field settings) it should be set
696 // to a canonical value to avoid duplicate programs with different keys.
697
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000698 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000699 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000700
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000701 desc.fEmitsPointSize = isPoints;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000702
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000703 bool requiresAttributeColors =
704 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000705 bool requiresAttributeCoverage =
706 !skipCoverage && SkToBool(desc.fVertexLayout &
707 kCoverage_VertexLayoutBit);
708
709 // fColorInput/fCoverageInput records how colors are specified for the.
710 // program. So we strip the bits from the layout to avoid false negatives
711 // when searching for an existing program in the cache.
712 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000713
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000714 desc.fColorFilterXfermode = skipColor ?
715 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000716 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000717
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000718 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
719
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000720 // no reason to do edge aa or look at per-vertex coverage if coverage is
721 // ignored
722 if (skipCoverage) {
723 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
724 kCoverage_VertexLayoutBit);
725 }
726
727 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
728 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
729 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000730 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000731 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000732 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000733 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000734 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000735 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000736 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000737 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000738 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000739 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000740
741 bool covIsSolidWhite = !requiresAttributeCoverage &&
742 0xffffffff == drawState.getCoverage();
743
744 if (skipCoverage) {
745 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
746 } else if (covIsSolidWhite) {
747 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
748 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
749 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
750 } else {
751 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
752 }
junov@google.comf93e7172011-03-31 21:26:24 +0000753
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000754 int lastEnabledStage = -1;
755
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000756 if (!skipCoverage && (desc.fVertexLayout &
757 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000758 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000759 } else {
760 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000761 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000762 }
763
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000764 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000765 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000766
767 stage.fOptFlags = 0;
768 stage.setEnabled(this->isStageEnabled(s));
769
770 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
771 skipCoverage;
772
773 if (!skip && stage.isEnabled()) {
774 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000775 const GrGLTexture* texture =
776 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000777 GrAssert(NULL != texture);
778 const GrSamplerState& sampler = drawState.getSampler(s);
779 // we matrix to invert when orientation is TopDown, so make sure
780 // we aren't in that case before flagging as identity.
781 if (TextureMatrixIsIdentity(texture, sampler)) {
782 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
783 } else if (!sampler.getMatrix().hasPerspective()) {
784 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
785 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000786
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000787 if (sampler.hasTextureDomain()) {
788 GrAssert(GrSamplerState::kClamp_WrapMode ==
789 sampler.getWrapX() &&
790 GrSamplerState::kClamp_WrapMode ==
791 sampler.getWrapY());
792 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
793 }
794
795 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000796 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000797 if (GrPixelConfigIsAlphaOnly(texture->config())) {
798 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000799 // the shader must smear the single channel after
800 // reading the texture
801 if (this->glCaps().textureRedSupport()) {
802 // we can use R8 textures so use kSmearRed
803 stage.fInConfigFlags |=
804 StageDesc::kSmearRed_InConfigFlag;
805 } else {
806 // we can use A8 textures so use kSmearAlpha
807 stage.fInConfigFlags |=
808 StageDesc::kSmearAlpha_InConfigFlag;
809 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000810 } else if (sampler.swapsRAndB()) {
811 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
812 }
813 }
814 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000815 // The shader generator assumes that color channels are bytes
816 // when rounding.
817 GrAssert(4 == GrBytesPerPixel(texture->config()));
818 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
819 fUnpremulConversion) {
820 stage.fInConfigFlags |=
821 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
822 } else {
823 stage.fInConfigFlags |=
824 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
825 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000826 }
827
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000828 setup_custom_stage(&stage, sampler, customStages,
829 &fCurrentProgram, s);
830
junov@google.comf93e7172011-03-31 21:26:24 +0000831 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000832 stage.fOptFlags = 0;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000833 stage.fInConfigFlags = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000834 stage.fCustomStageKey = 0;
835 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000836 }
837 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000838
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000839 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000840 // The shader generator assumes that color channels are bytes
841 // when rounding.
842 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
843 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
844 desc.fOutputConfig =
845 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
846 } else {
847 desc.fOutputConfig =
848 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
849 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000850 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000851 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000852 }
853
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000854 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000855
856 // currently the experimental GS will only work with triangle prims
857 // (and it doesn't do anything other than pass through values from
858 // the VS to the FS anyway).
859#if 0 && GR_GL_EXPERIMENTAL_GS
860 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
861#endif
862
bsalomon@google.coma3108262011-10-10 14:08:47 +0000863 // we want to avoid generating programs with different "first cov stage"
864 // values when they would compute the same result.
865 // We set field in the desc to kNumStages when either there are no
866 // coverage stages or the distinction between coverage and color is
867 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000868 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +0000869 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000870 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000871 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000872 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000873 }
874
875 // other coverage inputs
876 if (!hasCoverage) {
877 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000878 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +0000879 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
880 }
881
882 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000883 // color filter is applied between color/coverage computation
884 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000885 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000886 }
887
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000888 if (this->getCaps().fDualSourceBlendingSupport &&
889 !(blendOpts & (kEmitCoverage_BlendOptFlag |
890 kCoverageAsAlpha_BlendOptFlag))) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000891 if (kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000892 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000893 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000894 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000895 } else if (kSA_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000896 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
897 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000898 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000899 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000900 } else if (kSC_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000901 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
902 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000903 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000904 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000905 }
906 }
907 }
junov@google.comf93e7172011-03-31 21:26:24 +0000908}