blob: 6d75de8f4e61a188fb4e6c82fa943a2cd5c7ab16 [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.com6aef1fb2011-05-05 12:33:22 +0000146void GrGpuGLShaders::ProgramUnitTest() {
147
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
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000188 pdesc.fEdgeAANumEdges = (random.nextF() * (getMaxEdges() + 1));
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000189 pdesc.fEdgeAAConcave = random.nextF() > .5f;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000190
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000191 if (fDualSourceBlendingSupport) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000192 pdesc.fDualSrcOutput =
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000193 (ProgramDesc::DualSrcOutput)
194 (int)(random.nextF() * ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000195 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000196 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000197 }
198
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000199 for (int s = 0; s < kNumStages; ++s) {
200 // enable the stage?
201 if (random.nextF() > .5f) {
202 // use separate tex coords?
203 if (random.nextF() > .5f) {
204 int t = (int)(random.nextF() * kMaxTexCoords);
205 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
206 } else {
207 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
208 }
209 }
210 // use text-formatted verts?
211 if (random.nextF() > .5f) {
212 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
213 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000214 idx = (int)(random.nextF() * GR_ARRAY_COUNT(STAGE_OPTS));
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000215 StageDesc& stage = pdesc.fStages[s];
216 stage.fOptFlags = STAGE_OPTS[idx];
217 stage.fModulation = random_val(&random, StageDesc::kModulationCnt);
218 stage.fCoordMapping = random_val(&random, StageDesc::kCoordMappingCnt);
219 stage.fFetchMode = random_val(&random, StageDesc::kFetchModeCnt);
220 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000221 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000222 CachedData cachedData;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000223 program.genProgram(&cachedData);
224 DeleteProgram(&cachedData);
225 bool again = false;
226 if (again) {
227 program.genProgram(&cachedData);
228 DeleteProgram(&cachedData);
229 }
230 }
231}
232
junov@google.comf93e7172011-03-31 21:26:24 +0000233GrGpuGLShaders::GrGpuGLShaders() {
234
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000235 resetContext();
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000236
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000237 f4X4DownsampleFilterSupport = true;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000238 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000239 fDualSourceBlendingSupport =
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000240 fGLVersion >= 3.3f ||
241 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000242 } else {
243 fDualSourceBlendingSupport = false;
244 }
junov@google.comf93e7172011-03-31 21:26:24 +0000245
246 fProgramData = NULL;
247 fProgramCache = new ProgramCache();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000248
249#if 0
250 ProgramUnitTest();
251#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000252}
253
254GrGpuGLShaders::~GrGpuGLShaders() {
255 delete fProgramCache;
256}
257
258const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000259 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000260
261 if (GrGLProgram::kSetAsAttribute ==
262 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
263 return fHWDrawState.fSamplerStates[stage].getMatrix();
264 } else {
265 return fProgramData->fTextureMatrices[stage];
266 }
junov@google.comf93e7172011-03-31 21:26:24 +0000267}
268
269void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000270 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000271 if (GrGLProgram::kSetAsAttribute ==
272 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
273 fHWDrawState.fSamplerStates[stage].setMatrix(matrix);
274 } else {
275 fProgramData->fTextureMatrices[stage] = matrix;
276 }
junov@google.comf93e7172011-03-31 21:26:24 +0000277}
278
279void GrGpuGLShaders::resetContext() {
280 INHERITED::resetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000281
junov@google.comf93e7172011-03-31 21:26:24 +0000282 fHWGeometryState.fVertexLayout = 0;
283 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.com91961302011-05-09 18:39:58 +0000284 GR_GL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000285 for (int t = 0; t < kMaxTexCoords; ++t) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000286 GR_GL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000287 }
bsalomon@google.com91961302011-05-09 18:39:58 +0000288 GR_GL(EnableVertexAttribArray(GrGLProgram::PositionAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000289
290 fHWProgramID = 0;
291}
292
293void GrGpuGLShaders::flushViewMatrix() {
294 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000295 GrMatrix m;
296 m.setAll(
junov@google.comf93e7172011-03-31 21:26:24 +0000297 GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1,
298 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1,
299 0, 0, GrMatrix::I()[8]);
300 m.setConcat(m, fCurrDrawState.fViewMatrix);
301
302 // ES doesn't allow you to pass true to the transpose param,
303 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000304 GrGLfloat mt[] = {
305 GrScalarToFloat(m[GrMatrix::kMScaleX]),
306 GrScalarToFloat(m[GrMatrix::kMSkewY]),
307 GrScalarToFloat(m[GrMatrix::kMPersp0]),
308 GrScalarToFloat(m[GrMatrix::kMSkewX]),
309 GrScalarToFloat(m[GrMatrix::kMScaleY]),
310 GrScalarToFloat(m[GrMatrix::kMPersp1]),
311 GrScalarToFloat(m[GrMatrix::kMTransX]),
312 GrScalarToFloat(m[GrMatrix::kMTransY]),
313 GrScalarToFloat(m[GrMatrix::kMPersp2])
junov@google.comf93e7172011-03-31 21:26:24 +0000314 };
bsalomon@google.com91961302011-05-09 18:39:58 +0000315
316 if (GrGLProgram::kSetAsAttribute ==
317 fProgramData->fUniLocations.fViewMatrixUni) {
318 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
319 GR_GL(VertexAttrib4fv(baseIdx + 0, mt+0));
320 GR_GL(VertexAttrib4fv(baseIdx + 1, mt+3));
321 GR_GL(VertexAttrib4fv(baseIdx + 2, mt+6));
322 } else {
323 GrAssert(GrGLProgram::kUnusedUniform !=
324 fProgramData->fUniLocations.fViewMatrixUni);
325 GR_GL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
326 1, false, mt));
327 }
junov@google.comf93e7172011-03-31 21:26:24 +0000328}
329
junov@google.com6acc9b32011-05-16 18:32:07 +0000330void GrGpuGLShaders::flushTextureDomain(int s) {
331 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
332 if (GrGLProgram::kUnusedUniform != uni) {
twiz@google.com76b82742011-06-02 20:30:02 +0000333 const GrRect &texDom =
junov@google.com6acc9b32011-05-16 18:32:07 +0000334 fCurrDrawState.fSamplerStates[s].getTextureDomain();
335
twiz@google.com76b82742011-06-02 20:30:02 +0000336 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
337 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000338
junov@google.com2f839402011-05-24 15:13:01 +0000339 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000340
junov@google.com2f839402011-05-24 15:13:01 +0000341 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000342 GrScalarToFloat(texDom.left()),
343 GrScalarToFloat(texDom.top()),
344 GrScalarToFloat(texDom.right()),
345 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000346 };
347
348 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
349 GrGLTexture::Orientation orientation = texture->orientation();
350
351 // vertical flip if necessary
352 if (GrGLTexture::kBottomUp_Orientation == orientation) {
353 values[1] = 1.0f - values[1];
354 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000355 // The top and bottom were just flipped, so correct the ordering
356 // of elements so that values = (l, t, r, b).
357 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000358 }
359
360 values[0] *= SkScalarToFloat(texture->contentScaleX());
361 values[2] *= SkScalarToFloat(texture->contentScaleX());
362 values[1] *= SkScalarToFloat(texture->contentScaleY());
363 values[3] *= SkScalarToFloat(texture->contentScaleY());
364
365 GR_GL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000366 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000367 }
368}
369
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000370void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000371 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000372 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
373 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000374 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000375 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
376 getHWSamplerMatrix(s) != getSamplerMatrix(s))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000377
bsalomon@google.com91961302011-05-09 18:39:58 +0000378 GrAssert(NULL != fCurrDrawState.fTextures[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000379
bsalomon@google.com91961302011-05-09 18:39:58 +0000380 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000381
bsalomon@google.com91961302011-05-09 18:39:58 +0000382 GrMatrix m = getSamplerMatrix(s);
383 GrSamplerState::SampleMode mode =
384 fCurrDrawState.fSamplerStates[s].getSampleMode();
385 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000386
bsalomon@google.com91961302011-05-09 18:39:58 +0000387 // ES doesn't allow you to pass true to the transpose param,
388 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000389 GrGLfloat mt[] = {
390 GrScalarToFloat(m[GrMatrix::kMScaleX]),
391 GrScalarToFloat(m[GrMatrix::kMSkewY]),
392 GrScalarToFloat(m[GrMatrix::kMPersp0]),
393 GrScalarToFloat(m[GrMatrix::kMSkewX]),
394 GrScalarToFloat(m[GrMatrix::kMScaleY]),
395 GrScalarToFloat(m[GrMatrix::kMPersp1]),
396 GrScalarToFloat(m[GrMatrix::kMTransX]),
397 GrScalarToFloat(m[GrMatrix::kMTransY]),
398 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000399 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000400
bsalomon@google.com91961302011-05-09 18:39:58 +0000401 if (GrGLProgram::kSetAsAttribute ==
402 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
403 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
404 GR_GL(VertexAttrib4fv(baseIdx + 0, mt+0));
405 GR_GL(VertexAttrib4fv(baseIdx + 1, mt+3));
406 GR_GL(VertexAttrib4fv(baseIdx + 2, mt+6));
407 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000408 GR_GL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000409 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000410 recordHWSamplerMatrix(s, getSamplerMatrix(s));
411 }
412 }
junov@google.comf93e7172011-03-31 21:26:24 +0000413}
414
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000415void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000416
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000417 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
418 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com91961302011-05-09 18:39:58 +0000419 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000420 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
421 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
422 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000423
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000424 GrScalar centerX1 = sampler.getRadial2CenterX1();
425 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000426
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000427 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000428
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000429 // when were in the degenerate (linear) case the second
430 // value will be INF but the program doesn't read it. (We
431 // use the same 6 uniforms even though we don't need them
432 // all in the linear case just to keep the code complexity
433 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000434 float values[6] = {
435 GrScalarToFloat(a),
436 1 / (2.f * values[0]),
437 GrScalarToFloat(centerX1),
438 GrScalarToFloat(radius0),
439 GrScalarToFloat(GrMul(radius0, radius0)),
440 sampler.isRadial2PosRoot() ? 1.f : -1.f
441 };
442 GR_GL(Uniform1fv(uni, 6, values));
443 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
444 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
445 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
446 }
447}
448
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000449void GrGpuGLShaders::flushConvolution(int s) {
450 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
451 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
452 if (GrGLProgram::kUnusedUniform != kernelUni) {
453 GR_GL(Uniform1fv(kernelUni, sampler.getKernelWidth(), sampler.getKernel()));
454 }
455 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
456 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
457 GR_GL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement()));
458 }
459}
460
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000461void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000462 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000463 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000464 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
465 if (texture->allocWidth() != fProgramData->fTextureWidth[s] ||
466 texture->allocHeight() != fProgramData->fTextureWidth[s]) {
467
468 float texelSize[] = {1.f / texture->allocWidth(),
469 1.f / texture->allocHeight()};
470 GR_GL(Uniform2fv(uni, 1, texelSize));
471 }
472 }
junov@google.comf93e7172011-03-31 21:26:24 +0000473}
474
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000475void GrGpuGLShaders::flushEdgeAAData() {
476 const int& uni = fProgramData->fUniLocations.fEdgesUni;
477 if (GrGLProgram::kUnusedUniform != uni) {
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000478 int count = fCurrDrawState.fEdgeAANumEdges;
479 Edge edges[kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000480 // Flip the edges in Y
481 float height = fCurrDrawState.fRenderTarget->height();
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000482 for (int i = 0; i < count; ++i) {
483 edges[i] = fCurrDrawState.fEdgeAAEdges[i];
484 float b = edges[i].fY;
485 edges[i].fY = -b;
486 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000487 }
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000488 GR_GL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000489 }
490}
491
Scroggo01b87ec2011-05-11 18:05:38 +0000492static const float ONE_OVER_255 = 1.f / 255.f;
493
494#define GR_COLOR_TO_VEC4(color) {\
495 GrColorUnpackR(color) * ONE_OVER_255,\
496 GrColorUnpackG(color) * ONE_OVER_255,\
497 GrColorUnpackB(color) * ONE_OVER_255,\
498 GrColorUnpackA(color) * ONE_OVER_255 \
499}
500
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000501void GrGpuGLShaders::flushColor() {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000502 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000503 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000504 // color will be specified per-vertex as an attribute
505 // invalidate the const vertex attrib color
506 fHWDrawState.fColor = GrColor_ILLEGAL;
507 } else {
508 switch (desc.fColorType) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000509 case ProgramDesc::kAttribute_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000510 if (fHWDrawState.fColor != fCurrDrawState.fColor) {
511 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000512 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com91961302011-05-09 18:39:58 +0000513 GR_GL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(), c));
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000514 fHWDrawState.fColor = fCurrDrawState.fColor;
515 }
516 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000517 case ProgramDesc::kUniform_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000518 if (fProgramData->fColor != fCurrDrawState.fColor) {
519 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000520 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com91961302011-05-09 18:39:58 +0000521 GrAssert(GrGLProgram::kUnusedUniform !=
522 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000523 GR_GL(Uniform4fv(fProgramData->fUniLocations.fColorUni, 1, c));
524 fProgramData->fColor = fCurrDrawState.fColor;
525 }
526 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000527 case ProgramDesc::kNone_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000528 GrAssert(0xffffffff == fCurrDrawState.fColor);
529 break;
530 default:
531 GrCrash("Unknown color type.");
532 }
533 }
Scroggo97c88c22011-05-11 14:05:25 +0000534 if (fProgramData->fUniLocations.fColorFilterUni
535 != GrGLProgram::kUnusedUniform
536 && fProgramData->fColorFilterColor
537 != fCurrDrawState.fColorFilterColor) {
Scroggo01b87ec2011-05-11 18:05:38 +0000538 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColorFilterColor);
Scroggo97c88c22011-05-11 14:05:25 +0000539 GR_GL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
540 fProgramData->fColorFilterColor = fCurrDrawState.fColorFilterColor;
541 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000542}
543
544
junov@google.comf93e7172011-03-31 21:26:24 +0000545bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
546 if (!flushGLStateCommon(type)) {
547 return false;
548 }
549
550 if (fDirtyFlags.fRenderTargetChanged) {
551 // our coords are in pixel space and the GL matrices map to NDC
552 // so if the viewport changed, our matrix is now wrong.
junov@google.comf93e7172011-03-31 21:26:24 +0000553 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000554 // we assume all shader matrices may be wrong after viewport changes
555 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000556 }
557
junov@google.comf93e7172011-03-31 21:26:24 +0000558 buildProgram(type);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000559 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000560 if (NULL == fProgramData) {
561 GrAssert(!"Failed to create program!");
562 return false;
563 }
junov@google.comf93e7172011-03-31 21:26:24 +0000564
565 if (fHWProgramID != fProgramData->fProgramID) {
566 GR_GL(UseProgram(fProgramData->fProgramID));
567 fHWProgramID = fProgramData->fProgramID;
568 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000569 GrBlendCoeff srcCoeff = fCurrDrawState.fSrcBlend;
570 GrBlendCoeff dstCoeff = fCurrDrawState.fDstBlend;
571
572 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
573 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000574
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000575 this->flushColor();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000576
bsalomon@google.com91961302011-05-09 18:39:58 +0000577 GrMatrix* currViewMatrix;
578 if (GrGLProgram::kSetAsAttribute ==
579 fProgramData->fUniLocations.fViewMatrixUni) {
580 currViewMatrix = &fHWDrawState.fViewMatrix;
581 } else {
582 currViewMatrix = &fProgramData->fViewMatrix;
583 }
junov@google.comf93e7172011-03-31 21:26:24 +0000584
bsalomon@google.com91961302011-05-09 18:39:58 +0000585 if (*currViewMatrix != fCurrDrawState.fViewMatrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000586 flushViewMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000587 *currViewMatrix = fCurrDrawState.fViewMatrix;
junov@google.comf93e7172011-03-31 21:26:24 +0000588 }
589
590 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000591 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000592
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000593 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000594
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000595 this->flushConvolution(s);
596
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000597 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000598
599 this->flushTextureDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000600 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000601 this->flushEdgeAAData();
junov@google.comf93e7172011-03-31 21:26:24 +0000602 resetDirtyFlags();
603 return true;
604}
605
606void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000607}
608
609void GrGpuGLShaders::setupGeometry(int* startVertex,
610 int* startIndex,
611 int vertexCount,
612 int indexCount) {
613
614 int newColorOffset;
615 int newTexCoordOffsets[kMaxTexCoords];
616
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000617 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
618 this->getGeomSrc().fVertexLayout,
619 newTexCoordOffsets,
620 &newColorOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000621 int oldColorOffset;
622 int oldTexCoordOffsets[kMaxTexCoords];
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000623 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
624 fHWGeometryState.fVertexLayout,
625 oldTexCoordOffsets,
626 &oldColorOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000627 bool indexed = NULL != startIndex;
628
629 int extraVertexOffset;
630 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000631 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000632
633 GrGLenum scalarType;
634 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000635 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000636 scalarType = GrGLTextType;
637 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
638 } else {
639 scalarType = GrGLType;
640 texCoordNorm = false;
641 }
642
643 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
644 *startVertex = 0;
645 if (indexed) {
646 *startIndex += extraIndexOffset;
647 }
648
649 // all the Pointers must be set if any of these are true
650 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
651 vertexOffset != fHWGeometryState.fVertexOffset ||
652 newStride != oldStride;
653
654 // position and tex coord offsets change if above conditions are true
655 // or the type/normalization changed based on text vs nontext type coords.
656 bool posAndTexChange = allOffsetsChange ||
657 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
658 (kTextFormat_VertexLayoutBit &
659 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000660 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000661
662 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000663 int idx = GrGLProgram::PositionAttributeIdx();
664 GR_GL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
665 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000666 fHWGeometryState.fVertexOffset = vertexOffset;
667 }
668
669 for (int t = 0; t < kMaxTexCoords; ++t) {
670 if (newTexCoordOffsets[t] > 0) {
671 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000672 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000673 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000674 GR_GL(EnableVertexAttribArray(idx));
675 GR_GL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
676 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000677 } else if (posAndTexChange ||
678 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000679 GR_GL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
680 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000681 }
682 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000683 GR_GL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000684 }
685 }
686
687 if (newColorOffset > 0) {
688 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000689 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000690 if (oldColorOffset <= 0) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000691 GR_GL(EnableVertexAttribArray(idx));
692 GR_GL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000693 true, newStride, colorOffset));
694 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000695 GR_GL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000696 true, newStride, colorOffset));
697 }
698 } else if (oldColorOffset > 0) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000699 GR_GL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000700 }
701
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000702 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000703 fHWGeometryState.fArrayPtrsDirty = false;
704}
705
706void GrGpuGLShaders::buildProgram(GrPrimitiveType type) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000707 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
junov@google.comf93e7172011-03-31 21:26:24 +0000708
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000709 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000710 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000711
712 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
713
714 bool requiresAttributeColors = desc.fVertexLayout & kColor_VertexLayoutBit;
715 // fColorType records how colors are specified for the program. Strip
716 // the bit from the layout to avoid false negatives when searching for an
717 // existing program in the cache.
718 desc.fVertexLayout &= ~(kColor_VertexLayoutBit);
719
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000720 desc.fColorFilterXfermode = fCurrDrawState.fColorFilterXfermode;
721
junov@google.comf93e7172011-03-31 21:26:24 +0000722#if GR_AGGRESSIVE_SHADER_OPTS
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000723 if (!requiresAttributeColors && (0xffffffff == fCurrDrawState.fColor)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000724 desc.fColorType = ProgramDesc::kNone_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000725 } else
junov@google.comf93e7172011-03-31 21:26:24 +0000726#endif
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000727#if GR_GL_NO_CONSTANT_ATTRIBUTES
728 if (!requiresAttributeColors) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000729 desc.fColorType = ProgramDesc::kUniform_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000730 } else
731#endif
732 {
bsalomon@google.com6a77cc52011-04-28 17:33:34 +0000733 if (requiresAttributeColors) {} // suppress unused var warning
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000734 desc.fColorType = ProgramDesc::kAttribute_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000735 }
junov@google.comf93e7172011-03-31 21:26:24 +0000736
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000737 desc.fEdgeAANumEdges = fCurrDrawState.fEdgeAANumEdges;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000738 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 && SkToBool(fCurrDrawState.fFlagBits & kEdgeAAConcave_StateBit);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000739
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000740 int lastEnabledStage = -1;
741
junov@google.comf93e7172011-03-31 21:26:24 +0000742 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000743 StageDesc& stage = desc.fStages[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000744
tomhudson@google.com0d831722011-06-02 15:37:14 +0000745 stage.fOptFlags = 0;
746 stage.setEnabled(this->isStageEnabled(s));
junov@google.comf93e7172011-03-31 21:26:24 +0000747
tomhudson@google.com0d831722011-06-02 15:37:14 +0000748 if (stage.isEnabled()) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000749 lastEnabledStage = s;
junov@google.comf93e7172011-03-31 21:26:24 +0000750 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
751 GrAssert(NULL != texture);
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000752 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000753 // we matrix to invert when orientation is TopDown, so make sure
754 // we aren't in that case before flagging as identity.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000755 if (TextureMatrixIsIdentity(texture, sampler)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000756 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000757 } else if (!getSamplerMatrix(s).hasPerspective()) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000758 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000759 }
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000760 switch (sampler.getSampleMode()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000761 case GrSamplerState::kNormal_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000762 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000763 break;
764 case GrSamplerState::kRadial_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000765 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000766 break;
767 case GrSamplerState::kRadial2_SampleMode:
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000768 if (sampler.radial2IsDegenerate()) {
769 stage.fCoordMapping =
770 StageDesc::kRadial2GradientDegenerate_CoordMapping;
771 } else {
772 stage.fCoordMapping =
773 StageDesc::kRadial2Gradient_CoordMapping;
774 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000775 break;
776 case GrSamplerState::kSweep_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000777 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000778 break;
779 default:
780 GrCrash("Unexpected sample mode!");
781 break;
782 }
783
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000784 switch (sampler.getFilter()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000785 // these both can use a regular texture2D()
786 case GrSamplerState::kNearest_Filter:
787 case GrSamplerState::kBilinear_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000788 stage.fFetchMode = StageDesc::kSingle_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000789 break;
790 // performs 4 texture2D()s
791 case GrSamplerState::k4x4Downsample_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000792 stage.fFetchMode = StageDesc::k2x2_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000793 break;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000794 // performs fKernelWidth texture2D()s
795 case GrSamplerState::kConvolution_Filter:
796 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
797 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000798 default:
799 GrCrash("Unexpected filter!");
800 break;
junov@google.comf93e7172011-03-31 21:26:24 +0000801 }
802
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000803 if (sampler.hasTextureDomain()) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000804 GrAssert(GrSamplerState::kClamp_WrapMode ==
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000805 sampler.getWrapX() &&
806 GrSamplerState::kClamp_WrapMode ==
807 sampler.getWrapY());
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000808 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
junov@google.com6acc9b32011-05-16 18:32:07 +0000809 }
810
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000811 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000812 stage.fModulation = StageDesc::kAlpha_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000813 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000814 stage.fModulation = StageDesc::kColor_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000815 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000816 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) {
817 stage.fKernelWidth = sampler.getKernelWidth();
818 } else {
819 stage.fKernelWidth = 0;
820 }
junov@google.comf93e7172011-03-31 21:26:24 +0000821 } else {
822 stage.fOptFlags = 0;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000823 stage.fCoordMapping = (StageDesc::CoordMapping)0;
824 stage.fModulation = (StageDesc::Modulation)0;
junov@google.comf93e7172011-03-31 21:26:24 +0000825 }
826 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000827
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000828 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000829 // use canonical value when coverage/color distinction won't affect
830 // generated code to prevent duplicate programs.
831 desc.fFirstCoverageStage = kNumStages;
832 if (fCurrDrawState.fFirstCoverageStage <= lastEnabledStage) {
833 // color filter is applied between color/coverage computation
834 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
835 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
836 }
837
838 // We could consider cases where the final color is solid (0xff alpha)
839 // and the dst coeff can correctly be set to a non-dualsrc gl value.
840 // (e.g. solid draw, and dst coeff is kZero. It's correct to make
841 // the dst coeff be kISA. Or solid draw with kSA can be tweaked to be
842 // kOne).
843 if (fDualSourceBlendingSupport) {
844 if (kZero_BlendCoeff == fCurrDrawState.fDstBlend) {
845 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000846 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000847 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
848 } else if (kSA_BlendCoeff == fCurrDrawState.fDstBlend) {
849 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
850 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000851 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000852 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
853 } else if (kSC_BlendCoeff == fCurrDrawState.fDstBlend) {
854 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
855 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000856 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000857 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
858 }
859 }
860 }
junov@google.comf93e7172011-03-31 21:26:24 +0000861}