blob: a0a2df5802e493906221ba7524e054e13f59f047 [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;
bsalomon@google.com96399942012-02-13 14:39:16 +000058 const GrGLContextInfo& fGL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000059
junov@google.comf93e7172011-03-31 21:26:24 +000060public:
bsalomon@google.com96399942012-02-13 14:39:16 +000061 ProgramCache(const GrGLContextInfo& gl)
junov@google.comf93e7172011-03-31 21:26:24 +000062 : fCount(0)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000063 , fCurrLRUStamp(0)
bsalomon@google.com96399942012-02-13 14:39:16 +000064 , fGL(gl) {
junov@google.comf93e7172011-03-31 21:26:24 +000065 }
66
67 ~ProgramCache() {
68 for (int i = 0; i < fCount; ++i) {
bsalomon@google.com96399942012-02-13 14:39:16 +000069 GrGpuGLShaders::DeleteProgram(fGL.interface(),
70 &fEntries[i].fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000071 }
72 }
73
74 void abandon() {
75 fCount = 0;
76 }
77
78 void invalidateViewMatrices() {
79 for (int i = 0; i < fCount; ++i) {
80 // set to illegal matrix
81 fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix();
82 }
83 }
84
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000085 GrGLProgram::CachedData* getProgramData(const GrGLProgram& desc) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000086 Entry newEntry;
junov@google.comf7c00f62011-08-18 18:15:16 +000087 newEntry.fKey.setKeyData(desc.keyData());
88
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000089 Entry* entry = fHashCache.find(newEntry.fKey);
junov@google.comf93e7172011-03-31 21:26:24 +000090 if (NULL == entry) {
bsalomon@google.com96399942012-02-13 14:39:16 +000091 if (!desc.genProgram(fGL, &newEntry.fProgramData)) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000092 return NULL;
93 }
junov@google.comf93e7172011-03-31 21:26:24 +000094 if (fCount < kMaxEntries) {
95 entry = fEntries + fCount;
96 ++fCount;
97 } else {
98 GrAssert(kMaxEntries == fCount);
99 entry = fEntries;
100 for (int i = 1; i < kMaxEntries; ++i) {
101 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
102 entry = fEntries + i;
103 }
104 }
105 fHashCache.remove(entry->fKey, entry);
bsalomon@google.com96399942012-02-13 14:39:16 +0000106 GrGpuGLShaders::DeleteProgram(fGL.interface(),
107 &entry->fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +0000108 }
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000109 entry->copyAndTakeOwnership(newEntry);
junov@google.comf93e7172011-03-31 21:26:24 +0000110 fHashCache.insert(entry->fKey, entry);
111 }
112
113 entry->fLRUStamp = fCurrLRUStamp;
114 if (GR_UINT32_MAX == fCurrLRUStamp) {
115 // wrap around! just trash our LRU, one time hit.
116 for (int i = 0; i < fCount; ++i) {
117 fEntries[i].fLRUStamp = 0;
118 }
119 }
120 ++fCurrLRUStamp;
121 return &entry->fProgramData;
122 }
123};
124
junov@google.com53a55842011-06-08 22:55:10 +0000125void GrGpuGLShaders::abandonResources(){
126 INHERITED::abandonResources();
127 fProgramCache->abandon();
128}
129
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000130void GrGpuGLShaders::DeleteProgram(const GrGLInterface* gl,
131 CachedData* programData) {
132 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000133 if (programData->fGShaderID) {
134 GR_GL_CALL(gl, DeleteShader(programData->fGShaderID));
135 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000136 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
137 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000138 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
139}
140
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000141////////////////////////////////////////////////////////////////////////////////
142
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000143#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
144
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000145namespace {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000146
bsalomon@google.com74b98712011-11-11 19:46:16 +0000147// GrRandoms nextU() values have patterns in the low bits
148// So using nextU() % array_count might never take some values.
149int random_int(GrRandom* r, int count) {
150 return (int)(r->nextF() * count);
151}
152
153// min is inclusive, max is exclusive
154int random_int(GrRandom* r, int min, int max) {
155 return (int)(r->nextF() * (max-min)) + min;
156}
157
158bool random_bool(GrRandom* r) {
159 return r->nextF() > .5f;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000160}
161
162}
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000163
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000164bool GrGpuGLShaders::programUnitTest() {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000165
tomhudson@google.com086e5352011-12-08 14:44:10 +0000166 GrGLSLGeneration glslGeneration =
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000167 GrGetGLSLGeneration(this->glBinding(), this->glInterface());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000168 static const int STAGE_OPTS[] = {
169 0,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000170 StageDesc::kNoPerspective_OptFlagBit,
171 StageDesc::kIdentity_CoordMapping
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000172 };
bsalomon@google.com74b98712011-11-11 19:46:16 +0000173 static const int IN_CONFIG_FLAGS[] = {
174 StageDesc::kNone_InConfigFlag,
175 StageDesc::kSwapRAndB_InConfigFlag,
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000176 StageDesc::kSwapRAndB_InConfigFlag |
177 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag,
178 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000179 StageDesc::kSmearAlpha_InConfigFlag,
180 };
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000181 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000182 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000183
184 static const int NUM_TESTS = 512;
185
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000186 GrRandom random;
187 for (int t = 0; t < NUM_TESTS; ++t) {
188
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000189#if 0
190 GrPrintf("\nTest Program %d\n-------------\n", t);
191 static const int stop = -1;
192 if (t == stop) {
193 int breakpointhere = 9;
194 }
195#endif
196
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000197 pdesc.fVertexLayout = 0;
198 pdesc.fEmitsPointSize = random.nextF() > .5f;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000199 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000200 pdesc.fCoverageInput = 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.coma91e9232012-02-23 15:39:54 +0000214 pdesc.fOutputConfig = random_int(&random, ProgramDesc::kOutputConfigCnt);
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.com69cc6ad2012-01-17 14:25:10 +0000222 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000223 } else {
tomhudson@google.com93813632011-10-27 20:21:16 +0000224 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000225 }
226 pdesc.fEdgeAANumEdges = 0;
227 } else {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000228 pdesc.fEdgeAANumEdges = random_int(&random, 1, this->getMaxEdges());
229 pdesc.fEdgeAAConcave = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000230 }
231 } else {
232 pdesc.fEdgeAANumEdges = 0;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000233 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000234
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000235 pdesc.fColorMatrixEnabled = random_bool(&random);
236
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.coma91e9232012-02-23 15:39:54 +0000268 static const uint32_t kMulByAlphaMask =
269 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
270 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000271 switch (stage.fFetchMode) {
272 case StageDesc::kSingle_FetchMode:
273 stage.fKernelWidth = 0;
274 break;
275 case StageDesc::kConvolution_FetchMode:
276 stage.fKernelWidth = random_int(&random, 2, 8);
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000277 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000278 break;
279 case StageDesc::k2x2_FetchMode:
280 stage.fKernelWidth = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000281 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000282 break;
283 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000284 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000285 CachedData cachedData;
bsalomon@google.com96399942012-02-13 14:39:16 +0000286 if (!program.genProgram(this->glContextInfo(), &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.com96399942012-02-13 14:39:16 +0000317 fProgramCache = new ProgramCache(this->glContextInfo());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000318
319#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000320 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000321#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000322}
323
324GrGpuGLShaders::~GrGpuGLShaders() {
325 delete fProgramCache;
326}
327
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000328const GrMatrix& GrGpuGLShaders::getHWViewMatrix() {
329 GrAssert(fProgramData);
330
331 if (GrGLProgram::kSetAsAttribute ==
332 fProgramData->fUniLocations.fViewMatrixUni) {
333 return fHWDrawState.getViewMatrix();
334 } else {
335 return fProgramData->fViewMatrix;
336 }
337}
338
339void GrGpuGLShaders::recordHWViewMatrix(const GrMatrix& matrix) {
340 GrAssert(fProgramData);
341 if (GrGLProgram::kSetAsAttribute ==
342 fProgramData->fUniLocations.fViewMatrixUni) {
343 fHWDrawState.setViewMatrix(matrix);
344 } else {
345 fProgramData->fViewMatrix = matrix;
346 }
347}
348
junov@google.comf93e7172011-03-31 21:26:24 +0000349const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000350 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000351
352 if (GrGLProgram::kSetAsAttribute ==
353 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000354 return fHWDrawState.getSampler(stage).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000355 } else {
356 return fProgramData->fTextureMatrices[stage];
357 }
junov@google.comf93e7172011-03-31 21:26:24 +0000358}
359
360void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000361 GrAssert(fProgramData);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000362 if (GrGLProgram::kSetAsAttribute ==
bsalomon@google.com91961302011-05-09 18:39:58 +0000363 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000364 *fHWDrawState.sampler(stage)->matrix() = matrix;
bsalomon@google.com91961302011-05-09 18:39:58 +0000365 } else {
366 fProgramData->fTextureMatrices[stage] = matrix;
367 }
junov@google.comf93e7172011-03-31 21:26:24 +0000368}
369
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000370void GrGpuGLShaders::onResetContext() {
371 INHERITED::onResetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000372
junov@google.comf93e7172011-03-31 21:26:24 +0000373 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000374
375 // Third party GL code may have left vertex attributes enabled. Some GL
376 // implementations (osmesa) may read vetex attributes that are not required
377 // by the current shader. Therefore, we have to ensure that only the
378 // attributes we require for the current draw are enabled or we may cause an
379 // invalid read.
380
381 // Disable all vertex layout bits so that next flush will assume all
382 // optional vertex attributes are disabled.
383 fHWGeometryState.fVertexLayout = 0;
384
385 // We always use the this attribute and assume it is always enabled.
386 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
387 GL_CALL(EnableVertexAttribArray(posAttrIdx));
388 // Disable all other vertex attributes.
389 for (int va = 0; va < fMaxVertexAttribs; ++va) {
390 if (va != posAttrIdx) {
391 GL_CALL(DisableVertexAttribArray(va));
392 }
junov@google.comf93e7172011-03-31 21:26:24 +0000393 }
junov@google.comf93e7172011-03-31 21:26:24 +0000394
395 fHWProgramID = 0;
396}
397
398void GrGpuGLShaders::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000399 const GrMatrix& vm = this->getDrawState().getViewMatrix();
400 if (GrGpuGLShaders::getHWViewMatrix() != vm) {
junov@google.comf93e7172011-03-31 21:26:24 +0000401
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000402 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
403 GrAssert(NULL != rt);
404 GrMatrix m;
405 m.setAll(
406 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
407 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
408 0, 0, GrMatrix::I()[8]);
409 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000410
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000411 // ES doesn't allow you to pass true to the transpose param,
412 // so do our own transpose
413 GrGLfloat mt[] = {
414 GrScalarToFloat(m[GrMatrix::kMScaleX]),
415 GrScalarToFloat(m[GrMatrix::kMSkewY]),
416 GrScalarToFloat(m[GrMatrix::kMPersp0]),
417 GrScalarToFloat(m[GrMatrix::kMSkewX]),
418 GrScalarToFloat(m[GrMatrix::kMScaleY]),
419 GrScalarToFloat(m[GrMatrix::kMPersp1]),
420 GrScalarToFloat(m[GrMatrix::kMTransX]),
421 GrScalarToFloat(m[GrMatrix::kMTransY]),
422 GrScalarToFloat(m[GrMatrix::kMPersp2])
423 };
424
425 if (GrGLProgram::kSetAsAttribute ==
426 fProgramData->fUniLocations.fViewMatrixUni) {
427 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
428 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
429 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
430 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
431 } else {
432 GrAssert(GrGLProgram::kUnusedUniform !=
433 fProgramData->fUniLocations.fViewMatrixUni);
434 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
435 1, false, mt));
436 }
437 this->recordHWViewMatrix(vm);
bsalomon@google.com91961302011-05-09 18:39:58 +0000438 }
junov@google.comf93e7172011-03-31 21:26:24 +0000439}
440
junov@google.com6acc9b32011-05-16 18:32:07 +0000441void GrGpuGLShaders::flushTextureDomain(int s) {
442 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000443 const GrDrawState& drawState = this->getDrawState();
junov@google.com6acc9b32011-05-16 18:32:07 +0000444 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000445 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
junov@google.com6acc9b32011-05-16 18:32:07 +0000446
twiz@google.com76b82742011-06-02 20:30:02 +0000447 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
448 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000449
junov@google.com2f839402011-05-24 15:13:01 +0000450 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000451
junov@google.com2f839402011-05-24 15:13:01 +0000452 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000453 GrScalarToFloat(texDom.left()),
454 GrScalarToFloat(texDom.top()),
455 GrScalarToFloat(texDom.right()),
456 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000457 };
458
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000459 const GrGLTexture* texture =
460 static_cast<const GrGLTexture*>(drawState.getTexture(s));
junov@google.com2f839402011-05-24 15:13:01 +0000461 GrGLTexture::Orientation orientation = texture->orientation();
462
463 // vertical flip if necessary
464 if (GrGLTexture::kBottomUp_Orientation == orientation) {
465 values[1] = 1.0f - values[1];
466 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000467 // The top and bottom were just flipped, so correct the ordering
468 // of elements so that values = (l, t, r, b).
469 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000470 }
471
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000472 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000473 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000474 }
475}
476
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000477void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000478 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000479 const GrDrawState& drawState = this->getDrawState();
480 const GrGLTexture* texture =
481 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000482 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000483 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000484 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000485 this->getHWSamplerMatrix(s) != drawState.getSampler(s).getMatrix())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000486
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000487 GrMatrix m = drawState.getSampler(s).getMatrix();
488 GrSamplerState::SampleMode mode =
489 drawState.getSampler(s).getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000490 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000491
bsalomon@google.com91961302011-05-09 18:39:58 +0000492 // ES doesn't allow you to pass true to the transpose param,
493 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000494 GrGLfloat mt[] = {
495 GrScalarToFloat(m[GrMatrix::kMScaleX]),
496 GrScalarToFloat(m[GrMatrix::kMSkewY]),
497 GrScalarToFloat(m[GrMatrix::kMPersp0]),
498 GrScalarToFloat(m[GrMatrix::kMSkewX]),
499 GrScalarToFloat(m[GrMatrix::kMScaleY]),
500 GrScalarToFloat(m[GrMatrix::kMPersp1]),
501 GrScalarToFloat(m[GrMatrix::kMTransX]),
502 GrScalarToFloat(m[GrMatrix::kMTransY]),
503 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000504 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000505
bsalomon@google.com91961302011-05-09 18:39:58 +0000506 if (GrGLProgram::kSetAsAttribute ==
507 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
508 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000509 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
510 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
511 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000512 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000513 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000514 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000515 this->recordHWSamplerMatrix(s, drawState.getSampler(s).getMatrix());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000516 }
517 }
junov@google.comf93e7172011-03-31 21:26:24 +0000518}
519
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000520void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000521
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000522 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000523 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.com91961302011-05-09 18:39:58 +0000524 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000525 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
526 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
527 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000528
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000529 GrScalar centerX1 = sampler.getRadial2CenterX1();
530 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000531
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000532 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000533
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000534 // when were in the degenerate (linear) case the second
535 // value will be INF but the program doesn't read it. (We
536 // use the same 6 uniforms even though we don't need them
537 // all in the linear case just to keep the code complexity
538 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000539 float values[6] = {
540 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000541 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000542 GrScalarToFloat(centerX1),
543 GrScalarToFloat(radius0),
544 GrScalarToFloat(GrMul(radius0, radius0)),
545 sampler.isRadial2PosRoot() ? 1.f : -1.f
546 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000547 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000548 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
549 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
550 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
551 }
552}
553
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000554void GrGpuGLShaders::flushConvolution(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000555 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000556 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
557 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000558 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
559 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000560 }
561 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
562 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000563 GL_CALL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000564 }
565}
566
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000567void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000568 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000569 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000570 const GrGLTexture* texture =
571 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000572 if (texture->width() != fProgramData->fTextureWidth[s] ||
573 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000574
bsalomon@google.com99621082011-11-15 16:47:16 +0000575 float texelSize[] = {1.f / texture->width(),
576 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000577 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000578 fProgramData->fTextureWidth[s] = texture->width();
579 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000580 }
581 }
junov@google.comf93e7172011-03-31 21:26:24 +0000582}
583
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000584void GrGpuGLShaders::flushEdgeAAData() {
585 const int& uni = fProgramData->fUniLocations.fEdgesUni;
586 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000587 int count = this->getDrawState().getNumAAEdges();
tomhudson@google.com93813632011-10-27 20:21:16 +0000588 GrDrawState::Edge edges[GrDrawState::kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000589 // Flip the edges in Y
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000590 float height =
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000591 static_cast<float>(this->getDrawState().getRenderTarget()->height());
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000592 for (int i = 0; i < count; ++i) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000593 edges[i] = this->getDrawState().getAAEdges()[i];
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000594 float b = edges[i].fY;
595 edges[i].fY = -b;
596 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000597 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000598 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000599 }
600}
601
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000602void GrGpuGLShaders::flushColorMatrix() {
603 const ProgramDesc& desc = fCurrentProgram.getDesc();
604 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
605 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
606 if (GrGLProgram::kUnusedUniform != matrixUni
607 && GrGLProgram::kUnusedUniform != vecUni) {
608 const float* m = this->getDrawState().getColorMatrix();
609 GrGLfloat mt[] = {
610 m[0], m[5], m[10], m[15],
611 m[1], m[6], m[11], m[16],
612 m[2], m[7], m[12], m[17],
613 m[3], m[8], m[13], m[18],
614 };
615 static float scale = 1.0f / 255.0f;
616 GrGLfloat vec[] = {
617 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
618 };
619 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
620 GL_CALL(Uniform4fv(vecUni, 1, vec));
621 }
622}
623
Scroggo01b87ec2011-05-11 18:05:38 +0000624static const float ONE_OVER_255 = 1.f / 255.f;
625
626#define GR_COLOR_TO_VEC4(color) {\
627 GrColorUnpackR(color) * ONE_OVER_255,\
628 GrColorUnpackG(color) * ONE_OVER_255,\
629 GrColorUnpackB(color) * ONE_OVER_255,\
630 GrColorUnpackA(color) * ONE_OVER_255 \
631}
632
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000633void GrGpuGLShaders::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000634 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000635 const GrDrawState& drawState = this->getDrawState();
636
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000637 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000638 // color will be specified per-vertex as an attribute
639 // invalidate the const vertex attrib color
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000640 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000641 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000642 switch (desc.fColorInput) {
643 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000644 if (fHWDrawState.getColor() != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000645 // OpenGL ES only supports the float varieties of
646 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000647 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000648 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
649 c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000650 fHWDrawState.setColor(color);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000651 }
652 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000653 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000654 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000655 // OpenGL ES doesn't support unsigned byte varieties of
656 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000657 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000658 GrAssert(GrGLProgram::kUnusedUniform !=
659 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000660 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
661 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000662 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000663 }
664 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000665 case ProgramDesc::kSolidWhite_ColorInput:
666 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000667 break;
668 default:
669 GrCrash("Unknown color type.");
670 }
671 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000672 if (fProgramData->fUniLocations.fColorFilterUni
673 != GrGLProgram::kUnusedUniform
674 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000675 != drawState.getColorFilterColor()) {
676 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000677 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000678 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000679 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000680}
681
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000682void GrGpuGLShaders::flushCoverage(GrColor coverage) {
683 const ProgramDesc& desc = fCurrentProgram.getDesc();
684 const GrDrawState& drawState = this->getDrawState();
685
686
687 if (this->getGeomSrc().fVertexLayout & kCoverage_VertexLayoutBit) {
688 // coverage will be specified per-vertex as an attribute
689 // invalidate the const vertex attrib coverage
690 fHWDrawState.setCoverage4(GrColor_ILLEGAL);
691 } else {
692 switch (desc.fCoverageInput) {
693 case ProgramDesc::kAttribute_ColorInput:
694 if (fHWDrawState.getCoverage() != coverage) {
695 // OpenGL ES only supports the float varieties of
696 // glVertexAttrib
697 float c[] = GR_COLOR_TO_VEC4(coverage);
698 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
699 c));
700 fHWDrawState.setCoverage(coverage);
701 }
702 break;
703 case ProgramDesc::kUniform_ColorInput:
704 if (fProgramData->fCoverage != coverage) {
705 // OpenGL ES doesn't support unsigned byte varieties of
706 // glUniform
707 float c[] = GR_COLOR_TO_VEC4(coverage);
708 GrAssert(GrGLProgram::kUnusedUniform !=
709 fProgramData->fUniLocations.fCoverageUni);
710 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
711 1, c));
712 fProgramData->fCoverage = coverage;
713 }
714 break;
715 case ProgramDesc::kSolidWhite_ColorInput:
716 case ProgramDesc::kTransBlack_ColorInput:
717 break;
718 default:
719 GrCrash("Unknown coverage type.");
720 }
721 }
722}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000723
junov@google.comf93e7172011-03-31 21:26:24 +0000724bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
725 if (!flushGLStateCommon(type)) {
726 return false;
727 }
728
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000729 const GrDrawState& drawState = this->getDrawState();
730
junov@google.comf93e7172011-03-31 21:26:24 +0000731 if (fDirtyFlags.fRenderTargetChanged) {
732 // our coords are in pixel space and the GL matrices map to NDC
733 // so if the viewport changed, our matrix is now wrong.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000734 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
junov@google.comf93e7172011-03-31 21:26:24 +0000735 // we assume all shader matrices may be wrong after viewport changes
736 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000737 }
738
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000739 GrBlendCoeff srcCoeff;
740 GrBlendCoeff dstCoeff;
741 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
742 if (kSkipDraw_BlendOptFlag & blendOpts) {
743 return false;
744 }
745
746 this->buildProgram(type, blendOpts, dstCoeff);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000747 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000748 if (NULL == fProgramData) {
749 GrAssert(!"Failed to create program!");
750 return false;
751 }
junov@google.comf93e7172011-03-31 21:26:24 +0000752
753 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000754 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000755 fHWProgramID = fProgramData->fProgramID;
756 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000757 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
758 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000759
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000760 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000761 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000762 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
763 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000764 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000765 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
766 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000767 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000768 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000769 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000770 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000771 }
772 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000773 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000774
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000775 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000776
tomhudson@google.com93813632011-10-27 20:21:16 +0000777 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000778 if (this->isStageEnabled(s)) {
779 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000780
bsalomon@google.com40d92932011-12-13 18:40:47 +0000781 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000782
bsalomon@google.com40d92932011-12-13 18:40:47 +0000783 this->flushConvolution(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000784
bsalomon@google.com40d92932011-12-13 18:40:47 +0000785 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000786
bsalomon@google.com40d92932011-12-13 18:40:47 +0000787 this->flushTextureDomain(s);
788 }
junov@google.comf93e7172011-03-31 21:26:24 +0000789 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000790 this->flushEdgeAAData();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000791 this->flushColorMatrix();
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000792 resetDirtyFlags();
junov@google.comf93e7172011-03-31 21:26:24 +0000793 return true;
794}
795
796void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000797}
798
799void GrGpuGLShaders::setupGeometry(int* startVertex,
800 int* startIndex,
801 int vertexCount,
802 int indexCount) {
803
804 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000805 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000806 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000807 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000808
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000809 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
810 this->getGeomSrc().fVertexLayout,
811 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000812 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000813 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000814 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000815 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000816 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000817 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000818 int oldEdgeOffset;
819
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000820 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
821 fHWGeometryState.fVertexLayout,
822 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000823 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000824 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000825 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000826 bool indexed = NULL != startIndex;
827
828 int extraVertexOffset;
829 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000830 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000831
832 GrGLenum scalarType;
833 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000834 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000835 scalarType = GrGLTextType;
836 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
837 } else {
838 scalarType = GrGLType;
839 texCoordNorm = false;
840 }
841
842 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
843 *startVertex = 0;
844 if (indexed) {
845 *startIndex += extraIndexOffset;
846 }
847
848 // all the Pointers must be set if any of these are true
849 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
850 vertexOffset != fHWGeometryState.fVertexOffset ||
851 newStride != oldStride;
852
853 // position and tex coord offsets change if above conditions are true
854 // or the type/normalization changed based on text vs nontext type coords.
855 bool posAndTexChange = allOffsetsChange ||
856 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
857 (kTextFormat_VertexLayoutBit &
858 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000859 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000860
861 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000862 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000863 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000864 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000865 fHWGeometryState.fVertexOffset = vertexOffset;
866 }
867
tomhudson@google.com93813632011-10-27 20:21:16 +0000868 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000869 if (newTexCoordOffsets[t] > 0) {
870 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000871 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000872 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000873 GL_CALL(EnableVertexAttribArray(idx));
874 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000875 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000876 } else if (posAndTexChange ||
877 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000878 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000879 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000880 }
881 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000882 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000883 }
884 }
885
886 if (newColorOffset > 0) {
887 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000888 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000889 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000890 GL_CALL(EnableVertexAttribArray(idx));
891 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000892 true, newStride, colorOffset));
893 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000894 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000895 true, newStride, colorOffset));
896 }
897 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000898 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000899 }
900
bsalomon@google.coma3108262011-10-10 14:08:47 +0000901 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000902 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000903 int idx = GrGLProgram::CoverageAttributeIdx();
904 if (oldCoverageOffset <= 0) {
905 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000906 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000907 true, newStride, coverageOffset));
908 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000909 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000910 true, newStride, coverageOffset));
911 }
912 } else if (oldCoverageOffset > 0) {
913 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
914 }
915
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000916 if (newEdgeOffset > 0) {
917 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
918 int idx = GrGLProgram::EdgeAttributeIdx();
919 if (oldEdgeOffset <= 0) {
920 GL_CALL(EnableVertexAttribArray(idx));
921 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
922 false, newStride, edgeOffset));
923 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
924 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
925 false, newStride, edgeOffset));
926 }
927 } else if (oldEdgeOffset > 0) {
928 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
929 }
930
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000931 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000932 fHWGeometryState.fArrayPtrsDirty = false;
933}
934
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000935void GrGpuGLShaders::buildProgram(GrPrimitiveType type,
936 BlendOptFlags blendOpts,
937 GrBlendCoeff dstCoeff) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000938 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000939 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000940
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000941 // This should already have been caught
942 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
943
944 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
945
946 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
947 kEmitCoverage_BlendOptFlag));
948
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000949 // The descriptor is used as a cache key. Thus when a field of the
950 // descriptor will not affect program generation (because of the vertex
951 // layout in use or other descriptor field settings) it should be set
952 // to a canonical value to avoid duplicate programs with different keys.
953
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000954 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000955 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000956
957 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
958
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000959 bool requiresAttributeColors =
960 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000961 bool requiresAttributeCoverage =
962 !skipCoverage && SkToBool(desc.fVertexLayout &
963 kCoverage_VertexLayoutBit);
964
965 // fColorInput/fCoverageInput records how colors are specified for the.
966 // program. So we strip the bits from the layout to avoid false negatives
967 // when searching for an existing program in the cache.
968 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000969
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000970 desc.fColorFilterXfermode = skipColor ?
971 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000972 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000973
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000974 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
975
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000976 // no reason to do edge aa or look at per-vertex coverage if coverage is
977 // ignored
978 if (skipCoverage) {
979 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
980 kCoverage_VertexLayoutBit);
981 }
982
983 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
984 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
985 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000986 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000987 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000988 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000989 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000990 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000991 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000992 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000993 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000994 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000995 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000996
997 bool covIsSolidWhite = !requiresAttributeCoverage &&
998 0xffffffff == drawState.getCoverage();
999
1000 if (skipCoverage) {
1001 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
1002 } else if (covIsSolidWhite) {
1003 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
1004 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
1005 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
1006 } else {
1007 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
1008 }
junov@google.comf93e7172011-03-31 21:26:24 +00001009
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001010 desc.fEdgeAANumEdges = skipCoverage ? 0 : drawState.getNumAAEdges();
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001011 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001012 drawState.isConcaveEdgeAAState();
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +00001013
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001014 int lastEnabledStage = -1;
1015
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001016 if (!skipCoverage && (desc.fVertexLayout &
1017 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001018 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001019 } else {
1020 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +00001021 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001022 }
1023
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001024 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001025 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001026
1027 stage.fOptFlags = 0;
1028 stage.setEnabled(this->isStageEnabled(s));
1029
1030 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
1031 skipCoverage;
1032
1033 if (!skip && stage.isEnabled()) {
1034 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001035 const GrGLTexture* texture =
1036 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001037 GrAssert(NULL != texture);
1038 const GrSamplerState& sampler = drawState.getSampler(s);
1039 // we matrix to invert when orientation is TopDown, so make sure
1040 // we aren't in that case before flagging as identity.
1041 if (TextureMatrixIsIdentity(texture, sampler)) {
1042 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
1043 } else if (!sampler.getMatrix().hasPerspective()) {
1044 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
1045 }
1046 switch (sampler.getSampleMode()) {
1047 case GrSamplerState::kNormal_SampleMode:
1048 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
1049 break;
1050 case GrSamplerState::kRadial_SampleMode:
1051 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
1052 break;
1053 case GrSamplerState::kRadial2_SampleMode:
1054 if (sampler.radial2IsDegenerate()) {
1055 stage.fCoordMapping =
1056 StageDesc::kRadial2GradientDegenerate_CoordMapping;
1057 } else {
1058 stage.fCoordMapping =
1059 StageDesc::kRadial2Gradient_CoordMapping;
1060 }
1061 break;
1062 case GrSamplerState::kSweep_SampleMode:
1063 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
1064 break;
1065 default:
1066 GrCrash("Unexpected sample mode!");
1067 break;
1068 }
1069
1070 switch (sampler.getFilter()) {
1071 // these both can use a regular texture2D()
1072 case GrSamplerState::kNearest_Filter:
1073 case GrSamplerState::kBilinear_Filter:
1074 stage.fFetchMode = StageDesc::kSingle_FetchMode;
1075 break;
1076 // performs 4 texture2D()s
1077 case GrSamplerState::k4x4Downsample_Filter:
1078 stage.fFetchMode = StageDesc::k2x2_FetchMode;
1079 break;
1080 // performs fKernelWidth texture2D()s
1081 case GrSamplerState::kConvolution_Filter:
1082 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
1083 break;
1084 default:
1085 GrCrash("Unexpected filter!");
1086 break;
1087 }
1088
1089 if (sampler.hasTextureDomain()) {
1090 GrAssert(GrSamplerState::kClamp_WrapMode ==
1091 sampler.getWrapX() &&
1092 GrSamplerState::kClamp_WrapMode ==
1093 sampler.getWrapY());
1094 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
1095 }
1096
1097 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001098 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001099 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1100 // if we don't have texture swizzle support then
1101 // the shader must do an alpha smear after reading
1102 // the texture
1103 stage.fInConfigFlags |= StageDesc::kSmearAlpha_InConfigFlag;
1104 } else if (sampler.swapsRAndB()) {
1105 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
1106 }
1107 }
1108 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001109 // The shader generator assumes that color channels are bytes
1110 // when rounding.
1111 GrAssert(4 == GrBytesPerPixel(texture->config()));
1112 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
1113 fUnpremulConversion) {
1114 stage.fInConfigFlags |=
1115 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
1116 } else {
1117 stage.fInConfigFlags |=
1118 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
1119 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001120 }
1121
1122 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) {
1123 stage.fKernelWidth = sampler.getKernelWidth();
1124 } else {
1125 stage.fKernelWidth = 0;
1126 }
junov@google.comf93e7172011-03-31 21:26:24 +00001127 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001128 stage.fOptFlags = 0;
1129 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1130 stage.fInConfigFlags = 0;
1131 stage.fFetchMode = (StageDesc::FetchMode) 0;
1132 stage.fKernelWidth = 0;
junov@google.comf93e7172011-03-31 21:26:24 +00001133 }
1134 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001135
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001136 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001137 // The shader generator assumes that color channels are bytes
1138 // when rounding.
1139 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
1140 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
1141 desc.fOutputConfig =
1142 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
1143 } else {
1144 desc.fOutputConfig =
1145 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
1146 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001147 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001148 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001149 }
1150
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001151 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001152
1153 // currently the experimental GS will only work with triangle prims
1154 // (and it doesn't do anything other than pass through values from
1155 // the VS to the FS anyway).
1156#if 0 && GR_GL_EXPERIMENTAL_GS
1157 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1158#endif
1159
bsalomon@google.coma3108262011-10-10 14:08:47 +00001160 // we want to avoid generating programs with different "first cov stage"
1161 // values when they would compute the same result.
1162 // We set field in the desc to kNumStages when either there are no
1163 // coverage stages or the distinction between coverage and color is
1164 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001165 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +00001166 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001167 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001168 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001169 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +00001170 }
1171
1172 // other coverage inputs
1173 if (!hasCoverage) {
1174 hasCoverage =
1175 desc.fEdgeAANumEdges ||
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001176 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +00001177 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1178 }
1179
1180 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001181 // color filter is applied between color/coverage computation
1182 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001183 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001184 }
1185
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001186 if (this->getCaps().fDualSourceBlendingSupport &&
1187 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1188 kCoverageAsAlpha_BlendOptFlag))) {
1189 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001190 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001191 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001192 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001193 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001194 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1195 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001196 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001197 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001198 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001199 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1200 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001201 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001202 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001203 }
1204 }
1205 }
junov@google.comf93e7172011-03-31 21:26:24 +00001206}