blob: db7e3a7f7629e0ecce5667f15a7999b6669c87a5 [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
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000264 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode ||
265 stage.fFetchMode == StageDesc::kDilate_FetchMode ||
266 stage.fFetchMode == StageDesc::kErode_FetchMode) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000267 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
268 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000269 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000270 static const uint32_t kMulByAlphaMask =
271 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
272 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000273 switch (stage.fFetchMode) {
274 case StageDesc::kSingle_FetchMode:
275 stage.fKernelWidth = 0;
276 break;
277 case StageDesc::kConvolution_FetchMode:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000278 case StageDesc::kDilate_FetchMode:
279 case StageDesc::kErode_FetchMode:
bsalomon@google.com74b98712011-11-11 19:46:16 +0000280 stage.fKernelWidth = random_int(&random, 2, 8);
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000281 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000282 break;
283 case StageDesc::k2x2_FetchMode:
284 stage.fKernelWidth = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000285 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000286 break;
287 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000288 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000289 CachedData cachedData;
bsalomon@google.com96399942012-02-13 14:39:16 +0000290 if (!program.genProgram(this->glContextInfo(), &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000291 return false;
292 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000293 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000294 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000295 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000296}
297
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000298GrGpuGLShaders::GrGpuGLShaders(const GrGLContextInfo& ctxInfo)
299 : GrGpuGL(ctxInfo) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000300
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000301 // Enable supported shader-related caps
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000302 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000303 fCaps.fDualSourceBlendingSupport =
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000304 this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000305 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000306 fCaps.fShaderDerivativeSupport = true;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000307 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
308 fCaps.fGeometryShaderSupport =
309 this->glVersion() >= GR_GL_VER(3,2) &&
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000310 this->glslGeneration() >= k150_GrGLSLGeneration;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000311 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000312 fCaps.fShaderDerivativeSupport =
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000313 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000314 }
junov@google.comf93e7172011-03-31 21:26:24 +0000315
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000316 GR_GL_GetIntegerv(this->glInterface(),
317 GR_GL_MAX_VERTEX_ATTRIBS,
318 &fMaxVertexAttribs);
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000319
junov@google.comf93e7172011-03-31 21:26:24 +0000320 fProgramData = NULL;
bsalomon@google.com96399942012-02-13 14:39:16 +0000321 fProgramCache = new ProgramCache(this->glContextInfo());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000322
323#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000324 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000325#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000326}
327
328GrGpuGLShaders::~GrGpuGLShaders() {
329 delete fProgramCache;
330}
331
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000332const GrMatrix& GrGpuGLShaders::getHWViewMatrix() {
333 GrAssert(fProgramData);
334
335 if (GrGLProgram::kSetAsAttribute ==
336 fProgramData->fUniLocations.fViewMatrixUni) {
337 return fHWDrawState.getViewMatrix();
338 } else {
339 return fProgramData->fViewMatrix;
340 }
341}
342
343void GrGpuGLShaders::recordHWViewMatrix(const GrMatrix& matrix) {
344 GrAssert(fProgramData);
345 if (GrGLProgram::kSetAsAttribute ==
346 fProgramData->fUniLocations.fViewMatrixUni) {
347 fHWDrawState.setViewMatrix(matrix);
348 } else {
349 fProgramData->fViewMatrix = matrix;
350 }
351}
352
junov@google.comf93e7172011-03-31 21:26:24 +0000353const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000354 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000355
356 if (GrGLProgram::kSetAsAttribute ==
357 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000358 return fHWDrawState.getSampler(stage).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000359 } else {
360 return fProgramData->fTextureMatrices[stage];
361 }
junov@google.comf93e7172011-03-31 21:26:24 +0000362}
363
364void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000365 GrAssert(fProgramData);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000366 if (GrGLProgram::kSetAsAttribute ==
bsalomon@google.com91961302011-05-09 18:39:58 +0000367 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000368 *fHWDrawState.sampler(stage)->matrix() = matrix;
bsalomon@google.com91961302011-05-09 18:39:58 +0000369 } else {
370 fProgramData->fTextureMatrices[stage] = matrix;
371 }
junov@google.comf93e7172011-03-31 21:26:24 +0000372}
373
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000374void GrGpuGLShaders::onResetContext() {
375 INHERITED::onResetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000376
junov@google.comf93e7172011-03-31 21:26:24 +0000377 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000378
379 // Third party GL code may have left vertex attributes enabled. Some GL
380 // implementations (osmesa) may read vetex attributes that are not required
381 // by the current shader. Therefore, we have to ensure that only the
382 // attributes we require for the current draw are enabled or we may cause an
383 // invalid read.
384
385 // Disable all vertex layout bits so that next flush will assume all
386 // optional vertex attributes are disabled.
387 fHWGeometryState.fVertexLayout = 0;
388
389 // We always use the this attribute and assume it is always enabled.
390 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
391 GL_CALL(EnableVertexAttribArray(posAttrIdx));
392 // Disable all other vertex attributes.
393 for (int va = 0; va < fMaxVertexAttribs; ++va) {
394 if (va != posAttrIdx) {
395 GL_CALL(DisableVertexAttribArray(va));
396 }
junov@google.comf93e7172011-03-31 21:26:24 +0000397 }
junov@google.comf93e7172011-03-31 21:26:24 +0000398
399 fHWProgramID = 0;
400}
401
402void GrGpuGLShaders::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000403 const GrMatrix& vm = this->getDrawState().getViewMatrix();
404 if (GrGpuGLShaders::getHWViewMatrix() != vm) {
junov@google.comf93e7172011-03-31 21:26:24 +0000405
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000406 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
407 GrAssert(NULL != rt);
408 GrMatrix m;
409 m.setAll(
410 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
411 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
412 0, 0, GrMatrix::I()[8]);
413 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000414
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000415 // ES doesn't allow you to pass true to the transpose param,
416 // so do our own transpose
417 GrGLfloat mt[] = {
418 GrScalarToFloat(m[GrMatrix::kMScaleX]),
419 GrScalarToFloat(m[GrMatrix::kMSkewY]),
420 GrScalarToFloat(m[GrMatrix::kMPersp0]),
421 GrScalarToFloat(m[GrMatrix::kMSkewX]),
422 GrScalarToFloat(m[GrMatrix::kMScaleY]),
423 GrScalarToFloat(m[GrMatrix::kMPersp1]),
424 GrScalarToFloat(m[GrMatrix::kMTransX]),
425 GrScalarToFloat(m[GrMatrix::kMTransY]),
426 GrScalarToFloat(m[GrMatrix::kMPersp2])
427 };
428
429 if (GrGLProgram::kSetAsAttribute ==
430 fProgramData->fUniLocations.fViewMatrixUni) {
431 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
432 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
433 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
434 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
435 } else {
436 GrAssert(GrGLProgram::kUnusedUniform !=
437 fProgramData->fUniLocations.fViewMatrixUni);
438 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
439 1, false, mt));
440 }
441 this->recordHWViewMatrix(vm);
bsalomon@google.com91961302011-05-09 18:39:58 +0000442 }
junov@google.comf93e7172011-03-31 21:26:24 +0000443}
444
junov@google.com6acc9b32011-05-16 18:32:07 +0000445void GrGpuGLShaders::flushTextureDomain(int s) {
446 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000447 const GrDrawState& drawState = this->getDrawState();
junov@google.com6acc9b32011-05-16 18:32:07 +0000448 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000449 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
junov@google.com6acc9b32011-05-16 18:32:07 +0000450
twiz@google.com76b82742011-06-02 20:30:02 +0000451 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
452 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000453
junov@google.com2f839402011-05-24 15:13:01 +0000454 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000455
junov@google.com2f839402011-05-24 15:13:01 +0000456 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000457 GrScalarToFloat(texDom.left()),
458 GrScalarToFloat(texDom.top()),
459 GrScalarToFloat(texDom.right()),
460 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000461 };
462
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000463 const GrGLTexture* texture =
464 static_cast<const GrGLTexture*>(drawState.getTexture(s));
junov@google.com2f839402011-05-24 15:13:01 +0000465 GrGLTexture::Orientation orientation = texture->orientation();
466
467 // vertical flip if necessary
468 if (GrGLTexture::kBottomUp_Orientation == orientation) {
469 values[1] = 1.0f - values[1];
470 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000471 // The top and bottom were just flipped, so correct the ordering
472 // of elements so that values = (l, t, r, b).
473 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000474 }
475
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000476 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000477 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000478 }
479}
480
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000481void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000482 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000483 const GrDrawState& drawState = this->getDrawState();
484 const GrGLTexture* texture =
485 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000486 if (NULL != texture) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000487 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000488 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000489 this->getHWSamplerMatrix(s) != drawState.getSampler(s).getMatrix())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000490
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000491 GrMatrix m = drawState.getSampler(s).getMatrix();
492 GrSamplerState::SampleMode mode =
493 drawState.getSampler(s).getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000494 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000495
bsalomon@google.com91961302011-05-09 18:39:58 +0000496 // ES doesn't allow you to pass true to the transpose param,
497 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000498 GrGLfloat mt[] = {
499 GrScalarToFloat(m[GrMatrix::kMScaleX]),
500 GrScalarToFloat(m[GrMatrix::kMSkewY]),
501 GrScalarToFloat(m[GrMatrix::kMPersp0]),
502 GrScalarToFloat(m[GrMatrix::kMSkewX]),
503 GrScalarToFloat(m[GrMatrix::kMScaleY]),
504 GrScalarToFloat(m[GrMatrix::kMPersp1]),
505 GrScalarToFloat(m[GrMatrix::kMTransX]),
506 GrScalarToFloat(m[GrMatrix::kMTransY]),
507 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000508 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000509
bsalomon@google.com91961302011-05-09 18:39:58 +0000510 if (GrGLProgram::kSetAsAttribute ==
511 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
512 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000513 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
514 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
515 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000516 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000517 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000518 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000519 this->recordHWSamplerMatrix(s, drawState.getSampler(s).getMatrix());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000520 }
521 }
junov@google.comf93e7172011-03-31 21:26:24 +0000522}
523
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000524void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000525
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000526 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000527 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.com91961302011-05-09 18:39:58 +0000528 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000529 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
530 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
531 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000532
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000533 GrScalar centerX1 = sampler.getRadial2CenterX1();
534 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000535
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000536 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000537
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000538 // when were in the degenerate (linear) case the second
539 // value will be INF but the program doesn't read it. (We
540 // use the same 6 uniforms even though we don't need them
541 // all in the linear case just to keep the code complexity
542 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000543 float values[6] = {
544 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000545 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000546 GrScalarToFloat(centerX1),
547 GrScalarToFloat(radius0),
548 GrScalarToFloat(GrMul(radius0, radius0)),
549 sampler.isRadial2PosRoot() ? 1.f : -1.f
550 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000551 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000552 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
553 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
554 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
555 }
556}
557
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000558void GrGpuGLShaders::flushConvolution(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000559 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000560 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
561 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000562 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
563 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000564 }
565 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
566 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000567 const GrGLTexture* texture =
568 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
569 float imageIncrement[2] = { 0 };
570 switch (sampler.getFilterDirection()) {
571 case GrSamplerState::kX_FilterDirection:
572 imageIncrement[0] = 1.0f / texture->width();
573 break;
574 case GrSamplerState::kY_FilterDirection:
575 imageIncrement[1] = 1.0f / texture->height();
576 break;
577 default:
578 GrCrash("Unknown filter direction.");
579 }
580 GL_CALL(Uniform2fv(imageIncrementUni, 1, imageIncrement));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000581 }
582}
583
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000584void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000585 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000586 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000587 const GrGLTexture* texture =
588 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000589 if (texture->width() != fProgramData->fTextureWidth[s] ||
590 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000591
bsalomon@google.com99621082011-11-15 16:47:16 +0000592 float texelSize[] = {1.f / texture->width(),
593 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000594 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000595 fProgramData->fTextureWidth[s] = texture->width();
596 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000597 }
598 }
junov@google.comf93e7172011-03-31 21:26:24 +0000599}
600
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000601void GrGpuGLShaders::flushEdgeAAData() {
602 const int& uni = fProgramData->fUniLocations.fEdgesUni;
603 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000604 int count = this->getDrawState().getNumAAEdges();
tomhudson@google.com93813632011-10-27 20:21:16 +0000605 GrDrawState::Edge edges[GrDrawState::kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000606 // Flip the edges in Y
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000607 float height =
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000608 static_cast<float>(this->getDrawState().getRenderTarget()->height());
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000609 for (int i = 0; i < count; ++i) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000610 edges[i] = this->getDrawState().getAAEdges()[i];
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000611 float b = edges[i].fY;
612 edges[i].fY = -b;
613 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000614 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000615 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000616 }
617}
618
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000619void GrGpuGLShaders::flushColorMatrix() {
620 const ProgramDesc& desc = fCurrentProgram.getDesc();
621 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
622 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
623 if (GrGLProgram::kUnusedUniform != matrixUni
624 && GrGLProgram::kUnusedUniform != vecUni) {
625 const float* m = this->getDrawState().getColorMatrix();
626 GrGLfloat mt[] = {
627 m[0], m[5], m[10], m[15],
628 m[1], m[6], m[11], m[16],
629 m[2], m[7], m[12], m[17],
630 m[3], m[8], m[13], m[18],
631 };
632 static float scale = 1.0f / 255.0f;
633 GrGLfloat vec[] = {
634 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
635 };
636 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
637 GL_CALL(Uniform4fv(vecUni, 1, vec));
638 }
639}
640
Scroggo01b87ec2011-05-11 18:05:38 +0000641static const float ONE_OVER_255 = 1.f / 255.f;
642
643#define GR_COLOR_TO_VEC4(color) {\
644 GrColorUnpackR(color) * ONE_OVER_255,\
645 GrColorUnpackG(color) * ONE_OVER_255,\
646 GrColorUnpackB(color) * ONE_OVER_255,\
647 GrColorUnpackA(color) * ONE_OVER_255 \
648}
649
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000650void GrGpuGLShaders::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000651 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000652 const GrDrawState& drawState = this->getDrawState();
653
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000654 if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000655 // color will be specified per-vertex as an attribute
656 // invalidate the const vertex attrib color
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000657 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000658 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000659 switch (desc.fColorInput) {
660 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000661 if (fHWDrawState.getColor() != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000662 // OpenGL ES only supports the float varieties of
663 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000664 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000665 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
666 c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000667 fHWDrawState.setColor(color);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000668 }
669 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000670 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000671 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000672 // OpenGL ES doesn't support unsigned byte varieties of
673 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000674 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000675 GrAssert(GrGLProgram::kUnusedUniform !=
676 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000677 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
678 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000679 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000680 }
681 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000682 case ProgramDesc::kSolidWhite_ColorInput:
683 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000684 break;
685 default:
686 GrCrash("Unknown color type.");
687 }
688 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000689 if (fProgramData->fUniLocations.fColorFilterUni
690 != GrGLProgram::kUnusedUniform
691 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000692 != drawState.getColorFilterColor()) {
693 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000694 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000695 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000696 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000697}
698
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000699void GrGpuGLShaders::flushCoverage(GrColor coverage) {
700 const ProgramDesc& desc = fCurrentProgram.getDesc();
701 const GrDrawState& drawState = this->getDrawState();
702
703
704 if (this->getGeomSrc().fVertexLayout & kCoverage_VertexLayoutBit) {
705 // coverage will be specified per-vertex as an attribute
706 // invalidate the const vertex attrib coverage
707 fHWDrawState.setCoverage4(GrColor_ILLEGAL);
708 } else {
709 switch (desc.fCoverageInput) {
710 case ProgramDesc::kAttribute_ColorInput:
711 if (fHWDrawState.getCoverage() != coverage) {
712 // OpenGL ES only supports the float varieties of
713 // glVertexAttrib
714 float c[] = GR_COLOR_TO_VEC4(coverage);
715 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
716 c));
717 fHWDrawState.setCoverage(coverage);
718 }
719 break;
720 case ProgramDesc::kUniform_ColorInput:
721 if (fProgramData->fCoverage != coverage) {
722 // OpenGL ES doesn't support unsigned byte varieties of
723 // glUniform
724 float c[] = GR_COLOR_TO_VEC4(coverage);
725 GrAssert(GrGLProgram::kUnusedUniform !=
726 fProgramData->fUniLocations.fCoverageUni);
727 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
728 1, c));
729 fProgramData->fCoverage = coverage;
730 }
731 break;
732 case ProgramDesc::kSolidWhite_ColorInput:
733 case ProgramDesc::kTransBlack_ColorInput:
734 break;
735 default:
736 GrCrash("Unknown coverage type.");
737 }
738 }
739}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000740
junov@google.comf93e7172011-03-31 21:26:24 +0000741bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
742 if (!flushGLStateCommon(type)) {
743 return false;
744 }
745
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000746 const GrDrawState& drawState = this->getDrawState();
747
junov@google.comf93e7172011-03-31 21:26:24 +0000748 if (fDirtyFlags.fRenderTargetChanged) {
749 // our coords are in pixel space and the GL matrices map to NDC
750 // so if the viewport changed, our matrix is now wrong.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000751 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
junov@google.comf93e7172011-03-31 21:26:24 +0000752 // we assume all shader matrices may be wrong after viewport changes
753 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000754 }
755
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000756 GrBlendCoeff srcCoeff;
757 GrBlendCoeff dstCoeff;
758 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
759 if (kSkipDraw_BlendOptFlag & blendOpts) {
760 return false;
761 }
762
763 this->buildProgram(type, blendOpts, dstCoeff);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000764 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000765 if (NULL == fProgramData) {
766 GrAssert(!"Failed to create program!");
767 return false;
768 }
junov@google.comf93e7172011-03-31 21:26:24 +0000769
770 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000771 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000772 fHWProgramID = fProgramData->fProgramID;
773 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000774 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
775 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000776
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000777 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000778 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000779 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
780 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000781 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000782 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
783 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000784 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000785 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000786 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000787 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000788 }
789 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000790 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000791
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000792 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000793
tomhudson@google.com93813632011-10-27 20:21:16 +0000794 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000795 if (this->isStageEnabled(s)) {
796 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000797
bsalomon@google.com40d92932011-12-13 18:40:47 +0000798 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000799
bsalomon@google.com40d92932011-12-13 18:40:47 +0000800 this->flushConvolution(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000801
bsalomon@google.com40d92932011-12-13 18:40:47 +0000802 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000803
bsalomon@google.com40d92932011-12-13 18:40:47 +0000804 this->flushTextureDomain(s);
805 }
junov@google.comf93e7172011-03-31 21:26:24 +0000806 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000807 this->flushEdgeAAData();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000808 this->flushColorMatrix();
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000809 resetDirtyFlags();
junov@google.comf93e7172011-03-31 21:26:24 +0000810 return true;
811}
812
813void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000814}
815
816void GrGpuGLShaders::setupGeometry(int* startVertex,
817 int* startIndex,
818 int vertexCount,
819 int indexCount) {
820
821 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000822 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000823 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000824 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000825
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000826 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
827 this->getGeomSrc().fVertexLayout,
828 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000829 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000830 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000831 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000832 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000833 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000834 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000835 int oldEdgeOffset;
836
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000837 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
838 fHWGeometryState.fVertexLayout,
839 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000840 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000841 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000842 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000843 bool indexed = NULL != startIndex;
844
845 int extraVertexOffset;
846 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000847 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000848
849 GrGLenum scalarType;
850 bool texCoordNorm;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000851 if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000852 scalarType = GrGLTextType;
853 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
854 } else {
855 scalarType = GrGLType;
856 texCoordNorm = false;
857 }
858
859 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
860 *startVertex = 0;
861 if (indexed) {
862 *startIndex += extraIndexOffset;
863 }
864
865 // all the Pointers must be set if any of these are true
866 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
867 vertexOffset != fHWGeometryState.fVertexOffset ||
868 newStride != oldStride;
869
870 // position and tex coord offsets change if above conditions are true
871 // or the type/normalization changed based on text vs nontext type coords.
872 bool posAndTexChange = allOffsetsChange ||
873 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
874 (kTextFormat_VertexLayoutBit &
875 (fHWGeometryState.fVertexLayout ^
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000876 this->getGeomSrc().fVertexLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000877
878 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000879 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000880 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000881 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000882 fHWGeometryState.fVertexOffset = vertexOffset;
883 }
884
tomhudson@google.com93813632011-10-27 20:21:16 +0000885 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000886 if (newTexCoordOffsets[t] > 0) {
887 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000888 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000889 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000890 GL_CALL(EnableVertexAttribArray(idx));
891 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000892 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000893 } else if (posAndTexChange ||
894 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000895 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000896 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000897 }
898 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000899 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000900 }
901 }
902
903 if (newColorOffset > 0) {
904 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000905 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000906 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000907 GL_CALL(EnableVertexAttribArray(idx));
908 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000909 true, newStride, colorOffset));
910 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000911 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000912 true, newStride, colorOffset));
913 }
914 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000915 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000916 }
917
bsalomon@google.coma3108262011-10-10 14:08:47 +0000918 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000919 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000920 int idx = GrGLProgram::CoverageAttributeIdx();
921 if (oldCoverageOffset <= 0) {
922 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000923 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000924 true, newStride, coverageOffset));
925 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000926 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000927 true, newStride, coverageOffset));
928 }
929 } else if (oldCoverageOffset > 0) {
930 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
931 }
932
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000933 if (newEdgeOffset > 0) {
934 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
935 int idx = GrGLProgram::EdgeAttributeIdx();
936 if (oldEdgeOffset <= 0) {
937 GL_CALL(EnableVertexAttribArray(idx));
938 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
939 false, newStride, edgeOffset));
940 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
941 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
942 false, newStride, edgeOffset));
943 }
944 } else if (oldEdgeOffset > 0) {
945 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
946 }
947
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000948 fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000949 fHWGeometryState.fArrayPtrsDirty = false;
950}
951
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000952void GrGpuGLShaders::buildProgram(GrPrimitiveType type,
953 BlendOptFlags blendOpts,
954 GrBlendCoeff dstCoeff) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000955 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000956 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000957
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000958 // This should already have been caught
959 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
960
961 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
962
963 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
964 kEmitCoverage_BlendOptFlag));
965
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000966 // The descriptor is used as a cache key. Thus when a field of the
967 // descriptor will not affect program generation (because of the vertex
968 // layout in use or other descriptor field settings) it should be set
969 // to a canonical value to avoid duplicate programs with different keys.
970
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000971 // Must initialize all fields or cache will have false negatives!
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000972 desc.fVertexLayout = this->getGeomSrc().fVertexLayout;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000973
974 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
975
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000976 bool requiresAttributeColors =
977 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000978 bool requiresAttributeCoverage =
979 !skipCoverage && SkToBool(desc.fVertexLayout &
980 kCoverage_VertexLayoutBit);
981
982 // fColorInput/fCoverageInput records how colors are specified for the.
983 // program. So we strip the bits from the layout to avoid false negatives
984 // when searching for an existing program in the cache.
985 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000986
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000987 desc.fColorFilterXfermode = skipColor ?
988 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000989 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000990
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000991 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
992
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000993 // no reason to do edge aa or look at per-vertex coverage if coverage is
994 // ignored
995 if (skipCoverage) {
996 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
997 kCoverage_VertexLayoutBit);
998 }
999
1000 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
1001 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
1002 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001003 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001004 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001005 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001006 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001007 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001008 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001009 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001010 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001011 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001012 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001013
1014 bool covIsSolidWhite = !requiresAttributeCoverage &&
1015 0xffffffff == drawState.getCoverage();
1016
1017 if (skipCoverage) {
1018 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
1019 } else if (covIsSolidWhite) {
1020 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
1021 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
1022 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
1023 } else {
1024 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
1025 }
junov@google.comf93e7172011-03-31 21:26:24 +00001026
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001027 desc.fEdgeAANumEdges = skipCoverage ? 0 : drawState.getNumAAEdges();
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001028 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001029 drawState.isConcaveEdgeAAState();
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +00001030
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001031 int lastEnabledStage = -1;
1032
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001033 if (!skipCoverage && (desc.fVertexLayout &
1034 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001035 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001036 } else {
1037 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +00001038 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001039 }
1040
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001041 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001042 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001043
1044 stage.fOptFlags = 0;
1045 stage.setEnabled(this->isStageEnabled(s));
1046
1047 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
1048 skipCoverage;
1049
1050 if (!skip && stage.isEnabled()) {
1051 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001052 const GrGLTexture* texture =
1053 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001054 GrAssert(NULL != texture);
1055 const GrSamplerState& sampler = drawState.getSampler(s);
1056 // we matrix to invert when orientation is TopDown, so make sure
1057 // we aren't in that case before flagging as identity.
1058 if (TextureMatrixIsIdentity(texture, sampler)) {
1059 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
1060 } else if (!sampler.getMatrix().hasPerspective()) {
1061 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
1062 }
1063 switch (sampler.getSampleMode()) {
1064 case GrSamplerState::kNormal_SampleMode:
1065 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
1066 break;
1067 case GrSamplerState::kRadial_SampleMode:
1068 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
1069 break;
1070 case GrSamplerState::kRadial2_SampleMode:
1071 if (sampler.radial2IsDegenerate()) {
1072 stage.fCoordMapping =
1073 StageDesc::kRadial2GradientDegenerate_CoordMapping;
1074 } else {
1075 stage.fCoordMapping =
1076 StageDesc::kRadial2Gradient_CoordMapping;
1077 }
1078 break;
1079 case GrSamplerState::kSweep_SampleMode:
1080 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
1081 break;
1082 default:
1083 GrCrash("Unexpected sample mode!");
1084 break;
1085 }
1086
1087 switch (sampler.getFilter()) {
1088 // these both can use a regular texture2D()
1089 case GrSamplerState::kNearest_Filter:
1090 case GrSamplerState::kBilinear_Filter:
1091 stage.fFetchMode = StageDesc::kSingle_FetchMode;
1092 break;
1093 // performs 4 texture2D()s
1094 case GrSamplerState::k4x4Downsample_Filter:
1095 stage.fFetchMode = StageDesc::k2x2_FetchMode;
1096 break;
1097 // performs fKernelWidth texture2D()s
1098 case GrSamplerState::kConvolution_Filter:
1099 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
1100 break;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001101 case GrSamplerState::kDilate_Filter:
1102 stage.fFetchMode = StageDesc::kDilate_FetchMode;
1103 break;
1104 case GrSamplerState::kErode_Filter:
1105 stage.fFetchMode = StageDesc::kErode_FetchMode;
1106 break;
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001107 default:
1108 GrCrash("Unexpected filter!");
1109 break;
1110 }
1111
1112 if (sampler.hasTextureDomain()) {
1113 GrAssert(GrSamplerState::kClamp_WrapMode ==
1114 sampler.getWrapX() &&
1115 GrSamplerState::kClamp_WrapMode ==
1116 sampler.getWrapY());
1117 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
1118 }
1119
1120 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001121 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001122 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1123 // if we don't have texture swizzle support then
1124 // the shader must do an alpha smear after reading
1125 // the texture
1126 stage.fInConfigFlags |= StageDesc::kSmearAlpha_InConfigFlag;
1127 } else if (sampler.swapsRAndB()) {
1128 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
1129 }
1130 }
1131 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001132 // The shader generator assumes that color channels are bytes
1133 // when rounding.
1134 GrAssert(4 == GrBytesPerPixel(texture->config()));
1135 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
1136 fUnpremulConversion) {
1137 stage.fInConfigFlags |=
1138 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
1139 } else {
1140 stage.fInConfigFlags |=
1141 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
1142 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001143 }
1144
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001145 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter ||
1146 sampler.getFilter() == GrSamplerState::kDilate_Filter ||
1147 sampler.getFilter() == GrSamplerState::kErode_Filter) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001148 stage.fKernelWidth = sampler.getKernelWidth();
1149 } else {
1150 stage.fKernelWidth = 0;
1151 }
junov@google.comf93e7172011-03-31 21:26:24 +00001152 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001153 stage.fOptFlags = 0;
1154 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1155 stage.fInConfigFlags = 0;
1156 stage.fFetchMode = (StageDesc::FetchMode) 0;
1157 stage.fKernelWidth = 0;
junov@google.comf93e7172011-03-31 21:26:24 +00001158 }
1159 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001160
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001161 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001162 // The shader generator assumes that color channels are bytes
1163 // when rounding.
1164 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
1165 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
1166 desc.fOutputConfig =
1167 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
1168 } else {
1169 desc.fOutputConfig =
1170 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
1171 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001172 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001173 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001174 }
1175
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001176 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001177
1178 // currently the experimental GS will only work with triangle prims
1179 // (and it doesn't do anything other than pass through values from
1180 // the VS to the FS anyway).
1181#if 0 && GR_GL_EXPERIMENTAL_GS
1182 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1183#endif
1184
bsalomon@google.coma3108262011-10-10 14:08:47 +00001185 // we want to avoid generating programs with different "first cov stage"
1186 // values when they would compute the same result.
1187 // We set field in the desc to kNumStages when either there are no
1188 // coverage stages or the distinction between coverage and color is
1189 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001190 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +00001191 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001192 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001193 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001194 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +00001195 }
1196
1197 // other coverage inputs
1198 if (!hasCoverage) {
1199 hasCoverage =
1200 desc.fEdgeAANumEdges ||
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001201 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +00001202 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1203 }
1204
1205 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001206 // color filter is applied between color/coverage computation
1207 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001208 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001209 }
1210
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001211 if (this->getCaps().fDualSourceBlendingSupport &&
1212 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1213 kCoverageAsAlpha_BlendOptFlag))) {
1214 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001215 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001216 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001217 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001218 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001219 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1220 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001221 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001222 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001223 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001224 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1225 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001226 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001227 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001228 }
1229 }
1230 }
junov@google.comf93e7172011-03-31 21:26:24 +00001231}