blob: 8e7c25fe88179566c5eac917b6a17bd72456aa59 [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:
165 GrCrash("Attempting to get GLSL version in unknown or fixed-"
166 "function GL binding.");
bsalomon@google.com373a6632011-10-19 20:43:20 +0000167 return GrGLProgram::k110_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;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000207 pdesc.fColorInput = static_cast<int>(random.nextF() *
208 ProgramDesc::kColorInputCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000209
210 int idx = (int)(random.nextF() * (SkXfermode::kCoeffModesCnt));
211 pdesc.fColorFilterXfermode = (SkXfermode::Mode)idx;
212
tomhudson@google.com93813632011-10-27 20:21:16 +0000213 idx = (int)(random.nextF() * (GrDrawState::kNumStages + 1));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000214 pdesc.fFirstCoverageStage = idx;
215
bsalomon@google.coma3108262011-10-10 14:08:47 +0000216 pdesc.fVertexLayout |= (random.nextF() > .5f) ?
217 GrDrawTarget::kCoverage_VertexLayoutBit :
218 0;
219
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000220#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com6f92f182011-09-29 15:05:48 +0000221 pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport &&
222 random.nextF() > .5f;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000223#endif
bsalomon@google.comc4364992011-11-07 15:54:49 +0000224 pdesc.fOutputPM = static_cast<int>(random.nextF() *
225 ProgramDesc::kOutputPMCnt);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000226
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000227 bool edgeAA = random.nextF() > .5f;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000228 if (edgeAA) {
229 bool vertexEdgeAA = random.nextF() > .5f;
230 if (vertexEdgeAA) {
231 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000232 if (this->getCaps().fShaderDerivativeSupport) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000233 pdesc.fVertexEdgeType = random.nextF() > 0.5f ?
tomhudson@google.com93813632011-10-27 20:21:16 +0000234 GrDrawState::kHairQuad_EdgeType :
235 GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000236 } else {
tomhudson@google.com93813632011-10-27 20:21:16 +0000237 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000238 }
239 pdesc.fEdgeAANumEdges = 0;
240 } else {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000241 pdesc.fEdgeAANumEdges = static_cast<int>(1 + random.nextF() *
242 this->getMaxEdges());
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000243 pdesc.fEdgeAAConcave = random.nextF() > .5f;
244 }
245 } else {
246 pdesc.fEdgeAANumEdges = 0;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000247 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000248
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000249 if (this->getCaps().fDualSourceBlendingSupport) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000250 pdesc.fDualSrcOutput =
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000251 (ProgramDesc::DualSrcOutput)
252 (int)(random.nextF() * ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000253 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000254 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000255 }
256
tomhudson@google.com93813632011-10-27 20:21:16 +0000257 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000258 // enable the stage?
259 if (random.nextF() > .5f) {
260 // use separate tex coords?
261 if (random.nextF() > .5f) {
tomhudson@google.com93813632011-10-27 20:21:16 +0000262 int t = (int)(random.nextF() * GrDrawState::kMaxTexCoords);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000263 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
264 } else {
265 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
266 }
267 }
268 // use text-formatted verts?
269 if (random.nextF() > .5f) {
270 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
271 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000272 idx = (int)(random.nextF() * GR_ARRAY_COUNT(STAGE_OPTS));
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000273 StageDesc& stage = pdesc.fStages[s];
274 stage.fOptFlags = STAGE_OPTS[idx];
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000275 stage.fInputConfig = random_val(&random, StageDesc::kInputConfigCnt);
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000276 stage.fCoordMapping = random_val(&random, StageDesc::kCoordMappingCnt);
277 stage.fFetchMode = random_val(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000278 // convolution shaders don't work with persp tex matrix
279 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) {
280 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
281 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000282 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000283 stage.fKernelWidth = static_cast<int8_t>(4 * random.nextF() + 2);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000284 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000285 CachedData cachedData;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000286 if (!program.genProgram(this->glInterface(),
287 glslVersion,
288 &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000289 return false;
290 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000291 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000292 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000293 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000294}
295
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000296namespace {
297GrGLBinding get_binding_in_use(const GrGLInterface* gl) {
298 if (gl->supportsDesktop()) {
299 return kDesktop_GrGLBinding;
300 } else {
301 GrAssert(gl->supportsES2());
302 return kES2_GrGLBinding;
303 }
304}
305}
306
307GrGpuGLShaders::GrGpuGLShaders(const GrGLInterface* gl)
308 : GrGpuGL(gl, get_binding_in_use(gl)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000309
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000310 GrGLProgram::GLSLVersion glslVersion =
311 get_glsl_version(this->glBinding(), gl);
312
313 // Enable supported shader-releated caps
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000314 fCaps.fShaderSupport = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000315 fCaps.fSupportPerVertexCoverage = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000316 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000317 fCaps.fDualSourceBlendingSupport =
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000318 this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000319 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000320 fCaps.fShaderDerivativeSupport = true;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000321 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
322 fCaps.fGeometryShaderSupport =
323 this->glVersion() >= GR_GL_VER(3,2) &&
324 glslVersion >= GrGLProgram::k150_GLSLVersion;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000325 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000326 fCaps.fShaderDerivativeSupport =
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000327 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000328 }
junov@google.comf93e7172011-03-31 21:26:24 +0000329
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000330 GR_GL_GetIntegerv(gl, GR_GL_MAX_VERTEX_ATTRIBS, &fMaxVertexAttribs);
331
junov@google.comf93e7172011-03-31 21:26:24 +0000332 fProgramData = NULL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000333 fProgramCache = new ProgramCache(gl, glslVersion);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000334
335#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000336 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000337#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000338}
339
340GrGpuGLShaders::~GrGpuGLShaders() {
341 delete fProgramCache;
342}
343
344const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000345 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000346
347 if (GrGLProgram::kSetAsAttribute ==
348 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
349 return fHWDrawState.fSamplerStates[stage].getMatrix();
350 } else {
351 return fProgramData->fTextureMatrices[stage];
352 }
junov@google.comf93e7172011-03-31 21:26:24 +0000353}
354
355void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000356 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000357 if (GrGLProgram::kSetAsAttribute ==
358 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
359 fHWDrawState.fSamplerStates[stage].setMatrix(matrix);
360 } else {
361 fProgramData->fTextureMatrices[stage] = matrix;
362 }
junov@google.comf93e7172011-03-31 21:26:24 +0000363}
364
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000365void GrGpuGLShaders::onResetContext() {
366 INHERITED::onResetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000367
junov@google.comf93e7172011-03-31 21:26:24 +0000368 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000369
370 // Third party GL code may have left vertex attributes enabled. Some GL
371 // implementations (osmesa) may read vetex attributes that are not required
372 // by the current shader. Therefore, we have to ensure that only the
373 // attributes we require for the current draw are enabled or we may cause an
374 // invalid read.
375
376 // Disable all vertex layout bits so that next flush will assume all
377 // optional vertex attributes are disabled.
378 fHWGeometryState.fVertexLayout = 0;
379
380 // We always use the this attribute and assume it is always enabled.
381 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
382 GL_CALL(EnableVertexAttribArray(posAttrIdx));
383 // Disable all other vertex attributes.
384 for (int va = 0; va < fMaxVertexAttribs; ++va) {
385 if (va != posAttrIdx) {
386 GL_CALL(DisableVertexAttribArray(va));
387 }
junov@google.comf93e7172011-03-31 21:26:24 +0000388 }
junov@google.comf93e7172011-03-31 21:26:24 +0000389
390 fHWProgramID = 0;
391}
392
393void GrGpuGLShaders::flushViewMatrix() {
394 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000395 GrMatrix m;
396 m.setAll(
junov@google.comf93e7172011-03-31 21:26:24 +0000397 GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1,
398 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1,
399 0, 0, GrMatrix::I()[8]);
400 m.setConcat(m, fCurrDrawState.fViewMatrix);
401
402 // ES doesn't allow you to pass true to the transpose param,
403 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000404 GrGLfloat mt[] = {
405 GrScalarToFloat(m[GrMatrix::kMScaleX]),
406 GrScalarToFloat(m[GrMatrix::kMSkewY]),
407 GrScalarToFloat(m[GrMatrix::kMPersp0]),
408 GrScalarToFloat(m[GrMatrix::kMSkewX]),
409 GrScalarToFloat(m[GrMatrix::kMScaleY]),
410 GrScalarToFloat(m[GrMatrix::kMPersp1]),
411 GrScalarToFloat(m[GrMatrix::kMTransX]),
412 GrScalarToFloat(m[GrMatrix::kMTransY]),
413 GrScalarToFloat(m[GrMatrix::kMPersp2])
junov@google.comf93e7172011-03-31 21:26:24 +0000414 };
bsalomon@google.com91961302011-05-09 18:39:58 +0000415
416 if (GrGLProgram::kSetAsAttribute ==
417 fProgramData->fUniLocations.fViewMatrixUni) {
418 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000419 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
420 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
421 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000422 } else {
423 GrAssert(GrGLProgram::kUnusedUniform !=
424 fProgramData->fUniLocations.fViewMatrixUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000425 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
426 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000427 }
junov@google.comf93e7172011-03-31 21:26:24 +0000428}
429
junov@google.com6acc9b32011-05-16 18:32:07 +0000430void GrGpuGLShaders::flushTextureDomain(int s) {
431 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
432 if (GrGLProgram::kUnusedUniform != uni) {
twiz@google.com76b82742011-06-02 20:30:02 +0000433 const GrRect &texDom =
junov@google.com6acc9b32011-05-16 18:32:07 +0000434 fCurrDrawState.fSamplerStates[s].getTextureDomain();
435
twiz@google.com76b82742011-06-02 20:30:02 +0000436 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
437 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000438
junov@google.com2f839402011-05-24 15:13:01 +0000439 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000440
junov@google.com2f839402011-05-24 15:13:01 +0000441 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000442 GrScalarToFloat(texDom.left()),
443 GrScalarToFloat(texDom.top()),
444 GrScalarToFloat(texDom.right()),
445 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000446 };
447
448 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
449 GrGLTexture::Orientation orientation = texture->orientation();
450
451 // vertical flip if necessary
452 if (GrGLTexture::kBottomUp_Orientation == orientation) {
453 values[1] = 1.0f - values[1];
454 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000455 // The top and bottom were just flipped, so correct the ordering
456 // of elements so that values = (l, t, r, b).
457 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000458 }
459
460 values[0] *= SkScalarToFloat(texture->contentScaleX());
461 values[2] *= SkScalarToFloat(texture->contentScaleX());
462 values[1] *= SkScalarToFloat(texture->contentScaleY());
463 values[3] *= SkScalarToFloat(texture->contentScaleY());
464
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000465 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000466 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000467 }
468}
469
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000470void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000471 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000472 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
473 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000474 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000475 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
476 getHWSamplerMatrix(s) != getSamplerMatrix(s))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000477
bsalomon@google.com91961302011-05-09 18:39:58 +0000478 GrAssert(NULL != fCurrDrawState.fTextures[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000479
bsalomon@google.com91961302011-05-09 18:39:58 +0000480 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000481
bsalomon@google.com91961302011-05-09 18:39:58 +0000482 GrMatrix m = getSamplerMatrix(s);
483 GrSamplerState::SampleMode mode =
484 fCurrDrawState.fSamplerStates[s].getSampleMode();
485 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000486
bsalomon@google.com91961302011-05-09 18:39:58 +0000487 // ES doesn't allow you to pass true to the transpose param,
488 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000489 GrGLfloat mt[] = {
490 GrScalarToFloat(m[GrMatrix::kMScaleX]),
491 GrScalarToFloat(m[GrMatrix::kMSkewY]),
492 GrScalarToFloat(m[GrMatrix::kMPersp0]),
493 GrScalarToFloat(m[GrMatrix::kMSkewX]),
494 GrScalarToFloat(m[GrMatrix::kMScaleY]),
495 GrScalarToFloat(m[GrMatrix::kMPersp1]),
496 GrScalarToFloat(m[GrMatrix::kMTransX]),
497 GrScalarToFloat(m[GrMatrix::kMTransY]),
498 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000499 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000500
bsalomon@google.com91961302011-05-09 18:39:58 +0000501 if (GrGLProgram::kSetAsAttribute ==
502 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
503 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000504 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
505 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
506 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000507 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000508 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000509 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000510 recordHWSamplerMatrix(s, getSamplerMatrix(s));
511 }
512 }
junov@google.comf93e7172011-03-31 21:26:24 +0000513}
514
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000515void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000516
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000517 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
518 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com91961302011-05-09 18:39:58 +0000519 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000520 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
521 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
522 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000523
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000524 GrScalar centerX1 = sampler.getRadial2CenterX1();
525 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000526
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000527 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000528
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000529 // when were in the degenerate (linear) case the second
530 // value will be INF but the program doesn't read it. (We
531 // use the same 6 uniforms even though we don't need them
532 // all in the linear case just to keep the code complexity
533 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000534 float values[6] = {
535 GrScalarToFloat(a),
536 1 / (2.f * values[0]),
537 GrScalarToFloat(centerX1),
538 GrScalarToFloat(radius0),
539 GrScalarToFloat(GrMul(radius0, radius0)),
540 sampler.isRadial2PosRoot() ? 1.f : -1.f
541 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000542 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000543 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
544 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
545 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
546 }
547}
548
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000549void GrGpuGLShaders::flushConvolution(int s) {
550 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
551 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
552 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000553 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
554 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000555 }
556 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
557 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000558 GL_CALL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000559 }
560}
561
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000562void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000563 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000564 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000565 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000566 if (texture->allocatedWidth() != fProgramData->fTextureWidth[s] ||
567 texture->allocatedHeight() != fProgramData->fTextureWidth[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000568
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000569 float texelSize[] = {1.f / texture->allocatedWidth(),
570 1.f / texture->allocatedHeight()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000571 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000572 }
573 }
junov@google.comf93e7172011-03-31 21:26:24 +0000574}
575
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000576void GrGpuGLShaders::flushEdgeAAData() {
577 const int& uni = fProgramData->fUniLocations.fEdgesUni;
578 if (GrGLProgram::kUnusedUniform != uni) {
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000579 int count = fCurrDrawState.fEdgeAANumEdges;
tomhudson@google.com93813632011-10-27 20:21:16 +0000580 GrDrawState::Edge edges[GrDrawState::kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000581 // Flip the edges in Y
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000582 float height =
583 static_cast<float>(fCurrDrawState.fRenderTarget->height());
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000584 for (int i = 0; i < count; ++i) {
585 edges[i] = fCurrDrawState.fEdgeAAEdges[i];
586 float b = edges[i].fY;
587 edges[i].fY = -b;
588 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000589 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000590 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000591 }
592}
593
Scroggo01b87ec2011-05-11 18:05:38 +0000594static const float ONE_OVER_255 = 1.f / 255.f;
595
596#define GR_COLOR_TO_VEC4(color) {\
597 GrColorUnpackR(color) * ONE_OVER_255,\
598 GrColorUnpackG(color) * ONE_OVER_255,\
599 GrColorUnpackB(color) * ONE_OVER_255,\
600 GrColorUnpackA(color) * ONE_OVER_255 \
601}
602
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000603void GrGpuGLShaders::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000604 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000605 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000606 // color will be specified per-vertex as an attribute
607 // invalidate the const vertex attrib color
608 fHWDrawState.fColor = GrColor_ILLEGAL;
609 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000610 switch (desc.fColorInput) {
611 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000612 if (fHWDrawState.fColor != color) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000613 // OpenGL ES only supports the float varities of glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000614 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000615 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
616 c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000617 fHWDrawState.fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000618 }
619 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000620 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000621 if (fProgramData->fColor != color) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000622 // OpenGL ES only supports the float varities of glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000623 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000624 GrAssert(GrGLProgram::kUnusedUniform !=
625 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000626 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
627 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000628 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000629 }
630 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000631 case ProgramDesc::kSolidWhite_ColorInput:
632 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000633 break;
634 default:
635 GrCrash("Unknown color type.");
636 }
637 }
Scroggo97c88c22011-05-11 14:05:25 +0000638 if (fProgramData->fUniLocations.fColorFilterUni
639 != GrGLProgram::kUnusedUniform
640 && fProgramData->fColorFilterColor
641 != fCurrDrawState.fColorFilterColor) {
Scroggo01b87ec2011-05-11 18:05:38 +0000642 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColorFilterColor);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000643 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
Scroggo97c88c22011-05-11 14:05:25 +0000644 fProgramData->fColorFilterColor = fCurrDrawState.fColorFilterColor;
645 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000646}
647
648
junov@google.comf93e7172011-03-31 21:26:24 +0000649bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
650 if (!flushGLStateCommon(type)) {
651 return false;
652 }
653
654 if (fDirtyFlags.fRenderTargetChanged) {
655 // our coords are in pixel space and the GL matrices map to NDC
656 // so if the viewport changed, our matrix is now wrong.
junov@google.comf93e7172011-03-31 21:26:24 +0000657 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000658 // we assume all shader matrices may be wrong after viewport changes
659 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000660 }
661
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000662 GrBlendCoeff srcCoeff;
663 GrBlendCoeff dstCoeff;
664 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
665 if (kSkipDraw_BlendOptFlag & blendOpts) {
666 return false;
667 }
668
669 this->buildProgram(type, blendOpts, dstCoeff);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000670 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000671 if (NULL == fProgramData) {
672 GrAssert(!"Failed to create program!");
673 return false;
674 }
junov@google.comf93e7172011-03-31 21:26:24 +0000675
676 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000677 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000678 fHWProgramID = fProgramData->fProgramID;
679 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000680 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
681 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000682
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000683 GrColor color;
684 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
685 color = 0;
686 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
687 color = 0xffffffff;
688 } else {
689 color = fCurrDrawState.fColor;
690 }
691 this->flushColor(color);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000692
bsalomon@google.com91961302011-05-09 18:39:58 +0000693 GrMatrix* currViewMatrix;
694 if (GrGLProgram::kSetAsAttribute ==
695 fProgramData->fUniLocations.fViewMatrixUni) {
696 currViewMatrix = &fHWDrawState.fViewMatrix;
697 } else {
698 currViewMatrix = &fProgramData->fViewMatrix;
699 }
junov@google.comf93e7172011-03-31 21:26:24 +0000700
bsalomon@google.com91961302011-05-09 18:39:58 +0000701 if (*currViewMatrix != fCurrDrawState.fViewMatrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000702 flushViewMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000703 *currViewMatrix = fCurrDrawState.fViewMatrix;
junov@google.comf93e7172011-03-31 21:26:24 +0000704 }
705
tomhudson@google.com93813632011-10-27 20:21:16 +0000706 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000707 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000708
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000709 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000710
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000711 this->flushConvolution(s);
712
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000713 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000714
715 this->flushTextureDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000716 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000717 this->flushEdgeAAData();
junov@google.comf93e7172011-03-31 21:26:24 +0000718 resetDirtyFlags();
719 return true;
720}
721
722void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000723}
724
725void GrGpuGLShaders::setupGeometry(int* startVertex,
726 int* startIndex,
727 int vertexCount,
728 int indexCount) {
729
730 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000731 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000732 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000733 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000734
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000735 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
736 this->getGeomSrc().fVertexLayout,
737 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000738 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000739 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000740 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000741 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000742 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000743 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000744 int oldEdgeOffset;
745
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000746 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
747 fHWGeometryState.fVertexLayout,
748 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000749 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000750 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000751 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000752 bool indexed = NULL != startIndex;
753
754 int extraVertexOffset;
755 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000756 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000757
758 GrGLenum scalarType;
759 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000760 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000761 scalarType = GrGLTextType;
762 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
763 } else {
764 scalarType = GrGLType;
765 texCoordNorm = false;
766 }
767
768 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
769 *startVertex = 0;
770 if (indexed) {
771 *startIndex += extraIndexOffset;
772 }
773
774 // all the Pointers must be set if any of these are true
775 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
776 vertexOffset != fHWGeometryState.fVertexOffset ||
777 newStride != oldStride;
778
779 // position and tex coord offsets change if above conditions are true
780 // or the type/normalization changed based on text vs nontext type coords.
781 bool posAndTexChange = allOffsetsChange ||
782 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
783 (kTextFormat_VertexLayoutBit &
784 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000785 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000786
787 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000788 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000789 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000790 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000791 fHWGeometryState.fVertexOffset = vertexOffset;
792 }
793
tomhudson@google.com93813632011-10-27 20:21:16 +0000794 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000795 if (newTexCoordOffsets[t] > 0) {
796 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000797 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000798 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000799 GL_CALL(EnableVertexAttribArray(idx));
800 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000801 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000802 } else if (posAndTexChange ||
803 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000804 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000805 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000806 }
807 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000808 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000809 }
810 }
811
812 if (newColorOffset > 0) {
813 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000814 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000815 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000816 GL_CALL(EnableVertexAttribArray(idx));
817 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000818 true, newStride, colorOffset));
819 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000820 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000821 true, newStride, colorOffset));
822 }
823 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000824 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000825 }
826
bsalomon@google.coma3108262011-10-10 14:08:47 +0000827 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000828 // bind a single channel, they should all have the same value.
829 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000830 int idx = GrGLProgram::CoverageAttributeIdx();
831 if (oldCoverageOffset <= 0) {
832 GL_CALL(EnableVertexAttribArray(idx));
833 GL_CALL(VertexAttribPointer(idx, 1, GR_GL_UNSIGNED_BYTE,
834 true, newStride, coverageOffset));
835 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
836 GL_CALL(VertexAttribPointer(idx, 1, GR_GL_UNSIGNED_BYTE,
837 true, newStride, coverageOffset));
838 }
839 } else if (oldCoverageOffset > 0) {
840 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
841 }
842
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000843 if (newEdgeOffset > 0) {
844 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
845 int idx = GrGLProgram::EdgeAttributeIdx();
846 if (oldEdgeOffset <= 0) {
847 GL_CALL(EnableVertexAttribArray(idx));
848 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
849 false, newStride, edgeOffset));
850 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
851 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
852 false, newStride, edgeOffset));
853 }
854 } else if (oldEdgeOffset > 0) {
855 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
856 }
857
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000858 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000859 fHWGeometryState.fArrayPtrsDirty = false;
860}
861
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000862void GrGpuGLShaders::buildProgram(GrPrimitiveType type,
863 BlendOptFlags blendOpts,
864 GrBlendCoeff dstCoeff) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000865 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
junov@google.comf93e7172011-03-31 21:26:24 +0000866
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000867 // This should already have been caught
868 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
869
870 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
871
872 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
873 kEmitCoverage_BlendOptFlag));
874
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000875 // The descriptor is used as a cache key. Thus when a field of the
876 // descriptor will not affect program generation (because of the vertex
877 // layout in use or other descriptor field settings) it should be set
878 // to a canonical value to avoid duplicate programs with different keys.
879
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000880 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000881 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000882
883 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
884
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000885 bool requiresAttributeColors =
886 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000887 // fColorInput records how colors are specified for the program. Strip
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000888 // the bit from the layout to avoid false negatives when searching for an
889 // existing program in the cache.
890 desc.fVertexLayout &= ~(kColor_VertexLayoutBit);
891
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000892 desc.fColorFilterXfermode = skipColor ?
893 SkXfermode::kDst_Mode :
894 fCurrDrawState.fColorFilterXfermode;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000895
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000896 // no reason to do edge aa or look at per-vertex coverage if coverage is
897 // ignored
898 if (skipCoverage) {
899 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
900 kCoverage_VertexLayoutBit);
901 }
902
903 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
904 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
905 (!requiresAttributeColors &&
906 0xffffffff == fCurrDrawState.fColor);
907 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000908 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000909 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000910 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000911 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000912 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000913 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000914 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000915 }
junov@google.comf93e7172011-03-31 21:26:24 +0000916
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000917 desc.fEdgeAANumEdges = skipCoverage ? 0 : fCurrDrawState.fEdgeAANumEdges;
918 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 &&
919 SkToBool(fCurrDrawState.fFlagBits &
920 kEdgeAAConcave_StateBit);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000921
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000922 int lastEnabledStage = -1;
923
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000924 if (!skipCoverage && (desc.fVertexLayout &
925 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000926 desc.fVertexEdgeType = fCurrDrawState.fVertexEdgeType;
927 } else {
928 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000929 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000930 }
931
tomhudson@google.com93813632011-10-27 20:21:16 +0000932 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000933 StageDesc& stage = desc.fStages[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000934
tomhudson@google.com0d831722011-06-02 15:37:14 +0000935 stage.fOptFlags = 0;
936 stage.setEnabled(this->isStageEnabled(s));
junov@google.comf93e7172011-03-31 21:26:24 +0000937
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000938 bool skip = s < fCurrDrawState.fFirstCoverageStage ? skipColor :
939 skipCoverage;
940
941 if (!skip && stage.isEnabled()) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000942 lastEnabledStage = s;
junov@google.comf93e7172011-03-31 21:26:24 +0000943 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
944 GrAssert(NULL != texture);
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000945 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000946 // we matrix to invert when orientation is TopDown, so make sure
947 // we aren't in that case before flagging as identity.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000948 if (TextureMatrixIsIdentity(texture, sampler)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000949 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000950 } else if (!getSamplerMatrix(s).hasPerspective()) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000951 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000952 }
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000953 switch (sampler.getSampleMode()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000954 case GrSamplerState::kNormal_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000955 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000956 break;
957 case GrSamplerState::kRadial_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000958 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000959 break;
960 case GrSamplerState::kRadial2_SampleMode:
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000961 if (sampler.radial2IsDegenerate()) {
962 stage.fCoordMapping =
963 StageDesc::kRadial2GradientDegenerate_CoordMapping;
964 } else {
965 stage.fCoordMapping =
966 StageDesc::kRadial2Gradient_CoordMapping;
967 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000968 break;
969 case GrSamplerState::kSweep_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000970 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000971 break;
972 default:
973 GrCrash("Unexpected sample mode!");
974 break;
975 }
976
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000977 switch (sampler.getFilter()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000978 // these both can use a regular texture2D()
979 case GrSamplerState::kNearest_Filter:
980 case GrSamplerState::kBilinear_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000981 stage.fFetchMode = StageDesc::kSingle_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000982 break;
983 // performs 4 texture2D()s
984 case GrSamplerState::k4x4Downsample_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000985 stage.fFetchMode = StageDesc::k2x2_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000986 break;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000987 // performs fKernelWidth texture2D()s
988 case GrSamplerState::kConvolution_Filter:
989 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
990 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000991 default:
992 GrCrash("Unexpected filter!");
993 break;
junov@google.comf93e7172011-03-31 21:26:24 +0000994 }
995
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000996 if (sampler.hasTextureDomain()) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000997 GrAssert(GrSamplerState::kClamp_WrapMode ==
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000998 sampler.getWrapX() &&
999 GrSamplerState::kClamp_WrapMode ==
1000 sampler.getWrapY());
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001001 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
junov@google.com6acc9b32011-05-16 18:32:07 +00001002 }
1003
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001004 if (!this->glCaps().fTextureSwizzle &&
1005 GrPixelConfigIsAlphaOnly(texture->config())) {
1006 // if we don't have texture swizzle support then
1007 // the shader must do an alpha smear after reading
1008 // the texture
1009 stage.fInputConfig = StageDesc::kAlphaOnly_InputConfig;
junov@google.comf93e7172011-03-31 21:26:24 +00001010 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001011 stage.fInputConfig = StageDesc::kColor_InputConfig;
junov@google.comf93e7172011-03-31 21:26:24 +00001012 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001013
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001014 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) {
1015 stage.fKernelWidth = sampler.getKernelWidth();
1016 } else {
1017 stage.fKernelWidth = 0;
1018 }
junov@google.comf93e7172011-03-31 21:26:24 +00001019 } else {
1020 stage.fOptFlags = 0;
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001021 stage.fCoordMapping = (StageDesc::CoordMapping)0;
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001022 stage.fInputConfig = (StageDesc::InputConfig)0;
1023 stage.fFetchMode = (StageDesc::FetchMode) 0;
1024 stage.fKernelWidth = 0;
junov@google.comf93e7172011-03-31 21:26:24 +00001025 }
1026 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001027
bsalomon@google.comc4364992011-11-07 15:54:49 +00001028 if (GrPixelConfigIsUnpremultiplied(fCurrDrawState.fRenderTarget->config())) {
1029 desc.fOutputPM = ProgramDesc::kNo_OutputPM;
1030 } else {
1031 desc.fOutputPM = ProgramDesc::kYes_OutputPM;
1032 }
1033
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001034 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001035
1036 // currently the experimental GS will only work with triangle prims
1037 // (and it doesn't do anything other than pass through values from
1038 // the VS to the FS anyway).
1039#if 0 && GR_GL_EXPERIMENTAL_GS
1040 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1041#endif
1042
bsalomon@google.coma3108262011-10-10 14:08:47 +00001043 // we want to avoid generating programs with different "first cov stage"
1044 // values when they would compute the same result.
1045 // We set field in the desc to kNumStages when either there are no
1046 // coverage stages or the distinction between coverage and color is
1047 // immaterial.
tomhudson@google.com93813632011-10-27 20:21:16 +00001048 int firstCoverageStage = GrDrawState::kNumStages;
1049 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001050 bool hasCoverage = fCurrDrawState.fFirstCoverageStage <= lastEnabledStage;
1051 if (hasCoverage) {
1052 firstCoverageStage = fCurrDrawState.fFirstCoverageStage;
1053 }
1054
1055 // other coverage inputs
1056 if (!hasCoverage) {
1057 hasCoverage =
1058 desc.fEdgeAANumEdges ||
1059 (desc.fVertexLayout & GrDrawTarget::kCoverage_VertexLayoutBit) ||
1060 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1061 }
1062
1063 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001064 // color filter is applied between color/coverage computation
1065 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001066 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001067 }
1068
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001069 if (this->getCaps().fDualSourceBlendingSupport &&
1070 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1071 kCoverageAsAlpha_BlendOptFlag))) {
1072 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001073 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001074 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001075 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001076 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001077 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1078 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001079 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001080 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001081 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001082 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1083 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001084 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001085 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001086 }
1087 }
1088 }
junov@google.comf93e7172011-03-31 21:26:24 +00001089}