blob: 2ac26d56751706668f1d0c1b06458b6fbe096c9e [file] [log] [blame]
junov@google.comf93e7172011-03-31 21:26:24 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
junov@google.comf93e7172011-03-31 21:26:24 +00006 */
7
bsalomon@google.com5739d2c2012-05-31 15:07:19 +00008#include "GrGpuGL.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000010#include "GrBinHashKey.h"
11#include "effects/GrConvolutionEffect.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000012#include "GrCustomStage.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"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000015#include "GrGpuVertex.h"
junov@google.comf93e7172011-03-31 21:26:24 +000016#include "GrNoncopyable.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000017#include "GrProgramStageFactory.h"
18#include "GrRandom.h"
19#include "GrStringBuilder.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
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000026class GrGpuGL::ProgramCache : public ::GrNoncopyable {
junov@google.comf93e7172011-03-31 21:26:24 +000027private:
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.com5739d2c2012-05-31 15:07:19 +000071 GrGpuGL::DeleteProgram(fGL.interface(),
bsalomon@google.com96399942012-02-13 14:39:16 +000072 &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.com5739d2c2012-05-31 15:07:19 +0000109 GrGpuGL::DeleteProgram(fGL.interface(),
bsalomon@google.com96399942012-02-13 14:39:16 +0000110 &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
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000128void GrGpuGL::DeleteProgram(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000129 CachedData* programData) {
130 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000131 if (programData->fGShaderID) {
132 GR_GL_CALL(gl, DeleteShader(programData->fGShaderID));
133 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000134 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
135 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000136 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
137}
138
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000139////////////////////////////////////////////////////////////////////////////////
140
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000141void GrGpuGL::createProgramCache() {
142 fProgramData = NULL;
143 fProgramCache = new ProgramCache(this->glContextInfo());
144}
145
146void GrGpuGL::deleteProgramCache() {
147 delete fProgramCache;
148 fProgramCache = NULL;
149 fProgramData = NULL;
150}
151
152void GrGpuGL::abandonResources(){
153 INHERITED::abandonResources();
154 fProgramCache->abandon();
155 fHWProgramID = 0;
156}
157
158////////////////////////////////////////////////////////////////////////////////
159
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000160#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
161
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000162namespace {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000163
bsalomon@google.com74b98712011-11-11 19:46:16 +0000164// GrRandoms nextU() values have patterns in the low bits
165// So using nextU() % array_count might never take some values.
166int random_int(GrRandom* r, int count) {
167 return (int)(r->nextF() * count);
168}
169
170// min is inclusive, max is exclusive
171int random_int(GrRandom* r, int min, int max) {
172 return (int)(r->nextF() * (max-min)) + min;
173}
174
175bool random_bool(GrRandom* r) {
176 return r->nextF() > .5f;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000177}
178
179}
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000180
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000181bool GrGpuGL::programUnitTest() {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000182
tomhudson@google.com086e5352011-12-08 14:44:10 +0000183 GrGLSLGeneration glslGeneration =
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000184 GrGetGLSLGeneration(this->glBinding(), this->glInterface());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000185 static const int STAGE_OPTS[] = {
186 0,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000187 StageDesc::kNoPerspective_OptFlagBit,
188 StageDesc::kIdentity_CoordMapping
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000189 };
bsalomon@google.com74b98712011-11-11 19:46:16 +0000190 static const int IN_CONFIG_FLAGS[] = {
191 StageDesc::kNone_InConfigFlag,
192 StageDesc::kSwapRAndB_InConfigFlag,
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000193 StageDesc::kSwapRAndB_InConfigFlag |
194 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag,
195 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000196 StageDesc::kSmearAlpha_InConfigFlag,
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000197 StageDesc::kSmearRed_InConfigFlag,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000198 };
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000199 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000200 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000201
202 static const int NUM_TESTS = 512;
203
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000204 GrRandom random;
205 for (int t = 0; t < NUM_TESTS; ++t) {
206
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000207#if 0
208 GrPrintf("\nTest Program %d\n-------------\n", t);
209 static const int stop = -1;
210 if (t == stop) {
211 int breakpointhere = 9;
212 }
213#endif
214
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000215 pdesc.fVertexLayout = 0;
216 pdesc.fEmitsPointSize = random.nextF() > .5f;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000217 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000218 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000219
bsalomon@google.com74b98712011-11-11 19:46:16 +0000220 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000221
bsalomon@google.com74b98712011-11-11 19:46:16 +0000222 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000223
bsalomon@google.com74b98712011-11-11 19:46:16 +0000224 pdesc.fVertexLayout |= random_bool(&random) ?
bsalomon@google.coma3108262011-10-10 14:08:47 +0000225 GrDrawTarget::kCoverage_VertexLayoutBit :
226 0;
227
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000228#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com6f92f182011-09-29 15:05:48 +0000229 pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport &&
bsalomon@google.com74b98712011-11-11 19:46:16 +0000230 random_bool(&random);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000231#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000232 pdesc.fOutputConfig = random_int(&random, ProgramDesc::kOutputConfigCnt);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000233
bsalomon@google.com74b98712011-11-11 19:46:16 +0000234 bool edgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000235 if (edgeAA) {
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000236 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
237 if (this->getCaps().fShaderDerivativeSupport) {
238 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000239 } else {
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000240 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000241 }
242 } else {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000243 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000244
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000245 pdesc.fColorMatrixEnabled = random_bool(&random);
246
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000247 if (this->getCaps().fDualSourceBlendingSupport) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000248 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000249 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000250 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000251 }
252
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000253 GrCustomStage* customStages[GrDrawState::kNumStages];
254
tomhudson@google.com93813632011-10-27 20:21:16 +0000255 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000256 // enable the stage?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000257 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000258 // use separate tex coords?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000259 if (random_bool(&random)) {
260 int t = random_int(&random, GrDrawState::kMaxTexCoords);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000261 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
262 } else {
263 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
264 }
265 }
266 // use text-formatted verts?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000267 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000268 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
269 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000270 StageDesc& stage = pdesc.fStages[s];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000271
272 stage.fCustomStageKey = 0;
273 customStages[s] = NULL;
274
bsalomon@google.com74b98712011-11-11 19:46:16 +0000275 stage.fOptFlags = STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
276 stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];
277 stage.fCoordMapping = random_int(&random, StageDesc::kCoordMappingCnt);
278 stage.fFetchMode = random_int(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000279 // convolution shaders don't work with persp tex matrix
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000280 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode ||
281 stage.fFetchMode == StageDesc::kDilate_FetchMode ||
282 stage.fFetchMode == StageDesc::kErode_FetchMode) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000283 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
284 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000285 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000286 static const uint32_t kMulByAlphaMask =
287 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
288 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000289
bsalomon@google.com74b98712011-11-11 19:46:16 +0000290 switch (stage.fFetchMode) {
291 case StageDesc::kSingle_FetchMode:
292 stage.fKernelWidth = 0;
293 break;
294 case StageDesc::kConvolution_FetchMode:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000295 case StageDesc::kDilate_FetchMode:
296 case StageDesc::kErode_FetchMode:
bsalomon@google.com74b98712011-11-11 19:46:16 +0000297 stage.fKernelWidth = random_int(&random, 2, 8);
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000298 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000299 break;
300 case StageDesc::k2x2_FetchMode:
301 stage.fKernelWidth = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000302 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000303 break;
304 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000305
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000306 // TODO: is there a more elegant way to express this?
307 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) {
308 int direction = random_int(&random, 2);
tomhudson@google.comf1d88062012-05-10 12:43:21 +0000309 float kernel[MAX_KERNEL_WIDTH];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000310 for (int i = 0; i < stage.fKernelWidth; i++) {
311 kernel[i] = random.nextF();
312 }
313 customStages[s] = new GrConvolutionEffect(
314 (GrSamplerState::FilterDirection)direction,
315 stage.fKernelWidth, kernel);
316 stage.fCustomStageKey =
bsalomon@google.com16fd21b2012-05-21 21:18:13 +0000317 customStages[s]->getFactory().glStageKey(customStages[s]);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000318 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000319 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000320 CachedData cachedData;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000321 if (!program.genProgram(this->glContextInfo(), customStages,
322 &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000323 return false;
324 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000325 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000326 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000327 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000328}
329
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000330void GrGpuGL::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000331 const GrMatrix& vm = this->getDrawState().getViewMatrix();
bsalomon@google.com341767c2012-05-11 20:47:39 +0000332 if (!fProgramData->fViewMatrix.cheapEqualTo(vm)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000333
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000334 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
335 GrAssert(NULL != rt);
336 GrMatrix m;
337 m.setAll(
338 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
339 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
340 0, 0, GrMatrix::I()[8]);
341 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000342
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000343 // ES doesn't allow you to pass true to the transpose param,
344 // so do our own transpose
345 GrGLfloat mt[] = {
346 GrScalarToFloat(m[GrMatrix::kMScaleX]),
347 GrScalarToFloat(m[GrMatrix::kMSkewY]),
348 GrScalarToFloat(m[GrMatrix::kMPersp0]),
349 GrScalarToFloat(m[GrMatrix::kMSkewX]),
350 GrScalarToFloat(m[GrMatrix::kMScaleY]),
351 GrScalarToFloat(m[GrMatrix::kMPersp1]),
352 GrScalarToFloat(m[GrMatrix::kMTransX]),
353 GrScalarToFloat(m[GrMatrix::kMTransY]),
354 GrScalarToFloat(m[GrMatrix::kMPersp2])
355 };
356
bsalomon@google.com341767c2012-05-11 20:47:39 +0000357 GrAssert(GrGLProgram::kUnusedUniform !=
358 fProgramData->fUniLocations.fViewMatrixUni);
359 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
360 1, false, mt));
361 fProgramData->fViewMatrix = vm;
bsalomon@google.com91961302011-05-09 18:39:58 +0000362 }
junov@google.comf93e7172011-03-31 21:26:24 +0000363}
364
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000365void GrGpuGL::flushTextureDomain(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000366 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000367 const GrDrawState& drawState = this->getDrawState();
junov@google.com6acc9b32011-05-16 18:32:07 +0000368 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000369 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
junov@google.com6acc9b32011-05-16 18:32:07 +0000370
twiz@google.com76b82742011-06-02 20:30:02 +0000371 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
372 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000373
junov@google.com2f839402011-05-24 15:13:01 +0000374 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000375
junov@google.com2f839402011-05-24 15:13:01 +0000376 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000377 GrScalarToFloat(texDom.left()),
378 GrScalarToFloat(texDom.top()),
379 GrScalarToFloat(texDom.right()),
380 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000381 };
382
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000383 const GrGLTexture* texture =
384 static_cast<const GrGLTexture*>(drawState.getTexture(s));
junov@google.com2f839402011-05-24 15:13:01 +0000385 GrGLTexture::Orientation orientation = texture->orientation();
386
387 // vertical flip if necessary
388 if (GrGLTexture::kBottomUp_Orientation == orientation) {
389 values[1] = 1.0f - values[1];
390 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000391 // The top and bottom were just flipped, so correct the ordering
392 // of elements so that values = (l, t, r, b).
393 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000394 }
395
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000396 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000397 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000398 }
399}
400
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000401void GrGpuGL::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000402 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000403 const GrDrawState& drawState = this->getDrawState();
404 const GrGLTexture* texture =
405 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000406 if (NULL != texture) {
bsalomon@google.com341767c2012-05-11 20:47:39 +0000407 const GrMatrix& hwMatrix = fProgramData->fTextureMatrices[s];
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000408 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000409 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000410 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000411 !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000412
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000413 GrMatrix m = samplerMatrix;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000414 GrSamplerState::SampleMode mode =
415 drawState.getSampler(s).getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000416 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000417
bsalomon@google.com91961302011-05-09 18:39:58 +0000418 // ES doesn't allow you to pass true to the transpose param,
419 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000420 GrGLfloat mt[] = {
421 GrScalarToFloat(m[GrMatrix::kMScaleX]),
422 GrScalarToFloat(m[GrMatrix::kMSkewY]),
423 GrScalarToFloat(m[GrMatrix::kMPersp0]),
424 GrScalarToFloat(m[GrMatrix::kMSkewX]),
425 GrScalarToFloat(m[GrMatrix::kMScaleY]),
426 GrScalarToFloat(m[GrMatrix::kMPersp1]),
427 GrScalarToFloat(m[GrMatrix::kMTransX]),
428 GrScalarToFloat(m[GrMatrix::kMTransY]),
429 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000430 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000431
bsalomon@google.com341767c2012-05-11 20:47:39 +0000432 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
433 fProgramData->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000434 }
435 }
junov@google.comf93e7172011-03-31 21:26:24 +0000436}
437
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000438void GrGpuGL::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000439
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000440 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000441 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.com91961302011-05-09 18:39:58 +0000442 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000443 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
444 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
445 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000446
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000447 GrScalar centerX1 = sampler.getRadial2CenterX1();
448 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000449
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000450 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000451
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000452 // when were in the degenerate (linear) case the second
453 // value will be INF but the program doesn't read it. (We
454 // use the same 6 uniforms even though we don't need them
455 // all in the linear case just to keep the code complexity
456 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000457 float values[6] = {
458 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000459 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000460 GrScalarToFloat(centerX1),
461 GrScalarToFloat(radius0),
462 GrScalarToFloat(GrMul(radius0, radius0)),
463 sampler.isRadial2PosRoot() ? 1.f : -1.f
464 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000465 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000466 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
467 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
468 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
469 }
470}
471
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000472void GrGpuGL::flushConvolution(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000473 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000474 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
475 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000476 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
477 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000478 }
479 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
480 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000481 const GrGLTexture* texture =
482 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
483 float imageIncrement[2] = { 0 };
484 switch (sampler.getFilterDirection()) {
485 case GrSamplerState::kX_FilterDirection:
486 imageIncrement[0] = 1.0f / texture->width();
487 break;
488 case GrSamplerState::kY_FilterDirection:
489 imageIncrement[1] = 1.0f / texture->height();
490 break;
491 default:
492 GrCrash("Unknown filter direction.");
493 }
494 GL_CALL(Uniform2fv(imageIncrementUni, 1, imageIncrement));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000495 }
496}
497
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000498void GrGpuGL::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000499 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000500 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000501 const GrGLTexture* texture =
502 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000503 if (texture->width() != fProgramData->fTextureWidth[s] ||
504 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000505
bsalomon@google.com99621082011-11-15 16:47:16 +0000506 float texelSize[] = {1.f / texture->width(),
507 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000508 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000509 fProgramData->fTextureWidth[s] = texture->width();
510 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000511 }
512 }
junov@google.comf93e7172011-03-31 21:26:24 +0000513}
514
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000515void GrGpuGL::flushColorMatrix() {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000516 const ProgramDesc& desc = fCurrentProgram.getDesc();
517 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
518 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
519 if (GrGLProgram::kUnusedUniform != matrixUni
520 && GrGLProgram::kUnusedUniform != vecUni) {
521 const float* m = this->getDrawState().getColorMatrix();
522 GrGLfloat mt[] = {
523 m[0], m[5], m[10], m[15],
524 m[1], m[6], m[11], m[16],
525 m[2], m[7], m[12], m[17],
526 m[3], m[8], m[13], m[18],
527 };
528 static float scale = 1.0f / 255.0f;
529 GrGLfloat vec[] = {
530 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
531 };
532 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
533 GL_CALL(Uniform4fv(vecUni, 1, vec));
534 }
535}
536
Scroggo01b87ec2011-05-11 18:05:38 +0000537static const float ONE_OVER_255 = 1.f / 255.f;
538
539#define GR_COLOR_TO_VEC4(color) {\
540 GrColorUnpackR(color) * ONE_OVER_255,\
541 GrColorUnpackG(color) * ONE_OVER_255,\
542 GrColorUnpackB(color) * ONE_OVER_255,\
543 GrColorUnpackA(color) * ONE_OVER_255 \
544}
545
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000546void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000547 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000548 const GrDrawState& drawState = this->getDrawState();
549
bsalomon@google.come79c8152012-03-29 19:07:12 +0000550 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000551 // color will be specified per-vertex as an attribute
552 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000553 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000554 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000555 switch (desc.fColorInput) {
556 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000557 if (fHWConstAttribColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000558 // OpenGL ES only supports the float varieties of
559 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000560 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000561 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
562 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000563 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000564 }
565 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000566 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000567 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000568 // OpenGL ES doesn't support unsigned byte varieties of
569 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000570 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000571 GrAssert(GrGLProgram::kUnusedUniform !=
572 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000573 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
574 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000575 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000576 }
577 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000578 case ProgramDesc::kSolidWhite_ColorInput:
579 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000580 break;
581 default:
582 GrCrash("Unknown color type.");
583 }
584 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000585 if (fProgramData->fUniLocations.fColorFilterUni
586 != GrGLProgram::kUnusedUniform
587 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000588 != drawState.getColorFilterColor()) {
589 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000590 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000591 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000592 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000593}
594
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000595void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000596 const ProgramDesc& desc = fCurrentProgram.getDesc();
597 const GrDrawState& drawState = this->getDrawState();
598
599
bsalomon@google.come79c8152012-03-29 19:07:12 +0000600 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000601 // coverage will be specified per-vertex as an attribute
602 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000603 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000604 } else {
605 switch (desc.fCoverageInput) {
606 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000607 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000608 // OpenGL ES only supports the float varieties of
609 // glVertexAttrib
610 float c[] = GR_COLOR_TO_VEC4(coverage);
611 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
612 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000613 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000614 }
615 break;
616 case ProgramDesc::kUniform_ColorInput:
617 if (fProgramData->fCoverage != coverage) {
618 // OpenGL ES doesn't support unsigned byte varieties of
619 // glUniform
620 float c[] = GR_COLOR_TO_VEC4(coverage);
621 GrAssert(GrGLProgram::kUnusedUniform !=
622 fProgramData->fUniLocations.fCoverageUni);
623 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
624 1, c));
625 fProgramData->fCoverage = coverage;
626 }
627 break;
628 case ProgramDesc::kSolidWhite_ColorInput:
629 case ProgramDesc::kTransBlack_ColorInput:
630 break;
631 default:
632 GrCrash("Unknown coverage type.");
633 }
634 }
635}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000636
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000637bool GrGpuGL::flushGraphicsState(GrPrimitiveType type) {
junov@google.comf93e7172011-03-31 21:26:24 +0000638 if (!flushGLStateCommon(type)) {
639 return false;
640 }
641
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000642 const GrDrawState& drawState = this->getDrawState();
643
junov@google.comf93e7172011-03-31 21:26:24 +0000644 if (fDirtyFlags.fRenderTargetChanged) {
junov@google.comf93e7172011-03-31 21:26:24 +0000645 // we assume all shader matrices may be wrong after viewport changes
646 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000647 }
648
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000649 GrBlendCoeff srcCoeff;
650 GrBlendCoeff dstCoeff;
651 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
652 if (kSkipDraw_BlendOptFlag & blendOpts) {
653 return false;
654 }
655
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000656 GrCustomStage* customStages [GrDrawState::kNumStages];
657 this->buildProgram(type, blendOpts, dstCoeff, customStages);
658 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
659 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000660 if (NULL == fProgramData) {
661 GrAssert(!"Failed to create program!");
662 return false;
663 }
junov@google.comf93e7172011-03-31 21:26:24 +0000664
665 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000666 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000667 fHWProgramID = fProgramData->fProgramID;
668 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000669 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
670 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000671
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000672 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000673 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000674 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
675 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000676 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000677 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
678 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000679 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000680 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000681 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000682 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000683 }
684 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000685 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000686
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000687 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000688
tomhudson@google.com93813632011-10-27 20:21:16 +0000689 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000690 if (this->isStageEnabled(s)) {
691 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000692
bsalomon@google.com40d92932011-12-13 18:40:47 +0000693 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000694
bsalomon@google.com40d92932011-12-13 18:40:47 +0000695 this->flushConvolution(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000696
bsalomon@google.com40d92932011-12-13 18:40:47 +0000697 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000698
bsalomon@google.com40d92932011-12-13 18:40:47 +0000699 this->flushTextureDomain(s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000700
701 if (NULL != fProgramData->fCustomStage[s]) {
702 const GrSamplerState& sampler =
703 this->getDrawState().getSampler(s);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000704 const GrGLTexture* texture =
705 static_cast<const GrGLTexture*>(
706 this->getDrawState().getTexture(s));
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000707 fProgramData->fCustomStage[s]->setData(
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000708 this->glInterface(), *texture,
709 sampler.getCustomStage(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000710 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000711 }
junov@google.comf93e7172011-03-31 21:26:24 +0000712 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000713 this->flushColorMatrix();
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000714 resetDirtyFlags();
junov@google.comf93e7172011-03-31 21:26:24 +0000715 return true;
716}
717
bsalomon@google.com2717d562012-05-07 19:10:52 +0000718#if GR_TEXT_SCALAR_IS_USHORT
719 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
720 #define TEXT_COORDS_ARE_NORMALIZED 1
721#elif GR_TEXT_SCALAR_IS_FLOAT
722 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
723 #define TEXT_COORDS_ARE_NORMALIZED 0
724#elif GR_TEXT_SCALAR_IS_FIXED
725 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
726 #define TEXT_COORDS_ARE_NORMALIZED 0
727#else
728 #error "unknown GR_TEXT_SCALAR type"
729#endif
730
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000731void GrGpuGL::setupGeometry(int* startVertex,
junov@google.comf93e7172011-03-31 21:26:24 +0000732 int* startIndex,
733 int vertexCount,
734 int indexCount) {
735
736 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000737 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000738 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000739 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000740
bsalomon@google.come79c8152012-03-29 19:07:12 +0000741 GrVertexLayout currLayout = this->getVertexLayout();
742
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000743 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000744 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000745 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000746 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000747 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000748 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000749 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000750 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000751 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000752 int oldEdgeOffset;
753
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000754 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
755 fHWGeometryState.fVertexLayout,
756 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000757 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000758 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000759 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000760 bool indexed = NULL != startIndex;
761
762 int extraVertexOffset;
763 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000764 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000765
766 GrGLenum scalarType;
767 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000768 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000769 scalarType = TEXT_COORDS_GL_TYPE;
770 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000771 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000772 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
773 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000774 texCoordNorm = false;
775 }
776
777 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
778 *startVertex = 0;
779 if (indexed) {
780 *startIndex += extraIndexOffset;
781 }
782
783 // all the Pointers must be set if any of these are true
784 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
785 vertexOffset != fHWGeometryState.fVertexOffset ||
786 newStride != oldStride;
787
788 // position and tex coord offsets change if above conditions are true
789 // or the type/normalization changed based on text vs nontext type coords.
790 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000791 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000792 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000793 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000794
795 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000796 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000797 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000798 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000799 fHWGeometryState.fVertexOffset = vertexOffset;
800 }
801
tomhudson@google.com93813632011-10-27 20:21:16 +0000802 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000803 if (newTexCoordOffsets[t] > 0) {
804 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000805 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000806 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000807 GL_CALL(EnableVertexAttribArray(idx));
808 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000809 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000810 } else if (posAndTexChange ||
811 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000812 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000813 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000814 }
815 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000816 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000817 }
818 }
819
820 if (newColorOffset > 0) {
821 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000822 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000823 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000824 GL_CALL(EnableVertexAttribArray(idx));
825 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000826 true, newStride, colorOffset));
827 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000828 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000829 true, newStride, colorOffset));
830 }
831 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000832 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000833 }
834
bsalomon@google.coma3108262011-10-10 14:08:47 +0000835 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000836 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000837 int idx = GrGLProgram::CoverageAttributeIdx();
838 if (oldCoverageOffset <= 0) {
839 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000840 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000841 true, newStride, coverageOffset));
842 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000843 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000844 true, newStride, coverageOffset));
845 }
846 } else if (oldCoverageOffset > 0) {
847 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
848 }
849
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000850 if (newEdgeOffset > 0) {
851 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
852 int idx = GrGLProgram::EdgeAttributeIdx();
853 if (oldEdgeOffset <= 0) {
854 GL_CALL(EnableVertexAttribArray(idx));
855 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
856 false, newStride, edgeOffset));
857 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
858 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
859 false, newStride, edgeOffset));
860 }
861 } else if (oldEdgeOffset > 0) {
862 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
863 }
864
bsalomon@google.come79c8152012-03-29 19:07:12 +0000865 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000866 fHWGeometryState.fArrayPtrsDirty = false;
867}
868
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000869namespace {
870
871void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
872 const GrSamplerState& sampler,
873 GrCustomStage** customStages,
874 GrGLProgram* program, int index) {
875 GrCustomStage* customStage = sampler.getCustomStage();
876 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000877 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.com16fd21b2012-05-21 21:18:13 +0000878 stage->fCustomStageKey = factory.glStageKey(customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000879 customStages[index] = customStage;
880 } else {
881 stage->fCustomStageKey = 0;
882 customStages[index] = NULL;
883 }
884}
885
886}
887
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000888void GrGpuGL::buildProgram(GrPrimitiveType type,
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000889 BlendOptFlags blendOpts,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000890 GrBlendCoeff dstCoeff,
891 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000892 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000893 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000894
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000895 // This should already have been caught
896 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
897
898 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
899
900 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
901 kEmitCoverage_BlendOptFlag));
902
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000903 // The descriptor is used as a cache key. Thus when a field of the
904 // descriptor will not affect program generation (because of the vertex
905 // layout in use or other descriptor field settings) it should be set
906 // to a canonical value to avoid duplicate programs with different keys.
907
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000908 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000909 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000910
911 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
912
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000913 bool requiresAttributeColors =
914 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000915 bool requiresAttributeCoverage =
916 !skipCoverage && SkToBool(desc.fVertexLayout &
917 kCoverage_VertexLayoutBit);
918
919 // fColorInput/fCoverageInput records how colors are specified for the.
920 // program. So we strip the bits from the layout to avoid false negatives
921 // when searching for an existing program in the cache.
922 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000923
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000924 desc.fColorFilterXfermode = skipColor ?
925 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000926 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000927
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000928 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
929
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000930 // no reason to do edge aa or look at per-vertex coverage if coverage is
931 // ignored
932 if (skipCoverage) {
933 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
934 kCoverage_VertexLayoutBit);
935 }
936
937 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
938 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
939 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000940 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000941 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000942 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000943 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000944 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000945 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000946 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000947 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000948 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000949 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000950
951 bool covIsSolidWhite = !requiresAttributeCoverage &&
952 0xffffffff == drawState.getCoverage();
953
954 if (skipCoverage) {
955 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
956 } else if (covIsSolidWhite) {
957 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
958 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
959 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
960 } else {
961 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
962 }
junov@google.comf93e7172011-03-31 21:26:24 +0000963
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000964 int lastEnabledStage = -1;
965
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000966 if (!skipCoverage && (desc.fVertexLayout &
967 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000968 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000969 } else {
970 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000971 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000972 }
973
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000974 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000975 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000976
977 stage.fOptFlags = 0;
978 stage.setEnabled(this->isStageEnabled(s));
979
980 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
981 skipCoverage;
982
983 if (!skip && stage.isEnabled()) {
984 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000985 const GrGLTexture* texture =
986 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000987 GrAssert(NULL != texture);
988 const GrSamplerState& sampler = drawState.getSampler(s);
989 // we matrix to invert when orientation is TopDown, so make sure
990 // we aren't in that case before flagging as identity.
991 if (TextureMatrixIsIdentity(texture, sampler)) {
992 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
993 } else if (!sampler.getMatrix().hasPerspective()) {
994 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
995 }
996 switch (sampler.getSampleMode()) {
997 case GrSamplerState::kNormal_SampleMode:
998 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
999 break;
1000 case GrSamplerState::kRadial_SampleMode:
1001 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
1002 break;
1003 case GrSamplerState::kRadial2_SampleMode:
1004 if (sampler.radial2IsDegenerate()) {
1005 stage.fCoordMapping =
1006 StageDesc::kRadial2GradientDegenerate_CoordMapping;
1007 } else {
1008 stage.fCoordMapping =
1009 StageDesc::kRadial2Gradient_CoordMapping;
1010 }
1011 break;
1012 case GrSamplerState::kSweep_SampleMode:
1013 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
1014 break;
1015 default:
1016 GrCrash("Unexpected sample mode!");
1017 break;
1018 }
1019
1020 switch (sampler.getFilter()) {
1021 // these both can use a regular texture2D()
1022 case GrSamplerState::kNearest_Filter:
1023 case GrSamplerState::kBilinear_Filter:
1024 stage.fFetchMode = StageDesc::kSingle_FetchMode;
1025 break;
1026 // performs 4 texture2D()s
1027 case GrSamplerState::k4x4Downsample_Filter:
1028 stage.fFetchMode = StageDesc::k2x2_FetchMode;
1029 break;
1030 // performs fKernelWidth texture2D()s
1031 case GrSamplerState::kConvolution_Filter:
1032 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
1033 break;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001034 case GrSamplerState::kDilate_Filter:
1035 stage.fFetchMode = StageDesc::kDilate_FetchMode;
1036 break;
1037 case GrSamplerState::kErode_Filter:
1038 stage.fFetchMode = StageDesc::kErode_FetchMode;
1039 break;
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001040 default:
1041 GrCrash("Unexpected filter!");
1042 break;
1043 }
1044
1045 if (sampler.hasTextureDomain()) {
1046 GrAssert(GrSamplerState::kClamp_WrapMode ==
1047 sampler.getWrapX() &&
1048 GrSamplerState::kClamp_WrapMode ==
1049 sampler.getWrapY());
1050 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
1051 }
1052
1053 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001054 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001055 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1056 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +00001057 // the shader must smear the single channel after
1058 // reading the texture
1059 if (this->glCaps().textureRedSupport()) {
1060 // we can use R8 textures so use kSmearRed
1061 stage.fInConfigFlags |=
1062 StageDesc::kSmearRed_InConfigFlag;
1063 } else {
1064 // we can use A8 textures so use kSmearAlpha
1065 stage.fInConfigFlags |=
1066 StageDesc::kSmearAlpha_InConfigFlag;
1067 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001068 } else if (sampler.swapsRAndB()) {
1069 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
1070 }
1071 }
1072 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001073 // The shader generator assumes that color channels are bytes
1074 // when rounding.
1075 GrAssert(4 == GrBytesPerPixel(texture->config()));
1076 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
1077 fUnpremulConversion) {
1078 stage.fInConfigFlags |=
1079 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
1080 } else {
1081 stage.fInConfigFlags |=
1082 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
1083 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001084 }
1085
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001086 if (sampler.getFilter() == GrSamplerState::kDilate_Filter ||
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001087 sampler.getFilter() == GrSamplerState::kErode_Filter) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001088 stage.fKernelWidth = sampler.getKernelWidth();
1089 } else {
1090 stage.fKernelWidth = 0;
1091 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001092
1093 setup_custom_stage(&stage, sampler, customStages,
1094 &fCurrentProgram, s);
1095
junov@google.comf93e7172011-03-31 21:26:24 +00001096 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001097 stage.fOptFlags = 0;
1098 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1099 stage.fInConfigFlags = 0;
1100 stage.fFetchMode = (StageDesc::FetchMode) 0;
1101 stage.fKernelWidth = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001102 stage.fCustomStageKey = 0;
1103 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +00001104 }
1105 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001106
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001107 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001108 // The shader generator assumes that color channels are bytes
1109 // when rounding.
1110 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
1111 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
1112 desc.fOutputConfig =
1113 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
1114 } else {
1115 desc.fOutputConfig =
1116 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
1117 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001118 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001119 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001120 }
1121
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001122 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001123
1124 // currently the experimental GS will only work with triangle prims
1125 // (and it doesn't do anything other than pass through values from
1126 // the VS to the FS anyway).
1127#if 0 && GR_GL_EXPERIMENTAL_GS
1128 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1129#endif
1130
bsalomon@google.coma3108262011-10-10 14:08:47 +00001131 // we want to avoid generating programs with different "first cov stage"
1132 // values when they would compute the same result.
1133 // We set field in the desc to kNumStages when either there are no
1134 // coverage stages or the distinction between coverage and color is
1135 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001136 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +00001137 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001138 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001139 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001140 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +00001141 }
1142
1143 // other coverage inputs
1144 if (!hasCoverage) {
1145 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001146 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +00001147 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1148 }
1149
1150 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001151 // color filter is applied between color/coverage computation
1152 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001153 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001154 }
1155
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001156 if (this->getCaps().fDualSourceBlendingSupport &&
1157 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1158 kCoverageAsAlpha_BlendOptFlag))) {
1159 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001160 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001161 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001162 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001163 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001164 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1165 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001166 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001167 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001168 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001169 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1170 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001171 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001172 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001173 }
1174 }
1175 }
junov@google.comf93e7172011-03-31 21:26:24 +00001176}