blob: 5afd77e4055cc5c61be8f74bb21ec02ef6bf395e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
junov@google.comf93e7172011-03-31 21:26:24 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
junov@google.comf93e7172011-03-31 21:26:24 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
junov@google.comf93e7172011-03-31 21:26:24 +000010#include "GrBinHashKey.h"
junov@google.comf93e7172011-03-31 21:26:24 +000011#include "GrGLProgram.h"
12#include "GrGpuGLShaders.h"
13#include "GrGpuVertex.h"
junov@google.comf93e7172011-03-31 21:26:24 +000014#include "GrNoncopyable.h"
15#include "GrStringBuilder.h"
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000016#include "GrRandom.h"
junov@google.comf93e7172011-03-31 21:26:24 +000017
junov@google.comf93e7172011-03-31 21:26:24 +000018#define SKIP_CACHE_CHECK true
19#define GR_UINT32_MAX static_cast<uint32_t>(-1)
20
junov@google.comf93e7172011-03-31 21:26:24 +000021#include "GrTHashCache.h"
22
23class GrGpuGLShaders::ProgramCache : public ::GrNoncopyable {
24private:
25 class Entry;
26
27#if GR_DEBUG
28 typedef GrBinHashKey<Entry, 4> ProgramHashKey; // Flex the dynamic allocation muscle in debug
29#else
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000030 typedef GrBinHashKey<Entry, 64> ProgramHashKey;
junov@google.comf93e7172011-03-31 21:26:24 +000031#endif
32
33 class Entry : public ::GrNoncopyable {
34 public:
35 Entry() {}
junov@google.comf93e7172011-03-31 21:26:24 +000036 void copyAndTakeOwnership(Entry& entry) {
37 fProgramData.copyAndTakeOwnership(entry.fProgramData);
38 fKey.copyAndTakeOwnership(entry.fKey); // ownership transfer
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000039 fLRUStamp = entry.fLRUStamp;
junov@google.comf93e7172011-03-31 21:26:24 +000040 }
41
42 public:
43 int compare(const ProgramHashKey& key) const { return fKey.compare(key); }
44
45 public:
46 GrGLProgram::CachedData fProgramData;
47 ProgramHashKey fKey;
48 unsigned int fLRUStamp;
49 };
50
51 GrTHashTable<Entry, ProgramHashKey, 8> fHashCache;
52
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000053 // We may have kMaxEntries+1 shaders in the GL context because
54 // we create a new shader before evicting from the cache.
junov@google.comf93e7172011-03-31 21:26:24 +000055 enum {
56 kMaxEntries = 32
57 };
58 Entry fEntries[kMaxEntries];
59 int fCount;
60 unsigned int fCurrLRUStamp;
61
62public:
63 ProgramCache()
64 : fCount(0)
65 , fCurrLRUStamp(0) {
66 }
67
68 ~ProgramCache() {
69 for (int i = 0; i < fCount; ++i) {
70 GrGpuGLShaders::DeleteProgram(&fEntries[i].fProgramData);
71 }
72 }
73
74 void abandon() {
75 fCount = 0;
76 }
77
78 void invalidateViewMatrices() {
79 for (int i = 0; i < fCount; ++i) {
80 // set to illegal matrix
81 fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix();
82 }
83 }
84
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000085 GrGLProgram::CachedData* getProgramData(const GrGLProgram& desc) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000086 Entry newEntry;
87 while (newEntry.fKey.doPass()) {
88 desc.buildKey(newEntry.fKey);
junov@google.comf93e7172011-03-31 21:26:24 +000089 }
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000090 Entry* entry = fHashCache.find(newEntry.fKey);
junov@google.comf93e7172011-03-31 21:26:24 +000091 if (NULL == entry) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000092 if (!desc.genProgram(&newEntry.fProgramData)) {
93 return NULL;
94 }
junov@google.comf93e7172011-03-31 21:26:24 +000095 if (fCount < kMaxEntries) {
96 entry = fEntries + fCount;
97 ++fCount;
98 } else {
99 GrAssert(kMaxEntries == fCount);
100 entry = fEntries;
101 for (int i = 1; i < kMaxEntries; ++i) {
102 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
103 entry = fEntries + i;
104 }
105 }
106 fHashCache.remove(entry->fKey, entry);
107 GrGpuGLShaders::DeleteProgram(&entry->fProgramData);
108 }
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000109 entry->copyAndTakeOwnership(newEntry);
junov@google.comf93e7172011-03-31 21:26:24 +0000110 fHashCache.insert(entry->fKey, entry);
111 }
112
113 entry->fLRUStamp = fCurrLRUStamp;
114 if (GR_UINT32_MAX == fCurrLRUStamp) {
115 // wrap around! just trash our LRU, one time hit.
116 for (int i = 0; i < fCount; ++i) {
117 fEntries[i].fLRUStamp = 0;
118 }
119 }
120 ++fCurrLRUStamp;
121 return &entry->fProgramData;
122 }
123};
124
junov@google.com53a55842011-06-08 22:55:10 +0000125void GrGpuGLShaders::abandonResources(){
126 INHERITED::abandonResources();
127 fProgramCache->abandon();
128}
129
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000130void GrGpuGLShaders::DeleteProgram(CachedData* programData) {
junov@google.comf93e7172011-03-31 21:26:24 +0000131 GR_GL(DeleteShader(programData->fVShaderID));
132 GR_GL(DeleteShader(programData->fFShaderID));
133 GR_GL(DeleteProgram(programData->fProgramID));
134 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
135}
136
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000137////////////////////////////////////////////////////////////////////////////////
138
139namespace {
140 template <typename T>
141 T random_val(GrRandom* r, T count) {
142 return (T)(int)(r->nextF() * count);
143 }
144};
145
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000146bool GrGpuGLShaders::programUnitTest() {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000147
148 static const int STAGE_OPTS[] = {
149 0,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000150 StageDesc::kNoPerspective_OptFlagBit,
151 StageDesc::kIdentity_CoordMapping
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000152 };
153 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000154 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000155
156 static const int NUM_TESTS = 512;
157
158 // GrRandoms nextU() values have patterns in the low bits
159 // So using nextU() % array_count might never take some values.
160 GrRandom random;
161 for (int t = 0; t < NUM_TESTS; ++t) {
162
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000163#if 0
164 GrPrintf("\nTest Program %d\n-------------\n", t);
165 static const int stop = -1;
166 if (t == stop) {
167 int breakpointhere = 9;
168 }
169#endif
170
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000171 pdesc.fVertexLayout = 0;
172 pdesc.fEmitsPointSize = random.nextF() > .5f;
173 float colorType = random.nextF();
174 if (colorType < 1.f / 3.f) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000175 pdesc.fColorType = ProgramDesc::kAttribute_ColorType;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000176 } else if (colorType < 2.f / 3.f) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000177 pdesc.fColorType = ProgramDesc::kUniform_ColorType;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000178 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000179 pdesc.fColorType = ProgramDesc::kNone_ColorType;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000180 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000181
182 int idx = (int)(random.nextF() * (SkXfermode::kCoeffModesCnt));
183 pdesc.fColorFilterXfermode = (SkXfermode::Mode)idx;
184
185 idx = (int)(random.nextF() * (kNumStages+1));
186 pdesc.fFirstCoverageStage = idx;
187
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000188 bool edgeAA = random.nextF() > .5f;
189 if (edgeAA) {
190 pdesc.fEdgeAANumEdges = random.nextF() * this->getMaxEdges() + 1;
191 pdesc.fEdgeAAConcave = random.nextF() > .5f;
192 } else {
193 pdesc.fEdgeAANumEdges = 0;
194 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000195
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000196 if (fDualSourceBlendingSupport) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000197 pdesc.fDualSrcOutput =
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000198 (ProgramDesc::DualSrcOutput)
199 (int)(random.nextF() * ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000200 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000201 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000202 }
203
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000204 for (int s = 0; s < kNumStages; ++s) {
205 // enable the stage?
206 if (random.nextF() > .5f) {
207 // use separate tex coords?
208 if (random.nextF() > .5f) {
209 int t = (int)(random.nextF() * kMaxTexCoords);
210 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
211 } else {
212 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
213 }
214 }
215 // use text-formatted verts?
216 if (random.nextF() > .5f) {
217 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
218 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000219 idx = (int)(random.nextF() * GR_ARRAY_COUNT(STAGE_OPTS));
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000220 StageDesc& stage = pdesc.fStages[s];
221 stage.fOptFlags = STAGE_OPTS[idx];
222 stage.fModulation = random_val(&random, StageDesc::kModulationCnt);
223 stage.fCoordMapping = random_val(&random, StageDesc::kCoordMappingCnt);
224 stage.fFetchMode = random_val(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000225 // convolution shaders don't work with persp tex matrix
226 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) {
227 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
228 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000229 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000230 stage.fKernelWidth = 4 * random.nextF() + 2;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000231 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000232 CachedData cachedData;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000233 if (!program.genProgram(&cachedData)) {
234 return false;
235 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000236 DeleteProgram(&cachedData);
237 bool again = false;
238 if (again) {
239 program.genProgram(&cachedData);
240 DeleteProgram(&cachedData);
241 }
242 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000243 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000244}
245
junov@google.comf93e7172011-03-31 21:26:24 +0000246GrGpuGLShaders::GrGpuGLShaders() {
247
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000248 resetContext();
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000249
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000250 f4X4DownsampleFilterSupport = true;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000251 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000252 fDualSourceBlendingSupport =
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000253 fGLVersion >= 3.3f ||
254 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000255 } else {
256 fDualSourceBlendingSupport = false;
257 }
junov@google.comf93e7172011-03-31 21:26:24 +0000258
259 fProgramData = NULL;
260 fProgramCache = new ProgramCache();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000261
262#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000263 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000264#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000265}
266
267GrGpuGLShaders::~GrGpuGLShaders() {
268 delete fProgramCache;
269}
270
271const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000272 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000273
274 if (GrGLProgram::kSetAsAttribute ==
275 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
276 return fHWDrawState.fSamplerStates[stage].getMatrix();
277 } else {
278 return fProgramData->fTextureMatrices[stage];
279 }
junov@google.comf93e7172011-03-31 21:26:24 +0000280}
281
282void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000283 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000284 if (GrGLProgram::kSetAsAttribute ==
285 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
286 fHWDrawState.fSamplerStates[stage].setMatrix(matrix);
287 } else {
288 fProgramData->fTextureMatrices[stage] = matrix;
289 }
junov@google.comf93e7172011-03-31 21:26:24 +0000290}
291
292void GrGpuGLShaders::resetContext() {
293 INHERITED::resetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000294
junov@google.comf93e7172011-03-31 21:26:24 +0000295 fHWGeometryState.fVertexLayout = 0;
296 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.com91961302011-05-09 18:39:58 +0000297 GR_GL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000298 for (int t = 0; t < kMaxTexCoords; ++t) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000299 GR_GL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000300 }
bsalomon@google.com91961302011-05-09 18:39:58 +0000301 GR_GL(EnableVertexAttribArray(GrGLProgram::PositionAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000302
303 fHWProgramID = 0;
304}
305
306void GrGpuGLShaders::flushViewMatrix() {
307 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000308 GrMatrix m;
309 m.setAll(
junov@google.comf93e7172011-03-31 21:26:24 +0000310 GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1,
311 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1,
312 0, 0, GrMatrix::I()[8]);
313 m.setConcat(m, fCurrDrawState.fViewMatrix);
314
315 // ES doesn't allow you to pass true to the transpose param,
316 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000317 GrGLfloat mt[] = {
318 GrScalarToFloat(m[GrMatrix::kMScaleX]),
319 GrScalarToFloat(m[GrMatrix::kMSkewY]),
320 GrScalarToFloat(m[GrMatrix::kMPersp0]),
321 GrScalarToFloat(m[GrMatrix::kMSkewX]),
322 GrScalarToFloat(m[GrMatrix::kMScaleY]),
323 GrScalarToFloat(m[GrMatrix::kMPersp1]),
324 GrScalarToFloat(m[GrMatrix::kMTransX]),
325 GrScalarToFloat(m[GrMatrix::kMTransY]),
326 GrScalarToFloat(m[GrMatrix::kMPersp2])
junov@google.comf93e7172011-03-31 21:26:24 +0000327 };
bsalomon@google.com91961302011-05-09 18:39:58 +0000328
329 if (GrGLProgram::kSetAsAttribute ==
330 fProgramData->fUniLocations.fViewMatrixUni) {
331 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
332 GR_GL(VertexAttrib4fv(baseIdx + 0, mt+0));
333 GR_GL(VertexAttrib4fv(baseIdx + 1, mt+3));
334 GR_GL(VertexAttrib4fv(baseIdx + 2, mt+6));
335 } else {
336 GrAssert(GrGLProgram::kUnusedUniform !=
337 fProgramData->fUniLocations.fViewMatrixUni);
338 GR_GL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
339 1, false, mt));
340 }
junov@google.comf93e7172011-03-31 21:26:24 +0000341}
342
junov@google.com6acc9b32011-05-16 18:32:07 +0000343void GrGpuGLShaders::flushTextureDomain(int s) {
344 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
345 if (GrGLProgram::kUnusedUniform != uni) {
twiz@google.com76b82742011-06-02 20:30:02 +0000346 const GrRect &texDom =
junov@google.com6acc9b32011-05-16 18:32:07 +0000347 fCurrDrawState.fSamplerStates[s].getTextureDomain();
348
twiz@google.com76b82742011-06-02 20:30:02 +0000349 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
350 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000351
junov@google.com2f839402011-05-24 15:13:01 +0000352 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000353
junov@google.com2f839402011-05-24 15:13:01 +0000354 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000355 GrScalarToFloat(texDom.left()),
356 GrScalarToFloat(texDom.top()),
357 GrScalarToFloat(texDom.right()),
358 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000359 };
360
361 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
362 GrGLTexture::Orientation orientation = texture->orientation();
363
364 // vertical flip if necessary
365 if (GrGLTexture::kBottomUp_Orientation == orientation) {
366 values[1] = 1.0f - values[1];
367 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000368 // The top and bottom were just flipped, so correct the ordering
369 // of elements so that values = (l, t, r, b).
370 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000371 }
372
373 values[0] *= SkScalarToFloat(texture->contentScaleX());
374 values[2] *= SkScalarToFloat(texture->contentScaleX());
375 values[1] *= SkScalarToFloat(texture->contentScaleY());
376 values[3] *= SkScalarToFloat(texture->contentScaleY());
377
378 GR_GL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000379 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000380 }
381}
382
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000383void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000384 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000385 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
386 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000387 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000388 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
389 getHWSamplerMatrix(s) != getSamplerMatrix(s))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000390
bsalomon@google.com91961302011-05-09 18:39:58 +0000391 GrAssert(NULL != fCurrDrawState.fTextures[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000392
bsalomon@google.com91961302011-05-09 18:39:58 +0000393 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000394
bsalomon@google.com91961302011-05-09 18:39:58 +0000395 GrMatrix m = getSamplerMatrix(s);
396 GrSamplerState::SampleMode mode =
397 fCurrDrawState.fSamplerStates[s].getSampleMode();
398 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000399
bsalomon@google.com91961302011-05-09 18:39:58 +0000400 // ES doesn't allow you to pass true to the transpose param,
401 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000402 GrGLfloat mt[] = {
403 GrScalarToFloat(m[GrMatrix::kMScaleX]),
404 GrScalarToFloat(m[GrMatrix::kMSkewY]),
405 GrScalarToFloat(m[GrMatrix::kMPersp0]),
406 GrScalarToFloat(m[GrMatrix::kMSkewX]),
407 GrScalarToFloat(m[GrMatrix::kMScaleY]),
408 GrScalarToFloat(m[GrMatrix::kMPersp1]),
409 GrScalarToFloat(m[GrMatrix::kMTransX]),
410 GrScalarToFloat(m[GrMatrix::kMTransY]),
411 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000412 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000413
bsalomon@google.com91961302011-05-09 18:39:58 +0000414 if (GrGLProgram::kSetAsAttribute ==
415 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
416 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
417 GR_GL(VertexAttrib4fv(baseIdx + 0, mt+0));
418 GR_GL(VertexAttrib4fv(baseIdx + 1, mt+3));
419 GR_GL(VertexAttrib4fv(baseIdx + 2, mt+6));
420 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000421 GR_GL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000422 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000423 recordHWSamplerMatrix(s, getSamplerMatrix(s));
424 }
425 }
junov@google.comf93e7172011-03-31 21:26:24 +0000426}
427
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000428void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000429
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000430 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
431 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com91961302011-05-09 18:39:58 +0000432 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000433 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
434 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
435 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000436
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000437 GrScalar centerX1 = sampler.getRadial2CenterX1();
438 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000439
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000440 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000441
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000442 // when were in the degenerate (linear) case the second
443 // value will be INF but the program doesn't read it. (We
444 // use the same 6 uniforms even though we don't need them
445 // all in the linear case just to keep the code complexity
446 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000447 float values[6] = {
448 GrScalarToFloat(a),
449 1 / (2.f * values[0]),
450 GrScalarToFloat(centerX1),
451 GrScalarToFloat(radius0),
452 GrScalarToFloat(GrMul(radius0, radius0)),
453 sampler.isRadial2PosRoot() ? 1.f : -1.f
454 };
455 GR_GL(Uniform1fv(uni, 6, values));
456 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
457 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
458 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
459 }
460}
461
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000462void GrGpuGLShaders::flushConvolution(int s) {
463 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
464 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
465 if (GrGLProgram::kUnusedUniform != kernelUni) {
466 GR_GL(Uniform1fv(kernelUni, sampler.getKernelWidth(), sampler.getKernel()));
467 }
468 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
469 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
470 GR_GL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement()));
471 }
472}
473
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000474void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000475 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000476 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000477 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000478 if (texture->allocatedWidth() != fProgramData->fTextureWidth[s] ||
479 texture->allocatedHeight() != fProgramData->fTextureWidth[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000480
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000481 float texelSize[] = {1.f / texture->allocatedWidth(),
482 1.f / texture->allocatedHeight()};
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000483 GR_GL(Uniform2fv(uni, 1, texelSize));
484 }
485 }
junov@google.comf93e7172011-03-31 21:26:24 +0000486}
487
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000488void GrGpuGLShaders::flushEdgeAAData() {
489 const int& uni = fProgramData->fUniLocations.fEdgesUni;
490 if (GrGLProgram::kUnusedUniform != uni) {
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000491 int count = fCurrDrawState.fEdgeAANumEdges;
492 Edge edges[kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000493 // Flip the edges in Y
494 float height = fCurrDrawState.fRenderTarget->height();
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000495 for (int i = 0; i < count; ++i) {
496 edges[i] = fCurrDrawState.fEdgeAAEdges[i];
497 float b = edges[i].fY;
498 edges[i].fY = -b;
499 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000500 }
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000501 GR_GL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000502 }
503}
504
Scroggo01b87ec2011-05-11 18:05:38 +0000505static const float ONE_OVER_255 = 1.f / 255.f;
506
507#define GR_COLOR_TO_VEC4(color) {\
508 GrColorUnpackR(color) * ONE_OVER_255,\
509 GrColorUnpackG(color) * ONE_OVER_255,\
510 GrColorUnpackB(color) * ONE_OVER_255,\
511 GrColorUnpackA(color) * ONE_OVER_255 \
512}
513
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000514void GrGpuGLShaders::flushColor() {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000515 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000516 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000517 // color will be specified per-vertex as an attribute
518 // invalidate the const vertex attrib color
519 fHWDrawState.fColor = GrColor_ILLEGAL;
520 } else {
521 switch (desc.fColorType) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000522 case ProgramDesc::kAttribute_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000523 if (fHWDrawState.fColor != fCurrDrawState.fColor) {
524 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000525 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com91961302011-05-09 18:39:58 +0000526 GR_GL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(), c));
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000527 fHWDrawState.fColor = fCurrDrawState.fColor;
528 }
529 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000530 case ProgramDesc::kUniform_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000531 if (fProgramData->fColor != fCurrDrawState.fColor) {
532 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000533 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com91961302011-05-09 18:39:58 +0000534 GrAssert(GrGLProgram::kUnusedUniform !=
535 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000536 GR_GL(Uniform4fv(fProgramData->fUniLocations.fColorUni, 1, c));
537 fProgramData->fColor = fCurrDrawState.fColor;
538 }
539 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000540 case ProgramDesc::kNone_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000541 GrAssert(0xffffffff == fCurrDrawState.fColor);
542 break;
543 default:
544 GrCrash("Unknown color type.");
545 }
546 }
Scroggo97c88c22011-05-11 14:05:25 +0000547 if (fProgramData->fUniLocations.fColorFilterUni
548 != GrGLProgram::kUnusedUniform
549 && fProgramData->fColorFilterColor
550 != fCurrDrawState.fColorFilterColor) {
Scroggo01b87ec2011-05-11 18:05:38 +0000551 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColorFilterColor);
Scroggo97c88c22011-05-11 14:05:25 +0000552 GR_GL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
553 fProgramData->fColorFilterColor = fCurrDrawState.fColorFilterColor;
554 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000555}
556
557
junov@google.comf93e7172011-03-31 21:26:24 +0000558bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
559 if (!flushGLStateCommon(type)) {
560 return false;
561 }
562
563 if (fDirtyFlags.fRenderTargetChanged) {
564 // our coords are in pixel space and the GL matrices map to NDC
565 // so if the viewport changed, our matrix is now wrong.
junov@google.comf93e7172011-03-31 21:26:24 +0000566 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000567 // we assume all shader matrices may be wrong after viewport changes
568 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000569 }
570
junov@google.comf93e7172011-03-31 21:26:24 +0000571 buildProgram(type);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000572 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000573 if (NULL == fProgramData) {
574 GrAssert(!"Failed to create program!");
575 return false;
576 }
junov@google.comf93e7172011-03-31 21:26:24 +0000577
578 if (fHWProgramID != fProgramData->fProgramID) {
579 GR_GL(UseProgram(fProgramData->fProgramID));
580 fHWProgramID = fProgramData->fProgramID;
581 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000582 GrBlendCoeff srcCoeff = fCurrDrawState.fSrcBlend;
583 GrBlendCoeff dstCoeff = fCurrDrawState.fDstBlend;
584
585 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
586 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000587
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000588 this->flushColor();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000589
bsalomon@google.com91961302011-05-09 18:39:58 +0000590 GrMatrix* currViewMatrix;
591 if (GrGLProgram::kSetAsAttribute ==
592 fProgramData->fUniLocations.fViewMatrixUni) {
593 currViewMatrix = &fHWDrawState.fViewMatrix;
594 } else {
595 currViewMatrix = &fProgramData->fViewMatrix;
596 }
junov@google.comf93e7172011-03-31 21:26:24 +0000597
bsalomon@google.com91961302011-05-09 18:39:58 +0000598 if (*currViewMatrix != fCurrDrawState.fViewMatrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000599 flushViewMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000600 *currViewMatrix = fCurrDrawState.fViewMatrix;
junov@google.comf93e7172011-03-31 21:26:24 +0000601 }
602
603 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000604 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000605
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000606 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000607
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000608 this->flushConvolution(s);
609
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000610 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000611
612 this->flushTextureDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000613 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000614 this->flushEdgeAAData();
junov@google.comf93e7172011-03-31 21:26:24 +0000615 resetDirtyFlags();
616 return true;
617}
618
619void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000620}
621
622void GrGpuGLShaders::setupGeometry(int* startVertex,
623 int* startIndex,
624 int vertexCount,
625 int indexCount) {
626
627 int newColorOffset;
628 int newTexCoordOffsets[kMaxTexCoords];
629
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000630 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
631 this->getGeomSrc().fVertexLayout,
632 newTexCoordOffsets,
633 &newColorOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000634 int oldColorOffset;
635 int oldTexCoordOffsets[kMaxTexCoords];
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000636 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
637 fHWGeometryState.fVertexLayout,
638 oldTexCoordOffsets,
639 &oldColorOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000640 bool indexed = NULL != startIndex;
641
642 int extraVertexOffset;
643 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000644 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000645
646 GrGLenum scalarType;
647 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000648 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000649 scalarType = GrGLTextType;
650 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
651 } else {
652 scalarType = GrGLType;
653 texCoordNorm = false;
654 }
655
656 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
657 *startVertex = 0;
658 if (indexed) {
659 *startIndex += extraIndexOffset;
660 }
661
662 // all the Pointers must be set if any of these are true
663 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
664 vertexOffset != fHWGeometryState.fVertexOffset ||
665 newStride != oldStride;
666
667 // position and tex coord offsets change if above conditions are true
668 // or the type/normalization changed based on text vs nontext type coords.
669 bool posAndTexChange = allOffsetsChange ||
670 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
671 (kTextFormat_VertexLayoutBit &
672 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000673 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000674
675 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000676 int idx = GrGLProgram::PositionAttributeIdx();
677 GR_GL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
678 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000679 fHWGeometryState.fVertexOffset = vertexOffset;
680 }
681
682 for (int t = 0; t < kMaxTexCoords; ++t) {
683 if (newTexCoordOffsets[t] > 0) {
684 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000685 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000686 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000687 GR_GL(EnableVertexAttribArray(idx));
688 GR_GL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
689 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000690 } else if (posAndTexChange ||
691 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000692 GR_GL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
693 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000694 }
695 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000696 GR_GL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000697 }
698 }
699
700 if (newColorOffset > 0) {
701 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000702 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000703 if (oldColorOffset <= 0) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000704 GR_GL(EnableVertexAttribArray(idx));
705 GR_GL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000706 true, newStride, colorOffset));
707 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000708 GR_GL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000709 true, newStride, colorOffset));
710 }
711 } else if (oldColorOffset > 0) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000712 GR_GL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000713 }
714
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000715 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000716 fHWGeometryState.fArrayPtrsDirty = false;
717}
718
719void GrGpuGLShaders::buildProgram(GrPrimitiveType type) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000720 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
junov@google.comf93e7172011-03-31 21:26:24 +0000721
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000722 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000723 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000724
725 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
726
727 bool requiresAttributeColors = desc.fVertexLayout & kColor_VertexLayoutBit;
728 // fColorType records how colors are specified for the program. Strip
729 // the bit from the layout to avoid false negatives when searching for an
730 // existing program in the cache.
731 desc.fVertexLayout &= ~(kColor_VertexLayoutBit);
732
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000733 desc.fColorFilterXfermode = fCurrDrawState.fColorFilterXfermode;
734
junov@google.comf93e7172011-03-31 21:26:24 +0000735#if GR_AGGRESSIVE_SHADER_OPTS
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000736 if (!requiresAttributeColors && (0xffffffff == fCurrDrawState.fColor)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000737 desc.fColorType = ProgramDesc::kNone_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000738 } else
junov@google.comf93e7172011-03-31 21:26:24 +0000739#endif
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000740#if GR_GL_NO_CONSTANT_ATTRIBUTES
741 if (!requiresAttributeColors) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000742 desc.fColorType = ProgramDesc::kUniform_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000743 } else
744#endif
745 {
bsalomon@google.com6a77cc52011-04-28 17:33:34 +0000746 if (requiresAttributeColors) {} // suppress unused var warning
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000747 desc.fColorType = ProgramDesc::kAttribute_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000748 }
junov@google.comf93e7172011-03-31 21:26:24 +0000749
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000750 desc.fEdgeAANumEdges = fCurrDrawState.fEdgeAANumEdges;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000751 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 && SkToBool(fCurrDrawState.fFlagBits & kEdgeAAConcave_StateBit);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000752
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000753 int lastEnabledStage = -1;
754
junov@google.comf93e7172011-03-31 21:26:24 +0000755 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000756 StageDesc& stage = desc.fStages[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000757
tomhudson@google.com0d831722011-06-02 15:37:14 +0000758 stage.fOptFlags = 0;
759 stage.setEnabled(this->isStageEnabled(s));
junov@google.comf93e7172011-03-31 21:26:24 +0000760
tomhudson@google.com0d831722011-06-02 15:37:14 +0000761 if (stage.isEnabled()) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000762 lastEnabledStage = s;
junov@google.comf93e7172011-03-31 21:26:24 +0000763 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
764 GrAssert(NULL != texture);
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000765 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000766 // we matrix to invert when orientation is TopDown, so make sure
767 // we aren't in that case before flagging as identity.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000768 if (TextureMatrixIsIdentity(texture, sampler)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000769 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000770 } else if (!getSamplerMatrix(s).hasPerspective()) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000771 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000772 }
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000773 switch (sampler.getSampleMode()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000774 case GrSamplerState::kNormal_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000775 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000776 break;
777 case GrSamplerState::kRadial_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000778 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000779 break;
780 case GrSamplerState::kRadial2_SampleMode:
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000781 if (sampler.radial2IsDegenerate()) {
782 stage.fCoordMapping =
783 StageDesc::kRadial2GradientDegenerate_CoordMapping;
784 } else {
785 stage.fCoordMapping =
786 StageDesc::kRadial2Gradient_CoordMapping;
787 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000788 break;
789 case GrSamplerState::kSweep_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000790 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000791 break;
792 default:
793 GrCrash("Unexpected sample mode!");
794 break;
795 }
796
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000797 switch (sampler.getFilter()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000798 // these both can use a regular texture2D()
799 case GrSamplerState::kNearest_Filter:
800 case GrSamplerState::kBilinear_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000801 stage.fFetchMode = StageDesc::kSingle_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000802 break;
803 // performs 4 texture2D()s
804 case GrSamplerState::k4x4Downsample_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000805 stage.fFetchMode = StageDesc::k2x2_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000806 break;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000807 // performs fKernelWidth texture2D()s
808 case GrSamplerState::kConvolution_Filter:
809 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
810 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000811 default:
812 GrCrash("Unexpected filter!");
813 break;
junov@google.comf93e7172011-03-31 21:26:24 +0000814 }
815
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000816 if (sampler.hasTextureDomain()) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000817 GrAssert(GrSamplerState::kClamp_WrapMode ==
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000818 sampler.getWrapX() &&
819 GrSamplerState::kClamp_WrapMode ==
820 sampler.getWrapY());
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000821 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
junov@google.com6acc9b32011-05-16 18:32:07 +0000822 }
823
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000824 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000825 stage.fModulation = StageDesc::kAlpha_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000826 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000827 stage.fModulation = StageDesc::kColor_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000828 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000829 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) {
830 stage.fKernelWidth = sampler.getKernelWidth();
831 } else {
832 stage.fKernelWidth = 0;
833 }
junov@google.comf93e7172011-03-31 21:26:24 +0000834 } else {
835 stage.fOptFlags = 0;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000836 stage.fCoordMapping = (StageDesc::CoordMapping)0;
837 stage.fModulation = (StageDesc::Modulation)0;
junov@google.comf93e7172011-03-31 21:26:24 +0000838 }
839 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000840
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000841 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000842 // use canonical value when coverage/color distinction won't affect
843 // generated code to prevent duplicate programs.
844 desc.fFirstCoverageStage = kNumStages;
845 if (fCurrDrawState.fFirstCoverageStage <= lastEnabledStage) {
846 // color filter is applied between color/coverage computation
847 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
848 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
849 }
850
851 // We could consider cases where the final color is solid (0xff alpha)
852 // and the dst coeff can correctly be set to a non-dualsrc gl value.
853 // (e.g. solid draw, and dst coeff is kZero. It's correct to make
854 // the dst coeff be kISA. Or solid draw with kSA can be tweaked to be
855 // kOne).
856 if (fDualSourceBlendingSupport) {
857 if (kZero_BlendCoeff == fCurrDrawState.fDstBlend) {
858 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000859 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000860 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
861 } else if (kSA_BlendCoeff == fCurrDrawState.fDstBlend) {
862 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
863 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000864 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000865 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
866 } else if (kSC_BlendCoeff == fCurrDrawState.fDstBlend) {
867 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
868 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000869 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000870 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
871 }
872 }
873 }
junov@google.comf93e7172011-03-31 21:26:24 +0000874}