blob: b416b5cdd1d418f3b347a210dd0f51980694473f [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();
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000128 fHWProgramID = 0;
junov@google.com53a55842011-06-08 22:55:10 +0000129}
130
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000131void GrGpuGLShaders::DeleteProgram(const GrGLInterface* gl,
132 CachedData* programData) {
133 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000134 if (programData->fGShaderID) {
135 GR_GL_CALL(gl, DeleteShader(programData->fGShaderID));
136 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000137 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
138 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000139 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
140}
141
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000142////////////////////////////////////////////////////////////////////////////////
143
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000144#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
145
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000146namespace {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000147
bsalomon@google.com74b98712011-11-11 19:46:16 +0000148// GrRandoms nextU() values have patterns in the low bits
149// So using nextU() % array_count might never take some values.
150int random_int(GrRandom* r, int count) {
151 return (int)(r->nextF() * count);
152}
153
154// min is inclusive, max is exclusive
155int random_int(GrRandom* r, int min, int max) {
156 return (int)(r->nextF() * (max-min)) + min;
157}
158
159bool random_bool(GrRandom* r) {
160 return r->nextF() > .5f;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000161}
162
163}
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000164
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000165bool GrGpuGLShaders::programUnitTest() {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000166
tomhudson@google.com086e5352011-12-08 14:44:10 +0000167 GrGLSLGeneration glslGeneration =
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000168 GrGetGLSLGeneration(this->glBinding(), this->glInterface());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000169 static const int STAGE_OPTS[] = {
170 0,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000171 StageDesc::kNoPerspective_OptFlagBit,
172 StageDesc::kIdentity_CoordMapping
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000173 };
bsalomon@google.com74b98712011-11-11 19:46:16 +0000174 static const int IN_CONFIG_FLAGS[] = {
175 StageDesc::kNone_InConfigFlag,
176 StageDesc::kSwapRAndB_InConfigFlag,
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000177 StageDesc::kSwapRAndB_InConfigFlag |
178 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag,
179 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000180 StageDesc::kSmearAlpha_InConfigFlag,
181 };
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000182 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000183 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000184
185 static const int NUM_TESTS = 512;
186
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000187 GrRandom random;
188 for (int t = 0; t < NUM_TESTS; ++t) {
189
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000190#if 0
191 GrPrintf("\nTest Program %d\n-------------\n", t);
192 static const int stop = -1;
193 if (t == stop) {
194 int breakpointhere = 9;
195 }
196#endif
197
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000198 pdesc.fVertexLayout = 0;
199 pdesc.fEmitsPointSize = random.nextF() > .5f;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000200 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000201 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000202
bsalomon@google.com74b98712011-11-11 19:46:16 +0000203 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000204
bsalomon@google.com74b98712011-11-11 19:46:16 +0000205 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000206
bsalomon@google.com74b98712011-11-11 19:46:16 +0000207 pdesc.fVertexLayout |= random_bool(&random) ?
bsalomon@google.coma3108262011-10-10 14:08:47 +0000208 GrDrawTarget::kCoverage_VertexLayoutBit :
209 0;
210
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000211#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com6f92f182011-09-29 15:05:48 +0000212 pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport &&
bsalomon@google.com74b98712011-11-11 19:46:16 +0000213 random_bool(&random);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000214#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000215 pdesc.fOutputConfig = random_int(&random, ProgramDesc::kOutputConfigCnt);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000216
bsalomon@google.com74b98712011-11-11 19:46:16 +0000217 bool edgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000218 if (edgeAA) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000219 bool vertexEdgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000220 if (vertexEdgeAA) {
221 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000222 if (this->getCaps().fShaderDerivativeSupport) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000223 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000224 } else {
tomhudson@google.com93813632011-10-27 20:21:16 +0000225 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000226 }
227 pdesc.fEdgeAANumEdges = 0;
228 } else {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000229 pdesc.fEdgeAANumEdges = random_int(&random, 1, this->getMaxEdges());
230 pdesc.fEdgeAAConcave = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000231 }
232 } else {
233 pdesc.fEdgeAANumEdges = 0;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000234 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000235
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000236 pdesc.fColorMatrixEnabled = random_bool(&random);
237
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000238 if (this->getCaps().fDualSourceBlendingSupport) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000239 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000240 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000241 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000242 }
243
tomhudson@google.com93813632011-10-27 20:21:16 +0000244 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000245 // enable the stage?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000246 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000247 // use separate tex coords?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000248 if (random_bool(&random)) {
249 int t = random_int(&random, GrDrawState::kMaxTexCoords);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000250 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
251 } else {
252 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
253 }
254 }
255 // use text-formatted verts?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000256 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000257 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
258 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000259 StageDesc& stage = pdesc.fStages[s];
bsalomon@google.com74b98712011-11-11 19:46:16 +0000260 stage.fOptFlags = STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
261 stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];
262 stage.fCoordMapping = random_int(&random, StageDesc::kCoordMappingCnt);
263 stage.fFetchMode = random_int(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000264 // convolution shaders don't work with persp tex matrix
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000265 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode ||
266 stage.fFetchMode == StageDesc::kDilate_FetchMode ||
267 stage.fFetchMode == StageDesc::kErode_FetchMode) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000268 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
269 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000270 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000271 static const uint32_t kMulByAlphaMask =
272 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
273 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000274 switch (stage.fFetchMode) {
275 case StageDesc::kSingle_FetchMode:
276 stage.fKernelWidth = 0;
277 break;
278 case StageDesc::kConvolution_FetchMode:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000279 case StageDesc::kDilate_FetchMode:
280 case StageDesc::kErode_FetchMode:
bsalomon@google.com74b98712011-11-11 19:46:16 +0000281 stage.fKernelWidth = random_int(&random, 2, 8);
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000282 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000283 break;
284 case StageDesc::k2x2_FetchMode:
285 stage.fKernelWidth = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000286 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000287 break;
288 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000289 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000290 CachedData cachedData;
bsalomon@google.com96399942012-02-13 14:39:16 +0000291 if (!program.genProgram(this->glContextInfo(), &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000292 return false;
293 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000294 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000295 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000296 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000297}
298
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000299GrGpuGLShaders::GrGpuGLShaders(const GrGLContextInfo& ctxInfo)
300 : GrGpuGL(ctxInfo) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000301
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000302 // Enable supported shader-related caps
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000303 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000304 fCaps.fDualSourceBlendingSupport =
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000305 this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000306 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000307 fCaps.fShaderDerivativeSupport = true;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000308 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
309 fCaps.fGeometryShaderSupport =
310 this->glVersion() >= GR_GL_VER(3,2) &&
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000311 this->glslGeneration() >= k150_GrGLSLGeneration;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000312 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000313 fCaps.fShaderDerivativeSupport =
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000314 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000315 }
junov@google.comf93e7172011-03-31 21:26:24 +0000316
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000317 GR_GL_GetIntegerv(this->glInterface(),
318 GR_GL_MAX_VERTEX_ATTRIBS,
319 &fMaxVertexAttribs);
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000320
junov@google.comf93e7172011-03-31 21:26:24 +0000321 fProgramData = NULL;
bsalomon@google.com96399942012-02-13 14:39:16 +0000322 fProgramCache = new ProgramCache(this->glContextInfo());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000323
324#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000325 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000326#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000327}
328
329GrGpuGLShaders::~GrGpuGLShaders() {
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000330
331 if (fProgramData && 0 != fHWProgramID) {
332 // detach the current program so there is no confusion on OpenGL's part
333 // that we want it to be deleted
334 SkASSERT(fHWProgramID == fProgramData->fProgramID);
335 GL_CALL(UseProgram(0));
336 }
junov@google.comf93e7172011-03-31 21:26:24 +0000337 delete fProgramCache;
338}
339
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000340const GrMatrix& GrGpuGLShaders::getHWViewMatrix() {
341 GrAssert(fProgramData);
342
343 if (GrGLProgram::kSetAsAttribute ==
344 fProgramData->fUniLocations.fViewMatrixUni) {
345 return fHWDrawState.getViewMatrix();
346 } else {
347 return fProgramData->fViewMatrix;
348 }
349}
350
351void GrGpuGLShaders::recordHWViewMatrix(const GrMatrix& matrix) {
352 GrAssert(fProgramData);
353 if (GrGLProgram::kSetAsAttribute ==
354 fProgramData->fUniLocations.fViewMatrixUni) {
355 fHWDrawState.setViewMatrix(matrix);
356 } else {
357 fProgramData->fViewMatrix = matrix;
358 }
359}
360
junov@google.comf93e7172011-03-31 21:26:24 +0000361const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000362 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000363
364 if (GrGLProgram::kSetAsAttribute ==
365 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000366 return fHWDrawState.getSampler(stage).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000367 } else {
368 return fProgramData->fTextureMatrices[stage];
369 }
junov@google.comf93e7172011-03-31 21:26:24 +0000370}
371
372void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000373 GrAssert(fProgramData);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000374 if (GrGLProgram::kSetAsAttribute ==
bsalomon@google.com91961302011-05-09 18:39:58 +0000375 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000376 *fHWDrawState.sampler(stage)->matrix() = matrix;
bsalomon@google.com91961302011-05-09 18:39:58 +0000377 } else {
378 fProgramData->fTextureMatrices[stage] = matrix;
379 }
junov@google.comf93e7172011-03-31 21:26:24 +0000380}
381
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000382void GrGpuGLShaders::onResetContext() {
383 INHERITED::onResetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000384
junov@google.comf93e7172011-03-31 21:26:24 +0000385 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000386
387 // Third party GL code may have left vertex attributes enabled. Some GL
388 // implementations (osmesa) may read vetex attributes that are not required
389 // by the current shader. Therefore, we have to ensure that only the
390 // attributes we require for the current draw are enabled or we may cause an
391 // invalid read.
392
393 // Disable all vertex layout bits so that next flush will assume all
394 // optional vertex attributes are disabled.
395 fHWGeometryState.fVertexLayout = 0;
396
397 // We always use the this attribute and assume it is always enabled.
398 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
399 GL_CALL(EnableVertexAttribArray(posAttrIdx));
400 // Disable all other vertex attributes.
401 for (int va = 0; va < fMaxVertexAttribs; ++va) {
402 if (va != posAttrIdx) {
403 GL_CALL(DisableVertexAttribArray(va));
404 }
junov@google.comf93e7172011-03-31 21:26:24 +0000405 }
junov@google.comf93e7172011-03-31 21:26:24 +0000406
407 fHWProgramID = 0;
408}
409
410void GrGpuGLShaders::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000411 const GrMatrix& vm = this->getDrawState().getViewMatrix();
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000412 if (!GrGpuGLShaders::getHWViewMatrix().cheapEqualTo(vm)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000413
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000414 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
415 GrAssert(NULL != rt);
416 GrMatrix m;
417 m.setAll(
418 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
419 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
420 0, 0, GrMatrix::I()[8]);
421 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000422
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000423 // ES doesn't allow you to pass true to the transpose param,
424 // so do our own transpose
425 GrGLfloat mt[] = {
426 GrScalarToFloat(m[GrMatrix::kMScaleX]),
427 GrScalarToFloat(m[GrMatrix::kMSkewY]),
428 GrScalarToFloat(m[GrMatrix::kMPersp0]),
429 GrScalarToFloat(m[GrMatrix::kMSkewX]),
430 GrScalarToFloat(m[GrMatrix::kMScaleY]),
431 GrScalarToFloat(m[GrMatrix::kMPersp1]),
432 GrScalarToFloat(m[GrMatrix::kMTransX]),
433 GrScalarToFloat(m[GrMatrix::kMTransY]),
434 GrScalarToFloat(m[GrMatrix::kMPersp2])
435 };
436
437 if (GrGLProgram::kSetAsAttribute ==
438 fProgramData->fUniLocations.fViewMatrixUni) {
439 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
440 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
441 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
442 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
443 } else {
444 GrAssert(GrGLProgram::kUnusedUniform !=
445 fProgramData->fUniLocations.fViewMatrixUni);
446 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
447 1, false, mt));
448 }
449 this->recordHWViewMatrix(vm);
bsalomon@google.com91961302011-05-09 18:39:58 +0000450 }
junov@google.comf93e7172011-03-31 21:26:24 +0000451}
452
junov@google.com6acc9b32011-05-16 18:32:07 +0000453void GrGpuGLShaders::flushTextureDomain(int s) {
454 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000455 const GrDrawState& drawState = this->getDrawState();
junov@google.com6acc9b32011-05-16 18:32:07 +0000456 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000457 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
junov@google.com6acc9b32011-05-16 18:32:07 +0000458
twiz@google.com76b82742011-06-02 20:30:02 +0000459 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
460 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000461
junov@google.com2f839402011-05-24 15:13:01 +0000462 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000463
junov@google.com2f839402011-05-24 15:13:01 +0000464 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000465 GrScalarToFloat(texDom.left()),
466 GrScalarToFloat(texDom.top()),
467 GrScalarToFloat(texDom.right()),
468 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000469 };
470
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000471 const GrGLTexture* texture =
472 static_cast<const GrGLTexture*>(drawState.getTexture(s));
junov@google.com2f839402011-05-24 15:13:01 +0000473 GrGLTexture::Orientation orientation = texture->orientation();
474
475 // vertical flip if necessary
476 if (GrGLTexture::kBottomUp_Orientation == orientation) {
477 values[1] = 1.0f - values[1];
478 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000479 // The top and bottom were just flipped, so correct the ordering
480 // of elements so that values = (l, t, r, b).
481 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000482 }
483
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000484 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000485 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000486 }
487}
488
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000489void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000490 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000491 const GrDrawState& drawState = this->getDrawState();
492 const GrGLTexture* texture =
493 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000494 if (NULL != texture) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000495 const GrMatrix& hwMatrix = this->getHWSamplerMatrix(s);
496 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000497 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000498 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000499 !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000500
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000501 GrMatrix m = samplerMatrix;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000502 GrSamplerState::SampleMode mode =
503 drawState.getSampler(s).getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000504 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000505
bsalomon@google.com91961302011-05-09 18:39:58 +0000506 // ES doesn't allow you to pass true to the transpose param,
507 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000508 GrGLfloat mt[] = {
509 GrScalarToFloat(m[GrMatrix::kMScaleX]),
510 GrScalarToFloat(m[GrMatrix::kMSkewY]),
511 GrScalarToFloat(m[GrMatrix::kMPersp0]),
512 GrScalarToFloat(m[GrMatrix::kMSkewX]),
513 GrScalarToFloat(m[GrMatrix::kMScaleY]),
514 GrScalarToFloat(m[GrMatrix::kMPersp1]),
515 GrScalarToFloat(m[GrMatrix::kMTransX]),
516 GrScalarToFloat(m[GrMatrix::kMTransY]),
517 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000518 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000519
bsalomon@google.com91961302011-05-09 18:39:58 +0000520 if (GrGLProgram::kSetAsAttribute ==
521 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
522 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000523 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
524 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
525 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000526 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000527 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000528 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000529 this->recordHWSamplerMatrix(s, drawState.getSampler(s).getMatrix());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000530 }
531 }
junov@google.comf93e7172011-03-31 21:26:24 +0000532}
533
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000534void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000535
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000536 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000537 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.com91961302011-05-09 18:39:58 +0000538 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000539 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
540 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
541 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000542
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000543 GrScalar centerX1 = sampler.getRadial2CenterX1();
544 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000545
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000546 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000547
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000548 // when were in the degenerate (linear) case the second
549 // value will be INF but the program doesn't read it. (We
550 // use the same 6 uniforms even though we don't need them
551 // all in the linear case just to keep the code complexity
552 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000553 float values[6] = {
554 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000555 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000556 GrScalarToFloat(centerX1),
557 GrScalarToFloat(radius0),
558 GrScalarToFloat(GrMul(radius0, radius0)),
559 sampler.isRadial2PosRoot() ? 1.f : -1.f
560 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000561 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000562 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
563 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
564 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
565 }
566}
567
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000568void GrGpuGLShaders::flushConvolution(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000569 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000570 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
571 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000572 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
573 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000574 }
575 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
576 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000577 const GrGLTexture* texture =
578 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
579 float imageIncrement[2] = { 0 };
580 switch (sampler.getFilterDirection()) {
581 case GrSamplerState::kX_FilterDirection:
582 imageIncrement[0] = 1.0f / texture->width();
583 break;
584 case GrSamplerState::kY_FilterDirection:
585 imageIncrement[1] = 1.0f / texture->height();
586 break;
587 default:
588 GrCrash("Unknown filter direction.");
589 }
590 GL_CALL(Uniform2fv(imageIncrementUni, 1, imageIncrement));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000591 }
592}
593
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000594void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000595 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000596 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000597 const GrGLTexture* texture =
598 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000599 if (texture->width() != fProgramData->fTextureWidth[s] ||
600 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000601
bsalomon@google.com99621082011-11-15 16:47:16 +0000602 float texelSize[] = {1.f / texture->width(),
603 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000604 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000605 fProgramData->fTextureWidth[s] = texture->width();
606 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000607 }
608 }
junov@google.comf93e7172011-03-31 21:26:24 +0000609}
610
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000611void GrGpuGLShaders::flushEdgeAAData() {
612 const int& uni = fProgramData->fUniLocations.fEdgesUni;
613 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000614 int count = this->getDrawState().getNumAAEdges();
tomhudson@google.com93813632011-10-27 20:21:16 +0000615 GrDrawState::Edge edges[GrDrawState::kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000616 // Flip the edges in Y
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000617 float height =
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000618 static_cast<float>(this->getDrawState().getRenderTarget()->height());
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000619 for (int i = 0; i < count; ++i) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000620 edges[i] = this->getDrawState().getAAEdges()[i];
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000621 float b = edges[i].fY;
622 edges[i].fY = -b;
623 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000624 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000625 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000626 }
627}
628
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000629void GrGpuGLShaders::flushColorMatrix() {
630 const ProgramDesc& desc = fCurrentProgram.getDesc();
631 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
632 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
633 if (GrGLProgram::kUnusedUniform != matrixUni
634 && GrGLProgram::kUnusedUniform != vecUni) {
635 const float* m = this->getDrawState().getColorMatrix();
636 GrGLfloat mt[] = {
637 m[0], m[5], m[10], m[15],
638 m[1], m[6], m[11], m[16],
639 m[2], m[7], m[12], m[17],
640 m[3], m[8], m[13], m[18],
641 };
642 static float scale = 1.0f / 255.0f;
643 GrGLfloat vec[] = {
644 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
645 };
646 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
647 GL_CALL(Uniform4fv(vecUni, 1, vec));
648 }
649}
650
Scroggo01b87ec2011-05-11 18:05:38 +0000651static const float ONE_OVER_255 = 1.f / 255.f;
652
653#define GR_COLOR_TO_VEC4(color) {\
654 GrColorUnpackR(color) * ONE_OVER_255,\
655 GrColorUnpackG(color) * ONE_OVER_255,\
656 GrColorUnpackB(color) * ONE_OVER_255,\
657 GrColorUnpackA(color) * ONE_OVER_255 \
658}
659
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000660void GrGpuGLShaders::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000661 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000662 const GrDrawState& drawState = this->getDrawState();
663
bsalomon@google.come79c8152012-03-29 19:07:12 +0000664 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000665 // color will be specified per-vertex as an attribute
666 // invalidate the const vertex attrib color
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000667 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000668 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000669 switch (desc.fColorInput) {
670 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000671 if (fHWDrawState.getColor() != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000672 // OpenGL ES only supports the float varieties of
673 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000674 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000675 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
676 c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000677 fHWDrawState.setColor(color);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000678 }
679 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000680 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000681 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000682 // OpenGL ES doesn't support unsigned byte varieties of
683 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000684 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000685 GrAssert(GrGLProgram::kUnusedUniform !=
686 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000687 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
688 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000689 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000690 }
691 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000692 case ProgramDesc::kSolidWhite_ColorInput:
693 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000694 break;
695 default:
696 GrCrash("Unknown color type.");
697 }
698 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000699 if (fProgramData->fUniLocations.fColorFilterUni
700 != GrGLProgram::kUnusedUniform
701 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000702 != drawState.getColorFilterColor()) {
703 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000704 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000705 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000706 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000707}
708
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000709void GrGpuGLShaders::flushCoverage(GrColor coverage) {
710 const ProgramDesc& desc = fCurrentProgram.getDesc();
711 const GrDrawState& drawState = this->getDrawState();
712
713
bsalomon@google.come79c8152012-03-29 19:07:12 +0000714 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000715 // coverage will be specified per-vertex as an attribute
716 // invalidate the const vertex attrib coverage
717 fHWDrawState.setCoverage4(GrColor_ILLEGAL);
718 } else {
719 switch (desc.fCoverageInput) {
720 case ProgramDesc::kAttribute_ColorInput:
721 if (fHWDrawState.getCoverage() != coverage) {
722 // OpenGL ES only supports the float varieties of
723 // glVertexAttrib
724 float c[] = GR_COLOR_TO_VEC4(coverage);
725 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
726 c));
727 fHWDrawState.setCoverage(coverage);
728 }
729 break;
730 case ProgramDesc::kUniform_ColorInput:
731 if (fProgramData->fCoverage != coverage) {
732 // OpenGL ES doesn't support unsigned byte varieties of
733 // glUniform
734 float c[] = GR_COLOR_TO_VEC4(coverage);
735 GrAssert(GrGLProgram::kUnusedUniform !=
736 fProgramData->fUniLocations.fCoverageUni);
737 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
738 1, c));
739 fProgramData->fCoverage = coverage;
740 }
741 break;
742 case ProgramDesc::kSolidWhite_ColorInput:
743 case ProgramDesc::kTransBlack_ColorInput:
744 break;
745 default:
746 GrCrash("Unknown coverage type.");
747 }
748 }
749}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000750
junov@google.comf93e7172011-03-31 21:26:24 +0000751bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
752 if (!flushGLStateCommon(type)) {
753 return false;
754 }
755
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000756 const GrDrawState& drawState = this->getDrawState();
757
junov@google.comf93e7172011-03-31 21:26:24 +0000758 if (fDirtyFlags.fRenderTargetChanged) {
759 // our coords are in pixel space and the GL matrices map to NDC
760 // so if the viewport changed, our matrix is now wrong.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000761 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
junov@google.comf93e7172011-03-31 21:26:24 +0000762 // we assume all shader matrices may be wrong after viewport changes
763 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000764 }
765
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000766 GrBlendCoeff srcCoeff;
767 GrBlendCoeff dstCoeff;
768 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
769 if (kSkipDraw_BlendOptFlag & blendOpts) {
770 return false;
771 }
772
773 this->buildProgram(type, blendOpts, dstCoeff);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000774 fProgramData = fProgramCache->getProgramData(fCurrentProgram);
bsalomon@google.com91961302011-05-09 18:39:58 +0000775 if (NULL == fProgramData) {
776 GrAssert(!"Failed to create program!");
777 return false;
778 }
junov@google.comf93e7172011-03-31 21:26:24 +0000779
780 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000781 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000782 fHWProgramID = fProgramData->fProgramID;
783 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000784 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
785 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000786
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000787 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000788 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000789 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
790 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000791 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000792 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
793 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000794 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000795 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000796 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000797 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000798 }
799 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000800 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000801
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000802 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000803
tomhudson@google.com93813632011-10-27 20:21:16 +0000804 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000805 if (this->isStageEnabled(s)) {
806 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000807
bsalomon@google.com40d92932011-12-13 18:40:47 +0000808 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000809
bsalomon@google.com40d92932011-12-13 18:40:47 +0000810 this->flushConvolution(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000811
bsalomon@google.com40d92932011-12-13 18:40:47 +0000812 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000813
bsalomon@google.com40d92932011-12-13 18:40:47 +0000814 this->flushTextureDomain(s);
815 }
junov@google.comf93e7172011-03-31 21:26:24 +0000816 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000817 this->flushEdgeAAData();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000818 this->flushColorMatrix();
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000819 resetDirtyFlags();
junov@google.comf93e7172011-03-31 21:26:24 +0000820 return true;
821}
822
823void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000824}
825
826void GrGpuGLShaders::setupGeometry(int* startVertex,
827 int* startIndex,
828 int vertexCount,
829 int indexCount) {
830
831 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000832 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000833 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000834 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000835
bsalomon@google.come79c8152012-03-29 19:07:12 +0000836 GrVertexLayout currLayout = this->getVertexLayout();
837
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000838 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000839 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000840 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000841 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000842 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000843 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000844 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000845 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000846 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000847 int oldEdgeOffset;
848
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000849 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
850 fHWGeometryState.fVertexLayout,
851 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000852 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000853 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000854 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000855 bool indexed = NULL != startIndex;
856
857 int extraVertexOffset;
858 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000859 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000860
861 GrGLenum scalarType;
862 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000863 if (currLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000864 scalarType = GrGLTextType;
865 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
866 } else {
867 scalarType = GrGLType;
868 texCoordNorm = false;
869 }
870
871 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
872 *startVertex = 0;
873 if (indexed) {
874 *startIndex += extraIndexOffset;
875 }
876
877 // all the Pointers must be set if any of these are true
878 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
879 vertexOffset != fHWGeometryState.fVertexOffset ||
880 newStride != oldStride;
881
882 // position and tex coord offsets change if above conditions are true
883 // or the type/normalization changed based on text vs nontext type coords.
884 bool posAndTexChange = allOffsetsChange ||
885 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
886 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000887 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000888
889 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000890 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000891 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000892 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000893 fHWGeometryState.fVertexOffset = vertexOffset;
894 }
895
tomhudson@google.com93813632011-10-27 20:21:16 +0000896 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000897 if (newTexCoordOffsets[t] > 0) {
898 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000899 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000900 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000901 GL_CALL(EnableVertexAttribArray(idx));
902 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000903 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000904 } else if (posAndTexChange ||
905 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000906 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000907 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000908 }
909 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000910 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000911 }
912 }
913
914 if (newColorOffset > 0) {
915 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000916 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000917 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000918 GL_CALL(EnableVertexAttribArray(idx));
919 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000920 true, newStride, colorOffset));
921 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000922 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000923 true, newStride, colorOffset));
924 }
925 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000926 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000927 }
928
bsalomon@google.coma3108262011-10-10 14:08:47 +0000929 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000930 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000931 int idx = GrGLProgram::CoverageAttributeIdx();
932 if (oldCoverageOffset <= 0) {
933 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000934 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000935 true, newStride, coverageOffset));
936 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000937 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000938 true, newStride, coverageOffset));
939 }
940 } else if (oldCoverageOffset > 0) {
941 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
942 }
943
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000944 if (newEdgeOffset > 0) {
945 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
946 int idx = GrGLProgram::EdgeAttributeIdx();
947 if (oldEdgeOffset <= 0) {
948 GL_CALL(EnableVertexAttribArray(idx));
949 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
950 false, newStride, edgeOffset));
951 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
952 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
953 false, newStride, edgeOffset));
954 }
955 } else if (oldEdgeOffset > 0) {
956 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
957 }
958
bsalomon@google.come79c8152012-03-29 19:07:12 +0000959 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000960 fHWGeometryState.fArrayPtrsDirty = false;
961}
962
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000963void GrGpuGLShaders::buildProgram(GrPrimitiveType type,
964 BlendOptFlags blendOpts,
965 GrBlendCoeff dstCoeff) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000966 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000967 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000968
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000969 // This should already have been caught
970 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
971
972 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
973
974 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
975 kEmitCoverage_BlendOptFlag));
976
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000977 // The descriptor is used as a cache key. Thus when a field of the
978 // descriptor will not affect program generation (because of the vertex
979 // layout in use or other descriptor field settings) it should be set
980 // to a canonical value to avoid duplicate programs with different keys.
981
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000982 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000983 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000984
985 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
986
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000987 bool requiresAttributeColors =
988 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000989 bool requiresAttributeCoverage =
990 !skipCoverage && SkToBool(desc.fVertexLayout &
991 kCoverage_VertexLayoutBit);
992
993 // fColorInput/fCoverageInput records how colors are specified for the.
994 // program. So we strip the bits from the layout to avoid false negatives
995 // when searching for an existing program in the cache.
996 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000997
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000998 desc.fColorFilterXfermode = skipColor ?
999 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001000 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001001
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001002 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
1003
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001004 // no reason to do edge aa or look at per-vertex coverage if coverage is
1005 // ignored
1006 if (skipCoverage) {
1007 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
1008 kCoverage_VertexLayoutBit);
1009 }
1010
1011 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
1012 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
1013 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001014 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001015 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001016 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001017 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001018 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001019 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001020 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001021 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001022 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001023 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001024
1025 bool covIsSolidWhite = !requiresAttributeCoverage &&
1026 0xffffffff == drawState.getCoverage();
1027
1028 if (skipCoverage) {
1029 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
1030 } else if (covIsSolidWhite) {
1031 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
1032 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
1033 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
1034 } else {
1035 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
1036 }
junov@google.comf93e7172011-03-31 21:26:24 +00001037
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001038 desc.fEdgeAANumEdges = skipCoverage ? 0 : drawState.getNumAAEdges();
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001039 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001040 drawState.isConcaveEdgeAAState();
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +00001041
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001042 int lastEnabledStage = -1;
1043
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001044 if (!skipCoverage && (desc.fVertexLayout &
1045 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001046 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001047 } else {
1048 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +00001049 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001050 }
1051
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001052 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001053 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001054
1055 stage.fOptFlags = 0;
1056 stage.setEnabled(this->isStageEnabled(s));
1057
1058 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
1059 skipCoverage;
1060
1061 if (!skip && stage.isEnabled()) {
1062 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001063 const GrGLTexture* texture =
1064 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001065 GrAssert(NULL != texture);
1066 const GrSamplerState& sampler = drawState.getSampler(s);
1067 // we matrix to invert when orientation is TopDown, so make sure
1068 // we aren't in that case before flagging as identity.
1069 if (TextureMatrixIsIdentity(texture, sampler)) {
1070 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
1071 } else if (!sampler.getMatrix().hasPerspective()) {
1072 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
1073 }
1074 switch (sampler.getSampleMode()) {
1075 case GrSamplerState::kNormal_SampleMode:
1076 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
1077 break;
1078 case GrSamplerState::kRadial_SampleMode:
1079 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
1080 break;
1081 case GrSamplerState::kRadial2_SampleMode:
1082 if (sampler.radial2IsDegenerate()) {
1083 stage.fCoordMapping =
1084 StageDesc::kRadial2GradientDegenerate_CoordMapping;
1085 } else {
1086 stage.fCoordMapping =
1087 StageDesc::kRadial2Gradient_CoordMapping;
1088 }
1089 break;
1090 case GrSamplerState::kSweep_SampleMode:
1091 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
1092 break;
1093 default:
1094 GrCrash("Unexpected sample mode!");
1095 break;
1096 }
1097
1098 switch (sampler.getFilter()) {
1099 // these both can use a regular texture2D()
1100 case GrSamplerState::kNearest_Filter:
1101 case GrSamplerState::kBilinear_Filter:
1102 stage.fFetchMode = StageDesc::kSingle_FetchMode;
1103 break;
1104 // performs 4 texture2D()s
1105 case GrSamplerState::k4x4Downsample_Filter:
1106 stage.fFetchMode = StageDesc::k2x2_FetchMode;
1107 break;
1108 // performs fKernelWidth texture2D()s
1109 case GrSamplerState::kConvolution_Filter:
1110 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
1111 break;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001112 case GrSamplerState::kDilate_Filter:
1113 stage.fFetchMode = StageDesc::kDilate_FetchMode;
1114 break;
1115 case GrSamplerState::kErode_Filter:
1116 stage.fFetchMode = StageDesc::kErode_FetchMode;
1117 break;
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001118 default:
1119 GrCrash("Unexpected filter!");
1120 break;
1121 }
1122
1123 if (sampler.hasTextureDomain()) {
1124 GrAssert(GrSamplerState::kClamp_WrapMode ==
1125 sampler.getWrapX() &&
1126 GrSamplerState::kClamp_WrapMode ==
1127 sampler.getWrapY());
1128 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
1129 }
1130
1131 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001132 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001133 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1134 // if we don't have texture swizzle support then
1135 // the shader must do an alpha smear after reading
1136 // the texture
1137 stage.fInConfigFlags |= StageDesc::kSmearAlpha_InConfigFlag;
1138 } else if (sampler.swapsRAndB()) {
1139 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
1140 }
1141 }
1142 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001143 // The shader generator assumes that color channels are bytes
1144 // when rounding.
1145 GrAssert(4 == GrBytesPerPixel(texture->config()));
1146 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
1147 fUnpremulConversion) {
1148 stage.fInConfigFlags |=
1149 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
1150 } else {
1151 stage.fInConfigFlags |=
1152 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
1153 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001154 }
1155
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001156 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter ||
1157 sampler.getFilter() == GrSamplerState::kDilate_Filter ||
1158 sampler.getFilter() == GrSamplerState::kErode_Filter) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001159 stage.fKernelWidth = sampler.getKernelWidth();
1160 } else {
1161 stage.fKernelWidth = 0;
1162 }
junov@google.comf93e7172011-03-31 21:26:24 +00001163 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001164 stage.fOptFlags = 0;
1165 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1166 stage.fInConfigFlags = 0;
1167 stage.fFetchMode = (StageDesc::FetchMode) 0;
1168 stage.fKernelWidth = 0;
junov@google.comf93e7172011-03-31 21:26:24 +00001169 }
1170 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001171
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001172 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001173 // The shader generator assumes that color channels are bytes
1174 // when rounding.
1175 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
1176 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
1177 desc.fOutputConfig =
1178 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
1179 } else {
1180 desc.fOutputConfig =
1181 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
1182 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001183 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001184 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001185 }
1186
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001187 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001188
1189 // currently the experimental GS will only work with triangle prims
1190 // (and it doesn't do anything other than pass through values from
1191 // the VS to the FS anyway).
1192#if 0 && GR_GL_EXPERIMENTAL_GS
1193 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1194#endif
1195
bsalomon@google.coma3108262011-10-10 14:08:47 +00001196 // we want to avoid generating programs with different "first cov stage"
1197 // values when they would compute the same result.
1198 // We set field in the desc to kNumStages when either there are no
1199 // coverage stages or the distinction between coverage and color is
1200 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001201 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +00001202 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001203 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001204 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001205 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +00001206 }
1207
1208 // other coverage inputs
1209 if (!hasCoverage) {
1210 hasCoverage =
1211 desc.fEdgeAANumEdges ||
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001212 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +00001213 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1214 }
1215
1216 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001217 // color filter is applied between color/coverage computation
1218 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001219 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001220 }
1221
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001222 if (this->getCaps().fDualSourceBlendingSupport &&
1223 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1224 kCoverageAsAlpha_BlendOptFlag))) {
1225 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001226 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001227 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001228 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001229 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001230 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1231 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001232 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001233 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001234 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001235 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1236 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001237 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001238 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001239 }
1240 }
1241 }
junov@google.comf93e7172011-03-31 21:26:24 +00001242}