blob: e9aea2abfa7c3a37377418f29da2f95e9095fa65 [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"
tomhudson@google.com086e5352011-12-08 14:44:10 +000012#include "GrGLSL.h"
junov@google.comf93e7172011-03-31 21:26:24 +000013#include "GrGpuGLShaders.h"
14#include "GrGpuVertex.h"
junov@google.comf93e7172011-03-31 21:26:24 +000015#include "GrNoncopyable.h"
16#include "GrStringBuilder.h"
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000017#include "GrRandom.h"
junov@google.comf93e7172011-03-31 21:26:24 +000018
junov@google.comf93e7172011-03-31 21:26:24 +000019#define SKIP_CACHE_CHECK true
20#define GR_UINT32_MAX static_cast<uint32_t>(-1)
21
junov@google.comf93e7172011-03-31 21:26:24 +000022#include "GrTHashCache.h"
23
24class GrGpuGLShaders::ProgramCache : public ::GrNoncopyable {
25private:
26 class Entry;
27
junov@google.comf7c00f62011-08-18 18:15:16 +000028 typedef GrBinHashKey<Entry, GrGLProgram::kProgramKeySize> ProgramHashKey;
junov@google.comf93e7172011-03-31 21:26:24 +000029
30 class Entry : public ::GrNoncopyable {
31 public:
32 Entry() {}
junov@google.comf93e7172011-03-31 21:26:24 +000033 void copyAndTakeOwnership(Entry& entry) {
34 fProgramData.copyAndTakeOwnership(entry.fProgramData);
junov@google.comf7c00f62011-08-18 18:15:16 +000035 fKey = entry.fKey; // ownership transfer
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000036 fLRUStamp = entry.fLRUStamp;
junov@google.comf93e7172011-03-31 21:26:24 +000037 }
38
39 public:
40 int compare(const ProgramHashKey& key) const { return fKey.compare(key); }
41
42 public:
43 GrGLProgram::CachedData fProgramData;
44 ProgramHashKey fKey;
45 unsigned int fLRUStamp;
46 };
47
48 GrTHashTable<Entry, ProgramHashKey, 8> fHashCache;
49
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000050 // We may have kMaxEntries+1 shaders in the GL context because
51 // we create a new shader before evicting from the cache.
junov@google.comf93e7172011-03-31 21:26:24 +000052 enum {
53 kMaxEntries = 32
54 };
bsalomon@google.com4fa66942011-09-20 19:06:12 +000055 Entry fEntries[kMaxEntries];
56 int fCount;
57 unsigned int fCurrLRUStamp;
58 const GrGLInterface* fGL;
tomhudson@google.com086e5352011-12-08 14:44:10 +000059 GrGLSLGeneration fGLSLGeneration;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000060
junov@google.comf93e7172011-03-31 21:26:24 +000061public:
bsalomon@google.com4fa66942011-09-20 19:06:12 +000062 ProgramCache(const GrGLInterface* gl,
tomhudson@google.com086e5352011-12-08 14:44:10 +000063 GrGLSLGeneration glslGeneration)
junov@google.comf93e7172011-03-31 21:26:24 +000064 : fCount(0)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000065 , fCurrLRUStamp(0)
bsalomon@google.com4fa66942011-09-20 19:06:12 +000066 , fGL(gl)
tomhudson@google.com086e5352011-12-08 14:44:10 +000067 , fGLSLGeneration(glslGeneration) {
junov@google.comf93e7172011-03-31 21:26:24 +000068 }
69
70 ~ProgramCache() {
71 for (int i = 0; i < fCount; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000072 GrGpuGLShaders::DeleteProgram(fGL, &fEntries[i].fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000073 }
74 }
75
76 void abandon() {
77 fCount = 0;
78 }
79
80 void invalidateViewMatrices() {
81 for (int i = 0; i < fCount; ++i) {
82 // set to illegal matrix
83 fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix();
84 }
85 }
86
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000087 GrGLProgram::CachedData* getProgramData(const GrGLProgram& desc) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000088 Entry newEntry;
junov@google.comf7c00f62011-08-18 18:15:16 +000089 newEntry.fKey.setKeyData(desc.keyData());
90
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000091 Entry* entry = fHashCache.find(newEntry.fKey);
junov@google.comf93e7172011-03-31 21:26:24 +000092 if (NULL == entry) {
tomhudson@google.com086e5352011-12-08 14:44:10 +000093 if (!desc.genProgram(fGL, fGLSLGeneration,
94 &newEntry.fProgramData)) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000095 return NULL;
96 }
junov@google.comf93e7172011-03-31 21:26:24 +000097 if (fCount < kMaxEntries) {
98 entry = fEntries + fCount;
99 ++fCount;
100 } else {
101 GrAssert(kMaxEntries == fCount);
102 entry = fEntries;
103 for (int i = 1; i < kMaxEntries; ++i) {
104 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
105 entry = fEntries + i;
106 }
107 }
108 fHashCache.remove(entry->fKey, entry);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000109 GrGpuGLShaders::DeleteProgram(fGL, &entry->fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +0000110 }
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000111 entry->copyAndTakeOwnership(newEntry);
junov@google.comf93e7172011-03-31 21:26:24 +0000112 fHashCache.insert(entry->fKey, entry);
113 }
114
115 entry->fLRUStamp = fCurrLRUStamp;
116 if (GR_UINT32_MAX == fCurrLRUStamp) {
117 // wrap around! just trash our LRU, one time hit.
118 for (int i = 0; i < fCount; ++i) {
119 fEntries[i].fLRUStamp = 0;
120 }
121 }
122 ++fCurrLRUStamp;
123 return &entry->fProgramData;
124 }
125};
126
junov@google.com53a55842011-06-08 22:55:10 +0000127void GrGpuGLShaders::abandonResources(){
128 INHERITED::abandonResources();
129 fProgramCache->abandon();
130}
131
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000132void GrGpuGLShaders::DeleteProgram(const GrGLInterface* gl,
133 CachedData* programData) {
134 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000135 if (programData->fGShaderID) {
136 GR_GL_CALL(gl, DeleteShader(programData->fGShaderID));
137 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000138 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
139 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000140 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
141}
142
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000143////////////////////////////////////////////////////////////////////////////////
144
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000145#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
146
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000147namespace {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000148
bsalomon@google.com74b98712011-11-11 19:46:16 +0000149// GrRandoms nextU() values have patterns in the low bits
150// So using nextU() % array_count might never take some values.
151int random_int(GrRandom* r, int count) {
152 return (int)(r->nextF() * count);
153}
154
155// min is inclusive, max is exclusive
156int random_int(GrRandom* r, int min, int max) {
157 return (int)(r->nextF() * (max-min)) + min;
158}
159
160bool random_bool(GrRandom* r) {
161 return r->nextF() > .5f;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000162}
163
164}
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000165
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000166bool GrGpuGLShaders::programUnitTest() {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000167
tomhudson@google.com086e5352011-12-08 14:44:10 +0000168 GrGLSLGeneration glslGeneration =
169 GetGLSLGeneration(this->glBinding(), this->glInterface());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000170 static const int STAGE_OPTS[] = {
171 0,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000172 StageDesc::kNoPerspective_OptFlagBit,
173 StageDesc::kIdentity_CoordMapping
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000174 };
bsalomon@google.com74b98712011-11-11 19:46:16 +0000175 static const int IN_CONFIG_FLAGS[] = {
176 StageDesc::kNone_InConfigFlag,
177 StageDesc::kSwapRAndB_InConfigFlag,
178 StageDesc::kSwapRAndB_InConfigFlag | StageDesc::kMulRGBByAlpha_InConfigFlag,
179 StageDesc::kMulRGBByAlpha_InConfigFlag,
180 StageDesc::kSmearAlpha_InConfigFlag,
181 };
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000182 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000183 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000184
185 static const int NUM_TESTS = 512;
186
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000187 GrRandom random;
188 for (int t = 0; t < NUM_TESTS; ++t) {
189
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000190#if 0
191 GrPrintf("\nTest Program %d\n-------------\n", t);
192 static const int stop = -1;
193 if (t == stop) {
194 int breakpointhere = 9;
195 }
196#endif
197
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000198 pdesc.fVertexLayout = 0;
199 pdesc.fEmitsPointSize = random.nextF() > .5f;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000200 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000201
bsalomon@google.com74b98712011-11-11 19:46:16 +0000202 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000203
bsalomon@google.com74b98712011-11-11 19:46:16 +0000204 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000205
bsalomon@google.com74b98712011-11-11 19:46:16 +0000206 pdesc.fVertexLayout |= random_bool(&random) ?
bsalomon@google.coma3108262011-10-10 14:08:47 +0000207 GrDrawTarget::kCoverage_VertexLayoutBit :
208 0;
209
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000210#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com6f92f182011-09-29 15:05:48 +0000211 pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport &&
bsalomon@google.com74b98712011-11-11 19:46:16 +0000212 random_bool(&random);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000213#endif
bsalomon@google.com74b98712011-11-11 19:46:16 +0000214 pdesc.fOutputPM = random_int(&random, ProgramDesc::kOutputPMCnt);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000215
bsalomon@google.com74b98712011-11-11 19:46:16 +0000216 bool edgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000217 if (edgeAA) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000218 bool vertexEdgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000219 if (vertexEdgeAA) {
220 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000221 if (this->getCaps().fShaderDerivativeSupport) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000222 pdesc.fVertexEdgeType = random_bool(&random) ?
tomhudson@google.com93813632011-10-27 20:21:16 +0000223 GrDrawState::kHairQuad_EdgeType :
224 GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000225 } else {
tomhudson@google.com93813632011-10-27 20:21:16 +0000226 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000227 }
228 pdesc.fEdgeAANumEdges = 0;
229 } else {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000230 pdesc.fEdgeAANumEdges = random_int(&random, 1, this->getMaxEdges());
231 pdesc.fEdgeAAConcave = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000232 }
233 } else {
234 pdesc.fEdgeAANumEdges = 0;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000235 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000236
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000237 pdesc.fColorMatrixEnabled = random_bool(&random);
238
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000239 if (this->getCaps().fDualSourceBlendingSupport) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000240 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000241 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000242 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000243 }
244
tomhudson@google.com93813632011-10-27 20:21:16 +0000245 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000246 // enable the stage?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000247 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000248 // use separate tex coords?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000249 if (random_bool(&random)) {
250 int t = random_int(&random, GrDrawState::kMaxTexCoords);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000251 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
252 } else {
253 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
254 }
255 }
256 // use text-formatted verts?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000257 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000258 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
259 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000260 StageDesc& stage = pdesc.fStages[s];
bsalomon@google.com74b98712011-11-11 19:46:16 +0000261 stage.fOptFlags = STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
262 stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];
263 stage.fCoordMapping = random_int(&random, StageDesc::kCoordMappingCnt);
264 stage.fFetchMode = random_int(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000265 // convolution shaders don't work with persp tex matrix
266 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) {
267 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
268 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000269 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.com74b98712011-11-11 19:46:16 +0000270 switch (stage.fFetchMode) {
271 case StageDesc::kSingle_FetchMode:
272 stage.fKernelWidth = 0;
273 break;
274 case StageDesc::kConvolution_FetchMode:
275 stage.fKernelWidth = random_int(&random, 2, 8);
276 stage.fInConfigFlags &= ~StageDesc::kMulRGBByAlpha_InConfigFlag;
277 break;
278 case StageDesc::k2x2_FetchMode:
279 stage.fKernelWidth = 0;
280 stage.fInConfigFlags &= ~StageDesc::kMulRGBByAlpha_InConfigFlag;
281 break;
282 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000283 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000284 CachedData cachedData;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000285 if (!program.genProgram(this->glInterface(),
tomhudson@google.com086e5352011-12-08 14:44:10 +0000286 glslGeneration,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000287 &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000288 return false;
289 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000290 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000291 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000292 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000293}
294
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000295namespace {
296GrGLBinding get_binding_in_use(const GrGLInterface* gl) {
297 if (gl->supportsDesktop()) {
298 return kDesktop_GrGLBinding;
299 } else {
300 GrAssert(gl->supportsES2());
301 return kES2_GrGLBinding;
302 }
303}
304}
305
306GrGpuGLShaders::GrGpuGLShaders(const GrGLInterface* gl)
307 : GrGpuGL(gl, get_binding_in_use(gl)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000308
tomhudson@google.com086e5352011-12-08 14:44:10 +0000309 GrGLSLGeneration glslGeneration =
310 GetGLSLGeneration(this->glBinding(), gl);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000311
312 // Enable supported shader-releated caps
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000313 fCaps.fShaderSupport = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000314 fCaps.fSupportPerVertexCoverage = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000315 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000316 fCaps.fDualSourceBlendingSupport =
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000317 this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000318 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000319 fCaps.fShaderDerivativeSupport = true;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000320 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
321 fCaps.fGeometryShaderSupport =
322 this->glVersion() >= GR_GL_VER(3,2) &&
tomhudson@google.com086e5352011-12-08 14:44:10 +0000323 glslGeneration >= k150_GLSLGeneration;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000324 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000325 fCaps.fShaderDerivativeSupport =
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000326 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000327 }
junov@google.comf93e7172011-03-31 21:26:24 +0000328
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000329 GR_GL_GetIntegerv(gl, GR_GL_MAX_VERTEX_ATTRIBS, &fMaxVertexAttribs);
330
junov@google.comf93e7172011-03-31 21:26:24 +0000331 fProgramData = NULL;
tomhudson@google.com086e5352011-12-08 14:44:10 +0000332 fProgramCache = new ProgramCache(gl, glslGeneration);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000333
334#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000335 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000336#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000337}
338
339GrGpuGLShaders::~GrGpuGLShaders() {
340 delete fProgramCache;
341}
342
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000343const GrMatrix& GrGpuGLShaders::getHWViewMatrix() {
344 GrAssert(fProgramData);
345
346 if (GrGLProgram::kSetAsAttribute ==
347 fProgramData->fUniLocations.fViewMatrixUni) {
348 return fHWDrawState.getViewMatrix();
349 } else {
350 return fProgramData->fViewMatrix;
351 }
352}
353
354void GrGpuGLShaders::recordHWViewMatrix(const GrMatrix& matrix) {
355 GrAssert(fProgramData);
356 if (GrGLProgram::kSetAsAttribute ==
357 fProgramData->fUniLocations.fViewMatrixUni) {
358 fHWDrawState.setViewMatrix(matrix);
359 } else {
360 fProgramData->fViewMatrix = matrix;
361 }
362}
363
junov@google.comf93e7172011-03-31 21:26:24 +0000364const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000365 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000366
367 if (GrGLProgram::kSetAsAttribute ==
368 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000369 return fHWDrawState.getSampler(stage).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000370 } else {
371 return fProgramData->fTextureMatrices[stage];
372 }
junov@google.comf93e7172011-03-31 21:26:24 +0000373}
374
375void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000376 GrAssert(fProgramData);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000377 if (GrGLProgram::kSetAsAttribute ==
bsalomon@google.com91961302011-05-09 18:39:58 +0000378 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000379 *fHWDrawState.sampler(stage)->matrix() = matrix;
bsalomon@google.com91961302011-05-09 18:39:58 +0000380 } else {
381 fProgramData->fTextureMatrices[stage] = matrix;
382 }
junov@google.comf93e7172011-03-31 21:26:24 +0000383}
384
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000385void GrGpuGLShaders::onResetContext() {
386 INHERITED::onResetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000387
junov@google.comf93e7172011-03-31 21:26:24 +0000388 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000389
390 // Third party GL code may have left vertex attributes enabled. Some GL
391 // implementations (osmesa) may read vetex attributes that are not required
392 // by the current shader. Therefore, we have to ensure that only the
393 // attributes we require for the current draw are enabled or we may cause an
394 // invalid read.
395
396 // Disable all vertex layout bits so that next flush will assume all
397 // optional vertex attributes are disabled.
398 fHWGeometryState.fVertexLayout = 0;
399
400 // We always use the this attribute and assume it is always enabled.
401 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
402 GL_CALL(EnableVertexAttribArray(posAttrIdx));
403 // Disable all other vertex attributes.
404 for (int va = 0; va < fMaxVertexAttribs; ++va) {
405 if (va != posAttrIdx) {
406 GL_CALL(DisableVertexAttribArray(va));
407 }
junov@google.comf93e7172011-03-31 21:26:24 +0000408 }
junov@google.comf93e7172011-03-31 21:26:24 +0000409
410 fHWProgramID = 0;
411}
412
413void GrGpuGLShaders::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000414 const GrMatrix& vm = this->getDrawState().getViewMatrix();
415 if (GrGpuGLShaders::getHWViewMatrix() != vm) {
junov@google.comf93e7172011-03-31 21:26:24 +0000416
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000417 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
418 GrAssert(NULL != rt);
419 GrMatrix m;
420 m.setAll(
421 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
422 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
423 0, 0, GrMatrix::I()[8]);
424 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000425
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000426 // ES doesn't allow you to pass true to the transpose param,
427 // so do our own transpose
428 GrGLfloat mt[] = {
429 GrScalarToFloat(m[GrMatrix::kMScaleX]),
430 GrScalarToFloat(m[GrMatrix::kMSkewY]),
431 GrScalarToFloat(m[GrMatrix::kMPersp0]),
432 GrScalarToFloat(m[GrMatrix::kMSkewX]),
433 GrScalarToFloat(m[GrMatrix::kMScaleY]),
434 GrScalarToFloat(m[GrMatrix::kMPersp1]),
435 GrScalarToFloat(m[GrMatrix::kMTransX]),
436 GrScalarToFloat(m[GrMatrix::kMTransY]),
437 GrScalarToFloat(m[GrMatrix::kMPersp2])
438 };
439
440 if (GrGLProgram::kSetAsAttribute ==
441 fProgramData->fUniLocations.fViewMatrixUni) {
442 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
443 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
444 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
445 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
446 } else {
447 GrAssert(GrGLProgram::kUnusedUniform !=
448 fProgramData->fUniLocations.fViewMatrixUni);
449 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
450 1, false, mt));
451 }
452 this->recordHWViewMatrix(vm);
bsalomon@google.com91961302011-05-09 18:39:58 +0000453 }
junov@google.comf93e7172011-03-31 21:26:24 +0000454}
455
junov@google.com6acc9b32011-05-16 18:32:07 +0000456void GrGpuGLShaders::flushTextureDomain(int s) {
457 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000458 const GrDrawState& drawState = this->getDrawState();
junov@google.com6acc9b32011-05-16 18:32:07 +0000459 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000460 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
junov@google.com6acc9b32011-05-16 18:32:07 +0000461
twiz@google.com76b82742011-06-02 20:30:02 +0000462 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
463 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000464
junov@google.com2f839402011-05-24 15:13:01 +0000465 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000466
junov@google.com2f839402011-05-24 15:13:01 +0000467 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000468 GrScalarToFloat(texDom.left()),
469 GrScalarToFloat(texDom.top()),
470 GrScalarToFloat(texDom.right()),
471 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000472 };
473
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000474 const GrGLTexture* texture =
475 static_cast<const GrGLTexture*>(drawState.getTexture(s));
junov@google.com2f839402011-05-24 15:13:01 +0000476 GrGLTexture::Orientation orientation = texture->orientation();
477
478 // vertical flip if necessary
479 if (GrGLTexture::kBottomUp_Orientation == orientation) {
480 values[1] = 1.0f - values[1];
481 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000482 // The top and bottom were just flipped, so correct the ordering
483 // of elements so that values = (l, t, r, b).
484 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000485 }
486
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000487 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000488 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000489 }
490}
491
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000492void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000493 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000494 const GrDrawState& drawState = this->getDrawState();
495 const GrGLTexture* texture =
496 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000497 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000498 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000499 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000500 this->getHWSamplerMatrix(s) != drawState.getSampler(s).getMatrix())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000501
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000502 GrMatrix m = drawState.getSampler(s).getMatrix();
503 GrSamplerState::SampleMode mode =
504 drawState.getSampler(s).getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000505 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000506
bsalomon@google.com91961302011-05-09 18:39:58 +0000507 // ES doesn't allow you to pass true to the transpose param,
508 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000509 GrGLfloat mt[] = {
510 GrScalarToFloat(m[GrMatrix::kMScaleX]),
511 GrScalarToFloat(m[GrMatrix::kMSkewY]),
512 GrScalarToFloat(m[GrMatrix::kMPersp0]),
513 GrScalarToFloat(m[GrMatrix::kMSkewX]),
514 GrScalarToFloat(m[GrMatrix::kMScaleY]),
515 GrScalarToFloat(m[GrMatrix::kMPersp1]),
516 GrScalarToFloat(m[GrMatrix::kMTransX]),
517 GrScalarToFloat(m[GrMatrix::kMTransY]),
518 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000519 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000520
bsalomon@google.com91961302011-05-09 18:39:58 +0000521 if (GrGLProgram::kSetAsAttribute ==
522 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
523 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000524 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
525 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
526 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000527 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000528 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000529 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000530 this->recordHWSamplerMatrix(s, drawState.getSampler(s).getMatrix());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000531 }
532 }
junov@google.comf93e7172011-03-31 21:26:24 +0000533}
534
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000535void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000536
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000537 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000538 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.com91961302011-05-09 18:39:58 +0000539 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000540 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
541 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
542 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000543
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000544 GrScalar centerX1 = sampler.getRadial2CenterX1();
545 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000546
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000547 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000548
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000549 // when were in the degenerate (linear) case the second
550 // value will be INF but the program doesn't read it. (We
551 // use the same 6 uniforms even though we don't need them
552 // all in the linear case just to keep the code complexity
553 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000554 float values[6] = {
555 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000556 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000557 GrScalarToFloat(centerX1),
558 GrScalarToFloat(radius0),
559 GrScalarToFloat(GrMul(radius0, radius0)),
560 sampler.isRadial2PosRoot() ? 1.f : -1.f
561 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000562 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000563 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
564 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
565 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
566 }
567}
568
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000569void GrGpuGLShaders::flushConvolution(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000570 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000571 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
572 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000573 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
574 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000575 }
576 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
577 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000578 GL_CALL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000579 }
580}
581
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000582void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000583 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000584 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000585 const GrGLTexture* texture =
586 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000587 if (texture->width() != fProgramData->fTextureWidth[s] ||
588 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000589
bsalomon@google.com99621082011-11-15 16:47:16 +0000590 float texelSize[] = {1.f / texture->width(),
591 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000592 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000593 fProgramData->fTextureWidth[s] = texture->width();
594 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000595 }
596 }
junov@google.comf93e7172011-03-31 21:26:24 +0000597}
598
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000599void GrGpuGLShaders::flushEdgeAAData() {
600 const int& uni = fProgramData->fUniLocations.fEdgesUni;
601 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000602 int count = this->getDrawState().getNumAAEdges();
tomhudson@google.com93813632011-10-27 20:21:16 +0000603 GrDrawState::Edge edges[GrDrawState::kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000604 // Flip the edges in Y
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000605 float height =
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000606 static_cast<float>(this->getDrawState().getRenderTarget()->height());
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000607 for (int i = 0; i < count; ++i) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000608 edges[i] = this->getDrawState().getAAEdges()[i];
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000609 float b = edges[i].fY;
610 edges[i].fY = -b;
611 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000612 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000613 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000614 }
615}
616
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000617void GrGpuGLShaders::flushColorMatrix() {
618 const ProgramDesc& desc = fCurrentProgram.getDesc();
619 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
620 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
621 if (GrGLProgram::kUnusedUniform != matrixUni
622 && GrGLProgram::kUnusedUniform != vecUni) {
623 const float* m = this->getDrawState().getColorMatrix();
624 GrGLfloat mt[] = {
625 m[0], m[5], m[10], m[15],
626 m[1], m[6], m[11], m[16],
627 m[2], m[7], m[12], m[17],
628 m[3], m[8], m[13], m[18],
629 };
630 static float scale = 1.0f / 255.0f;
631 GrGLfloat vec[] = {
632 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
633 };
634 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
635 GL_CALL(Uniform4fv(vecUni, 1, vec));
636 }
637}
638
Scroggo01b87ec2011-05-11 18:05:38 +0000639static const float ONE_OVER_255 = 1.f / 255.f;
640
641#define GR_COLOR_TO_VEC4(color) {\
642 GrColorUnpackR(color) * ONE_OVER_255,\
643 GrColorUnpackG(color) * ONE_OVER_255,\
644 GrColorUnpackB(color) * ONE_OVER_255,\
645 GrColorUnpackA(color) * ONE_OVER_255 \
646}
647
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000648void GrGpuGLShaders::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000649 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000650 const GrDrawState& drawState = this->getDrawState();
651
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000652 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000653 // color will be specified per-vertex as an attribute
654 // invalidate the const vertex attrib color
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000655 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000656 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000657 switch (desc.fColorInput) {
658 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000659 if (fHWDrawState.getColor() != color) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000660 // OpenGL ES only supports the float varities of glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000661 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000662 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
663 c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000664 fHWDrawState.setColor(color);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000665 }
666 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000667 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000668 if (fProgramData->fColor != color) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000669 // OpenGL ES only supports the float varities of glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000670 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000671 GrAssert(GrGLProgram::kUnusedUniform !=
672 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000673 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
674 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000675 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000676 }
677 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000678 case ProgramDesc::kSolidWhite_ColorInput:
679 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000680 break;
681 default:
682 GrCrash("Unknown color type.");
683 }
684 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000685 if (fProgramData->fUniLocations.fColorFilterUni
686 != GrGLProgram::kUnusedUniform
687 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000688 != drawState.getColorFilterColor()) {
689 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000690 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000691 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000692 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000693}
694
695
junov@google.comf93e7172011-03-31 21:26:24 +0000696bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
697 if (!flushGLStateCommon(type)) {
698 return false;
699 }
700
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000701 const GrDrawState& drawState = this->getDrawState();
702
junov@google.comf93e7172011-03-31 21:26:24 +0000703 if (fDirtyFlags.fRenderTargetChanged) {
704 // our coords are in pixel space and the GL matrices map to NDC
705 // so if the viewport changed, our matrix is now wrong.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000706 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
junov@google.comf93e7172011-03-31 21:26:24 +0000707 // we assume all shader matrices may be wrong after viewport changes
708 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000709 }
710
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000711 GrBlendCoeff srcCoeff;
712 GrBlendCoeff dstCoeff;
713 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
714 if (kSkipDraw_BlendOptFlag & blendOpts) {
715 return false;
716 }
717
718 this->buildProgram(type, blendOpts, dstCoeff);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000719 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000720 if (NULL == fProgramData) {
721 GrAssert(!"Failed to create program!");
722 return false;
723 }
junov@google.comf93e7172011-03-31 21:26:24 +0000724
725 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000726 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000727 fHWProgramID = fProgramData->fProgramID;
728 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000729 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
730 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000731
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000732 GrColor color;
733 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
734 color = 0;
735 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
736 color = 0xffffffff;
737 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000738 color = drawState.getColor();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000739 }
740 this->flushColor(color);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000741
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000742 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000743
tomhudson@google.com93813632011-10-27 20:21:16 +0000744 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000745 if (this->isStageEnabled(s)) {
746 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000747
bsalomon@google.com40d92932011-12-13 18:40:47 +0000748 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000749
bsalomon@google.com40d92932011-12-13 18:40:47 +0000750 this->flushConvolution(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000751
bsalomon@google.com40d92932011-12-13 18:40:47 +0000752 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000753
bsalomon@google.com40d92932011-12-13 18:40:47 +0000754 this->flushTextureDomain(s);
755 }
junov@google.comf93e7172011-03-31 21:26:24 +0000756 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000757 this->flushEdgeAAData();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000758 this->flushColorMatrix();
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000759 resetDirtyFlags();
junov@google.comf93e7172011-03-31 21:26:24 +0000760 return true;
761}
762
763void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000764}
765
766void GrGpuGLShaders::setupGeometry(int* startVertex,
767 int* startIndex,
768 int vertexCount,
769 int indexCount) {
770
771 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000772 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000773 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000774 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000775
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000776 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
777 this->getGeomSrc().fVertexLayout,
778 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000779 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000780 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000781 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000782 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000783 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000784 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000785 int oldEdgeOffset;
786
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000787 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
788 fHWGeometryState.fVertexLayout,
789 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000790 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000791 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000792 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000793 bool indexed = NULL != startIndex;
794
795 int extraVertexOffset;
796 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000797 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000798
799 GrGLenum scalarType;
800 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000801 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000802 scalarType = GrGLTextType;
803 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
804 } else {
805 scalarType = GrGLType;
806 texCoordNorm = false;
807 }
808
809 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
810 *startVertex = 0;
811 if (indexed) {
812 *startIndex += extraIndexOffset;
813 }
814
815 // all the Pointers must be set if any of these are true
816 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
817 vertexOffset != fHWGeometryState.fVertexOffset ||
818 newStride != oldStride;
819
820 // position and tex coord offsets change if above conditions are true
821 // or the type/normalization changed based on text vs nontext type coords.
822 bool posAndTexChange = allOffsetsChange ||
823 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
824 (kTextFormat_VertexLayoutBit &
825 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000826 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000827
828 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000829 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000830 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000831 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000832 fHWGeometryState.fVertexOffset = vertexOffset;
833 }
834
tomhudson@google.com93813632011-10-27 20:21:16 +0000835 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000836 if (newTexCoordOffsets[t] > 0) {
837 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000838 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000839 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000840 GL_CALL(EnableVertexAttribArray(idx));
841 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000842 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000843 } else if (posAndTexChange ||
844 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000845 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000846 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000847 }
848 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000849 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000850 }
851 }
852
853 if (newColorOffset > 0) {
854 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000855 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000856 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000857 GL_CALL(EnableVertexAttribArray(idx));
858 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000859 true, newStride, colorOffset));
860 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000861 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000862 true, newStride, colorOffset));
863 }
864 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000865 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000866 }
867
bsalomon@google.coma3108262011-10-10 14:08:47 +0000868 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000869 // bind a single channel, they should all have the same value.
870 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000871 int idx = GrGLProgram::CoverageAttributeIdx();
872 if (oldCoverageOffset <= 0) {
873 GL_CALL(EnableVertexAttribArray(idx));
874 GL_CALL(VertexAttribPointer(idx, 1, GR_GL_UNSIGNED_BYTE,
875 true, newStride, coverageOffset));
876 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
877 GL_CALL(VertexAttribPointer(idx, 1, GR_GL_UNSIGNED_BYTE,
878 true, newStride, coverageOffset));
879 }
880 } else if (oldCoverageOffset > 0) {
881 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
882 }
883
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000884 if (newEdgeOffset > 0) {
885 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
886 int idx = GrGLProgram::EdgeAttributeIdx();
887 if (oldEdgeOffset <= 0) {
888 GL_CALL(EnableVertexAttribArray(idx));
889 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
890 false, newStride, edgeOffset));
891 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
892 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
893 false, newStride, edgeOffset));
894 }
895 } else if (oldEdgeOffset > 0) {
896 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
897 }
898
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000899 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000900 fHWGeometryState.fArrayPtrsDirty = false;
901}
902
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000903void GrGpuGLShaders::buildProgram(GrPrimitiveType type,
904 BlendOptFlags blendOpts,
905 GrBlendCoeff dstCoeff) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000906 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000907 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000908
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000909 // This should already have been caught
910 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
911
912 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
913
914 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
915 kEmitCoverage_BlendOptFlag));
916
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000917 // The descriptor is used as a cache key. Thus when a field of the
918 // descriptor will not affect program generation (because of the vertex
919 // layout in use or other descriptor field settings) it should be set
920 // to a canonical value to avoid duplicate programs with different keys.
921
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000922 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000923 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000924
925 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
926
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000927 bool requiresAttributeColors =
928 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000929 // fColorInput records how colors are specified for the program. Strip
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000930 // the bit from the layout to avoid false negatives when searching for an
931 // existing program in the cache.
932 desc.fVertexLayout &= ~(kColor_VertexLayoutBit);
933
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000934 desc.fColorFilterXfermode = skipColor ?
935 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000936 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000937
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000938 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
939
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000940 // no reason to do edge aa or look at per-vertex coverage if coverage is
941 // ignored
942 if (skipCoverage) {
943 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
944 kCoverage_VertexLayoutBit);
945 }
946
947 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
948 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
949 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000950 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000951 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000952 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000953 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000954 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000955 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000956 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000957 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000958 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000959 }
junov@google.comf93e7172011-03-31 21:26:24 +0000960
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000961 desc.fEdgeAANumEdges = skipCoverage ? 0 : drawState.getNumAAEdges();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000962 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000963 drawState.isConcaveEdgeAAState();
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000964
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000965 int lastEnabledStage = -1;
966
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000967 if (!skipCoverage && (desc.fVertexLayout &
968 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000969 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000970 } else {
971 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000972 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000973 }
974
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000975 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000976 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000977
978 stage.fOptFlags = 0;
979 stage.setEnabled(this->isStageEnabled(s));
980
981 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
982 skipCoverage;
983
984 if (!skip && stage.isEnabled()) {
985 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000986 const GrGLTexture* texture =
987 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000988 GrAssert(NULL != texture);
989 const GrSamplerState& sampler = drawState.getSampler(s);
990 // we matrix to invert when orientation is TopDown, so make sure
991 // we aren't in that case before flagging as identity.
992 if (TextureMatrixIsIdentity(texture, sampler)) {
993 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
994 } else if (!sampler.getMatrix().hasPerspective()) {
995 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
996 }
997 switch (sampler.getSampleMode()) {
998 case GrSamplerState::kNormal_SampleMode:
999 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
1000 break;
1001 case GrSamplerState::kRadial_SampleMode:
1002 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
1003 break;
1004 case GrSamplerState::kRadial2_SampleMode:
1005 if (sampler.radial2IsDegenerate()) {
1006 stage.fCoordMapping =
1007 StageDesc::kRadial2GradientDegenerate_CoordMapping;
1008 } else {
1009 stage.fCoordMapping =
1010 StageDesc::kRadial2Gradient_CoordMapping;
1011 }
1012 break;
1013 case GrSamplerState::kSweep_SampleMode:
1014 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
1015 break;
1016 default:
1017 GrCrash("Unexpected sample mode!");
1018 break;
1019 }
1020
1021 switch (sampler.getFilter()) {
1022 // these both can use a regular texture2D()
1023 case GrSamplerState::kNearest_Filter:
1024 case GrSamplerState::kBilinear_Filter:
1025 stage.fFetchMode = StageDesc::kSingle_FetchMode;
1026 break;
1027 // performs 4 texture2D()s
1028 case GrSamplerState::k4x4Downsample_Filter:
1029 stage.fFetchMode = StageDesc::k2x2_FetchMode;
1030 break;
1031 // performs fKernelWidth texture2D()s
1032 case GrSamplerState::kConvolution_Filter:
1033 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
1034 break;
1035 default:
1036 GrCrash("Unexpected filter!");
1037 break;
1038 }
1039
1040 if (sampler.hasTextureDomain()) {
1041 GrAssert(GrSamplerState::kClamp_WrapMode ==
1042 sampler.getWrapX() &&
1043 GrSamplerState::kClamp_WrapMode ==
1044 sampler.getWrapY());
1045 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
1046 }
1047
1048 stage.fInConfigFlags = 0;
1049 if (!this->glCaps().fTextureSwizzleSupport) {
1050 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1051 // if we don't have texture swizzle support then
1052 // the shader must do an alpha smear after reading
1053 // the texture
1054 stage.fInConfigFlags |= StageDesc::kSmearAlpha_InConfigFlag;
1055 } else if (sampler.swapsRAndB()) {
1056 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
1057 }
1058 }
1059 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
1060 stage.fInConfigFlags |= StageDesc::kMulRGBByAlpha_InConfigFlag;
1061 }
1062
1063 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) {
1064 stage.fKernelWidth = sampler.getKernelWidth();
1065 } else {
1066 stage.fKernelWidth = 0;
1067 }
junov@google.comf93e7172011-03-31 21:26:24 +00001068 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001069 stage.fOptFlags = 0;
1070 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1071 stage.fInConfigFlags = 0;
1072 stage.fFetchMode = (StageDesc::FetchMode) 0;
1073 stage.fKernelWidth = 0;
junov@google.comf93e7172011-03-31 21:26:24 +00001074 }
1075 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001076
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001077 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001078 desc.fOutputPM = ProgramDesc::kNo_OutputPM;
1079 } else {
1080 desc.fOutputPM = ProgramDesc::kYes_OutputPM;
1081 }
1082
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001083 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001084
1085 // currently the experimental GS will only work with triangle prims
1086 // (and it doesn't do anything other than pass through values from
1087 // the VS to the FS anyway).
1088#if 0 && GR_GL_EXPERIMENTAL_GS
1089 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1090#endif
1091
bsalomon@google.coma3108262011-10-10 14:08:47 +00001092 // we want to avoid generating programs with different "first cov stage"
1093 // values when they would compute the same result.
1094 // We set field in the desc to kNumStages when either there are no
1095 // coverage stages or the distinction between coverage and color is
1096 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001097 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +00001098 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001099 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001100 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001101 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +00001102 }
1103
1104 // other coverage inputs
1105 if (!hasCoverage) {
1106 hasCoverage =
1107 desc.fEdgeAANumEdges ||
1108 (desc.fVertexLayout & GrDrawTarget::kCoverage_VertexLayoutBit) ||
1109 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1110 }
1111
1112 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001113 // color filter is applied between color/coverage computation
1114 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001115 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001116 }
1117
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001118 if (this->getCaps().fDualSourceBlendingSupport &&
1119 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1120 kCoverageAsAlpha_BlendOptFlag))) {
1121 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001122 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001123 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001124 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001125 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001126 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1127 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001128 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001129 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001130 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001131 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1132 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001133 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001134 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001135 }
1136 }
1137 }
junov@google.comf93e7172011-03-31 21:26:24 +00001138}