blob: 88c81ab3374a9bc2f7be63a3680cb8cf92241523 [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"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000011#include "GrCustomStage.h"
junov@google.comf93e7172011-03-31 21:26:24 +000012#include "GrGLProgram.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000013#include "GrGLProgramStage.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000014#include "GrGLSL.h"
junov@google.comf93e7172011-03-31 21:26:24 +000015#include "GrGpuGLShaders.h"
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000016#include "../GrGpuVertex.h"
junov@google.comf93e7172011-03-31 21:26:24 +000017#include "GrNoncopyable.h"
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000018#include "../GrStringBuilder.h"
19#include "../GrRandom.h"
junov@google.comf93e7172011-03-31 21:26:24 +000020
junov@google.comf93e7172011-03-31 21:26:24 +000021#define SKIP_CACHE_CHECK true
22#define GR_UINT32_MAX static_cast<uint32_t>(-1)
23
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000024#include "../GrTHashCache.h"
junov@google.comf93e7172011-03-31 21:26:24 +000025
26class GrGpuGLShaders::ProgramCache : public ::GrNoncopyable {
27private:
28 class Entry;
29
junov@google.comf7c00f62011-08-18 18:15:16 +000030 typedef GrBinHashKey<Entry, GrGLProgram::kProgramKeySize> ProgramHashKey;
junov@google.comf93e7172011-03-31 21:26:24 +000031
32 class Entry : public ::GrNoncopyable {
33 public:
34 Entry() {}
junov@google.comf93e7172011-03-31 21:26:24 +000035 void copyAndTakeOwnership(Entry& entry) {
36 fProgramData.copyAndTakeOwnership(entry.fProgramData);
junov@google.comf7c00f62011-08-18 18:15:16 +000037 fKey = entry.fKey; // ownership transfer
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000038 fLRUStamp = entry.fLRUStamp;
junov@google.comf93e7172011-03-31 21:26:24 +000039 }
40
41 public:
42 int compare(const ProgramHashKey& key) const { return fKey.compare(key); }
43
44 public:
45 GrGLProgram::CachedData fProgramData;
46 ProgramHashKey fKey;
47 unsigned int fLRUStamp;
48 };
49
50 GrTHashTable<Entry, ProgramHashKey, 8> fHashCache;
51
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000052 // We may have kMaxEntries+1 shaders in the GL context because
53 // we create a new shader before evicting from the cache.
junov@google.comf93e7172011-03-31 21:26:24 +000054 enum {
55 kMaxEntries = 32
56 };
bsalomon@google.com4fa66942011-09-20 19:06:12 +000057 Entry fEntries[kMaxEntries];
58 int fCount;
59 unsigned int fCurrLRUStamp;
bsalomon@google.com96399942012-02-13 14:39:16 +000060 const GrGLContextInfo& fGL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000061
junov@google.comf93e7172011-03-31 21:26:24 +000062public:
bsalomon@google.com96399942012-02-13 14:39:16 +000063 ProgramCache(const GrGLContextInfo& gl)
junov@google.comf93e7172011-03-31 21:26:24 +000064 : fCount(0)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000065 , fCurrLRUStamp(0)
bsalomon@google.com96399942012-02-13 14:39:16 +000066 , fGL(gl) {
junov@google.comf93e7172011-03-31 21:26:24 +000067 }
68
69 ~ProgramCache() {
70 for (int i = 0; i < fCount; ++i) {
bsalomon@google.com96399942012-02-13 14:39:16 +000071 GrGpuGLShaders::DeleteProgram(fGL.interface(),
72 &fEntries[i].fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000073 }
74 }
75
76 void abandon() {
77 fCount = 0;
78 }
79
80 void invalidateViewMatrices() {
81 for (int i = 0; i < fCount; ++i) {
82 // set to illegal matrix
83 fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix();
84 }
85 }
86
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000087 GrGLProgram::CachedData* getProgramData(const GrGLProgram& desc,
88 GrCustomStage** stages) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000089 Entry newEntry;
junov@google.comf7c00f62011-08-18 18:15:16 +000090 newEntry.fKey.setKeyData(desc.keyData());
91
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000092 Entry* entry = fHashCache.find(newEntry.fKey);
junov@google.comf93e7172011-03-31 21:26:24 +000093 if (NULL == entry) {
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000094 if (!desc.genProgram(fGL, stages, &newEntry.fProgramData)) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000095 return NULL;
96 }
junov@google.comf93e7172011-03-31 21:26:24 +000097 if (fCount < kMaxEntries) {
98 entry = fEntries + fCount;
99 ++fCount;
100 } else {
101 GrAssert(kMaxEntries == fCount);
102 entry = fEntries;
103 for (int i = 1; i < kMaxEntries; ++i) {
104 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
105 entry = fEntries + i;
106 }
107 }
108 fHashCache.remove(entry->fKey, entry);
bsalomon@google.com96399942012-02-13 14:39:16 +0000109 GrGpuGLShaders::DeleteProgram(fGL.interface(),
110 &entry->fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +0000111 }
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000112 entry->copyAndTakeOwnership(newEntry);
junov@google.comf93e7172011-03-31 21:26:24 +0000113 fHashCache.insert(entry->fKey, entry);
114 }
115
116 entry->fLRUStamp = fCurrLRUStamp;
117 if (GR_UINT32_MAX == fCurrLRUStamp) {
118 // wrap around! just trash our LRU, one time hit.
119 for (int i = 0; i < fCount; ++i) {
120 fEntries[i].fLRUStamp = 0;
121 }
122 }
123 ++fCurrLRUStamp;
124 return &entry->fProgramData;
125 }
126};
127
junov@google.com53a55842011-06-08 22:55:10 +0000128void GrGpuGLShaders::abandonResources(){
129 INHERITED::abandonResources();
130 fProgramCache->abandon();
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000131 fHWProgramID = 0;
junov@google.com53a55842011-06-08 22:55:10 +0000132}
133
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000134void GrGpuGLShaders::DeleteProgram(const GrGLInterface* gl,
135 CachedData* programData) {
136 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000137 if (programData->fGShaderID) {
138 GR_GL_CALL(gl, DeleteShader(programData->fGShaderID));
139 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000140 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
141 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000142 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
143}
144
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000145////////////////////////////////////////////////////////////////////////////////
146
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000147#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
148
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000149namespace {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000150
bsalomon@google.com74b98712011-11-11 19:46:16 +0000151// GrRandoms nextU() values have patterns in the low bits
152// So using nextU() % array_count might never take some values.
153int random_int(GrRandom* r, int count) {
154 return (int)(r->nextF() * count);
155}
156
157// min is inclusive, max is exclusive
158int random_int(GrRandom* r, int min, int max) {
159 return (int)(r->nextF() * (max-min)) + min;
160}
161
162bool random_bool(GrRandom* r) {
163 return r->nextF() > .5f;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000164}
165
166}
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000167
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000168bool GrGpuGLShaders::programUnitTest() {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000169
tomhudson@google.com086e5352011-12-08 14:44:10 +0000170 GrGLSLGeneration glslGeneration =
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000171 GrGetGLSLGeneration(this->glBinding(), this->glInterface());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000172 static const int STAGE_OPTS[] = {
173 0,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000174 StageDesc::kNoPerspective_OptFlagBit,
175 StageDesc::kIdentity_CoordMapping
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000176 };
bsalomon@google.com74b98712011-11-11 19:46:16 +0000177 static const int IN_CONFIG_FLAGS[] = {
178 StageDesc::kNone_InConfigFlag,
179 StageDesc::kSwapRAndB_InConfigFlag,
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000180 StageDesc::kSwapRAndB_InConfigFlag |
181 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag,
182 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000183 StageDesc::kSmearAlpha_InConfigFlag,
184 };
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000185 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000186 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000187
188 static const int NUM_TESTS = 512;
189
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000190 GrRandom random;
191 for (int t = 0; t < NUM_TESTS; ++t) {
192
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000193#if 0
194 GrPrintf("\nTest Program %d\n-------------\n", t);
195 static const int stop = -1;
196 if (t == stop) {
197 int breakpointhere = 9;
198 }
199#endif
200
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000201 pdesc.fVertexLayout = 0;
202 pdesc.fEmitsPointSize = random.nextF() > .5f;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000203 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000204 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000205
bsalomon@google.com74b98712011-11-11 19:46:16 +0000206 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000207
bsalomon@google.com74b98712011-11-11 19:46:16 +0000208 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000209
bsalomon@google.com74b98712011-11-11 19:46:16 +0000210 pdesc.fVertexLayout |= random_bool(&random) ?
bsalomon@google.coma3108262011-10-10 14:08:47 +0000211 GrDrawTarget::kCoverage_VertexLayoutBit :
212 0;
213
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000214#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com6f92f182011-09-29 15:05:48 +0000215 pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport &&
bsalomon@google.com74b98712011-11-11 19:46:16 +0000216 random_bool(&random);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000217#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000218 pdesc.fOutputConfig = random_int(&random, ProgramDesc::kOutputConfigCnt);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000219
bsalomon@google.com74b98712011-11-11 19:46:16 +0000220 bool edgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000221 if (edgeAA) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000222 bool vertexEdgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000223 if (vertexEdgeAA) {
224 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000225 if (this->getCaps().fShaderDerivativeSupport) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000226 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000227 } else {
tomhudson@google.com93813632011-10-27 20:21:16 +0000228 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000229 }
230 pdesc.fEdgeAANumEdges = 0;
231 } else {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000232 pdesc.fEdgeAANumEdges = random_int(&random, 1, this->getMaxEdges());
233 pdesc.fEdgeAAConcave = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000234 }
235 } else {
236 pdesc.fEdgeAANumEdges = 0;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000237 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000238
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000239 pdesc.fColorMatrixEnabled = random_bool(&random);
240
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000241 if (this->getCaps().fDualSourceBlendingSupport) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000242 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000243 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000244 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000245 }
246
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000247 GrCustomStage* customStages[GrDrawState::kNumStages];
248
tomhudson@google.com93813632011-10-27 20:21:16 +0000249 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000250 // enable the stage?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000251 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000252 // use separate tex coords?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000253 if (random_bool(&random)) {
254 int t = random_int(&random, GrDrawState::kMaxTexCoords);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000255 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
256 } else {
257 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
258 }
259 }
260 // use text-formatted verts?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000261 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000262 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
263 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000264 StageDesc& stage = pdesc.fStages[s];
bsalomon@google.com74b98712011-11-11 19:46:16 +0000265 stage.fOptFlags = STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
266 stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];
267 stage.fCoordMapping = random_int(&random, StageDesc::kCoordMappingCnt);
268 stage.fFetchMode = random_int(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000269 // convolution shaders don't work with persp tex matrix
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000270 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode ||
271 stage.fFetchMode == StageDesc::kDilate_FetchMode ||
272 stage.fFetchMode == StageDesc::kErode_FetchMode) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000273 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
274 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000275 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000276 static const uint32_t kMulByAlphaMask =
277 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
278 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000279 switch (stage.fFetchMode) {
280 case StageDesc::kSingle_FetchMode:
281 stage.fKernelWidth = 0;
282 break;
283 case StageDesc::kConvolution_FetchMode:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000284 case StageDesc::kDilate_FetchMode:
285 case StageDesc::kErode_FetchMode:
bsalomon@google.com74b98712011-11-11 19:46:16 +0000286 stage.fKernelWidth = random_int(&random, 2, 8);
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000287 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000288 break;
289 case StageDesc::k2x2_FetchMode:
290 stage.fKernelWidth = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000291 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000292 break;
293 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000294
295 stage.fCustomStageKey = 0;
296 customStages[s] = NULL;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000297 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000298 CachedData cachedData;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000299 if (!program.genProgram(this->glContextInfo(), customStages,
300 &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000301 return false;
302 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000303 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000304 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000305 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000306}
307
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000308GrGpuGLShaders::GrGpuGLShaders(const GrGLContextInfo& ctxInfo)
309 : GrGpuGL(ctxInfo) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000310
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000311 // Enable supported shader-related caps
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000312 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000313 fCaps.fDualSourceBlendingSupport =
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000314 this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000315 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000316 fCaps.fShaderDerivativeSupport = true;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000317 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
318 fCaps.fGeometryShaderSupport =
319 this->glVersion() >= GR_GL_VER(3,2) &&
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000320 this->glslGeneration() >= k150_GrGLSLGeneration;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000321 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000322 fCaps.fShaderDerivativeSupport =
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000323 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000324 }
junov@google.comf93e7172011-03-31 21:26:24 +0000325
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000326 GR_GL_GetIntegerv(this->glInterface(),
327 GR_GL_MAX_VERTEX_ATTRIBS,
328 &fMaxVertexAttribs);
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000329
junov@google.comf93e7172011-03-31 21:26:24 +0000330 fProgramData = NULL;
bsalomon@google.com96399942012-02-13 14:39:16 +0000331 fProgramCache = new ProgramCache(this->glContextInfo());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000332
333#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000334 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000335#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000336}
337
338GrGpuGLShaders::~GrGpuGLShaders() {
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000339
340 if (fProgramData && 0 != fHWProgramID) {
341 // detach the current program so there is no confusion on OpenGL's part
342 // that we want it to be deleted
343 SkASSERT(fHWProgramID == fProgramData->fProgramID);
344 GL_CALL(UseProgram(0));
345 }
junov@google.comf93e7172011-03-31 21:26:24 +0000346 delete fProgramCache;
347}
348
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000349const GrMatrix& GrGpuGLShaders::getHWViewMatrix() {
350 GrAssert(fProgramData);
351
352 if (GrGLProgram::kSetAsAttribute ==
353 fProgramData->fUniLocations.fViewMatrixUni) {
354 return fHWDrawState.getViewMatrix();
355 } else {
356 return fProgramData->fViewMatrix;
357 }
358}
359
360void GrGpuGLShaders::recordHWViewMatrix(const GrMatrix& matrix) {
361 GrAssert(fProgramData);
362 if (GrGLProgram::kSetAsAttribute ==
363 fProgramData->fUniLocations.fViewMatrixUni) {
364 fHWDrawState.setViewMatrix(matrix);
365 } else {
366 fProgramData->fViewMatrix = matrix;
367 }
368}
369
junov@google.comf93e7172011-03-31 21:26:24 +0000370const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000371 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000372
373 if (GrGLProgram::kSetAsAttribute ==
374 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000375 return fHWDrawState.getSampler(stage).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000376 } else {
377 return fProgramData->fTextureMatrices[stage];
378 }
junov@google.comf93e7172011-03-31 21:26:24 +0000379}
380
381void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000382 GrAssert(fProgramData);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000383 if (GrGLProgram::kSetAsAttribute ==
bsalomon@google.com91961302011-05-09 18:39:58 +0000384 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000385 *fHWDrawState.sampler(stage)->matrix() = matrix;
bsalomon@google.com91961302011-05-09 18:39:58 +0000386 } else {
387 fProgramData->fTextureMatrices[stage] = matrix;
388 }
junov@google.comf93e7172011-03-31 21:26:24 +0000389}
390
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000391void GrGpuGLShaders::onResetContext() {
392 INHERITED::onResetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000393
junov@google.comf93e7172011-03-31 21:26:24 +0000394 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000395
396 // Third party GL code may have left vertex attributes enabled. Some GL
397 // implementations (osmesa) may read vetex attributes that are not required
398 // by the current shader. Therefore, we have to ensure that only the
399 // attributes we require for the current draw are enabled or we may cause an
400 // invalid read.
401
402 // Disable all vertex layout bits so that next flush will assume all
403 // optional vertex attributes are disabled.
404 fHWGeometryState.fVertexLayout = 0;
405
406 // We always use the this attribute and assume it is always enabled.
407 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
408 GL_CALL(EnableVertexAttribArray(posAttrIdx));
409 // Disable all other vertex attributes.
410 for (int va = 0; va < fMaxVertexAttribs; ++va) {
411 if (va != posAttrIdx) {
412 GL_CALL(DisableVertexAttribArray(va));
413 }
junov@google.comf93e7172011-03-31 21:26:24 +0000414 }
junov@google.comf93e7172011-03-31 21:26:24 +0000415
416 fHWProgramID = 0;
417}
418
419void GrGpuGLShaders::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000420 const GrMatrix& vm = this->getDrawState().getViewMatrix();
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000421 if (!GrGpuGLShaders::getHWViewMatrix().cheapEqualTo(vm)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000422
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000423 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
424 GrAssert(NULL != rt);
425 GrMatrix m;
426 m.setAll(
427 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
428 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
429 0, 0, GrMatrix::I()[8]);
430 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000431
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000432 // ES doesn't allow you to pass true to the transpose param,
433 // so do our own transpose
434 GrGLfloat mt[] = {
435 GrScalarToFloat(m[GrMatrix::kMScaleX]),
436 GrScalarToFloat(m[GrMatrix::kMSkewY]),
437 GrScalarToFloat(m[GrMatrix::kMPersp0]),
438 GrScalarToFloat(m[GrMatrix::kMSkewX]),
439 GrScalarToFloat(m[GrMatrix::kMScaleY]),
440 GrScalarToFloat(m[GrMatrix::kMPersp1]),
441 GrScalarToFloat(m[GrMatrix::kMTransX]),
442 GrScalarToFloat(m[GrMatrix::kMTransY]),
443 GrScalarToFloat(m[GrMatrix::kMPersp2])
444 };
445
446 if (GrGLProgram::kSetAsAttribute ==
447 fProgramData->fUniLocations.fViewMatrixUni) {
448 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
449 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
450 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
451 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
452 } else {
453 GrAssert(GrGLProgram::kUnusedUniform !=
454 fProgramData->fUniLocations.fViewMatrixUni);
455 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
456 1, false, mt));
457 }
458 this->recordHWViewMatrix(vm);
bsalomon@google.com91961302011-05-09 18:39:58 +0000459 }
junov@google.comf93e7172011-03-31 21:26:24 +0000460}
461
junov@google.com6acc9b32011-05-16 18:32:07 +0000462void GrGpuGLShaders::flushTextureDomain(int s) {
463 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000464 const GrDrawState& drawState = this->getDrawState();
junov@google.com6acc9b32011-05-16 18:32:07 +0000465 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000466 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
junov@google.com6acc9b32011-05-16 18:32:07 +0000467
twiz@google.com76b82742011-06-02 20:30:02 +0000468 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
469 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000470
junov@google.com2f839402011-05-24 15:13:01 +0000471 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000472
junov@google.com2f839402011-05-24 15:13:01 +0000473 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000474 GrScalarToFloat(texDom.left()),
475 GrScalarToFloat(texDom.top()),
476 GrScalarToFloat(texDom.right()),
477 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000478 };
479
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000480 const GrGLTexture* texture =
481 static_cast<const GrGLTexture*>(drawState.getTexture(s));
junov@google.com2f839402011-05-24 15:13:01 +0000482 GrGLTexture::Orientation orientation = texture->orientation();
483
484 // vertical flip if necessary
485 if (GrGLTexture::kBottomUp_Orientation == orientation) {
486 values[1] = 1.0f - values[1];
487 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000488 // The top and bottom were just flipped, so correct the ordering
489 // of elements so that values = (l, t, r, b).
490 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000491 }
492
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000493 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000494 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000495 }
496}
497
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000498void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000499 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000500 const GrDrawState& drawState = this->getDrawState();
501 const GrGLTexture* texture =
502 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000503 if (NULL != texture) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000504 const GrMatrix& hwMatrix = this->getHWSamplerMatrix(s);
505 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000506 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000507 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000508 !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000509
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000510 GrMatrix m = samplerMatrix;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000511 GrSamplerState::SampleMode mode =
512 drawState.getSampler(s).getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000513 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000514
bsalomon@google.com91961302011-05-09 18:39:58 +0000515 // ES doesn't allow you to pass true to the transpose param,
516 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000517 GrGLfloat mt[] = {
518 GrScalarToFloat(m[GrMatrix::kMScaleX]),
519 GrScalarToFloat(m[GrMatrix::kMSkewY]),
520 GrScalarToFloat(m[GrMatrix::kMPersp0]),
521 GrScalarToFloat(m[GrMatrix::kMSkewX]),
522 GrScalarToFloat(m[GrMatrix::kMScaleY]),
523 GrScalarToFloat(m[GrMatrix::kMPersp1]),
524 GrScalarToFloat(m[GrMatrix::kMTransX]),
525 GrScalarToFloat(m[GrMatrix::kMTransY]),
526 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000527 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000528
bsalomon@google.com91961302011-05-09 18:39:58 +0000529 if (GrGLProgram::kSetAsAttribute ==
530 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
531 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000532 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
533 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
534 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000535 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000536 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000537 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000538 this->recordHWSamplerMatrix(s, drawState.getSampler(s).getMatrix());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000539 }
540 }
junov@google.comf93e7172011-03-31 21:26:24 +0000541}
542
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000543void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000544
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000545 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000546 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.com91961302011-05-09 18:39:58 +0000547 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000548 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
549 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
550 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000551
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000552 GrScalar centerX1 = sampler.getRadial2CenterX1();
553 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000554
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000555 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000556
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000557 // when were in the degenerate (linear) case the second
558 // value will be INF but the program doesn't read it. (We
559 // use the same 6 uniforms even though we don't need them
560 // all in the linear case just to keep the code complexity
561 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000562 float values[6] = {
563 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000564 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000565 GrScalarToFloat(centerX1),
566 GrScalarToFloat(radius0),
567 GrScalarToFloat(GrMul(radius0, radius0)),
568 sampler.isRadial2PosRoot() ? 1.f : -1.f
569 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000570 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000571 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
572 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
573 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
574 }
575}
576
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000577void GrGpuGLShaders::flushConvolution(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000578 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000579 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
580 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000581 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
582 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000583 }
584 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
585 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000586 const GrGLTexture* texture =
587 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
588 float imageIncrement[2] = { 0 };
589 switch (sampler.getFilterDirection()) {
590 case GrSamplerState::kX_FilterDirection:
591 imageIncrement[0] = 1.0f / texture->width();
592 break;
593 case GrSamplerState::kY_FilterDirection:
594 imageIncrement[1] = 1.0f / texture->height();
595 break;
596 default:
597 GrCrash("Unknown filter direction.");
598 }
599 GL_CALL(Uniform2fv(imageIncrementUni, 1, imageIncrement));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000600 }
601}
602
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000603void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000604 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000605 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000606 const GrGLTexture* texture =
607 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000608 if (texture->width() != fProgramData->fTextureWidth[s] ||
609 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000610
bsalomon@google.com99621082011-11-15 16:47:16 +0000611 float texelSize[] = {1.f / texture->width(),
612 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000613 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000614 fProgramData->fTextureWidth[s] = texture->width();
615 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000616 }
617 }
junov@google.comf93e7172011-03-31 21:26:24 +0000618}
619
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000620void GrGpuGLShaders::flushEdgeAAData() {
621 const int& uni = fProgramData->fUniLocations.fEdgesUni;
622 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000623 int count = this->getDrawState().getNumAAEdges();
tomhudson@google.com93813632011-10-27 20:21:16 +0000624 GrDrawState::Edge edges[GrDrawState::kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000625 // Flip the edges in Y
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000626 float height =
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000627 static_cast<float>(this->getDrawState().getRenderTarget()->height());
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000628 for (int i = 0; i < count; ++i) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000629 edges[i] = this->getDrawState().getAAEdges()[i];
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000630 float b = edges[i].fY;
631 edges[i].fY = -b;
632 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000633 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000634 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000635 }
636}
637
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000638void GrGpuGLShaders::flushColorMatrix() {
639 const ProgramDesc& desc = fCurrentProgram.getDesc();
640 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
641 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
642 if (GrGLProgram::kUnusedUniform != matrixUni
643 && GrGLProgram::kUnusedUniform != vecUni) {
644 const float* m = this->getDrawState().getColorMatrix();
645 GrGLfloat mt[] = {
646 m[0], m[5], m[10], m[15],
647 m[1], m[6], m[11], m[16],
648 m[2], m[7], m[12], m[17],
649 m[3], m[8], m[13], m[18],
650 };
651 static float scale = 1.0f / 255.0f;
652 GrGLfloat vec[] = {
653 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
654 };
655 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
656 GL_CALL(Uniform4fv(vecUni, 1, vec));
657 }
658}
659
Scroggo01b87ec2011-05-11 18:05:38 +0000660static const float ONE_OVER_255 = 1.f / 255.f;
661
662#define GR_COLOR_TO_VEC4(color) {\
663 GrColorUnpackR(color) * ONE_OVER_255,\
664 GrColorUnpackG(color) * ONE_OVER_255,\
665 GrColorUnpackB(color) * ONE_OVER_255,\
666 GrColorUnpackA(color) * ONE_OVER_255 \
667}
668
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000669void GrGpuGLShaders::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000670 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000671 const GrDrawState& drawState = this->getDrawState();
672
bsalomon@google.come79c8152012-03-29 19:07:12 +0000673 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000674 // color will be specified per-vertex as an attribute
675 // invalidate the const vertex attrib color
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000676 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000677 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000678 switch (desc.fColorInput) {
679 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000680 if (fHWDrawState.getColor() != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000681 // OpenGL ES only supports the float varieties of
682 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000683 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000684 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
685 c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000686 fHWDrawState.setColor(color);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000687 }
688 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000689 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000690 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000691 // OpenGL ES doesn't support unsigned byte varieties of
692 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000693 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000694 GrAssert(GrGLProgram::kUnusedUniform !=
695 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000696 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
697 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000698 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000699 }
700 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000701 case ProgramDesc::kSolidWhite_ColorInput:
702 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000703 break;
704 default:
705 GrCrash("Unknown color type.");
706 }
707 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000708 if (fProgramData->fUniLocations.fColorFilterUni
709 != GrGLProgram::kUnusedUniform
710 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000711 != drawState.getColorFilterColor()) {
712 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000713 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000714 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000715 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000716}
717
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000718void GrGpuGLShaders::flushCoverage(GrColor coverage) {
719 const ProgramDesc& desc = fCurrentProgram.getDesc();
720 const GrDrawState& drawState = this->getDrawState();
721
722
bsalomon@google.come79c8152012-03-29 19:07:12 +0000723 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000724 // coverage will be specified per-vertex as an attribute
725 // invalidate the const vertex attrib coverage
726 fHWDrawState.setCoverage4(GrColor_ILLEGAL);
727 } else {
728 switch (desc.fCoverageInput) {
729 case ProgramDesc::kAttribute_ColorInput:
730 if (fHWDrawState.getCoverage() != coverage) {
731 // OpenGL ES only supports the float varieties of
732 // glVertexAttrib
733 float c[] = GR_COLOR_TO_VEC4(coverage);
734 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
735 c));
736 fHWDrawState.setCoverage(coverage);
737 }
738 break;
739 case ProgramDesc::kUniform_ColorInput:
740 if (fProgramData->fCoverage != coverage) {
741 // OpenGL ES doesn't support unsigned byte varieties of
742 // glUniform
743 float c[] = GR_COLOR_TO_VEC4(coverage);
744 GrAssert(GrGLProgram::kUnusedUniform !=
745 fProgramData->fUniLocations.fCoverageUni);
746 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
747 1, c));
748 fProgramData->fCoverage = coverage;
749 }
750 break;
751 case ProgramDesc::kSolidWhite_ColorInput:
752 case ProgramDesc::kTransBlack_ColorInput:
753 break;
754 default:
755 GrCrash("Unknown coverage type.");
756 }
757 }
758}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000759
junov@google.comf93e7172011-03-31 21:26:24 +0000760bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
761 if (!flushGLStateCommon(type)) {
762 return false;
763 }
764
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000765 const GrDrawState& drawState = this->getDrawState();
766
junov@google.comf93e7172011-03-31 21:26:24 +0000767 if (fDirtyFlags.fRenderTargetChanged) {
768 // our coords are in pixel space and the GL matrices map to NDC
769 // so if the viewport changed, our matrix is now wrong.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000770 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
junov@google.comf93e7172011-03-31 21:26:24 +0000771 // we assume all shader matrices may be wrong after viewport changes
772 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000773 }
774
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000775 GrBlendCoeff srcCoeff;
776 GrBlendCoeff dstCoeff;
777 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
778 if (kSkipDraw_BlendOptFlag & blendOpts) {
779 return false;
780 }
781
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000782 GrCustomStage* customStages [GrDrawState::kNumStages];
783 this->buildProgram(type, blendOpts, dstCoeff, customStages);
784 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
785 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000786 if (NULL == fProgramData) {
787 GrAssert(!"Failed to create program!");
788 return false;
789 }
junov@google.comf93e7172011-03-31 21:26:24 +0000790
791 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000792 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000793 fHWProgramID = fProgramData->fProgramID;
794 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000795 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
796 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000797
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000798 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000799 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000800 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
801 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000802 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000803 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
804 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000805 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000806 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000807 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000808 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000809 }
810 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000811 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000812
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000813 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000814
tomhudson@google.com93813632011-10-27 20:21:16 +0000815 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000816 if (this->isStageEnabled(s)) {
817 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000818
bsalomon@google.com40d92932011-12-13 18:40:47 +0000819 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000820
bsalomon@google.com40d92932011-12-13 18:40:47 +0000821 this->flushConvolution(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000822
bsalomon@google.com40d92932011-12-13 18:40:47 +0000823 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000824
bsalomon@google.com40d92932011-12-13 18:40:47 +0000825 this->flushTextureDomain(s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000826
827 if (NULL != fProgramData->fCustomStage[s]) {
828 const GrSamplerState& sampler =
829 this->getDrawState().getSampler(s);
830 fProgramData->fCustomStage[s]->setData(
831 this->glInterface(), sampler.getCustomStage());
832 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000833 }
junov@google.comf93e7172011-03-31 21:26:24 +0000834 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000835 this->flushEdgeAAData();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000836 this->flushColorMatrix();
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000837 resetDirtyFlags();
junov@google.comf93e7172011-03-31 21:26:24 +0000838 return true;
839}
840
841void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000842}
843
844void GrGpuGLShaders::setupGeometry(int* startVertex,
845 int* startIndex,
846 int vertexCount,
847 int indexCount) {
848
849 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000850 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000851 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000852 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000853
bsalomon@google.come79c8152012-03-29 19:07:12 +0000854 GrVertexLayout currLayout = this->getVertexLayout();
855
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000856 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000857 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000858 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000859 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000860 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000861 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000862 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000863 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000864 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000865 int oldEdgeOffset;
866
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000867 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
868 fHWGeometryState.fVertexLayout,
869 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000870 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000871 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000872 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000873 bool indexed = NULL != startIndex;
874
875 int extraVertexOffset;
876 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000877 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000878
879 GrGLenum scalarType;
880 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000881 if (currLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000882 scalarType = GrGLTextType;
883 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
884 } else {
885 scalarType = GrGLType;
886 texCoordNorm = false;
887 }
888
889 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
890 *startVertex = 0;
891 if (indexed) {
892 *startIndex += extraIndexOffset;
893 }
894
895 // all the Pointers must be set if any of these are true
896 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
897 vertexOffset != fHWGeometryState.fVertexOffset ||
898 newStride != oldStride;
899
900 // position and tex coord offsets change if above conditions are true
901 // or the type/normalization changed based on text vs nontext type coords.
902 bool posAndTexChange = allOffsetsChange ||
903 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
904 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000905 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000906
907 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000908 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000909 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000910 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000911 fHWGeometryState.fVertexOffset = vertexOffset;
912 }
913
tomhudson@google.com93813632011-10-27 20:21:16 +0000914 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000915 if (newTexCoordOffsets[t] > 0) {
916 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000917 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000918 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000919 GL_CALL(EnableVertexAttribArray(idx));
920 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000921 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000922 } else if (posAndTexChange ||
923 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000924 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000925 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000926 }
927 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000928 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000929 }
930 }
931
932 if (newColorOffset > 0) {
933 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000934 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000935 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000936 GL_CALL(EnableVertexAttribArray(idx));
937 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000938 true, newStride, colorOffset));
939 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000940 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000941 true, newStride, colorOffset));
942 }
943 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000944 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000945 }
946
bsalomon@google.coma3108262011-10-10 14:08:47 +0000947 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000948 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000949 int idx = GrGLProgram::CoverageAttributeIdx();
950 if (oldCoverageOffset <= 0) {
951 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000952 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000953 true, newStride, coverageOffset));
954 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000955 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000956 true, newStride, coverageOffset));
957 }
958 } else if (oldCoverageOffset > 0) {
959 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
960 }
961
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000962 if (newEdgeOffset > 0) {
963 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
964 int idx = GrGLProgram::EdgeAttributeIdx();
965 if (oldEdgeOffset <= 0) {
966 GL_CALL(EnableVertexAttribArray(idx));
967 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
968 false, newStride, edgeOffset));
969 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
970 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
971 false, newStride, edgeOffset));
972 }
973 } else if (oldEdgeOffset > 0) {
974 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
975 }
976
bsalomon@google.come79c8152012-03-29 19:07:12 +0000977 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000978 fHWGeometryState.fArrayPtrsDirty = false;
979}
980
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000981namespace {
982
983void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
984 const GrSamplerState& sampler,
985 GrCustomStage** customStages,
986 GrGLProgram* program, int index) {
987 GrCustomStage* customStage = sampler.getCustomStage();
988 if (customStage) {
989 GrGLProgramStageFactory* factory = customStage->getGLFactory();
990 stage->fCustomStageKey = factory->stageKey(customStage);
991 customStages[index] = customStage;
992 } else {
993 stage->fCustomStageKey = 0;
994 customStages[index] = NULL;
995 }
996}
997
998}
999
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001000void GrGpuGLShaders::buildProgram(GrPrimitiveType type,
1001 BlendOptFlags blendOpts,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001002 GrBlendCoeff dstCoeff,
1003 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001004 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001005 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +00001006
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001007 // This should already have been caught
1008 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
1009
1010 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
1011
1012 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
1013 kEmitCoverage_BlendOptFlag));
1014
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001015 // The descriptor is used as a cache key. Thus when a field of the
1016 // descriptor will not affect program generation (because of the vertex
1017 // layout in use or other descriptor field settings) it should be set
1018 // to a canonical value to avoid duplicate programs with different keys.
1019
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001020 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +00001021 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001022
1023 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
1024
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001025 bool requiresAttributeColors =
1026 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001027 bool requiresAttributeCoverage =
1028 !skipCoverage && SkToBool(desc.fVertexLayout &
1029 kCoverage_VertexLayoutBit);
1030
1031 // fColorInput/fCoverageInput records how colors are specified for the.
1032 // program. So we strip the bits from the layout to avoid false negatives
1033 // when searching for an existing program in the cache.
1034 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001035
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001036 desc.fColorFilterXfermode = skipColor ?
1037 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001038 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001039
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001040 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
1041
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001042 // no reason to do edge aa or look at per-vertex coverage if coverage is
1043 // ignored
1044 if (skipCoverage) {
1045 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
1046 kCoverage_VertexLayoutBit);
1047 }
1048
1049 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
1050 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
1051 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001052 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001053 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001054 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001055 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001056 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001057 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001058 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001059 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001060 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001061 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001062
1063 bool covIsSolidWhite = !requiresAttributeCoverage &&
1064 0xffffffff == drawState.getCoverage();
1065
1066 if (skipCoverage) {
1067 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
1068 } else if (covIsSolidWhite) {
1069 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
1070 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
1071 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
1072 } else {
1073 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
1074 }
junov@google.comf93e7172011-03-31 21:26:24 +00001075
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001076 desc.fEdgeAANumEdges = skipCoverage ? 0 : drawState.getNumAAEdges();
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001077 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001078 drawState.isConcaveEdgeAAState();
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +00001079
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001080 int lastEnabledStage = -1;
1081
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001082 if (!skipCoverage && (desc.fVertexLayout &
1083 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001084 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001085 } else {
1086 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +00001087 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001088 }
1089
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001090 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001091 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001092
1093 stage.fOptFlags = 0;
1094 stage.setEnabled(this->isStageEnabled(s));
1095
1096 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
1097 skipCoverage;
1098
1099 if (!skip && stage.isEnabled()) {
1100 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001101 const GrGLTexture* texture =
1102 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001103 GrAssert(NULL != texture);
1104 const GrSamplerState& sampler = drawState.getSampler(s);
1105 // we matrix to invert when orientation is TopDown, so make sure
1106 // we aren't in that case before flagging as identity.
1107 if (TextureMatrixIsIdentity(texture, sampler)) {
1108 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
1109 } else if (!sampler.getMatrix().hasPerspective()) {
1110 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
1111 }
1112 switch (sampler.getSampleMode()) {
1113 case GrSamplerState::kNormal_SampleMode:
1114 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
1115 break;
1116 case GrSamplerState::kRadial_SampleMode:
1117 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
1118 break;
1119 case GrSamplerState::kRadial2_SampleMode:
1120 if (sampler.radial2IsDegenerate()) {
1121 stage.fCoordMapping =
1122 StageDesc::kRadial2GradientDegenerate_CoordMapping;
1123 } else {
1124 stage.fCoordMapping =
1125 StageDesc::kRadial2Gradient_CoordMapping;
1126 }
1127 break;
1128 case GrSamplerState::kSweep_SampleMode:
1129 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
1130 break;
1131 default:
1132 GrCrash("Unexpected sample mode!");
1133 break;
1134 }
1135
1136 switch (sampler.getFilter()) {
1137 // these both can use a regular texture2D()
1138 case GrSamplerState::kNearest_Filter:
1139 case GrSamplerState::kBilinear_Filter:
1140 stage.fFetchMode = StageDesc::kSingle_FetchMode;
1141 break;
1142 // performs 4 texture2D()s
1143 case GrSamplerState::k4x4Downsample_Filter:
1144 stage.fFetchMode = StageDesc::k2x2_FetchMode;
1145 break;
1146 // performs fKernelWidth texture2D()s
1147 case GrSamplerState::kConvolution_Filter:
1148 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
1149 break;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001150 case GrSamplerState::kDilate_Filter:
1151 stage.fFetchMode = StageDesc::kDilate_FetchMode;
1152 break;
1153 case GrSamplerState::kErode_Filter:
1154 stage.fFetchMode = StageDesc::kErode_FetchMode;
1155 break;
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001156 default:
1157 GrCrash("Unexpected filter!");
1158 break;
1159 }
1160
1161 if (sampler.hasTextureDomain()) {
1162 GrAssert(GrSamplerState::kClamp_WrapMode ==
1163 sampler.getWrapX() &&
1164 GrSamplerState::kClamp_WrapMode ==
1165 sampler.getWrapY());
1166 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
1167 }
1168
1169 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001170 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001171 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1172 // if we don't have texture swizzle support then
epoger@google.com00484692012-04-30 19:02:08 +00001173 // the shader must do an alpha smear after reading
1174 // the texture
1175 stage.fInConfigFlags |= StageDesc::kSmearAlpha_InConfigFlag;
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001176 } else if (sampler.swapsRAndB()) {
1177 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
1178 }
1179 }
1180 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001181 // The shader generator assumes that color channels are bytes
1182 // when rounding.
1183 GrAssert(4 == GrBytesPerPixel(texture->config()));
1184 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
1185 fUnpremulConversion) {
1186 stage.fInConfigFlags |=
1187 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
1188 } else {
1189 stage.fInConfigFlags |=
1190 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
1191 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001192 }
1193
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001194 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter ||
1195 sampler.getFilter() == GrSamplerState::kDilate_Filter ||
1196 sampler.getFilter() == GrSamplerState::kErode_Filter) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001197 stage.fKernelWidth = sampler.getKernelWidth();
1198 } else {
1199 stage.fKernelWidth = 0;
1200 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001201
1202 setup_custom_stage(&stage, sampler, customStages,
1203 &fCurrentProgram, s);
1204
junov@google.comf93e7172011-03-31 21:26:24 +00001205 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001206 stage.fOptFlags = 0;
1207 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1208 stage.fInConfigFlags = 0;
1209 stage.fFetchMode = (StageDesc::FetchMode) 0;
1210 stage.fKernelWidth = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001211 stage.fCustomStageKey = 0;
1212 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +00001213 }
1214 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001215
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001216 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001217 // The shader generator assumes that color channels are bytes
1218 // when rounding.
1219 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
1220 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
1221 desc.fOutputConfig =
1222 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
1223 } else {
1224 desc.fOutputConfig =
1225 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
1226 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001227 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001228 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001229 }
1230
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001231 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001232
1233 // currently the experimental GS will only work with triangle prims
1234 // (and it doesn't do anything other than pass through values from
1235 // the VS to the FS anyway).
1236#if 0 && GR_GL_EXPERIMENTAL_GS
1237 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1238#endif
1239
bsalomon@google.coma3108262011-10-10 14:08:47 +00001240 // we want to avoid generating programs with different "first cov stage"
1241 // values when they would compute the same result.
1242 // We set field in the desc to kNumStages when either there are no
1243 // coverage stages or the distinction between coverage and color is
1244 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001245 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +00001246 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001247 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001248 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001249 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +00001250 }
1251
1252 // other coverage inputs
1253 if (!hasCoverage) {
1254 hasCoverage =
1255 desc.fEdgeAANumEdges ||
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001256 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +00001257 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1258 }
1259
1260 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001261 // color filter is applied between color/coverage computation
1262 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001263 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001264 }
1265
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001266 if (this->getCaps().fDualSourceBlendingSupport &&
1267 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1268 kCoverageAsAlpha_BlendOptFlag))) {
1269 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001270 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001271 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001272 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001273 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001274 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1275 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001276 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001277 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001278 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001279 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1280 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001281 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001282 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001283 }
1284 }
1285 }
junov@google.comf93e7172011-03-31 21:26:24 +00001286}