blob: 80f0268b02f24376bdc03c5724a39b24a2497b7e [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));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000133 if (programData->fGShaderID) {
134 GR_GL_CALL(gl, DeleteShader(programData->fGShaderID));
135 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000136 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
137 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000138 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
139}
140
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000141////////////////////////////////////////////////////////////////////////////////
142
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000143#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
144
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000145namespace {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000146
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000147GrGLProgram::GLSLVersion get_glsl_version(GrGLBinding binding,
148 const GrGLInterface* gl) {
149 GrGLSLVersion ver = GrGLGetGLSLVersion(gl);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000150 switch (binding) {
151 case kDesktop_GrGLBinding:
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000152 GrAssert(ver >= GR_GLSL_VER(1,20));
153 if (ver >= GR_GLSL_VER(1,50)) {
154 return GrGLProgram::k150_GLSLVersion;
155 } else if (ver >= GR_GLSL_VER(1,30)) {
156 return GrGLProgram::k130_GLSLVersion;
157 } else {
158 return GrGLProgram::k120_GLSLVersion;
159 }
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000160 case kES2_GrGLBinding:
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000161 // version 1.00 of ES GLSL based on ver 1.20 of desktop GLSL
162 GrAssert(ver >= GR_GL_VER(1,00));
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000163 return GrGLProgram::k120_GLSLVersion;
164 default:
165 GrCrash("Attempting to get GLSL version in unknown or fixed-"
166 "function GL binding.");
167 return GrGLProgram::k120_GLSLVersion; // suppress warning
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000168 }
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000169}
170
171template <typename T>
172T random_val(GrRandom* r, T count) {
173 return (T)(int)(r->nextF() * count);
174}
175
176}
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000177
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000178bool GrGpuGLShaders::programUnitTest() {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000179
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000180 GrGLProgram::GLSLVersion glslVersion =
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000181 get_glsl_version(this->glBinding(), this->glInterface());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000182 static const int STAGE_OPTS[] = {
183 0,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000184 StageDesc::kNoPerspective_OptFlagBit,
185 StageDesc::kIdentity_CoordMapping
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000186 };
187 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000188 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000189
190 static const int NUM_TESTS = 512;
191
192 // GrRandoms nextU() values have patterns in the low bits
193 // So using nextU() % array_count might never take some values.
194 GrRandom random;
195 for (int t = 0; t < NUM_TESTS; ++t) {
196
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000197#if 0
198 GrPrintf("\nTest Program %d\n-------------\n", t);
199 static const int stop = -1;
200 if (t == stop) {
201 int breakpointhere = 9;
202 }
203#endif
204
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000205 pdesc.fVertexLayout = 0;
206 pdesc.fEmitsPointSize = random.nextF() > .5f;
207 float colorType = random.nextF();
208 if (colorType < 1.f / 3.f) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000209 pdesc.fColorType = ProgramDesc::kAttribute_ColorType;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000210 } else if (colorType < 2.f / 3.f) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000211 pdesc.fColorType = ProgramDesc::kUniform_ColorType;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000212 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000213 pdesc.fColorType = ProgramDesc::kNone_ColorType;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000214 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000215
216 int idx = (int)(random.nextF() * (SkXfermode::kCoeffModesCnt));
217 pdesc.fColorFilterXfermode = (SkXfermode::Mode)idx;
218
219 idx = (int)(random.nextF() * (kNumStages+1));
220 pdesc.fFirstCoverageStage = idx;
221
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000222#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com6f92f182011-09-29 15:05:48 +0000223 pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport &&
224 random.nextF() > .5f;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000225#endif
226
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000227 bool edgeAA = random.nextF() > .5f;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000228 if (edgeAA) {
229 bool vertexEdgeAA = random.nextF() > .5f;
230 if (vertexEdgeAA) {
231 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000232 if (this->getCaps().fShaderDerivativeSupport) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000233 pdesc.fVertexEdgeType = random.nextF() > 0.5f ?
234 kHairQuad_EdgeType :
235 kHairLine_EdgeType;
236 } else {
237 pdesc.fVertexEdgeType = kHairLine_EdgeType;
238 }
239 pdesc.fEdgeAANumEdges = 0;
240 } else {
bsalomon@google.comc2c9b972011-10-03 13:17:22 +0000241 pdesc.fEdgeAANumEdges = SkToS8(1 + random.nextF() *
242 this->getMaxEdges());
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000243 pdesc.fEdgeAAConcave = random.nextF() > .5f;
244 }
245 } else {
246 pdesc.fEdgeAANumEdges = 0;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000247 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000248
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000249 if (this->getCaps().fDualSourceBlendingSupport) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000250 pdesc.fDualSrcOutput =
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000251 (ProgramDesc::DualSrcOutput)
252 (int)(random.nextF() * ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000253 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000254 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000255 }
256
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000257 for (int s = 0; s < kNumStages; ++s) {
258 // enable the stage?
259 if (random.nextF() > .5f) {
260 // use separate tex coords?
261 if (random.nextF() > .5f) {
262 int t = (int)(random.nextF() * kMaxTexCoords);
263 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
264 } else {
265 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
266 }
267 }
268 // use text-formatted verts?
269 if (random.nextF() > .5f) {
270 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
271 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000272 idx = (int)(random.nextF() * GR_ARRAY_COUNT(STAGE_OPTS));
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000273 StageDesc& stage = pdesc.fStages[s];
274 stage.fOptFlags = STAGE_OPTS[idx];
275 stage.fModulation = random_val(&random, StageDesc::kModulationCnt);
276 stage.fCoordMapping = random_val(&random, StageDesc::kCoordMappingCnt);
277 stage.fFetchMode = random_val(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000278 // convolution shaders don't work with persp tex matrix
279 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) {
280 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
281 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000282 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000283 stage.fKernelWidth = static_cast<int8_t>(4 * random.nextF() + 2);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000284 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000285 CachedData cachedData;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000286 if (!program.genProgram(this->glInterface(),
287 glslVersion,
288 &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000289 return false;
290 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000291 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000292 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000293 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000294}
295
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000296namespace {
297GrGLBinding get_binding_in_use(const GrGLInterface* gl) {
298 if (gl->supportsDesktop()) {
299 return kDesktop_GrGLBinding;
300 } else {
301 GrAssert(gl->supportsES2());
302 return kES2_GrGLBinding;
303 }
304}
305}
306
307GrGpuGLShaders::GrGpuGLShaders(const GrGLInterface* gl)
308 : GrGpuGL(gl, get_binding_in_use(gl)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000309
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000310 GrGLProgram::GLSLVersion glslVersion =
311 get_glsl_version(this->glBinding(), gl);
312
313 // Enable supported shader-releated caps
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000314 fCaps.fShaderSupport = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000315 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000316 fCaps.fDualSourceBlendingSupport =
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000317 this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000318 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000319 fCaps.fShaderDerivativeSupport = true;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000320 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
321 fCaps.fGeometryShaderSupport =
322 this->glVersion() >= GR_GL_VER(3,2) &&
323 glslVersion >= GrGLProgram::k150_GLSLVersion;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000324 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000325 fCaps.fShaderDerivativeSupport =
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000326 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000327 }
junov@google.comf93e7172011-03-31 21:26:24 +0000328
329 fProgramData = NULL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000330 fProgramCache = new ProgramCache(gl, glslVersion);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000331
332#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000333 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000334#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000335}
336
337GrGpuGLShaders::~GrGpuGLShaders() {
338 delete fProgramCache;
339}
340
341const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000342 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000343
344 if (GrGLProgram::kSetAsAttribute ==
345 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
346 return fHWDrawState.fSamplerStates[stage].getMatrix();
347 } else {
348 return fProgramData->fTextureMatrices[stage];
349 }
junov@google.comf93e7172011-03-31 21:26:24 +0000350}
351
352void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000353 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000354 if (GrGLProgram::kSetAsAttribute ==
355 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
356 fHWDrawState.fSamplerStates[stage].setMatrix(matrix);
357 } else {
358 fProgramData->fTextureMatrices[stage] = matrix;
359 }
junov@google.comf93e7172011-03-31 21:26:24 +0000360}
361
362void GrGpuGLShaders::resetContext() {
363 INHERITED::resetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000364
junov@google.comf93e7172011-03-31 21:26:24 +0000365 fHWGeometryState.fVertexLayout = 0;
366 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000367 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000368 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000369 for (int t = 0; t < kMaxTexCoords; ++t) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000370 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000371 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000372 GL_CALL(EnableVertexAttribArray(GrGLProgram::PositionAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000373
374 fHWProgramID = 0;
375}
376
377void GrGpuGLShaders::flushViewMatrix() {
378 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000379 GrMatrix m;
380 m.setAll(
junov@google.comf93e7172011-03-31 21:26:24 +0000381 GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1,
382 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1,
383 0, 0, GrMatrix::I()[8]);
384 m.setConcat(m, fCurrDrawState.fViewMatrix);
385
386 // ES doesn't allow you to pass true to the transpose param,
387 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000388 GrGLfloat mt[] = {
389 GrScalarToFloat(m[GrMatrix::kMScaleX]),
390 GrScalarToFloat(m[GrMatrix::kMSkewY]),
391 GrScalarToFloat(m[GrMatrix::kMPersp0]),
392 GrScalarToFloat(m[GrMatrix::kMSkewX]),
393 GrScalarToFloat(m[GrMatrix::kMScaleY]),
394 GrScalarToFloat(m[GrMatrix::kMPersp1]),
395 GrScalarToFloat(m[GrMatrix::kMTransX]),
396 GrScalarToFloat(m[GrMatrix::kMTransY]),
397 GrScalarToFloat(m[GrMatrix::kMPersp2])
junov@google.comf93e7172011-03-31 21:26:24 +0000398 };
bsalomon@google.com91961302011-05-09 18:39:58 +0000399
400 if (GrGLProgram::kSetAsAttribute ==
401 fProgramData->fUniLocations.fViewMatrixUni) {
402 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000403 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
404 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
405 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000406 } else {
407 GrAssert(GrGLProgram::kUnusedUniform !=
408 fProgramData->fUniLocations.fViewMatrixUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000409 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
410 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000411 }
junov@google.comf93e7172011-03-31 21:26:24 +0000412}
413
junov@google.com6acc9b32011-05-16 18:32:07 +0000414void GrGpuGLShaders::flushTextureDomain(int s) {
415 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
416 if (GrGLProgram::kUnusedUniform != uni) {
twiz@google.com76b82742011-06-02 20:30:02 +0000417 const GrRect &texDom =
junov@google.com6acc9b32011-05-16 18:32:07 +0000418 fCurrDrawState.fSamplerStates[s].getTextureDomain();
419
twiz@google.com76b82742011-06-02 20:30:02 +0000420 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
421 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000422
junov@google.com2f839402011-05-24 15:13:01 +0000423 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000424
junov@google.com2f839402011-05-24 15:13:01 +0000425 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000426 GrScalarToFloat(texDom.left()),
427 GrScalarToFloat(texDom.top()),
428 GrScalarToFloat(texDom.right()),
429 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000430 };
431
432 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
433 GrGLTexture::Orientation orientation = texture->orientation();
434
435 // vertical flip if necessary
436 if (GrGLTexture::kBottomUp_Orientation == orientation) {
437 values[1] = 1.0f - values[1];
438 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000439 // The top and bottom were just flipped, so correct the ordering
440 // of elements so that values = (l, t, r, b).
441 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000442 }
443
444 values[0] *= SkScalarToFloat(texture->contentScaleX());
445 values[2] *= SkScalarToFloat(texture->contentScaleX());
446 values[1] *= SkScalarToFloat(texture->contentScaleY());
447 values[3] *= SkScalarToFloat(texture->contentScaleY());
448
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000449 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000450 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000451 }
452}
453
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000454void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000455 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000456 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
457 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000458 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000459 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
460 getHWSamplerMatrix(s) != getSamplerMatrix(s))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000461
bsalomon@google.com91961302011-05-09 18:39:58 +0000462 GrAssert(NULL != fCurrDrawState.fTextures[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000463
bsalomon@google.com91961302011-05-09 18:39:58 +0000464 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000465
bsalomon@google.com91961302011-05-09 18:39:58 +0000466 GrMatrix m = getSamplerMatrix(s);
467 GrSamplerState::SampleMode mode =
468 fCurrDrawState.fSamplerStates[s].getSampleMode();
469 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000470
bsalomon@google.com91961302011-05-09 18:39:58 +0000471 // ES doesn't allow you to pass true to the transpose param,
472 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000473 GrGLfloat mt[] = {
474 GrScalarToFloat(m[GrMatrix::kMScaleX]),
475 GrScalarToFloat(m[GrMatrix::kMSkewY]),
476 GrScalarToFloat(m[GrMatrix::kMPersp0]),
477 GrScalarToFloat(m[GrMatrix::kMSkewX]),
478 GrScalarToFloat(m[GrMatrix::kMScaleY]),
479 GrScalarToFloat(m[GrMatrix::kMPersp1]),
480 GrScalarToFloat(m[GrMatrix::kMTransX]),
481 GrScalarToFloat(m[GrMatrix::kMTransY]),
482 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000483 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000484
bsalomon@google.com91961302011-05-09 18:39:58 +0000485 if (GrGLProgram::kSetAsAttribute ==
486 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
487 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000488 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
489 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
490 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000491 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000492 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000493 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000494 recordHWSamplerMatrix(s, getSamplerMatrix(s));
495 }
496 }
junov@google.comf93e7172011-03-31 21:26:24 +0000497}
498
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000499void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000500
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000501 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
502 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com91961302011-05-09 18:39:58 +0000503 if (GrGLProgram::kUnusedUniform != uni &&
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())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000507
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000508 GrScalar centerX1 = sampler.getRadial2CenterX1();
509 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000510
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000511 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000512
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000513 // when were in the degenerate (linear) case the second
514 // value will be INF but the program doesn't read it. (We
515 // use the same 6 uniforms even though we don't need them
516 // all in the linear case just to keep the code complexity
517 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000518 float values[6] = {
519 GrScalarToFloat(a),
520 1 / (2.f * values[0]),
521 GrScalarToFloat(centerX1),
522 GrScalarToFloat(radius0),
523 GrScalarToFloat(GrMul(radius0, radius0)),
524 sampler.isRadial2PosRoot() ? 1.f : -1.f
525 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000526 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000527 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
528 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
529 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
530 }
531}
532
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000533void GrGpuGLShaders::flushConvolution(int s) {
534 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
535 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
536 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000537 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
538 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000539 }
540 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
541 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000542 GL_CALL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000543 }
544}
545
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000546void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000547 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000548 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000549 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000550 if (texture->allocatedWidth() != fProgramData->fTextureWidth[s] ||
551 texture->allocatedHeight() != fProgramData->fTextureWidth[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000552
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000553 float texelSize[] = {1.f / texture->allocatedWidth(),
554 1.f / texture->allocatedHeight()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000555 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000556 }
557 }
junov@google.comf93e7172011-03-31 21:26:24 +0000558}
559
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000560void GrGpuGLShaders::flushEdgeAAData() {
561 const int& uni = fProgramData->fUniLocations.fEdgesUni;
562 if (GrGLProgram::kUnusedUniform != uni) {
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000563 int count = fCurrDrawState.fEdgeAANumEdges;
564 Edge edges[kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000565 // Flip the edges in Y
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000566 float height =
567 static_cast<float>(fCurrDrawState.fRenderTarget->height());
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000568 for (int i = 0; i < count; ++i) {
569 edges[i] = fCurrDrawState.fEdgeAAEdges[i];
570 float b = edges[i].fY;
571 edges[i].fY = -b;
572 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000573 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000574 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000575 }
576}
577
Scroggo01b87ec2011-05-11 18:05:38 +0000578static const float ONE_OVER_255 = 1.f / 255.f;
579
580#define GR_COLOR_TO_VEC4(color) {\
581 GrColorUnpackR(color) * ONE_OVER_255,\
582 GrColorUnpackG(color) * ONE_OVER_255,\
583 GrColorUnpackB(color) * ONE_OVER_255,\
584 GrColorUnpackA(color) * ONE_OVER_255 \
585}
586
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000587void GrGpuGLShaders::flushColor() {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000588 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000589 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000590 // color will be specified per-vertex as an attribute
591 // invalidate the const vertex attrib color
592 fHWDrawState.fColor = GrColor_ILLEGAL;
593 } else {
594 switch (desc.fColorType) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000595 case ProgramDesc::kAttribute_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000596 if (fHWDrawState.fColor != fCurrDrawState.fColor) {
597 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000598 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000599 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
600 c));
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000601 fHWDrawState.fColor = fCurrDrawState.fColor;
602 }
603 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000604 case ProgramDesc::kUniform_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000605 if (fProgramData->fColor != fCurrDrawState.fColor) {
606 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000607 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com91961302011-05-09 18:39:58 +0000608 GrAssert(GrGLProgram::kUnusedUniform !=
609 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000610 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
611 1, c));
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000612 fProgramData->fColor = fCurrDrawState.fColor;
613 }
614 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000615 case ProgramDesc::kNone_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000616 GrAssert(0xffffffff == fCurrDrawState.fColor);
617 break;
618 default:
619 GrCrash("Unknown color type.");
620 }
621 }
Scroggo97c88c22011-05-11 14:05:25 +0000622 if (fProgramData->fUniLocations.fColorFilterUni
623 != GrGLProgram::kUnusedUniform
624 && fProgramData->fColorFilterColor
625 != fCurrDrawState.fColorFilterColor) {
Scroggo01b87ec2011-05-11 18:05:38 +0000626 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColorFilterColor);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000627 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
Scroggo97c88c22011-05-11 14:05:25 +0000628 fProgramData->fColorFilterColor = fCurrDrawState.fColorFilterColor;
629 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000630}
631
632
junov@google.comf93e7172011-03-31 21:26:24 +0000633bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
634 if (!flushGLStateCommon(type)) {
635 return false;
636 }
637
638 if (fDirtyFlags.fRenderTargetChanged) {
639 // our coords are in pixel space and the GL matrices map to NDC
640 // so if the viewport changed, our matrix is now wrong.
junov@google.comf93e7172011-03-31 21:26:24 +0000641 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000642 // we assume all shader matrices may be wrong after viewport changes
643 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000644 }
645
junov@google.comf93e7172011-03-31 21:26:24 +0000646 buildProgram(type);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000647 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000648 if (NULL == fProgramData) {
649 GrAssert(!"Failed to create program!");
650 return false;
651 }
junov@google.comf93e7172011-03-31 21:26:24 +0000652
653 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000654 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000655 fHWProgramID = fProgramData->fProgramID;
656 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000657 GrBlendCoeff srcCoeff = fCurrDrawState.fSrcBlend;
658 GrBlendCoeff dstCoeff = fCurrDrawState.fDstBlend;
659
660 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
661 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000662
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000663 this->flushColor();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000664
bsalomon@google.com91961302011-05-09 18:39:58 +0000665 GrMatrix* currViewMatrix;
666 if (GrGLProgram::kSetAsAttribute ==
667 fProgramData->fUniLocations.fViewMatrixUni) {
668 currViewMatrix = &fHWDrawState.fViewMatrix;
669 } else {
670 currViewMatrix = &fProgramData->fViewMatrix;
671 }
junov@google.comf93e7172011-03-31 21:26:24 +0000672
bsalomon@google.com91961302011-05-09 18:39:58 +0000673 if (*currViewMatrix != fCurrDrawState.fViewMatrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000674 flushViewMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000675 *currViewMatrix = fCurrDrawState.fViewMatrix;
junov@google.comf93e7172011-03-31 21:26:24 +0000676 }
677
678 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000679 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000680
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000681 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000682
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000683 this->flushConvolution(s);
684
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000685 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000686
687 this->flushTextureDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000688 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000689 this->flushEdgeAAData();
junov@google.comf93e7172011-03-31 21:26:24 +0000690 resetDirtyFlags();
691 return true;
692}
693
694void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000695}
696
697void GrGpuGLShaders::setupGeometry(int* startVertex,
698 int* startIndex,
699 int vertexCount,
700 int indexCount) {
701
702 int newColorOffset;
703 int newTexCoordOffsets[kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000704 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000705
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000706 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
707 this->getGeomSrc().fVertexLayout,
708 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000709 &newColorOffset,
710 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000711 int oldColorOffset;
712 int oldTexCoordOffsets[kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000713 int oldEdgeOffset;
714
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000715 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
716 fHWGeometryState.fVertexLayout,
717 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000718 &oldColorOffset,
719 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000720 bool indexed = NULL != startIndex;
721
722 int extraVertexOffset;
723 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000724 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000725
726 GrGLenum scalarType;
727 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000728 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000729 scalarType = GrGLTextType;
730 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
731 } else {
732 scalarType = GrGLType;
733 texCoordNorm = false;
734 }
735
736 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
737 *startVertex = 0;
738 if (indexed) {
739 *startIndex += extraIndexOffset;
740 }
741
742 // all the Pointers must be set if any of these are true
743 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
744 vertexOffset != fHWGeometryState.fVertexOffset ||
745 newStride != oldStride;
746
747 // position and tex coord offsets change if above conditions are true
748 // or the type/normalization changed based on text vs nontext type coords.
749 bool posAndTexChange = allOffsetsChange ||
750 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
751 (kTextFormat_VertexLayoutBit &
752 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000753 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000754
755 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000756 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000757 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000758 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000759 fHWGeometryState.fVertexOffset = vertexOffset;
760 }
761
762 for (int t = 0; t < kMaxTexCoords; ++t) {
763 if (newTexCoordOffsets[t] > 0) {
764 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000765 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000766 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000767 GL_CALL(EnableVertexAttribArray(idx));
768 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000769 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000770 } else if (posAndTexChange ||
771 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000772 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000773 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000774 }
775 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000776 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000777 }
778 }
779
780 if (newColorOffset > 0) {
781 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000782 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000783 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000784 GL_CALL(EnableVertexAttribArray(idx));
785 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000786 true, newStride, colorOffset));
787 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000788 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000789 true, newStride, colorOffset));
790 }
791 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000792 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000793 }
794
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000795 if (newEdgeOffset > 0) {
796 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
797 int idx = GrGLProgram::EdgeAttributeIdx();
798 if (oldEdgeOffset <= 0) {
799 GL_CALL(EnableVertexAttribArray(idx));
800 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
801 false, newStride, edgeOffset));
802 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
803 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
804 false, newStride, edgeOffset));
805 }
806 } else if (oldEdgeOffset > 0) {
807 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
808 }
809
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000810 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000811 fHWGeometryState.fArrayPtrsDirty = false;
812}
813
814void GrGpuGLShaders::buildProgram(GrPrimitiveType type) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000815 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
junov@google.comf93e7172011-03-31 21:26:24 +0000816
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000817 // The descriptor is used as a cache key. Thus when a field of the
818 // descriptor will not affect program generation (because of the vertex
819 // layout in use or other descriptor field settings) it should be set
820 // to a canonical value to avoid duplicate programs with different keys.
821
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000822 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000823 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000824
825 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
826
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000827 bool requiresAttributeColors = 0 != (desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000828 // fColorType records how colors are specified for the program. Strip
829 // the bit from the layout to avoid false negatives when searching for an
830 // existing program in the cache.
831 desc.fVertexLayout &= ~(kColor_VertexLayoutBit);
832
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000833 desc.fColorFilterXfermode = fCurrDrawState.fColorFilterXfermode;
834
junov@google.comf93e7172011-03-31 21:26:24 +0000835#if GR_AGGRESSIVE_SHADER_OPTS
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000836 if (!requiresAttributeColors && (0xffffffff == fCurrDrawState.fColor)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000837 desc.fColorType = ProgramDesc::kNone_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000838 } else
junov@google.comf93e7172011-03-31 21:26:24 +0000839#endif
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000840#if GR_GL_NO_CONSTANT_ATTRIBUTES
841 if (!requiresAttributeColors) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000842 desc.fColorType = ProgramDesc::kUniform_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000843 } else
844#endif
845 {
bsalomon@google.com6a77cc52011-04-28 17:33:34 +0000846 if (requiresAttributeColors) {} // suppress unused var warning
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000847 desc.fColorType = ProgramDesc::kAttribute_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000848 }
junov@google.comf93e7172011-03-31 21:26:24 +0000849
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000850 desc.fEdgeAANumEdges = fCurrDrawState.fEdgeAANumEdges;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000851 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 && SkToBool(fCurrDrawState.fFlagBits & kEdgeAAConcave_StateBit);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000852
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000853 int lastEnabledStage = -1;
854
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000855 if (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit) {
856 desc.fVertexEdgeType = fCurrDrawState.fVertexEdgeType;
857 } else {
858 // use canonical value when not set to avoid cache misses
859 desc.fVertexEdgeType = GrDrawTarget::kHairLine_EdgeType;
860 }
861
junov@google.comf93e7172011-03-31 21:26:24 +0000862 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000863 StageDesc& stage = desc.fStages[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000864
tomhudson@google.com0d831722011-06-02 15:37:14 +0000865 stage.fOptFlags = 0;
866 stage.setEnabled(this->isStageEnabled(s));
junov@google.comf93e7172011-03-31 21:26:24 +0000867
tomhudson@google.com0d831722011-06-02 15:37:14 +0000868 if (stage.isEnabled()) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000869 lastEnabledStage = s;
junov@google.comf93e7172011-03-31 21:26:24 +0000870 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
871 GrAssert(NULL != texture);
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000872 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000873 // we matrix to invert when orientation is TopDown, so make sure
874 // we aren't in that case before flagging as identity.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000875 if (TextureMatrixIsIdentity(texture, sampler)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000876 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000877 } else if (!getSamplerMatrix(s).hasPerspective()) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000878 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000879 }
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000880 switch (sampler.getSampleMode()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000881 case GrSamplerState::kNormal_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000882 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000883 break;
884 case GrSamplerState::kRadial_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000885 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000886 break;
887 case GrSamplerState::kRadial2_SampleMode:
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000888 if (sampler.radial2IsDegenerate()) {
889 stage.fCoordMapping =
890 StageDesc::kRadial2GradientDegenerate_CoordMapping;
891 } else {
892 stage.fCoordMapping =
893 StageDesc::kRadial2Gradient_CoordMapping;
894 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000895 break;
896 case GrSamplerState::kSweep_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000897 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000898 break;
899 default:
900 GrCrash("Unexpected sample mode!");
901 break;
902 }
903
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000904 switch (sampler.getFilter()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000905 // these both can use a regular texture2D()
906 case GrSamplerState::kNearest_Filter:
907 case GrSamplerState::kBilinear_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000908 stage.fFetchMode = StageDesc::kSingle_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000909 break;
910 // performs 4 texture2D()s
911 case GrSamplerState::k4x4Downsample_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000912 stage.fFetchMode = StageDesc::k2x2_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000913 break;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000914 // performs fKernelWidth texture2D()s
915 case GrSamplerState::kConvolution_Filter:
916 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
917 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000918 default:
919 GrCrash("Unexpected filter!");
920 break;
junov@google.comf93e7172011-03-31 21:26:24 +0000921 }
922
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000923 if (sampler.hasTextureDomain()) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000924 GrAssert(GrSamplerState::kClamp_WrapMode ==
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000925 sampler.getWrapX() &&
926 GrSamplerState::kClamp_WrapMode ==
927 sampler.getWrapY());
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000928 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
junov@google.com6acc9b32011-05-16 18:32:07 +0000929 }
930
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000931 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000932 stage.fModulation = StageDesc::kAlpha_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000933 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000934 stage.fModulation = StageDesc::kColor_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000935 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000936 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) {
937 stage.fKernelWidth = sampler.getKernelWidth();
938 } else {
939 stage.fKernelWidth = 0;
940 }
junov@google.comf93e7172011-03-31 21:26:24 +0000941 } else {
942 stage.fOptFlags = 0;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000943 stage.fCoordMapping = (StageDesc::CoordMapping)0;
944 stage.fModulation = (StageDesc::Modulation)0;
junov@google.comf93e7172011-03-31 21:26:24 +0000945 }
946 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000947
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000948 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000949
950 // currently the experimental GS will only work with triangle prims
951 // (and it doesn't do anything other than pass through values from
952 // the VS to the FS anyway).
953#if 0 && GR_GL_EXPERIMENTAL_GS
954 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
955#endif
956
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000957 // use canonical value when coverage/color distinction won't affect
958 // generated code to prevent duplicate programs.
959 desc.fFirstCoverageStage = kNumStages;
960 if (fCurrDrawState.fFirstCoverageStage <= lastEnabledStage) {
961 // color filter is applied between color/coverage computation
962 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
963 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
964 }
965
966 // We could consider cases where the final color is solid (0xff alpha)
967 // and the dst coeff can correctly be set to a non-dualsrc gl value.
968 // (e.g. solid draw, and dst coeff is kZero. It's correct to make
969 // the dst coeff be kISA. Or solid draw with kSA can be tweaked to be
970 // kOne).
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000971 if (this->getCaps().fDualSourceBlendingSupport) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000972 if (kZero_BlendCoeff == fCurrDrawState.fDstBlend) {
973 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000974 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000975 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
976 } else if (kSA_BlendCoeff == fCurrDrawState.fDstBlend) {
977 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
978 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000979 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000980 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
981 } else if (kSC_BlendCoeff == fCurrDrawState.fDstBlend) {
982 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
983 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000984 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000985 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
986 }
987 }
988 }
junov@google.comf93e7172011-03-31 21:26:24 +0000989}