blob: 2448fa46eccbe6995c82f73b7492de43ad9d4d80 [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 "effects/GrConvolutionEffect.h"
bsalomon@google.comb505a122012-05-31 18:40:36 +000011#include "effects/GrMorphologyEffect.h"
12
13#include "GrBinHashKey.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000014#include "GrCustomStage.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000015#include "GrGLProgramStage.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000016#include "GrGLSL.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000017#include "GrGpuVertex.h"
junov@google.comf93e7172011-03-31 21:26:24 +000018#include "GrNoncopyable.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000019#include "GrProgramStageFactory.h"
20#include "GrRandom.h"
21#include "GrStringBuilder.h"
junov@google.comf93e7172011-03-31 21:26:24 +000022
junov@google.comf93e7172011-03-31 21:26:24 +000023#define SKIP_CACHE_CHECK true
24#define GR_UINT32_MAX static_cast<uint32_t>(-1)
25
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000026#include "../GrTHashCache.h"
junov@google.comf93e7172011-03-31 21:26:24 +000027
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000028class GrGpuGL::ProgramCache : public ::GrNoncopyable {
junov@google.comf93e7172011-03-31 21:26:24 +000029private:
30 class Entry;
31
junov@google.comf7c00f62011-08-18 18:15:16 +000032 typedef GrBinHashKey<Entry, GrGLProgram::kProgramKeySize> ProgramHashKey;
junov@google.comf93e7172011-03-31 21:26:24 +000033
34 class Entry : public ::GrNoncopyable {
35 public:
36 Entry() {}
junov@google.comf93e7172011-03-31 21:26:24 +000037 void copyAndTakeOwnership(Entry& entry) {
38 fProgramData.copyAndTakeOwnership(entry.fProgramData);
junov@google.comf7c00f62011-08-18 18:15:16 +000039 fKey = entry.fKey; // ownership transfer
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000040 fLRUStamp = entry.fLRUStamp;
junov@google.comf93e7172011-03-31 21:26:24 +000041 }
42
43 public:
44 int compare(const ProgramHashKey& key) const { return fKey.compare(key); }
45
46 public:
47 GrGLProgram::CachedData fProgramData;
48 ProgramHashKey fKey;
49 unsigned int fLRUStamp;
50 };
51
52 GrTHashTable<Entry, ProgramHashKey, 8> fHashCache;
53
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000054 // We may have kMaxEntries+1 shaders in the GL context because
55 // we create a new shader before evicting from the cache.
junov@google.comf93e7172011-03-31 21:26:24 +000056 enum {
57 kMaxEntries = 32
58 };
bsalomon@google.com4fa66942011-09-20 19:06:12 +000059 Entry fEntries[kMaxEntries];
60 int fCount;
61 unsigned int fCurrLRUStamp;
bsalomon@google.com96399942012-02-13 14:39:16 +000062 const GrGLContextInfo& fGL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000063
junov@google.comf93e7172011-03-31 21:26:24 +000064public:
bsalomon@google.com96399942012-02-13 14:39:16 +000065 ProgramCache(const GrGLContextInfo& gl)
junov@google.comf93e7172011-03-31 21:26:24 +000066 : fCount(0)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000067 , fCurrLRUStamp(0)
bsalomon@google.com96399942012-02-13 14:39:16 +000068 , fGL(gl) {
junov@google.comf93e7172011-03-31 21:26:24 +000069 }
70
71 ~ProgramCache() {
72 for (int i = 0; i < fCount; ++i) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000073 GrGpuGL::DeleteProgram(fGL.interface(),
bsalomon@google.com96399942012-02-13 14:39:16 +000074 &fEntries[i].fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000075 }
76 }
77
78 void abandon() {
79 fCount = 0;
80 }
81
82 void invalidateViewMatrices() {
83 for (int i = 0; i < fCount; ++i) {
84 // set to illegal matrix
85 fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix();
86 }
87 }
88
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000089 GrGLProgram::CachedData* getProgramData(const GrGLProgram& desc,
90 GrCustomStage** stages) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000091 Entry newEntry;
junov@google.comf7c00f62011-08-18 18:15:16 +000092 newEntry.fKey.setKeyData(desc.keyData());
93
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000094 Entry* entry = fHashCache.find(newEntry.fKey);
junov@google.comf93e7172011-03-31 21:26:24 +000095 if (NULL == entry) {
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000096 if (!desc.genProgram(fGL, stages, &newEntry.fProgramData)) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000097 return NULL;
98 }
junov@google.comf93e7172011-03-31 21:26:24 +000099 if (fCount < kMaxEntries) {
100 entry = fEntries + fCount;
101 ++fCount;
102 } else {
103 GrAssert(kMaxEntries == fCount);
104 entry = fEntries;
105 for (int i = 1; i < kMaxEntries; ++i) {
106 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
107 entry = fEntries + i;
108 }
109 }
110 fHashCache.remove(entry->fKey, entry);
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000111 GrGpuGL::DeleteProgram(fGL.interface(),
bsalomon@google.com96399942012-02-13 14:39:16 +0000112 &entry->fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +0000113 }
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000114 entry->copyAndTakeOwnership(newEntry);
junov@google.comf93e7172011-03-31 21:26:24 +0000115 fHashCache.insert(entry->fKey, entry);
116 }
117
118 entry->fLRUStamp = fCurrLRUStamp;
119 if (GR_UINT32_MAX == fCurrLRUStamp) {
120 // wrap around! just trash our LRU, one time hit.
121 for (int i = 0; i < fCount; ++i) {
122 fEntries[i].fLRUStamp = 0;
123 }
124 }
125 ++fCurrLRUStamp;
126 return &entry->fProgramData;
127 }
128};
129
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000130void GrGpuGL::DeleteProgram(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000131 CachedData* programData) {
132 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000133 if (programData->fGShaderID) {
134 GR_GL_CALL(gl, DeleteShader(programData->fGShaderID));
135 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000136 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
137 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000138 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
139}
140
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000141////////////////////////////////////////////////////////////////////////////////
142
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000143void GrGpuGL::createProgramCache() {
144 fProgramData = NULL;
145 fProgramCache = new ProgramCache(this->glContextInfo());
146}
147
148void GrGpuGL::deleteProgramCache() {
149 delete fProgramCache;
150 fProgramCache = NULL;
151 fProgramData = NULL;
152}
153
154void GrGpuGL::abandonResources(){
155 INHERITED::abandonResources();
156 fProgramCache->abandon();
157 fHWProgramID = 0;
158}
159
160////////////////////////////////////////////////////////////////////////////////
161
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000162#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
163
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000164namespace {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000165
bsalomon@google.com74b98712011-11-11 19:46:16 +0000166// GrRandoms nextU() values have patterns in the low bits
167// So using nextU() % array_count might never take some values.
168int random_int(GrRandom* r, int count) {
169 return (int)(r->nextF() * count);
170}
171
172// min is inclusive, max is exclusive
173int random_int(GrRandom* r, int min, int max) {
174 return (int)(r->nextF() * (max-min)) + min;
175}
176
177bool random_bool(GrRandom* r) {
178 return r->nextF() > .5f;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000179}
180
bsalomon@google.comb505a122012-05-31 18:40:36 +0000181typedef GrGLProgram::StageDesc StageDesc;
182// TODO: Effects should be able to register themselves for inclusion in the
183// randomly generated shaders. They should be able to configure themselves
184// randomly.
185GrCustomStage* create_random_effect(StageDesc* stageDesc,
186 GrRandom* random) {
187 enum EffectType {
188 kConvolution_EffectType,
189 kErode_EffectType,
190 kDilate_EffectType,
191
192 kEffectCount
193 };
194
195 // TODO: Remove this when generator doesn't apply this non-custom-stage
196 // notion to custom stages automatically.
197 static const uint32_t kMulByAlphaMask =
198 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
199 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
200
201 static const Gr1DKernelEffect::Direction gKernelDirections[] = {
202 Gr1DKernelEffect::kX_Direction,
203 Gr1DKernelEffect::kY_Direction
204 };
205
206 // TODO: When matrices are property of the custom-stage then remove the
207 // no-persp flag code below.
208 int effect = random_int(random, kEffectCount);
209 switch (effect) {
210 case kConvolution_EffectType: {
211 int direction = random_int(random, 2);
212 int kernelRadius = random_int(random, 1, 4);
213 float kernel[GrConvolutionEffect::kMaxKernelWidth];
214 for (int i = 0; i < GrConvolutionEffect::kMaxKernelWidth; i++) {
215 kernel[i] = random->nextF();
216 }
217 // does not work with perspective or mul-by-alpha-mask
218 stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
219 stageDesc->fInConfigFlags &= ~kMulByAlphaMask;
220 return new GrConvolutionEffect(gKernelDirections[direction],
221 kernelRadius,
222 kernel);
223 }
224 case kErode_EffectType: {
225 int direction = random_int(random, 2);
226 int kernelRadius = random_int(random, 1, 4);
227 // does not work with perspective or mul-by-alpha-mask
228 stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
229 stageDesc->fInConfigFlags &= ~kMulByAlphaMask;
230 return new GrMorphologyEffect(gKernelDirections[direction],
231 kernelRadius,
232 GrContext::kErode_MorphologyType);
233 }
234 case kDilate_EffectType: {
235 int direction = random_int(random, 2);
236 int kernelRadius = random_int(random, 1, 4);
237 // does not work with perspective or mul-by-alpha-mask
238 stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
239 stageDesc->fInConfigFlags &= ~kMulByAlphaMask;
240 return new GrMorphologyEffect(gKernelDirections[direction],
241 kernelRadius,
242 GrContext::kDilate_MorphologyType);
243 }
244 default:
245 GrCrash("Unexpected custom effect type");
246 }
247 return NULL;
248}
249
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000250}
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000251
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000252bool GrGpuGL::programUnitTest() {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000253
tomhudson@google.com086e5352011-12-08 14:44:10 +0000254 GrGLSLGeneration glslGeneration =
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000255 GrGetGLSLGeneration(this->glBinding(), this->glInterface());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000256 static const int STAGE_OPTS[] = {
257 0,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000258 StageDesc::kNoPerspective_OptFlagBit,
259 StageDesc::kIdentity_CoordMapping
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000260 };
bsalomon@google.com74b98712011-11-11 19:46:16 +0000261 static const int IN_CONFIG_FLAGS[] = {
262 StageDesc::kNone_InConfigFlag,
263 StageDesc::kSwapRAndB_InConfigFlag,
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000264 StageDesc::kSwapRAndB_InConfigFlag |
265 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag,
266 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000267 StageDesc::kSmearAlpha_InConfigFlag,
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000268 StageDesc::kSmearRed_InConfigFlag,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000269 };
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000270 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000271 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000272
273 static const int NUM_TESTS = 512;
274
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000275 GrRandom random;
276 for (int t = 0; t < NUM_TESTS; ++t) {
277
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000278#if 0
279 GrPrintf("\nTest Program %d\n-------------\n", t);
280 static const int stop = -1;
281 if (t == stop) {
282 int breakpointhere = 9;
283 }
284#endif
285
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000286 pdesc.fVertexLayout = 0;
287 pdesc.fEmitsPointSize = random.nextF() > .5f;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000288 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000289 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000290
bsalomon@google.com74b98712011-11-11 19:46:16 +0000291 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000292
bsalomon@google.com74b98712011-11-11 19:46:16 +0000293 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000294
bsalomon@google.com74b98712011-11-11 19:46:16 +0000295 pdesc.fVertexLayout |= random_bool(&random) ?
bsalomon@google.coma3108262011-10-10 14:08:47 +0000296 GrDrawTarget::kCoverage_VertexLayoutBit :
297 0;
298
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000299#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com6f92f182011-09-29 15:05:48 +0000300 pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport &&
bsalomon@google.com74b98712011-11-11 19:46:16 +0000301 random_bool(&random);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000302#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000303 pdesc.fOutputConfig = random_int(&random, ProgramDesc::kOutputConfigCnt);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000304
bsalomon@google.com74b98712011-11-11 19:46:16 +0000305 bool edgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000306 if (edgeAA) {
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000307 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
308 if (this->getCaps().fShaderDerivativeSupport) {
309 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000310 } else {
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000311 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000312 }
313 } else {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000314 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000315
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000316 pdesc.fColorMatrixEnabled = random_bool(&random);
317
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000318 if (this->getCaps().fDualSourceBlendingSupport) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000319 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000320 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000321 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000322 }
323
bsalomon@google.comb505a122012-05-31 18:40:36 +0000324 SkAutoTUnref<GrCustomStage> customStages[GrDrawState::kNumStages];
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000325
tomhudson@google.com93813632011-10-27 20:21:16 +0000326 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000327 // enable the stage?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000328 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000329 // use separate tex coords?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000330 if (random_bool(&random)) {
331 int t = random_int(&random, GrDrawState::kMaxTexCoords);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000332 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
333 } else {
334 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
335 }
336 }
337 // use text-formatted verts?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000338 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000339 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
340 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000341 StageDesc& stage = pdesc.fStages[s];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000342
343 stage.fCustomStageKey = 0;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000344
bsalomon@google.com74b98712011-11-11 19:46:16 +0000345 stage.fOptFlags = STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
346 stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];
347 stage.fCoordMapping = random_int(&random, StageDesc::kCoordMappingCnt);
348 stage.fFetchMode = random_int(&random, StageDesc::kFetchModeCnt);
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000349 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000350 static const uint32_t kMulByAlphaMask =
351 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
352 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000353
bsalomon@google.comb505a122012-05-31 18:40:36 +0000354 if (StageDesc::k2x2_FetchMode == stage.fFetchMode) {
355 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000356 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000357
bsalomon@google.comb505a122012-05-31 18:40:36 +0000358 bool useCustomEffect = random_bool(&random);
359 if (useCustomEffect) {
360 customStages[s].reset(create_random_effect(&stage, &random));
361 if (NULL != customStages[s]) {
362 stage.fCustomStageKey =
363 customStages[s]->getFactory().glStageKey(*customStages[s]);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000364 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000365 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000366 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000367 CachedData cachedData;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000368 GR_STATIC_ASSERT(sizeof(customStages) ==
369 GrDrawState::kNumStages * sizeof(GrCustomStage*));
370 GrCustomStage** stages = reinterpret_cast<GrCustomStage**>(&customStages);
371 if (!program.genProgram(this->glContextInfo(), stages, &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000372 return false;
373 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000374 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000375 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000376 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000377}
378
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000379void GrGpuGL::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000380 const GrMatrix& vm = this->getDrawState().getViewMatrix();
bsalomon@google.com341767c2012-05-11 20:47:39 +0000381 if (!fProgramData->fViewMatrix.cheapEqualTo(vm)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000382
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000383 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
384 GrAssert(NULL != rt);
385 GrMatrix m;
386 m.setAll(
387 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
388 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
389 0, 0, GrMatrix::I()[8]);
390 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000391
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000392 // ES doesn't allow you to pass true to the transpose param,
393 // so do our own transpose
394 GrGLfloat mt[] = {
395 GrScalarToFloat(m[GrMatrix::kMScaleX]),
396 GrScalarToFloat(m[GrMatrix::kMSkewY]),
397 GrScalarToFloat(m[GrMatrix::kMPersp0]),
398 GrScalarToFloat(m[GrMatrix::kMSkewX]),
399 GrScalarToFloat(m[GrMatrix::kMScaleY]),
400 GrScalarToFloat(m[GrMatrix::kMPersp1]),
401 GrScalarToFloat(m[GrMatrix::kMTransX]),
402 GrScalarToFloat(m[GrMatrix::kMTransY]),
403 GrScalarToFloat(m[GrMatrix::kMPersp2])
404 };
405
bsalomon@google.com341767c2012-05-11 20:47:39 +0000406 GrAssert(GrGLProgram::kUnusedUniform !=
407 fProgramData->fUniLocations.fViewMatrixUni);
408 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
409 1, false, mt));
410 fProgramData->fViewMatrix = vm;
bsalomon@google.com91961302011-05-09 18:39:58 +0000411 }
junov@google.comf93e7172011-03-31 21:26:24 +0000412}
413
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000414void GrGpuGL::flushTextureDomain(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000415 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000416 const GrDrawState& drawState = this->getDrawState();
junov@google.com6acc9b32011-05-16 18:32:07 +0000417 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000418 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
junov@google.com6acc9b32011-05-16 18:32:07 +0000419
twiz@google.com76b82742011-06-02 20:30:02 +0000420 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
421 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000422
junov@google.com2f839402011-05-24 15:13:01 +0000423 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000424
junov@google.com2f839402011-05-24 15:13:01 +0000425 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000426 GrScalarToFloat(texDom.left()),
427 GrScalarToFloat(texDom.top()),
428 GrScalarToFloat(texDom.right()),
429 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000430 };
431
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000432 const GrGLTexture* texture =
433 static_cast<const GrGLTexture*>(drawState.getTexture(s));
junov@google.com2f839402011-05-24 15:13:01 +0000434 GrGLTexture::Orientation orientation = texture->orientation();
435
436 // vertical flip if necessary
437 if (GrGLTexture::kBottomUp_Orientation == orientation) {
438 values[1] = 1.0f - values[1];
439 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000440 // The top and bottom were just flipped, so correct the ordering
441 // of elements so that values = (l, t, r, b).
442 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000443 }
444
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000445 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000446 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000447 }
448}
449
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000450void GrGpuGL::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000451 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000452 const GrDrawState& drawState = this->getDrawState();
453 const GrGLTexture* texture =
454 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000455 if (NULL != texture) {
bsalomon@google.com341767c2012-05-11 20:47:39 +0000456 const GrMatrix& hwMatrix = fProgramData->fTextureMatrices[s];
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000457 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000458 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000459 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000460 !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000461
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000462 GrMatrix m = samplerMatrix;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000463 GrSamplerState::SampleMode mode =
464 drawState.getSampler(s).getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000465 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000466
bsalomon@google.com91961302011-05-09 18:39:58 +0000467 // ES doesn't allow you to pass true to the transpose param,
468 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000469 GrGLfloat mt[] = {
470 GrScalarToFloat(m[GrMatrix::kMScaleX]),
471 GrScalarToFloat(m[GrMatrix::kMSkewY]),
472 GrScalarToFloat(m[GrMatrix::kMPersp0]),
473 GrScalarToFloat(m[GrMatrix::kMSkewX]),
474 GrScalarToFloat(m[GrMatrix::kMScaleY]),
475 GrScalarToFloat(m[GrMatrix::kMPersp1]),
476 GrScalarToFloat(m[GrMatrix::kMTransX]),
477 GrScalarToFloat(m[GrMatrix::kMTransY]),
478 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000479 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000480
bsalomon@google.com341767c2012-05-11 20:47:39 +0000481 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
482 fProgramData->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000483 }
484 }
junov@google.comf93e7172011-03-31 21:26:24 +0000485}
486
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000487void GrGpuGL::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000488
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000489 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000490 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.com91961302011-05-09 18:39:58 +0000491 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000492 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
493 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
494 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000495
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000496 GrScalar centerX1 = sampler.getRadial2CenterX1();
497 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000498
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000499 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000500
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000501 // when were in the degenerate (linear) case the second
502 // value will be INF but the program doesn't read it. (We
503 // use the same 6 uniforms even though we don't need them
504 // all in the linear case just to keep the code complexity
505 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000506 float values[6] = {
507 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000508 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000509 GrScalarToFloat(centerX1),
510 GrScalarToFloat(radius0),
511 GrScalarToFloat(GrMul(radius0, radius0)),
512 sampler.isRadial2PosRoot() ? 1.f : -1.f
513 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000514 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000515 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
516 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
517 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
518 }
519}
520
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000521void GrGpuGL::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000522 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000523 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000524 const GrGLTexture* texture =
525 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000526 if (texture->width() != fProgramData->fTextureWidth[s] ||
527 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000528
bsalomon@google.com99621082011-11-15 16:47:16 +0000529 float texelSize[] = {1.f / texture->width(),
530 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000531 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000532 fProgramData->fTextureWidth[s] = texture->width();
533 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000534 }
535 }
junov@google.comf93e7172011-03-31 21:26:24 +0000536}
537
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000538void GrGpuGL::flushColorMatrix() {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000539 const ProgramDesc& desc = fCurrentProgram.getDesc();
540 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
541 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
542 if (GrGLProgram::kUnusedUniform != matrixUni
543 && GrGLProgram::kUnusedUniform != vecUni) {
544 const float* m = this->getDrawState().getColorMatrix();
545 GrGLfloat mt[] = {
546 m[0], m[5], m[10], m[15],
547 m[1], m[6], m[11], m[16],
548 m[2], m[7], m[12], m[17],
549 m[3], m[8], m[13], m[18],
550 };
551 static float scale = 1.0f / 255.0f;
552 GrGLfloat vec[] = {
553 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
554 };
555 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
556 GL_CALL(Uniform4fv(vecUni, 1, vec));
557 }
558}
559
Scroggo01b87ec2011-05-11 18:05:38 +0000560static const float ONE_OVER_255 = 1.f / 255.f;
561
562#define GR_COLOR_TO_VEC4(color) {\
563 GrColorUnpackR(color) * ONE_OVER_255,\
564 GrColorUnpackG(color) * ONE_OVER_255,\
565 GrColorUnpackB(color) * ONE_OVER_255,\
566 GrColorUnpackA(color) * ONE_OVER_255 \
567}
568
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000569void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000570 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000571 const GrDrawState& drawState = this->getDrawState();
572
bsalomon@google.come79c8152012-03-29 19:07:12 +0000573 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000574 // color will be specified per-vertex as an attribute
575 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000576 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000577 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000578 switch (desc.fColorInput) {
579 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000580 if (fHWConstAttribColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000581 // OpenGL ES only supports the float varieties of
582 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000583 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000584 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
585 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000586 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000587 }
588 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000589 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000590 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000591 // OpenGL ES doesn't support unsigned byte varieties of
592 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000593 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000594 GrAssert(GrGLProgram::kUnusedUniform !=
595 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000596 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
597 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000598 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000599 }
600 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000601 case ProgramDesc::kSolidWhite_ColorInput:
602 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000603 break;
604 default:
605 GrCrash("Unknown color type.");
606 }
607 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000608 if (fProgramData->fUniLocations.fColorFilterUni
609 != GrGLProgram::kUnusedUniform
610 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000611 != drawState.getColorFilterColor()) {
612 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000613 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000614 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000615 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000616}
617
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000618void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000619 const ProgramDesc& desc = fCurrentProgram.getDesc();
620 const GrDrawState& drawState = this->getDrawState();
621
622
bsalomon@google.come79c8152012-03-29 19:07:12 +0000623 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000624 // coverage will be specified per-vertex as an attribute
625 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000626 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000627 } else {
628 switch (desc.fCoverageInput) {
629 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000630 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000631 // OpenGL ES only supports the float varieties of
632 // glVertexAttrib
633 float c[] = GR_COLOR_TO_VEC4(coverage);
634 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
635 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000636 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000637 }
638 break;
639 case ProgramDesc::kUniform_ColorInput:
640 if (fProgramData->fCoverage != coverage) {
641 // OpenGL ES doesn't support unsigned byte varieties of
642 // glUniform
643 float c[] = GR_COLOR_TO_VEC4(coverage);
644 GrAssert(GrGLProgram::kUnusedUniform !=
645 fProgramData->fUniLocations.fCoverageUni);
646 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
647 1, c));
648 fProgramData->fCoverage = coverage;
649 }
650 break;
651 case ProgramDesc::kSolidWhite_ColorInput:
652 case ProgramDesc::kTransBlack_ColorInput:
653 break;
654 default:
655 GrCrash("Unknown coverage type.");
656 }
657 }
658}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000659
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000660bool GrGpuGL::flushGraphicsState(GrPrimitiveType type) {
junov@google.comf93e7172011-03-31 21:26:24 +0000661 if (!flushGLStateCommon(type)) {
662 return false;
663 }
664
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000665 const GrDrawState& drawState = this->getDrawState();
666
junov@google.comf93e7172011-03-31 21:26:24 +0000667 if (fDirtyFlags.fRenderTargetChanged) {
junov@google.comf93e7172011-03-31 21:26:24 +0000668 // we assume all shader matrices may be wrong after viewport changes
669 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000670 }
671
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000672 GrBlendCoeff srcCoeff;
673 GrBlendCoeff dstCoeff;
674 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
675 if (kSkipDraw_BlendOptFlag & blendOpts) {
676 return false;
677 }
678
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000679 GrCustomStage* customStages [GrDrawState::kNumStages];
680 this->buildProgram(type, blendOpts, dstCoeff, customStages);
681 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
682 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000683 if (NULL == fProgramData) {
684 GrAssert(!"Failed to create program!");
685 return false;
686 }
junov@google.comf93e7172011-03-31 21:26:24 +0000687
688 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000689 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000690 fHWProgramID = fProgramData->fProgramID;
691 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000692 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
693 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000694
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000695 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000696 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000697 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
698 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000699 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000700 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
701 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000702 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000703 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000704 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000705 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000706 }
707 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000708 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000709
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000710 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000711
tomhudson@google.com93813632011-10-27 20:21:16 +0000712 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000713 if (this->isStageEnabled(s)) {
714 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000715
bsalomon@google.com40d92932011-12-13 18:40:47 +0000716 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000717
bsalomon@google.com40d92932011-12-13 18:40:47 +0000718 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000719
bsalomon@google.com40d92932011-12-13 18:40:47 +0000720 this->flushTextureDomain(s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000721
722 if (NULL != fProgramData->fCustomStage[s]) {
723 const GrSamplerState& sampler =
724 this->getDrawState().getSampler(s);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000725 const GrGLTexture* texture =
726 static_cast<const GrGLTexture*>(
727 this->getDrawState().getTexture(s));
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000728 fProgramData->fCustomStage[s]->setData(
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000729 this->glInterface(), *texture,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000730 *sampler.getCustomStage(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000731 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000732 }
junov@google.comf93e7172011-03-31 21:26:24 +0000733 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000734 this->flushColorMatrix();
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000735 resetDirtyFlags();
junov@google.comf93e7172011-03-31 21:26:24 +0000736 return true;
737}
738
bsalomon@google.com2717d562012-05-07 19:10:52 +0000739#if GR_TEXT_SCALAR_IS_USHORT
740 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
741 #define TEXT_COORDS_ARE_NORMALIZED 1
742#elif GR_TEXT_SCALAR_IS_FLOAT
743 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
744 #define TEXT_COORDS_ARE_NORMALIZED 0
745#elif GR_TEXT_SCALAR_IS_FIXED
746 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
747 #define TEXT_COORDS_ARE_NORMALIZED 0
748#else
749 #error "unknown GR_TEXT_SCALAR type"
750#endif
751
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000752void GrGpuGL::setupGeometry(int* startVertex,
junov@google.comf93e7172011-03-31 21:26:24 +0000753 int* startIndex,
754 int vertexCount,
755 int indexCount) {
756
757 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000758 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000759 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000760 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000761
bsalomon@google.come79c8152012-03-29 19:07:12 +0000762 GrVertexLayout currLayout = this->getVertexLayout();
763
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000764 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000765 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000766 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000767 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000768 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000769 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000770 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000771 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000772 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000773 int oldEdgeOffset;
774
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000775 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
776 fHWGeometryState.fVertexLayout,
777 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000778 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000779 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000780 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000781 bool indexed = NULL != startIndex;
782
783 int extraVertexOffset;
784 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000785 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000786
787 GrGLenum scalarType;
788 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000789 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000790 scalarType = TEXT_COORDS_GL_TYPE;
791 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000792 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000793 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
794 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000795 texCoordNorm = false;
796 }
797
798 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
799 *startVertex = 0;
800 if (indexed) {
801 *startIndex += extraIndexOffset;
802 }
803
804 // all the Pointers must be set if any of these are true
805 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
806 vertexOffset != fHWGeometryState.fVertexOffset ||
807 newStride != oldStride;
808
809 // position and tex coord offsets change if above conditions are true
810 // or the type/normalization changed based on text vs nontext type coords.
811 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000812 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000813 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000814 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000815
816 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000817 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000818 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000819 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000820 fHWGeometryState.fVertexOffset = vertexOffset;
821 }
822
tomhudson@google.com93813632011-10-27 20:21:16 +0000823 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000824 if (newTexCoordOffsets[t] > 0) {
825 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000826 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000827 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000828 GL_CALL(EnableVertexAttribArray(idx));
829 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000830 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000831 } else if (posAndTexChange ||
832 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000833 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000834 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000835 }
836 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000837 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000838 }
839 }
840
841 if (newColorOffset > 0) {
842 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000843 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000844 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000845 GL_CALL(EnableVertexAttribArray(idx));
846 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000847 true, newStride, colorOffset));
848 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000849 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000850 true, newStride, colorOffset));
851 }
852 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000853 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000854 }
855
bsalomon@google.coma3108262011-10-10 14:08:47 +0000856 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000857 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000858 int idx = GrGLProgram::CoverageAttributeIdx();
859 if (oldCoverageOffset <= 0) {
860 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000861 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000862 true, newStride, coverageOffset));
863 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000864 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000865 true, newStride, coverageOffset));
866 }
867 } else if (oldCoverageOffset > 0) {
868 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
869 }
870
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000871 if (newEdgeOffset > 0) {
872 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
873 int idx = GrGLProgram::EdgeAttributeIdx();
874 if (oldEdgeOffset <= 0) {
875 GL_CALL(EnableVertexAttribArray(idx));
876 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
877 false, newStride, edgeOffset));
878 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
879 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
880 false, newStride, edgeOffset));
881 }
882 } else if (oldEdgeOffset > 0) {
883 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
884 }
885
bsalomon@google.come79c8152012-03-29 19:07:12 +0000886 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000887 fHWGeometryState.fArrayPtrsDirty = false;
888}
889
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000890namespace {
891
892void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
893 const GrSamplerState& sampler,
894 GrCustomStage** customStages,
895 GrGLProgram* program, int index) {
896 GrCustomStage* customStage = sampler.getCustomStage();
897 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000898 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000899 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000900 customStages[index] = customStage;
901 } else {
902 stage->fCustomStageKey = 0;
903 customStages[index] = NULL;
904 }
905}
906
907}
908
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000909void GrGpuGL::buildProgram(GrPrimitiveType type,
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000910 BlendOptFlags blendOpts,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000911 GrBlendCoeff dstCoeff,
912 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000913 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000914 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000915
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000916 // This should already have been caught
917 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
918
919 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
920
921 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
922 kEmitCoverage_BlendOptFlag));
923
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000924 // The descriptor is used as a cache key. Thus when a field of the
925 // descriptor will not affect program generation (because of the vertex
926 // layout in use or other descriptor field settings) it should be set
927 // to a canonical value to avoid duplicate programs with different keys.
928
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000929 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000930 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000931
932 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
933
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000934 bool requiresAttributeColors =
935 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000936 bool requiresAttributeCoverage =
937 !skipCoverage && SkToBool(desc.fVertexLayout &
938 kCoverage_VertexLayoutBit);
939
940 // fColorInput/fCoverageInput records how colors are specified for the.
941 // program. So we strip the bits from the layout to avoid false negatives
942 // when searching for an existing program in the cache.
943 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000944
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000945 desc.fColorFilterXfermode = skipColor ?
946 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000947 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000948
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000949 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
950
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000951 // no reason to do edge aa or look at per-vertex coverage if coverage is
952 // ignored
953 if (skipCoverage) {
954 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
955 kCoverage_VertexLayoutBit);
956 }
957
958 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
959 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
960 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000961 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000962 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000963 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000964 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000965 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000966 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000967 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000968 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000969 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000970 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000971
972 bool covIsSolidWhite = !requiresAttributeCoverage &&
973 0xffffffff == drawState.getCoverage();
974
975 if (skipCoverage) {
976 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
977 } else if (covIsSolidWhite) {
978 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
979 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
980 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
981 } else {
982 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
983 }
junov@google.comf93e7172011-03-31 21:26:24 +0000984
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000985 int lastEnabledStage = -1;
986
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000987 if (!skipCoverage && (desc.fVertexLayout &
988 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000989 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000990 } else {
991 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000992 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000993 }
994
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000995 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000996 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000997
998 stage.fOptFlags = 0;
999 stage.setEnabled(this->isStageEnabled(s));
1000
1001 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
1002 skipCoverage;
1003
1004 if (!skip && stage.isEnabled()) {
1005 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001006 const GrGLTexture* texture =
1007 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001008 GrAssert(NULL != texture);
1009 const GrSamplerState& sampler = drawState.getSampler(s);
1010 // we matrix to invert when orientation is TopDown, so make sure
1011 // we aren't in that case before flagging as identity.
1012 if (TextureMatrixIsIdentity(texture, sampler)) {
1013 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
1014 } else if (!sampler.getMatrix().hasPerspective()) {
1015 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
1016 }
1017 switch (sampler.getSampleMode()) {
1018 case GrSamplerState::kNormal_SampleMode:
1019 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
1020 break;
1021 case GrSamplerState::kRadial_SampleMode:
1022 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
1023 break;
1024 case GrSamplerState::kRadial2_SampleMode:
1025 if (sampler.radial2IsDegenerate()) {
1026 stage.fCoordMapping =
1027 StageDesc::kRadial2GradientDegenerate_CoordMapping;
1028 } else {
1029 stage.fCoordMapping =
1030 StageDesc::kRadial2Gradient_CoordMapping;
1031 }
1032 break;
1033 case GrSamplerState::kSweep_SampleMode:
1034 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
1035 break;
1036 default:
1037 GrCrash("Unexpected sample mode!");
1038 break;
1039 }
1040
1041 switch (sampler.getFilter()) {
1042 // these both can use a regular texture2D()
1043 case GrSamplerState::kNearest_Filter:
1044 case GrSamplerState::kBilinear_Filter:
1045 stage.fFetchMode = StageDesc::kSingle_FetchMode;
1046 break;
1047 // performs 4 texture2D()s
1048 case GrSamplerState::k4x4Downsample_Filter:
1049 stage.fFetchMode = StageDesc::k2x2_FetchMode;
1050 break;
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001051 default:
1052 GrCrash("Unexpected filter!");
1053 break;
1054 }
1055
1056 if (sampler.hasTextureDomain()) {
1057 GrAssert(GrSamplerState::kClamp_WrapMode ==
1058 sampler.getWrapX() &&
1059 GrSamplerState::kClamp_WrapMode ==
1060 sampler.getWrapY());
1061 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
1062 }
1063
1064 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001065 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001066 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1067 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +00001068 // the shader must smear the single channel after
1069 // reading the texture
1070 if (this->glCaps().textureRedSupport()) {
1071 // we can use R8 textures so use kSmearRed
1072 stage.fInConfigFlags |=
1073 StageDesc::kSmearRed_InConfigFlag;
1074 } else {
1075 // we can use A8 textures so use kSmearAlpha
1076 stage.fInConfigFlags |=
1077 StageDesc::kSmearAlpha_InConfigFlag;
1078 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001079 } else if (sampler.swapsRAndB()) {
1080 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
1081 }
1082 }
1083 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001084 // The shader generator assumes that color channels are bytes
1085 // when rounding.
1086 GrAssert(4 == GrBytesPerPixel(texture->config()));
1087 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
1088 fUnpremulConversion) {
1089 stage.fInConfigFlags |=
1090 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
1091 } else {
1092 stage.fInConfigFlags |=
1093 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
1094 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001095 }
1096
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001097 setup_custom_stage(&stage, sampler, customStages,
1098 &fCurrentProgram, s);
1099
junov@google.comf93e7172011-03-31 21:26:24 +00001100 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001101 stage.fOptFlags = 0;
1102 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1103 stage.fInConfigFlags = 0;
1104 stage.fFetchMode = (StageDesc::FetchMode) 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001105 stage.fCustomStageKey = 0;
1106 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +00001107 }
1108 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001109
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001110 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001111 // The shader generator assumes that color channels are bytes
1112 // when rounding.
1113 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
1114 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
1115 desc.fOutputConfig =
1116 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
1117 } else {
1118 desc.fOutputConfig =
1119 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
1120 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001121 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001122 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001123 }
1124
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001125 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001126
1127 // currently the experimental GS will only work with triangle prims
1128 // (and it doesn't do anything other than pass through values from
1129 // the VS to the FS anyway).
1130#if 0 && GR_GL_EXPERIMENTAL_GS
1131 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1132#endif
1133
bsalomon@google.coma3108262011-10-10 14:08:47 +00001134 // we want to avoid generating programs with different "first cov stage"
1135 // values when they would compute the same result.
1136 // We set field in the desc to kNumStages when either there are no
1137 // coverage stages or the distinction between coverage and color is
1138 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001139 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +00001140 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001141 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001142 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001143 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +00001144 }
1145
1146 // other coverage inputs
1147 if (!hasCoverage) {
1148 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001149 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +00001150 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1151 }
1152
1153 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001154 // color filter is applied between color/coverage computation
1155 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001156 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001157 }
1158
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001159 if (this->getCaps().fDualSourceBlendingSupport &&
1160 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1161 kCoverageAsAlpha_BlendOptFlag))) {
1162 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001163 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001164 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001165 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001166 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001167 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1168 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001169 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001170 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001171 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001172 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1173 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001174 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001175 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001176 }
1177 }
1178 }
junov@google.comf93e7172011-03-31 21:26:24 +00001179}