blob: 7d15c4f134194d8ddfdca45b9978ab9f06a52b5a [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.com4fa66942011-09-20 19:06:12 +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.com4fa66942011-09-20 19:06:12 +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.com4fa66942011-09-20 19:06:12 +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.com4fa66942011-09-20 19:06:12 +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.com4fa66942011-09-20 19:06:12 +0000143
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000144GrGLProgram::GLSLVersion get_glsl_version(GrGLBinding binding, GrGLVersion glVersion) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000145 switch (binding) {
146 case kDesktop_GrGLBinding:
147 // TODO: proper check of the glsl version string
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000148 return (glVersion >= GR_GL_VER(3,0)) ?
149 GrGLProgram::k130_GLSLVersion :
150 GrGLProgram::k120_GLSLVersion;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000151 case kES2_GrGLBinding:
152 return GrGLProgram::k120_GLSLVersion;
153 default:
154 GrCrash("Attempting to get GLSL version in unknown or fixed-"
155 "function GL binding.");
156 return GrGLProgram::k120_GLSLVersion; // suppress warning
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000157 }
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000158}
159
160template <typename T>
161T random_val(GrRandom* r, T count) {
162 return (T)(int)(r->nextF() * count);
163}
164
165}
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000166
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000167bool GrGpuGLShaders::programUnitTest() {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000168
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000169 GrGLProgram::GLSLVersion glslVersion =
170 get_glsl_version(this->glBinding(), this->glVersion());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000171 static const int STAGE_OPTS[] = {
172 0,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000173 StageDesc::kNoPerspective_OptFlagBit,
174 StageDesc::kIdentity_CoordMapping
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000175 };
176 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000177 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000178
179 static const int NUM_TESTS = 512;
180
181 // GrRandoms nextU() values have patterns in the low bits
182 // So using nextU() % array_count might never take some values.
183 GrRandom random;
184 for (int t = 0; t < NUM_TESTS; ++t) {
185
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000186#if 0
187 GrPrintf("\nTest Program %d\n-------------\n", t);
188 static const int stop = -1;
189 if (t == stop) {
190 int breakpointhere = 9;
191 }
192#endif
193
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000194 pdesc.fVertexLayout = 0;
195 pdesc.fEmitsPointSize = random.nextF() > .5f;
196 float colorType = random.nextF();
197 if (colorType < 1.f / 3.f) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000198 pdesc.fColorType = ProgramDesc::kAttribute_ColorType;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000199 } else if (colorType < 2.f / 3.f) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000200 pdesc.fColorType = ProgramDesc::kUniform_ColorType;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000201 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000202 pdesc.fColorType = ProgramDesc::kNone_ColorType;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000203 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000204
205 int idx = (int)(random.nextF() * (SkXfermode::kCoeffModesCnt));
206 pdesc.fColorFilterXfermode = (SkXfermode::Mode)idx;
207
208 idx = (int)(random.nextF() * (kNumStages+1));
209 pdesc.fFirstCoverageStage = idx;
210
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000211 bool edgeAA = random.nextF() > .5f;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000212 if (edgeAA) {
213 bool vertexEdgeAA = random.nextF() > .5f;
214 if (vertexEdgeAA) {
215 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
216 if (this->supportsShaderDerivatives()) {
217 pdesc.fVertexEdgeType = random.nextF() > 0.5f ?
218 kHairQuad_EdgeType :
219 kHairLine_EdgeType;
220 } else {
221 pdesc.fVertexEdgeType = kHairLine_EdgeType;
222 }
223 pdesc.fEdgeAANumEdges = 0;
224 } else {
225 pdesc.fEdgeAANumEdges = random.nextF() * this->getMaxEdges() + 1;
226 pdesc.fEdgeAAConcave = random.nextF() > .5f;
227 }
228 } else {
229 pdesc.fEdgeAANumEdges = 0;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000230 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000231
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000232 if (fDualSourceBlendingSupport) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000233 pdesc.fDualSrcOutput =
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000234 (ProgramDesc::DualSrcOutput)
235 (int)(random.nextF() * ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000236 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000237 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000238 }
239
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000240 for (int s = 0; s < kNumStages; ++s) {
241 // enable the stage?
242 if (random.nextF() > .5f) {
243 // use separate tex coords?
244 if (random.nextF() > .5f) {
245 int t = (int)(random.nextF() * kMaxTexCoords);
246 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
247 } else {
248 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
249 }
250 }
251 // use text-formatted verts?
252 if (random.nextF() > .5f) {
253 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
254 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000255 idx = (int)(random.nextF() * GR_ARRAY_COUNT(STAGE_OPTS));
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000256 StageDesc& stage = pdesc.fStages[s];
257 stage.fOptFlags = STAGE_OPTS[idx];
258 stage.fModulation = random_val(&random, StageDesc::kModulationCnt);
259 stage.fCoordMapping = random_val(&random, StageDesc::kCoordMappingCnt);
260 stage.fFetchMode = random_val(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000261 // convolution shaders don't work with persp tex matrix
262 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) {
263 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
264 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000265 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000266 stage.fKernelWidth = 4 * random.nextF() + 2;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000267 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000268 CachedData cachedData;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000269 if (!program.genProgram(this->glInterface(),
270 glslVersion,
271 &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000272 return false;
273 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000274 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000275 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000276 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000277}
278
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000279namespace {
280GrGLBinding get_binding_in_use(const GrGLInterface* gl) {
281 if (gl->supportsDesktop()) {
282 return kDesktop_GrGLBinding;
283 } else {
284 GrAssert(gl->supportsES2());
285 return kES2_GrGLBinding;
286 }
287}
288}
289
290GrGpuGLShaders::GrGpuGLShaders(const GrGLInterface* gl)
291 : GrGpuGL(gl, get_binding_in_use(gl)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000292
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000293 fShaderSupport = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000294 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000295 fDualSourceBlendingSupport =
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000296 this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000297 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000298 fShaderDerivativeSupport = true;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000299 } else {
300 fDualSourceBlendingSupport = false;
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000301 fShaderDerivativeSupport =
302 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000303 }
junov@google.comf93e7172011-03-31 21:26:24 +0000304
305 fProgramData = NULL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000306 GrGLProgram::GLSLVersion glslVersion =
307 get_glsl_version(this->glBinding(), this->glVersion());
308 fProgramCache = new ProgramCache(gl, glslVersion);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000309
310#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000311 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000312#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000313}
314
315GrGpuGLShaders::~GrGpuGLShaders() {
316 delete fProgramCache;
317}
318
319const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000320 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000321
322 if (GrGLProgram::kSetAsAttribute ==
323 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
324 return fHWDrawState.fSamplerStates[stage].getMatrix();
325 } else {
326 return fProgramData->fTextureMatrices[stage];
327 }
junov@google.comf93e7172011-03-31 21:26:24 +0000328}
329
330void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000331 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000332 if (GrGLProgram::kSetAsAttribute ==
333 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
334 fHWDrawState.fSamplerStates[stage].setMatrix(matrix);
335 } else {
336 fProgramData->fTextureMatrices[stage] = matrix;
337 }
junov@google.comf93e7172011-03-31 21:26:24 +0000338}
339
340void GrGpuGLShaders::resetContext() {
341 INHERITED::resetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000342
junov@google.comf93e7172011-03-31 21:26:24 +0000343 fHWGeometryState.fVertexLayout = 0;
344 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000345 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000346 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000347 for (int t = 0; t < kMaxTexCoords; ++t) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000348 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000349 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000350 GL_CALL(EnableVertexAttribArray(GrGLProgram::PositionAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000351
352 fHWProgramID = 0;
353}
354
355void GrGpuGLShaders::flushViewMatrix() {
356 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000357 GrMatrix m;
358 m.setAll(
junov@google.comf93e7172011-03-31 21:26:24 +0000359 GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1,
360 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1,
361 0, 0, GrMatrix::I()[8]);
362 m.setConcat(m, fCurrDrawState.fViewMatrix);
363
364 // ES doesn't allow you to pass true to the transpose param,
365 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000366 GrGLfloat mt[] = {
367 GrScalarToFloat(m[GrMatrix::kMScaleX]),
368 GrScalarToFloat(m[GrMatrix::kMSkewY]),
369 GrScalarToFloat(m[GrMatrix::kMPersp0]),
370 GrScalarToFloat(m[GrMatrix::kMSkewX]),
371 GrScalarToFloat(m[GrMatrix::kMScaleY]),
372 GrScalarToFloat(m[GrMatrix::kMPersp1]),
373 GrScalarToFloat(m[GrMatrix::kMTransX]),
374 GrScalarToFloat(m[GrMatrix::kMTransY]),
375 GrScalarToFloat(m[GrMatrix::kMPersp2])
junov@google.comf93e7172011-03-31 21:26:24 +0000376 };
bsalomon@google.com91961302011-05-09 18:39:58 +0000377
378 if (GrGLProgram::kSetAsAttribute ==
379 fProgramData->fUniLocations.fViewMatrixUni) {
380 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000381 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
382 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
383 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000384 } else {
385 GrAssert(GrGLProgram::kUnusedUniform !=
386 fProgramData->fUniLocations.fViewMatrixUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000387 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
388 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000389 }
junov@google.comf93e7172011-03-31 21:26:24 +0000390}
391
junov@google.com6acc9b32011-05-16 18:32:07 +0000392void GrGpuGLShaders::flushTextureDomain(int s) {
393 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
394 if (GrGLProgram::kUnusedUniform != uni) {
twiz@google.com76b82742011-06-02 20:30:02 +0000395 const GrRect &texDom =
junov@google.com6acc9b32011-05-16 18:32:07 +0000396 fCurrDrawState.fSamplerStates[s].getTextureDomain();
397
twiz@google.com76b82742011-06-02 20:30:02 +0000398 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
399 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000400
junov@google.com2f839402011-05-24 15:13:01 +0000401 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000402
junov@google.com2f839402011-05-24 15:13:01 +0000403 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000404 GrScalarToFloat(texDom.left()),
405 GrScalarToFloat(texDom.top()),
406 GrScalarToFloat(texDom.right()),
407 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000408 };
409
410 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
411 GrGLTexture::Orientation orientation = texture->orientation();
412
413 // vertical flip if necessary
414 if (GrGLTexture::kBottomUp_Orientation == orientation) {
415 values[1] = 1.0f - values[1];
416 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000417 // The top and bottom were just flipped, so correct the ordering
418 // of elements so that values = (l, t, r, b).
419 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000420 }
421
422 values[0] *= SkScalarToFloat(texture->contentScaleX());
423 values[2] *= SkScalarToFloat(texture->contentScaleX());
424 values[1] *= SkScalarToFloat(texture->contentScaleY());
425 values[3] *= SkScalarToFloat(texture->contentScaleY());
426
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000427 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000428 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000429 }
430}
431
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000432void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000433 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000434 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
435 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000436 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000437 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
438 getHWSamplerMatrix(s) != getSamplerMatrix(s))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000439
bsalomon@google.com91961302011-05-09 18:39:58 +0000440 GrAssert(NULL != fCurrDrawState.fTextures[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000441
bsalomon@google.com91961302011-05-09 18:39:58 +0000442 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000443
bsalomon@google.com91961302011-05-09 18:39:58 +0000444 GrMatrix m = getSamplerMatrix(s);
445 GrSamplerState::SampleMode mode =
446 fCurrDrawState.fSamplerStates[s].getSampleMode();
447 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000448
bsalomon@google.com91961302011-05-09 18:39:58 +0000449 // ES doesn't allow you to pass true to the transpose param,
450 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000451 GrGLfloat mt[] = {
452 GrScalarToFloat(m[GrMatrix::kMScaleX]),
453 GrScalarToFloat(m[GrMatrix::kMSkewY]),
454 GrScalarToFloat(m[GrMatrix::kMPersp0]),
455 GrScalarToFloat(m[GrMatrix::kMSkewX]),
456 GrScalarToFloat(m[GrMatrix::kMScaleY]),
457 GrScalarToFloat(m[GrMatrix::kMPersp1]),
458 GrScalarToFloat(m[GrMatrix::kMTransX]),
459 GrScalarToFloat(m[GrMatrix::kMTransY]),
460 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000461 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000462
bsalomon@google.com91961302011-05-09 18:39:58 +0000463 if (GrGLProgram::kSetAsAttribute ==
464 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
465 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000466 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
467 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
468 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000469 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000470 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000471 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000472 recordHWSamplerMatrix(s, getSamplerMatrix(s));
473 }
474 }
junov@google.comf93e7172011-03-31 21:26:24 +0000475}
476
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000477void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000478
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000479 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
480 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com91961302011-05-09 18:39:58 +0000481 if (GrGLProgram::kUnusedUniform != uni &&
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())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000485
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000486 GrScalar centerX1 = sampler.getRadial2CenterX1();
487 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000488
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000489 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000490
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000491 // when were in the degenerate (linear) case the second
492 // value will be INF but the program doesn't read it. (We
493 // use the same 6 uniforms even though we don't need them
494 // all in the linear case just to keep the code complexity
495 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000496 float values[6] = {
497 GrScalarToFloat(a),
498 1 / (2.f * values[0]),
499 GrScalarToFloat(centerX1),
500 GrScalarToFloat(radius0),
501 GrScalarToFloat(GrMul(radius0, radius0)),
502 sampler.isRadial2PosRoot() ? 1.f : -1.f
503 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000504 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000505 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
506 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
507 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
508 }
509}
510
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000511void GrGpuGLShaders::flushConvolution(int s) {
512 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
513 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
514 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000515 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
516 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000517 }
518 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
519 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000520 GL_CALL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000521 }
522}
523
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000524void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000525 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000526 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000527 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000528 if (texture->allocatedWidth() != fProgramData->fTextureWidth[s] ||
529 texture->allocatedHeight() != fProgramData->fTextureWidth[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000530
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000531 float texelSize[] = {1.f / texture->allocatedWidth(),
532 1.f / texture->allocatedHeight()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000533 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000534 }
535 }
junov@google.comf93e7172011-03-31 21:26:24 +0000536}
537
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000538void GrGpuGLShaders::flushEdgeAAData() {
539 const int& uni = fProgramData->fUniLocations.fEdgesUni;
540 if (GrGLProgram::kUnusedUniform != uni) {
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000541 int count = fCurrDrawState.fEdgeAANumEdges;
542 Edge edges[kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000543 // Flip the edges in Y
544 float height = fCurrDrawState.fRenderTarget->height();
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000545 for (int i = 0; i < count; ++i) {
546 edges[i] = fCurrDrawState.fEdgeAAEdges[i];
547 float b = edges[i].fY;
548 edges[i].fY = -b;
549 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000550 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000551 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000552 }
553}
554
Scroggo01b87ec2011-05-11 18:05:38 +0000555static const float ONE_OVER_255 = 1.f / 255.f;
556
557#define GR_COLOR_TO_VEC4(color) {\
558 GrColorUnpackR(color) * ONE_OVER_255,\
559 GrColorUnpackG(color) * ONE_OVER_255,\
560 GrColorUnpackB(color) * ONE_OVER_255,\
561 GrColorUnpackA(color) * ONE_OVER_255 \
562}
563
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000564void GrGpuGLShaders::flushColor() {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000565 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000566 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000567 // color will be specified per-vertex as an attribute
568 // invalidate the const vertex attrib color
569 fHWDrawState.fColor = GrColor_ILLEGAL;
570 } else {
571 switch (desc.fColorType) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000572 case ProgramDesc::kAttribute_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000573 if (fHWDrawState.fColor != fCurrDrawState.fColor) {
574 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000575 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000576 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
577 c));
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000578 fHWDrawState.fColor = fCurrDrawState.fColor;
579 }
580 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000581 case ProgramDesc::kUniform_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000582 if (fProgramData->fColor != fCurrDrawState.fColor) {
583 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000584 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com91961302011-05-09 18:39:58 +0000585 GrAssert(GrGLProgram::kUnusedUniform !=
586 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000587 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
588 1, c));
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000589 fProgramData->fColor = fCurrDrawState.fColor;
590 }
591 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000592 case ProgramDesc::kNone_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000593 GrAssert(0xffffffff == fCurrDrawState.fColor);
594 break;
595 default:
596 GrCrash("Unknown color type.");
597 }
598 }
Scroggo97c88c22011-05-11 14:05:25 +0000599 if (fProgramData->fUniLocations.fColorFilterUni
600 != GrGLProgram::kUnusedUniform
601 && fProgramData->fColorFilterColor
602 != fCurrDrawState.fColorFilterColor) {
Scroggo01b87ec2011-05-11 18:05:38 +0000603 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColorFilterColor);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000604 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
Scroggo97c88c22011-05-11 14:05:25 +0000605 fProgramData->fColorFilterColor = fCurrDrawState.fColorFilterColor;
606 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000607}
608
609
junov@google.comf93e7172011-03-31 21:26:24 +0000610bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
611 if (!flushGLStateCommon(type)) {
612 return false;
613 }
614
615 if (fDirtyFlags.fRenderTargetChanged) {
616 // our coords are in pixel space and the GL matrices map to NDC
617 // so if the viewport changed, our matrix is now wrong.
junov@google.comf93e7172011-03-31 21:26:24 +0000618 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000619 // we assume all shader matrices may be wrong after viewport changes
620 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000621 }
622
junov@google.comf93e7172011-03-31 21:26:24 +0000623 buildProgram(type);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000624 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000625 if (NULL == fProgramData) {
626 GrAssert(!"Failed to create program!");
627 return false;
628 }
junov@google.comf93e7172011-03-31 21:26:24 +0000629
630 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000631 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000632 fHWProgramID = fProgramData->fProgramID;
633 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000634 GrBlendCoeff srcCoeff = fCurrDrawState.fSrcBlend;
635 GrBlendCoeff dstCoeff = fCurrDrawState.fDstBlend;
636
637 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
638 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000639
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000640 this->flushColor();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000641
bsalomon@google.com91961302011-05-09 18:39:58 +0000642 GrMatrix* currViewMatrix;
643 if (GrGLProgram::kSetAsAttribute ==
644 fProgramData->fUniLocations.fViewMatrixUni) {
645 currViewMatrix = &fHWDrawState.fViewMatrix;
646 } else {
647 currViewMatrix = &fProgramData->fViewMatrix;
648 }
junov@google.comf93e7172011-03-31 21:26:24 +0000649
bsalomon@google.com91961302011-05-09 18:39:58 +0000650 if (*currViewMatrix != fCurrDrawState.fViewMatrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000651 flushViewMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000652 *currViewMatrix = fCurrDrawState.fViewMatrix;
junov@google.comf93e7172011-03-31 21:26:24 +0000653 }
654
655 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000656 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000657
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000658 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000659
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000660 this->flushConvolution(s);
661
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000662 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000663
664 this->flushTextureDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000665 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000666 this->flushEdgeAAData();
junov@google.comf93e7172011-03-31 21:26:24 +0000667 resetDirtyFlags();
668 return true;
669}
670
671void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000672}
673
674void GrGpuGLShaders::setupGeometry(int* startVertex,
675 int* startIndex,
676 int vertexCount,
677 int indexCount) {
678
679 int newColorOffset;
680 int newTexCoordOffsets[kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000681 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000682
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000683 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
684 this->getGeomSrc().fVertexLayout,
685 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000686 &newColorOffset,
687 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000688 int oldColorOffset;
689 int oldTexCoordOffsets[kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000690 int oldEdgeOffset;
691
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000692 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
693 fHWGeometryState.fVertexLayout,
694 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000695 &oldColorOffset,
696 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000697 bool indexed = NULL != startIndex;
698
699 int extraVertexOffset;
700 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000701 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000702
703 GrGLenum scalarType;
704 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000705 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000706 scalarType = GrGLTextType;
707 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
708 } else {
709 scalarType = GrGLType;
710 texCoordNorm = false;
711 }
712
713 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
714 *startVertex = 0;
715 if (indexed) {
716 *startIndex += extraIndexOffset;
717 }
718
719 // all the Pointers must be set if any of these are true
720 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
721 vertexOffset != fHWGeometryState.fVertexOffset ||
722 newStride != oldStride;
723
724 // position and tex coord offsets change if above conditions are true
725 // or the type/normalization changed based on text vs nontext type coords.
726 bool posAndTexChange = allOffsetsChange ||
727 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
728 (kTextFormat_VertexLayoutBit &
729 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000730 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000731
732 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000733 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000734 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000735 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000736 fHWGeometryState.fVertexOffset = vertexOffset;
737 }
738
739 for (int t = 0; t < kMaxTexCoords; ++t) {
740 if (newTexCoordOffsets[t] > 0) {
741 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000742 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000743 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000744 GL_CALL(EnableVertexAttribArray(idx));
745 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000746 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000747 } else if (posAndTexChange ||
748 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000749 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000750 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000751 }
752 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000753 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000754 }
755 }
756
757 if (newColorOffset > 0) {
758 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000759 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000760 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000761 GL_CALL(EnableVertexAttribArray(idx));
762 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000763 true, newStride, colorOffset));
764 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000765 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000766 true, newStride, colorOffset));
767 }
768 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000769 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000770 }
771
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000772 if (newEdgeOffset > 0) {
773 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
774 int idx = GrGLProgram::EdgeAttributeIdx();
775 if (oldEdgeOffset <= 0) {
776 GL_CALL(EnableVertexAttribArray(idx));
777 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
778 false, newStride, edgeOffset));
779 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
780 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
781 false, newStride, edgeOffset));
782 }
783 } else if (oldEdgeOffset > 0) {
784 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
785 }
786
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000787 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000788 fHWGeometryState.fArrayPtrsDirty = false;
789}
790
791void GrGpuGLShaders::buildProgram(GrPrimitiveType type) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000792 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
junov@google.comf93e7172011-03-31 21:26:24 +0000793
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000794 // The descriptor is used as a cache key. Thus when a field of the
795 // descriptor will not affect program generation (because of the vertex
796 // layout in use or other descriptor field settings) it should be set
797 // to a canonical value to avoid duplicate programs with different keys.
798
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000799 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000800 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000801
802 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
803
804 bool requiresAttributeColors = desc.fVertexLayout & kColor_VertexLayoutBit;
805 // fColorType records how colors are specified for the program. Strip
806 // the bit from the layout to avoid false negatives when searching for an
807 // existing program in the cache.
808 desc.fVertexLayout &= ~(kColor_VertexLayoutBit);
809
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000810 desc.fColorFilterXfermode = fCurrDrawState.fColorFilterXfermode;
811
junov@google.comf93e7172011-03-31 21:26:24 +0000812#if GR_AGGRESSIVE_SHADER_OPTS
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000813 if (!requiresAttributeColors && (0xffffffff == fCurrDrawState.fColor)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000814 desc.fColorType = ProgramDesc::kNone_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000815 } else
junov@google.comf93e7172011-03-31 21:26:24 +0000816#endif
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000817#if GR_GL_NO_CONSTANT_ATTRIBUTES
818 if (!requiresAttributeColors) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000819 desc.fColorType = ProgramDesc::kUniform_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000820 } else
821#endif
822 {
bsalomon@google.com6a77cc52011-04-28 17:33:34 +0000823 if (requiresAttributeColors) {} // suppress unused var warning
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000824 desc.fColorType = ProgramDesc::kAttribute_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000825 }
junov@google.comf93e7172011-03-31 21:26:24 +0000826
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000827 desc.fEdgeAANumEdges = fCurrDrawState.fEdgeAANumEdges;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000828 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 && SkToBool(fCurrDrawState.fFlagBits & kEdgeAAConcave_StateBit);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000829
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000830 int lastEnabledStage = -1;
831
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000832 if (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit) {
833 desc.fVertexEdgeType = fCurrDrawState.fVertexEdgeType;
834 } else {
835 // use canonical value when not set to avoid cache misses
836 desc.fVertexEdgeType = GrDrawTarget::kHairLine_EdgeType;
837 }
838
junov@google.comf93e7172011-03-31 21:26:24 +0000839 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000840 StageDesc& stage = desc.fStages[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000841
tomhudson@google.com0d831722011-06-02 15:37:14 +0000842 stage.fOptFlags = 0;
843 stage.setEnabled(this->isStageEnabled(s));
junov@google.comf93e7172011-03-31 21:26:24 +0000844
tomhudson@google.com0d831722011-06-02 15:37:14 +0000845 if (stage.isEnabled()) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000846 lastEnabledStage = s;
junov@google.comf93e7172011-03-31 21:26:24 +0000847 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
848 GrAssert(NULL != texture);
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000849 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000850 // we matrix to invert when orientation is TopDown, so make sure
851 // we aren't in that case before flagging as identity.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000852 if (TextureMatrixIsIdentity(texture, sampler)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000853 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000854 } else if (!getSamplerMatrix(s).hasPerspective()) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000855 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000856 }
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000857 switch (sampler.getSampleMode()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000858 case GrSamplerState::kNormal_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000859 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000860 break;
861 case GrSamplerState::kRadial_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000862 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000863 break;
864 case GrSamplerState::kRadial2_SampleMode:
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000865 if (sampler.radial2IsDegenerate()) {
866 stage.fCoordMapping =
867 StageDesc::kRadial2GradientDegenerate_CoordMapping;
868 } else {
869 stage.fCoordMapping =
870 StageDesc::kRadial2Gradient_CoordMapping;
871 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000872 break;
873 case GrSamplerState::kSweep_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000874 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000875 break;
876 default:
877 GrCrash("Unexpected sample mode!");
878 break;
879 }
880
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000881 switch (sampler.getFilter()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000882 // these both can use a regular texture2D()
883 case GrSamplerState::kNearest_Filter:
884 case GrSamplerState::kBilinear_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000885 stage.fFetchMode = StageDesc::kSingle_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000886 break;
887 // performs 4 texture2D()s
888 case GrSamplerState::k4x4Downsample_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000889 stage.fFetchMode = StageDesc::k2x2_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000890 break;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000891 // performs fKernelWidth texture2D()s
892 case GrSamplerState::kConvolution_Filter:
893 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
894 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000895 default:
896 GrCrash("Unexpected filter!");
897 break;
junov@google.comf93e7172011-03-31 21:26:24 +0000898 }
899
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000900 if (sampler.hasTextureDomain()) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000901 GrAssert(GrSamplerState::kClamp_WrapMode ==
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000902 sampler.getWrapX() &&
903 GrSamplerState::kClamp_WrapMode ==
904 sampler.getWrapY());
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000905 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
junov@google.com6acc9b32011-05-16 18:32:07 +0000906 }
907
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000908 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000909 stage.fModulation = StageDesc::kAlpha_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000910 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000911 stage.fModulation = StageDesc::kColor_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000912 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000913 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) {
914 stage.fKernelWidth = sampler.getKernelWidth();
915 } else {
916 stage.fKernelWidth = 0;
917 }
junov@google.comf93e7172011-03-31 21:26:24 +0000918 } else {
919 stage.fOptFlags = 0;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000920 stage.fCoordMapping = (StageDesc::CoordMapping)0;
921 stage.fModulation = (StageDesc::Modulation)0;
junov@google.comf93e7172011-03-31 21:26:24 +0000922 }
923 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000924
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000925 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000926 // use canonical value when coverage/color distinction won't affect
927 // generated code to prevent duplicate programs.
928 desc.fFirstCoverageStage = kNumStages;
929 if (fCurrDrawState.fFirstCoverageStage <= lastEnabledStage) {
930 // color filter is applied between color/coverage computation
931 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
932 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
933 }
934
935 // We could consider cases where the final color is solid (0xff alpha)
936 // and the dst coeff can correctly be set to a non-dualsrc gl value.
937 // (e.g. solid draw, and dst coeff is kZero. It's correct to make
938 // the dst coeff be kISA. Or solid draw with kSA can be tweaked to be
939 // kOne).
940 if (fDualSourceBlendingSupport) {
941 if (kZero_BlendCoeff == fCurrDrawState.fDstBlend) {
942 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000943 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000944 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
945 } else if (kSA_BlendCoeff == fCurrDrawState.fDstBlend) {
946 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
947 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000948 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000949 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
950 } else if (kSC_BlendCoeff == fCurrDrawState.fDstBlend) {
951 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
952 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000953 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000954 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
955 }
956 }
957 }
junov@google.comf93e7172011-03-31 21:26:24 +0000958}