blob: 968b893349108f7455f99d8892489606e42abf88 [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
tomhudson@google.comdd182cb2012-02-10 21:01:00 +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"
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000014#include "../GrGpuVertex.h"
junov@google.comf93e7172011-03-31 21:26:24 +000015#include "GrNoncopyable.h"
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000016#include "../GrStringBuilder.h"
17#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
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000022#include "../GrTHashCache.h"
junov@google.comf93e7172011-03-31 21:26:24 +000023
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 =
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000169 GrGetGLSLGeneration(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.com2401ae82012-01-17 21:03:05 +0000201 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000202
bsalomon@google.com74b98712011-11-11 19:46:16 +0000203 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000204
bsalomon@google.com74b98712011-11-11 19:46:16 +0000205 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000206
bsalomon@google.com74b98712011-11-11 19:46:16 +0000207 pdesc.fVertexLayout |= random_bool(&random) ?
bsalomon@google.coma3108262011-10-10 14:08:47 +0000208 GrDrawTarget::kCoverage_VertexLayoutBit :
209 0;
210
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000211#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com6f92f182011-09-29 15:05:48 +0000212 pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport &&
bsalomon@google.com74b98712011-11-11 19:46:16 +0000213 random_bool(&random);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000214#endif
bsalomon@google.com74b98712011-11-11 19:46:16 +0000215 pdesc.fOutputPM = random_int(&random, ProgramDesc::kOutputPMCnt);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000216
bsalomon@google.com74b98712011-11-11 19:46:16 +0000217 bool edgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000218 if (edgeAA) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000219 bool vertexEdgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000220 if (vertexEdgeAA) {
221 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000222 if (this->getCaps().fShaderDerivativeSupport) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000223 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000224 } else {
tomhudson@google.com93813632011-10-27 20:21:16 +0000225 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000226 }
227 pdesc.fEdgeAANumEdges = 0;
228 } else {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000229 pdesc.fEdgeAANumEdges = random_int(&random, 1, this->getMaxEdges());
230 pdesc.fEdgeAAConcave = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000231 }
232 } else {
233 pdesc.fEdgeAANumEdges = 0;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000234 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000235
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000236 pdesc.fColorMatrixEnabled = random_bool(&random);
237
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000238 if (this->getCaps().fDualSourceBlendingSupport) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000239 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000240 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000241 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000242 }
243
tomhudson@google.com93813632011-10-27 20:21:16 +0000244 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000245 // enable the stage?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000246 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000247 // use separate tex coords?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000248 if (random_bool(&random)) {
249 int t = random_int(&random, GrDrawState::kMaxTexCoords);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000250 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
251 } else {
252 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
253 }
254 }
255 // use text-formatted verts?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000256 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000257 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
258 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000259 StageDesc& stage = pdesc.fStages[s];
bsalomon@google.com74b98712011-11-11 19:46:16 +0000260 stage.fOptFlags = STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
261 stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];
262 stage.fCoordMapping = random_int(&random, StageDesc::kCoordMappingCnt);
263 stage.fFetchMode = random_int(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000264 // convolution shaders don't work with persp tex matrix
265 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) {
266 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
267 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000268 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.com74b98712011-11-11 19:46:16 +0000269 switch (stage.fFetchMode) {
270 case StageDesc::kSingle_FetchMode:
271 stage.fKernelWidth = 0;
272 break;
273 case StageDesc::kConvolution_FetchMode:
274 stage.fKernelWidth = random_int(&random, 2, 8);
275 stage.fInConfigFlags &= ~StageDesc::kMulRGBByAlpha_InConfigFlag;
276 break;
277 case StageDesc::k2x2_FetchMode:
278 stage.fKernelWidth = 0;
279 stage.fInConfigFlags &= ~StageDesc::kMulRGBByAlpha_InConfigFlag;
280 break;
281 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000282 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000283 CachedData cachedData;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000284 if (!program.genProgram(this->glInterface(),
tomhudson@google.com086e5352011-12-08 14:44:10 +0000285 glslGeneration,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000286 &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000287 return false;
288 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000289 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000290 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000291 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000292}
293
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000294GrGpuGLShaders::GrGpuGLShaders(const GrGLContextInfo& ctxInfo)
295 : GrGpuGL(ctxInfo) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000296
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000297 // Enable supported shader-related caps
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000298 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000299 fCaps.fDualSourceBlendingSupport =
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000300 this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000301 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000302 fCaps.fShaderDerivativeSupport = true;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000303 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
304 fCaps.fGeometryShaderSupport =
305 this->glVersion() >= GR_GL_VER(3,2) &&
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000306 this->glslGeneration() >= k150_GrGLSLGeneration;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000307 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000308 fCaps.fShaderDerivativeSupport =
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000309 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000310 }
junov@google.comf93e7172011-03-31 21:26:24 +0000311
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000312 GR_GL_GetIntegerv(this->glInterface(),
313 GR_GL_MAX_VERTEX_ATTRIBS,
314 &fMaxVertexAttribs);
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000315
junov@google.comf93e7172011-03-31 21:26:24 +0000316 fProgramData = NULL;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000317 fProgramCache = new ProgramCache(this->glInterface(),
318 this->glslGeneration());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000319
320#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000321 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000322#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000323}
324
325GrGpuGLShaders::~GrGpuGLShaders() {
326 delete fProgramCache;
327}
328
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000329const GrMatrix& GrGpuGLShaders::getHWViewMatrix() {
330 GrAssert(fProgramData);
331
332 if (GrGLProgram::kSetAsAttribute ==
333 fProgramData->fUniLocations.fViewMatrixUni) {
334 return fHWDrawState.getViewMatrix();
335 } else {
336 return fProgramData->fViewMatrix;
337 }
338}
339
340void GrGpuGLShaders::recordHWViewMatrix(const GrMatrix& matrix) {
341 GrAssert(fProgramData);
342 if (GrGLProgram::kSetAsAttribute ==
343 fProgramData->fUniLocations.fViewMatrixUni) {
344 fHWDrawState.setViewMatrix(matrix);
345 } else {
346 fProgramData->fViewMatrix = matrix;
347 }
348}
349
junov@google.comf93e7172011-03-31 21:26:24 +0000350const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000351 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000352
353 if (GrGLProgram::kSetAsAttribute ==
354 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000355 return fHWDrawState.getSampler(stage).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000356 } else {
357 return fProgramData->fTextureMatrices[stage];
358 }
junov@google.comf93e7172011-03-31 21:26:24 +0000359}
360
361void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000362 GrAssert(fProgramData);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000363 if (GrGLProgram::kSetAsAttribute ==
bsalomon@google.com91961302011-05-09 18:39:58 +0000364 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000365 *fHWDrawState.sampler(stage)->matrix() = matrix;
bsalomon@google.com91961302011-05-09 18:39:58 +0000366 } else {
367 fProgramData->fTextureMatrices[stage] = matrix;
368 }
junov@google.comf93e7172011-03-31 21:26:24 +0000369}
370
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000371void GrGpuGLShaders::onResetContext() {
372 INHERITED::onResetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000373
junov@google.comf93e7172011-03-31 21:26:24 +0000374 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000375
376 // Third party GL code may have left vertex attributes enabled. Some GL
377 // implementations (osmesa) may read vetex attributes that are not required
378 // by the current shader. Therefore, we have to ensure that only the
379 // attributes we require for the current draw are enabled or we may cause an
380 // invalid read.
381
382 // Disable all vertex layout bits so that next flush will assume all
383 // optional vertex attributes are disabled.
384 fHWGeometryState.fVertexLayout = 0;
385
386 // We always use the this attribute and assume it is always enabled.
387 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
388 GL_CALL(EnableVertexAttribArray(posAttrIdx));
389 // Disable all other vertex attributes.
390 for (int va = 0; va < fMaxVertexAttribs; ++va) {
391 if (va != posAttrIdx) {
392 GL_CALL(DisableVertexAttribArray(va));
393 }
junov@google.comf93e7172011-03-31 21:26:24 +0000394 }
junov@google.comf93e7172011-03-31 21:26:24 +0000395
396 fHWProgramID = 0;
397}
398
399void GrGpuGLShaders::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000400 const GrMatrix& vm = this->getDrawState().getViewMatrix();
401 if (GrGpuGLShaders::getHWViewMatrix() != vm) {
junov@google.comf93e7172011-03-31 21:26:24 +0000402
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000403 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
404 GrAssert(NULL != rt);
405 GrMatrix m;
406 m.setAll(
407 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
408 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
409 0, 0, GrMatrix::I()[8]);
410 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000411
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000412 // ES doesn't allow you to pass true to the transpose param,
413 // so do our own transpose
414 GrGLfloat mt[] = {
415 GrScalarToFloat(m[GrMatrix::kMScaleX]),
416 GrScalarToFloat(m[GrMatrix::kMSkewY]),
417 GrScalarToFloat(m[GrMatrix::kMPersp0]),
418 GrScalarToFloat(m[GrMatrix::kMSkewX]),
419 GrScalarToFloat(m[GrMatrix::kMScaleY]),
420 GrScalarToFloat(m[GrMatrix::kMPersp1]),
421 GrScalarToFloat(m[GrMatrix::kMTransX]),
422 GrScalarToFloat(m[GrMatrix::kMTransY]),
423 GrScalarToFloat(m[GrMatrix::kMPersp2])
424 };
425
426 if (GrGLProgram::kSetAsAttribute ==
427 fProgramData->fUniLocations.fViewMatrixUni) {
428 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
429 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
430 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
431 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
432 } else {
433 GrAssert(GrGLProgram::kUnusedUniform !=
434 fProgramData->fUniLocations.fViewMatrixUni);
435 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
436 1, false, mt));
437 }
438 this->recordHWViewMatrix(vm);
bsalomon@google.com91961302011-05-09 18:39:58 +0000439 }
junov@google.comf93e7172011-03-31 21:26:24 +0000440}
441
junov@google.com6acc9b32011-05-16 18:32:07 +0000442void GrGpuGLShaders::flushTextureDomain(int s) {
443 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000444 const GrDrawState& drawState = this->getDrawState();
junov@google.com6acc9b32011-05-16 18:32:07 +0000445 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000446 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
junov@google.com6acc9b32011-05-16 18:32:07 +0000447
twiz@google.com76b82742011-06-02 20:30:02 +0000448 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
449 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000450
junov@google.com2f839402011-05-24 15:13:01 +0000451 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000452
junov@google.com2f839402011-05-24 15:13:01 +0000453 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000454 GrScalarToFloat(texDom.left()),
455 GrScalarToFloat(texDom.top()),
456 GrScalarToFloat(texDom.right()),
457 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000458 };
459
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000460 const GrGLTexture* texture =
461 static_cast<const GrGLTexture*>(drawState.getTexture(s));
junov@google.com2f839402011-05-24 15:13:01 +0000462 GrGLTexture::Orientation orientation = texture->orientation();
463
464 // vertical flip if necessary
465 if (GrGLTexture::kBottomUp_Orientation == orientation) {
466 values[1] = 1.0f - values[1];
467 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000468 // The top and bottom were just flipped, so correct the ordering
469 // of elements so that values = (l, t, r, b).
470 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000471 }
472
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000473 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000474 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000475 }
476}
477
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000478void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000479 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000480 const GrDrawState& drawState = this->getDrawState();
481 const GrGLTexture* texture =
482 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000483 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000484 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000485 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000486 this->getHWSamplerMatrix(s) != drawState.getSampler(s).getMatrix())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000487
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000488 GrMatrix m = drawState.getSampler(s).getMatrix();
489 GrSamplerState::SampleMode mode =
490 drawState.getSampler(s).getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000491 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000492
bsalomon@google.com91961302011-05-09 18:39:58 +0000493 // ES doesn't allow you to pass true to the transpose param,
494 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000495 GrGLfloat mt[] = {
496 GrScalarToFloat(m[GrMatrix::kMScaleX]),
497 GrScalarToFloat(m[GrMatrix::kMSkewY]),
498 GrScalarToFloat(m[GrMatrix::kMPersp0]),
499 GrScalarToFloat(m[GrMatrix::kMSkewX]),
500 GrScalarToFloat(m[GrMatrix::kMScaleY]),
501 GrScalarToFloat(m[GrMatrix::kMPersp1]),
502 GrScalarToFloat(m[GrMatrix::kMTransX]),
503 GrScalarToFloat(m[GrMatrix::kMTransY]),
504 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000505 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000506
bsalomon@google.com91961302011-05-09 18:39:58 +0000507 if (GrGLProgram::kSetAsAttribute ==
508 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
509 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000510 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
511 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
512 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000513 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000514 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000515 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000516 this->recordHWSamplerMatrix(s, drawState.getSampler(s).getMatrix());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000517 }
518 }
junov@google.comf93e7172011-03-31 21:26:24 +0000519}
520
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000521void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000522
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000523 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000524 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.com91961302011-05-09 18:39:58 +0000525 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000526 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
527 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
528 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000529
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000530 GrScalar centerX1 = sampler.getRadial2CenterX1();
531 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000532
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000533 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000534
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000535 // when were in the degenerate (linear) case the second
536 // value will be INF but the program doesn't read it. (We
537 // use the same 6 uniforms even though we don't need them
538 // all in the linear case just to keep the code complexity
539 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000540 float values[6] = {
541 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000542 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000543 GrScalarToFloat(centerX1),
544 GrScalarToFloat(radius0),
545 GrScalarToFloat(GrMul(radius0, radius0)),
546 sampler.isRadial2PosRoot() ? 1.f : -1.f
547 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000548 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000549 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
550 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
551 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
552 }
553}
554
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000555void GrGpuGLShaders::flushConvolution(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000556 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000557 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
558 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000559 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
560 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000561 }
562 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
563 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000564 GL_CALL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000565 }
566}
567
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000568void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000569 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000570 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000571 const GrGLTexture* texture =
572 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000573 if (texture->width() != fProgramData->fTextureWidth[s] ||
574 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000575
bsalomon@google.com99621082011-11-15 16:47:16 +0000576 float texelSize[] = {1.f / texture->width(),
577 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000578 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000579 fProgramData->fTextureWidth[s] = texture->width();
580 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000581 }
582 }
junov@google.comf93e7172011-03-31 21:26:24 +0000583}
584
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000585void GrGpuGLShaders::flushEdgeAAData() {
586 const int& uni = fProgramData->fUniLocations.fEdgesUni;
587 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000588 int count = this->getDrawState().getNumAAEdges();
tomhudson@google.com93813632011-10-27 20:21:16 +0000589 GrDrawState::Edge edges[GrDrawState::kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000590 // Flip the edges in Y
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000591 float height =
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000592 static_cast<float>(this->getDrawState().getRenderTarget()->height());
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000593 for (int i = 0; i < count; ++i) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000594 edges[i] = this->getDrawState().getAAEdges()[i];
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000595 float b = edges[i].fY;
596 edges[i].fY = -b;
597 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000598 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000599 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000600 }
601}
602
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000603void GrGpuGLShaders::flushColorMatrix() {
604 const ProgramDesc& desc = fCurrentProgram.getDesc();
605 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
606 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
607 if (GrGLProgram::kUnusedUniform != matrixUni
608 && GrGLProgram::kUnusedUniform != vecUni) {
609 const float* m = this->getDrawState().getColorMatrix();
610 GrGLfloat mt[] = {
611 m[0], m[5], m[10], m[15],
612 m[1], m[6], m[11], m[16],
613 m[2], m[7], m[12], m[17],
614 m[3], m[8], m[13], m[18],
615 };
616 static float scale = 1.0f / 255.0f;
617 GrGLfloat vec[] = {
618 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
619 };
620 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
621 GL_CALL(Uniform4fv(vecUni, 1, vec));
622 }
623}
624
Scroggo01b87ec2011-05-11 18:05:38 +0000625static const float ONE_OVER_255 = 1.f / 255.f;
626
627#define GR_COLOR_TO_VEC4(color) {\
628 GrColorUnpackR(color) * ONE_OVER_255,\
629 GrColorUnpackG(color) * ONE_OVER_255,\
630 GrColorUnpackB(color) * ONE_OVER_255,\
631 GrColorUnpackA(color) * ONE_OVER_255 \
632}
633
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000634void GrGpuGLShaders::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000635 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000636 const GrDrawState& drawState = this->getDrawState();
637
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000638 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000639 // color will be specified per-vertex as an attribute
640 // invalidate the const vertex attrib color
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000641 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000642 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000643 switch (desc.fColorInput) {
644 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000645 if (fHWDrawState.getColor() != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000646 // OpenGL ES only supports the float varieties of
647 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000648 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000649 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
650 c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000651 fHWDrawState.setColor(color);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000652 }
653 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000654 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000655 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000656 // OpenGL ES doesn't support unsigned byte varieties of
657 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000658 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000659 GrAssert(GrGLProgram::kUnusedUniform !=
660 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000661 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
662 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000663 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000664 }
665 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000666 case ProgramDesc::kSolidWhite_ColorInput:
667 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000668 break;
669 default:
670 GrCrash("Unknown color type.");
671 }
672 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000673 if (fProgramData->fUniLocations.fColorFilterUni
674 != GrGLProgram::kUnusedUniform
675 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000676 != drawState.getColorFilterColor()) {
677 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000678 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000679 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000680 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000681}
682
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000683void GrGpuGLShaders::flushCoverage(GrColor coverage) {
684 const ProgramDesc& desc = fCurrentProgram.getDesc();
685 const GrDrawState& drawState = this->getDrawState();
686
687
688 if (this->getGeomSrc().fVertexLayout & kCoverage_VertexLayoutBit) {
689 // coverage will be specified per-vertex as an attribute
690 // invalidate the const vertex attrib coverage
691 fHWDrawState.setCoverage4(GrColor_ILLEGAL);
692 } else {
693 switch (desc.fCoverageInput) {
694 case ProgramDesc::kAttribute_ColorInput:
695 if (fHWDrawState.getCoverage() != coverage) {
696 // OpenGL ES only supports the float varieties of
697 // glVertexAttrib
698 float c[] = GR_COLOR_TO_VEC4(coverage);
699 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
700 c));
701 fHWDrawState.setCoverage(coverage);
702 }
703 break;
704 case ProgramDesc::kUniform_ColorInput:
705 if (fProgramData->fCoverage != coverage) {
706 // OpenGL ES doesn't support unsigned byte varieties of
707 // glUniform
708 float c[] = GR_COLOR_TO_VEC4(coverage);
709 GrAssert(GrGLProgram::kUnusedUniform !=
710 fProgramData->fUniLocations.fCoverageUni);
711 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
712 1, c));
713 fProgramData->fCoverage = coverage;
714 }
715 break;
716 case ProgramDesc::kSolidWhite_ColorInput:
717 case ProgramDesc::kTransBlack_ColorInput:
718 break;
719 default:
720 GrCrash("Unknown coverage type.");
721 }
722 }
723}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000724
junov@google.comf93e7172011-03-31 21:26:24 +0000725bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
726 if (!flushGLStateCommon(type)) {
727 return false;
728 }
729
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000730 const GrDrawState& drawState = this->getDrawState();
731
junov@google.comf93e7172011-03-31 21:26:24 +0000732 if (fDirtyFlags.fRenderTargetChanged) {
733 // our coords are in pixel space and the GL matrices map to NDC
734 // so if the viewport changed, our matrix is now wrong.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000735 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
junov@google.comf93e7172011-03-31 21:26:24 +0000736 // we assume all shader matrices may be wrong after viewport changes
737 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000738 }
739
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000740 GrBlendCoeff srcCoeff;
741 GrBlendCoeff dstCoeff;
742 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
743 if (kSkipDraw_BlendOptFlag & blendOpts) {
744 return false;
745 }
746
747 this->buildProgram(type, blendOpts, dstCoeff);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000748 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000749 if (NULL == fProgramData) {
750 GrAssert(!"Failed to create program!");
751 return false;
752 }
junov@google.comf93e7172011-03-31 21:26:24 +0000753
754 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000755 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000756 fHWProgramID = fProgramData->fProgramID;
757 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000758 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
759 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000760
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000761 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000762 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000763 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
764 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000765 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000766 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
767 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000768 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000769 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000770 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000771 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000772 }
773 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000774 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000775
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000776 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000777
tomhudson@google.com93813632011-10-27 20:21:16 +0000778 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000779 if (this->isStageEnabled(s)) {
780 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000781
bsalomon@google.com40d92932011-12-13 18:40:47 +0000782 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000783
bsalomon@google.com40d92932011-12-13 18:40:47 +0000784 this->flushConvolution(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000785
bsalomon@google.com40d92932011-12-13 18:40:47 +0000786 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000787
bsalomon@google.com40d92932011-12-13 18:40:47 +0000788 this->flushTextureDomain(s);
789 }
junov@google.comf93e7172011-03-31 21:26:24 +0000790 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000791 this->flushEdgeAAData();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000792 this->flushColorMatrix();
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000793 resetDirtyFlags();
junov@google.comf93e7172011-03-31 21:26:24 +0000794 return true;
795}
796
797void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000798}
799
800void GrGpuGLShaders::setupGeometry(int* startVertex,
801 int* startIndex,
802 int vertexCount,
803 int indexCount) {
804
805 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000806 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000807 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000808 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000809
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000810 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
811 this->getGeomSrc().fVertexLayout,
812 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000813 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000814 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000815 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000816 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000817 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000818 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000819 int oldEdgeOffset;
820
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000821 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
822 fHWGeometryState.fVertexLayout,
823 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000824 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000825 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000826 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000827 bool indexed = NULL != startIndex;
828
829 int extraVertexOffset;
830 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000831 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000832
833 GrGLenum scalarType;
834 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000835 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000836 scalarType = GrGLTextType;
837 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
838 } else {
839 scalarType = GrGLType;
840 texCoordNorm = false;
841 }
842
843 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
844 *startVertex = 0;
845 if (indexed) {
846 *startIndex += extraIndexOffset;
847 }
848
849 // all the Pointers must be set if any of these are true
850 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
851 vertexOffset != fHWGeometryState.fVertexOffset ||
852 newStride != oldStride;
853
854 // position and tex coord offsets change if above conditions are true
855 // or the type/normalization changed based on text vs nontext type coords.
856 bool posAndTexChange = allOffsetsChange ||
857 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
858 (kTextFormat_VertexLayoutBit &
859 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000860 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000861
862 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000863 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000864 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000865 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000866 fHWGeometryState.fVertexOffset = vertexOffset;
867 }
868
tomhudson@google.com93813632011-10-27 20:21:16 +0000869 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000870 if (newTexCoordOffsets[t] > 0) {
871 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000872 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000873 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000874 GL_CALL(EnableVertexAttribArray(idx));
875 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000876 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000877 } else if (posAndTexChange ||
878 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000879 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000880 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000881 }
882 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000883 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000884 }
885 }
886
887 if (newColorOffset > 0) {
888 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000889 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000890 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000891 GL_CALL(EnableVertexAttribArray(idx));
892 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000893 true, newStride, colorOffset));
894 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000895 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000896 true, newStride, colorOffset));
897 }
898 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000899 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000900 }
901
bsalomon@google.coma3108262011-10-10 14:08:47 +0000902 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000903 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000904 int idx = GrGLProgram::CoverageAttributeIdx();
905 if (oldCoverageOffset <= 0) {
906 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000907 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000908 true, newStride, coverageOffset));
909 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000910 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000911 true, newStride, coverageOffset));
912 }
913 } else if (oldCoverageOffset > 0) {
914 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
915 }
916
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000917 if (newEdgeOffset > 0) {
918 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
919 int idx = GrGLProgram::EdgeAttributeIdx();
920 if (oldEdgeOffset <= 0) {
921 GL_CALL(EnableVertexAttribArray(idx));
922 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
923 false, newStride, edgeOffset));
924 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
925 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
926 false, newStride, edgeOffset));
927 }
928 } else if (oldEdgeOffset > 0) {
929 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
930 }
931
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000932 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000933 fHWGeometryState.fArrayPtrsDirty = false;
934}
935
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000936void GrGpuGLShaders::buildProgram(GrPrimitiveType type,
937 BlendOptFlags blendOpts,
938 GrBlendCoeff dstCoeff) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000939 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000940 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000941
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000942 // This should already have been caught
943 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
944
945 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
946
947 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
948 kEmitCoverage_BlendOptFlag));
949
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000950 // The descriptor is used as a cache key. Thus when a field of the
951 // descriptor will not affect program generation (because of the vertex
952 // layout in use or other descriptor field settings) it should be set
953 // to a canonical value to avoid duplicate programs with different keys.
954
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000955 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000956 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000957
958 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
959
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000960 bool requiresAttributeColors =
961 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000962 bool requiresAttributeCoverage =
963 !skipCoverage && SkToBool(desc.fVertexLayout &
964 kCoverage_VertexLayoutBit);
965
966 // fColorInput/fCoverageInput records how colors are specified for the.
967 // program. So we strip the bits from the layout to avoid false negatives
968 // when searching for an existing program in the cache.
969 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000970
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000971 desc.fColorFilterXfermode = skipColor ?
972 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000973 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000974
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000975 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
976
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000977 // no reason to do edge aa or look at per-vertex coverage if coverage is
978 // ignored
979 if (skipCoverage) {
980 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
981 kCoverage_VertexLayoutBit);
982 }
983
984 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
985 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
986 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000987 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000988 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000989 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000990 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000991 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000992 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000993 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000994 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000995 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000996 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000997
998 bool covIsSolidWhite = !requiresAttributeCoverage &&
999 0xffffffff == drawState.getCoverage();
1000
1001 if (skipCoverage) {
1002 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
1003 } else if (covIsSolidWhite) {
1004 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
1005 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
1006 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
1007 } else {
1008 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
1009 }
junov@google.comf93e7172011-03-31 21:26:24 +00001010
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001011 desc.fEdgeAANumEdges = skipCoverage ? 0 : drawState.getNumAAEdges();
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001012 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001013 drawState.isConcaveEdgeAAState();
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +00001014
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001015 int lastEnabledStage = -1;
1016
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001017 if (!skipCoverage && (desc.fVertexLayout &
1018 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001019 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001020 } else {
1021 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +00001022 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001023 }
1024
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001025 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001026 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001027
1028 stage.fOptFlags = 0;
1029 stage.setEnabled(this->isStageEnabled(s));
1030
1031 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
1032 skipCoverage;
1033
1034 if (!skip && stage.isEnabled()) {
1035 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001036 const GrGLTexture* texture =
1037 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001038 GrAssert(NULL != texture);
1039 const GrSamplerState& sampler = drawState.getSampler(s);
1040 // we matrix to invert when orientation is TopDown, so make sure
1041 // we aren't in that case before flagging as identity.
1042 if (TextureMatrixIsIdentity(texture, sampler)) {
1043 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
1044 } else if (!sampler.getMatrix().hasPerspective()) {
1045 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
1046 }
1047 switch (sampler.getSampleMode()) {
1048 case GrSamplerState::kNormal_SampleMode:
1049 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
1050 break;
1051 case GrSamplerState::kRadial_SampleMode:
1052 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
1053 break;
1054 case GrSamplerState::kRadial2_SampleMode:
1055 if (sampler.radial2IsDegenerate()) {
1056 stage.fCoordMapping =
1057 StageDesc::kRadial2GradientDegenerate_CoordMapping;
1058 } else {
1059 stage.fCoordMapping =
1060 StageDesc::kRadial2Gradient_CoordMapping;
1061 }
1062 break;
1063 case GrSamplerState::kSweep_SampleMode:
1064 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
1065 break;
1066 default:
1067 GrCrash("Unexpected sample mode!");
1068 break;
1069 }
1070
1071 switch (sampler.getFilter()) {
1072 // these both can use a regular texture2D()
1073 case GrSamplerState::kNearest_Filter:
1074 case GrSamplerState::kBilinear_Filter:
1075 stage.fFetchMode = StageDesc::kSingle_FetchMode;
1076 break;
1077 // performs 4 texture2D()s
1078 case GrSamplerState::k4x4Downsample_Filter:
1079 stage.fFetchMode = StageDesc::k2x2_FetchMode;
1080 break;
1081 // performs fKernelWidth texture2D()s
1082 case GrSamplerState::kConvolution_Filter:
1083 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
1084 break;
1085 default:
1086 GrCrash("Unexpected filter!");
1087 break;
1088 }
1089
1090 if (sampler.hasTextureDomain()) {
1091 GrAssert(GrSamplerState::kClamp_WrapMode ==
1092 sampler.getWrapX() &&
1093 GrSamplerState::kClamp_WrapMode ==
1094 sampler.getWrapY());
1095 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
1096 }
1097
1098 stage.fInConfigFlags = 0;
1099 if (!this->glCaps().fTextureSwizzleSupport) {
1100 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1101 // if we don't have texture swizzle support then
1102 // the shader must do an alpha smear after reading
1103 // the texture
1104 stage.fInConfigFlags |= StageDesc::kSmearAlpha_InConfigFlag;
1105 } else if (sampler.swapsRAndB()) {
1106 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
1107 }
1108 }
1109 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
1110 stage.fInConfigFlags |= StageDesc::kMulRGBByAlpha_InConfigFlag;
1111 }
1112
1113 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) {
1114 stage.fKernelWidth = sampler.getKernelWidth();
1115 } else {
1116 stage.fKernelWidth = 0;
1117 }
junov@google.comf93e7172011-03-31 21:26:24 +00001118 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001119 stage.fOptFlags = 0;
1120 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1121 stage.fInConfigFlags = 0;
1122 stage.fFetchMode = (StageDesc::FetchMode) 0;
1123 stage.fKernelWidth = 0;
junov@google.comf93e7172011-03-31 21:26:24 +00001124 }
1125 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001126
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001127 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001128 desc.fOutputPM = ProgramDesc::kNo_OutputPM;
1129 } else {
1130 desc.fOutputPM = ProgramDesc::kYes_OutputPM;
1131 }
1132
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001133 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001134
1135 // currently the experimental GS will only work with triangle prims
1136 // (and it doesn't do anything other than pass through values from
1137 // the VS to the FS anyway).
1138#if 0 && GR_GL_EXPERIMENTAL_GS
1139 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1140#endif
1141
bsalomon@google.coma3108262011-10-10 14:08:47 +00001142 // we want to avoid generating programs with different "first cov stage"
1143 // values when they would compute the same result.
1144 // We set field in the desc to kNumStages when either there are no
1145 // coverage stages or the distinction between coverage and color is
1146 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001147 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +00001148 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001149 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001150 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001151 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +00001152 }
1153
1154 // other coverage inputs
1155 if (!hasCoverage) {
1156 hasCoverage =
1157 desc.fEdgeAANumEdges ||
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001158 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +00001159 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1160 }
1161
1162 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001163 // color filter is applied between color/coverage computation
1164 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001165 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001166 }
1167
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001168 if (this->getCaps().fDualSourceBlendingSupport &&
1169 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1170 kCoverageAsAlpha_BlendOptFlag))) {
1171 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001172 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001173 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001174 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001175 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001176 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1177 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001178 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001179 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001180 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001181 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1182 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001183 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001184 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001185 }
1186 }
1187 }
junov@google.comf93e7172011-03-31 21:26:24 +00001188}