blob: ae5b1725a2292fb0c11981107394e98534129a76 [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.com373a6632011-10-19 20:43:20 +0000152 GrAssert(ver >= GR_GLSL_VER(1,10));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000153 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 {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000158 return GrGLProgram::k110_GLSLVersion;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000159 }
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.com373a6632011-10-19 20:43:20 +0000163 return GrGLProgram::k110_GLSLVersion;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000164 default:
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000165 GrCrash("Unknown GL Binding");
bsalomon@google.com373a6632011-10-19 20:43:20 +0000166 return GrGLProgram::k110_GLSLVersion; // suppress warning
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000167 }
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000168}
169
bsalomon@google.com74b98712011-11-11 19:46:16 +0000170// GrRandoms nextU() values have patterns in the low bits
171// So using nextU() % array_count might never take some values.
172int random_int(GrRandom* r, int count) {
173 return (int)(r->nextF() * count);
174}
175
176// min is inclusive, max is exclusive
177int random_int(GrRandom* r, int min, int max) {
178 return (int)(r->nextF() * (max-min)) + min;
179}
180
181bool random_bool(GrRandom* r) {
182 return r->nextF() > .5f;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000183}
184
185}
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000186
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000187bool GrGpuGLShaders::programUnitTest() {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000188
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000189 GrGLProgram::GLSLVersion glslVersion =
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000190 get_glsl_version(this->glBinding(), this->glInterface());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000191 static const int STAGE_OPTS[] = {
192 0,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000193 StageDesc::kNoPerspective_OptFlagBit,
194 StageDesc::kIdentity_CoordMapping
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000195 };
bsalomon@google.com74b98712011-11-11 19:46:16 +0000196 static const int IN_CONFIG_FLAGS[] = {
197 StageDesc::kNone_InConfigFlag,
198 StageDesc::kSwapRAndB_InConfigFlag,
199 StageDesc::kSwapRAndB_InConfigFlag | StageDesc::kMulRGBByAlpha_InConfigFlag,
200 StageDesc::kMulRGBByAlpha_InConfigFlag,
201 StageDesc::kSmearAlpha_InConfigFlag,
202 };
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000203 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000204 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000205
206 static const int NUM_TESTS = 512;
207
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000208 GrRandom random;
209 for (int t = 0; t < NUM_TESTS; ++t) {
210
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000211#if 0
212 GrPrintf("\nTest Program %d\n-------------\n", t);
213 static const int stop = -1;
214 if (t == stop) {
215 int breakpointhere = 9;
216 }
217#endif
218
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000219 pdesc.fVertexLayout = 0;
220 pdesc.fEmitsPointSize = random.nextF() > .5f;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000221 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000222
bsalomon@google.com74b98712011-11-11 19:46:16 +0000223 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000224
bsalomon@google.com74b98712011-11-11 19:46:16 +0000225 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000226
bsalomon@google.com74b98712011-11-11 19:46:16 +0000227 pdesc.fVertexLayout |= random_bool(&random) ?
bsalomon@google.coma3108262011-10-10 14:08:47 +0000228 GrDrawTarget::kCoverage_VertexLayoutBit :
229 0;
230
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000231#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com6f92f182011-09-29 15:05:48 +0000232 pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport &&
bsalomon@google.com74b98712011-11-11 19:46:16 +0000233 random_bool(&random);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000234#endif
bsalomon@google.com74b98712011-11-11 19:46:16 +0000235 pdesc.fOutputPM = random_int(&random, ProgramDesc::kOutputPMCnt);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000236
bsalomon@google.com74b98712011-11-11 19:46:16 +0000237 bool edgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000238 if (edgeAA) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000239 bool vertexEdgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000240 if (vertexEdgeAA) {
241 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000242 if (this->getCaps().fShaderDerivativeSupport) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000243 pdesc.fVertexEdgeType = random_bool(&random) ?
tomhudson@google.com93813632011-10-27 20:21:16 +0000244 GrDrawState::kHairQuad_EdgeType :
245 GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000246 } else {
tomhudson@google.com93813632011-10-27 20:21:16 +0000247 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000248 }
249 pdesc.fEdgeAANumEdges = 0;
250 } else {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000251 pdesc.fEdgeAANumEdges = random_int(&random, 1, this->getMaxEdges());
252 pdesc.fEdgeAAConcave = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000253 }
254 } else {
255 pdesc.fEdgeAANumEdges = 0;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000256 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000257
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000258 if (this->getCaps().fDualSourceBlendingSupport) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000259 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000260 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000261 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000262 }
263
tomhudson@google.com93813632011-10-27 20:21:16 +0000264 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000265 // enable the stage?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000266 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000267 // use separate tex coords?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000268 if (random_bool(&random)) {
269 int t = random_int(&random, GrDrawState::kMaxTexCoords);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000270 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
271 } else {
272 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
273 }
274 }
275 // use text-formatted verts?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000276 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000277 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
278 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000279 StageDesc& stage = pdesc.fStages[s];
bsalomon@google.com74b98712011-11-11 19:46:16 +0000280 stage.fOptFlags = STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
281 stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];
282 stage.fCoordMapping = random_int(&random, StageDesc::kCoordMappingCnt);
283 stage.fFetchMode = random_int(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000284 // convolution shaders don't work with persp tex matrix
285 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) {
286 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
287 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000288 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.com74b98712011-11-11 19:46:16 +0000289 switch (stage.fFetchMode) {
290 case StageDesc::kSingle_FetchMode:
291 stage.fKernelWidth = 0;
292 break;
293 case StageDesc::kConvolution_FetchMode:
294 stage.fKernelWidth = random_int(&random, 2, 8);
295 stage.fInConfigFlags &= ~StageDesc::kMulRGBByAlpha_InConfigFlag;
296 break;
297 case StageDesc::k2x2_FetchMode:
298 stage.fKernelWidth = 0;
299 stage.fInConfigFlags &= ~StageDesc::kMulRGBByAlpha_InConfigFlag;
300 break;
301 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000302 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000303 CachedData cachedData;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000304 if (!program.genProgram(this->glInterface(),
305 glslVersion,
306 &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000307 return false;
308 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000309 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000310 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000311 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000312}
313
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000314namespace {
315GrGLBinding get_binding_in_use(const GrGLInterface* gl) {
316 if (gl->supportsDesktop()) {
317 return kDesktop_GrGLBinding;
318 } else {
319 GrAssert(gl->supportsES2());
320 return kES2_GrGLBinding;
321 }
322}
323}
324
325GrGpuGLShaders::GrGpuGLShaders(const GrGLInterface* gl)
326 : GrGpuGL(gl, get_binding_in_use(gl)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000327
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000328 GrGLProgram::GLSLVersion glslVersion =
329 get_glsl_version(this->glBinding(), gl);
330
331 // Enable supported shader-releated caps
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000332 fCaps.fShaderSupport = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000333 fCaps.fSupportPerVertexCoverage = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000334 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000335 fCaps.fDualSourceBlendingSupport =
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000336 this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000337 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000338 fCaps.fShaderDerivativeSupport = true;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000339 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
340 fCaps.fGeometryShaderSupport =
341 this->glVersion() >= GR_GL_VER(3,2) &&
342 glslVersion >= GrGLProgram::k150_GLSLVersion;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000343 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000344 fCaps.fShaderDerivativeSupport =
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000345 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000346 }
junov@google.comf93e7172011-03-31 21:26:24 +0000347
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000348 GR_GL_GetIntegerv(gl, GR_GL_MAX_VERTEX_ATTRIBS, &fMaxVertexAttribs);
349
junov@google.comf93e7172011-03-31 21:26:24 +0000350 fProgramData = NULL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000351 fProgramCache = new ProgramCache(gl, glslVersion);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000352
353#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000354 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000355#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000356}
357
358GrGpuGLShaders::~GrGpuGLShaders() {
359 delete fProgramCache;
360}
361
362const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000363 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000364
365 if (GrGLProgram::kSetAsAttribute ==
366 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
367 return fHWDrawState.fSamplerStates[stage].getMatrix();
368 } else {
369 return fProgramData->fTextureMatrices[stage];
370 }
junov@google.comf93e7172011-03-31 21:26:24 +0000371}
372
373void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000374 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000375 if (GrGLProgram::kSetAsAttribute ==
376 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
377 fHWDrawState.fSamplerStates[stage].setMatrix(matrix);
378 } else {
379 fProgramData->fTextureMatrices[stage] = matrix;
380 }
junov@google.comf93e7172011-03-31 21:26:24 +0000381}
382
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000383void GrGpuGLShaders::onResetContext() {
384 INHERITED::onResetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000385
junov@google.comf93e7172011-03-31 21:26:24 +0000386 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000387
388 // Third party GL code may have left vertex attributes enabled. Some GL
389 // implementations (osmesa) may read vetex attributes that are not required
390 // by the current shader. Therefore, we have to ensure that only the
391 // attributes we require for the current draw are enabled or we may cause an
392 // invalid read.
393
394 // Disable all vertex layout bits so that next flush will assume all
395 // optional vertex attributes are disabled.
396 fHWGeometryState.fVertexLayout = 0;
397
398 // We always use the this attribute and assume it is always enabled.
399 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
400 GL_CALL(EnableVertexAttribArray(posAttrIdx));
401 // Disable all other vertex attributes.
402 for (int va = 0; va < fMaxVertexAttribs; ++va) {
403 if (va != posAttrIdx) {
404 GL_CALL(DisableVertexAttribArray(va));
405 }
junov@google.comf93e7172011-03-31 21:26:24 +0000406 }
junov@google.comf93e7172011-03-31 21:26:24 +0000407
408 fHWProgramID = 0;
409}
410
411void GrGpuGLShaders::flushViewMatrix() {
412 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000413 GrMatrix m;
414 m.setAll(
junov@google.comf93e7172011-03-31 21:26:24 +0000415 GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1,
416 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1,
417 0, 0, GrMatrix::I()[8]);
418 m.setConcat(m, fCurrDrawState.fViewMatrix);
419
420 // ES doesn't allow you to pass true to the transpose param,
421 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000422 GrGLfloat mt[] = {
423 GrScalarToFloat(m[GrMatrix::kMScaleX]),
424 GrScalarToFloat(m[GrMatrix::kMSkewY]),
425 GrScalarToFloat(m[GrMatrix::kMPersp0]),
426 GrScalarToFloat(m[GrMatrix::kMSkewX]),
427 GrScalarToFloat(m[GrMatrix::kMScaleY]),
428 GrScalarToFloat(m[GrMatrix::kMPersp1]),
429 GrScalarToFloat(m[GrMatrix::kMTransX]),
430 GrScalarToFloat(m[GrMatrix::kMTransY]),
431 GrScalarToFloat(m[GrMatrix::kMPersp2])
junov@google.comf93e7172011-03-31 21:26:24 +0000432 };
bsalomon@google.com91961302011-05-09 18:39:58 +0000433
434 if (GrGLProgram::kSetAsAttribute ==
435 fProgramData->fUniLocations.fViewMatrixUni) {
436 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000437 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
438 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
439 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000440 } else {
441 GrAssert(GrGLProgram::kUnusedUniform !=
442 fProgramData->fUniLocations.fViewMatrixUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000443 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
444 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000445 }
junov@google.comf93e7172011-03-31 21:26:24 +0000446}
447
junov@google.com6acc9b32011-05-16 18:32:07 +0000448void GrGpuGLShaders::flushTextureDomain(int s) {
449 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
450 if (GrGLProgram::kUnusedUniform != uni) {
twiz@google.com76b82742011-06-02 20:30:02 +0000451 const GrRect &texDom =
junov@google.com6acc9b32011-05-16 18:32:07 +0000452 fCurrDrawState.fSamplerStates[s].getTextureDomain();
453
twiz@google.com76b82742011-06-02 20:30:02 +0000454 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
455 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000456
junov@google.com2f839402011-05-24 15:13:01 +0000457 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000458
junov@google.com2f839402011-05-24 15:13:01 +0000459 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000460 GrScalarToFloat(texDom.left()),
461 GrScalarToFloat(texDom.top()),
462 GrScalarToFloat(texDom.right()),
463 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000464 };
465
466 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
467 GrGLTexture::Orientation orientation = texture->orientation();
468
469 // vertical flip if necessary
470 if (GrGLTexture::kBottomUp_Orientation == orientation) {
471 values[1] = 1.0f - values[1];
472 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000473 // The top and bottom were just flipped, so correct the ordering
474 // of elements so that values = (l, t, r, b).
475 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000476 }
477
478 values[0] *= SkScalarToFloat(texture->contentScaleX());
479 values[2] *= SkScalarToFloat(texture->contentScaleX());
480 values[1] *= SkScalarToFloat(texture->contentScaleY());
481 values[3] *= SkScalarToFloat(texture->contentScaleY());
482
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000483 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000484 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000485 }
486}
487
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000488void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000489 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000490 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
491 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000492 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000493 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
494 getHWSamplerMatrix(s) != getSamplerMatrix(s))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000495
bsalomon@google.com91961302011-05-09 18:39:58 +0000496 GrAssert(NULL != fCurrDrawState.fTextures[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000497
bsalomon@google.com91961302011-05-09 18:39:58 +0000498 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000499
bsalomon@google.com91961302011-05-09 18:39:58 +0000500 GrMatrix m = getSamplerMatrix(s);
501 GrSamplerState::SampleMode mode =
502 fCurrDrawState.fSamplerStates[s].getSampleMode();
503 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000504
bsalomon@google.com91961302011-05-09 18:39:58 +0000505 // ES doesn't allow you to pass true to the transpose param,
506 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000507 GrGLfloat mt[] = {
508 GrScalarToFloat(m[GrMatrix::kMScaleX]),
509 GrScalarToFloat(m[GrMatrix::kMSkewY]),
510 GrScalarToFloat(m[GrMatrix::kMPersp0]),
511 GrScalarToFloat(m[GrMatrix::kMSkewX]),
512 GrScalarToFloat(m[GrMatrix::kMScaleY]),
513 GrScalarToFloat(m[GrMatrix::kMPersp1]),
514 GrScalarToFloat(m[GrMatrix::kMTransX]),
515 GrScalarToFloat(m[GrMatrix::kMTransY]),
516 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000517 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000518
bsalomon@google.com91961302011-05-09 18:39:58 +0000519 if (GrGLProgram::kSetAsAttribute ==
520 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
521 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000522 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
523 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
524 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000525 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000526 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000527 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000528 recordHWSamplerMatrix(s, getSamplerMatrix(s));
529 }
530 }
junov@google.comf93e7172011-03-31 21:26:24 +0000531}
532
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000533void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000534
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000535 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
536 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com91961302011-05-09 18:39:58 +0000537 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000538 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
539 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
540 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000541
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000542 GrScalar centerX1 = sampler.getRadial2CenterX1();
543 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000544
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000545 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000546
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000547 // when were in the degenerate (linear) case the second
548 // value will be INF but the program doesn't read it. (We
549 // use the same 6 uniforms even though we don't need them
550 // all in the linear case just to keep the code complexity
551 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000552 float values[6] = {
553 GrScalarToFloat(a),
554 1 / (2.f * values[0]),
555 GrScalarToFloat(centerX1),
556 GrScalarToFloat(radius0),
557 GrScalarToFloat(GrMul(radius0, radius0)),
558 sampler.isRadial2PosRoot() ? 1.f : -1.f
559 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000560 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000561 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
562 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
563 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
564 }
565}
566
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000567void GrGpuGLShaders::flushConvolution(int s) {
568 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
569 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
570 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000571 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
572 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000573 }
574 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
575 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000576 GL_CALL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000577 }
578}
579
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000580void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000581 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000582 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000583 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000584 if (texture->allocatedWidth() != fProgramData->fTextureWidth[s] ||
585 texture->allocatedHeight() != fProgramData->fTextureWidth[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000586
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000587 float texelSize[] = {1.f / texture->allocatedWidth(),
588 1.f / texture->allocatedHeight()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000589 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000590 }
591 }
junov@google.comf93e7172011-03-31 21:26:24 +0000592}
593
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000594void GrGpuGLShaders::flushEdgeAAData() {
595 const int& uni = fProgramData->fUniLocations.fEdgesUni;
596 if (GrGLProgram::kUnusedUniform != uni) {
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000597 int count = fCurrDrawState.fEdgeAANumEdges;
tomhudson@google.com93813632011-10-27 20:21:16 +0000598 GrDrawState::Edge edges[GrDrawState::kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000599 // Flip the edges in Y
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000600 float height =
601 static_cast<float>(fCurrDrawState.fRenderTarget->height());
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000602 for (int i = 0; i < count; ++i) {
603 edges[i] = fCurrDrawState.fEdgeAAEdges[i];
604 float b = edges[i].fY;
605 edges[i].fY = -b;
606 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000607 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000608 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000609 }
610}
611
Scroggo01b87ec2011-05-11 18:05:38 +0000612static const float ONE_OVER_255 = 1.f / 255.f;
613
614#define GR_COLOR_TO_VEC4(color) {\
615 GrColorUnpackR(color) * ONE_OVER_255,\
616 GrColorUnpackG(color) * ONE_OVER_255,\
617 GrColorUnpackB(color) * ONE_OVER_255,\
618 GrColorUnpackA(color) * ONE_OVER_255 \
619}
620
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000621void GrGpuGLShaders::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000622 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000623 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000624 // color will be specified per-vertex as an attribute
625 // invalidate the const vertex attrib color
626 fHWDrawState.fColor = GrColor_ILLEGAL;
627 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000628 switch (desc.fColorInput) {
629 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000630 if (fHWDrawState.fColor != color) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000631 // OpenGL ES only supports the float varities of glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000632 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000633 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
634 c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000635 fHWDrawState.fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000636 }
637 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000638 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000639 if (fProgramData->fColor != color) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000640 // OpenGL ES only supports the float varities of glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000641 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000642 GrAssert(GrGLProgram::kUnusedUniform !=
643 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000644 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
645 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000646 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000647 }
648 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000649 case ProgramDesc::kSolidWhite_ColorInput:
650 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000651 break;
652 default:
653 GrCrash("Unknown color type.");
654 }
655 }
Scroggo97c88c22011-05-11 14:05:25 +0000656 if (fProgramData->fUniLocations.fColorFilterUni
657 != GrGLProgram::kUnusedUniform
658 && fProgramData->fColorFilterColor
659 != fCurrDrawState.fColorFilterColor) {
Scroggo01b87ec2011-05-11 18:05:38 +0000660 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColorFilterColor);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000661 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
Scroggo97c88c22011-05-11 14:05:25 +0000662 fProgramData->fColorFilterColor = fCurrDrawState.fColorFilterColor;
663 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000664}
665
666
junov@google.comf93e7172011-03-31 21:26:24 +0000667bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
668 if (!flushGLStateCommon(type)) {
669 return false;
670 }
671
672 if (fDirtyFlags.fRenderTargetChanged) {
673 // our coords are in pixel space and the GL matrices map to NDC
674 // so if the viewport changed, our matrix is now wrong.
junov@google.comf93e7172011-03-31 21:26:24 +0000675 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000676 // we assume all shader matrices may be wrong after viewport changes
677 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000678 }
679
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000680 GrBlendCoeff srcCoeff;
681 GrBlendCoeff dstCoeff;
682 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
683 if (kSkipDraw_BlendOptFlag & blendOpts) {
684 return false;
685 }
686
687 this->buildProgram(type, blendOpts, dstCoeff);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000688 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000689 if (NULL == fProgramData) {
690 GrAssert(!"Failed to create program!");
691 return false;
692 }
junov@google.comf93e7172011-03-31 21:26:24 +0000693
694 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000695 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000696 fHWProgramID = fProgramData->fProgramID;
697 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000698 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
699 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000700
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000701 GrColor color;
702 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
703 color = 0;
704 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
705 color = 0xffffffff;
706 } else {
707 color = fCurrDrawState.fColor;
708 }
709 this->flushColor(color);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000710
bsalomon@google.com91961302011-05-09 18:39:58 +0000711 GrMatrix* currViewMatrix;
712 if (GrGLProgram::kSetAsAttribute ==
713 fProgramData->fUniLocations.fViewMatrixUni) {
714 currViewMatrix = &fHWDrawState.fViewMatrix;
715 } else {
716 currViewMatrix = &fProgramData->fViewMatrix;
717 }
junov@google.comf93e7172011-03-31 21:26:24 +0000718
bsalomon@google.com91961302011-05-09 18:39:58 +0000719 if (*currViewMatrix != fCurrDrawState.fViewMatrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000720 flushViewMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000721 *currViewMatrix = fCurrDrawState.fViewMatrix;
junov@google.comf93e7172011-03-31 21:26:24 +0000722 }
723
tomhudson@google.com93813632011-10-27 20:21:16 +0000724 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000725 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000726
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000727 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000728
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000729 this->flushConvolution(s);
730
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000731 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000732
733 this->flushTextureDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000734 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000735 this->flushEdgeAAData();
junov@google.comf93e7172011-03-31 21:26:24 +0000736 resetDirtyFlags();
737 return true;
738}
739
740void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000741}
742
743void GrGpuGLShaders::setupGeometry(int* startVertex,
744 int* startIndex,
745 int vertexCount,
746 int indexCount) {
747
748 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000749 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000750 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000751 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000752
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000753 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
754 this->getGeomSrc().fVertexLayout,
755 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000756 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000757 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000758 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000759 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000760 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000761 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000762 int oldEdgeOffset;
763
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000764 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
765 fHWGeometryState.fVertexLayout,
766 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000767 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000768 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000769 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000770 bool indexed = NULL != startIndex;
771
772 int extraVertexOffset;
773 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000774 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000775
776 GrGLenum scalarType;
777 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000778 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000779 scalarType = GrGLTextType;
780 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
781 } else {
782 scalarType = GrGLType;
783 texCoordNorm = false;
784 }
785
786 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
787 *startVertex = 0;
788 if (indexed) {
789 *startIndex += extraIndexOffset;
790 }
791
792 // all the Pointers must be set if any of these are true
793 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
794 vertexOffset != fHWGeometryState.fVertexOffset ||
795 newStride != oldStride;
796
797 // position and tex coord offsets change if above conditions are true
798 // or the type/normalization changed based on text vs nontext type coords.
799 bool posAndTexChange = allOffsetsChange ||
800 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
801 (kTextFormat_VertexLayoutBit &
802 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000803 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000804
805 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000806 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000807 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000808 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000809 fHWGeometryState.fVertexOffset = vertexOffset;
810 }
811
tomhudson@google.com93813632011-10-27 20:21:16 +0000812 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000813 if (newTexCoordOffsets[t] > 0) {
814 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000815 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000816 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000817 GL_CALL(EnableVertexAttribArray(idx));
818 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000819 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000820 } else if (posAndTexChange ||
821 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000822 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000823 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000824 }
825 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000826 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000827 }
828 }
829
830 if (newColorOffset > 0) {
831 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000832 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000833 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000834 GL_CALL(EnableVertexAttribArray(idx));
835 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000836 true, newStride, colorOffset));
837 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000838 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000839 true, newStride, colorOffset));
840 }
841 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000842 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000843 }
844
bsalomon@google.coma3108262011-10-10 14:08:47 +0000845 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000846 // bind a single channel, they should all have the same value.
847 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000848 int idx = GrGLProgram::CoverageAttributeIdx();
849 if (oldCoverageOffset <= 0) {
850 GL_CALL(EnableVertexAttribArray(idx));
851 GL_CALL(VertexAttribPointer(idx, 1, GR_GL_UNSIGNED_BYTE,
852 true, newStride, coverageOffset));
853 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
854 GL_CALL(VertexAttribPointer(idx, 1, GR_GL_UNSIGNED_BYTE,
855 true, newStride, coverageOffset));
856 }
857 } else if (oldCoverageOffset > 0) {
858 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
859 }
860
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000861 if (newEdgeOffset > 0) {
862 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
863 int idx = GrGLProgram::EdgeAttributeIdx();
864 if (oldEdgeOffset <= 0) {
865 GL_CALL(EnableVertexAttribArray(idx));
866 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
867 false, newStride, edgeOffset));
868 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
869 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
870 false, newStride, edgeOffset));
871 }
872 } else if (oldEdgeOffset > 0) {
873 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
874 }
875
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000876 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000877 fHWGeometryState.fArrayPtrsDirty = false;
878}
879
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000880void GrGpuGLShaders::buildProgram(GrPrimitiveType type,
881 BlendOptFlags blendOpts,
882 GrBlendCoeff dstCoeff) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000883 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
junov@google.comf93e7172011-03-31 21:26:24 +0000884
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000885 // This should already have been caught
886 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
887
888 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
889
890 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
891 kEmitCoverage_BlendOptFlag));
892
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000893 // The descriptor is used as a cache key. Thus when a field of the
894 // descriptor will not affect program generation (because of the vertex
895 // layout in use or other descriptor field settings) it should be set
896 // to a canonical value to avoid duplicate programs with different keys.
897
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000898 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000899 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000900
901 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
902
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000903 bool requiresAttributeColors =
904 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000905 // fColorInput records how colors are specified for the program. Strip
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000906 // the bit from the layout to avoid false negatives when searching for an
907 // existing program in the cache.
908 desc.fVertexLayout &= ~(kColor_VertexLayoutBit);
909
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000910 desc.fColorFilterXfermode = skipColor ?
911 SkXfermode::kDst_Mode :
912 fCurrDrawState.fColorFilterXfermode;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000913
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000914 // no reason to do edge aa or look at per-vertex coverage if coverage is
915 // ignored
916 if (skipCoverage) {
917 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
918 kCoverage_VertexLayoutBit);
919 }
920
921 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
922 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
923 (!requiresAttributeColors &&
924 0xffffffff == fCurrDrawState.fColor);
925 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000926 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000927 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000928 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000929 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000930 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000931 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000932 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000933 }
junov@google.comf93e7172011-03-31 21:26:24 +0000934
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000935 desc.fEdgeAANumEdges = skipCoverage ? 0 : fCurrDrawState.fEdgeAANumEdges;
936 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 &&
937 SkToBool(fCurrDrawState.fFlagBits &
938 kEdgeAAConcave_StateBit);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000939
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000940 int lastEnabledStage = -1;
941
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000942 if (!skipCoverage && (desc.fVertexLayout &
943 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000944 desc.fVertexEdgeType = fCurrDrawState.fVertexEdgeType;
945 } else {
946 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000947 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000948 }
949
tomhudson@google.com93813632011-10-27 20:21:16 +0000950 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000951 StageDesc& stage = desc.fStages[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000952
tomhudson@google.com0d831722011-06-02 15:37:14 +0000953 stage.fOptFlags = 0;
954 stage.setEnabled(this->isStageEnabled(s));
junov@google.comf93e7172011-03-31 21:26:24 +0000955
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000956 bool skip = s < fCurrDrawState.fFirstCoverageStage ? skipColor :
957 skipCoverage;
958
959 if (!skip && stage.isEnabled()) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000960 lastEnabledStage = s;
junov@google.comf93e7172011-03-31 21:26:24 +0000961 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
962 GrAssert(NULL != texture);
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000963 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000964 // we matrix to invert when orientation is TopDown, so make sure
965 // we aren't in that case before flagging as identity.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000966 if (TextureMatrixIsIdentity(texture, sampler)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000967 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000968 } else if (!getSamplerMatrix(s).hasPerspective()) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000969 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000970 }
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000971 switch (sampler.getSampleMode()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000972 case GrSamplerState::kNormal_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000973 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000974 break;
975 case GrSamplerState::kRadial_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000976 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000977 break;
978 case GrSamplerState::kRadial2_SampleMode:
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000979 if (sampler.radial2IsDegenerate()) {
980 stage.fCoordMapping =
981 StageDesc::kRadial2GradientDegenerate_CoordMapping;
982 } else {
983 stage.fCoordMapping =
984 StageDesc::kRadial2Gradient_CoordMapping;
985 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000986 break;
987 case GrSamplerState::kSweep_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000988 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000989 break;
990 default:
991 GrCrash("Unexpected sample mode!");
992 break;
993 }
994
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000995 switch (sampler.getFilter()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000996 // these both can use a regular texture2D()
997 case GrSamplerState::kNearest_Filter:
998 case GrSamplerState::kBilinear_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000999 stage.fFetchMode = StageDesc::kSingle_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001000 break;
1001 // performs 4 texture2D()s
1002 case GrSamplerState::k4x4Downsample_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001003 stage.fFetchMode = StageDesc::k2x2_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001004 break;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001005 // performs fKernelWidth texture2D()s
1006 case GrSamplerState::kConvolution_Filter:
1007 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
1008 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001009 default:
1010 GrCrash("Unexpected filter!");
1011 break;
junov@google.comf93e7172011-03-31 21:26:24 +00001012 }
1013
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001014 if (sampler.hasTextureDomain()) {
tomhudson@google.com0d831722011-06-02 15:37:14 +00001015 GrAssert(GrSamplerState::kClamp_WrapMode ==
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001016 sampler.getWrapX() &&
1017 GrSamplerState::kClamp_WrapMode ==
1018 sampler.getWrapY());
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001019 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
junov@google.com6acc9b32011-05-16 18:32:07 +00001020 }
1021
bsalomon@google.com74b98712011-11-11 19:46:16 +00001022 stage.fInConfigFlags = 0;
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001023 if (!this->glCaps().fTextureSwizzleSupport) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001024 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1025 // if we don't have texture swizzle support then
1026 // the shader must do an alpha smear after reading
1027 // the texture
bsalomon@google.com74b98712011-11-11 19:46:16 +00001028 stage.fInConfigFlags |= StageDesc::kSmearAlpha_InConfigFlag;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001029 } else if (sampler.swapsRAndB()) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001030 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001031 }
bsalomon@google.com74b98712011-11-11 19:46:16 +00001032 }
1033 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
1034 stage.fInConfigFlags = StageDesc::kMulRGBByAlpha_InConfigFlag;
junov@google.comf93e7172011-03-31 21:26:24 +00001035 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001036
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001037 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) {
1038 stage.fKernelWidth = sampler.getKernelWidth();
1039 } else {
1040 stage.fKernelWidth = 0;
1041 }
junov@google.comf93e7172011-03-31 21:26:24 +00001042 } else {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001043 stage.fOptFlags = 0;
1044 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1045 stage.fInConfigFlags = 0;
1046 stage.fFetchMode = (StageDesc::FetchMode) 0;
1047 stage.fKernelWidth = 0;
junov@google.comf93e7172011-03-31 21:26:24 +00001048 }
1049 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001050
bsalomon@google.comc4364992011-11-07 15:54:49 +00001051 if (GrPixelConfigIsUnpremultiplied(fCurrDrawState.fRenderTarget->config())) {
1052 desc.fOutputPM = ProgramDesc::kNo_OutputPM;
1053 } else {
1054 desc.fOutputPM = ProgramDesc::kYes_OutputPM;
1055 }
1056
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001057 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001058
1059 // currently the experimental GS will only work with triangle prims
1060 // (and it doesn't do anything other than pass through values from
1061 // the VS to the FS anyway).
1062#if 0 && GR_GL_EXPERIMENTAL_GS
1063 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1064#endif
1065
bsalomon@google.coma3108262011-10-10 14:08:47 +00001066 // we want to avoid generating programs with different "first cov stage"
1067 // values when they would compute the same result.
1068 // We set field in the desc to kNumStages when either there are no
1069 // coverage stages or the distinction between coverage and color is
1070 // immaterial.
tomhudson@google.com93813632011-10-27 20:21:16 +00001071 int firstCoverageStage = GrDrawState::kNumStages;
1072 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001073 bool hasCoverage = fCurrDrawState.fFirstCoverageStage <= lastEnabledStage;
1074 if (hasCoverage) {
1075 firstCoverageStage = fCurrDrawState.fFirstCoverageStage;
1076 }
1077
1078 // other coverage inputs
1079 if (!hasCoverage) {
1080 hasCoverage =
1081 desc.fEdgeAANumEdges ||
1082 (desc.fVertexLayout & GrDrawTarget::kCoverage_VertexLayoutBit) ||
1083 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1084 }
1085
1086 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001087 // color filter is applied between color/coverage computation
1088 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001089 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001090 }
1091
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001092 if (this->getCaps().fDualSourceBlendingSupport &&
1093 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1094 kCoverageAsAlpha_BlendOptFlag))) {
1095 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001096 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001097 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001098 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001099 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001100 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1101 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001102 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001103 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001104 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001105 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1106 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001107 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001108 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001109 }
1110 }
1111 }
junov@google.comf93e7172011-03-31 21:26:24 +00001112}