blob: 4093a0da0d775d4faedea1d6aea2aa7806a136f1 [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,
176 StageDesc::kSwapRAndB_InConfigFlag | StageDesc::kMulRGBByAlpha_InConfigFlag,
177 StageDesc::kMulRGBByAlpha_InConfigFlag,
178 StageDesc::kSmearAlpha_InConfigFlag,
179 };
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000180 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000181 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000182
183 static const int NUM_TESTS = 512;
184
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000185 GrRandom random;
186 for (int t = 0; t < NUM_TESTS; ++t) {
187
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000188#if 0
189 GrPrintf("\nTest Program %d\n-------------\n", t);
190 static const int stop = -1;
191 if (t == stop) {
192 int breakpointhere = 9;
193 }
194#endif
195
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000196 pdesc.fVertexLayout = 0;
197 pdesc.fEmitsPointSize = random.nextF() > .5f;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000198 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000199 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000200
bsalomon@google.com74b98712011-11-11 19:46:16 +0000201 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000202
bsalomon@google.com74b98712011-11-11 19:46:16 +0000203 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000204
bsalomon@google.com74b98712011-11-11 19:46:16 +0000205 pdesc.fVertexLayout |= random_bool(&random) ?
bsalomon@google.coma3108262011-10-10 14:08:47 +0000206 GrDrawTarget::kCoverage_VertexLayoutBit :
207 0;
208
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000209#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com6f92f182011-09-29 15:05:48 +0000210 pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport &&
bsalomon@google.com74b98712011-11-11 19:46:16 +0000211 random_bool(&random);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000212#endif
bsalomon@google.com74b98712011-11-11 19:46:16 +0000213 pdesc.fOutputPM = random_int(&random, ProgramDesc::kOutputPMCnt);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000214
bsalomon@google.com74b98712011-11-11 19:46:16 +0000215 bool edgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000216 if (edgeAA) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000217 bool vertexEdgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000218 if (vertexEdgeAA) {
219 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000220 if (this->getCaps().fShaderDerivativeSupport) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000221 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000222 } else {
tomhudson@google.com93813632011-10-27 20:21:16 +0000223 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000224 }
225 pdesc.fEdgeAANumEdges = 0;
226 } else {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000227 pdesc.fEdgeAANumEdges = random_int(&random, 1, this->getMaxEdges());
228 pdesc.fEdgeAAConcave = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000229 }
230 } else {
231 pdesc.fEdgeAANumEdges = 0;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000232 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000233
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000234 pdesc.fColorMatrixEnabled = random_bool(&random);
235
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000236 if (this->getCaps().fDualSourceBlendingSupport) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000237 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000238 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000239 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000240 }
241
tomhudson@google.com93813632011-10-27 20:21:16 +0000242 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000243 // enable the stage?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000244 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000245 // use separate tex coords?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000246 if (random_bool(&random)) {
247 int t = random_int(&random, GrDrawState::kMaxTexCoords);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000248 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
249 } else {
250 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
251 }
252 }
253 // use text-formatted verts?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000254 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000255 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
256 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000257 StageDesc& stage = pdesc.fStages[s];
bsalomon@google.com74b98712011-11-11 19:46:16 +0000258 stage.fOptFlags = STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
259 stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];
260 stage.fCoordMapping = random_int(&random, StageDesc::kCoordMappingCnt);
261 stage.fFetchMode = random_int(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000262 // convolution shaders don't work with persp tex matrix
263 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) {
264 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
265 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000266 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.com74b98712011-11-11 19:46:16 +0000267 switch (stage.fFetchMode) {
268 case StageDesc::kSingle_FetchMode:
269 stage.fKernelWidth = 0;
270 break;
271 case StageDesc::kConvolution_FetchMode:
272 stage.fKernelWidth = random_int(&random, 2, 8);
273 stage.fInConfigFlags &= ~StageDesc::kMulRGBByAlpha_InConfigFlag;
274 break;
275 case StageDesc::k2x2_FetchMode:
276 stage.fKernelWidth = 0;
277 stage.fInConfigFlags &= ~StageDesc::kMulRGBByAlpha_InConfigFlag;
278 break;
279 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000280 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000281 CachedData cachedData;
bsalomon@google.com96399942012-02-13 14:39:16 +0000282 if (!program.genProgram(this->glContextInfo(), &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000283 return false;
284 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000285 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000286 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000287 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000288}
289
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000290GrGpuGLShaders::GrGpuGLShaders(const GrGLContextInfo& ctxInfo)
291 : GrGpuGL(ctxInfo) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000292
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000293 // Enable supported shader-related caps
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000294 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000295 fCaps.fDualSourceBlendingSupport =
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000296 this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000297 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000298 fCaps.fShaderDerivativeSupport = true;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000299 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
300 fCaps.fGeometryShaderSupport =
301 this->glVersion() >= GR_GL_VER(3,2) &&
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000302 this->glslGeneration() >= k150_GrGLSLGeneration;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000303 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000304 fCaps.fShaderDerivativeSupport =
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000305 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000306 }
junov@google.comf93e7172011-03-31 21:26:24 +0000307
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000308 GR_GL_GetIntegerv(this->glInterface(),
309 GR_GL_MAX_VERTEX_ATTRIBS,
310 &fMaxVertexAttribs);
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000311
junov@google.comf93e7172011-03-31 21:26:24 +0000312 fProgramData = NULL;
bsalomon@google.com96399942012-02-13 14:39:16 +0000313 fProgramCache = new ProgramCache(this->glContextInfo());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000314
315#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000316 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000317#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000318}
319
320GrGpuGLShaders::~GrGpuGLShaders() {
321 delete fProgramCache;
322}
323
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000324const GrMatrix& GrGpuGLShaders::getHWViewMatrix() {
325 GrAssert(fProgramData);
326
327 if (GrGLProgram::kSetAsAttribute ==
328 fProgramData->fUniLocations.fViewMatrixUni) {
329 return fHWDrawState.getViewMatrix();
330 } else {
331 return fProgramData->fViewMatrix;
332 }
333}
334
335void GrGpuGLShaders::recordHWViewMatrix(const GrMatrix& matrix) {
336 GrAssert(fProgramData);
337 if (GrGLProgram::kSetAsAttribute ==
338 fProgramData->fUniLocations.fViewMatrixUni) {
339 fHWDrawState.setViewMatrix(matrix);
340 } else {
341 fProgramData->fViewMatrix = matrix;
342 }
343}
344
junov@google.comf93e7172011-03-31 21:26:24 +0000345const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000346 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000347
348 if (GrGLProgram::kSetAsAttribute ==
349 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000350 return fHWDrawState.getSampler(stage).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000351 } else {
352 return fProgramData->fTextureMatrices[stage];
353 }
junov@google.comf93e7172011-03-31 21:26:24 +0000354}
355
356void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000357 GrAssert(fProgramData);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000358 if (GrGLProgram::kSetAsAttribute ==
bsalomon@google.com91961302011-05-09 18:39:58 +0000359 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000360 *fHWDrawState.sampler(stage)->matrix() = matrix;
bsalomon@google.com91961302011-05-09 18:39:58 +0000361 } else {
362 fProgramData->fTextureMatrices[stage] = matrix;
363 }
junov@google.comf93e7172011-03-31 21:26:24 +0000364}
365
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000366void GrGpuGLShaders::onResetContext() {
367 INHERITED::onResetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000368
junov@google.comf93e7172011-03-31 21:26:24 +0000369 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000370
371 // Third party GL code may have left vertex attributes enabled. Some GL
372 // implementations (osmesa) may read vetex attributes that are not required
373 // by the current shader. Therefore, we have to ensure that only the
374 // attributes we require for the current draw are enabled or we may cause an
375 // invalid read.
376
377 // Disable all vertex layout bits so that next flush will assume all
378 // optional vertex attributes are disabled.
379 fHWGeometryState.fVertexLayout = 0;
380
381 // We always use the this attribute and assume it is always enabled.
382 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
383 GL_CALL(EnableVertexAttribArray(posAttrIdx));
384 // Disable all other vertex attributes.
385 for (int va = 0; va < fMaxVertexAttribs; ++va) {
386 if (va != posAttrIdx) {
387 GL_CALL(DisableVertexAttribArray(va));
388 }
junov@google.comf93e7172011-03-31 21:26:24 +0000389 }
junov@google.comf93e7172011-03-31 21:26:24 +0000390
391 fHWProgramID = 0;
392}
393
394void GrGpuGLShaders::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000395 const GrMatrix& vm = this->getDrawState().getViewMatrix();
396 if (GrGpuGLShaders::getHWViewMatrix() != vm) {
junov@google.comf93e7172011-03-31 21:26:24 +0000397
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000398 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
399 GrAssert(NULL != rt);
400 GrMatrix m;
401 m.setAll(
402 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
403 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
404 0, 0, GrMatrix::I()[8]);
405 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000406
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000407 // ES doesn't allow you to pass true to the transpose param,
408 // so do our own transpose
409 GrGLfloat mt[] = {
410 GrScalarToFloat(m[GrMatrix::kMScaleX]),
411 GrScalarToFloat(m[GrMatrix::kMSkewY]),
412 GrScalarToFloat(m[GrMatrix::kMPersp0]),
413 GrScalarToFloat(m[GrMatrix::kMSkewX]),
414 GrScalarToFloat(m[GrMatrix::kMScaleY]),
415 GrScalarToFloat(m[GrMatrix::kMPersp1]),
416 GrScalarToFloat(m[GrMatrix::kMTransX]),
417 GrScalarToFloat(m[GrMatrix::kMTransY]),
418 GrScalarToFloat(m[GrMatrix::kMPersp2])
419 };
420
421 if (GrGLProgram::kSetAsAttribute ==
422 fProgramData->fUniLocations.fViewMatrixUni) {
423 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
424 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
425 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
426 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
427 } else {
428 GrAssert(GrGLProgram::kUnusedUniform !=
429 fProgramData->fUniLocations.fViewMatrixUni);
430 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
431 1, false, mt));
432 }
433 this->recordHWViewMatrix(vm);
bsalomon@google.com91961302011-05-09 18:39:58 +0000434 }
junov@google.comf93e7172011-03-31 21:26:24 +0000435}
436
junov@google.com6acc9b32011-05-16 18:32:07 +0000437void GrGpuGLShaders::flushTextureDomain(int s) {
438 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000439 const GrDrawState& drawState = this->getDrawState();
junov@google.com6acc9b32011-05-16 18:32:07 +0000440 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000441 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
junov@google.com6acc9b32011-05-16 18:32:07 +0000442
twiz@google.com76b82742011-06-02 20:30:02 +0000443 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
444 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000445
junov@google.com2f839402011-05-24 15:13:01 +0000446 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000447
junov@google.com2f839402011-05-24 15:13:01 +0000448 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000449 GrScalarToFloat(texDom.left()),
450 GrScalarToFloat(texDom.top()),
451 GrScalarToFloat(texDom.right()),
452 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000453 };
454
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000455 const GrGLTexture* texture =
456 static_cast<const GrGLTexture*>(drawState.getTexture(s));
junov@google.com2f839402011-05-24 15:13:01 +0000457 GrGLTexture::Orientation orientation = texture->orientation();
458
459 // vertical flip if necessary
460 if (GrGLTexture::kBottomUp_Orientation == orientation) {
461 values[1] = 1.0f - values[1];
462 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000463 // The top and bottom were just flipped, so correct the ordering
464 // of elements so that values = (l, t, r, b).
465 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000466 }
467
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000468 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000469 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000470 }
471}
472
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000473void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000474 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000475 const GrDrawState& drawState = this->getDrawState();
476 const GrGLTexture* texture =
477 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000478 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000479 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000480 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000481 this->getHWSamplerMatrix(s) != drawState.getSampler(s).getMatrix())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000482
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000483 GrMatrix m = drawState.getSampler(s).getMatrix();
484 GrSamplerState::SampleMode mode =
485 drawState.getSampler(s).getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000486 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000487
bsalomon@google.com91961302011-05-09 18:39:58 +0000488 // ES doesn't allow you to pass true to the transpose param,
489 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000490 GrGLfloat mt[] = {
491 GrScalarToFloat(m[GrMatrix::kMScaleX]),
492 GrScalarToFloat(m[GrMatrix::kMSkewY]),
493 GrScalarToFloat(m[GrMatrix::kMPersp0]),
494 GrScalarToFloat(m[GrMatrix::kMSkewX]),
495 GrScalarToFloat(m[GrMatrix::kMScaleY]),
496 GrScalarToFloat(m[GrMatrix::kMPersp1]),
497 GrScalarToFloat(m[GrMatrix::kMTransX]),
498 GrScalarToFloat(m[GrMatrix::kMTransY]),
499 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000500 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000501
bsalomon@google.com91961302011-05-09 18:39:58 +0000502 if (GrGLProgram::kSetAsAttribute ==
503 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
504 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000505 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
506 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
507 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000508 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000509 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000510 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000511 this->recordHWSamplerMatrix(s, drawState.getSampler(s).getMatrix());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000512 }
513 }
junov@google.comf93e7172011-03-31 21:26:24 +0000514}
515
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000516void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000517
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000518 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000519 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.com91961302011-05-09 18:39:58 +0000520 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000521 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
522 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
523 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000524
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000525 GrScalar centerX1 = sampler.getRadial2CenterX1();
526 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000527
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000528 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000529
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000530 // when were in the degenerate (linear) case the second
531 // value will be INF but the program doesn't read it. (We
532 // use the same 6 uniforms even though we don't need them
533 // all in the linear case just to keep the code complexity
534 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000535 float values[6] = {
536 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000537 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000538 GrScalarToFloat(centerX1),
539 GrScalarToFloat(radius0),
540 GrScalarToFloat(GrMul(radius0, radius0)),
541 sampler.isRadial2PosRoot() ? 1.f : -1.f
542 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000543 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000544 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
545 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
546 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
547 }
548}
549
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000550void GrGpuGLShaders::flushConvolution(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000551 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000552 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
553 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000554 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
555 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000556 }
557 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
558 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000559 GL_CALL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000560 }
561}
562
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000563void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000564 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000565 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000566 const GrGLTexture* texture =
567 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000568 if (texture->width() != fProgramData->fTextureWidth[s] ||
569 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000570
bsalomon@google.com99621082011-11-15 16:47:16 +0000571 float texelSize[] = {1.f / texture->width(),
572 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000573 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000574 fProgramData->fTextureWidth[s] = texture->width();
575 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000576 }
577 }
junov@google.comf93e7172011-03-31 21:26:24 +0000578}
579
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000580void GrGpuGLShaders::flushEdgeAAData() {
581 const int& uni = fProgramData->fUniLocations.fEdgesUni;
582 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000583 int count = this->getDrawState().getNumAAEdges();
tomhudson@google.com93813632011-10-27 20:21:16 +0000584 GrDrawState::Edge edges[GrDrawState::kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000585 // Flip the edges in Y
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000586 float height =
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000587 static_cast<float>(this->getDrawState().getRenderTarget()->height());
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000588 for (int i = 0; i < count; ++i) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000589 edges[i] = this->getDrawState().getAAEdges()[i];
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000590 float b = edges[i].fY;
591 edges[i].fY = -b;
592 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000593 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000594 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000595 }
596}
597
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000598void GrGpuGLShaders::flushColorMatrix() {
599 const ProgramDesc& desc = fCurrentProgram.getDesc();
600 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
601 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
602 if (GrGLProgram::kUnusedUniform != matrixUni
603 && GrGLProgram::kUnusedUniform != vecUni) {
604 const float* m = this->getDrawState().getColorMatrix();
605 GrGLfloat mt[] = {
606 m[0], m[5], m[10], m[15],
607 m[1], m[6], m[11], m[16],
608 m[2], m[7], m[12], m[17],
609 m[3], m[8], m[13], m[18],
610 };
611 static float scale = 1.0f / 255.0f;
612 GrGLfloat vec[] = {
613 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
614 };
615 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
616 GL_CALL(Uniform4fv(vecUni, 1, vec));
617 }
618}
619
Scroggo01b87ec2011-05-11 18:05:38 +0000620static const float ONE_OVER_255 = 1.f / 255.f;
621
622#define GR_COLOR_TO_VEC4(color) {\
623 GrColorUnpackR(color) * ONE_OVER_255,\
624 GrColorUnpackG(color) * ONE_OVER_255,\
625 GrColorUnpackB(color) * ONE_OVER_255,\
626 GrColorUnpackA(color) * ONE_OVER_255 \
627}
628
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000629void GrGpuGLShaders::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000630 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000631 const GrDrawState& drawState = this->getDrawState();
632
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000633 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000634 // color will be specified per-vertex as an attribute
635 // invalidate the const vertex attrib color
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000636 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000637 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000638 switch (desc.fColorInput) {
639 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000640 if (fHWDrawState.getColor() != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000641 // OpenGL ES only supports the float varieties of
642 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000643 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000644 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
645 c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000646 fHWDrawState.setColor(color);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000647 }
648 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000649 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000650 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000651 // OpenGL ES doesn't support unsigned byte varieties of
652 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000653 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000654 GrAssert(GrGLProgram::kUnusedUniform !=
655 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000656 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
657 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000658 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000659 }
660 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000661 case ProgramDesc::kSolidWhite_ColorInput:
662 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000663 break;
664 default:
665 GrCrash("Unknown color type.");
666 }
667 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000668 if (fProgramData->fUniLocations.fColorFilterUni
669 != GrGLProgram::kUnusedUniform
670 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000671 != drawState.getColorFilterColor()) {
672 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000673 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000674 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000675 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000676}
677
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000678void GrGpuGLShaders::flushCoverage(GrColor coverage) {
679 const ProgramDesc& desc = fCurrentProgram.getDesc();
680 const GrDrawState& drawState = this->getDrawState();
681
682
683 if (this->getGeomSrc().fVertexLayout & kCoverage_VertexLayoutBit) {
684 // coverage will be specified per-vertex as an attribute
685 // invalidate the const vertex attrib coverage
686 fHWDrawState.setCoverage4(GrColor_ILLEGAL);
687 } else {
688 switch (desc.fCoverageInput) {
689 case ProgramDesc::kAttribute_ColorInput:
690 if (fHWDrawState.getCoverage() != coverage) {
691 // OpenGL ES only supports the float varieties of
692 // glVertexAttrib
693 float c[] = GR_COLOR_TO_VEC4(coverage);
694 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
695 c));
696 fHWDrawState.setCoverage(coverage);
697 }
698 break;
699 case ProgramDesc::kUniform_ColorInput:
700 if (fProgramData->fCoverage != coverage) {
701 // OpenGL ES doesn't support unsigned byte varieties of
702 // glUniform
703 float c[] = GR_COLOR_TO_VEC4(coverage);
704 GrAssert(GrGLProgram::kUnusedUniform !=
705 fProgramData->fUniLocations.fCoverageUni);
706 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
707 1, c));
708 fProgramData->fCoverage = coverage;
709 }
710 break;
711 case ProgramDesc::kSolidWhite_ColorInput:
712 case ProgramDesc::kTransBlack_ColorInput:
713 break;
714 default:
715 GrCrash("Unknown coverage type.");
716 }
717 }
718}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000719
junov@google.comf93e7172011-03-31 21:26:24 +0000720bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
721 if (!flushGLStateCommon(type)) {
722 return false;
723 }
724
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000725 const GrDrawState& drawState = this->getDrawState();
726
junov@google.comf93e7172011-03-31 21:26:24 +0000727 if (fDirtyFlags.fRenderTargetChanged) {
728 // our coords are in pixel space and the GL matrices map to NDC
729 // so if the viewport changed, our matrix is now wrong.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000730 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
junov@google.comf93e7172011-03-31 21:26:24 +0000731 // we assume all shader matrices may be wrong after viewport changes
732 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000733 }
734
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000735 GrBlendCoeff srcCoeff;
736 GrBlendCoeff dstCoeff;
737 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
738 if (kSkipDraw_BlendOptFlag & blendOpts) {
739 return false;
740 }
741
742 this->buildProgram(type, blendOpts, dstCoeff);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000743 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000744 if (NULL == fProgramData) {
745 GrAssert(!"Failed to create program!");
746 return false;
747 }
junov@google.comf93e7172011-03-31 21:26:24 +0000748
749 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000750 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000751 fHWProgramID = fProgramData->fProgramID;
752 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000753 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
754 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000755
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000756 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000757 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000758 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
759 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000760 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000761 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
762 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000763 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000764 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000765 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000766 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000767 }
768 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000769 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000770
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000771 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000772
tomhudson@google.com93813632011-10-27 20:21:16 +0000773 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000774 if (this->isStageEnabled(s)) {
775 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000776
bsalomon@google.com40d92932011-12-13 18:40:47 +0000777 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000778
bsalomon@google.com40d92932011-12-13 18:40:47 +0000779 this->flushConvolution(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000780
bsalomon@google.com40d92932011-12-13 18:40:47 +0000781 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000782
bsalomon@google.com40d92932011-12-13 18:40:47 +0000783 this->flushTextureDomain(s);
784 }
junov@google.comf93e7172011-03-31 21:26:24 +0000785 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000786 this->flushEdgeAAData();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000787 this->flushColorMatrix();
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000788 resetDirtyFlags();
junov@google.comf93e7172011-03-31 21:26:24 +0000789 return true;
790}
791
792void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000793}
794
795void GrGpuGLShaders::setupGeometry(int* startVertex,
796 int* startIndex,
797 int vertexCount,
798 int indexCount) {
799
800 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000801 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000802 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000803 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000804
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000805 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
806 this->getGeomSrc().fVertexLayout,
807 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000808 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000809 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000810 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000811 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000812 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000813 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000814 int oldEdgeOffset;
815
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000816 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
817 fHWGeometryState.fVertexLayout,
818 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000819 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000820 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000821 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000822 bool indexed = NULL != startIndex;
823
824 int extraVertexOffset;
825 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000826 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000827
828 GrGLenum scalarType;
829 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000830 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000831 scalarType = GrGLTextType;
832 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
833 } else {
834 scalarType = GrGLType;
835 texCoordNorm = false;
836 }
837
838 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
839 *startVertex = 0;
840 if (indexed) {
841 *startIndex += extraIndexOffset;
842 }
843
844 // all the Pointers must be set if any of these are true
845 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
846 vertexOffset != fHWGeometryState.fVertexOffset ||
847 newStride != oldStride;
848
849 // position and tex coord offsets change if above conditions are true
850 // or the type/normalization changed based on text vs nontext type coords.
851 bool posAndTexChange = allOffsetsChange ||
852 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
853 (kTextFormat_VertexLayoutBit &
854 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000855 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000856
857 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000858 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000859 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000860 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000861 fHWGeometryState.fVertexOffset = vertexOffset;
862 }
863
tomhudson@google.com93813632011-10-27 20:21:16 +0000864 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000865 if (newTexCoordOffsets[t] > 0) {
866 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000867 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000868 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000869 GL_CALL(EnableVertexAttribArray(idx));
870 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000871 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000872 } else if (posAndTexChange ||
873 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000874 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 }
877 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000878 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000879 }
880 }
881
882 if (newColorOffset > 0) {
883 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000884 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000885 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000886 GL_CALL(EnableVertexAttribArray(idx));
887 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000888 true, newStride, colorOffset));
889 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000890 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000891 true, newStride, colorOffset));
892 }
893 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000894 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000895 }
896
bsalomon@google.coma3108262011-10-10 14:08:47 +0000897 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000898 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000899 int idx = GrGLProgram::CoverageAttributeIdx();
900 if (oldCoverageOffset <= 0) {
901 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000902 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000903 true, newStride, coverageOffset));
904 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000905 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000906 true, newStride, coverageOffset));
907 }
908 } else if (oldCoverageOffset > 0) {
909 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
910 }
911
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000912 if (newEdgeOffset > 0) {
913 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
914 int idx = GrGLProgram::EdgeAttributeIdx();
915 if (oldEdgeOffset <= 0) {
916 GL_CALL(EnableVertexAttribArray(idx));
917 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
918 false, newStride, edgeOffset));
919 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
920 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
921 false, newStride, edgeOffset));
922 }
923 } else if (oldEdgeOffset > 0) {
924 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
925 }
926
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000927 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000928 fHWGeometryState.fArrayPtrsDirty = false;
929}
930
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000931void GrGpuGLShaders::buildProgram(GrPrimitiveType type,
932 BlendOptFlags blendOpts,
933 GrBlendCoeff dstCoeff) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000934 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000935 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000936
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000937 // This should already have been caught
938 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
939
940 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
941
942 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
943 kEmitCoverage_BlendOptFlag));
944
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000945 // The descriptor is used as a cache key. Thus when a field of the
946 // descriptor will not affect program generation (because of the vertex
947 // layout in use or other descriptor field settings) it should be set
948 // to a canonical value to avoid duplicate programs with different keys.
949
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000950 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000951 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000952
953 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
954
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000955 bool requiresAttributeColors =
956 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000957 bool requiresAttributeCoverage =
958 !skipCoverage && SkToBool(desc.fVertexLayout &
959 kCoverage_VertexLayoutBit);
960
961 // fColorInput/fCoverageInput records how colors are specified for the.
962 // program. So we strip the bits from the layout to avoid false negatives
963 // when searching for an existing program in the cache.
964 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000965
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000966 desc.fColorFilterXfermode = skipColor ?
967 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000968 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000969
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000970 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
971
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000972 // no reason to do edge aa or look at per-vertex coverage if coverage is
973 // ignored
974 if (skipCoverage) {
975 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
976 kCoverage_VertexLayoutBit);
977 }
978
979 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
980 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
981 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000982 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000983 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000984 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000985 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000986 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000987 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000988 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000989 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000990 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000991 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000992
993 bool covIsSolidWhite = !requiresAttributeCoverage &&
994 0xffffffff == drawState.getCoverage();
995
996 if (skipCoverage) {
997 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
998 } else if (covIsSolidWhite) {
999 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
1000 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
1001 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
1002 } else {
1003 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
1004 }
junov@google.comf93e7172011-03-31 21:26:24 +00001005
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001006 desc.fEdgeAANumEdges = skipCoverage ? 0 : drawState.getNumAAEdges();
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001007 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001008 drawState.isConcaveEdgeAAState();
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +00001009
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001010 int lastEnabledStage = -1;
1011
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001012 if (!skipCoverage && (desc.fVertexLayout &
1013 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001014 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001015 } else {
1016 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +00001017 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001018 }
1019
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001020 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001021 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001022
1023 stage.fOptFlags = 0;
1024 stage.setEnabled(this->isStageEnabled(s));
1025
1026 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
1027 skipCoverage;
1028
1029 if (!skip && stage.isEnabled()) {
1030 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001031 const GrGLTexture* texture =
1032 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001033 GrAssert(NULL != texture);
1034 const GrSamplerState& sampler = drawState.getSampler(s);
1035 // we matrix to invert when orientation is TopDown, so make sure
1036 // we aren't in that case before flagging as identity.
1037 if (TextureMatrixIsIdentity(texture, sampler)) {
1038 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
1039 } else if (!sampler.getMatrix().hasPerspective()) {
1040 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
1041 }
1042 switch (sampler.getSampleMode()) {
1043 case GrSamplerState::kNormal_SampleMode:
1044 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
1045 break;
1046 case GrSamplerState::kRadial_SampleMode:
1047 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
1048 break;
1049 case GrSamplerState::kRadial2_SampleMode:
1050 if (sampler.radial2IsDegenerate()) {
1051 stage.fCoordMapping =
1052 StageDesc::kRadial2GradientDegenerate_CoordMapping;
1053 } else {
1054 stage.fCoordMapping =
1055 StageDesc::kRadial2Gradient_CoordMapping;
1056 }
1057 break;
1058 case GrSamplerState::kSweep_SampleMode:
1059 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
1060 break;
1061 default:
1062 GrCrash("Unexpected sample mode!");
1063 break;
1064 }
1065
1066 switch (sampler.getFilter()) {
1067 // these both can use a regular texture2D()
1068 case GrSamplerState::kNearest_Filter:
1069 case GrSamplerState::kBilinear_Filter:
1070 stage.fFetchMode = StageDesc::kSingle_FetchMode;
1071 break;
1072 // performs 4 texture2D()s
1073 case GrSamplerState::k4x4Downsample_Filter:
1074 stage.fFetchMode = StageDesc::k2x2_FetchMode;
1075 break;
1076 // performs fKernelWidth texture2D()s
1077 case GrSamplerState::kConvolution_Filter:
1078 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
1079 break;
1080 default:
1081 GrCrash("Unexpected filter!");
1082 break;
1083 }
1084
1085 if (sampler.hasTextureDomain()) {
1086 GrAssert(GrSamplerState::kClamp_WrapMode ==
1087 sampler.getWrapX() &&
1088 GrSamplerState::kClamp_WrapMode ==
1089 sampler.getWrapY());
1090 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
1091 }
1092
1093 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001094 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001095 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1096 // if we don't have texture swizzle support then
1097 // the shader must do an alpha smear after reading
1098 // the texture
1099 stage.fInConfigFlags |= StageDesc::kSmearAlpha_InConfigFlag;
1100 } else if (sampler.swapsRAndB()) {
1101 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
1102 }
1103 }
1104 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
1105 stage.fInConfigFlags |= StageDesc::kMulRGBByAlpha_InConfigFlag;
1106 }
1107
1108 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) {
1109 stage.fKernelWidth = sampler.getKernelWidth();
1110 } else {
1111 stage.fKernelWidth = 0;
1112 }
junov@google.comf93e7172011-03-31 21:26:24 +00001113 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001114 stage.fOptFlags = 0;
1115 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1116 stage.fInConfigFlags = 0;
1117 stage.fFetchMode = (StageDesc::FetchMode) 0;
1118 stage.fKernelWidth = 0;
junov@google.comf93e7172011-03-31 21:26:24 +00001119 }
1120 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001121
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001122 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001123 desc.fOutputPM = ProgramDesc::kNo_OutputPM;
1124 } else {
1125 desc.fOutputPM = ProgramDesc::kYes_OutputPM;
1126 }
1127
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001128 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001129
1130 // currently the experimental GS will only work with triangle prims
1131 // (and it doesn't do anything other than pass through values from
1132 // the VS to the FS anyway).
1133#if 0 && GR_GL_EXPERIMENTAL_GS
1134 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1135#endif
1136
bsalomon@google.coma3108262011-10-10 14:08:47 +00001137 // we want to avoid generating programs with different "first cov stage"
1138 // values when they would compute the same result.
1139 // We set field in the desc to kNumStages when either there are no
1140 // coverage stages or the distinction between coverage and color is
1141 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001142 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +00001143 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001144 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001145 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001146 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +00001147 }
1148
1149 // other coverage inputs
1150 if (!hasCoverage) {
1151 hasCoverage =
1152 desc.fEdgeAANumEdges ||
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001153 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +00001154 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1155 }
1156
1157 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001158 // color filter is applied between color/coverage computation
1159 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001160 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001161 }
1162
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001163 if (this->getCaps().fDualSourceBlendingSupport &&
1164 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1165 kCoverageAsAlpha_BlendOptFlag))) {
1166 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001167 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001168 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001169 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001170 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001171 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1172 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001173 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001174 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001175 } else if (kSC_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::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001179 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001180 }
1181 }
1182 }
junov@google.comf93e7172011-03-31 21:26:24 +00001183}