blob: f894f1c33a29a9da0158fe4c66e0e6648a26b668 [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.com98874cd2011-09-20 17:33:24 +000054 Entry fEntries[kMaxEntries];
55 int fCount;
56 unsigned int fCurrLRUStamp;
57 const GrGLInterface* fGL;
58 GrGLProgram::GLSLVersion fGLSLVersion;
59
junov@google.comf93e7172011-03-31 21:26:24 +000060public:
bsalomon@google.com98874cd2011-09-20 17:33:24 +000061 ProgramCache(const GrGLInterface* gl,
62 GrGLProgram::GLSLVersion glslVersion)
junov@google.comf93e7172011-03-31 21:26:24 +000063 : fCount(0)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000064 , fCurrLRUStamp(0)
bsalomon@google.com98874cd2011-09-20 17:33:24 +000065 , fGL(gl)
66 , fGLSLVersion(glslVersion) {
junov@google.comf93e7172011-03-31 21:26:24 +000067 }
68
69 ~ProgramCache() {
70 for (int i = 0; i < fCount; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000071 GrGpuGLShaders::DeleteProgram(fGL, &fEntries[i].fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000072 }
73 }
74
75 void abandon() {
76 fCount = 0;
77 }
78
79 void invalidateViewMatrices() {
80 for (int i = 0; i < fCount; ++i) {
81 // set to illegal matrix
82 fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix();
83 }
84 }
85
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000086 GrGLProgram::CachedData* getProgramData(const GrGLProgram& desc) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000087 Entry newEntry;
junov@google.comf7c00f62011-08-18 18:15:16 +000088 newEntry.fKey.setKeyData(desc.keyData());
89
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000090 Entry* entry = fHashCache.find(newEntry.fKey);
junov@google.comf93e7172011-03-31 21:26:24 +000091 if (NULL == entry) {
bsalomon@google.com98874cd2011-09-20 17:33:24 +000092 if (!desc.genProgram(fGL, fGLSLVersion, &newEntry.fProgramData)) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000093 return NULL;
94 }
junov@google.comf93e7172011-03-31 21:26:24 +000095 if (fCount < kMaxEntries) {
96 entry = fEntries + fCount;
97 ++fCount;
98 } else {
99 GrAssert(kMaxEntries == fCount);
100 entry = fEntries;
101 for (int i = 1; i < kMaxEntries; ++i) {
102 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
103 entry = fEntries + i;
104 }
105 }
106 fHashCache.remove(entry->fKey, entry);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000107 GrGpuGLShaders::DeleteProgram(fGL, &entry->fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +0000108 }
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000109 entry->copyAndTakeOwnership(newEntry);
junov@google.comf93e7172011-03-31 21:26:24 +0000110 fHashCache.insert(entry->fKey, entry);
111 }
112
113 entry->fLRUStamp = fCurrLRUStamp;
114 if (GR_UINT32_MAX == fCurrLRUStamp) {
115 // wrap around! just trash our LRU, one time hit.
116 for (int i = 0; i < fCount; ++i) {
117 fEntries[i].fLRUStamp = 0;
118 }
119 }
120 ++fCurrLRUStamp;
121 return &entry->fProgramData;
122 }
123};
124
junov@google.com53a55842011-06-08 22:55:10 +0000125void GrGpuGLShaders::abandonResources(){
126 INHERITED::abandonResources();
127 fProgramCache->abandon();
128}
129
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000130void GrGpuGLShaders::DeleteProgram(const GrGLInterface* gl,
131 CachedData* programData) {
132 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
133 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
134 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000135 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
136}
137
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000138////////////////////////////////////////////////////////////////////////////////
139
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000140#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
141
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000142namespace {
bsalomon@google.com98874cd2011-09-20 17:33:24 +0000143
144GrGLProgram::GLSLVersion get_glsl_version(GrGLBinding binding, float glVersion) {
145 switch (binding) {
146 case kDesktop_GrGLBinding:
147 return (glVersion >= 3.2) ? GrGLProgram::k150_GLSLVersion :
148 GrGLProgram::k120_GLSLVersion;
149 case kES2_GrGLBinding:
150 return GrGLProgram::k120_GLSLVersion;
151 default:
152 GrCrash("Attempting to get GLSL version in unknown or fixed-"
153 "function GL binding.");
154 return GrGLProgram::k120_GLSLVersion; // suppress warning
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000155 }
bsalomon@google.com98874cd2011-09-20 17:33:24 +0000156}
157
158template <typename T>
159T random_val(GrRandom* r, T count) {
160 return (T)(int)(r->nextF() * count);
161}
162
163}
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000164
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000165bool GrGpuGLShaders::programUnitTest() {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000166
bsalomon@google.com98874cd2011-09-20 17:33:24 +0000167 GrGLProgram::GLSLVersion glslVersion =
168 get_glsl_version(this->glBinding(), this->glVersion());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000169 static const int STAGE_OPTS[] = {
170 0,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000171 StageDesc::kNoPerspective_OptFlagBit,
172 StageDesc::kIdentity_CoordMapping
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000173 };
174 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000175 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000176
177 static const int NUM_TESTS = 512;
178
179 // GrRandoms nextU() values have patterns in the low bits
180 // So using nextU() % array_count might never take some values.
181 GrRandom random;
182 for (int t = 0; t < NUM_TESTS; ++t) {
183
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000184#if 0
185 GrPrintf("\nTest Program %d\n-------------\n", t);
186 static const int stop = -1;
187 if (t == stop) {
188 int breakpointhere = 9;
189 }
190#endif
191
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000192 pdesc.fVertexLayout = 0;
193 pdesc.fEmitsPointSize = random.nextF() > .5f;
194 float colorType = random.nextF();
195 if (colorType < 1.f / 3.f) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000196 pdesc.fColorType = ProgramDesc::kAttribute_ColorType;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000197 } else if (colorType < 2.f / 3.f) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000198 pdesc.fColorType = ProgramDesc::kUniform_ColorType;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000199 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000200 pdesc.fColorType = ProgramDesc::kNone_ColorType;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000201 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000202
203 int idx = (int)(random.nextF() * (SkXfermode::kCoeffModesCnt));
204 pdesc.fColorFilterXfermode = (SkXfermode::Mode)idx;
205
206 idx = (int)(random.nextF() * (kNumStages+1));
207 pdesc.fFirstCoverageStage = idx;
208
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000209 bool edgeAA = random.nextF() > .5f;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000210 if (edgeAA) {
211 bool vertexEdgeAA = random.nextF() > .5f;
212 if (vertexEdgeAA) {
213 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
214 if (this->supportsShaderDerivatives()) {
215 pdesc.fVertexEdgeType = random.nextF() > 0.5f ?
216 kHairQuad_EdgeType :
217 kHairLine_EdgeType;
218 } else {
219 pdesc.fVertexEdgeType = kHairLine_EdgeType;
220 }
221 pdesc.fEdgeAANumEdges = 0;
222 } else {
223 pdesc.fEdgeAANumEdges = random.nextF() * this->getMaxEdges() + 1;
224 pdesc.fEdgeAAConcave = random.nextF() > .5f;
225 }
226 } else {
227 pdesc.fEdgeAANumEdges = 0;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000228 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000229
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000230 if (fDualSourceBlendingSupport) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000231 pdesc.fDualSrcOutput =
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000232 (ProgramDesc::DualSrcOutput)
233 (int)(random.nextF() * ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000234 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000235 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000236 }
237
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000238 for (int s = 0; s < kNumStages; ++s) {
239 // enable the stage?
240 if (random.nextF() > .5f) {
241 // use separate tex coords?
242 if (random.nextF() > .5f) {
243 int t = (int)(random.nextF() * kMaxTexCoords);
244 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
245 } else {
246 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
247 }
248 }
249 // use text-formatted verts?
250 if (random.nextF() > .5f) {
251 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
252 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000253 idx = (int)(random.nextF() * GR_ARRAY_COUNT(STAGE_OPTS));
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000254 StageDesc& stage = pdesc.fStages[s];
255 stage.fOptFlags = STAGE_OPTS[idx];
256 stage.fModulation = random_val(&random, StageDesc::kModulationCnt);
257 stage.fCoordMapping = random_val(&random, StageDesc::kCoordMappingCnt);
258 stage.fFetchMode = random_val(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000259 // convolution shaders don't work with persp tex matrix
260 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) {
261 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
262 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000263 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000264 stage.fKernelWidth = 4 * random.nextF() + 2;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000265 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000266 CachedData cachedData;
bsalomon@google.com98874cd2011-09-20 17:33:24 +0000267 if (!program.genProgram(this->glInterface(),
268 glslVersion,
269 &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000270 return false;
271 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000272 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000273 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000274 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000275}
276
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000277namespace {
278GrGLBinding get_binding_in_use(const GrGLInterface* gl) {
279 if (gl->supportsDesktop()) {
280 return kDesktop_GrGLBinding;
281 } else {
282 GrAssert(gl->supportsES2());
283 return kES2_GrGLBinding;
284 }
285}
286}
287
288GrGpuGLShaders::GrGpuGLShaders(const GrGLInterface* gl)
289 : GrGpuGL(gl, get_binding_in_use(gl)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000290
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000291 fShaderSupport = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000292 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000293 fDualSourceBlendingSupport =
bsalomon@google.com98874cd2011-09-20 17:33:24 +0000294 this->glVersion() >= 3.25f || // TODO: when resolving Issue 387 change
295 // this back to 3.3
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000296 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000297 fShaderDerivativeSupport = true;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000298 } else {
299 fDualSourceBlendingSupport = false;
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000300 fShaderDerivativeSupport =
301 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000302 }
junov@google.comf93e7172011-03-31 21:26:24 +0000303
304 fProgramData = NULL;
bsalomon@google.com98874cd2011-09-20 17:33:24 +0000305 GrGLProgram::GLSLVersion glslVersion =
306 get_glsl_version(this->glBinding(), this->glVersion());
307 fProgramCache = new ProgramCache(gl, glslVersion);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000308
309#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000310 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000311#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000312}
313
314GrGpuGLShaders::~GrGpuGLShaders() {
315 delete fProgramCache;
316}
317
318const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000319 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000320
321 if (GrGLProgram::kSetAsAttribute ==
322 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
323 return fHWDrawState.fSamplerStates[stage].getMatrix();
324 } else {
325 return fProgramData->fTextureMatrices[stage];
326 }
junov@google.comf93e7172011-03-31 21:26:24 +0000327}
328
329void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000330 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000331 if (GrGLProgram::kSetAsAttribute ==
332 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
333 fHWDrawState.fSamplerStates[stage].setMatrix(matrix);
334 } else {
335 fProgramData->fTextureMatrices[stage] = matrix;
336 }
junov@google.comf93e7172011-03-31 21:26:24 +0000337}
338
339void GrGpuGLShaders::resetContext() {
340 INHERITED::resetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000341
junov@google.comf93e7172011-03-31 21:26:24 +0000342 fHWGeometryState.fVertexLayout = 0;
343 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000344 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000345 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000346 for (int t = 0; t < kMaxTexCoords; ++t) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000347 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000348 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000349 GL_CALL(EnableVertexAttribArray(GrGLProgram::PositionAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000350
351 fHWProgramID = 0;
352}
353
354void GrGpuGLShaders::flushViewMatrix() {
355 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000356 GrMatrix m;
357 m.setAll(
junov@google.comf93e7172011-03-31 21:26:24 +0000358 GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1,
359 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1,
360 0, 0, GrMatrix::I()[8]);
361 m.setConcat(m, fCurrDrawState.fViewMatrix);
362
363 // ES doesn't allow you to pass true to the transpose param,
364 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000365 GrGLfloat mt[] = {
366 GrScalarToFloat(m[GrMatrix::kMScaleX]),
367 GrScalarToFloat(m[GrMatrix::kMSkewY]),
368 GrScalarToFloat(m[GrMatrix::kMPersp0]),
369 GrScalarToFloat(m[GrMatrix::kMSkewX]),
370 GrScalarToFloat(m[GrMatrix::kMScaleY]),
371 GrScalarToFloat(m[GrMatrix::kMPersp1]),
372 GrScalarToFloat(m[GrMatrix::kMTransX]),
373 GrScalarToFloat(m[GrMatrix::kMTransY]),
374 GrScalarToFloat(m[GrMatrix::kMPersp2])
junov@google.comf93e7172011-03-31 21:26:24 +0000375 };
bsalomon@google.com91961302011-05-09 18:39:58 +0000376
377 if (GrGLProgram::kSetAsAttribute ==
378 fProgramData->fUniLocations.fViewMatrixUni) {
379 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000380 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
381 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
382 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000383 } else {
384 GrAssert(GrGLProgram::kUnusedUniform !=
385 fProgramData->fUniLocations.fViewMatrixUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000386 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
387 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000388 }
junov@google.comf93e7172011-03-31 21:26:24 +0000389}
390
junov@google.com6acc9b32011-05-16 18:32:07 +0000391void GrGpuGLShaders::flushTextureDomain(int s) {
392 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
393 if (GrGLProgram::kUnusedUniform != uni) {
twiz@google.com76b82742011-06-02 20:30:02 +0000394 const GrRect &texDom =
junov@google.com6acc9b32011-05-16 18:32:07 +0000395 fCurrDrawState.fSamplerStates[s].getTextureDomain();
396
twiz@google.com76b82742011-06-02 20:30:02 +0000397 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
398 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000399
junov@google.com2f839402011-05-24 15:13:01 +0000400 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000401
junov@google.com2f839402011-05-24 15:13:01 +0000402 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000403 GrScalarToFloat(texDom.left()),
404 GrScalarToFloat(texDom.top()),
405 GrScalarToFloat(texDom.right()),
406 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000407 };
408
409 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
410 GrGLTexture::Orientation orientation = texture->orientation();
411
412 // vertical flip if necessary
413 if (GrGLTexture::kBottomUp_Orientation == orientation) {
414 values[1] = 1.0f - values[1];
415 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000416 // The top and bottom were just flipped, so correct the ordering
417 // of elements so that values = (l, t, r, b).
418 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000419 }
420
421 values[0] *= SkScalarToFloat(texture->contentScaleX());
422 values[2] *= SkScalarToFloat(texture->contentScaleX());
423 values[1] *= SkScalarToFloat(texture->contentScaleY());
424 values[3] *= SkScalarToFloat(texture->contentScaleY());
425
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000426 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000427 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000428 }
429}
430
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000431void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000432 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000433 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
434 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000435 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000436 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
437 getHWSamplerMatrix(s) != getSamplerMatrix(s))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000438
bsalomon@google.com91961302011-05-09 18:39:58 +0000439 GrAssert(NULL != fCurrDrawState.fTextures[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000440
bsalomon@google.com91961302011-05-09 18:39:58 +0000441 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000442
bsalomon@google.com91961302011-05-09 18:39:58 +0000443 GrMatrix m = getSamplerMatrix(s);
444 GrSamplerState::SampleMode mode =
445 fCurrDrawState.fSamplerStates[s].getSampleMode();
446 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000447
bsalomon@google.com91961302011-05-09 18:39:58 +0000448 // ES doesn't allow you to pass true to the transpose param,
449 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000450 GrGLfloat mt[] = {
451 GrScalarToFloat(m[GrMatrix::kMScaleX]),
452 GrScalarToFloat(m[GrMatrix::kMSkewY]),
453 GrScalarToFloat(m[GrMatrix::kMPersp0]),
454 GrScalarToFloat(m[GrMatrix::kMSkewX]),
455 GrScalarToFloat(m[GrMatrix::kMScaleY]),
456 GrScalarToFloat(m[GrMatrix::kMPersp1]),
457 GrScalarToFloat(m[GrMatrix::kMTransX]),
458 GrScalarToFloat(m[GrMatrix::kMTransY]),
459 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000460 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000461
bsalomon@google.com91961302011-05-09 18:39:58 +0000462 if (GrGLProgram::kSetAsAttribute ==
463 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
464 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000465 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
466 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
467 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000468 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000469 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000470 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000471 recordHWSamplerMatrix(s, getSamplerMatrix(s));
472 }
473 }
junov@google.comf93e7172011-03-31 21:26:24 +0000474}
475
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000476void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000477
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000478 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
479 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com91961302011-05-09 18:39:58 +0000480 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000481 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
482 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
483 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000484
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000485 GrScalar centerX1 = sampler.getRadial2CenterX1();
486 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000487
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000488 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000489
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000490 // when were in the degenerate (linear) case the second
491 // value will be INF but the program doesn't read it. (We
492 // use the same 6 uniforms even though we don't need them
493 // all in the linear case just to keep the code complexity
494 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000495 float values[6] = {
496 GrScalarToFloat(a),
497 1 / (2.f * values[0]),
498 GrScalarToFloat(centerX1),
499 GrScalarToFloat(radius0),
500 GrScalarToFloat(GrMul(radius0, radius0)),
501 sampler.isRadial2PosRoot() ? 1.f : -1.f
502 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000503 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000504 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
505 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
506 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
507 }
508}
509
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000510void GrGpuGLShaders::flushConvolution(int s) {
511 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
512 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
513 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000514 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
515 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000516 }
517 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
518 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000519 GL_CALL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000520 }
521}
522
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000523void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000524 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000525 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000526 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000527 if (texture->allocatedWidth() != fProgramData->fTextureWidth[s] ||
528 texture->allocatedHeight() != fProgramData->fTextureWidth[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000529
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000530 float texelSize[] = {1.f / texture->allocatedWidth(),
531 1.f / texture->allocatedHeight()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000532 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000533 }
534 }
junov@google.comf93e7172011-03-31 21:26:24 +0000535}
536
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000537void GrGpuGLShaders::flushEdgeAAData() {
538 const int& uni = fProgramData->fUniLocations.fEdgesUni;
539 if (GrGLProgram::kUnusedUniform != uni) {
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000540 int count = fCurrDrawState.fEdgeAANumEdges;
541 Edge edges[kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000542 // Flip the edges in Y
543 float height = fCurrDrawState.fRenderTarget->height();
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000544 for (int i = 0; i < count; ++i) {
545 edges[i] = fCurrDrawState.fEdgeAAEdges[i];
546 float b = edges[i].fY;
547 edges[i].fY = -b;
548 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000549 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000550 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000551 }
552}
553
Scroggo01b87ec2011-05-11 18:05:38 +0000554static const float ONE_OVER_255 = 1.f / 255.f;
555
556#define GR_COLOR_TO_VEC4(color) {\
557 GrColorUnpackR(color) * ONE_OVER_255,\
558 GrColorUnpackG(color) * ONE_OVER_255,\
559 GrColorUnpackB(color) * ONE_OVER_255,\
560 GrColorUnpackA(color) * ONE_OVER_255 \
561}
562
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000563void GrGpuGLShaders::flushColor() {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000564 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000565 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000566 // color will be specified per-vertex as an attribute
567 // invalidate the const vertex attrib color
568 fHWDrawState.fColor = GrColor_ILLEGAL;
569 } else {
570 switch (desc.fColorType) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000571 case ProgramDesc::kAttribute_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000572 if (fHWDrawState.fColor != fCurrDrawState.fColor) {
573 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000574 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000575 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
576 c));
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000577 fHWDrawState.fColor = fCurrDrawState.fColor;
578 }
579 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000580 case ProgramDesc::kUniform_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000581 if (fProgramData->fColor != fCurrDrawState.fColor) {
582 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000583 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com91961302011-05-09 18:39:58 +0000584 GrAssert(GrGLProgram::kUnusedUniform !=
585 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000586 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
587 1, c));
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000588 fProgramData->fColor = fCurrDrawState.fColor;
589 }
590 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000591 case ProgramDesc::kNone_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000592 GrAssert(0xffffffff == fCurrDrawState.fColor);
593 break;
594 default:
595 GrCrash("Unknown color type.");
596 }
597 }
Scroggo97c88c22011-05-11 14:05:25 +0000598 if (fProgramData->fUniLocations.fColorFilterUni
599 != GrGLProgram::kUnusedUniform
600 && fProgramData->fColorFilterColor
601 != fCurrDrawState.fColorFilterColor) {
Scroggo01b87ec2011-05-11 18:05:38 +0000602 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColorFilterColor);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000603 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
Scroggo97c88c22011-05-11 14:05:25 +0000604 fProgramData->fColorFilterColor = fCurrDrawState.fColorFilterColor;
605 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000606}
607
608
junov@google.comf93e7172011-03-31 21:26:24 +0000609bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
610 if (!flushGLStateCommon(type)) {
611 return false;
612 }
613
614 if (fDirtyFlags.fRenderTargetChanged) {
615 // our coords are in pixel space and the GL matrices map to NDC
616 // so if the viewport changed, our matrix is now wrong.
junov@google.comf93e7172011-03-31 21:26:24 +0000617 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000618 // we assume all shader matrices may be wrong after viewport changes
619 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000620 }
621
junov@google.comf93e7172011-03-31 21:26:24 +0000622 buildProgram(type);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000623 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000624 if (NULL == fProgramData) {
625 GrAssert(!"Failed to create program!");
626 return false;
627 }
junov@google.comf93e7172011-03-31 21:26:24 +0000628
629 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000630 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000631 fHWProgramID = fProgramData->fProgramID;
632 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000633 GrBlendCoeff srcCoeff = fCurrDrawState.fSrcBlend;
634 GrBlendCoeff dstCoeff = fCurrDrawState.fDstBlend;
635
636 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
637 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000638
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000639 this->flushColor();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000640
bsalomon@google.com91961302011-05-09 18:39:58 +0000641 GrMatrix* currViewMatrix;
642 if (GrGLProgram::kSetAsAttribute ==
643 fProgramData->fUniLocations.fViewMatrixUni) {
644 currViewMatrix = &fHWDrawState.fViewMatrix;
645 } else {
646 currViewMatrix = &fProgramData->fViewMatrix;
647 }
junov@google.comf93e7172011-03-31 21:26:24 +0000648
bsalomon@google.com91961302011-05-09 18:39:58 +0000649 if (*currViewMatrix != fCurrDrawState.fViewMatrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000650 flushViewMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000651 *currViewMatrix = fCurrDrawState.fViewMatrix;
junov@google.comf93e7172011-03-31 21:26:24 +0000652 }
653
654 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000655 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000656
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000657 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000658
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000659 this->flushConvolution(s);
660
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000661 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000662
663 this->flushTextureDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000664 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000665 this->flushEdgeAAData();
junov@google.comf93e7172011-03-31 21:26:24 +0000666 resetDirtyFlags();
667 return true;
668}
669
670void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000671}
672
673void GrGpuGLShaders::setupGeometry(int* startVertex,
674 int* startIndex,
675 int vertexCount,
676 int indexCount) {
677
678 int newColorOffset;
679 int newTexCoordOffsets[kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000680 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000681
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000682 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
683 this->getGeomSrc().fVertexLayout,
684 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000685 &newColorOffset,
686 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000687 int oldColorOffset;
688 int oldTexCoordOffsets[kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000689 int oldEdgeOffset;
690
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000691 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
692 fHWGeometryState.fVertexLayout,
693 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000694 &oldColorOffset,
695 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000696 bool indexed = NULL != startIndex;
697
698 int extraVertexOffset;
699 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000700 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000701
702 GrGLenum scalarType;
703 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000704 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000705 scalarType = GrGLTextType;
706 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
707 } else {
708 scalarType = GrGLType;
709 texCoordNorm = false;
710 }
711
712 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
713 *startVertex = 0;
714 if (indexed) {
715 *startIndex += extraIndexOffset;
716 }
717
718 // all the Pointers must be set if any of these are true
719 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
720 vertexOffset != fHWGeometryState.fVertexOffset ||
721 newStride != oldStride;
722
723 // position and tex coord offsets change if above conditions are true
724 // or the type/normalization changed based on text vs nontext type coords.
725 bool posAndTexChange = allOffsetsChange ||
726 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
727 (kTextFormat_VertexLayoutBit &
728 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000729 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000730
731 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000732 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000733 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000734 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000735 fHWGeometryState.fVertexOffset = vertexOffset;
736 }
737
738 for (int t = 0; t < kMaxTexCoords; ++t) {
739 if (newTexCoordOffsets[t] > 0) {
740 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000741 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000742 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000743 GL_CALL(EnableVertexAttribArray(idx));
744 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000745 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000746 } else if (posAndTexChange ||
747 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000748 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000749 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000750 }
751 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000752 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000753 }
754 }
755
756 if (newColorOffset > 0) {
757 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000758 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000759 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000760 GL_CALL(EnableVertexAttribArray(idx));
761 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000762 true, newStride, colorOffset));
763 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000764 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000765 true, newStride, colorOffset));
766 }
767 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000768 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000769 }
770
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000771 if (newEdgeOffset > 0) {
772 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
773 int idx = GrGLProgram::EdgeAttributeIdx();
774 if (oldEdgeOffset <= 0) {
775 GL_CALL(EnableVertexAttribArray(idx));
776 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
777 false, newStride, edgeOffset));
778 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
779 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
780 false, newStride, edgeOffset));
781 }
782 } else if (oldEdgeOffset > 0) {
783 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
784 }
785
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000786 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000787 fHWGeometryState.fArrayPtrsDirty = false;
788}
789
790void GrGpuGLShaders::buildProgram(GrPrimitiveType type) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000791 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
junov@google.comf93e7172011-03-31 21:26:24 +0000792
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000793 // The descriptor is used as a cache key. Thus when a field of the
794 // descriptor will not affect program generation (because of the vertex
795 // layout in use or other descriptor field settings) it should be set
796 // to a canonical value to avoid duplicate programs with different keys.
797
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000798 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000799 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000800
801 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
802
803 bool requiresAttributeColors = desc.fVertexLayout & kColor_VertexLayoutBit;
804 // fColorType records how colors are specified for the program. Strip
805 // the bit from the layout to avoid false negatives when searching for an
806 // existing program in the cache.
807 desc.fVertexLayout &= ~(kColor_VertexLayoutBit);
808
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000809 desc.fColorFilterXfermode = fCurrDrawState.fColorFilterXfermode;
810
junov@google.comf93e7172011-03-31 21:26:24 +0000811#if GR_AGGRESSIVE_SHADER_OPTS
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000812 if (!requiresAttributeColors && (0xffffffff == fCurrDrawState.fColor)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000813 desc.fColorType = ProgramDesc::kNone_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000814 } else
junov@google.comf93e7172011-03-31 21:26:24 +0000815#endif
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000816#if GR_GL_NO_CONSTANT_ATTRIBUTES
817 if (!requiresAttributeColors) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000818 desc.fColorType = ProgramDesc::kUniform_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000819 } else
820#endif
821 {
bsalomon@google.com6a77cc52011-04-28 17:33:34 +0000822 if (requiresAttributeColors) {} // suppress unused var warning
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000823 desc.fColorType = ProgramDesc::kAttribute_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000824 }
junov@google.comf93e7172011-03-31 21:26:24 +0000825
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000826 desc.fEdgeAANumEdges = fCurrDrawState.fEdgeAANumEdges;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000827 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 && SkToBool(fCurrDrawState.fFlagBits & kEdgeAAConcave_StateBit);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000828
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000829 int lastEnabledStage = -1;
830
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000831 if (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit) {
832 desc.fVertexEdgeType = fCurrDrawState.fVertexEdgeType;
833 } else {
834 // use canonical value when not set to avoid cache misses
835 desc.fVertexEdgeType = GrDrawTarget::kHairLine_EdgeType;
836 }
837
junov@google.comf93e7172011-03-31 21:26:24 +0000838 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000839 StageDesc& stage = desc.fStages[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000840
tomhudson@google.com0d831722011-06-02 15:37:14 +0000841 stage.fOptFlags = 0;
842 stage.setEnabled(this->isStageEnabled(s));
junov@google.comf93e7172011-03-31 21:26:24 +0000843
tomhudson@google.com0d831722011-06-02 15:37:14 +0000844 if (stage.isEnabled()) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000845 lastEnabledStage = s;
junov@google.comf93e7172011-03-31 21:26:24 +0000846 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
847 GrAssert(NULL != texture);
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000848 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000849 // we matrix to invert when orientation is TopDown, so make sure
850 // we aren't in that case before flagging as identity.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000851 if (TextureMatrixIsIdentity(texture, sampler)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000852 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000853 } else if (!getSamplerMatrix(s).hasPerspective()) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000854 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000855 }
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000856 switch (sampler.getSampleMode()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000857 case GrSamplerState::kNormal_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000858 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000859 break;
860 case GrSamplerState::kRadial_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000861 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000862 break;
863 case GrSamplerState::kRadial2_SampleMode:
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000864 if (sampler.radial2IsDegenerate()) {
865 stage.fCoordMapping =
866 StageDesc::kRadial2GradientDegenerate_CoordMapping;
867 } else {
868 stage.fCoordMapping =
869 StageDesc::kRadial2Gradient_CoordMapping;
870 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000871 break;
872 case GrSamplerState::kSweep_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000873 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000874 break;
875 default:
876 GrCrash("Unexpected sample mode!");
877 break;
878 }
879
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000880 switch (sampler.getFilter()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000881 // these both can use a regular texture2D()
882 case GrSamplerState::kNearest_Filter:
883 case GrSamplerState::kBilinear_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000884 stage.fFetchMode = StageDesc::kSingle_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000885 break;
886 // performs 4 texture2D()s
887 case GrSamplerState::k4x4Downsample_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000888 stage.fFetchMode = StageDesc::k2x2_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000889 break;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000890 // performs fKernelWidth texture2D()s
891 case GrSamplerState::kConvolution_Filter:
892 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
893 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000894 default:
895 GrCrash("Unexpected filter!");
896 break;
junov@google.comf93e7172011-03-31 21:26:24 +0000897 }
898
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000899 if (sampler.hasTextureDomain()) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000900 GrAssert(GrSamplerState::kClamp_WrapMode ==
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000901 sampler.getWrapX() &&
902 GrSamplerState::kClamp_WrapMode ==
903 sampler.getWrapY());
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000904 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
junov@google.com6acc9b32011-05-16 18:32:07 +0000905 }
906
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000907 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000908 stage.fModulation = StageDesc::kAlpha_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000909 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000910 stage.fModulation = StageDesc::kColor_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000911 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000912 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) {
913 stage.fKernelWidth = sampler.getKernelWidth();
914 } else {
915 stage.fKernelWidth = 0;
916 }
junov@google.comf93e7172011-03-31 21:26:24 +0000917 } else {
918 stage.fOptFlags = 0;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000919 stage.fCoordMapping = (StageDesc::CoordMapping)0;
920 stage.fModulation = (StageDesc::Modulation)0;
junov@google.comf93e7172011-03-31 21:26:24 +0000921 }
922 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000923
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000924 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000925 // use canonical value when coverage/color distinction won't affect
926 // generated code to prevent duplicate programs.
927 desc.fFirstCoverageStage = kNumStages;
928 if (fCurrDrawState.fFirstCoverageStage <= lastEnabledStage) {
929 // color filter is applied between color/coverage computation
930 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
931 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
932 }
933
934 // We could consider cases where the final color is solid (0xff alpha)
935 // and the dst coeff can correctly be set to a non-dualsrc gl value.
936 // (e.g. solid draw, and dst coeff is kZero. It's correct to make
937 // the dst coeff be kISA. Or solid draw with kSA can be tweaked to be
938 // kOne).
939 if (fDualSourceBlendingSupport) {
940 if (kZero_BlendCoeff == fCurrDrawState.fDstBlend) {
941 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000942 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000943 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
944 } else if (kSA_BlendCoeff == fCurrDrawState.fDstBlend) {
945 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
946 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000947 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000948 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
949 } else if (kSC_BlendCoeff == fCurrDrawState.fDstBlend) {
950 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
951 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000952 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000953 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
954 }
955 }
956 }
junov@google.comf93e7172011-03-31 21:26:24 +0000957}