blob: 177f48a1a877de5ae78cc56aff1f2b2399ee7adc [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.com341767c2012-05-11 20:47:39 +00008#include "GrGpuGLShaders.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000010#include "GrBinHashKey.h"
11#include "effects/GrConvolutionEffect.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000012#include "GrCustomStage.h"
junov@google.comf93e7172011-03-31 21:26:24 +000013#include "GrGLProgram.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
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000025#include "../GrTHashCache.h"
junov@google.comf93e7172011-03-31 21:26:24 +000026
27class GrGpuGLShaders::ProgramCache : public ::GrNoncopyable {
28private:
29 class Entry;
30
junov@google.comf7c00f62011-08-18 18:15:16 +000031 typedef GrBinHashKey<Entry, GrGLProgram::kProgramKeySize> ProgramHashKey;
junov@google.comf93e7172011-03-31 21:26:24 +000032
33 class Entry : public ::GrNoncopyable {
34 public:
35 Entry() {}
junov@google.comf93e7172011-03-31 21:26:24 +000036 void copyAndTakeOwnership(Entry& entry) {
37 fProgramData.copyAndTakeOwnership(entry.fProgramData);
junov@google.comf7c00f62011-08-18 18:15:16 +000038 fKey = entry.fKey; // ownership transfer
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000039 fLRUStamp = entry.fLRUStamp;
junov@google.comf93e7172011-03-31 21:26:24 +000040 }
41
42 public:
43 int compare(const ProgramHashKey& key) const { return fKey.compare(key); }
44
45 public:
46 GrGLProgram::CachedData fProgramData;
47 ProgramHashKey fKey;
48 unsigned int fLRUStamp;
49 };
50
51 GrTHashTable<Entry, ProgramHashKey, 8> fHashCache;
52
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000053 // We may have kMaxEntries+1 shaders in the GL context because
54 // we create a new shader before evicting from the cache.
junov@google.comf93e7172011-03-31 21:26:24 +000055 enum {
56 kMaxEntries = 32
57 };
bsalomon@google.com4fa66942011-09-20 19:06:12 +000058 Entry fEntries[kMaxEntries];
59 int fCount;
60 unsigned int fCurrLRUStamp;
bsalomon@google.com96399942012-02-13 14:39:16 +000061 const GrGLContextInfo& fGL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000062
junov@google.comf93e7172011-03-31 21:26:24 +000063public:
bsalomon@google.com96399942012-02-13 14:39:16 +000064 ProgramCache(const GrGLContextInfo& gl)
junov@google.comf93e7172011-03-31 21:26:24 +000065 : fCount(0)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000066 , fCurrLRUStamp(0)
bsalomon@google.com96399942012-02-13 14:39:16 +000067 , fGL(gl) {
junov@google.comf93e7172011-03-31 21:26:24 +000068 }
69
70 ~ProgramCache() {
71 for (int i = 0; i < fCount; ++i) {
bsalomon@google.com96399942012-02-13 14:39:16 +000072 GrGpuGLShaders::DeleteProgram(fGL.interface(),
73 &fEntries[i].fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000074 }
75 }
76
77 void abandon() {
78 fCount = 0;
79 }
80
81 void invalidateViewMatrices() {
82 for (int i = 0; i < fCount; ++i) {
83 // set to illegal matrix
84 fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix();
85 }
86 }
87
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000088 GrGLProgram::CachedData* getProgramData(const GrGLProgram& desc,
89 GrCustomStage** stages) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000090 Entry newEntry;
junov@google.comf7c00f62011-08-18 18:15:16 +000091 newEntry.fKey.setKeyData(desc.keyData());
92
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000093 Entry* entry = fHashCache.find(newEntry.fKey);
junov@google.comf93e7172011-03-31 21:26:24 +000094 if (NULL == entry) {
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000095 if (!desc.genProgram(fGL, stages, &newEntry.fProgramData)) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000096 return NULL;
97 }
junov@google.comf93e7172011-03-31 21:26:24 +000098 if (fCount < kMaxEntries) {
99 entry = fEntries + fCount;
100 ++fCount;
101 } else {
102 GrAssert(kMaxEntries == fCount);
103 entry = fEntries;
104 for (int i = 1; i < kMaxEntries; ++i) {
105 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
106 entry = fEntries + i;
107 }
108 }
109 fHashCache.remove(entry->fKey, entry);
bsalomon@google.com96399942012-02-13 14:39:16 +0000110 GrGpuGLShaders::DeleteProgram(fGL.interface(),
111 &entry->fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +0000112 }
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000113 entry->copyAndTakeOwnership(newEntry);
junov@google.comf93e7172011-03-31 21:26:24 +0000114 fHashCache.insert(entry->fKey, entry);
115 }
116
117 entry->fLRUStamp = fCurrLRUStamp;
118 if (GR_UINT32_MAX == fCurrLRUStamp) {
119 // wrap around! just trash our LRU, one time hit.
120 for (int i = 0; i < fCount; ++i) {
121 fEntries[i].fLRUStamp = 0;
122 }
123 }
124 ++fCurrLRUStamp;
125 return &entry->fProgramData;
126 }
127};
128
junov@google.com53a55842011-06-08 22:55:10 +0000129void GrGpuGLShaders::abandonResources(){
130 INHERITED::abandonResources();
131 fProgramCache->abandon();
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000132 fHWProgramID = 0;
junov@google.com53a55842011-06-08 22:55:10 +0000133}
134
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000135void GrGpuGLShaders::DeleteProgram(const GrGLInterface* gl,
136 CachedData* programData) {
137 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000138 if (programData->fGShaderID) {
139 GR_GL_CALL(gl, DeleteShader(programData->fGShaderID));
140 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000141 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
142 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000143 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
144}
145
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000146////////////////////////////////////////////////////////////////////////////////
147
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000148#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
149
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000150namespace {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000151
bsalomon@google.com74b98712011-11-11 19:46:16 +0000152// GrRandoms nextU() values have patterns in the low bits
153// So using nextU() % array_count might never take some values.
154int random_int(GrRandom* r, int count) {
155 return (int)(r->nextF() * count);
156}
157
158// min is inclusive, max is exclusive
159int random_int(GrRandom* r, int min, int max) {
160 return (int)(r->nextF() * (max-min)) + min;
161}
162
163bool random_bool(GrRandom* r) {
164 return r->nextF() > .5f;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000165}
166
167}
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000168
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000169bool GrGpuGLShaders::programUnitTest() {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000170
tomhudson@google.com086e5352011-12-08 14:44:10 +0000171 GrGLSLGeneration glslGeneration =
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000172 GrGetGLSLGeneration(this->glBinding(), this->glInterface());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000173 static const int STAGE_OPTS[] = {
174 0,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000175 StageDesc::kNoPerspective_OptFlagBit,
176 StageDesc::kIdentity_CoordMapping
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000177 };
bsalomon@google.com74b98712011-11-11 19:46:16 +0000178 static const int IN_CONFIG_FLAGS[] = {
179 StageDesc::kNone_InConfigFlag,
180 StageDesc::kSwapRAndB_InConfigFlag,
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000181 StageDesc::kSwapRAndB_InConfigFlag |
182 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag,
183 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000184 StageDesc::kSmearAlpha_InConfigFlag,
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000185 StageDesc::kSmearRed_InConfigFlag,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000186 };
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000187 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000188 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000189
190 static const int NUM_TESTS = 512;
191
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000192 GrRandom random;
193 for (int t = 0; t < NUM_TESTS; ++t) {
194
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000195#if 0
196 GrPrintf("\nTest Program %d\n-------------\n", t);
197 static const int stop = -1;
198 if (t == stop) {
199 int breakpointhere = 9;
200 }
201#endif
202
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000203 pdesc.fVertexLayout = 0;
204 pdesc.fEmitsPointSize = random.nextF() > .5f;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000205 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000206 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000207
bsalomon@google.com74b98712011-11-11 19:46:16 +0000208 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000209
bsalomon@google.com74b98712011-11-11 19:46:16 +0000210 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000211
bsalomon@google.com74b98712011-11-11 19:46:16 +0000212 pdesc.fVertexLayout |= random_bool(&random) ?
bsalomon@google.coma3108262011-10-10 14:08:47 +0000213 GrDrawTarget::kCoverage_VertexLayoutBit :
214 0;
215
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000216#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com6f92f182011-09-29 15:05:48 +0000217 pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport &&
bsalomon@google.com74b98712011-11-11 19:46:16 +0000218 random_bool(&random);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000219#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000220 pdesc.fOutputConfig = random_int(&random, ProgramDesc::kOutputConfigCnt);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000221
bsalomon@google.com74b98712011-11-11 19:46:16 +0000222 bool edgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000223 if (edgeAA) {
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000224 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
225 if (this->getCaps().fShaderDerivativeSupport) {
226 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000227 } else {
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000228 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000229 }
230 } else {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000231 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000232
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000233 pdesc.fColorMatrixEnabled = random_bool(&random);
234
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000235 if (this->getCaps().fDualSourceBlendingSupport) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000236 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000237 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000238 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000239 }
240
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000241 GrCustomStage* customStages[GrDrawState::kNumStages];
242
tomhudson@google.com93813632011-10-27 20:21:16 +0000243 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000244 // enable the stage?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000245 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000246 // use separate tex coords?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000247 if (random_bool(&random)) {
248 int t = random_int(&random, GrDrawState::kMaxTexCoords);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000249 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
250 } else {
251 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
252 }
253 }
254 // use text-formatted verts?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000255 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000256 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
257 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000258 StageDesc& stage = pdesc.fStages[s];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000259
260 stage.fCustomStageKey = 0;
261 customStages[s] = NULL;
262
bsalomon@google.com74b98712011-11-11 19:46:16 +0000263 stage.fOptFlags = STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
264 stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];
265 stage.fCoordMapping = random_int(&random, StageDesc::kCoordMappingCnt);
266 stage.fFetchMode = random_int(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000267 // convolution shaders don't work with persp tex matrix
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000268 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode ||
269 stage.fFetchMode == StageDesc::kDilate_FetchMode ||
270 stage.fFetchMode == StageDesc::kErode_FetchMode) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000271 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
272 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000273 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000274 static const uint32_t kMulByAlphaMask =
275 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
276 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000277
bsalomon@google.com74b98712011-11-11 19:46:16 +0000278 switch (stage.fFetchMode) {
279 case StageDesc::kSingle_FetchMode:
280 stage.fKernelWidth = 0;
281 break;
282 case StageDesc::kConvolution_FetchMode:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000283 case StageDesc::kDilate_FetchMode:
284 case StageDesc::kErode_FetchMode:
bsalomon@google.com74b98712011-11-11 19:46:16 +0000285 stage.fKernelWidth = random_int(&random, 2, 8);
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000286 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000287 break;
288 case StageDesc::k2x2_FetchMode:
289 stage.fKernelWidth = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000290 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000291 break;
292 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000293
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000294 // TODO: is there a more elegant way to express this?
295 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) {
296 int direction = random_int(&random, 2);
tomhudson@google.comf1d88062012-05-10 12:43:21 +0000297 float kernel[MAX_KERNEL_WIDTH];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000298 for (int i = 0; i < stage.fKernelWidth; i++) {
299 kernel[i] = random.nextF();
300 }
301 customStages[s] = new GrConvolutionEffect(
302 (GrSamplerState::FilterDirection)direction,
303 stage.fKernelWidth, kernel);
304 stage.fCustomStageKey =
305 customStages[s]->getFactory()->stageKey(customStages[s]);
306 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000307 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000308 CachedData cachedData;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000309 if (!program.genProgram(this->glContextInfo(), customStages,
310 &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000311 return false;
312 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000313 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000314 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000315 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000316}
317
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000318GrGpuGLShaders::GrGpuGLShaders(const GrGLContextInfo& ctxInfo)
319 : GrGpuGL(ctxInfo) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000320
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000321 // Enable supported shader-related caps
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000322 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000323 fCaps.fDualSourceBlendingSupport =
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000324 this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000325 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000326 fCaps.fShaderDerivativeSupport = true;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000327 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
328 fCaps.fGeometryShaderSupport =
329 this->glVersion() >= GR_GL_VER(3,2) &&
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000330 this->glslGeneration() >= k150_GrGLSLGeneration;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000331 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000332 fCaps.fShaderDerivativeSupport =
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000333 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000334 }
junov@google.comf93e7172011-03-31 21:26:24 +0000335
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000336 GR_GL_GetIntegerv(this->glInterface(),
337 GR_GL_MAX_VERTEX_ATTRIBS,
338 &fMaxVertexAttribs);
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000339
junov@google.comf93e7172011-03-31 21:26:24 +0000340 fProgramData = NULL;
bsalomon@google.com96399942012-02-13 14:39:16 +0000341 fProgramCache = new ProgramCache(this->glContextInfo());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000342
343#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000344 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000345#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000346}
347
348GrGpuGLShaders::~GrGpuGLShaders() {
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000349
350 if (fProgramData && 0 != fHWProgramID) {
351 // detach the current program so there is no confusion on OpenGL's part
352 // that we want it to be deleted
353 SkASSERT(fHWProgramID == fProgramData->fProgramID);
354 GL_CALL(UseProgram(0));
355 }
junov@google.comf93e7172011-03-31 21:26:24 +0000356 delete fProgramCache;
357}
358
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000359void GrGpuGLShaders::onResetContext() {
360 INHERITED::onResetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000361
junov@google.comf93e7172011-03-31 21:26:24 +0000362 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000363
364 // Third party GL code may have left vertex attributes enabled. Some GL
365 // implementations (osmesa) may read vetex attributes that are not required
366 // by the current shader. Therefore, we have to ensure that only the
367 // attributes we require for the current draw are enabled or we may cause an
368 // invalid read.
369
370 // Disable all vertex layout bits so that next flush will assume all
371 // optional vertex attributes are disabled.
372 fHWGeometryState.fVertexLayout = 0;
373
374 // We always use the this attribute and assume it is always enabled.
375 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
376 GL_CALL(EnableVertexAttribArray(posAttrIdx));
377 // Disable all other vertex attributes.
378 for (int va = 0; va < fMaxVertexAttribs; ++va) {
379 if (va != posAttrIdx) {
380 GL_CALL(DisableVertexAttribArray(va));
381 }
junov@google.comf93e7172011-03-31 21:26:24 +0000382 }
junov@google.comf93e7172011-03-31 21:26:24 +0000383
384 fHWProgramID = 0;
385}
386
387void GrGpuGLShaders::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000388 const GrMatrix& vm = this->getDrawState().getViewMatrix();
bsalomon@google.com341767c2012-05-11 20:47:39 +0000389 if (!fProgramData->fViewMatrix.cheapEqualTo(vm)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000390
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000391 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
392 GrAssert(NULL != rt);
393 GrMatrix m;
394 m.setAll(
395 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
396 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
397 0, 0, GrMatrix::I()[8]);
398 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000399
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000400 // ES doesn't allow you to pass true to the transpose param,
401 // so do our own transpose
402 GrGLfloat mt[] = {
403 GrScalarToFloat(m[GrMatrix::kMScaleX]),
404 GrScalarToFloat(m[GrMatrix::kMSkewY]),
405 GrScalarToFloat(m[GrMatrix::kMPersp0]),
406 GrScalarToFloat(m[GrMatrix::kMSkewX]),
407 GrScalarToFloat(m[GrMatrix::kMScaleY]),
408 GrScalarToFloat(m[GrMatrix::kMPersp1]),
409 GrScalarToFloat(m[GrMatrix::kMTransX]),
410 GrScalarToFloat(m[GrMatrix::kMTransY]),
411 GrScalarToFloat(m[GrMatrix::kMPersp2])
412 };
413
bsalomon@google.com341767c2012-05-11 20:47:39 +0000414 GrAssert(GrGLProgram::kUnusedUniform !=
415 fProgramData->fUniLocations.fViewMatrixUni);
416 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
417 1, false, mt));
418 fProgramData->fViewMatrix = vm;
bsalomon@google.com91961302011-05-09 18:39:58 +0000419 }
junov@google.comf93e7172011-03-31 21:26:24 +0000420}
421
junov@google.com6acc9b32011-05-16 18:32:07 +0000422void GrGpuGLShaders::flushTextureDomain(int s) {
423 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000424 const GrDrawState& drawState = this->getDrawState();
junov@google.com6acc9b32011-05-16 18:32:07 +0000425 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000426 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
junov@google.com6acc9b32011-05-16 18:32:07 +0000427
twiz@google.com76b82742011-06-02 20:30:02 +0000428 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
429 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000430
junov@google.com2f839402011-05-24 15:13:01 +0000431 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000432
junov@google.com2f839402011-05-24 15:13:01 +0000433 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000434 GrScalarToFloat(texDom.left()),
435 GrScalarToFloat(texDom.top()),
436 GrScalarToFloat(texDom.right()),
437 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000438 };
439
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000440 const GrGLTexture* texture =
441 static_cast<const GrGLTexture*>(drawState.getTexture(s));
junov@google.com2f839402011-05-24 15:13:01 +0000442 GrGLTexture::Orientation orientation = texture->orientation();
443
444 // vertical flip if necessary
445 if (GrGLTexture::kBottomUp_Orientation == orientation) {
446 values[1] = 1.0f - values[1];
447 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000448 // The top and bottom were just flipped, so correct the ordering
449 // of elements so that values = (l, t, r, b).
450 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000451 }
452
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000453 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000454 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000455 }
456}
457
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000458void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000459 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000460 const GrDrawState& drawState = this->getDrawState();
461 const GrGLTexture* texture =
462 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000463 if (NULL != texture) {
bsalomon@google.com341767c2012-05-11 20:47:39 +0000464 const GrMatrix& hwMatrix = fProgramData->fTextureMatrices[s];
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000465 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000466 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000467 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000468 !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000469
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000470 GrMatrix m = samplerMatrix;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000471 GrSamplerState::SampleMode mode =
472 drawState.getSampler(s).getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000473 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000474
bsalomon@google.com91961302011-05-09 18:39:58 +0000475 // ES doesn't allow you to pass true to the transpose param,
476 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000477 GrGLfloat mt[] = {
478 GrScalarToFloat(m[GrMatrix::kMScaleX]),
479 GrScalarToFloat(m[GrMatrix::kMSkewY]),
480 GrScalarToFloat(m[GrMatrix::kMPersp0]),
481 GrScalarToFloat(m[GrMatrix::kMSkewX]),
482 GrScalarToFloat(m[GrMatrix::kMScaleY]),
483 GrScalarToFloat(m[GrMatrix::kMPersp1]),
484 GrScalarToFloat(m[GrMatrix::kMTransX]),
485 GrScalarToFloat(m[GrMatrix::kMTransY]),
486 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000487 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000488
bsalomon@google.com341767c2012-05-11 20:47:39 +0000489 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
490 fProgramData->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000491 }
492 }
junov@google.comf93e7172011-03-31 21:26:24 +0000493}
494
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000495void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000496
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000497 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000498 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.com91961302011-05-09 18:39:58 +0000499 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000500 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
501 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
502 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000503
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000504 GrScalar centerX1 = sampler.getRadial2CenterX1();
505 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000506
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000507 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000508
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000509 // when were in the degenerate (linear) case the second
510 // value will be INF but the program doesn't read it. (We
511 // use the same 6 uniforms even though we don't need them
512 // all in the linear case just to keep the code complexity
513 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000514 float values[6] = {
515 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000516 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000517 GrScalarToFloat(centerX1),
518 GrScalarToFloat(radius0),
519 GrScalarToFloat(GrMul(radius0, radius0)),
520 sampler.isRadial2PosRoot() ? 1.f : -1.f
521 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000522 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000523 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
524 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
525 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
526 }
527}
528
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000529void GrGpuGLShaders::flushConvolution(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000530 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000531 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
532 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000533 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
534 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000535 }
536 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
537 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000538 const GrGLTexture* texture =
539 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
540 float imageIncrement[2] = { 0 };
541 switch (sampler.getFilterDirection()) {
542 case GrSamplerState::kX_FilterDirection:
543 imageIncrement[0] = 1.0f / texture->width();
544 break;
545 case GrSamplerState::kY_FilterDirection:
546 imageIncrement[1] = 1.0f / texture->height();
547 break;
548 default:
549 GrCrash("Unknown filter direction.");
550 }
551 GL_CALL(Uniform2fv(imageIncrementUni, 1, imageIncrement));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000552 }
553}
554
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000555void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000556 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000557 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000558 const GrGLTexture* texture =
559 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000560 if (texture->width() != fProgramData->fTextureWidth[s] ||
561 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000562
bsalomon@google.com99621082011-11-15 16:47:16 +0000563 float texelSize[] = {1.f / texture->width(),
564 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000565 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000566 fProgramData->fTextureWidth[s] = texture->width();
567 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000568 }
569 }
junov@google.comf93e7172011-03-31 21:26:24 +0000570}
571
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000572void GrGpuGLShaders::flushColorMatrix() {
573 const ProgramDesc& desc = fCurrentProgram.getDesc();
574 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
575 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
576 if (GrGLProgram::kUnusedUniform != matrixUni
577 && GrGLProgram::kUnusedUniform != vecUni) {
578 const float* m = this->getDrawState().getColorMatrix();
579 GrGLfloat mt[] = {
580 m[0], m[5], m[10], m[15],
581 m[1], m[6], m[11], m[16],
582 m[2], m[7], m[12], m[17],
583 m[3], m[8], m[13], m[18],
584 };
585 static float scale = 1.0f / 255.0f;
586 GrGLfloat vec[] = {
587 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
588 };
589 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
590 GL_CALL(Uniform4fv(vecUni, 1, vec));
591 }
592}
593
Scroggo01b87ec2011-05-11 18:05:38 +0000594static const float ONE_OVER_255 = 1.f / 255.f;
595
596#define GR_COLOR_TO_VEC4(color) {\
597 GrColorUnpackR(color) * ONE_OVER_255,\
598 GrColorUnpackG(color) * ONE_OVER_255,\
599 GrColorUnpackB(color) * ONE_OVER_255,\
600 GrColorUnpackA(color) * ONE_OVER_255 \
601}
602
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000603void GrGpuGLShaders::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000604 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000605 const GrDrawState& drawState = this->getDrawState();
606
bsalomon@google.come79c8152012-03-29 19:07:12 +0000607 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000608 // color will be specified per-vertex as an attribute
609 // invalidate the const vertex attrib color
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000610 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000611 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000612 switch (desc.fColorInput) {
613 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000614 if (fHWDrawState.getColor() != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000615 // OpenGL ES only supports the float varieties of
616 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000617 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000618 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
619 c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000620 fHWDrawState.setColor(color);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000621 }
622 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000623 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000624 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000625 // OpenGL ES doesn't support unsigned byte varieties of
626 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000627 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000628 GrAssert(GrGLProgram::kUnusedUniform !=
629 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000630 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
631 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000632 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000633 }
634 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000635 case ProgramDesc::kSolidWhite_ColorInput:
636 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000637 break;
638 default:
639 GrCrash("Unknown color type.");
640 }
641 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000642 if (fProgramData->fUniLocations.fColorFilterUni
643 != GrGLProgram::kUnusedUniform
644 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000645 != drawState.getColorFilterColor()) {
646 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000647 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000648 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000649 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000650}
651
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000652void GrGpuGLShaders::flushCoverage(GrColor coverage) {
653 const ProgramDesc& desc = fCurrentProgram.getDesc();
654 const GrDrawState& drawState = this->getDrawState();
655
656
bsalomon@google.come79c8152012-03-29 19:07:12 +0000657 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000658 // coverage will be specified per-vertex as an attribute
659 // invalidate the const vertex attrib coverage
660 fHWDrawState.setCoverage4(GrColor_ILLEGAL);
661 } else {
662 switch (desc.fCoverageInput) {
663 case ProgramDesc::kAttribute_ColorInput:
664 if (fHWDrawState.getCoverage() != coverage) {
665 // OpenGL ES only supports the float varieties of
666 // glVertexAttrib
667 float c[] = GR_COLOR_TO_VEC4(coverage);
668 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
669 c));
670 fHWDrawState.setCoverage(coverage);
671 }
672 break;
673 case ProgramDesc::kUniform_ColorInput:
674 if (fProgramData->fCoverage != coverage) {
675 // OpenGL ES doesn't support unsigned byte varieties of
676 // glUniform
677 float c[] = GR_COLOR_TO_VEC4(coverage);
678 GrAssert(GrGLProgram::kUnusedUniform !=
679 fProgramData->fUniLocations.fCoverageUni);
680 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
681 1, c));
682 fProgramData->fCoverage = coverage;
683 }
684 break;
685 case ProgramDesc::kSolidWhite_ColorInput:
686 case ProgramDesc::kTransBlack_ColorInput:
687 break;
688 default:
689 GrCrash("Unknown coverage type.");
690 }
691 }
692}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000693
junov@google.comf93e7172011-03-31 21:26:24 +0000694bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
695 if (!flushGLStateCommon(type)) {
696 return false;
697 }
698
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000699 const GrDrawState& drawState = this->getDrawState();
700
junov@google.comf93e7172011-03-31 21:26:24 +0000701 if (fDirtyFlags.fRenderTargetChanged) {
702 // our coords are in pixel space and the GL matrices map to NDC
703 // so if the viewport changed, our matrix is now wrong.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000704 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
junov@google.comf93e7172011-03-31 21:26:24 +0000705 // we assume all shader matrices may be wrong after viewport changes
706 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000707 }
708
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000709 GrBlendCoeff srcCoeff;
710 GrBlendCoeff dstCoeff;
711 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
712 if (kSkipDraw_BlendOptFlag & blendOpts) {
713 return false;
714 }
715
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000716 GrCustomStage* customStages [GrDrawState::kNumStages];
717 this->buildProgram(type, blendOpts, dstCoeff, customStages);
718 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
719 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000720 if (NULL == fProgramData) {
721 GrAssert(!"Failed to create program!");
722 return false;
723 }
junov@google.comf93e7172011-03-31 21:26:24 +0000724
725 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000726 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000727 fHWProgramID = fProgramData->fProgramID;
728 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000729 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
730 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000731
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000732 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000733 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000734 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
735 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000736 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000737 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
738 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000739 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000740 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000741 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000742 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000743 }
744 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000745 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000746
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000747 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000748
tomhudson@google.com93813632011-10-27 20:21:16 +0000749 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000750 if (this->isStageEnabled(s)) {
751 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000752
bsalomon@google.com40d92932011-12-13 18:40:47 +0000753 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000754
bsalomon@google.com40d92932011-12-13 18:40:47 +0000755 this->flushConvolution(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000756
bsalomon@google.com40d92932011-12-13 18:40:47 +0000757 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000758
bsalomon@google.com40d92932011-12-13 18:40:47 +0000759 this->flushTextureDomain(s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000760
761 if (NULL != fProgramData->fCustomStage[s]) {
762 const GrSamplerState& sampler =
763 this->getDrawState().getSampler(s);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000764 const GrGLTexture* texture =
765 static_cast<const GrGLTexture*>(
766 this->getDrawState().getTexture(s));
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000767 fProgramData->fCustomStage[s]->setData(
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000768 this->glInterface(), sampler.getCustomStage(), texture);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000769 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000770 }
junov@google.comf93e7172011-03-31 21:26:24 +0000771 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000772 this->flushColorMatrix();
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000773 resetDirtyFlags();
junov@google.comf93e7172011-03-31 21:26:24 +0000774 return true;
775}
776
777void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000778}
779
bsalomon@google.com2717d562012-05-07 19:10:52 +0000780#if GR_TEXT_SCALAR_IS_USHORT
781 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
782 #define TEXT_COORDS_ARE_NORMALIZED 1
783#elif GR_TEXT_SCALAR_IS_FLOAT
784 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
785 #define TEXT_COORDS_ARE_NORMALIZED 0
786#elif GR_TEXT_SCALAR_IS_FIXED
787 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
788 #define TEXT_COORDS_ARE_NORMALIZED 0
789#else
790 #error "unknown GR_TEXT_SCALAR type"
791#endif
792
junov@google.comf93e7172011-03-31 21:26:24 +0000793void GrGpuGLShaders::setupGeometry(int* startVertex,
794 int* startIndex,
795 int vertexCount,
796 int indexCount) {
797
798 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000799 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000800 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000801 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000802
bsalomon@google.come79c8152012-03-29 19:07:12 +0000803 GrVertexLayout currLayout = this->getVertexLayout();
804
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000805 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000806 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000807 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000808 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000809 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000810 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000811 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000812 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000813 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000814 int oldEdgeOffset;
815
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000816 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
817 fHWGeometryState.fVertexLayout,
818 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000819 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000820 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000821 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000822 bool indexed = NULL != startIndex;
823
824 int extraVertexOffset;
825 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000826 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000827
828 GrGLenum scalarType;
829 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000830 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000831 scalarType = TEXT_COORDS_GL_TYPE;
832 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000833 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000834 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
835 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000836 texCoordNorm = false;
837 }
838
839 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
840 *startVertex = 0;
841 if (indexed) {
842 *startIndex += extraIndexOffset;
843 }
844
845 // all the Pointers must be set if any of these are true
846 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
847 vertexOffset != fHWGeometryState.fVertexOffset ||
848 newStride != oldStride;
849
850 // position and tex coord offsets change if above conditions are true
851 // or the type/normalization changed based on text vs nontext type coords.
852 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000853 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000854 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000855 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000856
857 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000858 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000859 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000860 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000861 fHWGeometryState.fVertexOffset = vertexOffset;
862 }
863
tomhudson@google.com93813632011-10-27 20:21:16 +0000864 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000865 if (newTexCoordOffsets[t] > 0) {
866 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000867 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000868 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000869 GL_CALL(EnableVertexAttribArray(idx));
870 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000871 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000872 } else if (posAndTexChange ||
873 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000874 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000875 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000876 }
877 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000878 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000879 }
880 }
881
882 if (newColorOffset > 0) {
883 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000884 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000885 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000886 GL_CALL(EnableVertexAttribArray(idx));
887 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000888 true, newStride, colorOffset));
889 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000890 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000891 true, newStride, colorOffset));
892 }
893 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000894 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000895 }
896
bsalomon@google.coma3108262011-10-10 14:08:47 +0000897 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000898 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000899 int idx = GrGLProgram::CoverageAttributeIdx();
900 if (oldCoverageOffset <= 0) {
901 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000902 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000903 true, newStride, coverageOffset));
904 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000905 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000906 true, newStride, coverageOffset));
907 }
908 } else if (oldCoverageOffset > 0) {
909 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
910 }
911
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000912 if (newEdgeOffset > 0) {
913 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
914 int idx = GrGLProgram::EdgeAttributeIdx();
915 if (oldEdgeOffset <= 0) {
916 GL_CALL(EnableVertexAttribArray(idx));
917 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
918 false, newStride, edgeOffset));
919 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
920 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
921 false, newStride, edgeOffset));
922 }
923 } else if (oldEdgeOffset > 0) {
924 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
925 }
926
bsalomon@google.come79c8152012-03-29 19:07:12 +0000927 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000928 fHWGeometryState.fArrayPtrsDirty = false;
929}
930
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000931namespace {
932
933void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
934 const GrSamplerState& sampler,
935 GrCustomStage** customStages,
936 GrGLProgram* program, int index) {
937 GrCustomStage* customStage = sampler.getCustomStage();
938 if (customStage) {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000939 GrProgramStageFactory* factory = customStage->getFactory();
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000940 stage->fCustomStageKey = factory->stageKey(customStage);
941 customStages[index] = customStage;
942 } else {
943 stage->fCustomStageKey = 0;
944 customStages[index] = NULL;
945 }
946}
947
948}
949
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000950void GrGpuGLShaders::buildProgram(GrPrimitiveType type,
951 BlendOptFlags blendOpts,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000952 GrBlendCoeff dstCoeff,
953 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000954 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000955 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000956
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000957 // This should already have been caught
958 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
959
960 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
961
962 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
963 kEmitCoverage_BlendOptFlag));
964
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000965 // The descriptor is used as a cache key. Thus when a field of the
966 // descriptor will not affect program generation (because of the vertex
967 // layout in use or other descriptor field settings) it should be set
968 // to a canonical value to avoid duplicate programs with different keys.
969
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000970 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000971 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000972
973 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
974
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000975 bool requiresAttributeColors =
976 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000977 bool requiresAttributeCoverage =
978 !skipCoverage && SkToBool(desc.fVertexLayout &
979 kCoverage_VertexLayoutBit);
980
981 // fColorInput/fCoverageInput records how colors are specified for the.
982 // program. So we strip the bits from the layout to avoid false negatives
983 // when searching for an existing program in the cache.
984 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000985
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000986 desc.fColorFilterXfermode = skipColor ?
987 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000988 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000989
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000990 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
991
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000992 // no reason to do edge aa or look at per-vertex coverage if coverage is
993 // ignored
994 if (skipCoverage) {
995 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
996 kCoverage_VertexLayoutBit);
997 }
998
999 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
1000 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
1001 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001002 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001003 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001004 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001005 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001006 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001007 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001008 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001009 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001010 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001011 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001012
1013 bool covIsSolidWhite = !requiresAttributeCoverage &&
1014 0xffffffff == drawState.getCoverage();
1015
1016 if (skipCoverage) {
1017 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
1018 } else if (covIsSolidWhite) {
1019 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
1020 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
1021 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
1022 } else {
1023 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
1024 }
junov@google.comf93e7172011-03-31 21:26:24 +00001025
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001026 int lastEnabledStage = -1;
1027
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001028 if (!skipCoverage && (desc.fVertexLayout &
1029 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001030 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001031 } else {
1032 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +00001033 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001034 }
1035
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001036 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001037 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001038
1039 stage.fOptFlags = 0;
1040 stage.setEnabled(this->isStageEnabled(s));
1041
1042 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
1043 skipCoverage;
1044
1045 if (!skip && stage.isEnabled()) {
1046 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001047 const GrGLTexture* texture =
1048 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001049 GrAssert(NULL != texture);
1050 const GrSamplerState& sampler = drawState.getSampler(s);
1051 // we matrix to invert when orientation is TopDown, so make sure
1052 // we aren't in that case before flagging as identity.
1053 if (TextureMatrixIsIdentity(texture, sampler)) {
1054 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
1055 } else if (!sampler.getMatrix().hasPerspective()) {
1056 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
1057 }
1058 switch (sampler.getSampleMode()) {
1059 case GrSamplerState::kNormal_SampleMode:
1060 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
1061 break;
1062 case GrSamplerState::kRadial_SampleMode:
1063 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
1064 break;
1065 case GrSamplerState::kRadial2_SampleMode:
1066 if (sampler.radial2IsDegenerate()) {
1067 stage.fCoordMapping =
1068 StageDesc::kRadial2GradientDegenerate_CoordMapping;
1069 } else {
1070 stage.fCoordMapping =
1071 StageDesc::kRadial2Gradient_CoordMapping;
1072 }
1073 break;
1074 case GrSamplerState::kSweep_SampleMode:
1075 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
1076 break;
1077 default:
1078 GrCrash("Unexpected sample mode!");
1079 break;
1080 }
1081
1082 switch (sampler.getFilter()) {
1083 // these both can use a regular texture2D()
1084 case GrSamplerState::kNearest_Filter:
1085 case GrSamplerState::kBilinear_Filter:
1086 stage.fFetchMode = StageDesc::kSingle_FetchMode;
1087 break;
1088 // performs 4 texture2D()s
1089 case GrSamplerState::k4x4Downsample_Filter:
1090 stage.fFetchMode = StageDesc::k2x2_FetchMode;
1091 break;
1092 // performs fKernelWidth texture2D()s
1093 case GrSamplerState::kConvolution_Filter:
1094 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
1095 break;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001096 case GrSamplerState::kDilate_Filter:
1097 stage.fFetchMode = StageDesc::kDilate_FetchMode;
1098 break;
1099 case GrSamplerState::kErode_Filter:
1100 stage.fFetchMode = StageDesc::kErode_FetchMode;
1101 break;
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001102 default:
1103 GrCrash("Unexpected filter!");
1104 break;
1105 }
1106
1107 if (sampler.hasTextureDomain()) {
1108 GrAssert(GrSamplerState::kClamp_WrapMode ==
1109 sampler.getWrapX() &&
1110 GrSamplerState::kClamp_WrapMode ==
1111 sampler.getWrapY());
1112 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
1113 }
1114
1115 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001116 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001117 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1118 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +00001119 // the shader must smear the single channel after
1120 // reading the texture
1121 if (this->glCaps().textureRedSupport()) {
1122 // we can use R8 textures so use kSmearRed
1123 stage.fInConfigFlags |=
1124 StageDesc::kSmearRed_InConfigFlag;
1125 } else {
1126 // we can use A8 textures so use kSmearAlpha
1127 stage.fInConfigFlags |=
1128 StageDesc::kSmearAlpha_InConfigFlag;
1129 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001130 } else if (sampler.swapsRAndB()) {
1131 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
1132 }
1133 }
1134 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001135 // The shader generator assumes that color channels are bytes
1136 // when rounding.
1137 GrAssert(4 == GrBytesPerPixel(texture->config()));
1138 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
1139 fUnpremulConversion) {
1140 stage.fInConfigFlags |=
1141 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
1142 } else {
1143 stage.fInConfigFlags |=
1144 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
1145 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001146 }
1147
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001148 if (sampler.getFilter() == GrSamplerState::kDilate_Filter ||
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001149 sampler.getFilter() == GrSamplerState::kErode_Filter) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001150 stage.fKernelWidth = sampler.getKernelWidth();
1151 } else {
1152 stage.fKernelWidth = 0;
1153 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001154
1155 setup_custom_stage(&stage, sampler, customStages,
1156 &fCurrentProgram, s);
1157
junov@google.comf93e7172011-03-31 21:26:24 +00001158 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001159 stage.fOptFlags = 0;
1160 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1161 stage.fInConfigFlags = 0;
1162 stage.fFetchMode = (StageDesc::FetchMode) 0;
1163 stage.fKernelWidth = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001164 stage.fCustomStageKey = 0;
1165 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +00001166 }
1167 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001168
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001169 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001170 // The shader generator assumes that color channels are bytes
1171 // when rounding.
1172 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
1173 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
1174 desc.fOutputConfig =
1175 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
1176 } else {
1177 desc.fOutputConfig =
1178 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
1179 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001180 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001181 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001182 }
1183
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001184 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001185
1186 // currently the experimental GS will only work with triangle prims
1187 // (and it doesn't do anything other than pass through values from
1188 // the VS to the FS anyway).
1189#if 0 && GR_GL_EXPERIMENTAL_GS
1190 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1191#endif
1192
bsalomon@google.coma3108262011-10-10 14:08:47 +00001193 // we want to avoid generating programs with different "first cov stage"
1194 // values when they would compute the same result.
1195 // We set field in the desc to kNumStages when either there are no
1196 // coverage stages or the distinction between coverage and color is
1197 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001198 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +00001199 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001200 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001201 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001202 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +00001203 }
1204
1205 // other coverage inputs
1206 if (!hasCoverage) {
1207 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001208 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +00001209 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1210 }
1211
1212 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001213 // color filter is applied between color/coverage computation
1214 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001215 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001216 }
1217
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001218 if (this->getCaps().fDualSourceBlendingSupport &&
1219 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1220 kCoverageAsAlpha_BlendOptFlag))) {
1221 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001222 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001223 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001224 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001225 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001226 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1227 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001228 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001229 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001230 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001231 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1232 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001233 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001234 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001235 }
1236 }
1237 }
junov@google.comf93e7172011-03-31 21:26:24 +00001238}