blob: 9018a4ee1c4ff47ac9c26d823410d7746a411382 [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
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000237 if (this->getCaps().fDualSourceBlendingSupport) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000238 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000239 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000240 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000241 }
242
tomhudson@google.com93813632011-10-27 20:21:16 +0000243 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000244 // enable the stage?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000245 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000246 // use separate tex coords?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000247 if (random_bool(&random)) {
248 int t = random_int(&random, GrDrawState::kMaxTexCoords);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000249 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
250 } else {
251 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
252 }
253 }
254 // use text-formatted verts?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000255 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000256 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
257 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000258 StageDesc& stage = pdesc.fStages[s];
bsalomon@google.com74b98712011-11-11 19:46:16 +0000259 stage.fOptFlags = STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
260 stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];
261 stage.fCoordMapping = random_int(&random, StageDesc::kCoordMappingCnt);
262 stage.fFetchMode = random_int(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000263 // convolution shaders don't work with persp tex matrix
264 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) {
265 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
266 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000267 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.com74b98712011-11-11 19:46:16 +0000268 switch (stage.fFetchMode) {
269 case StageDesc::kSingle_FetchMode:
270 stage.fKernelWidth = 0;
271 break;
272 case StageDesc::kConvolution_FetchMode:
273 stage.fKernelWidth = random_int(&random, 2, 8);
274 stage.fInConfigFlags &= ~StageDesc::kMulRGBByAlpha_InConfigFlag;
275 break;
276 case StageDesc::k2x2_FetchMode:
277 stage.fKernelWidth = 0;
278 stage.fInConfigFlags &= ~StageDesc::kMulRGBByAlpha_InConfigFlag;
279 break;
280 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000281 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000282 CachedData cachedData;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000283 if (!program.genProgram(this->glInterface(),
tomhudson@google.com086e5352011-12-08 14:44:10 +0000284 glslGeneration,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000285 &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000286 return false;
287 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000288 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000289 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000290 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000291}
292
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000293namespace {
294GrGLBinding get_binding_in_use(const GrGLInterface* gl) {
295 if (gl->supportsDesktop()) {
296 return kDesktop_GrGLBinding;
297 } else {
298 GrAssert(gl->supportsES2());
299 return kES2_GrGLBinding;
300 }
301}
302}
303
304GrGpuGLShaders::GrGpuGLShaders(const GrGLInterface* gl)
305 : GrGpuGL(gl, get_binding_in_use(gl)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000306
tomhudson@google.com086e5352011-12-08 14:44:10 +0000307 GrGLSLGeneration glslGeneration =
308 GetGLSLGeneration(this->glBinding(), gl);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000309
310 // Enable supported shader-releated caps
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000311 fCaps.fShaderSupport = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000312 fCaps.fSupportPerVertexCoverage = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000313 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000314 fCaps.fDualSourceBlendingSupport =
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000315 this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000316 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000317 fCaps.fShaderDerivativeSupport = true;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000318 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
319 fCaps.fGeometryShaderSupport =
320 this->glVersion() >= GR_GL_VER(3,2) &&
tomhudson@google.com086e5352011-12-08 14:44:10 +0000321 glslGeneration >= k150_GLSLGeneration;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000322 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000323 fCaps.fShaderDerivativeSupport =
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000324 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000325 }
junov@google.comf93e7172011-03-31 21:26:24 +0000326
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000327 GR_GL_GetIntegerv(gl, GR_GL_MAX_VERTEX_ATTRIBS, &fMaxVertexAttribs);
328
junov@google.comf93e7172011-03-31 21:26:24 +0000329 fProgramData = NULL;
tomhudson@google.com086e5352011-12-08 14:44:10 +0000330 fProgramCache = new ProgramCache(gl, glslGeneration);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000331
332#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000333 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000334#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000335}
336
337GrGpuGLShaders::~GrGpuGLShaders() {
338 delete fProgramCache;
339}
340
341const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000342 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000343
344 if (GrGLProgram::kSetAsAttribute ==
345 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000346 return fHWDrawState.fSamplerStates[stage].getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000347 } else {
348 return fProgramData->fTextureMatrices[stage];
349 }
junov@google.comf93e7172011-03-31 21:26:24 +0000350}
351
352void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000353 GrAssert(fProgramData);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000354 if (GrGLProgram::kSetAsAttribute ==
bsalomon@google.com91961302011-05-09 18:39:58 +0000355 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000356 fHWDrawState.fSamplerStates[stage].setMatrix(matrix);
bsalomon@google.com91961302011-05-09 18:39:58 +0000357 } else {
358 fProgramData->fTextureMatrices[stage] = matrix;
359 }
junov@google.comf93e7172011-03-31 21:26:24 +0000360}
361
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000362void GrGpuGLShaders::onResetContext() {
363 INHERITED::onResetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000364
junov@google.comf93e7172011-03-31 21:26:24 +0000365 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000366
367 // Third party GL code may have left vertex attributes enabled. Some GL
368 // implementations (osmesa) may read vetex attributes that are not required
369 // by the current shader. Therefore, we have to ensure that only the
370 // attributes we require for the current draw are enabled or we may cause an
371 // invalid read.
372
373 // Disable all vertex layout bits so that next flush will assume all
374 // optional vertex attributes are disabled.
375 fHWGeometryState.fVertexLayout = 0;
376
377 // We always use the this attribute and assume it is always enabled.
378 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
379 GL_CALL(EnableVertexAttribArray(posAttrIdx));
380 // Disable all other vertex attributes.
381 for (int va = 0; va < fMaxVertexAttribs; ++va) {
382 if (va != posAttrIdx) {
383 GL_CALL(DisableVertexAttribArray(va));
384 }
junov@google.comf93e7172011-03-31 21:26:24 +0000385 }
junov@google.comf93e7172011-03-31 21:26:24 +0000386
387 fHWProgramID = 0;
388}
389
390void GrGpuGLShaders::flushViewMatrix() {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000391 GrAssert(NULL != fCurrDrawState.fRenderTarget);
392 GrMatrix m;
393 m.setAll(
394 GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1,
395 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1,
396 0, 0, GrMatrix::I()[8]);
397 m.setConcat(m, fCurrDrawState.fViewMatrix);
junov@google.comf93e7172011-03-31 21:26:24 +0000398
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000399 // ES doesn't allow you to pass true to the transpose param,
400 // so do our own transpose
401 GrGLfloat mt[] = {
402 GrScalarToFloat(m[GrMatrix::kMScaleX]),
403 GrScalarToFloat(m[GrMatrix::kMSkewY]),
404 GrScalarToFloat(m[GrMatrix::kMPersp0]),
405 GrScalarToFloat(m[GrMatrix::kMSkewX]),
406 GrScalarToFloat(m[GrMatrix::kMScaleY]),
407 GrScalarToFloat(m[GrMatrix::kMPersp1]),
408 GrScalarToFloat(m[GrMatrix::kMTransX]),
409 GrScalarToFloat(m[GrMatrix::kMTransY]),
410 GrScalarToFloat(m[GrMatrix::kMPersp2])
411 };
412
413 if (GrGLProgram::kSetAsAttribute ==
bsalomon@google.com91961302011-05-09 18:39:58 +0000414 fProgramData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000415 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
416 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
417 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
418 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000419 } else {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000420 GrAssert(GrGLProgram::kUnusedUniform !=
421 fProgramData->fUniLocations.fViewMatrixUni);
422 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
423 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000424 }
junov@google.comf93e7172011-03-31 21:26:24 +0000425}
426
junov@google.com6acc9b32011-05-16 18:32:07 +0000427void GrGpuGLShaders::flushTextureDomain(int s) {
428 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
429 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000430 const GrRect &texDom =
431 fCurrDrawState.fSamplerStates[s].getTextureDomain();
junov@google.com6acc9b32011-05-16 18:32:07 +0000432
twiz@google.com76b82742011-06-02 20:30:02 +0000433 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
434 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000435
junov@google.com2f839402011-05-24 15:13:01 +0000436 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000437
junov@google.com2f839402011-05-24 15:13:01 +0000438 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000439 GrScalarToFloat(texDom.left()),
440 GrScalarToFloat(texDom.top()),
441 GrScalarToFloat(texDom.right()),
442 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000443 };
444
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000445 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
junov@google.com2f839402011-05-24 15:13:01 +0000446 GrGLTexture::Orientation orientation = texture->orientation();
447
448 // vertical flip if necessary
449 if (GrGLTexture::kBottomUp_Orientation == orientation) {
450 values[1] = 1.0f - values[1];
451 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000452 // The top and bottom were just flipped, so correct the ordering
453 // of elements so that values = (l, t, r, b).
454 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000455 }
456
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000457 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000458 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000459 }
460}
461
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000462void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000463 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000464 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000465 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000466 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000467 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000468 getHWSamplerMatrix(s) != getSamplerMatrix(s))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000469
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000470 GrAssert(NULL != fCurrDrawState.fTextures[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000471
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000472 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
473
474 GrMatrix m = getSamplerMatrix(s);
475 GrSamplerState::SampleMode mode =
476 fCurrDrawState.fSamplerStates[s].getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000477 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000478
bsalomon@google.com91961302011-05-09 18:39:58 +0000479 // ES doesn't allow you to pass true to the transpose param,
480 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000481 GrGLfloat mt[] = {
482 GrScalarToFloat(m[GrMatrix::kMScaleX]),
483 GrScalarToFloat(m[GrMatrix::kMSkewY]),
484 GrScalarToFloat(m[GrMatrix::kMPersp0]),
485 GrScalarToFloat(m[GrMatrix::kMSkewX]),
486 GrScalarToFloat(m[GrMatrix::kMScaleY]),
487 GrScalarToFloat(m[GrMatrix::kMPersp1]),
488 GrScalarToFloat(m[GrMatrix::kMTransX]),
489 GrScalarToFloat(m[GrMatrix::kMTransY]),
490 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000491 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000492
bsalomon@google.com91961302011-05-09 18:39:58 +0000493 if (GrGLProgram::kSetAsAttribute ==
494 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
495 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000496 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
497 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
498 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000499 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000500 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000501 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000502 recordHWSamplerMatrix(s, getSamplerMatrix(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000503 }
504 }
junov@google.comf93e7172011-03-31 21:26:24 +0000505}
506
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000507void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000508
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000509 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000510 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com91961302011-05-09 18:39:58 +0000511 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000512 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
513 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
514 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000515
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000516 GrScalar centerX1 = sampler.getRadial2CenterX1();
517 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000518
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000519 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000520
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000521 // when were in the degenerate (linear) case the second
522 // value will be INF but the program doesn't read it. (We
523 // use the same 6 uniforms even though we don't need them
524 // all in the linear case just to keep the code complexity
525 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000526 float values[6] = {
527 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000528 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000529 GrScalarToFloat(centerX1),
530 GrScalarToFloat(radius0),
531 GrScalarToFloat(GrMul(radius0, radius0)),
532 sampler.isRadial2PosRoot() ? 1.f : -1.f
533 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000534 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000535 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
536 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
537 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
538 }
539}
540
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000541void GrGpuGLShaders::flushConvolution(int s) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000542 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000543 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
544 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000545 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
546 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000547 }
548 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
549 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000550 GL_CALL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000551 }
552}
553
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000554void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000555 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000556 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000557 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
bsalomon@google.com99621082011-11-15 16:47:16 +0000558 if (texture->width() != fProgramData->fTextureWidth[s] ||
559 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000560
bsalomon@google.com99621082011-11-15 16:47:16 +0000561 float texelSize[] = {1.f / texture->width(),
562 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000563 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000564 fProgramData->fTextureWidth[s] = texture->width();
565 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000566 }
567 }
junov@google.comf93e7172011-03-31 21:26:24 +0000568}
569
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000570void GrGpuGLShaders::flushEdgeAAData() {
571 const int& uni = fProgramData->fUniLocations.fEdgesUni;
572 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000573 int count = fCurrDrawState.fEdgeAANumEdges;
tomhudson@google.com93813632011-10-27 20:21:16 +0000574 GrDrawState::Edge edges[GrDrawState::kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000575 // Flip the edges in Y
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000576 float height =
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000577 static_cast<float>(fCurrDrawState.fRenderTarget->height());
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000578 for (int i = 0; i < count; ++i) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000579 edges[i] = fCurrDrawState.fEdgeAAEdges[i];
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000580 float b = edges[i].fY;
581 edges[i].fY = -b;
582 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000583 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000584 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000585 }
586}
587
Scroggo01b87ec2011-05-11 18:05:38 +0000588static const float ONE_OVER_255 = 1.f / 255.f;
589
590#define GR_COLOR_TO_VEC4(color) {\
591 GrColorUnpackR(color) * ONE_OVER_255,\
592 GrColorUnpackG(color) * ONE_OVER_255,\
593 GrColorUnpackB(color) * ONE_OVER_255,\
594 GrColorUnpackA(color) * ONE_OVER_255 \
595}
596
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000597void GrGpuGLShaders::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000598 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000599 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000600 // color will be specified per-vertex as an attribute
601 // invalidate the const vertex attrib color
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000602 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000603 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000604 switch (desc.fColorInput) {
605 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000606 if (fHWDrawState.fColor != color) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000607 // OpenGL ES only supports the float varities of glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000608 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000609 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
610 c));
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000611 fHWDrawState.fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000612 }
613 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000614 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000615 if (fProgramData->fColor != color) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000616 // OpenGL ES only supports the float varities of glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000617 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000618 GrAssert(GrGLProgram::kUnusedUniform !=
619 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000620 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
621 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000622 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000623 }
624 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000625 case ProgramDesc::kSolidWhite_ColorInput:
626 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000627 break;
628 default:
629 GrCrash("Unknown color type.");
630 }
631 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000632 if (fProgramData->fUniLocations.fColorFilterUni
633 != GrGLProgram::kUnusedUniform
634 && fProgramData->fColorFilterColor
635 != fCurrDrawState.fColorFilterColor) {
636 float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColorFilterColor);
637 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
638 fProgramData->fColorFilterColor = fCurrDrawState.fColorFilterColor;
Scroggo97c88c22011-05-11 14:05:25 +0000639 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000640}
641
642
junov@google.comf93e7172011-03-31 21:26:24 +0000643bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
644 if (!flushGLStateCommon(type)) {
645 return false;
646 }
647
648 if (fDirtyFlags.fRenderTargetChanged) {
649 // our coords are in pixel space and the GL matrices map to NDC
650 // so if the viewport changed, our matrix is now wrong.
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000651 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000652 // we assume all shader matrices may be wrong after viewport changes
653 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000654 }
655
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000656 GrBlendCoeff srcCoeff;
657 GrBlendCoeff dstCoeff;
658 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
659 if (kSkipDraw_BlendOptFlag & blendOpts) {
660 return false;
661 }
662
663 this->buildProgram(type, blendOpts, dstCoeff);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000664 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000665 if (NULL == fProgramData) {
666 GrAssert(!"Failed to create program!");
667 return false;
668 }
junov@google.comf93e7172011-03-31 21:26:24 +0000669
670 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000671 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000672 fHWProgramID = fProgramData->fProgramID;
673 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000674 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
675 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000676
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000677 GrColor color;
678 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
679 color = 0;
680 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
681 color = 0xffffffff;
682 } else {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000683 color = fCurrDrawState.fColor;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000684 }
685 this->flushColor(color);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000686
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000687 GrMatrix* currViewMatrix;
688 if (GrGLProgram::kSetAsAttribute ==
689 fProgramData->fUniLocations.fViewMatrixUni) {
690 currViewMatrix = &fHWDrawState.fViewMatrix;
691 } else {
692 currViewMatrix = &fProgramData->fViewMatrix;
693 }
694
695 if (*currViewMatrix != fCurrDrawState.fViewMatrix) {
696 flushViewMatrix();
697 *currViewMatrix = fCurrDrawState.fViewMatrix;
698 }
junov@google.comf93e7172011-03-31 21:26:24 +0000699
tomhudson@google.com93813632011-10-27 20:21:16 +0000700 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000701 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000702
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000703 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000704
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000705 this->flushConvolution(s);
706
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000707 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000708
709 this->flushTextureDomain(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000710 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000711 this->flushEdgeAAData();
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000712 resetDirtyFlags();
junov@google.comf93e7172011-03-31 21:26:24 +0000713 return true;
714}
715
716void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000717}
718
719void GrGpuGLShaders::setupGeometry(int* startVertex,
720 int* startIndex,
721 int vertexCount,
722 int indexCount) {
723
724 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000725 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000726 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000727 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000728
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000729 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
730 this->getGeomSrc().fVertexLayout,
731 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000732 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000733 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000734 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000735 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000736 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000737 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000738 int oldEdgeOffset;
739
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000740 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
741 fHWGeometryState.fVertexLayout,
742 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000743 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000744 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000745 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000746 bool indexed = NULL != startIndex;
747
748 int extraVertexOffset;
749 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000750 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000751
752 GrGLenum scalarType;
753 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000754 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000755 scalarType = GrGLTextType;
756 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
757 } else {
758 scalarType = GrGLType;
759 texCoordNorm = false;
760 }
761
762 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
763 *startVertex = 0;
764 if (indexed) {
765 *startIndex += extraIndexOffset;
766 }
767
768 // all the Pointers must be set if any of these are true
769 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
770 vertexOffset != fHWGeometryState.fVertexOffset ||
771 newStride != oldStride;
772
773 // position and tex coord offsets change if above conditions are true
774 // or the type/normalization changed based on text vs nontext type coords.
775 bool posAndTexChange = allOffsetsChange ||
776 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
777 (kTextFormat_VertexLayoutBit &
778 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000779 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000780
781 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000782 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000783 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000784 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000785 fHWGeometryState.fVertexOffset = vertexOffset;
786 }
787
tomhudson@google.com93813632011-10-27 20:21:16 +0000788 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000789 if (newTexCoordOffsets[t] > 0) {
790 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000791 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000792 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000793 GL_CALL(EnableVertexAttribArray(idx));
794 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000795 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000796 } else if (posAndTexChange ||
797 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000798 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000799 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000800 }
801 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000802 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000803 }
804 }
805
806 if (newColorOffset > 0) {
807 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000808 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000809 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000810 GL_CALL(EnableVertexAttribArray(idx));
811 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000812 true, newStride, colorOffset));
813 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000814 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000815 true, newStride, colorOffset));
816 }
817 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000818 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000819 }
820
bsalomon@google.coma3108262011-10-10 14:08:47 +0000821 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000822 // bind a single channel, they should all have the same value.
823 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000824 int idx = GrGLProgram::CoverageAttributeIdx();
825 if (oldCoverageOffset <= 0) {
826 GL_CALL(EnableVertexAttribArray(idx));
827 GL_CALL(VertexAttribPointer(idx, 1, GR_GL_UNSIGNED_BYTE,
828 true, newStride, coverageOffset));
829 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
830 GL_CALL(VertexAttribPointer(idx, 1, GR_GL_UNSIGNED_BYTE,
831 true, newStride, coverageOffset));
832 }
833 } else if (oldCoverageOffset > 0) {
834 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
835 }
836
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000837 if (newEdgeOffset > 0) {
838 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
839 int idx = GrGLProgram::EdgeAttributeIdx();
840 if (oldEdgeOffset <= 0) {
841 GL_CALL(EnableVertexAttribArray(idx));
842 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
843 false, newStride, edgeOffset));
844 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
845 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
846 false, newStride, edgeOffset));
847 }
848 } else if (oldEdgeOffset > 0) {
849 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
850 }
851
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000852 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000853 fHWGeometryState.fArrayPtrsDirty = false;
854}
855
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000856void GrGpuGLShaders::buildProgram(GrPrimitiveType type,
857 BlendOptFlags blendOpts,
858 GrBlendCoeff dstCoeff) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000859 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
junov@google.comf93e7172011-03-31 21:26:24 +0000860
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000861 // This should already have been caught
862 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
863
864 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
865
866 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
867 kEmitCoverage_BlendOptFlag));
868
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000869 // The descriptor is used as a cache key. Thus when a field of the
870 // descriptor will not affect program generation (because of the vertex
871 // layout in use or other descriptor field settings) it should be set
872 // to a canonical value to avoid duplicate programs with different keys.
873
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000874 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000875 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000876
877 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
878
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000879 bool requiresAttributeColors =
880 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000881 // fColorInput records how colors are specified for the program. Strip
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000882 // the bit from the layout to avoid false negatives when searching for an
883 // existing program in the cache.
884 desc.fVertexLayout &= ~(kColor_VertexLayoutBit);
885
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000886 desc.fColorFilterXfermode = skipColor ?
887 SkXfermode::kDst_Mode :
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000888 fCurrDrawState.fColorFilterXfermode;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000889
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000890 // no reason to do edge aa or look at per-vertex coverage if coverage is
891 // ignored
892 if (skipCoverage) {
893 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
894 kCoverage_VertexLayoutBit);
895 }
896
897 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
898 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
899 (!requiresAttributeColors &&
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000900 0xffffffff == fCurrDrawState.fColor);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000901 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000902 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000903 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000904 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000905 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000906 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000907 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000908 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000909 }
junov@google.comf93e7172011-03-31 21:26:24 +0000910
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000911 desc.fEdgeAANumEdges = skipCoverage ? 0 : fCurrDrawState.fEdgeAANumEdges;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000912 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 &&
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000913 SkToBool(fCurrDrawState.fFlagBits &
914 kEdgeAAConcave_StateBit);
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000915
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000916 int lastEnabledStage = -1;
917
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000918 if (!skipCoverage && (desc.fVertexLayout &
919 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000920 desc.fVertexEdgeType = fCurrDrawState.fVertexEdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000921 } else {
922 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000923 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000924 }
925
tomhudson@google.com93813632011-10-27 20:21:16 +0000926 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000927 StageDesc& stage = desc.fStages[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000928
tomhudson@google.com0d831722011-06-02 15:37:14 +0000929 stage.fOptFlags = 0;
930 stage.setEnabled(this->isStageEnabled(s));
junov@google.comf93e7172011-03-31 21:26:24 +0000931
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000932 bool skip = s < fCurrDrawState.fFirstCoverageStage ? skipColor :
933 skipCoverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000934
935 if (!skip && stage.isEnabled()) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000936 lastEnabledStage = s;
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000937 GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000938 GrAssert(NULL != texture);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000939 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
junov@google.comf93e7172011-03-31 21:26:24 +0000940 // we matrix to invert when orientation is TopDown, so make sure
941 // we aren't in that case before flagging as identity.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000942 if (TextureMatrixIsIdentity(texture, sampler)) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000943 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000944 } else if (!getSamplerMatrix(s).hasPerspective()) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000945 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
junov@google.comf93e7172011-03-31 21:26:24 +0000946 }
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000947 switch (sampler.getSampleMode()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000948 case GrSamplerState::kNormal_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000949 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000950 break;
951 case GrSamplerState::kRadial_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000952 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000953 break;
954 case GrSamplerState::kRadial2_SampleMode:
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000955 if (sampler.radial2IsDegenerate()) {
956 stage.fCoordMapping =
957 StageDesc::kRadial2GradientDegenerate_CoordMapping;
958 } else {
959 stage.fCoordMapping =
960 StageDesc::kRadial2Gradient_CoordMapping;
961 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000962 break;
963 case GrSamplerState::kSweep_SampleMode:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000964 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000965 break;
966 default:
967 GrCrash("Unexpected sample mode!");
968 break;
969 }
970
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000971 switch (sampler.getFilter()) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000972 // these both can use a regular texture2D()
973 case GrSamplerState::kNearest_Filter:
974 case GrSamplerState::kBilinear_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000975 stage.fFetchMode = StageDesc::kSingle_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000976 break;
977 // performs 4 texture2D()s
978 case GrSamplerState::k4x4Downsample_Filter:
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000979 stage.fFetchMode = StageDesc::k2x2_FetchMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000980 break;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000981 // performs fKernelWidth texture2D()s
982 case GrSamplerState::kConvolution_Filter:
983 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
984 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000985 default:
986 GrCrash("Unexpected filter!");
987 break;
junov@google.comf93e7172011-03-31 21:26:24 +0000988 }
989
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000990 if (sampler.hasTextureDomain()) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000991 GrAssert(GrSamplerState::kClamp_WrapMode ==
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000992 sampler.getWrapX() &&
993 GrSamplerState::kClamp_WrapMode ==
994 sampler.getWrapY());
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000995 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
junov@google.com6acc9b32011-05-16 18:32:07 +0000996 }
997
bsalomon@google.com74b98712011-11-11 19:46:16 +0000998 stage.fInConfigFlags = 0;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000999 if (!this->glCaps().fTextureSwizzleSupport) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001000 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1001 // if we don't have texture swizzle support then
1002 // the shader must do an alpha smear after reading
1003 // the texture
bsalomon@google.com74b98712011-11-11 19:46:16 +00001004 stage.fInConfigFlags |= StageDesc::kSmearAlpha_InConfigFlag;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001005 } else if (sampler.swapsRAndB()) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001006 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001007 }
bsalomon@google.com74b98712011-11-11 19:46:16 +00001008 }
1009 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.com15717922011-11-22 19:57:18 +00001010 stage.fInConfigFlags |= StageDesc::kMulRGBByAlpha_InConfigFlag;
junov@google.comf93e7172011-03-31 21:26:24 +00001011 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001012
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001013 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) {
1014 stage.fKernelWidth = sampler.getKernelWidth();
1015 } else {
1016 stage.fKernelWidth = 0;
1017 }
junov@google.comf93e7172011-03-31 21:26:24 +00001018 } else {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001019 stage.fOptFlags = 0;
1020 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1021 stage.fInConfigFlags = 0;
1022 stage.fFetchMode = (StageDesc::FetchMode) 0;
1023 stage.fKernelWidth = 0;
junov@google.comf93e7172011-03-31 21:26:24 +00001024 }
1025 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001026
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001027 if (GrPixelConfigIsUnpremultiplied(fCurrDrawState.fRenderTarget->config())) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001028 desc.fOutputPM = ProgramDesc::kNo_OutputPM;
1029 } else {
1030 desc.fOutputPM = ProgramDesc::kYes_OutputPM;
1031 }
1032
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001033 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001034
1035 // currently the experimental GS will only work with triangle prims
1036 // (and it doesn't do anything other than pass through values from
1037 // the VS to the FS anyway).
1038#if 0 && GR_GL_EXPERIMENTAL_GS
1039 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1040#endif
1041
bsalomon@google.coma3108262011-10-10 14:08:47 +00001042 // we want to avoid generating programs with different "first cov stage"
1043 // values when they would compute the same result.
1044 // We set field in the desc to kNumStages when either there are no
1045 // coverage stages or the distinction between coverage and color is
1046 // immaterial.
tomhudson@google.com93813632011-10-27 20:21:16 +00001047 int firstCoverageStage = GrDrawState::kNumStages;
1048 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001049 bool hasCoverage = fCurrDrawState.fFirstCoverageStage <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001050 if (hasCoverage) {
bsalomon@google.com3d0835b2011-12-08 16:12:03 +00001051 firstCoverageStage = fCurrDrawState.fFirstCoverageStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001052 }
1053
1054 // other coverage inputs
1055 if (!hasCoverage) {
1056 hasCoverage =
1057 desc.fEdgeAANumEdges ||
1058 (desc.fVertexLayout & GrDrawTarget::kCoverage_VertexLayoutBit) ||
1059 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1060 }
1061
1062 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001063 // color filter is applied between color/coverage computation
1064 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001065 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001066 }
1067
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001068 if (this->getCaps().fDualSourceBlendingSupport &&
1069 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1070 kCoverageAsAlpha_BlendOptFlag))) {
1071 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001072 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001073 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001074 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001075 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001076 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1077 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001078 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001079 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001080 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001081 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1082 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001083 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001084 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001085 }
1086 }
1087 }
junov@google.comf93e7172011-03-31 21:26:24 +00001088}