blob: b8fcb1b03e14fa30b6934c635853f12fd39ddf72 [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
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000013#include "GrCustomStage.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000014#include "GrGLProgramStage.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000015#include "GrGLSL.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000016#include "GrGpuVertex.h"
junov@google.comf93e7172011-03-31 21:26:24 +000017#include "GrNoncopyable.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000018#include "GrProgramStageFactory.h"
19#include "GrRandom.h"
20#include "GrStringBuilder.h"
junov@google.comf93e7172011-03-31 21:26:24 +000021
junov@google.comf93e7172011-03-31 21:26:24 +000022#define SKIP_CACHE_CHECK true
23#define GR_UINT32_MAX static_cast<uint32_t>(-1)
24
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000025void GrGpuGL::ProgramCache::Entry::copyAndTakeOwnership(Entry& entry) {
26 fProgramData.copyAndTakeOwnership(entry.fProgramData);
27 fKey = entry.fKey; // ownership transfer
28 fLRUStamp = entry.fLRUStamp;
29}
junov@google.comf93e7172011-03-31 21:26:24 +000030
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000031GrGpuGL::ProgramCache::ProgramCache(const GrGLContextInfo& gl)
32 : fCount(0)
33 , fCurrLRUStamp(0)
34 , fGL(gl) {
35}
junov@google.comf93e7172011-03-31 21:26:24 +000036
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000037GrGpuGL::ProgramCache::~ProgramCache() {
38 for (int i = 0; i < fCount; ++i) {
39 GrGpuGL::DeleteProgram(fGL.interface(),
40 &fEntries[i].fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000041 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000042}
junov@google.comf93e7172011-03-31 21:26:24 +000043
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000044void GrGpuGL::ProgramCache::abandon() {
45 fCount = 0;
46}
47
48void GrGpuGL::ProgramCache::invalidateViewMatrices() {
49 for (int i = 0; i < fCount; ++i) {
50 // set to illegal matrix
51 fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +000052 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000053}
junov@google.comf93e7172011-03-31 21:26:24 +000054
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000055GrGLProgram::CachedData* GrGpuGL::ProgramCache::getProgramData(
56 const GrGLProgram& desc,
57 GrCustomStage** stages) {
58 Entry newEntry;
59 newEntry.fKey.setKeyData(desc.keyData());
junov@google.comf7c00f62011-08-18 18:15:16 +000060
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000061 Entry* entry = fHashCache.find(newEntry.fKey);
62 if (NULL == entry) {
63 if (!desc.genProgram(fGL, stages, &newEntry.fProgramData)) {
64 return NULL;
65 }
66 if (fCount < kMaxEntries) {
67 entry = fEntries + fCount;
68 ++fCount;
69 } else {
70 GrAssert(kMaxEntries == fCount);
71 entry = fEntries;
72 for (int i = 1; i < kMaxEntries; ++i) {
73 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
74 entry = fEntries + i;
junov@google.comf93e7172011-03-31 21:26:24 +000075 }
junov@google.comf93e7172011-03-31 21:26:24 +000076 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000077 fHashCache.remove(entry->fKey, entry);
78 GrGpuGL::DeleteProgram(fGL.interface(),
79 &entry->fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000080 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000081 entry->copyAndTakeOwnership(newEntry);
82 fHashCache.insert(entry->fKey, entry);
junov@google.comf93e7172011-03-31 21:26:24 +000083 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000084
85 entry->fLRUStamp = fCurrLRUStamp;
86 if (GR_UINT32_MAX == fCurrLRUStamp) {
87 // wrap around! just trash our LRU, one time hit.
88 for (int i = 0; i < fCount; ++i) {
89 fEntries[i].fLRUStamp = 0;
90 }
91 }
92 ++fCurrLRUStamp;
93 return &entry->fProgramData;
94}
junov@google.comf93e7172011-03-31 21:26:24 +000095
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000096void GrGpuGL::DeleteProgram(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +000097 CachedData* programData) {
98 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000099 if (programData->fGShaderID) {
100 GR_GL_CALL(gl, DeleteShader(programData->fGShaderID));
101 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000102 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
103 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000104 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
105}
106
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000107////////////////////////////////////////////////////////////////////////////////
108
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000109void GrGpuGL::abandonResources(){
110 INHERITED::abandonResources();
111 fProgramCache->abandon();
112 fHWProgramID = 0;
113}
114
115////////////////////////////////////////////////////////////////////////////////
116
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000117#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
118
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000119namespace {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000120
bsalomon@google.com74b98712011-11-11 19:46:16 +0000121// GrRandoms nextU() values have patterns in the low bits
122// So using nextU() % array_count might never take some values.
123int random_int(GrRandom* r, int count) {
124 return (int)(r->nextF() * count);
125}
126
127// min is inclusive, max is exclusive
128int random_int(GrRandom* r, int min, int max) {
129 return (int)(r->nextF() * (max-min)) + min;
130}
131
132bool random_bool(GrRandom* r) {
133 return r->nextF() > .5f;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000134}
135
bsalomon@google.comb505a122012-05-31 18:40:36 +0000136typedef GrGLProgram::StageDesc StageDesc;
137// TODO: Effects should be able to register themselves for inclusion in the
138// randomly generated shaders. They should be able to configure themselves
139// randomly.
140GrCustomStage* create_random_effect(StageDesc* stageDesc,
141 GrRandom* random) {
142 enum EffectType {
143 kConvolution_EffectType,
144 kErode_EffectType,
145 kDilate_EffectType,
146
147 kEffectCount
148 };
149
150 // TODO: Remove this when generator doesn't apply this non-custom-stage
151 // notion to custom stages automatically.
152 static const uint32_t kMulByAlphaMask =
153 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
154 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
155
156 static const Gr1DKernelEffect::Direction gKernelDirections[] = {
157 Gr1DKernelEffect::kX_Direction,
158 Gr1DKernelEffect::kY_Direction
159 };
160
161 // TODO: When matrices are property of the custom-stage then remove the
162 // no-persp flag code below.
163 int effect = random_int(random, kEffectCount);
164 switch (effect) {
165 case kConvolution_EffectType: {
166 int direction = random_int(random, 2);
167 int kernelRadius = random_int(random, 1, 4);
168 float kernel[GrConvolutionEffect::kMaxKernelWidth];
169 for (int i = 0; i < GrConvolutionEffect::kMaxKernelWidth; i++) {
170 kernel[i] = random->nextF();
171 }
172 // does not work with perspective or mul-by-alpha-mask
173 stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
174 stageDesc->fInConfigFlags &= ~kMulByAlphaMask;
175 return new GrConvolutionEffect(gKernelDirections[direction],
176 kernelRadius,
177 kernel);
178 }
179 case kErode_EffectType: {
180 int direction = random_int(random, 2);
181 int kernelRadius = random_int(random, 1, 4);
182 // does not work with perspective or mul-by-alpha-mask
183 stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
184 stageDesc->fInConfigFlags &= ~kMulByAlphaMask;
185 return new GrMorphologyEffect(gKernelDirections[direction],
186 kernelRadius,
187 GrContext::kErode_MorphologyType);
188 }
189 case kDilate_EffectType: {
190 int direction = random_int(random, 2);
191 int kernelRadius = random_int(random, 1, 4);
192 // does not work with perspective or mul-by-alpha-mask
193 stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
194 stageDesc->fInConfigFlags &= ~kMulByAlphaMask;
195 return new GrMorphologyEffect(gKernelDirections[direction],
196 kernelRadius,
197 GrContext::kDilate_MorphologyType);
198 }
199 default:
200 GrCrash("Unexpected custom effect type");
201 }
202 return NULL;
203}
204
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000205}
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000206
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000207bool GrGpuGL::programUnitTest() {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000208
tomhudson@google.com086e5352011-12-08 14:44:10 +0000209 GrGLSLGeneration glslGeneration =
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000210 GrGetGLSLGeneration(this->glBinding(), this->glInterface());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000211 static const int STAGE_OPTS[] = {
212 0,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000213 StageDesc::kNoPerspective_OptFlagBit,
214 StageDesc::kIdentity_CoordMapping
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000215 };
bsalomon@google.com74b98712011-11-11 19:46:16 +0000216 static const int IN_CONFIG_FLAGS[] = {
217 StageDesc::kNone_InConfigFlag,
218 StageDesc::kSwapRAndB_InConfigFlag,
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000219 StageDesc::kSwapRAndB_InConfigFlag |
220 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag,
221 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000222 StageDesc::kSmearAlpha_InConfigFlag,
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000223 StageDesc::kSmearRed_InConfigFlag,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000224 };
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000225 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000226 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000227
228 static const int NUM_TESTS = 512;
229
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000230 GrRandom random;
231 for (int t = 0; t < NUM_TESTS; ++t) {
232
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000233#if 0
234 GrPrintf("\nTest Program %d\n-------------\n", t);
235 static const int stop = -1;
236 if (t == stop) {
237 int breakpointhere = 9;
238 }
239#endif
240
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000241 pdesc.fVertexLayout = 0;
242 pdesc.fEmitsPointSize = random.nextF() > .5f;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000243 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000244 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000245
bsalomon@google.com74b98712011-11-11 19:46:16 +0000246 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000247
bsalomon@google.com74b98712011-11-11 19:46:16 +0000248 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000249
bsalomon@google.com74b98712011-11-11 19:46:16 +0000250 pdesc.fVertexLayout |= random_bool(&random) ?
bsalomon@google.coma3108262011-10-10 14:08:47 +0000251 GrDrawTarget::kCoverage_VertexLayoutBit :
252 0;
253
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000254#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com6f92f182011-09-29 15:05:48 +0000255 pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport &&
bsalomon@google.com74b98712011-11-11 19:46:16 +0000256 random_bool(&random);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000257#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000258 pdesc.fOutputConfig = random_int(&random, ProgramDesc::kOutputConfigCnt);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000259
bsalomon@google.com74b98712011-11-11 19:46:16 +0000260 bool edgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000261 if (edgeAA) {
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000262 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
263 if (this->getCaps().fShaderDerivativeSupport) {
264 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000265 } else {
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000266 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000267 }
268 } else {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000269 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000270
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000271 pdesc.fColorMatrixEnabled = random_bool(&random);
272
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000273 if (this->getCaps().fDualSourceBlendingSupport) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000274 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000275 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000276 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000277 }
278
bsalomon@google.comb505a122012-05-31 18:40:36 +0000279 SkAutoTUnref<GrCustomStage> customStages[GrDrawState::kNumStages];
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000280
tomhudson@google.com93813632011-10-27 20:21:16 +0000281 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000282 // enable the stage?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000283 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000284 // use separate tex coords?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000285 if (random_bool(&random)) {
286 int t = random_int(&random, GrDrawState::kMaxTexCoords);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000287 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
288 } else {
289 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
290 }
291 }
292 // use text-formatted verts?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000293 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000294 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
295 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000296 StageDesc& stage = pdesc.fStages[s];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000297
298 stage.fCustomStageKey = 0;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000299
bsalomon@google.com74b98712011-11-11 19:46:16 +0000300 stage.fOptFlags = STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
301 stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];
302 stage.fCoordMapping = random_int(&random, StageDesc::kCoordMappingCnt);
303 stage.fFetchMode = random_int(&random, StageDesc::kFetchModeCnt);
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000304 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000305 static const uint32_t kMulByAlphaMask =
306 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
307 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000308
bsalomon@google.comb505a122012-05-31 18:40:36 +0000309 if (StageDesc::k2x2_FetchMode == stage.fFetchMode) {
310 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000311 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000312
bsalomon@google.comb505a122012-05-31 18:40:36 +0000313 bool useCustomEffect = random_bool(&random);
314 if (useCustomEffect) {
315 customStages[s].reset(create_random_effect(&stage, &random));
316 if (NULL != customStages[s]) {
317 stage.fCustomStageKey =
318 customStages[s]->getFactory().glStageKey(*customStages[s]);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000319 }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000320 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000321 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000322 CachedData cachedData;
bsalomon@google.comb505a122012-05-31 18:40:36 +0000323 GR_STATIC_ASSERT(sizeof(customStages) ==
324 GrDrawState::kNumStages * sizeof(GrCustomStage*));
325 GrCustomStage** stages = reinterpret_cast<GrCustomStage**>(&customStages);
326 if (!program.genProgram(this->glContextInfo(), stages, &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000327 return false;
328 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000329 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000330 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000331 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000332}
333
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000334void GrGpuGL::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000335 const GrMatrix& vm = this->getDrawState().getViewMatrix();
bsalomon@google.com341767c2012-05-11 20:47:39 +0000336 if (!fProgramData->fViewMatrix.cheapEqualTo(vm)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000337
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000338 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
339 GrAssert(NULL != rt);
340 GrMatrix m;
341 m.setAll(
342 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
343 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
344 0, 0, GrMatrix::I()[8]);
345 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000346
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000347 // ES doesn't allow you to pass true to the transpose param,
348 // so do our own transpose
349 GrGLfloat mt[] = {
350 GrScalarToFloat(m[GrMatrix::kMScaleX]),
351 GrScalarToFloat(m[GrMatrix::kMSkewY]),
352 GrScalarToFloat(m[GrMatrix::kMPersp0]),
353 GrScalarToFloat(m[GrMatrix::kMSkewX]),
354 GrScalarToFloat(m[GrMatrix::kMScaleY]),
355 GrScalarToFloat(m[GrMatrix::kMPersp1]),
356 GrScalarToFloat(m[GrMatrix::kMTransX]),
357 GrScalarToFloat(m[GrMatrix::kMTransY]),
358 GrScalarToFloat(m[GrMatrix::kMPersp2])
359 };
360
bsalomon@google.com341767c2012-05-11 20:47:39 +0000361 GrAssert(GrGLProgram::kUnusedUniform !=
362 fProgramData->fUniLocations.fViewMatrixUni);
363 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
364 1, false, mt));
365 fProgramData->fViewMatrix = vm;
bsalomon@google.com91961302011-05-09 18:39:58 +0000366 }
junov@google.comf93e7172011-03-31 21:26:24 +0000367}
368
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000369void GrGpuGL::flushTextureDomain(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000370 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000371 const GrDrawState& drawState = this->getDrawState();
junov@google.com6acc9b32011-05-16 18:32:07 +0000372 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000373 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
junov@google.com6acc9b32011-05-16 18:32:07 +0000374
twiz@google.com76b82742011-06-02 20:30:02 +0000375 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
376 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000377
junov@google.com2f839402011-05-24 15:13:01 +0000378 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000379
junov@google.com2f839402011-05-24 15:13:01 +0000380 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000381 GrScalarToFloat(texDom.left()),
382 GrScalarToFloat(texDom.top()),
383 GrScalarToFloat(texDom.right()),
384 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000385 };
386
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000387 const GrGLTexture* texture =
388 static_cast<const GrGLTexture*>(drawState.getTexture(s));
junov@google.com2f839402011-05-24 15:13:01 +0000389 GrGLTexture::Orientation orientation = texture->orientation();
390
391 // vertical flip if necessary
392 if (GrGLTexture::kBottomUp_Orientation == orientation) {
393 values[1] = 1.0f - values[1];
394 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000395 // The top and bottom were just flipped, so correct the ordering
396 // of elements so that values = (l, t, r, b).
397 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000398 }
399
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000400 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000401 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000402 }
403}
404
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000405void GrGpuGL::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000406 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000407 const GrDrawState& drawState = this->getDrawState();
408 const GrGLTexture* texture =
409 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000410 if (NULL != texture) {
bsalomon@google.com341767c2012-05-11 20:47:39 +0000411 const GrMatrix& hwMatrix = fProgramData->fTextureMatrices[s];
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000412 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000413 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000414 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000415 !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000416
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000417 GrMatrix m = samplerMatrix;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000418 GrSamplerState::SampleMode mode =
419 drawState.getSampler(s).getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000420 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000421
bsalomon@google.com91961302011-05-09 18:39:58 +0000422 // ES doesn't allow you to pass true to the transpose param,
423 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000424 GrGLfloat mt[] = {
425 GrScalarToFloat(m[GrMatrix::kMScaleX]),
426 GrScalarToFloat(m[GrMatrix::kMSkewY]),
427 GrScalarToFloat(m[GrMatrix::kMPersp0]),
428 GrScalarToFloat(m[GrMatrix::kMSkewX]),
429 GrScalarToFloat(m[GrMatrix::kMScaleY]),
430 GrScalarToFloat(m[GrMatrix::kMPersp1]),
431 GrScalarToFloat(m[GrMatrix::kMTransX]),
432 GrScalarToFloat(m[GrMatrix::kMTransY]),
433 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000434 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000435
bsalomon@google.com341767c2012-05-11 20:47:39 +0000436 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
437 fProgramData->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000438 }
439 }
junov@google.comf93e7172011-03-31 21:26:24 +0000440}
441
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000442void GrGpuGL::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000443
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000444 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000445 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.com91961302011-05-09 18:39:58 +0000446 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000447 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
448 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
449 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000450
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000451 GrScalar centerX1 = sampler.getRadial2CenterX1();
452 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000453
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000454 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000455
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000456 // when were in the degenerate (linear) case the second
457 // value will be INF but the program doesn't read it. (We
458 // use the same 6 uniforms even though we don't need them
459 // all in the linear case just to keep the code complexity
460 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000461 float values[6] = {
462 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000463 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000464 GrScalarToFloat(centerX1),
465 GrScalarToFloat(radius0),
466 GrScalarToFloat(GrMul(radius0, radius0)),
467 sampler.isRadial2PosRoot() ? 1.f : -1.f
468 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000469 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000470 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
471 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
472 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
473 }
474}
475
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000476void GrGpuGL::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000477 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000478 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000479 const GrGLTexture* texture =
480 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000481 if (texture->width() != fProgramData->fTextureWidth[s] ||
482 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000483
bsalomon@google.com99621082011-11-15 16:47:16 +0000484 float texelSize[] = {1.f / texture->width(),
485 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000486 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000487 fProgramData->fTextureWidth[s] = texture->width();
488 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000489 }
490 }
junov@google.comf93e7172011-03-31 21:26:24 +0000491}
492
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000493void GrGpuGL::flushColorMatrix() {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000494 const ProgramDesc& desc = fCurrentProgram.getDesc();
495 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
496 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
497 if (GrGLProgram::kUnusedUniform != matrixUni
498 && GrGLProgram::kUnusedUniform != vecUni) {
499 const float* m = this->getDrawState().getColorMatrix();
500 GrGLfloat mt[] = {
501 m[0], m[5], m[10], m[15],
502 m[1], m[6], m[11], m[16],
503 m[2], m[7], m[12], m[17],
504 m[3], m[8], m[13], m[18],
505 };
506 static float scale = 1.0f / 255.0f;
507 GrGLfloat vec[] = {
508 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
509 };
510 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
511 GL_CALL(Uniform4fv(vecUni, 1, vec));
512 }
513}
514
Scroggo01b87ec2011-05-11 18:05:38 +0000515static const float ONE_OVER_255 = 1.f / 255.f;
516
517#define GR_COLOR_TO_VEC4(color) {\
518 GrColorUnpackR(color) * ONE_OVER_255,\
519 GrColorUnpackG(color) * ONE_OVER_255,\
520 GrColorUnpackB(color) * ONE_OVER_255,\
521 GrColorUnpackA(color) * ONE_OVER_255 \
522}
523
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000524void GrGpuGL::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000525 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000526 const GrDrawState& drawState = this->getDrawState();
527
bsalomon@google.come79c8152012-03-29 19:07:12 +0000528 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000529 // color will be specified per-vertex as an attribute
530 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000531 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000532 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000533 switch (desc.fColorInput) {
534 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000535 if (fHWConstAttribColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000536 // OpenGL ES only supports the float varieties of
537 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000538 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000539 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
540 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000541 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000542 }
543 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000544 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000545 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000546 // OpenGL ES doesn't support unsigned byte varieties of
547 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000548 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000549 GrAssert(GrGLProgram::kUnusedUniform !=
550 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000551 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
552 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000553 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000554 }
555 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000556 case ProgramDesc::kSolidWhite_ColorInput:
557 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000558 break;
559 default:
560 GrCrash("Unknown color type.");
561 }
562 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000563 if (fProgramData->fUniLocations.fColorFilterUni
564 != GrGLProgram::kUnusedUniform
565 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000566 != drawState.getColorFilterColor()) {
567 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000568 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000569 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000570 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000571}
572
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000573void GrGpuGL::flushCoverage(GrColor coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000574 const ProgramDesc& desc = fCurrentProgram.getDesc();
575 const GrDrawState& drawState = this->getDrawState();
576
577
bsalomon@google.come79c8152012-03-29 19:07:12 +0000578 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000579 // coverage will be specified per-vertex as an attribute
580 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000581 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000582 } else {
583 switch (desc.fCoverageInput) {
584 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000585 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000586 // OpenGL ES only supports the float varieties of
587 // glVertexAttrib
588 float c[] = GR_COLOR_TO_VEC4(coverage);
589 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
590 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000591 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000592 }
593 break;
594 case ProgramDesc::kUniform_ColorInput:
595 if (fProgramData->fCoverage != coverage) {
596 // OpenGL ES doesn't support unsigned byte varieties of
597 // glUniform
598 float c[] = GR_COLOR_TO_VEC4(coverage);
599 GrAssert(GrGLProgram::kUnusedUniform !=
600 fProgramData->fUniLocations.fCoverageUni);
601 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
602 1, c));
603 fProgramData->fCoverage = coverage;
604 }
605 break;
606 case ProgramDesc::kSolidWhite_ColorInput:
607 case ProgramDesc::kTransBlack_ColorInput:
608 break;
609 default:
610 GrCrash("Unknown coverage type.");
611 }
612 }
613}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000614
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000615bool GrGpuGL::flushGraphicsState(GrPrimitiveType type) {
junov@google.comf93e7172011-03-31 21:26:24 +0000616 if (!flushGLStateCommon(type)) {
617 return false;
618 }
619
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000620 const GrDrawState& drawState = this->getDrawState();
621
junov@google.comf93e7172011-03-31 21:26:24 +0000622 if (fDirtyFlags.fRenderTargetChanged) {
junov@google.comf93e7172011-03-31 21:26:24 +0000623 // we assume all shader matrices may be wrong after viewport changes
624 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000625 }
626
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000627 GrBlendCoeff srcCoeff;
628 GrBlendCoeff dstCoeff;
629 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
630 if (kSkipDraw_BlendOptFlag & blendOpts) {
631 return false;
632 }
633
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000634 GrCustomStage* customStages [GrDrawState::kNumStages];
635 this->buildProgram(type, blendOpts, dstCoeff, customStages);
636 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
637 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000638 if (NULL == fProgramData) {
639 GrAssert(!"Failed to create program!");
640 return false;
641 }
junov@google.comf93e7172011-03-31 21:26:24 +0000642
643 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000644 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000645 fHWProgramID = fProgramData->fProgramID;
646 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000647 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
648 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000649
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000650 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000651 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000652 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
653 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000654 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000655 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
656 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000657 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000658 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000659 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000660 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000661 }
662 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000663 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000664
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000665 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000666
tomhudson@google.com93813632011-10-27 20:21:16 +0000667 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000668 if (this->isStageEnabled(s)) {
669 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000670
bsalomon@google.com40d92932011-12-13 18:40:47 +0000671 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000672
bsalomon@google.com40d92932011-12-13 18:40:47 +0000673 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000674
bsalomon@google.com40d92932011-12-13 18:40:47 +0000675 this->flushTextureDomain(s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000676
677 if (NULL != fProgramData->fCustomStage[s]) {
678 const GrSamplerState& sampler =
679 this->getDrawState().getSampler(s);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000680 const GrGLTexture* texture =
681 static_cast<const GrGLTexture*>(
682 this->getDrawState().getTexture(s));
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000683 fProgramData->fCustomStage[s]->setData(
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000684 this->glInterface(), *texture,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000685 *sampler.getCustomStage(), s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000686 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000687 }
junov@google.comf93e7172011-03-31 21:26:24 +0000688 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000689 this->flushColorMatrix();
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000690 resetDirtyFlags();
junov@google.comf93e7172011-03-31 21:26:24 +0000691 return true;
692}
693
bsalomon@google.com2717d562012-05-07 19:10:52 +0000694#if GR_TEXT_SCALAR_IS_USHORT
695 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
696 #define TEXT_COORDS_ARE_NORMALIZED 1
697#elif GR_TEXT_SCALAR_IS_FLOAT
698 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
699 #define TEXT_COORDS_ARE_NORMALIZED 0
700#elif GR_TEXT_SCALAR_IS_FIXED
701 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
702 #define TEXT_COORDS_ARE_NORMALIZED 0
703#else
704 #error "unknown GR_TEXT_SCALAR type"
705#endif
706
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000707void GrGpuGL::setupGeometry(int* startVertex,
junov@google.comf93e7172011-03-31 21:26:24 +0000708 int* startIndex,
709 int vertexCount,
710 int indexCount) {
711
712 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000713 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000714 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000715 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000716
bsalomon@google.come79c8152012-03-29 19:07:12 +0000717 GrVertexLayout currLayout = this->getVertexLayout();
718
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000719 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000720 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000721 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000722 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000723 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000724 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000725 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000726 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000727 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000728 int oldEdgeOffset;
729
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000730 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
731 fHWGeometryState.fVertexLayout,
732 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000733 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000734 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000735 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000736 bool indexed = NULL != startIndex;
737
738 int extraVertexOffset;
739 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000740 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000741
742 GrGLenum scalarType;
743 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000744 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000745 scalarType = TEXT_COORDS_GL_TYPE;
746 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000747 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000748 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
749 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000750 texCoordNorm = false;
751 }
752
753 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
754 *startVertex = 0;
755 if (indexed) {
756 *startIndex += extraIndexOffset;
757 }
758
759 // all the Pointers must be set if any of these are true
760 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
761 vertexOffset != fHWGeometryState.fVertexOffset ||
762 newStride != oldStride;
763
764 // position and tex coord offsets change if above conditions are true
765 // or the type/normalization changed based on text vs nontext type coords.
766 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000767 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000768 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000769 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000770
771 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000772 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000773 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000774 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000775 fHWGeometryState.fVertexOffset = vertexOffset;
776 }
777
tomhudson@google.com93813632011-10-27 20:21:16 +0000778 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000779 if (newTexCoordOffsets[t] > 0) {
780 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000781 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000782 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000783 GL_CALL(EnableVertexAttribArray(idx));
784 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000785 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000786 } else if (posAndTexChange ||
787 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000788 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000789 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000790 }
791 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000792 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000793 }
794 }
795
796 if (newColorOffset > 0) {
797 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000798 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000799 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000800 GL_CALL(EnableVertexAttribArray(idx));
801 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000802 true, newStride, colorOffset));
803 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000804 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000805 true, newStride, colorOffset));
806 }
807 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000808 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000809 }
810
bsalomon@google.coma3108262011-10-10 14:08:47 +0000811 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000812 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000813 int idx = GrGLProgram::CoverageAttributeIdx();
814 if (oldCoverageOffset <= 0) {
815 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000816 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000817 true, newStride, coverageOffset));
818 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000819 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000820 true, newStride, coverageOffset));
821 }
822 } else if (oldCoverageOffset > 0) {
823 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
824 }
825
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000826 if (newEdgeOffset > 0) {
827 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
828 int idx = GrGLProgram::EdgeAttributeIdx();
829 if (oldEdgeOffset <= 0) {
830 GL_CALL(EnableVertexAttribArray(idx));
831 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
832 false, newStride, edgeOffset));
833 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
834 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
835 false, newStride, edgeOffset));
836 }
837 } else if (oldEdgeOffset > 0) {
838 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
839 }
840
bsalomon@google.come79c8152012-03-29 19:07:12 +0000841 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000842 fHWGeometryState.fArrayPtrsDirty = false;
843}
844
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000845namespace {
846
847void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
848 const GrSamplerState& sampler,
849 GrCustomStage** customStages,
850 GrGLProgram* program, int index) {
851 GrCustomStage* customStage = sampler.getCustomStage();
852 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000853 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.comb505a122012-05-31 18:40:36 +0000854 stage->fCustomStageKey = factory.glStageKey(*customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000855 customStages[index] = customStage;
856 } else {
857 stage->fCustomStageKey = 0;
858 customStages[index] = NULL;
859 }
860}
861
862}
863
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000864void GrGpuGL::buildProgram(GrPrimitiveType type,
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000865 BlendOptFlags blendOpts,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000866 GrBlendCoeff dstCoeff,
867 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000868 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000869 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000870
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000871 // This should already have been caught
872 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
873
874 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
875
876 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
877 kEmitCoverage_BlendOptFlag));
878
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000879 // The descriptor is used as a cache key. Thus when a field of the
880 // descriptor will not affect program generation (because of the vertex
881 // layout in use or other descriptor field settings) it should be set
882 // to a canonical value to avoid duplicate programs with different keys.
883
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000884 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000885 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000886
887 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
888
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000889 bool requiresAttributeColors =
890 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000891 bool requiresAttributeCoverage =
892 !skipCoverage && SkToBool(desc.fVertexLayout &
893 kCoverage_VertexLayoutBit);
894
895 // fColorInput/fCoverageInput records how colors are specified for the.
896 // program. So we strip the bits from the layout to avoid false negatives
897 // when searching for an existing program in the cache.
898 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000899
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000900 desc.fColorFilterXfermode = skipColor ?
901 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000902 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000903
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000904 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
905
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000906 // no reason to do edge aa or look at per-vertex coverage if coverage is
907 // ignored
908 if (skipCoverage) {
909 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
910 kCoverage_VertexLayoutBit);
911 }
912
913 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
914 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
915 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000916 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000917 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000918 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000919 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000920 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000921 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000922 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000923 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000924 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000925 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000926
927 bool covIsSolidWhite = !requiresAttributeCoverage &&
928 0xffffffff == drawState.getCoverage();
929
930 if (skipCoverage) {
931 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
932 } else if (covIsSolidWhite) {
933 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
934 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
935 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
936 } else {
937 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
938 }
junov@google.comf93e7172011-03-31 21:26:24 +0000939
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000940 int lastEnabledStage = -1;
941
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000942 if (!skipCoverage && (desc.fVertexLayout &
943 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000944 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000945 } else {
946 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +0000947 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000948 }
949
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000950 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000951 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000952
953 stage.fOptFlags = 0;
954 stage.setEnabled(this->isStageEnabled(s));
955
956 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
957 skipCoverage;
958
959 if (!skip && stage.isEnabled()) {
960 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000961 const GrGLTexture* texture =
962 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000963 GrAssert(NULL != texture);
964 const GrSamplerState& sampler = drawState.getSampler(s);
965 // we matrix to invert when orientation is TopDown, so make sure
966 // we aren't in that case before flagging as identity.
967 if (TextureMatrixIsIdentity(texture, sampler)) {
968 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
969 } else if (!sampler.getMatrix().hasPerspective()) {
970 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
971 }
972 switch (sampler.getSampleMode()) {
973 case GrSamplerState::kNormal_SampleMode:
974 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
975 break;
976 case GrSamplerState::kRadial_SampleMode:
977 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
978 break;
979 case GrSamplerState::kRadial2_SampleMode:
980 if (sampler.radial2IsDegenerate()) {
981 stage.fCoordMapping =
982 StageDesc::kRadial2GradientDegenerate_CoordMapping;
983 } else {
984 stage.fCoordMapping =
985 StageDesc::kRadial2Gradient_CoordMapping;
986 }
987 break;
988 case GrSamplerState::kSweep_SampleMode:
989 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
990 break;
991 default:
992 GrCrash("Unexpected sample mode!");
993 break;
994 }
995
996 switch (sampler.getFilter()) {
997 // these both can use a regular texture2D()
998 case GrSamplerState::kNearest_Filter:
999 case GrSamplerState::kBilinear_Filter:
1000 stage.fFetchMode = StageDesc::kSingle_FetchMode;
1001 break;
1002 // performs 4 texture2D()s
1003 case GrSamplerState::k4x4Downsample_Filter:
1004 stage.fFetchMode = StageDesc::k2x2_FetchMode;
1005 break;
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001006 default:
1007 GrCrash("Unexpected filter!");
1008 break;
1009 }
1010
1011 if (sampler.hasTextureDomain()) {
1012 GrAssert(GrSamplerState::kClamp_WrapMode ==
1013 sampler.getWrapX() &&
1014 GrSamplerState::kClamp_WrapMode ==
1015 sampler.getWrapY());
1016 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
1017 }
1018
1019 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001020 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001021 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1022 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +00001023 // the shader must smear the single channel after
1024 // reading the texture
1025 if (this->glCaps().textureRedSupport()) {
1026 // we can use R8 textures so use kSmearRed
1027 stage.fInConfigFlags |=
1028 StageDesc::kSmearRed_InConfigFlag;
1029 } else {
1030 // we can use A8 textures so use kSmearAlpha
1031 stage.fInConfigFlags |=
1032 StageDesc::kSmearAlpha_InConfigFlag;
1033 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001034 } else if (sampler.swapsRAndB()) {
1035 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
1036 }
1037 }
1038 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001039 // The shader generator assumes that color channels are bytes
1040 // when rounding.
1041 GrAssert(4 == GrBytesPerPixel(texture->config()));
1042 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
1043 fUnpremulConversion) {
1044 stage.fInConfigFlags |=
1045 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
1046 } else {
1047 stage.fInConfigFlags |=
1048 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
1049 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001050 }
1051
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001052 setup_custom_stage(&stage, sampler, customStages,
1053 &fCurrentProgram, s);
1054
junov@google.comf93e7172011-03-31 21:26:24 +00001055 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001056 stage.fOptFlags = 0;
1057 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1058 stage.fInConfigFlags = 0;
1059 stage.fFetchMode = (StageDesc::FetchMode) 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001060 stage.fCustomStageKey = 0;
1061 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +00001062 }
1063 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001064
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001065 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001066 // The shader generator assumes that color channels are bytes
1067 // when rounding.
1068 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
1069 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
1070 desc.fOutputConfig =
1071 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
1072 } else {
1073 desc.fOutputConfig =
1074 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
1075 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001076 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001077 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001078 }
1079
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001080 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001081
1082 // currently the experimental GS will only work with triangle prims
1083 // (and it doesn't do anything other than pass through values from
1084 // the VS to the FS anyway).
1085#if 0 && GR_GL_EXPERIMENTAL_GS
1086 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1087#endif
1088
bsalomon@google.coma3108262011-10-10 14:08:47 +00001089 // we want to avoid generating programs with different "first cov stage"
1090 // values when they would compute the same result.
1091 // We set field in the desc to kNumStages when either there are no
1092 // coverage stages or the distinction between coverage and color is
1093 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001094 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +00001095 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001096 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001097 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001098 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +00001099 }
1100
1101 // other coverage inputs
1102 if (!hasCoverage) {
1103 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001104 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +00001105 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1106 }
1107
1108 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001109 // color filter is applied between color/coverage computation
1110 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001111 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001112 }
1113
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001114 if (this->getCaps().fDualSourceBlendingSupport &&
1115 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1116 kCoverageAsAlpha_BlendOptFlag))) {
1117 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001118 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001119 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001120 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001121 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001122 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1123 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001124 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001125 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001126 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001127 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1128 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001129 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001130 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001131 }
1132 }
1133 }
junov@google.comf93e7172011-03-31 21:26:24 +00001134}