blob: 56a893dc3a464ccff1cc896635491996a309de8d [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 +000017GrGpuGL::ProgramCache::ProgramCache(const GrGLContextInfo& gl)
18 : fCount(0)
19 , fCurrLRUStamp(0)
20 , fGL(gl) {
21}
junov@google.comf93e7172011-03-31 21:26:24 +000022
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000023void GrGpuGL::ProgramCache::abandon() {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000024 for (int i = 0; i < fCount; ++i) {
25 GrAssert(NULL != fEntries[i].fProgram.get());
26 fEntries[i].fProgram->abandon();
27 fEntries[i].fProgram.reset(NULL);
28 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000029 fCount = 0;
30}
31
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000032GrGLProgram* GrGpuGL::ProgramCache::getProgram(const ProgramDesc& desc, GrCustomStage** stages) {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000033 Entry newEntry;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000034 newEntry.fKey.setKeyData(desc.asKey());
35
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000036 Entry* entry = fHashCache.find(newEntry.fKey);
37 if (NULL == entry) {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000038 newEntry.fProgram.reset(GrGLProgram::Create(fGL, desc, stages));
39 if (NULL == newEntry.fProgram.get()) {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000040 return NULL;
41 }
42 if (fCount < kMaxEntries) {
43 entry = fEntries + fCount;
44 ++fCount;
45 } else {
46 GrAssert(kMaxEntries == fCount);
47 entry = fEntries;
48 for (int i = 1; i < kMaxEntries; ++i) {
49 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
50 entry = fEntries + i;
junov@google.comf93e7172011-03-31 21:26:24 +000051 }
junov@google.comf93e7172011-03-31 21:26:24 +000052 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000053 fHashCache.remove(entry->fKey, entry);
junov@google.comf93e7172011-03-31 21:26:24 +000054 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000055 *entry = newEntry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000056 fHashCache.insert(entry->fKey, entry);
junov@google.comf93e7172011-03-31 21:26:24 +000057 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000058
59 entry->fLRUStamp = fCurrLRUStamp;
60 if (GR_UINT32_MAX == fCurrLRUStamp) {
61 // wrap around! just trash our LRU, one time hit.
62 for (int i = 0; i < fCount; ++i) {
63 fEntries[i].fLRUStamp = 0;
64 }
65 }
66 ++fCurrLRUStamp;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000067 return entry->fProgram;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000068}
junov@google.comf93e7172011-03-31 21:26:24 +000069
bsalomon@google.com1e257a52011-07-06 19:52:16 +000070////////////////////////////////////////////////////////////////////////////////
71
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000072void GrGpuGL::abandonResources(){
73 INHERITED::abandonResources();
74 fProgramCache->abandon();
75 fHWProgramID = 0;
76}
77
78////////////////////////////////////////////////////////////////////////////////
79
bsalomon@google.com0b77d682011-08-19 13:28:54 +000080#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
81
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000082void GrGpuGL::flushViewMatrix(DrawType type) {
bsalomon@google.com4c883782012-06-04 19:05:11 +000083 const GrGLRenderTarget* rt = static_cast<const GrGLRenderTarget*>(this->getDrawState().getRenderTarget());
84 SkISize viewportSize;
85 const GrGLIRect& viewport = rt->getViewport();
86 viewportSize.set(viewport.fWidth, viewport.fHeight);
junov@google.comf93e7172011-03-31 21:26:24 +000087
bsalomon@google.com4c883782012-06-04 19:05:11 +000088 const GrMatrix& vm = this->getDrawState().getViewMatrix();
89
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000090 if (kStencilPath_DrawType == type) {
bsalomon@google.com05a718c2012-06-29 14:01:53 +000091 if (fHWPathMatrixState.fViewMatrix != vm ||
92 fHWPathMatrixState.fRTSize != viewportSize) {
93 // rescale the coords from skia's "device" coords to GL's normalized coords,
94 // and perform a y-flip.
95 GrMatrix m;
96 m.setScale(GrIntToScalar(2) / rt->width(), GrIntToScalar(-2) / rt->height());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000097 m.postTranslate(-GR_Scalar1, GR_Scalar1);
bsalomon@google.com05a718c2012-06-29 14:01:53 +000098 m.preConcat(vm);
99
100 // GL wants a column-major 4x4.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000101 GrGLfloat mv[] = {
102 // col 0
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000103 GrScalarToFloat(m[GrMatrix::kMScaleX]),
104 GrScalarToFloat(m[GrMatrix::kMSkewY]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000105 0,
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000106 GrScalarToFloat(m[GrMatrix::kMPersp0]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000107
108 // col 1
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000109 GrScalarToFloat(m[GrMatrix::kMSkewX]),
110 GrScalarToFloat(m[GrMatrix::kMScaleY]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000111 0,
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000112 GrScalarToFloat(m[GrMatrix::kMPersp1]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000113
114 // col 2
115 0, 0, 0, 0,
116
117 // col3
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000118 GrScalarToFloat(m[GrMatrix::kMTransX]),
119 GrScalarToFloat(m[GrMatrix::kMTransY]),
120 0.0f,
121 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000122 };
123 GL_CALL(MatrixMode(GR_GL_PROJECTION));
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000124 GL_CALL(LoadMatrixf(mv));
125 fHWPathMatrixState.fViewMatrix = vm;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000126 fHWPathMatrixState.fRTSize = viewportSize;
127 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000128 } else if (!fCurrentProgram->fViewMatrix.cheapEqualTo(vm) ||
129 fCurrentProgram->fViewportSize != viewportSize) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000130 GrMatrix m;
131 m.setAll(
bsalomon@google.com4c883782012-06-04 19:05:11 +0000132 GrIntToScalar(2) / viewportSize.fWidth, 0, -GR_Scalar1,
133 0,-GrIntToScalar(2) / viewportSize.fHeight, GR_Scalar1,
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000134 0, 0, GrMatrix::I()[8]);
135 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000136
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000137 // ES doesn't allow you to pass true to the transpose param,
138 // so do our own transpose
139 GrGLfloat mt[] = {
140 GrScalarToFloat(m[GrMatrix::kMScaleX]),
141 GrScalarToFloat(m[GrMatrix::kMSkewY]),
142 GrScalarToFloat(m[GrMatrix::kMPersp0]),
143 GrScalarToFloat(m[GrMatrix::kMSkewX]),
144 GrScalarToFloat(m[GrMatrix::kMScaleY]),
145 GrScalarToFloat(m[GrMatrix::kMPersp1]),
146 GrScalarToFloat(m[GrMatrix::kMTransX]),
147 GrScalarToFloat(m[GrMatrix::kMTransY]),
148 GrScalarToFloat(m[GrMatrix::kMPersp2])
149 };
150
bsalomon@google.com341767c2012-05-11 20:47:39 +0000151 GrAssert(GrGLProgram::kUnusedUniform !=
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000152 fCurrentProgram->fUniLocations.fViewMatrixUni);
153 GL_CALL(UniformMatrix3fv(fCurrentProgram->fUniLocations.fViewMatrixUni,
bsalomon@google.com341767c2012-05-11 20:47:39 +0000154 1, false, mt));
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000155 fCurrentProgram->fViewMatrix = vm;
156 fCurrentProgram->fViewportSize = viewportSize;
bsalomon@google.com91961302011-05-09 18:39:58 +0000157 }
junov@google.comf93e7172011-03-31 21:26:24 +0000158}
159
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000160///////////////////////////////////////////////////////////////////////////////
junov@google.com6acc9b32011-05-16 18:32:07 +0000161
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000162// helpers for texture matrices
junov@google.com6acc9b32011-05-16 18:32:07 +0000163
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000164void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000165 GrMatrix* matrix) {
166 GrAssert(NULL != texture);
167 GrAssert(NULL != matrix);
168 GrGLTexture::Orientation orientation = texture->orientation();
169 if (GrGLTexture::kBottomUp_Orientation == orientation) {
170 GrMatrix invY;
171 invY.setAll(GR_Scalar1, 0, 0,
172 0, -GR_Scalar1, GR_Scalar1,
173 0, 0, GrMatrix::I()[8]);
174 matrix->postConcat(invY);
175 } else {
176 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
junov@google.com6acc9b32011-05-16 18:32:07 +0000177 }
178}
179
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000180bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
181 const GrSamplerState& sampler) {
182 GrAssert(NULL != texture);
183 if (!sampler.getMatrix().isIdentity()) {
184 return false;
185 }
186 GrGLTexture::Orientation orientation = texture->orientation();
187 if (GrGLTexture::kBottomUp_Orientation == orientation) {
188 return false;
189 } else {
190 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
191 }
192 return true;
193}
194
195///////////////////////////////////////////////////////////////////////////////
196
197void GrGpuGL::flushTextureMatrixAndDomain(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000198 const GrDrawState& drawState = this->getDrawState();
199 const GrGLTexture* texture =
200 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000201 if (NULL != texture) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000202
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000203 bool orientationChange = fCurrentProgram->fTextureOrientation[s] !=
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000204 texture->orientation();
205
206 const GrGLint& matrixUni =
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000207 fCurrentProgram->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000208
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000209 const GrMatrix& hwMatrix = fCurrentProgram->fTextureMatrices[s];
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000210 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000211
212 if (GrGLProgram::kUnusedUniform != matrixUni &&
213 (orientationChange || !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000214
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000215 GrMatrix m = samplerMatrix;
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000216 AdjustTextureMatrix(texture, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000217
bsalomon@google.com91961302011-05-09 18:39:58 +0000218 // ES doesn't allow you to pass true to the transpose param,
219 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000220 GrGLfloat mt[] = {
221 GrScalarToFloat(m[GrMatrix::kMScaleX]),
222 GrScalarToFloat(m[GrMatrix::kMSkewY]),
223 GrScalarToFloat(m[GrMatrix::kMPersp0]),
224 GrScalarToFloat(m[GrMatrix::kMSkewX]),
225 GrScalarToFloat(m[GrMatrix::kMScaleY]),
226 GrScalarToFloat(m[GrMatrix::kMPersp1]),
227 GrScalarToFloat(m[GrMatrix::kMTransX]),
228 GrScalarToFloat(m[GrMatrix::kMTransY]),
229 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000230 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000231
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000232 GL_CALL(UniformMatrix3fv(matrixUni, 1, false, mt));
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000233 fCurrentProgram->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000234 }
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000235
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000236 fCurrentProgram->fTextureOrientation[s] = texture->orientation();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000237 }
junov@google.comf93e7172011-03-31 21:26:24 +0000238}
239
junov@google.comf93e7172011-03-31 21:26:24 +0000240
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000241void GrGpuGL::flushColorMatrix() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000242 int matrixUni = fCurrentProgram->fUniLocations.fColorMatrixUni;
243 int vecUni = fCurrentProgram->fUniLocations.fColorMatrixVecUni;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000244 if (GrGLProgram::kUnusedUniform != matrixUni
245 && GrGLProgram::kUnusedUniform != vecUni) {
246 const float* m = this->getDrawState().getColorMatrix();
247 GrGLfloat mt[] = {
248 m[0], m[5], m[10], m[15],
249 m[1], m[6], m[11], m[16],
250 m[2], m[7], m[12], m[17],
251 m[3], m[8], m[13], m[18],
252 };
253 static float scale = 1.0f / 255.0f;
254 GrGLfloat vec[] = {
255 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
256 };
257 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
258 GL_CALL(Uniform4fv(vecUni, 1, vec));
259 }
260}
261
Scroggo01b87ec2011-05-11 18:05:38 +0000262static const float ONE_OVER_255 = 1.f / 255.f;
263
264#define GR_COLOR_TO_VEC4(color) {\
265 GrColorUnpackR(color) * ONE_OVER_255,\
266 GrColorUnpackG(color) * ONE_OVER_255,\
267 GrColorUnpackB(color) * ONE_OVER_255,\
268 GrColorUnpackA(color) * ONE_OVER_255 \
269}
270
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000271void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000272 const ProgramDesc& desc = fCurrentProgram->getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000273 const GrDrawState& drawState = this->getDrawState();
274
bsalomon@google.come79c8152012-03-29 19:07:12 +0000275 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000276 // color will be specified per-vertex as an attribute
277 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000278 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000279 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000280 switch (desc.fColorInput) {
281 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000282 if (fHWConstAttribColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000283 // OpenGL ES only supports the float varieties of
284 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000285 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000286 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
287 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000288 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000289 }
290 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000291 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000292 if (fCurrentProgram->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000293 // OpenGL ES doesn't support unsigned byte varieties of
294 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000295 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000296 GrAssert(GrGLProgram::kUnusedUniform !=
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000297 fCurrentProgram->fUniLocations.fColorUni);
298 GL_CALL(Uniform4fv(fCurrentProgram->fUniLocations.fColorUni, 1, c));
299 fCurrentProgram->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000300 }
301 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000302 case ProgramDesc::kSolidWhite_ColorInput:
303 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000304 break;
305 default:
306 GrCrash("Unknown color type.");
307 }
308 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000309 if (fCurrentProgram->fUniLocations.fColorFilterUni
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000310 != GrGLProgram::kUnusedUniform
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000311 && fCurrentProgram->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000312 != drawState.getColorFilterColor()) {
313 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000314 GL_CALL(Uniform4fv(fCurrentProgram->fUniLocations.fColorFilterUni, 1, c));
315 fCurrentProgram->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000316 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000317}
318
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000319void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000320 const ProgramDesc& desc = fCurrentProgram->getDesc();
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000321 // const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000322
323
bsalomon@google.come79c8152012-03-29 19:07:12 +0000324 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000325 // coverage will be specified per-vertex as an attribute
326 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000327 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000328 } else {
329 switch (desc.fCoverageInput) {
330 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000331 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000332 // OpenGL ES only supports the float varieties of
333 // glVertexAttrib
334 float c[] = GR_COLOR_TO_VEC4(coverage);
335 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
336 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000337 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000338 }
339 break;
340 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000341 if (fCurrentProgram->fCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000342 // OpenGL ES doesn't support unsigned byte varieties of
343 // glUniform
344 float c[] = GR_COLOR_TO_VEC4(coverage);
345 GrAssert(GrGLProgram::kUnusedUniform !=
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000346 fCurrentProgram->fUniLocations.fCoverageUni);
347 GL_CALL(Uniform4fv(fCurrentProgram->fUniLocations.fCoverageUni, 1, c));
348 fCurrentProgram->fCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000349 }
350 break;
351 case ProgramDesc::kSolidWhite_ColorInput:
352 case ProgramDesc::kTransBlack_ColorInput:
353 break;
354 default:
355 GrCrash("Unknown coverage type.");
356 }
357 }
358}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000359
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000360bool GrGpuGL::flushGraphicsState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000361 const GrDrawState& drawState = this->getDrawState();
362
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000363 // GrGpu::setupClipAndFlushState should have already checked this
364 // and bailed if not true.
365 GrAssert(NULL != drawState.getRenderTarget());
366
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000367 if (kStencilPath_DrawType != type) {
368 this->flushMiscFixedFunctionState();
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000369
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000370 GrBlendCoeff srcCoeff;
371 GrBlendCoeff dstCoeff;
372 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
373 if (kSkipDraw_BlendOptFlag & blendOpts) {
374 return false;
375 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000376
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000377 GrCustomStage* customStages [GrDrawState::kNumStages];
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000378 GrGLProgram::Desc desc;
379 this->buildProgram(kDrawPoints_DrawType == type, blendOpts, dstCoeff, customStages, &desc);
380
381 fCurrentProgram.reset(fProgramCache->getProgram(desc, customStages));
382 if (NULL == fCurrentProgram.get()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000383 GrAssert(!"Failed to create program!");
384 return false;
385 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000386 fCurrentProgram.get()->ref();
junov@google.comf93e7172011-03-31 21:26:24 +0000387
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000388 if (fHWProgramID != fCurrentProgram->fProgramID) {
389 GL_CALL(UseProgram(fCurrentProgram->fProgramID));
390 fHWProgramID = fCurrentProgram->fProgramID;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000391 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000392 fCurrentProgram->overrideBlend(&srcCoeff, &dstCoeff);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000393 this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000394
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000395 GrColor color;
396 GrColor coverage;
397 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
398 color = 0;
399 coverage = 0;
400 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
401 color = 0xffffffff;
402 coverage = drawState.getCoverage();
403 } else {
404 color = drawState.getColor();
405 coverage = drawState.getCoverage();
406 }
407 this->flushColor(color);
408 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000409
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000410 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
411 if (this->isStageEnabled(s)) {
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000412#if GR_DEBUG
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000413 // check for circular rendering
414 GrAssert(NULL == drawState.getRenderTarget() ||
415 NULL == drawState.getTexture(s) ||
416 drawState.getTexture(s)->asRenderTarget() !=
417 drawState.getRenderTarget());
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000418#endif
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000419 this->flushBoundTextureAndParams(s);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000420
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000421 this->flushTextureMatrixAndDomain(s);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000422
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000423 if (NULL != fCurrentProgram->fProgramStage[s]) {
424 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
425 const GrGLTexture* texture = static_cast<const GrGLTexture*>(
426 this->getDrawState().getTexture(s));
427 fCurrentProgram->fProgramStage[s]->setData(this->glInterface(),
428 *sampler.getCustomStage(),
429 drawState.getRenderTarget(), s);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000430 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000431 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000432 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000433 this->flushColorMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000434 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000435 this->flushStencil(type);
436 this->flushViewMatrix(type);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000437 this->flushScissor();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000438 this->flushAAState(type);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000439
440 GrIRect* rect = NULL;
441 GrIRect clipBounds;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000442 if (drawState.isClipState()) {
bsalomon@google.com4c883782012-06-04 19:05:11 +0000443 fClip.getConservativeBounds().roundOut(&clipBounds);
444 rect = &clipBounds;
445 }
446 // This must come after textures are flushed because a texture may need
447 // to be msaa-resolved (which will modify bound FBO state).
448 this->flushRenderTarget(rect);
449
junov@google.comf93e7172011-03-31 21:26:24 +0000450 return true;
451}
452
bsalomon@google.com2717d562012-05-07 19:10:52 +0000453#if GR_TEXT_SCALAR_IS_USHORT
454 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
455 #define TEXT_COORDS_ARE_NORMALIZED 1
456#elif GR_TEXT_SCALAR_IS_FLOAT
457 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
458 #define TEXT_COORDS_ARE_NORMALIZED 0
459#elif GR_TEXT_SCALAR_IS_FIXED
460 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
461 #define TEXT_COORDS_ARE_NORMALIZED 0
462#else
463 #error "unknown GR_TEXT_SCALAR type"
464#endif
465
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000466void GrGpuGL::setupGeometry(int* startVertex,
bsalomon@google.com49209392012-06-05 15:13:46 +0000467 int* startIndex,
468 int vertexCount,
469 int indexCount) {
junov@google.comf93e7172011-03-31 21:26:24 +0000470
471 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000472 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000473 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000474 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000475
bsalomon@google.come79c8152012-03-29 19:07:12 +0000476 GrVertexLayout currLayout = this->getVertexLayout();
477
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000478 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000479 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000480 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000481 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000482 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000483 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000484 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000485 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000486 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000487 int oldEdgeOffset;
488
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000489 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
490 fHWGeometryState.fVertexLayout,
491 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000492 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000493 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000494 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000495 bool indexed = NULL != startIndex;
496
497 int extraVertexOffset;
498 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000499 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000500
501 GrGLenum scalarType;
502 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000503 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000504 scalarType = TEXT_COORDS_GL_TYPE;
505 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000506 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000507 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
508 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000509 texCoordNorm = false;
510 }
511
512 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
513 *startVertex = 0;
514 if (indexed) {
515 *startIndex += extraIndexOffset;
516 }
517
518 // all the Pointers must be set if any of these are true
519 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
520 vertexOffset != fHWGeometryState.fVertexOffset ||
521 newStride != oldStride;
522
523 // position and tex coord offsets change if above conditions are true
524 // or the type/normalization changed based on text vs nontext type coords.
525 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000526 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000527 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000528 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000529
530 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000531 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000532 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000533 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000534 fHWGeometryState.fVertexOffset = vertexOffset;
535 }
536
tomhudson@google.com93813632011-10-27 20:21:16 +0000537 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000538 if (newTexCoordOffsets[t] > 0) {
539 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000540 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000541 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000542 GL_CALL(EnableVertexAttribArray(idx));
543 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000544 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000545 } else if (posAndTexChange ||
546 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000547 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000548 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000549 }
550 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000551 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000552 }
553 }
554
555 if (newColorOffset > 0) {
556 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000557 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000558 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000559 GL_CALL(EnableVertexAttribArray(idx));
560 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000561 true, newStride, colorOffset));
562 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000563 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000564 true, newStride, colorOffset));
565 }
566 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000567 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000568 }
569
bsalomon@google.coma3108262011-10-10 14:08:47 +0000570 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000571 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000572 int idx = GrGLProgram::CoverageAttributeIdx();
573 if (oldCoverageOffset <= 0) {
574 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000575 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000576 true, newStride, coverageOffset));
577 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000578 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000579 true, newStride, coverageOffset));
580 }
581 } else if (oldCoverageOffset > 0) {
582 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
583 }
584
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000585 if (newEdgeOffset > 0) {
586 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
587 int idx = GrGLProgram::EdgeAttributeIdx();
588 if (oldEdgeOffset <= 0) {
589 GL_CALL(EnableVertexAttribArray(idx));
590 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
591 false, newStride, edgeOffset));
592 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
593 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
594 false, newStride, edgeOffset));
595 }
596 } else if (oldEdgeOffset > 0) {
597 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
598 }
599
bsalomon@google.come79c8152012-03-29 19:07:12 +0000600 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000601 fHWGeometryState.fArrayPtrsDirty = false;
602}
603
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000604namespace {
605
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000606void setup_custom_stage(GrGLProgram::Desc::StageDesc* stage,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000607 const GrSamplerState& sampler,
608 GrCustomStage** customStages,
609 GrGLProgram* program, int index) {
610 GrCustomStage* customStage = sampler.getCustomStage();
611 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000612 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000613 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000614 customStages[index] = customStage;
615 } else {
616 stage->fCustomStageKey = 0;
617 customStages[index] = NULL;
618 }
619}
620
621}
622
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000623void GrGpuGL::buildProgram(bool isPoints,
bsalomon@google.com49209392012-06-05 15:13:46 +0000624 BlendOptFlags blendOpts,
625 GrBlendCoeff dstCoeff,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000626 GrCustomStage** customStages,
627 ProgramDesc* desc) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000628 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000629
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000630 // This should already have been caught
631 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
632
633 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
634
635 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
636 kEmitCoverage_BlendOptFlag));
637
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000638 // The descriptor is used as a cache key. Thus when a field of the
639 // descriptor will not affect program generation (because of the vertex
640 // layout in use or other descriptor field settings) it should be set
641 // to a canonical value to avoid duplicate programs with different keys.
642
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000643 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000644 desc->fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000645
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000646 desc->fEmitsPointSize = isPoints;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000647
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000648 bool requiresAttributeColors =
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000649 !skipColor && SkToBool(desc->fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000650 bool requiresAttributeCoverage =
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000651 !skipCoverage && SkToBool(desc->fVertexLayout &
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000652 kCoverage_VertexLayoutBit);
653
654 // fColorInput/fCoverageInput records how colors are specified for the.
655 // program. So we strip the bits from the layout to avoid false negatives
656 // when searching for an existing program in the cache.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000657 desc->fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000658
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000659 desc->fColorFilterXfermode = skipColor ?
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000660 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000661 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000662
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000663 desc->fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000664
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000665 // no reason to do edge aa or look at per-vertex coverage if coverage is
666 // ignored
667 if (skipCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000668 desc->fVertexLayout &= ~(kEdge_VertexLayoutBit |
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000669 kCoverage_VertexLayoutBit);
670 }
671
672 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
673 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
674 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000675 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000676 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000677 desc->fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000678 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000679 desc->fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000680 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000681 desc->fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000682 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000683 desc->fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000684 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000685
686 bool covIsSolidWhite = !requiresAttributeCoverage &&
687 0xffffffff == drawState.getCoverage();
688
689 if (skipCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000690 desc->fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000691 } else if (covIsSolidWhite) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000692 desc->fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000693 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000694 desc->fCoverageInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000695 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000696 desc->fCoverageInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000697 }
junov@google.comf93e7172011-03-31 21:26:24 +0000698
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000699 int lastEnabledStage = -1;
700
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000701 if (!skipCoverage && (desc->fVertexLayout &
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000702 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000703 desc->fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000704 } else {
705 // use canonical value when not set to avoid cache misses
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000706 desc->fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000707 }
708
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000709 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000710 StageDesc& stage = desc->fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000711
712 stage.fOptFlags = 0;
713 stage.setEnabled(this->isStageEnabled(s));
714
715 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
716 skipCoverage;
717
718 if (!skip && stage.isEnabled()) {
719 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000720 const GrGLTexture* texture =
721 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000722 GrAssert(NULL != texture);
723 const GrSamplerState& sampler = drawState.getSampler(s);
724 // we matrix to invert when orientation is TopDown, so make sure
725 // we aren't in that case before flagging as identity.
726 if (TextureMatrixIsIdentity(texture, sampler)) {
727 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
728 } else if (!sampler.getMatrix().hasPerspective()) {
729 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
730 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000731
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000732 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000733 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000734 if (GrPixelConfigIsAlphaOnly(texture->config())) {
735 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000736 // the shader must smear the single channel after
737 // reading the texture
738 if (this->glCaps().textureRedSupport()) {
739 // we can use R8 textures so use kSmearRed
740 stage.fInConfigFlags |=
741 StageDesc::kSmearRed_InConfigFlag;
742 } else {
743 // we can use A8 textures so use kSmearAlpha
744 stage.fInConfigFlags |=
745 StageDesc::kSmearAlpha_InConfigFlag;
746 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000747 } else if (sampler.swapsRAndB()) {
748 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
749 }
750 }
751 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000752 // The shader generator assumes that color channels are bytes
753 // when rounding.
754 GrAssert(4 == GrBytesPerPixel(texture->config()));
755 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
756 fUnpremulConversion) {
757 stage.fInConfigFlags |=
758 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
759 } else {
760 stage.fInConfigFlags |=
761 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
762 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000763 }
764
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000765 setup_custom_stage(&stage, sampler, customStages, fCurrentProgram.get(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000766
junov@google.comf93e7172011-03-31 21:26:24 +0000767 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000768 stage.fOptFlags = 0;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000769 stage.fInConfigFlags = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000770 stage.fCustomStageKey = 0;
771 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000772 }
773 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000774
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000775 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000776 // The shader generator assumes that color channels are bytes
777 // when rounding.
778 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
779 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000780 desc->fOutputConfig =
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000781 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
782 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000783 desc->fOutputConfig =
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000784 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
785 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000786 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000787 desc->fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000788 }
789
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000790 desc->fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000791
792 // currently the experimental GS will only work with triangle prims
793 // (and it doesn't do anything other than pass through values from
794 // the VS to the FS anyway).
795#if 0 && GR_GL_EXPERIMENTAL_GS
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000796 desc->fExperimentalGS = this->getCaps().fGeometryShaderSupport;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000797#endif
798
bsalomon@google.coma3108262011-10-10 14:08:47 +0000799 // we want to avoid generating programs with different "first cov stage"
800 // values when they would compute the same result.
801 // We set field in the desc to kNumStages when either there are no
802 // coverage stages or the distinction between coverage and color is
803 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000804 int firstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000805 desc->fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000806 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000807 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000808 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000809 }
810
811 // other coverage inputs
812 if (!hasCoverage) {
813 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000814 requiresAttributeCoverage ||
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000815 (desc->fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000816 }
817
818 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000819 // color filter is applied between color/coverage computation
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000820 if (SkXfermode::kDst_Mode != desc->fColorFilterXfermode) {
821 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000822 }
823
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000824 if (this->getCaps().fDualSourceBlendingSupport &&
825 !(blendOpts & (kEmitCoverage_BlendOptFlag |
826 kCoverageAsAlpha_BlendOptFlag))) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000827 if (kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000828 // write the coverage value to second color
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000829 desc->fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
830 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000831 } else if (kSA_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000832 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
833 // cover
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000834 desc->fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
835 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000836 } else if (kSC_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000837 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
838 // cover
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000839 desc->fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
840 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000841 }
842 }
843 }
junov@google.comf93e7172011-03-31 21:26:24 +0000844}