blob: e33f469f9350232f0f42d366c46bef58b6ce9c93 [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) {
116 if (fHWPathMatrixState.fViewMatrix != vm) {
117 // We use the GL model view matrix to hold the draw state's view
118 // matrix and the GL projection matrix to convert to normalized y-up
119 // coords.
120 GrGLfloat mv[] = {
121 // col 0
122 GrScalarToFloat(vm[GrMatrix::kMScaleX]),
123 GrScalarToFloat(vm[GrMatrix::kMSkewY]),
124 0,
125 GrScalarToFloat(vm[GrMatrix::kMPersp0]),
126
127 // col 1
128 GrScalarToFloat(vm[GrMatrix::kMSkewX]),
129 GrScalarToFloat(vm[GrMatrix::kMScaleY]),
130 0,
131 GrScalarToFloat(vm[GrMatrix::kMPersp1]),
132
133 // col 2
134 0, 0, 0, 0,
135
136 // col3
137 GrScalarToFloat(vm[GrMatrix::kMTransX]),
138 GrScalarToFloat(vm[GrMatrix::kMTransY]),
139 0.5f,
140 GrScalarToFloat(vm[GrMatrix::kMPersp2])
141 };
142 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
143 GL_CALL(LoadMatrixf(mv));
144 fHWPathMatrixState.fViewMatrix = vm;
145 }
146 if (fHWPathMatrixState.fRTSize != viewportSize) {
147 GrGLfloat p[] = {
148 // col 0
149 2.f / rt->width(), 0, 0, 0,
150
151 // col 1
152 0, -2.f / rt->height(), 0, 0,
153
154 // col 2
155 0, 0, 1.f, 0,
156
157 // col 3
158 -1.f, 1.f, 0, 1.f,
159 };
160 GL_CALL(MatrixMode(GR_GL_PROJECTION));
161 GL_CALL(LoadMatrixf(p));
162 fHWPathMatrixState.fRTSize = viewportSize;
163 }
164 } else if (!fProgramData->fViewMatrix.cheapEqualTo(vm) ||
bsalomon@google.com4c883782012-06-04 19:05:11 +0000165 fProgramData->fViewportSize != viewportSize) {
166
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000167 GrMatrix m;
168 m.setAll(
bsalomon@google.com4c883782012-06-04 19:05:11 +0000169 GrIntToScalar(2) / viewportSize.fWidth, 0, -GR_Scalar1,
170 0,-GrIntToScalar(2) / viewportSize.fHeight, GR_Scalar1,
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000171 0, 0, GrMatrix::I()[8]);
172 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000173
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000174 // ES doesn't allow you to pass true to the transpose param,
175 // so do our own transpose
176 GrGLfloat mt[] = {
177 GrScalarToFloat(m[GrMatrix::kMScaleX]),
178 GrScalarToFloat(m[GrMatrix::kMSkewY]),
179 GrScalarToFloat(m[GrMatrix::kMPersp0]),
180 GrScalarToFloat(m[GrMatrix::kMSkewX]),
181 GrScalarToFloat(m[GrMatrix::kMScaleY]),
182 GrScalarToFloat(m[GrMatrix::kMPersp1]),
183 GrScalarToFloat(m[GrMatrix::kMTransX]),
184 GrScalarToFloat(m[GrMatrix::kMTransY]),
185 GrScalarToFloat(m[GrMatrix::kMPersp2])
186 };
187
bsalomon@google.com341767c2012-05-11 20:47:39 +0000188 GrAssert(GrGLProgram::kUnusedUniform !=
189 fProgramData->fUniLocations.fViewMatrixUni);
190 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
191 1, false, mt));
192 fProgramData->fViewMatrix = vm;
bsalomon@google.com4c883782012-06-04 19:05:11 +0000193 fProgramData->fViewportSize = viewportSize;
bsalomon@google.com91961302011-05-09 18:39:58 +0000194 }
junov@google.comf93e7172011-03-31 21:26:24 +0000195}
196
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000197///////////////////////////////////////////////////////////////////////////////
junov@google.com6acc9b32011-05-16 18:32:07 +0000198
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000199// helpers for texture matrices
junov@google.com6acc9b32011-05-16 18:32:07 +0000200
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000201void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000202 GrMatrix* matrix) {
203 GrAssert(NULL != texture);
204 GrAssert(NULL != matrix);
205 GrGLTexture::Orientation orientation = texture->orientation();
206 if (GrGLTexture::kBottomUp_Orientation == orientation) {
207 GrMatrix invY;
208 invY.setAll(GR_Scalar1, 0, 0,
209 0, -GR_Scalar1, GR_Scalar1,
210 0, 0, GrMatrix::I()[8]);
211 matrix->postConcat(invY);
212 } else {
213 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
junov@google.com6acc9b32011-05-16 18:32:07 +0000214 }
215}
216
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000217bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
218 const GrSamplerState& sampler) {
219 GrAssert(NULL != texture);
220 if (!sampler.getMatrix().isIdentity()) {
221 return false;
222 }
223 GrGLTexture::Orientation orientation = texture->orientation();
224 if (GrGLTexture::kBottomUp_Orientation == orientation) {
225 return false;
226 } else {
227 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
228 }
229 return true;
230}
231
232///////////////////////////////////////////////////////////////////////////////
233
234void GrGpuGL::flushTextureMatrixAndDomain(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000235 const GrDrawState& drawState = this->getDrawState();
236 const GrGLTexture* texture =
237 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000238 if (NULL != texture) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000239
240 bool orientationChange = fProgramData->fTextureOrientation[s] !=
241 texture->orientation();
242
243 const GrGLint& matrixUni =
244 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
245
bsalomon@google.com341767c2012-05-11 20:47:39 +0000246 const GrMatrix& hwMatrix = fProgramData->fTextureMatrices[s];
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000247 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000248
249 if (GrGLProgram::kUnusedUniform != matrixUni &&
250 (orientationChange || !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000251
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000252 GrMatrix m = samplerMatrix;
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000253 AdjustTextureMatrix(texture, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000254
bsalomon@google.com91961302011-05-09 18:39:58 +0000255 // ES doesn't allow you to pass true to the transpose param,
256 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000257 GrGLfloat mt[] = {
258 GrScalarToFloat(m[GrMatrix::kMScaleX]),
259 GrScalarToFloat(m[GrMatrix::kMSkewY]),
260 GrScalarToFloat(m[GrMatrix::kMPersp0]),
261 GrScalarToFloat(m[GrMatrix::kMSkewX]),
262 GrScalarToFloat(m[GrMatrix::kMScaleY]),
263 GrScalarToFloat(m[GrMatrix::kMPersp1]),
264 GrScalarToFloat(m[GrMatrix::kMTransX]),
265 GrScalarToFloat(m[GrMatrix::kMTransY]),
266 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000267 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000268
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000269 GL_CALL(UniformMatrix3fv(matrixUni, 1, false, mt));
bsalomon@google.com341767c2012-05-11 20:47:39 +0000270 fProgramData->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000271 }
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000272
273 const GrGLint& domUni =
274 fProgramData->fUniLocations.fStages[s].fTexDomUni;
275 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
276 if (GrGLProgram::kUnusedUniform != domUni &&
277 (orientationChange ||fProgramData->fTextureDomain[s] != texDom)) {
278
279 fProgramData->fTextureDomain[s] = texDom;
280
281 float values[4] = {
282 GrScalarToFloat(texDom.left()),
283 GrScalarToFloat(texDom.top()),
284 GrScalarToFloat(texDom.right()),
285 GrScalarToFloat(texDom.bottom())
286 };
287
288 // vertical flip if necessary
289 if (GrGLTexture::kBottomUp_Orientation == texture->orientation()) {
290 values[1] = 1.0f - values[1];
291 values[3] = 1.0f - values[3];
292 // The top and bottom were just flipped, so correct the ordering
293 // of elements so that values = (l, t, r, b).
294 SkTSwap(values[1], values[3]);
295 }
296 GL_CALL(Uniform4fv(domUni, 1, values));
297 }
298 fProgramData->fTextureOrientation[s] = texture->orientation();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000299 }
junov@google.comf93e7172011-03-31 21:26:24 +0000300}
301
junov@google.comf93e7172011-03-31 21:26:24 +0000302
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000303void GrGpuGL::flushColorMatrix() {
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000304 // const ProgramDesc& desc = fCurrentProgram.getDesc();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000305 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
306 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
307 if (GrGLProgram::kUnusedUniform != matrixUni
308 && GrGLProgram::kUnusedUniform != vecUni) {
309 const float* m = this->getDrawState().getColorMatrix();
310 GrGLfloat mt[] = {
311 m[0], m[5], m[10], m[15],
312 m[1], m[6], m[11], m[16],
313 m[2], m[7], m[12], m[17],
314 m[3], m[8], m[13], m[18],
315 };
316 static float scale = 1.0f / 255.0f;
317 GrGLfloat vec[] = {
318 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
319 };
320 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
321 GL_CALL(Uniform4fv(vecUni, 1, vec));
322 }
323}
324
Scroggo01b87ec2011-05-11 18:05:38 +0000325static const float ONE_OVER_255 = 1.f / 255.f;
326
327#define GR_COLOR_TO_VEC4(color) {\
328 GrColorUnpackR(color) * ONE_OVER_255,\
329 GrColorUnpackG(color) * ONE_OVER_255,\
330 GrColorUnpackB(color) * ONE_OVER_255,\
331 GrColorUnpackA(color) * ONE_OVER_255 \
332}
333
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000334void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000335 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000336 const GrDrawState& drawState = this->getDrawState();
337
bsalomon@google.come79c8152012-03-29 19:07:12 +0000338 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000339 // color will be specified per-vertex as an attribute
340 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000341 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000342 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000343 switch (desc.fColorInput) {
344 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000345 if (fHWConstAttribColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000346 // OpenGL ES only supports the float varieties of
347 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000348 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000349 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
350 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000351 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000352 }
353 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000354 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000355 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000356 // OpenGL ES doesn't support unsigned byte varieties of
357 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000358 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000359 GrAssert(GrGLProgram::kUnusedUniform !=
360 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000361 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
362 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000363 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000364 }
365 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000366 case ProgramDesc::kSolidWhite_ColorInput:
367 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000368 break;
369 default:
370 GrCrash("Unknown color type.");
371 }
372 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000373 if (fProgramData->fUniLocations.fColorFilterUni
374 != GrGLProgram::kUnusedUniform
375 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000376 != drawState.getColorFilterColor()) {
377 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000378 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000379 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000380 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000381}
382
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000383void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000384 const ProgramDesc& desc = fCurrentProgram.getDesc();
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000385 // const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000386
387
bsalomon@google.come79c8152012-03-29 19:07:12 +0000388 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000389 // coverage will be specified per-vertex as an attribute
390 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000391 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000392 } else {
393 switch (desc.fCoverageInput) {
394 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000395 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000396 // OpenGL ES only supports the float varieties of
397 // glVertexAttrib
398 float c[] = GR_COLOR_TO_VEC4(coverage);
399 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
400 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000401 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000402 }
403 break;
404 case ProgramDesc::kUniform_ColorInput:
405 if (fProgramData->fCoverage != coverage) {
406 // OpenGL ES doesn't support unsigned byte varieties of
407 // glUniform
408 float c[] = GR_COLOR_TO_VEC4(coverage);
409 GrAssert(GrGLProgram::kUnusedUniform !=
410 fProgramData->fUniLocations.fCoverageUni);
411 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
412 1, c));
413 fProgramData->fCoverage = coverage;
414 }
415 break;
416 case ProgramDesc::kSolidWhite_ColorInput:
417 case ProgramDesc::kTransBlack_ColorInput:
418 break;
419 default:
420 GrCrash("Unknown coverage type.");
421 }
422 }
423}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000424
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000425bool GrGpuGL::flushGraphicsState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000426 const GrDrawState& drawState = this->getDrawState();
427
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000428 // GrGpu::setupClipAndFlushState should have already checked this
429 // and bailed if not true.
430 GrAssert(NULL != drawState.getRenderTarget());
431
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000432 if (kStencilPath_DrawType != type) {
433 this->flushMiscFixedFunctionState();
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000434
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000435 GrBlendCoeff srcCoeff;
436 GrBlendCoeff dstCoeff;
437 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
438 if (kSkipDraw_BlendOptFlag & blendOpts) {
439 return false;
440 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000441
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000442 GrCustomStage* customStages [GrDrawState::kNumStages];
443 this->buildProgram(kDrawPoints_DrawType == type,
444 blendOpts, dstCoeff, customStages);
445 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
446 customStages);
447 if (NULL == fProgramData) {
448 GrAssert(!"Failed to create program!");
449 return false;
450 }
junov@google.comf93e7172011-03-31 21:26:24 +0000451
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000452 if (fHWProgramID != fProgramData->fProgramID) {
453 GL_CALL(UseProgram(fProgramData->fProgramID));
454 fHWProgramID = fProgramData->fProgramID;
455 }
456 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
457 this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000458
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000459 GrColor color;
460 GrColor coverage;
461 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
462 color = 0;
463 coverage = 0;
464 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
465 color = 0xffffffff;
466 coverage = drawState.getCoverage();
467 } else {
468 color = drawState.getColor();
469 coverage = drawState.getCoverage();
470 }
471 this->flushColor(color);
472 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000473
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000474 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
475 if (this->isStageEnabled(s)) {
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000476#if GR_DEBUG
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000477 // check for circular rendering
478 GrAssert(NULL == drawState.getRenderTarget() ||
479 NULL == drawState.getTexture(s) ||
480 drawState.getTexture(s)->asRenderTarget() !=
481 drawState.getRenderTarget());
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000482#endif
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000483 this->flushBoundTextureAndParams(s);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000484
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000485 this->flushTextureMatrixAndDomain(s);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000486
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000487 if (NULL != fProgramData->fCustomStage[s]) {
488 const GrSamplerState& sampler =
489 this->getDrawState().getSampler(s);
490 const GrGLTexture* texture =
491 static_cast<const GrGLTexture*>(
492 this->getDrawState().getTexture(s));
493 fProgramData->fCustomStage[s]->setData(
494 this->glInterface(), *texture,
495 *sampler.getCustomStage(), s);
496 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000497 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000498 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000499 this->flushColorMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000500 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000501 this->flushStencil(type);
502 this->flushViewMatrix(type);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000503 this->flushScissor();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000504 this->flushAAState(type);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000505
506 GrIRect* rect = NULL;
507 GrIRect clipBounds;
508 if (drawState.isClipState() &&
509 fClip.hasConservativeBounds()) {
510 fClip.getConservativeBounds().roundOut(&clipBounds);
511 rect = &clipBounds;
512 }
513 // This must come after textures are flushed because a texture may need
514 // to be msaa-resolved (which will modify bound FBO state).
515 this->flushRenderTarget(rect);
516
junov@google.comf93e7172011-03-31 21:26:24 +0000517 return true;
518}
519
bsalomon@google.com2717d562012-05-07 19:10:52 +0000520#if GR_TEXT_SCALAR_IS_USHORT
521 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
522 #define TEXT_COORDS_ARE_NORMALIZED 1
523#elif GR_TEXT_SCALAR_IS_FLOAT
524 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
525 #define TEXT_COORDS_ARE_NORMALIZED 0
526#elif GR_TEXT_SCALAR_IS_FIXED
527 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
528 #define TEXT_COORDS_ARE_NORMALIZED 0
529#else
530 #error "unknown GR_TEXT_SCALAR type"
531#endif
532
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000533void GrGpuGL::setupGeometry(int* startVertex,
bsalomon@google.com49209392012-06-05 15:13:46 +0000534 int* startIndex,
535 int vertexCount,
536 int indexCount) {
junov@google.comf93e7172011-03-31 21:26:24 +0000537
538 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000539 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000540 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000541 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000542
bsalomon@google.come79c8152012-03-29 19:07:12 +0000543 GrVertexLayout currLayout = this->getVertexLayout();
544
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000545 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000546 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000547 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000548 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000549 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000550 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000551 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000552 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000553 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000554 int oldEdgeOffset;
555
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000556 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
557 fHWGeometryState.fVertexLayout,
558 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000559 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000560 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000561 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000562 bool indexed = NULL != startIndex;
563
564 int extraVertexOffset;
565 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000566 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000567
568 GrGLenum scalarType;
569 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000570 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000571 scalarType = TEXT_COORDS_GL_TYPE;
572 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000573 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000574 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
575 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000576 texCoordNorm = false;
577 }
578
579 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
580 *startVertex = 0;
581 if (indexed) {
582 *startIndex += extraIndexOffset;
583 }
584
585 // all the Pointers must be set if any of these are true
586 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
587 vertexOffset != fHWGeometryState.fVertexOffset ||
588 newStride != oldStride;
589
590 // position and tex coord offsets change if above conditions are true
591 // or the type/normalization changed based on text vs nontext type coords.
592 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000593 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000594 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000595 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000596
597 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000598 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000599 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000600 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000601 fHWGeometryState.fVertexOffset = vertexOffset;
602 }
603
tomhudson@google.com93813632011-10-27 20:21:16 +0000604 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000605 if (newTexCoordOffsets[t] > 0) {
606 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000607 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000608 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000609 GL_CALL(EnableVertexAttribArray(idx));
610 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000611 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000612 } else if (posAndTexChange ||
613 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000614 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000615 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000616 }
617 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000618 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000619 }
620 }
621
622 if (newColorOffset > 0) {
623 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000624 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000625 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000626 GL_CALL(EnableVertexAttribArray(idx));
627 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000628 true, newStride, colorOffset));
629 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000630 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000631 true, newStride, colorOffset));
632 }
633 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000634 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000635 }
636
bsalomon@google.coma3108262011-10-10 14:08:47 +0000637 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000638 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000639 int idx = GrGLProgram::CoverageAttributeIdx();
640 if (oldCoverageOffset <= 0) {
641 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000642 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000643 true, newStride, coverageOffset));
644 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000645 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000646 true, newStride, coverageOffset));
647 }
648 } else if (oldCoverageOffset > 0) {
649 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
650 }
651
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000652 if (newEdgeOffset > 0) {
653 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
654 int idx = GrGLProgram::EdgeAttributeIdx();
655 if (oldEdgeOffset <= 0) {
656 GL_CALL(EnableVertexAttribArray(idx));
657 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
658 false, newStride, edgeOffset));
659 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
660 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
661 false, newStride, edgeOffset));
662 }
663 } else if (oldEdgeOffset > 0) {
664 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
665 }
666
bsalomon@google.come79c8152012-03-29 19:07:12 +0000667 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000668 fHWGeometryState.fArrayPtrsDirty = false;
669}
670
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000671namespace {
672
673void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
674 const GrSamplerState& sampler,
675 GrCustomStage** customStages,
676 GrGLProgram* program, int index) {
677 GrCustomStage* customStage = sampler.getCustomStage();
678 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000679 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000680 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000681 customStages[index] = customStage;
682 } else {
683 stage->fCustomStageKey = 0;
684 customStages[index] = NULL;
685 }
686}
687
688}
689
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000690void GrGpuGL::buildProgram(bool isPoints,
bsalomon@google.com49209392012-06-05 15:13:46 +0000691 BlendOptFlags blendOpts,
692 GrBlendCoeff dstCoeff,
693 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000694 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000695 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000696
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000697 // This should already have been caught
698 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
699
700 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
701
702 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
703 kEmitCoverage_BlendOptFlag));
704
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000705 // The descriptor is used as a cache key. Thus when a field of the
706 // descriptor will not affect program generation (because of the vertex
707 // layout in use or other descriptor field settings) it should be set
708 // to a canonical value to avoid duplicate programs with different keys.
709
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000710 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000711 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000712
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000713 desc.fEmitsPointSize = isPoints;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000714
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000715 bool requiresAttributeColors =
716 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000717 bool requiresAttributeCoverage =
718 !skipCoverage && SkToBool(desc.fVertexLayout &
719 kCoverage_VertexLayoutBit);
720
721 // fColorInput/fCoverageInput records how colors are specified for the.
722 // program. So we strip the bits from the layout to avoid false negatives
723 // when searching for an existing program in the cache.
724 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000725
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000726 desc.fColorFilterXfermode = skipColor ?
727 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000728 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000729
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000730 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
731
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000732 // no reason to do edge aa or look at per-vertex coverage if coverage is
733 // ignored
734 if (skipCoverage) {
735 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
736 kCoverage_VertexLayoutBit);
737 }
738
739 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
740 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
741 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000742 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000743 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000744 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000745 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000746 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000747 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000748 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000749 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000750 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000751 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000752
753 bool covIsSolidWhite = !requiresAttributeCoverage &&
754 0xffffffff == drawState.getCoverage();
755
756 if (skipCoverage) {
757 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
758 } else if (covIsSolidWhite) {
759 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
760 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
761 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
762 } else {
763 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
764 }
junov@google.comf93e7172011-03-31 21:26:24 +0000765
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000766 int lastEnabledStage = -1;
767
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000768 if (!skipCoverage && (desc.fVertexLayout &
769 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000770 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000771 } else {
772 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000773 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000774 }
775
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000776 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000777 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000778
779 stage.fOptFlags = 0;
780 stage.setEnabled(this->isStageEnabled(s));
781
782 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
783 skipCoverage;
784
785 if (!skip && stage.isEnabled()) {
786 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000787 const GrGLTexture* texture =
788 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000789 GrAssert(NULL != texture);
790 const GrSamplerState& sampler = drawState.getSampler(s);
791 // we matrix to invert when orientation is TopDown, so make sure
792 // we aren't in that case before flagging as identity.
793 if (TextureMatrixIsIdentity(texture, sampler)) {
794 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
795 } else if (!sampler.getMatrix().hasPerspective()) {
796 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
797 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000798
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000799 if (sampler.hasTextureDomain()) {
800 GrAssert(GrSamplerState::kClamp_WrapMode ==
801 sampler.getWrapX() &&
802 GrSamplerState::kClamp_WrapMode ==
803 sampler.getWrapY());
804 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
805 }
806
807 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000808 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000809 if (GrPixelConfigIsAlphaOnly(texture->config())) {
810 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000811 // the shader must smear the single channel after
812 // reading the texture
813 if (this->glCaps().textureRedSupport()) {
814 // we can use R8 textures so use kSmearRed
815 stage.fInConfigFlags |=
816 StageDesc::kSmearRed_InConfigFlag;
817 } else {
818 // we can use A8 textures so use kSmearAlpha
819 stage.fInConfigFlags |=
820 StageDesc::kSmearAlpha_InConfigFlag;
821 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000822 } else if (sampler.swapsRAndB()) {
823 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
824 }
825 }
826 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000827 // The shader generator assumes that color channels are bytes
828 // when rounding.
829 GrAssert(4 == GrBytesPerPixel(texture->config()));
830 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
831 fUnpremulConversion) {
832 stage.fInConfigFlags |=
833 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
834 } else {
835 stage.fInConfigFlags |=
836 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
837 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000838 }
839
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000840 setup_custom_stage(&stage, sampler, customStages,
841 &fCurrentProgram, s);
842
junov@google.comf93e7172011-03-31 21:26:24 +0000843 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000844 stage.fOptFlags = 0;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000845 stage.fInConfigFlags = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000846 stage.fCustomStageKey = 0;
847 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000848 }
849 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000850
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000851 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000852 // The shader generator assumes that color channels are bytes
853 // when rounding.
854 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
855 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
856 desc.fOutputConfig =
857 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
858 } else {
859 desc.fOutputConfig =
860 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
861 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000862 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000863 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000864 }
865
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000866 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000867
868 // currently the experimental GS will only work with triangle prims
869 // (and it doesn't do anything other than pass through values from
870 // the VS to the FS anyway).
871#if 0 && GR_GL_EXPERIMENTAL_GS
872 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
873#endif
874
bsalomon@google.coma3108262011-10-10 14:08:47 +0000875 // we want to avoid generating programs with different "first cov stage"
876 // values when they would compute the same result.
877 // We set field in the desc to kNumStages when either there are no
878 // coverage stages or the distinction between coverage and color is
879 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000880 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +0000881 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000882 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000883 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000884 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000885 }
886
887 // other coverage inputs
888 if (!hasCoverage) {
889 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000890 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +0000891 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
892 }
893
894 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000895 // color filter is applied between color/coverage computation
896 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000897 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000898 }
899
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000900 if (this->getCaps().fDualSourceBlendingSupport &&
901 !(blendOpts & (kEmitCoverage_BlendOptFlag |
902 kCoverageAsAlpha_BlendOptFlag))) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000903 if (kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000904 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000905 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000906 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000907 } else if (kSA_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000908 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
909 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000910 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000911 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000912 } else if (kSC_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000913 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
914 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000915 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000916 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000917 }
918 }
919 }
junov@google.comf93e7172011-03-31 21:26:24 +0000920}