blob: 802574ceb10ec28bd0069df029ac1063dc52eefb [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 =
bsalomon@google.com16fd21b2012-05-21 21:18:13 +0000305 customStages[s]->getFactory().glStageKey(customStages[s]);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000306 }
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;
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000385 fHWConstAttribColor = GrColor_ILLEGAL;
386 fHWConstAttribCoverage = GrColor_ILLEGAL;
junov@google.comf93e7172011-03-31 21:26:24 +0000387}
388
389void GrGpuGLShaders::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000390 const GrMatrix& vm = this->getDrawState().getViewMatrix();
bsalomon@google.com341767c2012-05-11 20:47:39 +0000391 if (!fProgramData->fViewMatrix.cheapEqualTo(vm)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000392
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000393 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
394 GrAssert(NULL != rt);
395 GrMatrix m;
396 m.setAll(
397 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
398 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
399 0, 0, GrMatrix::I()[8]);
400 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000401
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000402 // ES doesn't allow you to pass true to the transpose param,
403 // so do our own transpose
404 GrGLfloat mt[] = {
405 GrScalarToFloat(m[GrMatrix::kMScaleX]),
406 GrScalarToFloat(m[GrMatrix::kMSkewY]),
407 GrScalarToFloat(m[GrMatrix::kMPersp0]),
408 GrScalarToFloat(m[GrMatrix::kMSkewX]),
409 GrScalarToFloat(m[GrMatrix::kMScaleY]),
410 GrScalarToFloat(m[GrMatrix::kMPersp1]),
411 GrScalarToFloat(m[GrMatrix::kMTransX]),
412 GrScalarToFloat(m[GrMatrix::kMTransY]),
413 GrScalarToFloat(m[GrMatrix::kMPersp2])
414 };
415
bsalomon@google.com341767c2012-05-11 20:47:39 +0000416 GrAssert(GrGLProgram::kUnusedUniform !=
417 fProgramData->fUniLocations.fViewMatrixUni);
418 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
419 1, false, mt));
420 fProgramData->fViewMatrix = vm;
bsalomon@google.com91961302011-05-09 18:39:58 +0000421 }
junov@google.comf93e7172011-03-31 21:26:24 +0000422}
423
junov@google.com6acc9b32011-05-16 18:32:07 +0000424void GrGpuGLShaders::flushTextureDomain(int s) {
425 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000426 const GrDrawState& drawState = this->getDrawState();
junov@google.com6acc9b32011-05-16 18:32:07 +0000427 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000428 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
junov@google.com6acc9b32011-05-16 18:32:07 +0000429
twiz@google.com76b82742011-06-02 20:30:02 +0000430 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
431 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000432
junov@google.com2f839402011-05-24 15:13:01 +0000433 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000434
junov@google.com2f839402011-05-24 15:13:01 +0000435 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000436 GrScalarToFloat(texDom.left()),
437 GrScalarToFloat(texDom.top()),
438 GrScalarToFloat(texDom.right()),
439 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000440 };
441
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000442 const GrGLTexture* texture =
443 static_cast<const GrGLTexture*>(drawState.getTexture(s));
junov@google.com2f839402011-05-24 15:13:01 +0000444 GrGLTexture::Orientation orientation = texture->orientation();
445
446 // vertical flip if necessary
447 if (GrGLTexture::kBottomUp_Orientation == orientation) {
448 values[1] = 1.0f - values[1];
449 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000450 // The top and bottom were just flipped, so correct the ordering
451 // of elements so that values = (l, t, r, b).
452 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000453 }
454
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000455 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000456 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000457 }
458}
459
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000460void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000461 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000462 const GrDrawState& drawState = this->getDrawState();
463 const GrGLTexture* texture =
464 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000465 if (NULL != texture) {
bsalomon@google.com341767c2012-05-11 20:47:39 +0000466 const GrMatrix& hwMatrix = fProgramData->fTextureMatrices[s];
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000467 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000468 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000469 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000470 !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000471
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000472 GrMatrix m = samplerMatrix;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000473 GrSamplerState::SampleMode mode =
474 drawState.getSampler(s).getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000475 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000476
bsalomon@google.com91961302011-05-09 18:39:58 +0000477 // ES doesn't allow you to pass true to the transpose param,
478 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000479 GrGLfloat mt[] = {
480 GrScalarToFloat(m[GrMatrix::kMScaleX]),
481 GrScalarToFloat(m[GrMatrix::kMSkewY]),
482 GrScalarToFloat(m[GrMatrix::kMPersp0]),
483 GrScalarToFloat(m[GrMatrix::kMSkewX]),
484 GrScalarToFloat(m[GrMatrix::kMScaleY]),
485 GrScalarToFloat(m[GrMatrix::kMPersp1]),
486 GrScalarToFloat(m[GrMatrix::kMTransX]),
487 GrScalarToFloat(m[GrMatrix::kMTransY]),
488 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000489 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000490
bsalomon@google.com341767c2012-05-11 20:47:39 +0000491 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
492 fProgramData->fTextureMatrices[s] = samplerMatrix;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000493 }
494 }
junov@google.comf93e7172011-03-31 21:26:24 +0000495}
496
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000497void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000498
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000499 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000500 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.com91961302011-05-09 18:39:58 +0000501 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000502 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
503 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
504 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000505
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000506 GrScalar centerX1 = sampler.getRadial2CenterX1();
507 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000508
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000509 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000510
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000511 // when were in the degenerate (linear) case the second
512 // value will be INF but the program doesn't read it. (We
513 // use the same 6 uniforms even though we don't need them
514 // all in the linear case just to keep the code complexity
515 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000516 float values[6] = {
517 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000518 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000519 GrScalarToFloat(centerX1),
520 GrScalarToFloat(radius0),
521 GrScalarToFloat(GrMul(radius0, radius0)),
522 sampler.isRadial2PosRoot() ? 1.f : -1.f
523 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000524 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000525 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
526 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
527 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
528 }
529}
530
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000531void GrGpuGLShaders::flushConvolution(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000532 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000533 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
534 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000535 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
536 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000537 }
538 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
539 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000540 const GrGLTexture* texture =
541 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
542 float imageIncrement[2] = { 0 };
543 switch (sampler.getFilterDirection()) {
544 case GrSamplerState::kX_FilterDirection:
545 imageIncrement[0] = 1.0f / texture->width();
546 break;
547 case GrSamplerState::kY_FilterDirection:
548 imageIncrement[1] = 1.0f / texture->height();
549 break;
550 default:
551 GrCrash("Unknown filter direction.");
552 }
553 GL_CALL(Uniform2fv(imageIncrementUni, 1, imageIncrement));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000554 }
555}
556
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000557void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000558 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000559 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000560 const GrGLTexture* texture =
561 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000562 if (texture->width() != fProgramData->fTextureWidth[s] ||
563 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000564
bsalomon@google.com99621082011-11-15 16:47:16 +0000565 float texelSize[] = {1.f / texture->width(),
566 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000567 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000568 fProgramData->fTextureWidth[s] = texture->width();
569 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000570 }
571 }
junov@google.comf93e7172011-03-31 21:26:24 +0000572}
573
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000574void GrGpuGLShaders::flushColorMatrix() {
575 const ProgramDesc& desc = fCurrentProgram.getDesc();
576 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
577 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
578 if (GrGLProgram::kUnusedUniform != matrixUni
579 && GrGLProgram::kUnusedUniform != vecUni) {
580 const float* m = this->getDrawState().getColorMatrix();
581 GrGLfloat mt[] = {
582 m[0], m[5], m[10], m[15],
583 m[1], m[6], m[11], m[16],
584 m[2], m[7], m[12], m[17],
585 m[3], m[8], m[13], m[18],
586 };
587 static float scale = 1.0f / 255.0f;
588 GrGLfloat vec[] = {
589 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
590 };
591 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
592 GL_CALL(Uniform4fv(vecUni, 1, vec));
593 }
594}
595
Scroggo01b87ec2011-05-11 18:05:38 +0000596static const float ONE_OVER_255 = 1.f / 255.f;
597
598#define GR_COLOR_TO_VEC4(color) {\
599 GrColorUnpackR(color) * ONE_OVER_255,\
600 GrColorUnpackG(color) * ONE_OVER_255,\
601 GrColorUnpackB(color) * ONE_OVER_255,\
602 GrColorUnpackA(color) * ONE_OVER_255 \
603}
604
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000605void GrGpuGLShaders::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000606 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000607 const GrDrawState& drawState = this->getDrawState();
608
bsalomon@google.come79c8152012-03-29 19:07:12 +0000609 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000610 // color will be specified per-vertex as an attribute
611 // invalidate the const vertex attrib color
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000612 fHWConstAttribColor = GrColor_ILLEGAL;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000613 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000614 switch (desc.fColorInput) {
615 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000616 if (fHWConstAttribColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000617 // OpenGL ES only supports the float varieties of
618 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000619 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000620 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
621 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000622 fHWConstAttribColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000623 }
624 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000625 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000626 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000627 // OpenGL ES doesn't support unsigned byte varieties of
628 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000629 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000630 GrAssert(GrGLProgram::kUnusedUniform !=
631 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000632 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
633 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000634 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000635 }
636 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000637 case ProgramDesc::kSolidWhite_ColorInput:
638 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000639 break;
640 default:
641 GrCrash("Unknown color type.");
642 }
643 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000644 if (fProgramData->fUniLocations.fColorFilterUni
645 != GrGLProgram::kUnusedUniform
646 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000647 != drawState.getColorFilterColor()) {
648 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000649 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000650 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000651 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000652}
653
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000654void GrGpuGLShaders::flushCoverage(GrColor coverage) {
655 const ProgramDesc& desc = fCurrentProgram.getDesc();
656 const GrDrawState& drawState = this->getDrawState();
657
658
bsalomon@google.come79c8152012-03-29 19:07:12 +0000659 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000660 // coverage will be specified per-vertex as an attribute
661 // invalidate the const vertex attrib coverage
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000662 fHWConstAttribCoverage = GrColor_ILLEGAL;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000663 } else {
664 switch (desc.fCoverageInput) {
665 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com255fa162012-05-21 21:44:59 +0000666 if (fHWConstAttribCoverage != coverage) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000667 // OpenGL ES only supports the float varieties of
668 // glVertexAttrib
669 float c[] = GR_COLOR_TO_VEC4(coverage);
670 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
671 c));
bsalomon@google.com8d49d932012-05-21 21:40:12 +0000672 fHWConstAttribCoverage = coverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000673 }
674 break;
675 case ProgramDesc::kUniform_ColorInput:
676 if (fProgramData->fCoverage != coverage) {
677 // OpenGL ES doesn't support unsigned byte varieties of
678 // glUniform
679 float c[] = GR_COLOR_TO_VEC4(coverage);
680 GrAssert(GrGLProgram::kUnusedUniform !=
681 fProgramData->fUniLocations.fCoverageUni);
682 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
683 1, c));
684 fProgramData->fCoverage = coverage;
685 }
686 break;
687 case ProgramDesc::kSolidWhite_ColorInput:
688 case ProgramDesc::kTransBlack_ColorInput:
689 break;
690 default:
691 GrCrash("Unknown coverage type.");
692 }
693 }
694}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000695
junov@google.comf93e7172011-03-31 21:26:24 +0000696bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
697 if (!flushGLStateCommon(type)) {
698 return false;
699 }
700
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000701 const GrDrawState& drawState = this->getDrawState();
702
junov@google.comf93e7172011-03-31 21:26:24 +0000703 if (fDirtyFlags.fRenderTargetChanged) {
junov@google.comf93e7172011-03-31 21:26:24 +0000704 // we assume all shader matrices may be wrong after viewport changes
705 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000706 }
707
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000708 GrBlendCoeff srcCoeff;
709 GrBlendCoeff dstCoeff;
710 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
711 if (kSkipDraw_BlendOptFlag & blendOpts) {
712 return false;
713 }
714
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000715 GrCustomStage* customStages [GrDrawState::kNumStages];
716 this->buildProgram(type, blendOpts, dstCoeff, customStages);
717 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
718 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000719 if (NULL == fProgramData) {
720 GrAssert(!"Failed to create program!");
721 return false;
722 }
junov@google.comf93e7172011-03-31 21:26:24 +0000723
724 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000725 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000726 fHWProgramID = fProgramData->fProgramID;
727 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000728 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
729 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000730
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000731 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000732 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000733 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
734 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000735 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000736 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
737 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000738 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000739 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000740 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000741 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000742 }
743 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000744 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000745
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000746 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000747
tomhudson@google.com93813632011-10-27 20:21:16 +0000748 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000749 if (this->isStageEnabled(s)) {
750 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000751
bsalomon@google.com40d92932011-12-13 18:40:47 +0000752 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000753
bsalomon@google.com40d92932011-12-13 18:40:47 +0000754 this->flushConvolution(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000755
bsalomon@google.com40d92932011-12-13 18:40:47 +0000756 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000757
bsalomon@google.com40d92932011-12-13 18:40:47 +0000758 this->flushTextureDomain(s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000759
760 if (NULL != fProgramData->fCustomStage[s]) {
761 const GrSamplerState& sampler =
762 this->getDrawState().getSampler(s);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000763 const GrGLTexture* texture =
764 static_cast<const GrGLTexture*>(
765 this->getDrawState().getTexture(s));
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000766 fProgramData->fCustomStage[s]->setData(
tomhudson@google.com6a820b62012-05-24 15:10:14 +0000767 this->glInterface(), *texture,
768 sampler.getCustomStage(), s);
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
bsalomon@google.com2717d562012-05-07 19:10:52 +0000777#if GR_TEXT_SCALAR_IS_USHORT
778 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
779 #define TEXT_COORDS_ARE_NORMALIZED 1
780#elif GR_TEXT_SCALAR_IS_FLOAT
781 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
782 #define TEXT_COORDS_ARE_NORMALIZED 0
783#elif GR_TEXT_SCALAR_IS_FIXED
784 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
785 #define TEXT_COORDS_ARE_NORMALIZED 0
786#else
787 #error "unknown GR_TEXT_SCALAR type"
788#endif
789
junov@google.comf93e7172011-03-31 21:26:24 +0000790void GrGpuGLShaders::setupGeometry(int* startVertex,
791 int* startIndex,
792 int vertexCount,
793 int indexCount) {
794
795 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000796 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000797 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000798 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000799
bsalomon@google.come79c8152012-03-29 19:07:12 +0000800 GrVertexLayout currLayout = this->getVertexLayout();
801
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000802 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000803 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000804 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000805 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000806 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000807 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000808 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000809 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000810 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000811 int oldEdgeOffset;
812
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000813 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
814 fHWGeometryState.fVertexLayout,
815 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000816 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000817 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000818 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000819 bool indexed = NULL != startIndex;
820
821 int extraVertexOffset;
822 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000823 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000824
825 GrGLenum scalarType;
826 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000827 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000828 scalarType = TEXT_COORDS_GL_TYPE;
829 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000830 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000831 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
832 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000833 texCoordNorm = false;
834 }
835
836 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
837 *startVertex = 0;
838 if (indexed) {
839 *startIndex += extraIndexOffset;
840 }
841
842 // all the Pointers must be set if any of these are true
843 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
844 vertexOffset != fHWGeometryState.fVertexOffset ||
845 newStride != oldStride;
846
847 // position and tex coord offsets change if above conditions are true
848 // or the type/normalization changed based on text vs nontext type coords.
849 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000850 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000851 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000852 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000853
854 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000855 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000856 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000857 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000858 fHWGeometryState.fVertexOffset = vertexOffset;
859 }
860
tomhudson@google.com93813632011-10-27 20:21:16 +0000861 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000862 if (newTexCoordOffsets[t] > 0) {
863 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000864 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000865 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000866 GL_CALL(EnableVertexAttribArray(idx));
867 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000868 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000869 } else if (posAndTexChange ||
870 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000871 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000872 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000873 }
874 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000875 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000876 }
877 }
878
879 if (newColorOffset > 0) {
880 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000881 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000882 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000883 GL_CALL(EnableVertexAttribArray(idx));
884 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000885 true, newStride, colorOffset));
886 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000887 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000888 true, newStride, colorOffset));
889 }
890 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000891 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000892 }
893
bsalomon@google.coma3108262011-10-10 14:08:47 +0000894 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000895 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000896 int idx = GrGLProgram::CoverageAttributeIdx();
897 if (oldCoverageOffset <= 0) {
898 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000899 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000900 true, newStride, coverageOffset));
901 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
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 }
905 } else if (oldCoverageOffset > 0) {
906 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
907 }
908
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000909 if (newEdgeOffset > 0) {
910 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
911 int idx = GrGLProgram::EdgeAttributeIdx();
912 if (oldEdgeOffset <= 0) {
913 GL_CALL(EnableVertexAttribArray(idx));
914 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
915 false, newStride, edgeOffset));
916 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
917 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
918 false, newStride, edgeOffset));
919 }
920 } else if (oldEdgeOffset > 0) {
921 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
922 }
923
bsalomon@google.come79c8152012-03-29 19:07:12 +0000924 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000925 fHWGeometryState.fArrayPtrsDirty = false;
926}
927
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000928namespace {
929
930void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
931 const GrSamplerState& sampler,
932 GrCustomStage** customStages,
933 GrGLProgram* program, int index) {
934 GrCustomStage* customStage = sampler.getCustomStage();
935 if (customStage) {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000936 const GrProgramStageFactory& factory = customStage->getFactory();
bsalomon@google.com16fd21b2012-05-21 21:18:13 +0000937 stage->fCustomStageKey = factory.glStageKey(customStage);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000938 customStages[index] = customStage;
939 } else {
940 stage->fCustomStageKey = 0;
941 customStages[index] = NULL;
942 }
943}
944
945}
946
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000947void GrGpuGLShaders::buildProgram(GrPrimitiveType type,
948 BlendOptFlags blendOpts,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000949 GrBlendCoeff dstCoeff,
950 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000951 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000952 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +0000953
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000954 // This should already have been caught
955 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
956
957 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
958
959 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
960 kEmitCoverage_BlendOptFlag));
961
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000962 // The descriptor is used as a cache key. Thus when a field of the
963 // descriptor will not affect program generation (because of the vertex
964 // layout in use or other descriptor field settings) it should be set
965 // to a canonical value to avoid duplicate programs with different keys.
966
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000967 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +0000968 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000969
970 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
971
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000972 bool requiresAttributeColors =
973 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000974 bool requiresAttributeCoverage =
975 !skipCoverage && SkToBool(desc.fVertexLayout &
976 kCoverage_VertexLayoutBit);
977
978 // fColorInput/fCoverageInput records how colors are specified for the.
979 // program. So we strip the bits from the layout to avoid false negatives
980 // when searching for an existing program in the cache.
981 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000982
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000983 desc.fColorFilterXfermode = skipColor ?
984 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000985 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000986
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000987 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
988
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000989 // no reason to do edge aa or look at per-vertex coverage if coverage is
990 // ignored
991 if (skipCoverage) {
992 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
993 kCoverage_VertexLayoutBit);
994 }
995
996 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
997 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
998 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000999 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001000 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001001 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001002 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001003 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001004 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001005 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001006 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001007 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001008 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001009
1010 bool covIsSolidWhite = !requiresAttributeCoverage &&
1011 0xffffffff == drawState.getCoverage();
1012
1013 if (skipCoverage) {
1014 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
1015 } else if (covIsSolidWhite) {
1016 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
1017 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
1018 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
1019 } else {
1020 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
1021 }
junov@google.comf93e7172011-03-31 21:26:24 +00001022
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001023 int lastEnabledStage = -1;
1024
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001025 if (!skipCoverage && (desc.fVertexLayout &
1026 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001027 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001028 } else {
1029 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +00001030 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001031 }
1032
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001033 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001034 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001035
1036 stage.fOptFlags = 0;
1037 stage.setEnabled(this->isStageEnabled(s));
1038
1039 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
1040 skipCoverage;
1041
1042 if (!skip && stage.isEnabled()) {
1043 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001044 const GrGLTexture* texture =
1045 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001046 GrAssert(NULL != texture);
1047 const GrSamplerState& sampler = drawState.getSampler(s);
1048 // we matrix to invert when orientation is TopDown, so make sure
1049 // we aren't in that case before flagging as identity.
1050 if (TextureMatrixIsIdentity(texture, sampler)) {
1051 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
1052 } else if (!sampler.getMatrix().hasPerspective()) {
1053 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
1054 }
1055 switch (sampler.getSampleMode()) {
1056 case GrSamplerState::kNormal_SampleMode:
1057 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
1058 break;
1059 case GrSamplerState::kRadial_SampleMode:
1060 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
1061 break;
1062 case GrSamplerState::kRadial2_SampleMode:
1063 if (sampler.radial2IsDegenerate()) {
1064 stage.fCoordMapping =
1065 StageDesc::kRadial2GradientDegenerate_CoordMapping;
1066 } else {
1067 stage.fCoordMapping =
1068 StageDesc::kRadial2Gradient_CoordMapping;
1069 }
1070 break;
1071 case GrSamplerState::kSweep_SampleMode:
1072 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
1073 break;
1074 default:
1075 GrCrash("Unexpected sample mode!");
1076 break;
1077 }
1078
1079 switch (sampler.getFilter()) {
1080 // these both can use a regular texture2D()
1081 case GrSamplerState::kNearest_Filter:
1082 case GrSamplerState::kBilinear_Filter:
1083 stage.fFetchMode = StageDesc::kSingle_FetchMode;
1084 break;
1085 // performs 4 texture2D()s
1086 case GrSamplerState::k4x4Downsample_Filter:
1087 stage.fFetchMode = StageDesc::k2x2_FetchMode;
1088 break;
1089 // performs fKernelWidth texture2D()s
1090 case GrSamplerState::kConvolution_Filter:
1091 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
1092 break;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001093 case GrSamplerState::kDilate_Filter:
1094 stage.fFetchMode = StageDesc::kDilate_FetchMode;
1095 break;
1096 case GrSamplerState::kErode_Filter:
1097 stage.fFetchMode = StageDesc::kErode_FetchMode;
1098 break;
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001099 default:
1100 GrCrash("Unexpected filter!");
1101 break;
1102 }
1103
1104 if (sampler.hasTextureDomain()) {
1105 GrAssert(GrSamplerState::kClamp_WrapMode ==
1106 sampler.getWrapX() &&
1107 GrSamplerState::kClamp_WrapMode ==
1108 sampler.getWrapY());
1109 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
1110 }
1111
1112 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001113 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001114 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1115 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +00001116 // the shader must smear the single channel after
1117 // reading the texture
1118 if (this->glCaps().textureRedSupport()) {
1119 // we can use R8 textures so use kSmearRed
1120 stage.fInConfigFlags |=
1121 StageDesc::kSmearRed_InConfigFlag;
1122 } else {
1123 // we can use A8 textures so use kSmearAlpha
1124 stage.fInConfigFlags |=
1125 StageDesc::kSmearAlpha_InConfigFlag;
1126 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001127 } else if (sampler.swapsRAndB()) {
1128 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
1129 }
1130 }
1131 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001132 // The shader generator assumes that color channels are bytes
1133 // when rounding.
1134 GrAssert(4 == GrBytesPerPixel(texture->config()));
1135 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
1136 fUnpremulConversion) {
1137 stage.fInConfigFlags |=
1138 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
1139 } else {
1140 stage.fInConfigFlags |=
1141 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
1142 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001143 }
1144
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001145 if (sampler.getFilter() == GrSamplerState::kDilate_Filter ||
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001146 sampler.getFilter() == GrSamplerState::kErode_Filter) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001147 stage.fKernelWidth = sampler.getKernelWidth();
1148 } else {
1149 stage.fKernelWidth = 0;
1150 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001151
1152 setup_custom_stage(&stage, sampler, customStages,
1153 &fCurrentProgram, s);
1154
junov@google.comf93e7172011-03-31 21:26:24 +00001155 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001156 stage.fOptFlags = 0;
1157 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1158 stage.fInConfigFlags = 0;
1159 stage.fFetchMode = (StageDesc::FetchMode) 0;
1160 stage.fKernelWidth = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001161 stage.fCustomStageKey = 0;
1162 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +00001163 }
1164 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001165
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001166 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001167 // The shader generator assumes that color channels are bytes
1168 // when rounding.
1169 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
1170 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
1171 desc.fOutputConfig =
1172 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
1173 } else {
1174 desc.fOutputConfig =
1175 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
1176 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001177 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001178 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001179 }
1180
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001181 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001182
1183 // currently the experimental GS will only work with triangle prims
1184 // (and it doesn't do anything other than pass through values from
1185 // the VS to the FS anyway).
1186#if 0 && GR_GL_EXPERIMENTAL_GS
1187 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1188#endif
1189
bsalomon@google.coma3108262011-10-10 14:08:47 +00001190 // we want to avoid generating programs with different "first cov stage"
1191 // values when they would compute the same result.
1192 // We set field in the desc to kNumStages when either there are no
1193 // coverage stages or the distinction between coverage and color is
1194 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001195 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +00001196 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001197 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001198 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001199 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +00001200 }
1201
1202 // other coverage inputs
1203 if (!hasCoverage) {
1204 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001205 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +00001206 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1207 }
1208
1209 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001210 // color filter is applied between color/coverage computation
1211 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001212 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001213 }
1214
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001215 if (this->getCaps().fDualSourceBlendingSupport &&
1216 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1217 kCoverageAsAlpha_BlendOptFlag))) {
1218 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001219 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001220 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001221 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001222 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001223 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1224 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001225 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001226 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001227 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001228 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1229 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001230 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001231 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001232 }
1233 }
1234 }
junov@google.comf93e7172011-03-31 21:26:24 +00001235}