blob: a9a39536739c373aaff5252464079bcbe74948ed [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;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000188 if (edgeAA) {
189 bool vertexEdgeAA = random.nextF() > .5f;
190 if (vertexEdgeAA) {
191 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
192 if (this->supportsShaderDerivatives()) {
193 pdesc.fVertexEdgeType = random.nextF() > 0.5f ?
194 kHairQuad_EdgeType :
195 kHairLine_EdgeType;
196 } else {
197 pdesc.fVertexEdgeType = kHairLine_EdgeType;
198 }
199 pdesc.fEdgeAANumEdges = 0;
200 } else {
201 pdesc.fEdgeAANumEdges = random.nextF() * this->getMaxEdges() + 1;
202 pdesc.fEdgeAAConcave = random.nextF() > .5f;
203 }
204 } else {
205 pdesc.fEdgeAANumEdges = 0;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000206 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000207
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000208 if (fDualSourceBlendingSupport) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000209 pdesc.fDualSrcOutput =
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000210 (ProgramDesc::DualSrcOutput)
211 (int)(random.nextF() * ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000212 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000213 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000214 }
215
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000216 for (int s = 0; s < kNumStages; ++s) {
217 // enable the stage?
218 if (random.nextF() > .5f) {
219 // use separate tex coords?
220 if (random.nextF() > .5f) {
221 int t = (int)(random.nextF() * kMaxTexCoords);
222 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
223 } else {
224 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
225 }
226 }
227 // use text-formatted verts?
228 if (random.nextF() > .5f) {
229 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
230 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000231 idx = (int)(random.nextF() * GR_ARRAY_COUNT(STAGE_OPTS));
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000232 StageDesc& stage = pdesc.fStages[s];
233 stage.fOptFlags = STAGE_OPTS[idx];
234 stage.fModulation = random_val(&random, StageDesc::kModulationCnt);
235 stage.fCoordMapping = random_val(&random, StageDesc::kCoordMappingCnt);
236 stage.fFetchMode = random_val(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000237 // convolution shaders don't work with persp tex matrix
238 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) {
239 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
240 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000241 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000242 stage.fKernelWidth = 4 * random.nextF() + 2;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000243 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000244 CachedData cachedData;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000245 if (!program.genProgram(this->glInterface(), &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000246 return false;
247 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000248 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000249 bool again = false;
250 if (again) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000251 program.genProgram(this->glInterface(), &cachedData);
252 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000253 }
254 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000255 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000256}
257
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000258namespace {
259GrGLBinding get_binding_in_use(const GrGLInterface* gl) {
260 if (gl->supportsDesktop()) {
261 return kDesktop_GrGLBinding;
262 } else {
263 GrAssert(gl->supportsES2());
264 return kES2_GrGLBinding;
265 }
266}
267}
268
269GrGpuGLShaders::GrGpuGLShaders(const GrGLInterface* gl)
270 : GrGpuGL(gl, get_binding_in_use(gl)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000271
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000272 fShaderSupport = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000273 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000274 fDualSourceBlendingSupport =
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000275 fGLVersion >= 3.3f ||
276 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000277 fShaderDerivativeSupport = true;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000278 } else {
279 fDualSourceBlendingSupport = false;
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000280 fShaderDerivativeSupport =
281 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000282 }
junov@google.comf93e7172011-03-31 21:26:24 +0000283
284 fProgramData = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000285 fProgramCache = new ProgramCache(gl);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000286
287#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000288 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000289#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000290}
291
292GrGpuGLShaders::~GrGpuGLShaders() {
293 delete fProgramCache;
294}
295
296const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000297 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000298
299 if (GrGLProgram::kSetAsAttribute ==
300 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
301 return fHWDrawState.fSamplerStates[stage].getMatrix();
302 } else {
303 return fProgramData->fTextureMatrices[stage];
304 }
junov@google.comf93e7172011-03-31 21:26:24 +0000305}
306
307void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000308 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000309 if (GrGLProgram::kSetAsAttribute ==
310 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
311 fHWDrawState.fSamplerStates[stage].setMatrix(matrix);
312 } else {
313 fProgramData->fTextureMatrices[stage] = matrix;
314 }
junov@google.comf93e7172011-03-31 21:26:24 +0000315}
316
317void GrGpuGLShaders::resetContext() {
318 INHERITED::resetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000319
junov@google.comf93e7172011-03-31 21:26:24 +0000320 fHWGeometryState.fVertexLayout = 0;
321 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000322 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000323 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000324 for (int t = 0; t < kMaxTexCoords; ++t) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000325 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000326 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000327 GL_CALL(EnableVertexAttribArray(GrGLProgram::PositionAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000328
329 fHWProgramID = 0;
330}
331
332void GrGpuGLShaders::flushViewMatrix() {
333 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000334 GrMatrix m;
335 m.setAll(
junov@google.comf93e7172011-03-31 21:26:24 +0000336 GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1,
337 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1,
338 0, 0, GrMatrix::I()[8]);
339 m.setConcat(m, fCurrDrawState.fViewMatrix);
340
341 // ES doesn't allow you to pass true to the transpose param,
342 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000343 GrGLfloat mt[] = {
344 GrScalarToFloat(m[GrMatrix::kMScaleX]),
345 GrScalarToFloat(m[GrMatrix::kMSkewY]),
346 GrScalarToFloat(m[GrMatrix::kMPersp0]),
347 GrScalarToFloat(m[GrMatrix::kMSkewX]),
348 GrScalarToFloat(m[GrMatrix::kMScaleY]),
349 GrScalarToFloat(m[GrMatrix::kMPersp1]),
350 GrScalarToFloat(m[GrMatrix::kMTransX]),
351 GrScalarToFloat(m[GrMatrix::kMTransY]),
352 GrScalarToFloat(m[GrMatrix::kMPersp2])
junov@google.comf93e7172011-03-31 21:26:24 +0000353 };
bsalomon@google.com91961302011-05-09 18:39:58 +0000354
355 if (GrGLProgram::kSetAsAttribute ==
356 fProgramData->fUniLocations.fViewMatrixUni) {
357 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000358 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
359 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
360 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000361 } else {
362 GrAssert(GrGLProgram::kUnusedUniform !=
363 fProgramData->fUniLocations.fViewMatrixUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000364 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
365 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000366 }
junov@google.comf93e7172011-03-31 21:26:24 +0000367}
368
junov@google.com6acc9b32011-05-16 18:32:07 +0000369void GrGpuGLShaders::flushTextureDomain(int s) {
370 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
371 if (GrGLProgram::kUnusedUniform != uni) {
twiz@google.com76b82742011-06-02 20:30:02 +0000372 const GrRect &texDom =
junov@google.com6acc9b32011-05-16 18:32:07 +0000373 fCurrDrawState.fSamplerStates[s].getTextureDomain();
374
twiz@google.com76b82742011-06-02 20:30:02 +0000375 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
376 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000377
junov@google.com2f839402011-05-24 15:13:01 +0000378 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000379
junov@google.com2f839402011-05-24 15:13:01 +0000380 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000381 GrScalarToFloat(texDom.left()),
382 GrScalarToFloat(texDom.top()),
383 GrScalarToFloat(texDom.right()),
384 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000385 };
386
387 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
388 GrGLTexture::Orientation orientation = texture->orientation();
389
390 // vertical flip if necessary
391 if (GrGLTexture::kBottomUp_Orientation == orientation) {
392 values[1] = 1.0f - values[1];
393 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000394 // The top and bottom were just flipped, so correct the ordering
395 // of elements so that values = (l, t, r, b).
396 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000397 }
398
399 values[0] *= SkScalarToFloat(texture->contentScaleX());
400 values[2] *= SkScalarToFloat(texture->contentScaleX());
401 values[1] *= SkScalarToFloat(texture->contentScaleY());
402 values[3] *= SkScalarToFloat(texture->contentScaleY());
403
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000404 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000405 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000406 }
407}
408
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000409void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000410 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000411 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
412 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000413 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000414 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
415 getHWSamplerMatrix(s) != getSamplerMatrix(s))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000416
bsalomon@google.com91961302011-05-09 18:39:58 +0000417 GrAssert(NULL != fCurrDrawState.fTextures[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000418
bsalomon@google.com91961302011-05-09 18:39:58 +0000419 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000420
bsalomon@google.com91961302011-05-09 18:39:58 +0000421 GrMatrix m = getSamplerMatrix(s);
422 GrSamplerState::SampleMode mode =
423 fCurrDrawState.fSamplerStates[s].getSampleMode();
424 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000425
bsalomon@google.com91961302011-05-09 18:39:58 +0000426 // ES doesn't allow you to pass true to the transpose param,
427 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000428 GrGLfloat mt[] = {
429 GrScalarToFloat(m[GrMatrix::kMScaleX]),
430 GrScalarToFloat(m[GrMatrix::kMSkewY]),
431 GrScalarToFloat(m[GrMatrix::kMPersp0]),
432 GrScalarToFloat(m[GrMatrix::kMSkewX]),
433 GrScalarToFloat(m[GrMatrix::kMScaleY]),
434 GrScalarToFloat(m[GrMatrix::kMPersp1]),
435 GrScalarToFloat(m[GrMatrix::kMTransX]),
436 GrScalarToFloat(m[GrMatrix::kMTransY]),
437 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000438 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000439
bsalomon@google.com91961302011-05-09 18:39:58 +0000440 if (GrGLProgram::kSetAsAttribute ==
441 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
442 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000443 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
444 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
445 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000446 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000447 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000448 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000449 recordHWSamplerMatrix(s, getSamplerMatrix(s));
450 }
451 }
junov@google.comf93e7172011-03-31 21:26:24 +0000452}
453
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000454void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000455
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000456 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
457 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com91961302011-05-09 18:39:58 +0000458 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000459 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
460 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
461 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000462
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000463 GrScalar centerX1 = sampler.getRadial2CenterX1();
464 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000465
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000466 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000467
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000468 // when were in the degenerate (linear) case the second
469 // value will be INF but the program doesn't read it. (We
470 // use the same 6 uniforms even though we don't need them
471 // all in the linear case just to keep the code complexity
472 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000473 float values[6] = {
474 GrScalarToFloat(a),
475 1 / (2.f * values[0]),
476 GrScalarToFloat(centerX1),
477 GrScalarToFloat(radius0),
478 GrScalarToFloat(GrMul(radius0, radius0)),
479 sampler.isRadial2PosRoot() ? 1.f : -1.f
480 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000481 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000482 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
483 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
484 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
485 }
486}
487
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000488void GrGpuGLShaders::flushConvolution(int s) {
489 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
490 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
491 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000492 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
493 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000494 }
495 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
496 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000497 GL_CALL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000498 }
499}
500
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000501void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000502 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000503 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000504 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000505 if (texture->allocatedWidth() != fProgramData->fTextureWidth[s] ||
506 texture->allocatedHeight() != fProgramData->fTextureWidth[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000507
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000508 float texelSize[] = {1.f / texture->allocatedWidth(),
509 1.f / texture->allocatedHeight()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000510 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000511 }
512 }
junov@google.comf93e7172011-03-31 21:26:24 +0000513}
514
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000515void GrGpuGLShaders::flushEdgeAAData() {
516 const int& uni = fProgramData->fUniLocations.fEdgesUni;
517 if (GrGLProgram::kUnusedUniform != uni) {
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000518 int count = fCurrDrawState.fEdgeAANumEdges;
519 Edge edges[kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000520 // Flip the edges in Y
521 float height = fCurrDrawState.fRenderTarget->height();
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000522 for (int i = 0; i < count; ++i) {
523 edges[i] = fCurrDrawState.fEdgeAAEdges[i];
524 float b = edges[i].fY;
525 edges[i].fY = -b;
526 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000527 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000528 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000529 }
530}
531
Scroggo01b87ec2011-05-11 18:05:38 +0000532static const float ONE_OVER_255 = 1.f / 255.f;
533
534#define GR_COLOR_TO_VEC4(color) {\
535 GrColorUnpackR(color) * ONE_OVER_255,\
536 GrColorUnpackG(color) * ONE_OVER_255,\
537 GrColorUnpackB(color) * ONE_OVER_255,\
538 GrColorUnpackA(color) * ONE_OVER_255 \
539}
540
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000541void GrGpuGLShaders::flushColor() {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000542 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000543 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000544 // color will be specified per-vertex as an attribute
545 // invalidate the const vertex attrib color
546 fHWDrawState.fColor = GrColor_ILLEGAL;
547 } else {
548 switch (desc.fColorType) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000549 case ProgramDesc::kAttribute_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000550 if (fHWDrawState.fColor != fCurrDrawState.fColor) {
551 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000552 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000553 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
554 c));
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000555 fHWDrawState.fColor = fCurrDrawState.fColor;
556 }
557 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000558 case ProgramDesc::kUniform_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000559 if (fProgramData->fColor != fCurrDrawState.fColor) {
560 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000561 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com91961302011-05-09 18:39:58 +0000562 GrAssert(GrGLProgram::kUnusedUniform !=
563 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000564 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
565 1, c));
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000566 fProgramData->fColor = fCurrDrawState.fColor;
567 }
568 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000569 case ProgramDesc::kNone_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000570 GrAssert(0xffffffff == fCurrDrawState.fColor);
571 break;
572 default:
573 GrCrash("Unknown color type.");
574 }
575 }
Scroggo97c88c22011-05-11 14:05:25 +0000576 if (fProgramData->fUniLocations.fColorFilterUni
577 != GrGLProgram::kUnusedUniform
578 && fProgramData->fColorFilterColor
579 != fCurrDrawState.fColorFilterColor) {
Scroggo01b87ec2011-05-11 18:05:38 +0000580 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColorFilterColor);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000581 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
Scroggo97c88c22011-05-11 14:05:25 +0000582 fProgramData->fColorFilterColor = fCurrDrawState.fColorFilterColor;
583 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000584}
585
586
junov@google.comf93e7172011-03-31 21:26:24 +0000587bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
588 if (!flushGLStateCommon(type)) {
589 return false;
590 }
591
592 if (fDirtyFlags.fRenderTargetChanged) {
593 // our coords are in pixel space and the GL matrices map to NDC
594 // so if the viewport changed, our matrix is now wrong.
junov@google.comf93e7172011-03-31 21:26:24 +0000595 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000596 // we assume all shader matrices may be wrong after viewport changes
597 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000598 }
599
junov@google.comf93e7172011-03-31 21:26:24 +0000600 buildProgram(type);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000601 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000602 if (NULL == fProgramData) {
603 GrAssert(!"Failed to create program!");
604 return false;
605 }
junov@google.comf93e7172011-03-31 21:26:24 +0000606
607 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000608 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000609 fHWProgramID = fProgramData->fProgramID;
610 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000611 GrBlendCoeff srcCoeff = fCurrDrawState.fSrcBlend;
612 GrBlendCoeff dstCoeff = fCurrDrawState.fDstBlend;
613
614 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
615 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000616
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000617 this->flushColor();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000618
bsalomon@google.com91961302011-05-09 18:39:58 +0000619 GrMatrix* currViewMatrix;
620 if (GrGLProgram::kSetAsAttribute ==
621 fProgramData->fUniLocations.fViewMatrixUni) {
622 currViewMatrix = &fHWDrawState.fViewMatrix;
623 } else {
624 currViewMatrix = &fProgramData->fViewMatrix;
625 }
junov@google.comf93e7172011-03-31 21:26:24 +0000626
bsalomon@google.com91961302011-05-09 18:39:58 +0000627 if (*currViewMatrix != fCurrDrawState.fViewMatrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000628 flushViewMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000629 *currViewMatrix = fCurrDrawState.fViewMatrix;
junov@google.comf93e7172011-03-31 21:26:24 +0000630 }
631
632 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000633 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000634
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000635 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000636
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000637 this->flushConvolution(s);
638
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000639 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000640
641 this->flushTextureDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000642 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000643 this->flushEdgeAAData();
junov@google.comf93e7172011-03-31 21:26:24 +0000644 resetDirtyFlags();
645 return true;
646}
647
648void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000649}
650
651void GrGpuGLShaders::setupGeometry(int* startVertex,
652 int* startIndex,
653 int vertexCount,
654 int indexCount) {
655
656 int newColorOffset;
657 int newTexCoordOffsets[kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000658 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000659
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000660 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
661 this->getGeomSrc().fVertexLayout,
662 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000663 &newColorOffset,
664 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000665 int oldColorOffset;
666 int oldTexCoordOffsets[kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000667 int oldEdgeOffset;
668
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000669 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
670 fHWGeometryState.fVertexLayout,
671 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000672 &oldColorOffset,
673 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000674 bool indexed = NULL != startIndex;
675
676 int extraVertexOffset;
677 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000678 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000679
680 GrGLenum scalarType;
681 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000682 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000683 scalarType = GrGLTextType;
684 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
685 } else {
686 scalarType = GrGLType;
687 texCoordNorm = false;
688 }
689
690 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
691 *startVertex = 0;
692 if (indexed) {
693 *startIndex += extraIndexOffset;
694 }
695
696 // all the Pointers must be set if any of these are true
697 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
698 vertexOffset != fHWGeometryState.fVertexOffset ||
699 newStride != oldStride;
700
701 // position and tex coord offsets change if above conditions are true
702 // or the type/normalization changed based on text vs nontext type coords.
703 bool posAndTexChange = allOffsetsChange ||
704 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
705 (kTextFormat_VertexLayoutBit &
706 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000707 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000708
709 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000710 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000711 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000712 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000713 fHWGeometryState.fVertexOffset = vertexOffset;
714 }
715
716 for (int t = 0; t < kMaxTexCoords; ++t) {
717 if (newTexCoordOffsets[t] > 0) {
718 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000719 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000720 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000721 GL_CALL(EnableVertexAttribArray(idx));
722 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000723 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000724 } else if (posAndTexChange ||
725 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000726 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000727 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000728 }
729 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000730 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000731 }
732 }
733
734 if (newColorOffset > 0) {
735 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000736 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000737 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000738 GL_CALL(EnableVertexAttribArray(idx));
739 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000740 true, newStride, colorOffset));
741 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000742 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000743 true, newStride, colorOffset));
744 }
745 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000746 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000747 }
748
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000749 if (newEdgeOffset > 0) {
750 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
751 int idx = GrGLProgram::EdgeAttributeIdx();
752 if (oldEdgeOffset <= 0) {
753 GL_CALL(EnableVertexAttribArray(idx));
754 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
755 false, newStride, edgeOffset));
756 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
757 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
758 false, newStride, edgeOffset));
759 }
760 } else if (oldEdgeOffset > 0) {
761 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
762 }
763
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000764 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000765 fHWGeometryState.fArrayPtrsDirty = false;
766}
767
768void GrGpuGLShaders::buildProgram(GrPrimitiveType type) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000769 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
junov@google.comf93e7172011-03-31 21:26:24 +0000770
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000771 // The descriptor is used as a cache key. Thus when a field of the
772 // descriptor will not affect program generation (because of the vertex
773 // layout in use or other descriptor field settings) it should be set
774 // to a canonical value to avoid duplicate programs with different keys.
775
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000776 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000777 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000778
779 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
780
781 bool requiresAttributeColors = desc.fVertexLayout & kColor_VertexLayoutBit;
782 // fColorType records how colors are specified for the program. Strip
783 // the bit from the layout to avoid false negatives when searching for an
784 // existing program in the cache.
785 desc.fVertexLayout &= ~(kColor_VertexLayoutBit);
786
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000787 desc.fColorFilterXfermode = fCurrDrawState.fColorFilterXfermode;
788
junov@google.comf93e7172011-03-31 21:26:24 +0000789#if GR_AGGRESSIVE_SHADER_OPTS
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000790 if (!requiresAttributeColors && (0xffffffff == fCurrDrawState.fColor)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000791 desc.fColorType = ProgramDesc::kNone_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000792 } else
junov@google.comf93e7172011-03-31 21:26:24 +0000793#endif
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000794#if GR_GL_NO_CONSTANT_ATTRIBUTES
795 if (!requiresAttributeColors) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000796 desc.fColorType = ProgramDesc::kUniform_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000797 } else
798#endif
799 {
bsalomon@google.com6a77cc52011-04-28 17:33:34 +0000800 if (requiresAttributeColors) {} // suppress unused var warning
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000801 desc.fColorType = ProgramDesc::kAttribute_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000802 }
junov@google.comf93e7172011-03-31 21:26:24 +0000803
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000804 desc.fEdgeAANumEdges = fCurrDrawState.fEdgeAANumEdges;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000805 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 && SkToBool(fCurrDrawState.fFlagBits & kEdgeAAConcave_StateBit);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000806
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000807 int lastEnabledStage = -1;
808
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000809 if (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit) {
810 desc.fVertexEdgeType = fCurrDrawState.fVertexEdgeType;
811 } else {
812 // use canonical value when not set to avoid cache misses
813 desc.fVertexEdgeType = GrDrawTarget::kHairLine_EdgeType;
814 }
815
junov@google.comf93e7172011-03-31 21:26:24 +0000816 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000817 StageDesc& stage = desc.fStages[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000818
tomhudson@google.com0d831722011-06-02 15:37:14 +0000819 stage.fOptFlags = 0;
820 stage.setEnabled(this->isStageEnabled(s));
junov@google.comf93e7172011-03-31 21:26:24 +0000821
tomhudson@google.com0d831722011-06-02 15:37:14 +0000822 if (stage.isEnabled()) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000823 lastEnabledStage = s;
junov@google.comf93e7172011-03-31 21:26:24 +0000824 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
825 GrAssert(NULL != texture);
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000826 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000827 // we matrix to invert when orientation is TopDown, so make sure
828 // we aren't in that case before flagging as identity.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000829 if (TextureMatrixIsIdentity(texture, sampler)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000830 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000831 } else if (!getSamplerMatrix(s).hasPerspective()) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000832 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000833 }
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000834 switch (sampler.getSampleMode()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000835 case GrSamplerState::kNormal_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000836 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000837 break;
838 case GrSamplerState::kRadial_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000839 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000840 break;
841 case GrSamplerState::kRadial2_SampleMode:
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000842 if (sampler.radial2IsDegenerate()) {
843 stage.fCoordMapping =
844 StageDesc::kRadial2GradientDegenerate_CoordMapping;
845 } else {
846 stage.fCoordMapping =
847 StageDesc::kRadial2Gradient_CoordMapping;
848 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000849 break;
850 case GrSamplerState::kSweep_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000851 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000852 break;
853 default:
854 GrCrash("Unexpected sample mode!");
855 break;
856 }
857
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000858 switch (sampler.getFilter()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000859 // these both can use a regular texture2D()
860 case GrSamplerState::kNearest_Filter:
861 case GrSamplerState::kBilinear_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000862 stage.fFetchMode = StageDesc::kSingle_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000863 break;
864 // performs 4 texture2D()s
865 case GrSamplerState::k4x4Downsample_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000866 stage.fFetchMode = StageDesc::k2x2_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000867 break;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000868 // performs fKernelWidth texture2D()s
869 case GrSamplerState::kConvolution_Filter:
870 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
871 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000872 default:
873 GrCrash("Unexpected filter!");
874 break;
junov@google.comf93e7172011-03-31 21:26:24 +0000875 }
876
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000877 if (sampler.hasTextureDomain()) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000878 GrAssert(GrSamplerState::kClamp_WrapMode ==
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000879 sampler.getWrapX() &&
880 GrSamplerState::kClamp_WrapMode ==
881 sampler.getWrapY());
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000882 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
junov@google.com6acc9b32011-05-16 18:32:07 +0000883 }
884
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000885 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000886 stage.fModulation = StageDesc::kAlpha_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000887 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000888 stage.fModulation = StageDesc::kColor_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000889 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000890 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) {
891 stage.fKernelWidth = sampler.getKernelWidth();
892 } else {
893 stage.fKernelWidth = 0;
894 }
junov@google.comf93e7172011-03-31 21:26:24 +0000895 } else {
896 stage.fOptFlags = 0;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000897 stage.fCoordMapping = (StageDesc::CoordMapping)0;
898 stage.fModulation = (StageDesc::Modulation)0;
junov@google.comf93e7172011-03-31 21:26:24 +0000899 }
900 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000901
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000902 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000903 // use canonical value when coverage/color distinction won't affect
904 // generated code to prevent duplicate programs.
905 desc.fFirstCoverageStage = kNumStages;
906 if (fCurrDrawState.fFirstCoverageStage <= lastEnabledStage) {
907 // color filter is applied between color/coverage computation
908 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
909 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
910 }
911
912 // We could consider cases where the final color is solid (0xff alpha)
913 // and the dst coeff can correctly be set to a non-dualsrc gl value.
914 // (e.g. solid draw, and dst coeff is kZero. It's correct to make
915 // the dst coeff be kISA. Or solid draw with kSA can be tweaked to be
916 // kOne).
917 if (fDualSourceBlendingSupport) {
918 if (kZero_BlendCoeff == fCurrDrawState.fDstBlend) {
919 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000920 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000921 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
922 } else if (kSA_BlendCoeff == fCurrDrawState.fDstBlend) {
923 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
924 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000925 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000926 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
927 } else if (kSC_BlendCoeff == fCurrDrawState.fDstBlend) {
928 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
929 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000930 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000931 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
932 }
933 }
934 }
junov@google.comf93e7172011-03-31 21:26:24 +0000935}