blob: 4f39965322140a9058dd45e2ca31bb2e75692a10 [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
223 pdesc.fExperimentalGS = random.nextF() > .5f;
224#endif
225
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000226 bool edgeAA = random.nextF() > .5f;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000227 if (edgeAA) {
228 bool vertexEdgeAA = random.nextF() > .5f;
229 if (vertexEdgeAA) {
230 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000231 if (this->getCaps().fShaderDerivativeSupport) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000232 pdesc.fVertexEdgeType = random.nextF() > 0.5f ?
233 kHairQuad_EdgeType :
234 kHairLine_EdgeType;
235 } else {
236 pdesc.fVertexEdgeType = kHairLine_EdgeType;
237 }
238 pdesc.fEdgeAANumEdges = 0;
239 } else {
240 pdesc.fEdgeAANumEdges = random.nextF() * this->getMaxEdges() + 1;
241 pdesc.fEdgeAAConcave = random.nextF() > .5f;
242 }
243 } else {
244 pdesc.fEdgeAANumEdges = 0;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000245 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000246
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000247 if (this->getCaps().fDualSourceBlendingSupport) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000248 pdesc.fDualSrcOutput =
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000249 (ProgramDesc::DualSrcOutput)
250 (int)(random.nextF() * ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000251 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000252 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000253 }
254
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000255 for (int s = 0; s < kNumStages; ++s) {
256 // enable the stage?
257 if (random.nextF() > .5f) {
258 // use separate tex coords?
259 if (random.nextF() > .5f) {
260 int t = (int)(random.nextF() * kMaxTexCoords);
261 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
262 } else {
263 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
264 }
265 }
266 // use text-formatted verts?
267 if (random.nextF() > .5f) {
268 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
269 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000270 idx = (int)(random.nextF() * GR_ARRAY_COUNT(STAGE_OPTS));
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000271 StageDesc& stage = pdesc.fStages[s];
272 stage.fOptFlags = STAGE_OPTS[idx];
273 stage.fModulation = random_val(&random, StageDesc::kModulationCnt);
274 stage.fCoordMapping = random_val(&random, StageDesc::kCoordMappingCnt);
275 stage.fFetchMode = random_val(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000276 // convolution shaders don't work with persp tex matrix
277 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) {
278 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
279 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000280 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000281 stage.fKernelWidth = 4 * random.nextF() + 2;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000282 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000283 CachedData cachedData;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000284 if (!program.genProgram(this->glInterface(),
285 glslVersion,
286 &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000287 return false;
288 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000289 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000290 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000291 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000292}
293
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000294namespace {
295GrGLBinding get_binding_in_use(const GrGLInterface* gl) {
296 if (gl->supportsDesktop()) {
297 return kDesktop_GrGLBinding;
298 } else {
299 GrAssert(gl->supportsES2());
300 return kES2_GrGLBinding;
301 }
302}
303}
304
305GrGpuGLShaders::GrGpuGLShaders(const GrGLInterface* gl)
306 : GrGpuGL(gl, get_binding_in_use(gl)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000307
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000308 GrGLProgram::GLSLVersion glslVersion =
309 get_glsl_version(this->glBinding(), gl);
310
311 // Enable supported shader-releated caps
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000312 fCaps.fShaderSupport = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000313 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000314 fCaps.fDualSourceBlendingSupport =
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000315 this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000316 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000317 fCaps.fShaderDerivativeSupport = true;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000318 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
319 fCaps.fGeometryShaderSupport =
320 this->glVersion() >= GR_GL_VER(3,2) &&
321 glslVersion >= GrGLProgram::k150_GLSLVersion;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000322 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000323 fCaps.fShaderDerivativeSupport =
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000324 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000325 }
junov@google.comf93e7172011-03-31 21:26:24 +0000326
327 fProgramData = NULL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000328 fProgramCache = new ProgramCache(gl, glslVersion);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000329
330#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000331 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000332#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000333}
334
335GrGpuGLShaders::~GrGpuGLShaders() {
336 delete fProgramCache;
337}
338
339const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000340 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000341
342 if (GrGLProgram::kSetAsAttribute ==
343 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
344 return fHWDrawState.fSamplerStates[stage].getMatrix();
345 } else {
346 return fProgramData->fTextureMatrices[stage];
347 }
junov@google.comf93e7172011-03-31 21:26:24 +0000348}
349
350void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000351 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000352 if (GrGLProgram::kSetAsAttribute ==
353 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
354 fHWDrawState.fSamplerStates[stage].setMatrix(matrix);
355 } else {
356 fProgramData->fTextureMatrices[stage] = matrix;
357 }
junov@google.comf93e7172011-03-31 21:26:24 +0000358}
359
360void GrGpuGLShaders::resetContext() {
361 INHERITED::resetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000362
junov@google.comf93e7172011-03-31 21:26:24 +0000363 fHWGeometryState.fVertexLayout = 0;
364 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000365 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000366 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000367 for (int t = 0; t < kMaxTexCoords; ++t) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000368 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000369 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000370 GL_CALL(EnableVertexAttribArray(GrGLProgram::PositionAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000371
372 fHWProgramID = 0;
373}
374
375void GrGpuGLShaders::flushViewMatrix() {
376 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000377 GrMatrix m;
378 m.setAll(
junov@google.comf93e7172011-03-31 21:26:24 +0000379 GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1,
380 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1,
381 0, 0, GrMatrix::I()[8]);
382 m.setConcat(m, fCurrDrawState.fViewMatrix);
383
384 // ES doesn't allow you to pass true to the transpose param,
385 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000386 GrGLfloat mt[] = {
387 GrScalarToFloat(m[GrMatrix::kMScaleX]),
388 GrScalarToFloat(m[GrMatrix::kMSkewY]),
389 GrScalarToFloat(m[GrMatrix::kMPersp0]),
390 GrScalarToFloat(m[GrMatrix::kMSkewX]),
391 GrScalarToFloat(m[GrMatrix::kMScaleY]),
392 GrScalarToFloat(m[GrMatrix::kMPersp1]),
393 GrScalarToFloat(m[GrMatrix::kMTransX]),
394 GrScalarToFloat(m[GrMatrix::kMTransY]),
395 GrScalarToFloat(m[GrMatrix::kMPersp2])
junov@google.comf93e7172011-03-31 21:26:24 +0000396 };
bsalomon@google.com91961302011-05-09 18:39:58 +0000397
398 if (GrGLProgram::kSetAsAttribute ==
399 fProgramData->fUniLocations.fViewMatrixUni) {
400 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000401 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
402 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
403 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000404 } else {
405 GrAssert(GrGLProgram::kUnusedUniform !=
406 fProgramData->fUniLocations.fViewMatrixUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000407 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
408 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000409 }
junov@google.comf93e7172011-03-31 21:26:24 +0000410}
411
junov@google.com6acc9b32011-05-16 18:32:07 +0000412void GrGpuGLShaders::flushTextureDomain(int s) {
413 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
414 if (GrGLProgram::kUnusedUniform != uni) {
twiz@google.com76b82742011-06-02 20:30:02 +0000415 const GrRect &texDom =
junov@google.com6acc9b32011-05-16 18:32:07 +0000416 fCurrDrawState.fSamplerStates[s].getTextureDomain();
417
twiz@google.com76b82742011-06-02 20:30:02 +0000418 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
419 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000420
junov@google.com2f839402011-05-24 15:13:01 +0000421 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000422
junov@google.com2f839402011-05-24 15:13:01 +0000423 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000424 GrScalarToFloat(texDom.left()),
425 GrScalarToFloat(texDom.top()),
426 GrScalarToFloat(texDom.right()),
427 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000428 };
429
430 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
431 GrGLTexture::Orientation orientation = texture->orientation();
432
433 // vertical flip if necessary
434 if (GrGLTexture::kBottomUp_Orientation == orientation) {
435 values[1] = 1.0f - values[1];
436 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000437 // The top and bottom were just flipped, so correct the ordering
438 // of elements so that values = (l, t, r, b).
439 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000440 }
441
442 values[0] *= SkScalarToFloat(texture->contentScaleX());
443 values[2] *= SkScalarToFloat(texture->contentScaleX());
444 values[1] *= SkScalarToFloat(texture->contentScaleY());
445 values[3] *= SkScalarToFloat(texture->contentScaleY());
446
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000447 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000448 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000449 }
450}
451
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000452void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000453 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000454 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
455 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000456 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000457 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
458 getHWSamplerMatrix(s) != getSamplerMatrix(s))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000459
bsalomon@google.com91961302011-05-09 18:39:58 +0000460 GrAssert(NULL != fCurrDrawState.fTextures[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000461
bsalomon@google.com91961302011-05-09 18:39:58 +0000462 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000463
bsalomon@google.com91961302011-05-09 18:39:58 +0000464 GrMatrix m = getSamplerMatrix(s);
465 GrSamplerState::SampleMode mode =
466 fCurrDrawState.fSamplerStates[s].getSampleMode();
467 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000468
bsalomon@google.com91961302011-05-09 18:39:58 +0000469 // ES doesn't allow you to pass true to the transpose param,
470 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000471 GrGLfloat mt[] = {
472 GrScalarToFloat(m[GrMatrix::kMScaleX]),
473 GrScalarToFloat(m[GrMatrix::kMSkewY]),
474 GrScalarToFloat(m[GrMatrix::kMPersp0]),
475 GrScalarToFloat(m[GrMatrix::kMSkewX]),
476 GrScalarToFloat(m[GrMatrix::kMScaleY]),
477 GrScalarToFloat(m[GrMatrix::kMPersp1]),
478 GrScalarToFloat(m[GrMatrix::kMTransX]),
479 GrScalarToFloat(m[GrMatrix::kMTransY]),
480 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000481 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000482
bsalomon@google.com91961302011-05-09 18:39:58 +0000483 if (GrGLProgram::kSetAsAttribute ==
484 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
485 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000486 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
487 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
488 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000489 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000490 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000491 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000492 recordHWSamplerMatrix(s, getSamplerMatrix(s));
493 }
494 }
junov@google.comf93e7172011-03-31 21:26:24 +0000495}
496
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000497void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000498
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000499 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
500 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com91961302011-05-09 18:39:58 +0000501 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000502 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
503 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
504 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000505
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000506 GrScalar centerX1 = sampler.getRadial2CenterX1();
507 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000508
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000509 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000510
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000511 // when were in the degenerate (linear) case the second
512 // value will be INF but the program doesn't read it. (We
513 // use the same 6 uniforms even though we don't need them
514 // all in the linear case just to keep the code complexity
515 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000516 float values[6] = {
517 GrScalarToFloat(a),
518 1 / (2.f * values[0]),
519 GrScalarToFloat(centerX1),
520 GrScalarToFloat(radius0),
521 GrScalarToFloat(GrMul(radius0, radius0)),
522 sampler.isRadial2PosRoot() ? 1.f : -1.f
523 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000524 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000525 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
526 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
527 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
528 }
529}
530
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000531void GrGpuGLShaders::flushConvolution(int s) {
532 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
533 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
534 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000535 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
536 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000537 }
538 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
539 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000540 GL_CALL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000541 }
542}
543
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000544void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000545 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000546 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000547 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000548 if (texture->allocatedWidth() != fProgramData->fTextureWidth[s] ||
549 texture->allocatedHeight() != fProgramData->fTextureWidth[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000550
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000551 float texelSize[] = {1.f / texture->allocatedWidth(),
552 1.f / texture->allocatedHeight()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000553 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000554 }
555 }
junov@google.comf93e7172011-03-31 21:26:24 +0000556}
557
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000558void GrGpuGLShaders::flushEdgeAAData() {
559 const int& uni = fProgramData->fUniLocations.fEdgesUni;
560 if (GrGLProgram::kUnusedUniform != uni) {
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000561 int count = fCurrDrawState.fEdgeAANumEdges;
562 Edge edges[kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000563 // Flip the edges in Y
564 float height = fCurrDrawState.fRenderTarget->height();
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000565 for (int i = 0; i < count; ++i) {
566 edges[i] = fCurrDrawState.fEdgeAAEdges[i];
567 float b = edges[i].fY;
568 edges[i].fY = -b;
569 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000570 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000571 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000572 }
573}
574
Scroggo01b87ec2011-05-11 18:05:38 +0000575static const float ONE_OVER_255 = 1.f / 255.f;
576
577#define GR_COLOR_TO_VEC4(color) {\
578 GrColorUnpackR(color) * ONE_OVER_255,\
579 GrColorUnpackG(color) * ONE_OVER_255,\
580 GrColorUnpackB(color) * ONE_OVER_255,\
581 GrColorUnpackA(color) * ONE_OVER_255 \
582}
583
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000584void GrGpuGLShaders::flushColor() {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000585 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000586 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000587 // color will be specified per-vertex as an attribute
588 // invalidate the const vertex attrib color
589 fHWDrawState.fColor = GrColor_ILLEGAL;
590 } else {
591 switch (desc.fColorType) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000592 case ProgramDesc::kAttribute_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000593 if (fHWDrawState.fColor != fCurrDrawState.fColor) {
594 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000595 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000596 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
597 c));
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000598 fHWDrawState.fColor = fCurrDrawState.fColor;
599 }
600 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000601 case ProgramDesc::kUniform_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000602 if (fProgramData->fColor != fCurrDrawState.fColor) {
603 // OpenGL ES only supports the float varities of glVertexAttrib
Scroggo01b87ec2011-05-11 18:05:38 +0000604 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor);
bsalomon@google.com91961302011-05-09 18:39:58 +0000605 GrAssert(GrGLProgram::kUnusedUniform !=
606 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000607 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
608 1, c));
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000609 fProgramData->fColor = fCurrDrawState.fColor;
610 }
611 break;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000612 case ProgramDesc::kNone_ColorType:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000613 GrAssert(0xffffffff == fCurrDrawState.fColor);
614 break;
615 default:
616 GrCrash("Unknown color type.");
617 }
618 }
Scroggo97c88c22011-05-11 14:05:25 +0000619 if (fProgramData->fUniLocations.fColorFilterUni
620 != GrGLProgram::kUnusedUniform
621 && fProgramData->fColorFilterColor
622 != fCurrDrawState.fColorFilterColor) {
Scroggo01b87ec2011-05-11 18:05:38 +0000623 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColorFilterColor);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000624 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
Scroggo97c88c22011-05-11 14:05:25 +0000625 fProgramData->fColorFilterColor = fCurrDrawState.fColorFilterColor;
626 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000627}
628
629
junov@google.comf93e7172011-03-31 21:26:24 +0000630bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
631 if (!flushGLStateCommon(type)) {
632 return false;
633 }
634
635 if (fDirtyFlags.fRenderTargetChanged) {
636 // our coords are in pixel space and the GL matrices map to NDC
637 // so if the viewport changed, our matrix is now wrong.
junov@google.comf93e7172011-03-31 21:26:24 +0000638 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000639 // we assume all shader matrices may be wrong after viewport changes
640 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000641 }
642
junov@google.comf93e7172011-03-31 21:26:24 +0000643 buildProgram(type);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000644 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000645 if (NULL == fProgramData) {
646 GrAssert(!"Failed to create program!");
647 return false;
648 }
junov@google.comf93e7172011-03-31 21:26:24 +0000649
650 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000651 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000652 fHWProgramID = fProgramData->fProgramID;
653 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000654 GrBlendCoeff srcCoeff = fCurrDrawState.fSrcBlend;
655 GrBlendCoeff dstCoeff = fCurrDrawState.fDstBlend;
656
657 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
658 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000659
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000660 this->flushColor();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000661
bsalomon@google.com91961302011-05-09 18:39:58 +0000662 GrMatrix* currViewMatrix;
663 if (GrGLProgram::kSetAsAttribute ==
664 fProgramData->fUniLocations.fViewMatrixUni) {
665 currViewMatrix = &fHWDrawState.fViewMatrix;
666 } else {
667 currViewMatrix = &fProgramData->fViewMatrix;
668 }
junov@google.comf93e7172011-03-31 21:26:24 +0000669
bsalomon@google.com91961302011-05-09 18:39:58 +0000670 if (*currViewMatrix != fCurrDrawState.fViewMatrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000671 flushViewMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000672 *currViewMatrix = fCurrDrawState.fViewMatrix;
junov@google.comf93e7172011-03-31 21:26:24 +0000673 }
674
675 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000676 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000677
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000678 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000679
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000680 this->flushConvolution(s);
681
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000682 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000683
684 this->flushTextureDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000685 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000686 this->flushEdgeAAData();
junov@google.comf93e7172011-03-31 21:26:24 +0000687 resetDirtyFlags();
688 return true;
689}
690
691void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000692}
693
694void GrGpuGLShaders::setupGeometry(int* startVertex,
695 int* startIndex,
696 int vertexCount,
697 int indexCount) {
698
699 int newColorOffset;
700 int newTexCoordOffsets[kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000701 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000702
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000703 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
704 this->getGeomSrc().fVertexLayout,
705 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000706 &newColorOffset,
707 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000708 int oldColorOffset;
709 int oldTexCoordOffsets[kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000710 int oldEdgeOffset;
711
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000712 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
713 fHWGeometryState.fVertexLayout,
714 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000715 &oldColorOffset,
716 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000717 bool indexed = NULL != startIndex;
718
719 int extraVertexOffset;
720 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000721 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000722
723 GrGLenum scalarType;
724 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000725 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000726 scalarType = GrGLTextType;
727 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
728 } else {
729 scalarType = GrGLType;
730 texCoordNorm = false;
731 }
732
733 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
734 *startVertex = 0;
735 if (indexed) {
736 *startIndex += extraIndexOffset;
737 }
738
739 // all the Pointers must be set if any of these are true
740 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
741 vertexOffset != fHWGeometryState.fVertexOffset ||
742 newStride != oldStride;
743
744 // position and tex coord offsets change if above conditions are true
745 // or the type/normalization changed based on text vs nontext type coords.
746 bool posAndTexChange = allOffsetsChange ||
747 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
748 (kTextFormat_VertexLayoutBit &
749 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000750 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000751
752 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000753 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000754 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000755 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000756 fHWGeometryState.fVertexOffset = vertexOffset;
757 }
758
759 for (int t = 0; t < kMaxTexCoords; ++t) {
760 if (newTexCoordOffsets[t] > 0) {
761 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000762 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000763 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000764 GL_CALL(EnableVertexAttribArray(idx));
765 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000766 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000767 } else if (posAndTexChange ||
768 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000769 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000770 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000771 }
772 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000773 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000774 }
775 }
776
777 if (newColorOffset > 0) {
778 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000779 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000780 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000781 GL_CALL(EnableVertexAttribArray(idx));
782 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000783 true, newStride, colorOffset));
784 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000785 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000786 true, newStride, colorOffset));
787 }
788 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000789 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000790 }
791
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000792 if (newEdgeOffset > 0) {
793 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
794 int idx = GrGLProgram::EdgeAttributeIdx();
795 if (oldEdgeOffset <= 0) {
796 GL_CALL(EnableVertexAttribArray(idx));
797 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
798 false, newStride, edgeOffset));
799 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
800 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
801 false, newStride, edgeOffset));
802 }
803 } else if (oldEdgeOffset > 0) {
804 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
805 }
806
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000807 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000808 fHWGeometryState.fArrayPtrsDirty = false;
809}
810
811void GrGpuGLShaders::buildProgram(GrPrimitiveType type) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000812 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
junov@google.comf93e7172011-03-31 21:26:24 +0000813
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000814 // The descriptor is used as a cache key. Thus when a field of the
815 // descriptor will not affect program generation (because of the vertex
816 // layout in use or other descriptor field settings) it should be set
817 // to a canonical value to avoid duplicate programs with different keys.
818
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000819 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000820 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000821
822 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
823
824 bool requiresAttributeColors = desc.fVertexLayout & kColor_VertexLayoutBit;
825 // fColorType records how colors are specified for the program. Strip
826 // the bit from the layout to avoid false negatives when searching for an
827 // existing program in the cache.
828 desc.fVertexLayout &= ~(kColor_VertexLayoutBit);
829
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000830 desc.fColorFilterXfermode = fCurrDrawState.fColorFilterXfermode;
831
junov@google.comf93e7172011-03-31 21:26:24 +0000832#if GR_AGGRESSIVE_SHADER_OPTS
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000833 if (!requiresAttributeColors && (0xffffffff == fCurrDrawState.fColor)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000834 desc.fColorType = ProgramDesc::kNone_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000835 } else
junov@google.comf93e7172011-03-31 21:26:24 +0000836#endif
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000837#if GR_GL_NO_CONSTANT_ATTRIBUTES
838 if (!requiresAttributeColors) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000839 desc.fColorType = ProgramDesc::kUniform_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000840 } else
841#endif
842 {
bsalomon@google.com6a77cc52011-04-28 17:33:34 +0000843 if (requiresAttributeColors) {} // suppress unused var warning
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000844 desc.fColorType = ProgramDesc::kAttribute_ColorType;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000845 }
junov@google.comf93e7172011-03-31 21:26:24 +0000846
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000847 desc.fEdgeAANumEdges = fCurrDrawState.fEdgeAANumEdges;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000848 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 && SkToBool(fCurrDrawState.fFlagBits & kEdgeAAConcave_StateBit);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000849
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000850 int lastEnabledStage = -1;
851
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000852 if (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit) {
853 desc.fVertexEdgeType = fCurrDrawState.fVertexEdgeType;
854 } else {
855 // use canonical value when not set to avoid cache misses
856 desc.fVertexEdgeType = GrDrawTarget::kHairLine_EdgeType;
857 }
858
junov@google.comf93e7172011-03-31 21:26:24 +0000859 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000860 StageDesc& stage = desc.fStages[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000861
tomhudson@google.com0d831722011-06-02 15:37:14 +0000862 stage.fOptFlags = 0;
863 stage.setEnabled(this->isStageEnabled(s));
junov@google.comf93e7172011-03-31 21:26:24 +0000864
tomhudson@google.com0d831722011-06-02 15:37:14 +0000865 if (stage.isEnabled()) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000866 lastEnabledStage = s;
junov@google.comf93e7172011-03-31 21:26:24 +0000867 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
868 GrAssert(NULL != texture);
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000869 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000870 // we matrix to invert when orientation is TopDown, so make sure
871 // we aren't in that case before flagging as identity.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000872 if (TextureMatrixIsIdentity(texture, sampler)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000873 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000874 } else if (!getSamplerMatrix(s).hasPerspective()) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000875 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000876 }
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000877 switch (sampler.getSampleMode()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000878 case GrSamplerState::kNormal_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000879 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000880 break;
881 case GrSamplerState::kRadial_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000882 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000883 break;
884 case GrSamplerState::kRadial2_SampleMode:
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000885 if (sampler.radial2IsDegenerate()) {
886 stage.fCoordMapping =
887 StageDesc::kRadial2GradientDegenerate_CoordMapping;
888 } else {
889 stage.fCoordMapping =
890 StageDesc::kRadial2Gradient_CoordMapping;
891 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000892 break;
893 case GrSamplerState::kSweep_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000894 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000895 break;
896 default:
897 GrCrash("Unexpected sample mode!");
898 break;
899 }
900
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000901 switch (sampler.getFilter()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000902 // these both can use a regular texture2D()
903 case GrSamplerState::kNearest_Filter:
904 case GrSamplerState::kBilinear_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000905 stage.fFetchMode = StageDesc::kSingle_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000906 break;
907 // performs 4 texture2D()s
908 case GrSamplerState::k4x4Downsample_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000909 stage.fFetchMode = StageDesc::k2x2_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000910 break;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000911 // performs fKernelWidth texture2D()s
912 case GrSamplerState::kConvolution_Filter:
913 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
914 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000915 default:
916 GrCrash("Unexpected filter!");
917 break;
junov@google.comf93e7172011-03-31 21:26:24 +0000918 }
919
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000920 if (sampler.hasTextureDomain()) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000921 GrAssert(GrSamplerState::kClamp_WrapMode ==
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000922 sampler.getWrapX() &&
923 GrSamplerState::kClamp_WrapMode ==
924 sampler.getWrapY());
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000925 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
junov@google.com6acc9b32011-05-16 18:32:07 +0000926 }
927
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000928 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000929 stage.fModulation = StageDesc::kAlpha_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000930 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000931 stage.fModulation = StageDesc::kColor_Modulation;
junov@google.comf93e7172011-03-31 21:26:24 +0000932 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000933 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) {
934 stage.fKernelWidth = sampler.getKernelWidth();
935 } else {
936 stage.fKernelWidth = 0;
937 }
junov@google.comf93e7172011-03-31 21:26:24 +0000938 } else {
939 stage.fOptFlags = 0;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000940 stage.fCoordMapping = (StageDesc::CoordMapping)0;
941 stage.fModulation = (StageDesc::Modulation)0;
junov@google.comf93e7172011-03-31 21:26:24 +0000942 }
943 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000944
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000945 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000946
947 // currently the experimental GS will only work with triangle prims
948 // (and it doesn't do anything other than pass through values from
949 // the VS to the FS anyway).
950#if 0 && GR_GL_EXPERIMENTAL_GS
951 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
952#endif
953
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000954 // use canonical value when coverage/color distinction won't affect
955 // generated code to prevent duplicate programs.
956 desc.fFirstCoverageStage = kNumStages;
957 if (fCurrDrawState.fFirstCoverageStage <= lastEnabledStage) {
958 // color filter is applied between color/coverage computation
959 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
960 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
961 }
962
963 // We could consider cases where the final color is solid (0xff alpha)
964 // and the dst coeff can correctly be set to a non-dualsrc gl value.
965 // (e.g. solid draw, and dst coeff is kZero. It's correct to make
966 // the dst coeff be kISA. Or solid draw with kSA can be tweaked to be
967 // kOne).
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000968 if (this->getCaps().fDualSourceBlendingSupport) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000969 if (kZero_BlendCoeff == fCurrDrawState.fDstBlend) {
970 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000971 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000972 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
973 } else if (kSA_BlendCoeff == fCurrDrawState.fDstBlend) {
974 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
975 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000976 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000977 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
978 } else if (kSC_BlendCoeff == fCurrDrawState.fDstBlend) {
979 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
980 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000981 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000982 desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage;
983 }
984 }
985 }
junov@google.comf93e7172011-03-31 21:26:24 +0000986}