blob: 748cf1ac47789b2abb1d4988c18c14ba71a05c3d [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
junov@google.comf7c00f62011-08-18 18:15:16 +000027 typedef GrBinHashKey<Entry, GrGLProgram::kProgramKeySize> ProgramHashKey;
junov@google.comf93e7172011-03-31 21:26:24 +000028
29 class Entry : public ::GrNoncopyable {
30 public:
31 Entry() {}
junov@google.comf93e7172011-03-31 21:26:24 +000032 void copyAndTakeOwnership(Entry& entry) {
33 fProgramData.copyAndTakeOwnership(entry.fProgramData);
junov@google.comf7c00f62011-08-18 18:15:16 +000034 fKey = entry.fKey; // ownership transfer
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000035 fLRUStamp = entry.fLRUStamp;
junov@google.comf93e7172011-03-31 21:26:24 +000036 }
37
38 public:
39 int compare(const ProgramHashKey& key) const { return fKey.compare(key); }
40
41 public:
42 GrGLProgram::CachedData fProgramData;
43 ProgramHashKey fKey;
44 unsigned int fLRUStamp;
45 };
46
47 GrTHashTable<Entry, ProgramHashKey, 8> fHashCache;
48
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000049 // We may have kMaxEntries+1 shaders in the GL context because
50 // we create a new shader before evicting from the cache.
junov@google.comf93e7172011-03-31 21:26:24 +000051 enum {
52 kMaxEntries = 32
53 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +000054 Entry fEntries[kMaxEntries];
55 int fCount;
56 unsigned int fCurrLRUStamp;
57 const GrGLInterface* fGL;
junov@google.comf93e7172011-03-31 21:26:24 +000058public:
bsalomon@google.com0b77d682011-08-19 13:28:54 +000059 ProgramCache(const GrGLInterface* gl)
junov@google.comf93e7172011-03-31 21:26:24 +000060 : fCount(0)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000061 , fCurrLRUStamp(0)
62 , fGL(gl) {
junov@google.comf93e7172011-03-31 21:26:24 +000063 }
64
65 ~ProgramCache() {
66 for (int i = 0; i < fCount; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000067 GrGpuGLShaders::DeleteProgram(fGL, &fEntries[i].fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000068 }
69 }
70
71 void abandon() {
72 fCount = 0;
73 }
74
75 void invalidateViewMatrices() {
76 for (int i = 0; i < fCount; ++i) {
77 // set to illegal matrix
78 fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix();
79 }
80 }
81
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000082 GrGLProgram::CachedData* getProgramData(const GrGLProgram& desc) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000083 Entry newEntry;
junov@google.comf7c00f62011-08-18 18:15:16 +000084 newEntry.fKey.setKeyData(desc.keyData());
85
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000086 Entry* entry = fHashCache.find(newEntry.fKey);
junov@google.comf93e7172011-03-31 21:26:24 +000087 if (NULL == entry) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000088 if (!desc.genProgram(fGL, &newEntry.fProgramData)) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000089 return NULL;
90 }
junov@google.comf93e7172011-03-31 21:26:24 +000091 if (fCount < kMaxEntries) {
92 entry = fEntries + fCount;
93 ++fCount;
94 } else {
95 GrAssert(kMaxEntries == fCount);
96 entry = fEntries;
97 for (int i = 1; i < kMaxEntries; ++i) {
98 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
99 entry = fEntries + i;
100 }
101 }
102 fHashCache.remove(entry->fKey, entry);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000103 GrGpuGLShaders::DeleteProgram(fGL, &entry->fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +0000104 }
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000105 entry->copyAndTakeOwnership(newEntry);
junov@google.comf93e7172011-03-31 21:26:24 +0000106 fHashCache.insert(entry->fKey, entry);
107 }
108
109 entry->fLRUStamp = fCurrLRUStamp;
110 if (GR_UINT32_MAX == fCurrLRUStamp) {
111 // wrap around! just trash our LRU, one time hit.
112 for (int i = 0; i < fCount; ++i) {
113 fEntries[i].fLRUStamp = 0;
114 }
115 }
116 ++fCurrLRUStamp;
117 return &entry->fProgramData;
118 }
119};
120
junov@google.com53a55842011-06-08 22:55:10 +0000121void GrGpuGLShaders::abandonResources(){
122 INHERITED::abandonResources();
123 fProgramCache->abandon();
124}
125
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000126void GrGpuGLShaders::DeleteProgram(const GrGLInterface* gl,
127 CachedData* programData) {
128 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
129 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
130 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000131 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
132}
133
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000134////////////////////////////////////////////////////////////////////////////////
135
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000136#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
137
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000138namespace {
139 template <typename T>
140 T random_val(GrRandom* r, T count) {
141 return (T)(int)(r->nextF() * count);
142 }
143};
144
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000145bool GrGpuGLShaders::programUnitTest() {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000146
147 static const int STAGE_OPTS[] = {
148 0,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000149 StageDesc::kNoPerspective_OptFlagBit,
150 StageDesc::kIdentity_CoordMapping
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000151 };
152 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000153 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000154
155 static const int NUM_TESTS = 512;
156
157 // GrRandoms nextU() values have patterns in the low bits
158 // So using nextU() % array_count might never take some values.
159 GrRandom random;
160 for (int t = 0; t < NUM_TESTS; ++t) {
161
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000162#if 0
163 GrPrintf("\nTest Program %d\n-------------\n", t);
164 static const int stop = -1;
165 if (t == stop) {
166 int breakpointhere = 9;
167 }
168#endif
169
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000170 pdesc.fVertexLayout = 0;
171 pdesc.fEmitsPointSize = random.nextF() > .5f;
172 float colorType = random.nextF();
173 if (colorType < 1.f / 3.f) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000174 pdesc.fColorType = ProgramDesc::kAttribute_ColorType;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000175 } else if (colorType < 2.f / 3.f) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000176 pdesc.fColorType = ProgramDesc::kUniform_ColorType;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000177 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000178 pdesc.fColorType = ProgramDesc::kNone_ColorType;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000179 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000180
181 int idx = (int)(random.nextF() * (SkXfermode::kCoeffModesCnt));
182 pdesc.fColorFilterXfermode = (SkXfermode::Mode)idx;
183
184 idx = (int)(random.nextF() * (kNumStages+1));
185 pdesc.fFirstCoverageStage = idx;
186
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000187 bool edgeAA = random.nextF() > .5f;
188 if (edgeAA) {
189 pdesc.fEdgeAANumEdges = random.nextF() * this->getMaxEdges() + 1;
190 pdesc.fEdgeAAConcave = random.nextF() > .5f;
191 } else {
192 pdesc.fEdgeAANumEdges = 0;
193 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000194
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000195 if (fDualSourceBlendingSupport) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000196 pdesc.fDualSrcOutput =
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000197 (ProgramDesc::DualSrcOutput)
198 (int)(random.nextF() * ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000199 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000200 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000201 }
202
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000203 for (int s = 0; s < kNumStages; ++s) {
204 // enable the stage?
205 if (random.nextF() > .5f) {
206 // use separate tex coords?
207 if (random.nextF() > .5f) {
208 int t = (int)(random.nextF() * kMaxTexCoords);
209 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
210 } else {
211 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
212 }
213 }
214 // use text-formatted verts?
215 if (random.nextF() > .5f) {
216 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
217 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000218 idx = (int)(random.nextF() * GR_ARRAY_COUNT(STAGE_OPTS));
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000219 StageDesc& stage = pdesc.fStages[s];
220 stage.fOptFlags = STAGE_OPTS[idx];
221 stage.fModulation = random_val(&random, StageDesc::kModulationCnt);
222 stage.fCoordMapping = random_val(&random, StageDesc::kCoordMappingCnt);
223 stage.fFetchMode = random_val(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000224 // convolution shaders don't work with persp tex matrix
225 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) {
226 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
227 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000228 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000229 stage.fKernelWidth = 4 * random.nextF() + 2;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000230 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000231 CachedData cachedData;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000232 if (!program.genProgram(this->glInterface(), &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000233 return false;
234 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000235 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000236 bool again = false;
237 if (again) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000238 program.genProgram(this->glInterface(), &cachedData);
239 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000240 }
241 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000242 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000243}
244
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000245namespace {
246GrGLBinding get_binding_in_use(const GrGLInterface* gl) {
247 if (gl->supportsDesktop()) {
248 return kDesktop_GrGLBinding;
249 } else {
250 GrAssert(gl->supportsES2());
251 return kES2_GrGLBinding;
252 }
253}
254}
255
256GrGpuGLShaders::GrGpuGLShaders(const GrGLInterface* gl)
257 : GrGpuGL(gl, get_binding_in_use(gl)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000258
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000259 resetContext();
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000260
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000261 f4X4DownsampleFilterSupport = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000262 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000263 fDualSourceBlendingSupport =
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000264 fGLVersion >= 3.3f ||
265 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000266 } else {
267 fDualSourceBlendingSupport = false;
268 }
junov@google.comf93e7172011-03-31 21:26:24 +0000269
270 fProgramData = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000271 fProgramCache = new ProgramCache(gl);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000272
273#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000274 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000275#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000276}
277
278GrGpuGLShaders::~GrGpuGLShaders() {
279 delete fProgramCache;
280}
281
282const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000283 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000284
285 if (GrGLProgram::kSetAsAttribute ==
286 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
287 return fHWDrawState.fSamplerStates[stage].getMatrix();
288 } else {
289 return fProgramData->fTextureMatrices[stage];
290 }
junov@google.comf93e7172011-03-31 21:26:24 +0000291}
292
293void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000294 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000295 if (GrGLProgram::kSetAsAttribute ==
296 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
297 fHWDrawState.fSamplerStates[stage].setMatrix(matrix);
298 } else {
299 fProgramData->fTextureMatrices[stage] = matrix;
300 }
junov@google.comf93e7172011-03-31 21:26:24 +0000301}
302
303void GrGpuGLShaders::resetContext() {
304 INHERITED::resetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000305
junov@google.comf93e7172011-03-31 21:26:24 +0000306 fHWGeometryState.fVertexLayout = 0;
307 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000308 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000309 for (int t = 0; t < kMaxTexCoords; ++t) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000310 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000311 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000312 GL_CALL(EnableVertexAttribArray(GrGLProgram::PositionAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000313
314 fHWProgramID = 0;
315}
316
317void GrGpuGLShaders::flushViewMatrix() {
318 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000319 GrMatrix m;
320 m.setAll(
junov@google.comf93e7172011-03-31 21:26:24 +0000321 GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1,
322 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1,
323 0, 0, GrMatrix::I()[8]);
324 m.setConcat(m, fCurrDrawState.fViewMatrix);
325
326 // ES doesn't allow you to pass true to the transpose param,
327 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000328 GrGLfloat mt[] = {
329 GrScalarToFloat(m[GrMatrix::kMScaleX]),
330 GrScalarToFloat(m[GrMatrix::kMSkewY]),
331 GrScalarToFloat(m[GrMatrix::kMPersp0]),
332 GrScalarToFloat(m[GrMatrix::kMSkewX]),
333 GrScalarToFloat(m[GrMatrix::kMScaleY]),
334 GrScalarToFloat(m[GrMatrix::kMPersp1]),
335 GrScalarToFloat(m[GrMatrix::kMTransX]),
336 GrScalarToFloat(m[GrMatrix::kMTransY]),
337 GrScalarToFloat(m[GrMatrix::kMPersp2])
junov@google.comf93e7172011-03-31 21:26:24 +0000338 };
bsalomon@google.com91961302011-05-09 18:39:58 +0000339
340 if (GrGLProgram::kSetAsAttribute ==
341 fProgramData->fUniLocations.fViewMatrixUni) {
342 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000343 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
344 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
345 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000346 } else {
347 GrAssert(GrGLProgram::kUnusedUniform !=
348 fProgramData->fUniLocations.fViewMatrixUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000349 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
350 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000351 }
junov@google.comf93e7172011-03-31 21:26:24 +0000352}
353
junov@google.com6acc9b32011-05-16 18:32:07 +0000354void GrGpuGLShaders::flushTextureDomain(int s) {
355 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
356 if (GrGLProgram::kUnusedUniform != uni) {
twiz@google.com76b82742011-06-02 20:30:02 +0000357 const GrRect &texDom =
junov@google.com6acc9b32011-05-16 18:32:07 +0000358 fCurrDrawState.fSamplerStates[s].getTextureDomain();
359
twiz@google.com76b82742011-06-02 20:30:02 +0000360 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
361 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000362
junov@google.com2f839402011-05-24 15:13:01 +0000363 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000364
junov@google.com2f839402011-05-24 15:13:01 +0000365 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000366 GrScalarToFloat(texDom.left()),
367 GrScalarToFloat(texDom.top()),
368 GrScalarToFloat(texDom.right()),
369 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000370 };
371
372 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
373 GrGLTexture::Orientation orientation = texture->orientation();
374
375 // vertical flip if necessary
376 if (GrGLTexture::kBottomUp_Orientation == orientation) {
377 values[1] = 1.0f - values[1];
378 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000379 // The top and bottom were just flipped, so correct the ordering
380 // of elements so that values = (l, t, r, b).
381 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000382 }
383
384 values[0] *= SkScalarToFloat(texture->contentScaleX());
385 values[2] *= SkScalarToFloat(texture->contentScaleX());
386 values[1] *= SkScalarToFloat(texture->contentScaleY());
387 values[3] *= SkScalarToFloat(texture->contentScaleY());
388
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000389 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000390 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000391 }
392}
393
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000394void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000395 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000396 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
397 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000398 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000399 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
400 getHWSamplerMatrix(s) != getSamplerMatrix(s))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000401
bsalomon@google.com91961302011-05-09 18:39:58 +0000402 GrAssert(NULL != fCurrDrawState.fTextures[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000403
bsalomon@google.com91961302011-05-09 18:39:58 +0000404 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000405
bsalomon@google.com91961302011-05-09 18:39:58 +0000406 GrMatrix m = getSamplerMatrix(s);
407 GrSamplerState::SampleMode mode =
408 fCurrDrawState.fSamplerStates[s].getSampleMode();
409 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000410
bsalomon@google.com91961302011-05-09 18:39:58 +0000411 // ES doesn't allow you to pass true to the transpose param,
412 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000413 GrGLfloat mt[] = {
414 GrScalarToFloat(m[GrMatrix::kMScaleX]),
415 GrScalarToFloat(m[GrMatrix::kMSkewY]),
416 GrScalarToFloat(m[GrMatrix::kMPersp0]),
417 GrScalarToFloat(m[GrMatrix::kMSkewX]),
418 GrScalarToFloat(m[GrMatrix::kMScaleY]),
419 GrScalarToFloat(m[GrMatrix::kMPersp1]),
420 GrScalarToFloat(m[GrMatrix::kMTransX]),
421 GrScalarToFloat(m[GrMatrix::kMTransY]),
422 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000423 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000424
bsalomon@google.com91961302011-05-09 18:39:58 +0000425 if (GrGLProgram::kSetAsAttribute ==
426 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
427 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000428 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
429 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
430 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000431 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000432 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000433 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000434 recordHWSamplerMatrix(s, getSamplerMatrix(s));
435 }
436 }
junov@google.comf93e7172011-03-31 21:26:24 +0000437}
438
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000439void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000440
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000441 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
442 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com91961302011-05-09 18:39:58 +0000443 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000444 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
445 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
446 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000447
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000448 GrScalar centerX1 = sampler.getRadial2CenterX1();
449 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000450
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000451 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000452
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000453 // when were in the degenerate (linear) case the second
454 // value will be INF but the program doesn't read it. (We
455 // use the same 6 uniforms even though we don't need them
456 // all in the linear case just to keep the code complexity
457 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000458 float values[6] = {
459 GrScalarToFloat(a),
460 1 / (2.f * values[0]),
461 GrScalarToFloat(centerX1),
462 GrScalarToFloat(radius0),
463 GrScalarToFloat(GrMul(radius0, radius0)),
464 sampler.isRadial2PosRoot() ? 1.f : -1.f
465 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000466 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000467 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
468 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
469 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
470 }
471}
472
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000473void GrGpuGLShaders::flushConvolution(int s) {
474 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
475 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
476 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000477 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
478 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000479 }
480 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
481 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000482 GL_CALL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000483 }
484}
485
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000486void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000487 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000488 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000489 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000490 if (texture->allocatedWidth() != fProgramData->fTextureWidth[s] ||
491 texture->allocatedHeight() != fProgramData->fTextureWidth[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000492
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000493 float texelSize[] = {1.f / texture->allocatedWidth(),
494 1.f / texture->allocatedHeight()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000495 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000496 }
497 }
junov@google.comf93e7172011-03-31 21:26:24 +0000498}
499
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000500void GrGpuGLShaders::flushEdgeAAData() {
501 const int& uni = fProgramData->fUniLocations.fEdgesUni;
502 if (GrGLProgram::kUnusedUniform != uni) {
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000503 int count = fCurrDrawState.fEdgeAANumEdges;
504 Edge edges[kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000505 // Flip the edges in Y
506 float height = fCurrDrawState.fRenderTarget->height();
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000507 for (int i = 0; i < count; ++i) {
508 edges[i] = fCurrDrawState.fEdgeAAEdges[i];
509 float b = edges[i].fY;
510 edges[i].fY = -b;
511 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000512 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000513 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000514 }
515}
516
Scroggo01b87ec2011-05-11 18:05:38 +0000517static const float ONE_OVER_255 = 1.f / 255.f;
518
519#define GR_COLOR_TO_VEC4(color) {\
520 GrColorUnpackR(color) * ONE_OVER_255,\
521 GrColorUnpackG(color) * ONE_OVER_255,\
522 GrColorUnpackB(color) * ONE_OVER_255,\
523 GrColorUnpackA(color) * ONE_OVER_255 \
524}
525
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000526void GrGpuGLShaders::flushColor() {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000527 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000528 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000529 // color will be specified per-vertex as an attribute
530 // invalidate the const vertex attrib color
531 fHWDrawState.fColor = GrColor_ILLEGAL;
532 } else {
533 switch (desc.fColorType) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000534 case ProgramDesc::kAttribute_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000535 if (fHWDrawState.fColor != fCurrDrawState.fColor) {
536 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000537 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000538 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
539 c));
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000540 fHWDrawState.fColor = fCurrDrawState.fColor;
541 }
542 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000543 case ProgramDesc::kUniform_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000544 if (fProgramData->fColor != fCurrDrawState.fColor) {
545 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000546 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com91961302011-05-09 18:39:58 +0000547 GrAssert(GrGLProgram::kUnusedUniform !=
548 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000549 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
550 1, c));
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000551 fProgramData->fColor = fCurrDrawState.fColor;
552 }
553 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000554 case ProgramDesc::kNone_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000555 GrAssert(0xffffffff == fCurrDrawState.fColor);
556 break;
557 default:
558 GrCrash("Unknown color type.");
559 }
560 }
Scroggo97c88c22011-05-11 14:05:25 +0000561 if (fProgramData->fUniLocations.fColorFilterUni
562 != GrGLProgram::kUnusedUniform
563 && fProgramData->fColorFilterColor
564 != fCurrDrawState.fColorFilterColor) {
Scroggo01b87ec2011-05-11 18:05:38 +0000565 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColorFilterColor);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000566 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
Scroggo97c88c22011-05-11 14:05:25 +0000567 fProgramData->fColorFilterColor = fCurrDrawState.fColorFilterColor;
568 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000569}
570
571
junov@google.comf93e7172011-03-31 21:26:24 +0000572bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
573 if (!flushGLStateCommon(type)) {
574 return false;
575 }
576
577 if (fDirtyFlags.fRenderTargetChanged) {
578 // our coords are in pixel space and the GL matrices map to NDC
579 // so if the viewport changed, our matrix is now wrong.
junov@google.comf93e7172011-03-31 21:26:24 +0000580 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000581 // we assume all shader matrices may be wrong after viewport changes
582 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000583 }
584
junov@google.comf93e7172011-03-31 21:26:24 +0000585 buildProgram(type);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000586 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000587 if (NULL == fProgramData) {
588 GrAssert(!"Failed to create program!");
589 return false;
590 }
junov@google.comf93e7172011-03-31 21:26:24 +0000591
592 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000593 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000594 fHWProgramID = fProgramData->fProgramID;
595 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000596 GrBlendCoeff srcCoeff = fCurrDrawState.fSrcBlend;
597 GrBlendCoeff dstCoeff = fCurrDrawState.fDstBlend;
598
599 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
600 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000601
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000602 this->flushColor();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000603
bsalomon@google.com91961302011-05-09 18:39:58 +0000604 GrMatrix* currViewMatrix;
605 if (GrGLProgram::kSetAsAttribute ==
606 fProgramData->fUniLocations.fViewMatrixUni) {
607 currViewMatrix = &fHWDrawState.fViewMatrix;
608 } else {
609 currViewMatrix = &fProgramData->fViewMatrix;
610 }
junov@google.comf93e7172011-03-31 21:26:24 +0000611
bsalomon@google.com91961302011-05-09 18:39:58 +0000612 if (*currViewMatrix != fCurrDrawState.fViewMatrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000613 flushViewMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000614 *currViewMatrix = fCurrDrawState.fViewMatrix;
junov@google.comf93e7172011-03-31 21:26:24 +0000615 }
616
617 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000618 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000619
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000620 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000621
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000622 this->flushConvolution(s);
623
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000624 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000625
626 this->flushTextureDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000627 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000628 this->flushEdgeAAData();
junov@google.comf93e7172011-03-31 21:26:24 +0000629 resetDirtyFlags();
630 return true;
631}
632
633void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000634}
635
636void GrGpuGLShaders::setupGeometry(int* startVertex,
637 int* startIndex,
638 int vertexCount,
639 int indexCount) {
640
641 int newColorOffset;
642 int newTexCoordOffsets[kMaxTexCoords];
643
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000644 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
645 this->getGeomSrc().fVertexLayout,
646 newTexCoordOffsets,
647 &newColorOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000648 int oldColorOffset;
649 int oldTexCoordOffsets[kMaxTexCoords];
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000650 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
651 fHWGeometryState.fVertexLayout,
652 oldTexCoordOffsets,
653 &oldColorOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000654 bool indexed = NULL != startIndex;
655
656 int extraVertexOffset;
657 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000658 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000659
660 GrGLenum scalarType;
661 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000662 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000663 scalarType = GrGLTextType;
664 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
665 } else {
666 scalarType = GrGLType;
667 texCoordNorm = false;
668 }
669
670 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
671 *startVertex = 0;
672 if (indexed) {
673 *startIndex += extraIndexOffset;
674 }
675
676 // all the Pointers must be set if any of these are true
677 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
678 vertexOffset != fHWGeometryState.fVertexOffset ||
679 newStride != oldStride;
680
681 // position and tex coord offsets change if above conditions are true
682 // or the type/normalization changed based on text vs nontext type coords.
683 bool posAndTexChange = allOffsetsChange ||
684 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
685 (kTextFormat_VertexLayoutBit &
686 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000687 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000688
689 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000690 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000691 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000692 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000693 fHWGeometryState.fVertexOffset = vertexOffset;
694 }
695
696 for (int t = 0; t < kMaxTexCoords; ++t) {
697 if (newTexCoordOffsets[t] > 0) {
698 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000699 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000700 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000701 GL_CALL(EnableVertexAttribArray(idx));
702 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000703 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000704 } else if (posAndTexChange ||
705 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000706 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000707 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000708 }
709 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000710 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000711 }
712 }
713
714 if (newColorOffset > 0) {
715 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000716 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000717 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000718 GL_CALL(EnableVertexAttribArray(idx));
719 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000720 true, newStride, colorOffset));
721 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000722 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000723 true, newStride, colorOffset));
724 }
725 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000726 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000727 }
728
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000729 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000730 fHWGeometryState.fArrayPtrsDirty = false;
731}
732
733void GrGpuGLShaders::buildProgram(GrPrimitiveType type) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000734 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
junov@google.comf93e7172011-03-31 21:26:24 +0000735
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000736 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000737 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000738
739 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
740
741 bool requiresAttributeColors = desc.fVertexLayout & kColor_VertexLayoutBit;
742 // fColorType records how colors are specified for the program. Strip
743 // the bit from the layout to avoid false negatives when searching for an
744 // existing program in the cache.
745 desc.fVertexLayout &= ~(kColor_VertexLayoutBit);
746
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000747 desc.fColorFilterXfermode = fCurrDrawState.fColorFilterXfermode;
748
junov@google.comf93e7172011-03-31 21:26:24 +0000749#if GR_AGGRESSIVE_SHADER_OPTS
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000750 if (!requiresAttributeColors && (0xffffffff == fCurrDrawState.fColor)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000751 desc.fColorType = ProgramDesc::kNone_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000752 } else
junov@google.comf93e7172011-03-31 21:26:24 +0000753#endif
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000754#if GR_GL_NO_CONSTANT_ATTRIBUTES
755 if (!requiresAttributeColors) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000756 desc.fColorType = ProgramDesc::kUniform_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000757 } else
758#endif
759 {
bsalomon@google.com6a77cc52011-04-28 17:33:34 +0000760 if (requiresAttributeColors) {} // suppress unused var warning
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000761 desc.fColorType = ProgramDesc::kAttribute_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000762 }
junov@google.comf93e7172011-03-31 21:26:24 +0000763
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000764 desc.fEdgeAANumEdges = fCurrDrawState.fEdgeAANumEdges;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000765 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 && SkToBool(fCurrDrawState.fFlagBits & kEdgeAAConcave_StateBit);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000766
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000767 int lastEnabledStage = -1;
768
junov@google.comf93e7172011-03-31 21:26:24 +0000769 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000770 StageDesc& stage = desc.fStages[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000771
tomhudson@google.com0d831722011-06-02 15:37:14 +0000772 stage.fOptFlags = 0;
773 stage.setEnabled(this->isStageEnabled(s));
junov@google.comf93e7172011-03-31 21:26:24 +0000774
tomhudson@google.com0d831722011-06-02 15:37:14 +0000775 if (stage.isEnabled()) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000776 lastEnabledStage = s;
junov@google.comf93e7172011-03-31 21:26:24 +0000777 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
778 GrAssert(NULL != texture);
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000779 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000780 // we matrix to invert when orientation is TopDown, so make sure
781 // we aren't in that case before flagging as identity.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000782 if (TextureMatrixIsIdentity(texture, sampler)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000783 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000784 } else if (!getSamplerMatrix(s).hasPerspective()) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000785 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000786 }
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000787 switch (sampler.getSampleMode()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000788 case GrSamplerState::kNormal_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000789 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000790 break;
791 case GrSamplerState::kRadial_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000792 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000793 break;
794 case GrSamplerState::kRadial2_SampleMode:
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000795 if (sampler.radial2IsDegenerate()) {
796 stage.fCoordMapping =
797 StageDesc::kRadial2GradientDegenerate_CoordMapping;
798 } else {
799 stage.fCoordMapping =
800 StageDesc::kRadial2Gradient_CoordMapping;
801 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000802 break;
803 case GrSamplerState::kSweep_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000804 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000805 break;
806 default:
807 GrCrash("Unexpected sample mode!");
808 break;
809 }
810
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000811 switch (sampler.getFilter()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000812 // these both can use a regular texture2D()
813 case GrSamplerState::kNearest_Filter:
814 case GrSamplerState::kBilinear_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000815 stage.fFetchMode = StageDesc::kSingle_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000816 break;
817 // performs 4 texture2D()s
818 case GrSamplerState::k4x4Downsample_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000819 stage.fFetchMode = StageDesc::k2x2_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000820 break;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000821 // performs fKernelWidth texture2D()s
822 case GrSamplerState::kConvolution_Filter:
823 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
824 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000825 default:
826 GrCrash("Unexpected filter!");
827 break;
junov@google.comf93e7172011-03-31 21:26:24 +0000828 }
829
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000830 if (sampler.hasTextureDomain()) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000831 GrAssert(GrSamplerState::kClamp_WrapMode ==
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000832 sampler.getWrapX() &&
833 GrSamplerState::kClamp_WrapMode ==
834 sampler.getWrapY());
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000835 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
junov@google.com6acc9b32011-05-16 18:32:07 +0000836 }
837
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000838 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000839 stage.fModulation = StageDesc::kAlpha_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000840 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000841 stage.fModulation = StageDesc::kColor_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000842 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000843 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) {
844 stage.fKernelWidth = sampler.getKernelWidth();
845 } else {
846 stage.fKernelWidth = 0;
847 }
junov@google.comf93e7172011-03-31 21:26:24 +0000848 } else {
849 stage.fOptFlags = 0;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000850 stage.fCoordMapping = (StageDesc::CoordMapping)0;
851 stage.fModulation = (StageDesc::Modulation)0;
junov@google.comf93e7172011-03-31 21:26:24 +0000852 }
853 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000854
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000855 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000856 // use canonical value when coverage/color distinction won't affect
857 // generated code to prevent duplicate programs.
858 desc.fFirstCoverageStage = kNumStages;
859 if (fCurrDrawState.fFirstCoverageStage <= lastEnabledStage) {
860 // color filter is applied between color/coverage computation
861 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
862 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
863 }
864
865 // We could consider cases where the final color is solid (0xff alpha)
866 // and the dst coeff can correctly be set to a non-dualsrc gl value.
867 // (e.g. solid draw, and dst coeff is kZero. It's correct to make
868 // the dst coeff be kISA. Or solid draw with kSA can be tweaked to be
869 // kOne).
870 if (fDualSourceBlendingSupport) {
871 if (kZero_BlendCoeff == fCurrDrawState.fDstBlend) {
872 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000873 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000874 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
875 } else if (kSA_BlendCoeff == fCurrDrawState.fDstBlend) {
876 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
877 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000878 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000879 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
880 } else if (kSC_BlendCoeff == fCurrDrawState.fDstBlend) {
881 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
882 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000883 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000884 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
885 }
886 }
887 }
junov@google.comf93e7172011-03-31 21:26:24 +0000888}