blob: 077f80772a5cfaac4df1b3e393e11327ea900566 [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 const GrGLint& domUni = fCurrentProgram->fUniLocations.fStages[s].fTexDomUni;
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000252 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
253 if (GrGLProgram::kUnusedUniform != domUni &&
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000254 (orientationChange ||fCurrentProgram->fTextureDomain[s] != texDom)) {
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000255
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000256 fCurrentProgram->fTextureDomain[s] = texDom;
bsalomon@google.com890e3b52012-06-01 19:01:37 +0000257
258 float values[4] = {
259 GrScalarToFloat(texDom.left()),
260 GrScalarToFloat(texDom.top()),
261 GrScalarToFloat(texDom.right()),
262 GrScalarToFloat(texDom.bottom())
263 };
264
265 // vertical flip if necessary
266 if (GrGLTexture::kBottomUp_Orientation == texture->orientation()) {
267 values[1] = 1.0f - values[1];
268 values[3] = 1.0f - values[3];
269 // The top and bottom were just flipped, so correct the ordering
270 // of elements so that values = (l, t, r, b).
271 SkTSwap(values[1], values[3]);
272 }
273 GL_CALL(Uniform4fv(domUni, 1, values));
274 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000275 fCurrentProgram->fTextureOrientation[s] = texture->orientation();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000276 }
junov@google.comf93e7172011-03-31 21:26:24 +0000277}
278
junov@google.comf93e7172011-03-31 21:26:24 +0000279
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000280void GrGpuGL::flushColorMatrix() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000281 int matrixUni = fCurrentProgram->fUniLocations.fColorMatrixUni;
282 int vecUni = fCurrentProgram->fUniLocations.fColorMatrixVecUni;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000283 if (GrGLProgram::kUnusedUniform != matrixUni
284 && GrGLProgram::kUnusedUniform != vecUni) {
285 const float* m = this->getDrawState().getColorMatrix();
286 GrGLfloat mt[] = {
287 m[0], m[5], m[10], m[15],
288 m[1], m[6], m[11], m[16],
289 m[2], m[7], m[12], m[17],
290 m[3], m[8], m[13], m[18],
291 };
292 static float scale = 1.0f / 255.0f;
293 GrGLfloat vec[] = {
294 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
295 };
296 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
297 GL_CALL(Uniform4fv(vecUni, 1, vec));
298 }
299}
300
Scroggo01b87ec2011-05-11 18:05:38 +0000301static const float ONE_OVER_255 = 1.f / 255.f;
302
303#define GR_COLOR_TO_VEC4(color) {\
304 GrColorUnpackR(color) * ONE_OVER_255,\
305 GrColorUnpackG(color) * ONE_OVER_255,\
306 GrColorUnpackB(color) * ONE_OVER_255,\
307 GrColorUnpackA(color) * ONE_OVER_255 \
308}
309
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000310void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000311 const ProgramDesc& desc = fCurrentProgram->getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000312 const GrDrawState& drawState = this->getDrawState();
313
bsalomon@google.come79c8152012-03-29 19:07:12 +0000314 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000315 // color will be specified per-vertex as an attribute
316 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000317 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000318 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000319 switch (desc.fColorInput) {
320 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000321 if (fHWConstAttribColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000322 // OpenGL ES only supports the float varieties of
323 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000324 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000325 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
326 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000327 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000328 }
329 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000330 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000331 if (fCurrentProgram->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000332 // OpenGL ES doesn't support unsigned byte varieties of
333 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000334 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000335 GrAssert(GrGLProgram::kUnusedUniform !=
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000336 fCurrentProgram->fUniLocations.fColorUni);
337 GL_CALL(Uniform4fv(fCurrentProgram->fUniLocations.fColorUni, 1, c));
338 fCurrentProgram->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000339 }
340 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000341 case ProgramDesc::kSolidWhite_ColorInput:
342 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000343 break;
344 default:
345 GrCrash("Unknown color type.");
346 }
347 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000348 if (fCurrentProgram->fUniLocations.fColorFilterUni
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000349 != GrGLProgram::kUnusedUniform
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000350 && fCurrentProgram->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000351 != drawState.getColorFilterColor()) {
352 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000353 GL_CALL(Uniform4fv(fCurrentProgram->fUniLocations.fColorFilterUni, 1, c));
354 fCurrentProgram->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000355 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000356}
357
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000358void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000359 const ProgramDesc& desc = fCurrentProgram->getDesc();
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000360 // const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000361
362
bsalomon@google.come79c8152012-03-29 19:07:12 +0000363 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000364 // coverage will be specified per-vertex as an attribute
365 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000366 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000367 } else {
368 switch (desc.fCoverageInput) {
369 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000370 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000371 // OpenGL ES only supports the float varieties of
372 // glVertexAttrib
373 float c[] = GR_COLOR_TO_VEC4(coverage);
374 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
375 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000376 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000377 }
378 break;
379 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000380 if (fCurrentProgram->fCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000381 // OpenGL ES doesn't support unsigned byte varieties of
382 // glUniform
383 float c[] = GR_COLOR_TO_VEC4(coverage);
384 GrAssert(GrGLProgram::kUnusedUniform !=
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000385 fCurrentProgram->fUniLocations.fCoverageUni);
386 GL_CALL(Uniform4fv(fCurrentProgram->fUniLocations.fCoverageUni, 1, c));
387 fCurrentProgram->fCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000388 }
389 break;
390 case ProgramDesc::kSolidWhite_ColorInput:
391 case ProgramDesc::kTransBlack_ColorInput:
392 break;
393 default:
394 GrCrash("Unknown coverage type.");
395 }
396 }
397}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000398
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000399bool GrGpuGL::flushGraphicsState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000400 const GrDrawState& drawState = this->getDrawState();
401
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000402 // GrGpu::setupClipAndFlushState should have already checked this
403 // and bailed if not true.
404 GrAssert(NULL != drawState.getRenderTarget());
405
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000406 if (kStencilPath_DrawType != type) {
407 this->flushMiscFixedFunctionState();
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000408
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000409 GrBlendCoeff srcCoeff;
410 GrBlendCoeff dstCoeff;
411 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
412 if (kSkipDraw_BlendOptFlag & blendOpts) {
413 return false;
414 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000415
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000416 GrCustomStage* customStages [GrDrawState::kNumStages];
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000417 GrGLProgram::Desc desc;
418 this->buildProgram(kDrawPoints_DrawType == type, blendOpts, dstCoeff, customStages, &desc);
419
420 fCurrentProgram.reset(fProgramCache->getProgram(desc, customStages));
421 if (NULL == fCurrentProgram.get()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000422 GrAssert(!"Failed to create program!");
423 return false;
424 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000425 fCurrentProgram.get()->ref();
junov@google.comf93e7172011-03-31 21:26:24 +0000426
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000427 if (fHWProgramID != fCurrentProgram->fProgramID) {
428 GL_CALL(UseProgram(fCurrentProgram->fProgramID));
429 fHWProgramID = fCurrentProgram->fProgramID;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000430 }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000431 fCurrentProgram->overrideBlend(&srcCoeff, &dstCoeff);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000432 this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000433
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000434 GrColor color;
435 GrColor coverage;
436 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
437 color = 0;
438 coverage = 0;
439 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
440 color = 0xffffffff;
441 coverage = drawState.getCoverage();
442 } else {
443 color = drawState.getColor();
444 coverage = drawState.getCoverage();
445 }
446 this->flushColor(color);
447 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000448
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000449 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
450 if (this->isStageEnabled(s)) {
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000451#if GR_DEBUG
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000452 // check for circular rendering
453 GrAssert(NULL == drawState.getRenderTarget() ||
454 NULL == drawState.getTexture(s) ||
455 drawState.getTexture(s)->asRenderTarget() !=
456 drawState.getRenderTarget());
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000457#endif
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000458 this->flushBoundTextureAndParams(s);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000459
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000460 this->flushTextureMatrixAndDomain(s);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000461
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000462 if (NULL != fCurrentProgram->fProgramStage[s]) {
463 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
464 const GrGLTexture* texture = static_cast<const GrGLTexture*>(
465 this->getDrawState().getTexture(s));
466 fCurrentProgram->fProgramStage[s]->setData(this->glInterface(),
467 *sampler.getCustomStage(),
468 drawState.getRenderTarget(), s);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000469 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000470 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000471 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000472 this->flushColorMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000473 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000474 this->flushStencil(type);
475 this->flushViewMatrix(type);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000476 this->flushScissor();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000477 this->flushAAState(type);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000478
479 GrIRect* rect = NULL;
480 GrIRect clipBounds;
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000481 if (drawState.isClipState()) {
bsalomon@google.com4c883782012-06-04 19:05:11 +0000482 fClip.getConservativeBounds().roundOut(&clipBounds);
483 rect = &clipBounds;
484 }
485 // This must come after textures are flushed because a texture may need
486 // to be msaa-resolved (which will modify bound FBO state).
487 this->flushRenderTarget(rect);
488
junov@google.comf93e7172011-03-31 21:26:24 +0000489 return true;
490}
491
bsalomon@google.com2717d562012-05-07 19:10:52 +0000492#if GR_TEXT_SCALAR_IS_USHORT
493 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
494 #define TEXT_COORDS_ARE_NORMALIZED 1
495#elif GR_TEXT_SCALAR_IS_FLOAT
496 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
497 #define TEXT_COORDS_ARE_NORMALIZED 0
498#elif GR_TEXT_SCALAR_IS_FIXED
499 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
500 #define TEXT_COORDS_ARE_NORMALIZED 0
501#else
502 #error "unknown GR_TEXT_SCALAR type"
503#endif
504
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000505void GrGpuGL::setupGeometry(int* startVertex,
bsalomon@google.com49209392012-06-05 15:13:46 +0000506 int* startIndex,
507 int vertexCount,
508 int indexCount) {
junov@google.comf93e7172011-03-31 21:26:24 +0000509
510 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000511 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000512 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000513 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000514
bsalomon@google.come79c8152012-03-29 19:07:12 +0000515 GrVertexLayout currLayout = this->getVertexLayout();
516
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000517 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000518 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000519 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000520 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000521 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000522 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000523 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000524 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000525 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000526 int oldEdgeOffset;
527
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000528 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
529 fHWGeometryState.fVertexLayout,
530 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000531 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000532 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000533 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000534 bool indexed = NULL != startIndex;
535
536 int extraVertexOffset;
537 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000538 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000539
540 GrGLenum scalarType;
541 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000542 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000543 scalarType = TEXT_COORDS_GL_TYPE;
544 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000545 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000546 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
547 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000548 texCoordNorm = false;
549 }
550
551 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
552 *startVertex = 0;
553 if (indexed) {
554 *startIndex += extraIndexOffset;
555 }
556
557 // all the Pointers must be set if any of these are true
558 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
559 vertexOffset != fHWGeometryState.fVertexOffset ||
560 newStride != oldStride;
561
562 // position and tex coord offsets change if above conditions are true
563 // or the type/normalization changed based on text vs nontext type coords.
564 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000565 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000566 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000567 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000568
569 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000570 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000571 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000572 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000573 fHWGeometryState.fVertexOffset = vertexOffset;
574 }
575
tomhudson@google.com93813632011-10-27 20:21:16 +0000576 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000577 if (newTexCoordOffsets[t] > 0) {
578 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000579 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000580 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000581 GL_CALL(EnableVertexAttribArray(idx));
582 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000583 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000584 } else if (posAndTexChange ||
585 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000586 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000587 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000588 }
589 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000590 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000591 }
592 }
593
594 if (newColorOffset > 0) {
595 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000596 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000597 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000598 GL_CALL(EnableVertexAttribArray(idx));
599 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000600 true, newStride, colorOffset));
601 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000602 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000603 true, newStride, colorOffset));
604 }
605 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000606 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000607 }
608
bsalomon@google.coma3108262011-10-10 14:08:47 +0000609 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000610 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000611 int idx = GrGLProgram::CoverageAttributeIdx();
612 if (oldCoverageOffset <= 0) {
613 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000614 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000615 true, newStride, coverageOffset));
616 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000617 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000618 true, newStride, coverageOffset));
619 }
620 } else if (oldCoverageOffset > 0) {
621 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
622 }
623
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000624 if (newEdgeOffset > 0) {
625 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
626 int idx = GrGLProgram::EdgeAttributeIdx();
627 if (oldEdgeOffset <= 0) {
628 GL_CALL(EnableVertexAttribArray(idx));
629 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
630 false, newStride, edgeOffset));
631 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
632 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
633 false, newStride, edgeOffset));
634 }
635 } else if (oldEdgeOffset > 0) {
636 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
637 }
638
bsalomon@google.come79c8152012-03-29 19:07:12 +0000639 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000640 fHWGeometryState.fArrayPtrsDirty = false;
641}
642
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000643namespace {
644
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000645void setup_custom_stage(GrGLProgram::Desc::StageDesc* stage,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000646 const GrSamplerState& sampler,
647 GrCustomStage** customStages,
648 GrGLProgram* program, int index) {
649 GrCustomStage* customStage = sampler.getCustomStage();
650 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000651 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000652 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000653 customStages[index] = customStage;
654 } else {
655 stage->fCustomStageKey = 0;
656 customStages[index] = NULL;
657 }
658}
659
660}
661
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000662void GrGpuGL::buildProgram(bool isPoints,
bsalomon@google.com49209392012-06-05 15:13:46 +0000663 BlendOptFlags blendOpts,
664 GrBlendCoeff dstCoeff,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000665 GrCustomStage** customStages,
666 ProgramDesc* desc) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000667 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000668
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000669 // This should already have been caught
670 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
671
672 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
673
674 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
675 kEmitCoverage_BlendOptFlag));
676
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000677 // The descriptor is used as a cache key. Thus when a field of the
678 // descriptor will not affect program generation (because of the vertex
679 // layout in use or other descriptor field settings) it should be set
680 // to a canonical value to avoid duplicate programs with different keys.
681
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000682 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000683 desc->fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000684
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000685 desc->fEmitsPointSize = isPoints;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000686
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000687 bool requiresAttributeColors =
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000688 !skipColor && SkToBool(desc->fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000689 bool requiresAttributeCoverage =
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000690 !skipCoverage && SkToBool(desc->fVertexLayout &
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000691 kCoverage_VertexLayoutBit);
692
693 // fColorInput/fCoverageInput records how colors are specified for the.
694 // program. So we strip the bits from the layout to avoid false negatives
695 // when searching for an existing program in the cache.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000696 desc->fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000697
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000698 desc->fColorFilterXfermode = skipColor ?
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000699 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000700 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000701
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000702 desc->fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000703
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000704 // no reason to do edge aa or look at per-vertex coverage if coverage is
705 // ignored
706 if (skipCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000707 desc->fVertexLayout &= ~(kEdge_VertexLayoutBit |
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000708 kCoverage_VertexLayoutBit);
709 }
710
711 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
712 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
713 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000714 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000715 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000716 desc->fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000717 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000718 desc->fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000719 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000720 desc->fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000721 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000722 desc->fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000723 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000724
725 bool covIsSolidWhite = !requiresAttributeCoverage &&
726 0xffffffff == drawState.getCoverage();
727
728 if (skipCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000729 desc->fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000730 } else if (covIsSolidWhite) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000731 desc->fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000732 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000733 desc->fCoverageInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000734 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000735 desc->fCoverageInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000736 }
junov@google.comf93e7172011-03-31 21:26:24 +0000737
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000738 int lastEnabledStage = -1;
739
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000740 if (!skipCoverage && (desc->fVertexLayout &
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000741 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000742 desc->fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000743 } else {
744 // use canonical value when not set to avoid cache misses
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000745 desc->fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000746 }
747
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000748 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000749 StageDesc& stage = desc->fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000750
751 stage.fOptFlags = 0;
752 stage.setEnabled(this->isStageEnabled(s));
753
754 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
755 skipCoverage;
756
757 if (!skip && stage.isEnabled()) {
758 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000759 const GrGLTexture* texture =
760 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000761 GrAssert(NULL != texture);
762 const GrSamplerState& sampler = drawState.getSampler(s);
763 // we matrix to invert when orientation is TopDown, so make sure
764 // we aren't in that case before flagging as identity.
765 if (TextureMatrixIsIdentity(texture, sampler)) {
766 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
767 } else if (!sampler.getMatrix().hasPerspective()) {
768 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
769 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000770
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000771 if (sampler.hasTextureDomain()) {
772 GrAssert(GrSamplerState::kClamp_WrapMode ==
773 sampler.getWrapX() &&
774 GrSamplerState::kClamp_WrapMode ==
775 sampler.getWrapY());
776 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
777 }
778
779 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000780 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000781 if (GrPixelConfigIsAlphaOnly(texture->config())) {
782 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000783 // the shader must smear the single channel after
784 // reading the texture
785 if (this->glCaps().textureRedSupport()) {
786 // we can use R8 textures so use kSmearRed
787 stage.fInConfigFlags |=
788 StageDesc::kSmearRed_InConfigFlag;
789 } else {
790 // we can use A8 textures so use kSmearAlpha
791 stage.fInConfigFlags |=
792 StageDesc::kSmearAlpha_InConfigFlag;
793 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000794 } else if (sampler.swapsRAndB()) {
795 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
796 }
797 }
798 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000799 // The shader generator assumes that color channels are bytes
800 // when rounding.
801 GrAssert(4 == GrBytesPerPixel(texture->config()));
802 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
803 fUnpremulConversion) {
804 stage.fInConfigFlags |=
805 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
806 } else {
807 stage.fInConfigFlags |=
808 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
809 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000810 }
811
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000812 setup_custom_stage(&stage, sampler, customStages, fCurrentProgram.get(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000813
junov@google.comf93e7172011-03-31 21:26:24 +0000814 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000815 stage.fOptFlags = 0;
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000816 stage.fInConfigFlags = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000817 stage.fCustomStageKey = 0;
818 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +0000819 }
820 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000821
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000822 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000823 // The shader generator assumes that color channels are bytes
824 // when rounding.
825 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
826 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000827 desc->fOutputConfig =
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000828 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
829 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000830 desc->fOutputConfig =
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000831 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
832 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000833 } else {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000834 desc->fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000835 }
836
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000837 desc->fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000838
839 // currently the experimental GS will only work with triangle prims
840 // (and it doesn't do anything other than pass through values from
841 // the VS to the FS anyway).
842#if 0 && GR_GL_EXPERIMENTAL_GS
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000843 desc->fExperimentalGS = this->getCaps().fGeometryShaderSupport;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000844#endif
845
bsalomon@google.coma3108262011-10-10 14:08:47 +0000846 // we want to avoid generating programs with different "first cov stage"
847 // values when they would compute the same result.
848 // We set field in the desc to kNumStages when either there are no
849 // coverage stages or the distinction between coverage and color is
850 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000851 int firstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000852 desc->fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000853 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000854 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000855 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +0000856 }
857
858 // other coverage inputs
859 if (!hasCoverage) {
860 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000861 requiresAttributeCoverage ||
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000862 (desc->fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000863 }
864
865 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000866 // color filter is applied between color/coverage computation
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000867 if (SkXfermode::kDst_Mode != desc->fColorFilterXfermode) {
868 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000869 }
870
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000871 if (this->getCaps().fDualSourceBlendingSupport &&
872 !(blendOpts & (kEmitCoverage_BlendOptFlag |
873 kCoverageAsAlpha_BlendOptFlag))) {
bsalomon@google.com47059542012-06-06 20:51:20 +0000874 if (kZero_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000875 // write the coverage value to second color
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000876 desc->fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
877 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000878 } else if (kSA_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000879 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
880 // cover
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000881 desc->fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
882 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com47059542012-06-06 20:51:20 +0000883 } else if (kSC_GrBlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000884 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
885 // cover
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000886 desc->fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
887 desc->fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000888 }
889 }
890 }
junov@google.comf93e7172011-03-31 21:26:24 +0000891}