blob: 7f982e724d231ef70137b0dbcf9e636ccef92d30 [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 +000023GrGpuGL::ProgramCache::~ProgramCache() {
24 for (int i = 0; i < fCount; ++i) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000025 GrGpuGL::DeleteProgram(fGL.interface(), fEntries[i].fProgram);
junov@google.comf93e7172011-03-31 21:26:24 +000026 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000027}
junov@google.comf93e7172011-03-31 21:26:24 +000028
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000029void GrGpuGL::ProgramCache::abandon() {
30 fCount = 0;
31}
32
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000033GrGLProgram* GrGpuGL::ProgramCache::getProgram(const ProgramDesc& desc, GrCustomStage** stages) {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000034 Entry newEntry;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000035 newEntry.fKey.setKeyData(desc.asKey());
36
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000037 Entry* entry = fHashCache.find(newEntry.fKey);
38 if (NULL == entry) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000039 newEntry.fProgram.reset(SkNEW(GrGLProgram));
40 if (!newEntry.fProgram->genProgram(fGL, desc, stages)) {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000041 return NULL;
42 }
43 if (fCount < kMaxEntries) {
44 entry = fEntries + fCount;
45 ++fCount;
46 } else {
47 GrAssert(kMaxEntries == fCount);
48 entry = fEntries;
49 for (int i = 1; i < kMaxEntries; ++i) {
50 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
51 entry = fEntries + i;
junov@google.comf93e7172011-03-31 21:26:24 +000052 }
junov@google.comf93e7172011-03-31 21:26:24 +000053 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000054 fHashCache.remove(entry->fKey, entry);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000055 GrGpuGL::DeleteProgram(fGL.interface(), entry->fProgram);
junov@google.comf93e7172011-03-31 21:26:24 +000056 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000057 *entry = newEntry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000058 fHashCache.insert(entry->fKey, entry);
junov@google.comf93e7172011-03-31 21:26:24 +000059 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000060
61 entry->fLRUStamp = fCurrLRUStamp;
62 if (GR_UINT32_MAX == fCurrLRUStamp) {
63 // wrap around! just trash our LRU, one time hit.
64 for (int i = 0; i < fCount; ++i) {
65 fEntries[i].fLRUStamp = 0;
66 }
67 }
68 ++fCurrLRUStamp;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000069 return entry->fProgram;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000070}
junov@google.comf93e7172011-03-31 21:26:24 +000071
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000072void GrGpuGL::DeleteProgram(const GrGLInterface* gl, GrGLProgram* program) {
73 GR_GL_CALL(gl, DeleteShader(program->fVShaderID));
74 if (program->fGShaderID) {
75 GR_GL_CALL(gl, DeleteShader(program->fGShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000076 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000077 GR_GL_CALL(gl, DeleteShader(program->fFShaderID));
78 GR_GL_CALL(gl, DeleteProgram(program->fProgramID));
79 GR_DEBUGCODE(program->fVShaderID = 0);
80 GR_DEBUGCODE(program->fGShaderID = 0);
81 GR_DEBUGCODE(program->fFShaderID = 0);
82 GR_DEBUGCODE(program->fProgramID = 0);
junov@google.comf93e7172011-03-31 21:26:24 +000083}
84
bsalomon@google.com1e257a52011-07-06 19:52:16 +000085////////////////////////////////////////////////////////////////////////////////
86
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000087void GrGpuGL::abandonResources(){
88 INHERITED::abandonResources();
89 fProgramCache->abandon();
90 fHWProgramID = 0;
91}
92
93////////////////////////////////////////////////////////////////////////////////
94
bsalomon@google.com0b77d682011-08-19 13:28:54 +000095#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
96
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000097void GrGpuGL::flushViewMatrix(DrawType type) {
bsalomon@google.com4c883782012-06-04 19:05:11 +000098 const GrGLRenderTarget* rt = static_cast<const GrGLRenderTarget*>(this->getDrawState().getRenderTarget());
99 SkISize viewportSize;
100 const GrGLIRect& viewport = rt->getViewport();
101 viewportSize.set(viewport.fWidth, viewport.fHeight);
junov@google.comf93e7172011-03-31 21:26:24 +0000102
bsalomon@google.com4c883782012-06-04 19:05:11 +0000103 const GrMatrix& vm = this->getDrawState().getViewMatrix();
104
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000105 if (kStencilPath_DrawType == type) {
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000106 if (fHWPathMatrixState.fViewMatrix != vm ||
107 fHWPathMatrixState.fRTSize != viewportSize) {
108 // rescale the coords from skia's "device" coords to GL's normalized coords,
109 // and perform a y-flip.
110 GrMatrix m;
111 m.setScale(GrIntToScalar(2) / rt->width(), GrIntToScalar(-2) / rt->height());
robertphillips@google.com59f46b82012-07-10 17:30:58 +0000112 m.postTranslate(-GR_Scalar1, GR_Scalar1);
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000113 m.preConcat(vm);
114
115 // GL wants a column-major 4x4.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000116 GrGLfloat mv[] = {
117 // col 0
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000118 GrScalarToFloat(m[GrMatrix::kMScaleX]),
119 GrScalarToFloat(m[GrMatrix::kMSkewY]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000120 0,
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000121 GrScalarToFloat(m[GrMatrix::kMPersp0]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000122
123 // col 1
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000124 GrScalarToFloat(m[GrMatrix::kMSkewX]),
125 GrScalarToFloat(m[GrMatrix::kMScaleY]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000126 0,
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000127 GrScalarToFloat(m[GrMatrix::kMPersp1]),
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000128
129 // col 2
130 0, 0, 0, 0,
131
132 // col3
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000133 GrScalarToFloat(m[GrMatrix::kMTransX]),
134 GrScalarToFloat(m[GrMatrix::kMTransY]),
135 0.0f,
136 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000137 };
138 GL_CALL(MatrixMode(GR_GL_PROJECTION));
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000139 GL_CALL(LoadMatrixf(mv));
140 fHWPathMatrixState.fViewMatrix = vm;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000141 fHWPathMatrixState.fRTSize = viewportSize;
142 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000143 } else if (!fCurrentProgram->fViewMatrix.cheapEqualTo(vm) ||
144 fCurrentProgram->fViewportSize != viewportSize) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000145 GrMatrix m;
146 m.setAll(
bsalomon@google.com4c883782012-06-04 19:05:11 +0000147 GrIntToScalar(2) / viewportSize.fWidth, 0, -GR_Scalar1,
148 0,-GrIntToScalar(2) / viewportSize.fHeight, GR_Scalar1,
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000149 0, 0, GrMatrix::I()[8]);
150 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000151
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000152 // ES doesn't allow you to pass true to the transpose param,
153 // so do our own transpose
154 GrGLfloat mt[] = {
155 GrScalarToFloat(m[GrMatrix::kMScaleX]),
156 GrScalarToFloat(m[GrMatrix::kMSkewY]),
157 GrScalarToFloat(m[GrMatrix::kMPersp0]),
158 GrScalarToFloat(m[GrMatrix::kMSkewX]),
159 GrScalarToFloat(m[GrMatrix::kMScaleY]),
160 GrScalarToFloat(m[GrMatrix::kMPersp1]),
161 GrScalarToFloat(m[GrMatrix::kMTransX]),
162 GrScalarToFloat(m[GrMatrix::kMTransY]),
163 GrScalarToFloat(m[GrMatrix::kMPersp2])
164 };
165
bsalomon@google.com341767c2012-05-11 20:47:39 +0000166 GrAssert(GrGLProgram::kUnusedUniform !=
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000167 fCurrentProgram->fUniLocations.fViewMatrixUni);
168 GL_CALL(UniformMatrix3fv(fCurrentProgram->fUniLocations.fViewMatrixUni,
bsalomon@google.com341767c2012-05-11 20:47:39 +0000169 1, false, mt));
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000170 fCurrentProgram->fViewMatrix = vm;
171 fCurrentProgram->fViewportSize = viewportSize;
bsalomon@google.com91961302011-05-09 18:39:58 +0000172 }
junov@google.comf93e7172011-03-31 21:26:24 +0000173}
174
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000175///////////////////////////////////////////////////////////////////////////////
junov@google.com6acc9b32011-05-16 18:32:07 +0000176
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000177// helpers for texture matrices
junov@google.com6acc9b32011-05-16 18:32:07 +0000178
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000179void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000180 GrMatrix* matrix) {
181 GrAssert(NULL != texture);
182 GrAssert(NULL != matrix);
183 GrGLTexture::Orientation orientation = texture->orientation();
184 if (GrGLTexture::kBottomUp_Orientation == orientation) {
185 GrMatrix invY;
186 invY.setAll(GR_Scalar1, 0, 0,
187 0, -GR_Scalar1, GR_Scalar1,
188 0, 0, GrMatrix::I()[8]);
189 matrix->postConcat(invY);
190 } else {
191 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
junov@google.com6acc9b32011-05-16 18:32:07 +0000192 }
193}
194
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000195bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
196 const GrSamplerState& sampler) {
197 GrAssert(NULL != texture);
198 if (!sampler.getMatrix().isIdentity()) {
199 return false;
200 }
201 GrGLTexture::Orientation orientation = texture->orientation();
202 if (GrGLTexture::kBottomUp_Orientation == orientation) {
203 return false;
204 } else {
205 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
206 }
207 return true;
208}
209
210///////////////////////////////////////////////////////////////////////////////
211
212void GrGpuGL::flushTextureMatrixAndDomain(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000213 const GrDrawState& drawState = this->getDrawState();
214 const GrGLTexture* texture =
215 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000216 if (NULL != texture) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000217
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000218 bool orientationChange = fCurrentProgram->fTextureOrientation[s] !=
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000219 texture->orientation();
220
221 const GrGLint& matrixUni =
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000222 fCurrentProgram->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000223
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000224 const GrMatrix& hwMatrix = fCurrentProgram->fTextureMatrices[s];
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000225 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000226
227 if (GrGLProgram::kUnusedUniform != matrixUni &&
228 (orientationChange || !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000229
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000230 GrMatrix m = samplerMatrix;
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000231 AdjustTextureMatrix(texture, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000232
bsalomon@google.com91961302011-05-09 18:39:58 +0000233 // ES doesn't allow you to pass true to the transpose param,
234 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000235 GrGLfloat mt[] = {
236 GrScalarToFloat(m[GrMatrix::kMScaleX]),
237 GrScalarToFloat(m[GrMatrix::kMSkewY]),
238 GrScalarToFloat(m[GrMatrix::kMPersp0]),
239 GrScalarToFloat(m[GrMatrix::kMSkewX]),
240 GrScalarToFloat(m[GrMatrix::kMScaleY]),
241 GrScalarToFloat(m[GrMatrix::kMPersp1]),
242 GrScalarToFloat(m[GrMatrix::kMTransX]),
243 GrScalarToFloat(m[GrMatrix::kMTransY]),
244 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000245 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000246
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000247 GL_CALL(UniformMatrix3fv(matrixUni, 1, false, mt));
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000248 fCurrentProgram->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000249 }
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000250
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000251 fCurrentProgram->fTextureOrientation[s] = texture->orientation();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000252 }
junov@google.comf93e7172011-03-31 21:26:24 +0000253}
254
junov@google.comf93e7172011-03-31 21:26:24 +0000255
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000256void GrGpuGL::flushColorMatrix() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000257 int matrixUni = fCurrentProgram->fUniLocations.fColorMatrixUni;
258 int vecUni = fCurrentProgram->fUniLocations.fColorMatrixVecUni;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000259 if (GrGLProgram::kUnusedUniform != matrixUni
260 && GrGLProgram::kUnusedUniform != vecUni) {
261 const float* m = this->getDrawState().getColorMatrix();
262 GrGLfloat mt[] = {
263 m[0], m[5], m[10], m[15],
264 m[1], m[6], m[11], m[16],
265 m[2], m[7], m[12], m[17],
266 m[3], m[8], m[13], m[18],
267 };
268 static float scale = 1.0f / 255.0f;
269 GrGLfloat vec[] = {
270 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
271 };
272 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
273 GL_CALL(Uniform4fv(vecUni, 1, vec));
274 }
275}
276
Scroggo01b87ec2011-05-11 18:05:38 +0000277static const float ONE_OVER_255 = 1.f / 255.f;
278
279#define GR_COLOR_TO_VEC4(color) {\
280 GrColorUnpackR(color) * ONE_OVER_255,\
281 GrColorUnpackG(color) * ONE_OVER_255,\
282 GrColorUnpackB(color) * ONE_OVER_255,\
283 GrColorUnpackA(color) * ONE_OVER_255 \
284}
285
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000286void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000287 const ProgramDesc& desc = fCurrentProgram->getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000288 const GrDrawState& drawState = this->getDrawState();
289
bsalomon@google.come79c8152012-03-29 19:07:12 +0000290 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000291 // color will be specified per-vertex as an attribute
292 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000293 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000294 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000295 switch (desc.fColorInput) {
296 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000297 if (fHWConstAttribColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000298 // OpenGL ES only supports the float varieties of
299 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000300 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000301 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
302 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000303 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000304 }
305 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000306 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000307 if (fCurrentProgram->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000308 // OpenGL ES doesn't support unsigned byte varieties of
309 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000310 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000311 GrAssert(GrGLProgram::kUnusedUniform !=
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000312 fCurrentProgram->fUniLocations.fColorUni);
313 GL_CALL(Uniform4fv(fCurrentProgram->fUniLocations.fColorUni, 1, c));
314 fCurrentProgram->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000315 }
316 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000317 case ProgramDesc::kSolidWhite_ColorInput:
318 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000319 break;
320 default:
321 GrCrash("Unknown color type.");
322 }
323 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000324 if (fCurrentProgram->fUniLocations.fColorFilterUni
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000325 != GrGLProgram::kUnusedUniform
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000326 && fCurrentProgram->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000327 != drawState.getColorFilterColor()) {
328 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000329 GL_CALL(Uniform4fv(fCurrentProgram->fUniLocations.fColorFilterUni, 1, c));
330 fCurrentProgram->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000331 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000332}
333
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000334void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000335 const ProgramDesc& desc = fCurrentProgram->getDesc();
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000336 // const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000337
338
bsalomon@google.come79c8152012-03-29 19:07:12 +0000339 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000340 // coverage will be specified per-vertex as an attribute
341 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000342 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000343 } else {
344 switch (desc.fCoverageInput) {
345 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000346 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000347 // OpenGL ES only supports the float varieties of
348 // glVertexAttrib
349 float c[] = GR_COLOR_TO_VEC4(coverage);
350 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
351 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000352 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000353 }
354 break;
355 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000356 if (fCurrentProgram->fCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000357 // OpenGL ES doesn't support unsigned byte varieties of
358 // glUniform
359 float c[] = GR_COLOR_TO_VEC4(coverage);
360 GrAssert(GrGLProgram::kUnusedUniform !=
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000361 fCurrentProgram->fUniLocations.fCoverageUni);
362 GL_CALL(Uniform4fv(fCurrentProgram->fUniLocations.fCoverageUni, 1, c));
363 fCurrentProgram->fCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000364 }
365 break;
366 case ProgramDesc::kSolidWhite_ColorInput:
367 case ProgramDesc::kTransBlack_ColorInput:
368 break;
369 default:
370 GrCrash("Unknown coverage type.");
371 }
372 }
373}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000374
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000375bool GrGpuGL::flushGraphicsState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000376 const GrDrawState& drawState = this->getDrawState();
377
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000378 // GrGpu::setupClipAndFlushState should have already checked this
379 // and bailed if not true.
380 GrAssert(NULL != drawState.getRenderTarget());
381
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000382 if (kStencilPath_DrawType != type) {
383 this->flushMiscFixedFunctionState();
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000384
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000385 GrBlendCoeff srcCoeff;
386 GrBlendCoeff dstCoeff;
387 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
388 if (kSkipDraw_BlendOptFlag & blendOpts) {
389 return false;
390 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000391
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000392 GrCustomStage* customStages [GrDrawState::kNumStages];
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000393 GrGLProgram::Desc desc;
394 this->buildProgram(kDrawPoints_DrawType == type, blendOpts, dstCoeff, customStages, &desc);
395
396 fCurrentProgram.reset(fProgramCache->getProgram(desc, customStages));
397 if (NULL == fCurrentProgram.get()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000398 GrAssert(!"Failed to create program!");
399 return false;
400 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000401 fCurrentProgram.get()->ref();
junov@google.comf93e7172011-03-31 21:26:24 +0000402
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000403 if (fHWProgramID != fCurrentProgram->fProgramID) {
404 GL_CALL(UseProgram(fCurrentProgram->fProgramID));
405 fHWProgramID = fCurrentProgram->fProgramID;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000406 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000407 fCurrentProgram->overrideBlend(&srcCoeff, &dstCoeff);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000408 this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000409
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000410 GrColor color;
411 GrColor coverage;
412 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
413 color = 0;
414 coverage = 0;
415 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
416 color = 0xffffffff;
417 coverage = drawState.getCoverage();
418 } else {
419 color = drawState.getColor();
420 coverage = drawState.getCoverage();
421 }
422 this->flushColor(color);
423 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000424
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000425 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
426 if (this->isStageEnabled(s)) {
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000427#if GR_DEBUG
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000428 // check for circular rendering
429 GrAssert(NULL == drawState.getRenderTarget() ||
430 NULL == drawState.getTexture(s) ||
431 drawState.getTexture(s)->asRenderTarget() !=
432 drawState.getRenderTarget());
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000433#endif
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000434 this->flushBoundTextureAndParams(s);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000435
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000436 this->flushTextureMatrixAndDomain(s);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000437
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000438 if (NULL != fCurrentProgram->fProgramStage[s]) {
439 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
440 const GrGLTexture* texture = static_cast<const GrGLTexture*>(
441 this->getDrawState().getTexture(s));
442 fCurrentProgram->fProgramStage[s]->setData(this->glInterface(),
443 *sampler.getCustomStage(),
444 drawState.getRenderTarget(), s);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000445 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000446 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000447 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000448 this->flushColorMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000449 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000450 this->flushStencil(type);
451 this->flushViewMatrix(type);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000452 this->flushScissor();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000453 this->flushAAState(type);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000454
455 GrIRect* rect = NULL;
456 GrIRect clipBounds;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000457 if (drawState.isClipState()) {
bsalomon@google.com4c883782012-06-04 19:05:11 +0000458 fClip.getConservativeBounds().roundOut(&clipBounds);
459 rect = &clipBounds;
460 }
461 // This must come after textures are flushed because a texture may need
462 // to be msaa-resolved (which will modify bound FBO state).
463 this->flushRenderTarget(rect);
464
junov@google.comf93e7172011-03-31 21:26:24 +0000465 return true;
466}
467
bsalomon@google.com2717d562012-05-07 19:10:52 +0000468#if GR_TEXT_SCALAR_IS_USHORT
469 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
470 #define TEXT_COORDS_ARE_NORMALIZED 1
471#elif GR_TEXT_SCALAR_IS_FLOAT
472 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
473 #define TEXT_COORDS_ARE_NORMALIZED 0
474#elif GR_TEXT_SCALAR_IS_FIXED
475 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
476 #define TEXT_COORDS_ARE_NORMALIZED 0
477#else
478 #error "unknown GR_TEXT_SCALAR type"
479#endif
480
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000481void GrGpuGL::setupGeometry(int* startVertex,
bsalomon@google.com49209392012-06-05 15:13:46 +0000482 int* startIndex,
483 int vertexCount,
484 int indexCount) {
junov@google.comf93e7172011-03-31 21:26:24 +0000485
486 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000487 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000488 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000489 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000490
bsalomon@google.come79c8152012-03-29 19:07:12 +0000491 GrVertexLayout currLayout = this->getVertexLayout();
492
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000493 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000494 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000495 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000496 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000497 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000498 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000499 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000500 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000501 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000502 int oldEdgeOffset;
503
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000504 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
505 fHWGeometryState.fVertexLayout,
506 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000507 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000508 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000509 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000510 bool indexed = NULL != startIndex;
511
512 int extraVertexOffset;
513 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000514 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000515
516 GrGLenum scalarType;
517 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000518 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000519 scalarType = TEXT_COORDS_GL_TYPE;
520 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000521 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000522 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
523 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000524 texCoordNorm = false;
525 }
526
527 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
528 *startVertex = 0;
529 if (indexed) {
530 *startIndex += extraIndexOffset;
531 }
532
533 // all the Pointers must be set if any of these are true
534 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
535 vertexOffset != fHWGeometryState.fVertexOffset ||
536 newStride != oldStride;
537
538 // position and tex coord offsets change if above conditions are true
539 // or the type/normalization changed based on text vs nontext type coords.
540 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000541 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000542 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000543 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000544
545 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000546 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000547 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000548 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000549 fHWGeometryState.fVertexOffset = vertexOffset;
550 }
551
tomhudson@google.com93813632011-10-27 20:21:16 +0000552 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000553 if (newTexCoordOffsets[t] > 0) {
554 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000555 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000556 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000557 GL_CALL(EnableVertexAttribArray(idx));
558 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000559 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000560 } else if (posAndTexChange ||
561 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000562 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000563 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000564 }
565 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000566 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000567 }
568 }
569
570 if (newColorOffset > 0) {
571 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000572 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000573 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000574 GL_CALL(EnableVertexAttribArray(idx));
575 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000576 true, newStride, colorOffset));
577 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000578 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000579 true, newStride, colorOffset));
580 }
581 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000582 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000583 }
584
bsalomon@google.coma3108262011-10-10 14:08:47 +0000585 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000586 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000587 int idx = GrGLProgram::CoverageAttributeIdx();
588 if (oldCoverageOffset <= 0) {
589 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000590 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000591 true, newStride, coverageOffset));
592 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000593 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000594 true, newStride, coverageOffset));
595 }
596 } else if (oldCoverageOffset > 0) {
597 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
598 }
599
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000600 if (newEdgeOffset > 0) {
601 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
602 int idx = GrGLProgram::EdgeAttributeIdx();
603 if (oldEdgeOffset <= 0) {
604 GL_CALL(EnableVertexAttribArray(idx));
605 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
606 false, newStride, edgeOffset));
607 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
608 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
609 false, newStride, edgeOffset));
610 }
611 } else if (oldEdgeOffset > 0) {
612 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
613 }
614
bsalomon@google.come79c8152012-03-29 19:07:12 +0000615 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000616 fHWGeometryState.fArrayPtrsDirty = false;
617}
618
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000619namespace {
620
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000621void setup_custom_stage(GrGLProgram::Desc::StageDesc* stage,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000622 const GrSamplerState& sampler,
623 GrCustomStage** customStages,
624 GrGLProgram* program, int index) {
625 GrCustomStage* customStage = sampler.getCustomStage();
626 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000627 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000628 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000629 customStages[index] = customStage;
630 } else {
631 stage->fCustomStageKey = 0;
632 customStages[index] = NULL;
633 }
634}
635
636}
637
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000638void GrGpuGL::buildProgram(bool isPoints,
bsalomon@google.com49209392012-06-05 15:13:46 +0000639 BlendOptFlags blendOpts,
640 GrBlendCoeff dstCoeff,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000641 GrCustomStage** customStages,
642 ProgramDesc* desc) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000643 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000644
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000645 // This should already have been caught
646 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
647
648 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
649
650 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
651 kEmitCoverage_BlendOptFlag));
652
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000653 // The descriptor is used as a cache key. Thus when a field of the
654 // descriptor will not affect program generation (because of the vertex
655 // layout in use or other descriptor field settings) it should be set
656 // to a canonical value to avoid duplicate programs with different keys.
657
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000658 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000659 desc->fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000660
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000661 desc->fEmitsPointSize = isPoints;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000662
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000663 bool requiresAttributeColors =
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000664 !skipColor && SkToBool(desc->fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000665 bool requiresAttributeCoverage =
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000666 !skipCoverage && SkToBool(desc->fVertexLayout &
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000667 kCoverage_VertexLayoutBit);
668
669 // fColorInput/fCoverageInput records how colors are specified for the.
670 // program. So we strip the bits from the layout to avoid false negatives
671 // when searching for an existing program in the cache.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000672 desc->fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000673
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000674 desc->fColorFilterXfermode = skipColor ?
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000675 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000676 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000677
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000678 desc->fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000679
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000680 // no reason to do edge aa or look at per-vertex coverage if coverage is
681 // ignored
682 if (skipCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000683 desc->fVertexLayout &= ~(kEdge_VertexLayoutBit |
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000684 kCoverage_VertexLayoutBit);
685 }
686
687 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
688 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
689 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000690 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000691 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000692 desc->fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000693 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000694 desc->fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000695 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000696 desc->fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000697 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000698 desc->fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000699 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000700
701 bool covIsSolidWhite = !requiresAttributeCoverage &&
702 0xffffffff == drawState.getCoverage();
703
704 if (skipCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000705 desc->fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000706 } else if (covIsSolidWhite) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000707 desc->fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000708 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000709 desc->fCoverageInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000710 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000711 desc->fCoverageInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000712 }
junov@google.comf93e7172011-03-31 21:26:24 +0000713
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000714 int lastEnabledStage = -1;
715
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000716 if (!skipCoverage && (desc->fVertexLayout &
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000717 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000718 desc->fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000719 } else {
720 // use canonical value when not set to avoid cache misses
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000721 desc->fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000722 }
723
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000724 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000725 StageDesc& stage = desc->fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000726
727 stage.fOptFlags = 0;
728 stage.setEnabled(this->isStageEnabled(s));
729
730 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
731 skipCoverage;
732
733 if (!skip && stage.isEnabled()) {
734 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000735 const GrGLTexture* texture =
736 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000737 GrAssert(NULL != texture);
738 const GrSamplerState& sampler = drawState.getSampler(s);
739 // we matrix to invert when orientation is TopDown, so make sure
740 // we aren't in that case before flagging as identity.
741 if (TextureMatrixIsIdentity(texture, sampler)) {
742 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
743 } else if (!sampler.getMatrix().hasPerspective()) {
744 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
745 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000746
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000747 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000748 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000749 if (GrPixelConfigIsAlphaOnly(texture->config())) {
750 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000751 // the shader must smear the single channel after
752 // reading the texture
753 if (this->glCaps().textureRedSupport()) {
754 // we can use R8 textures so use kSmearRed
755 stage.fInConfigFlags |=
756 StageDesc::kSmearRed_InConfigFlag;
757 } else {
758 // we can use A8 textures so use kSmearAlpha
759 stage.fInConfigFlags |=
760 StageDesc::kSmearAlpha_InConfigFlag;
761 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000762 } else if (sampler.swapsRAndB()) {
763 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
764 }
765 }
766 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000767 // The shader generator assumes that color channels are bytes
768 // when rounding.
769 GrAssert(4 == GrBytesPerPixel(texture->config()));
770 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
771 fUnpremulConversion) {
772 stage.fInConfigFlags |=
773 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
774 } else {
775 stage.fInConfigFlags |=
776 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
777 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000778 }
779
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000780 setup_custom_stage(&stage, sampler, customStages, fCurrentProgram.get(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000781
junov@google.comf93e7172011-03-31 21:26:24 +0000782 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000783 stage.fOptFlags = 0;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000784 stage.fInConfigFlags = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000785 stage.fCustomStageKey = 0;
786 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000787 }
788 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000789
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000790 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000791 // The shader generator assumes that color channels are bytes
792 // when rounding.
793 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
794 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000795 desc->fOutputConfig =
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000796 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
797 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000798 desc->fOutputConfig =
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000799 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
800 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000801 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000802 desc->fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000803 }
804
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000805 desc->fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000806
807 // currently the experimental GS will only work with triangle prims
808 // (and it doesn't do anything other than pass through values from
809 // the VS to the FS anyway).
810#if 0 && GR_GL_EXPERIMENTAL_GS
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000811 desc->fExperimentalGS = this->getCaps().fGeometryShaderSupport;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000812#endif
813
bsalomon@google.coma3108262011-10-10 14:08:47 +0000814 // we want to avoid generating programs with different "first cov stage"
815 // values when they would compute the same result.
816 // We set field in the desc to kNumStages when either there are no
817 // coverage stages or the distinction between coverage and color is
818 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000819 int firstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000820 desc->fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000821 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000822 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000823 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000824 }
825
826 // other coverage inputs
827 if (!hasCoverage) {
828 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000829 requiresAttributeCoverage ||
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000830 (desc->fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000831 }
832
833 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000834 // color filter is applied between color/coverage computation
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000835 if (SkXfermode::kDst_Mode != desc->fColorFilterXfermode) {
836 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000837 }
838
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000839 if (this->getCaps().fDualSourceBlendingSupport &&
840 !(blendOpts & (kEmitCoverage_BlendOptFlag |
841 kCoverageAsAlpha_BlendOptFlag))) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000842 if (kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000843 // write the coverage value to second color
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000844 desc->fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
845 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000846 } else if (kSA_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000847 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
848 // cover
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000849 desc->fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
850 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000851 } else if (kSC_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000852 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
853 // cover
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000854 desc->fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
855 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000856 }
857 }
858 }
junov@google.comf93e7172011-03-31 21:26:24 +0000859}