blob: 49aa87c3e64a49d89c0b4a59020118efab2f0359 [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
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00009#include "GrBinHashKey.h"
10#include "effects/GrConvolutionEffect.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000011#include "GrCustomStage.h"
junov@google.comf93e7172011-03-31 21:26:24 +000012#include "GrGLProgram.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000013#include "GrGLProgramStage.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000014#include "GrGLSL.h"
junov@google.comf93e7172011-03-31 21:26:24 +000015#include "GrGpuGLShaders.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.com8f9cbd62011-12-09 15:55:34 +0000359const GrMatrix& GrGpuGLShaders::getHWViewMatrix() {
360 GrAssert(fProgramData);
361
362 if (GrGLProgram::kSetAsAttribute ==
363 fProgramData->fUniLocations.fViewMatrixUni) {
364 return fHWDrawState.getViewMatrix();
365 } else {
366 return fProgramData->fViewMatrix;
367 }
368}
369
370void GrGpuGLShaders::recordHWViewMatrix(const GrMatrix& matrix) {
371 GrAssert(fProgramData);
372 if (GrGLProgram::kSetAsAttribute ==
373 fProgramData->fUniLocations.fViewMatrixUni) {
374 fHWDrawState.setViewMatrix(matrix);
375 } else {
376 fProgramData->fViewMatrix = matrix;
377 }
378}
379
junov@google.comf93e7172011-03-31 21:26:24 +0000380const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000381 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000382
383 if (GrGLProgram::kSetAsAttribute ==
384 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000385 return fHWDrawState.getSampler(stage).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000386 } else {
387 return fProgramData->fTextureMatrices[stage];
388 }
junov@google.comf93e7172011-03-31 21:26:24 +0000389}
390
391void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000392 GrAssert(fProgramData);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000393 if (GrGLProgram::kSetAsAttribute ==
bsalomon@google.com91961302011-05-09 18:39:58 +0000394 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000395 *fHWDrawState.sampler(stage)->matrix() = matrix;
bsalomon@google.com91961302011-05-09 18:39:58 +0000396 } else {
397 fProgramData->fTextureMatrices[stage] = matrix;
398 }
junov@google.comf93e7172011-03-31 21:26:24 +0000399}
400
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000401void GrGpuGLShaders::onResetContext() {
402 INHERITED::onResetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000403
junov@google.comf93e7172011-03-31 21:26:24 +0000404 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000405
406 // Third party GL code may have left vertex attributes enabled. Some GL
407 // implementations (osmesa) may read vetex attributes that are not required
408 // by the current shader. Therefore, we have to ensure that only the
409 // attributes we require for the current draw are enabled or we may cause an
410 // invalid read.
411
412 // Disable all vertex layout bits so that next flush will assume all
413 // optional vertex attributes are disabled.
414 fHWGeometryState.fVertexLayout = 0;
415
416 // We always use the this attribute and assume it is always enabled.
417 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
418 GL_CALL(EnableVertexAttribArray(posAttrIdx));
419 // Disable all other vertex attributes.
420 for (int va = 0; va < fMaxVertexAttribs; ++va) {
421 if (va != posAttrIdx) {
422 GL_CALL(DisableVertexAttribArray(va));
423 }
junov@google.comf93e7172011-03-31 21:26:24 +0000424 }
junov@google.comf93e7172011-03-31 21:26:24 +0000425
426 fHWProgramID = 0;
427}
428
429void GrGpuGLShaders::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000430 const GrMatrix& vm = this->getDrawState().getViewMatrix();
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000431 if (!GrGpuGLShaders::getHWViewMatrix().cheapEqualTo(vm)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000432
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000433 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
434 GrAssert(NULL != rt);
435 GrMatrix m;
436 m.setAll(
437 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
438 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
439 0, 0, GrMatrix::I()[8]);
440 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000441
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000442 // ES doesn't allow you to pass true to the transpose param,
443 // so do our own transpose
444 GrGLfloat mt[] = {
445 GrScalarToFloat(m[GrMatrix::kMScaleX]),
446 GrScalarToFloat(m[GrMatrix::kMSkewY]),
447 GrScalarToFloat(m[GrMatrix::kMPersp0]),
448 GrScalarToFloat(m[GrMatrix::kMSkewX]),
449 GrScalarToFloat(m[GrMatrix::kMScaleY]),
450 GrScalarToFloat(m[GrMatrix::kMPersp1]),
451 GrScalarToFloat(m[GrMatrix::kMTransX]),
452 GrScalarToFloat(m[GrMatrix::kMTransY]),
453 GrScalarToFloat(m[GrMatrix::kMPersp2])
454 };
455
456 if (GrGLProgram::kSetAsAttribute ==
457 fProgramData->fUniLocations.fViewMatrixUni) {
458 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
459 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
460 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
461 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
462 } else {
463 GrAssert(GrGLProgram::kUnusedUniform !=
464 fProgramData->fUniLocations.fViewMatrixUni);
465 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
466 1, false, mt));
467 }
468 this->recordHWViewMatrix(vm);
bsalomon@google.com91961302011-05-09 18:39:58 +0000469 }
junov@google.comf93e7172011-03-31 21:26:24 +0000470}
471
junov@google.com6acc9b32011-05-16 18:32:07 +0000472void GrGpuGLShaders::flushTextureDomain(int s) {
473 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000474 const GrDrawState& drawState = this->getDrawState();
junov@google.com6acc9b32011-05-16 18:32:07 +0000475 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000476 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
junov@google.com6acc9b32011-05-16 18:32:07 +0000477
twiz@google.com76b82742011-06-02 20:30:02 +0000478 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
479 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000480
junov@google.com2f839402011-05-24 15:13:01 +0000481 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000482
junov@google.com2f839402011-05-24 15:13:01 +0000483 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000484 GrScalarToFloat(texDom.left()),
485 GrScalarToFloat(texDom.top()),
486 GrScalarToFloat(texDom.right()),
487 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000488 };
489
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000490 const GrGLTexture* texture =
491 static_cast<const GrGLTexture*>(drawState.getTexture(s));
junov@google.com2f839402011-05-24 15:13:01 +0000492 GrGLTexture::Orientation orientation = texture->orientation();
493
494 // vertical flip if necessary
495 if (GrGLTexture::kBottomUp_Orientation == orientation) {
496 values[1] = 1.0f - values[1];
497 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000498 // The top and bottom were just flipped, so correct the ordering
499 // of elements so that values = (l, t, r, b).
500 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000501 }
502
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000503 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000504 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000505 }
506}
507
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000508void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000509 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000510 const GrDrawState& drawState = this->getDrawState();
511 const GrGLTexture* texture =
512 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000513 if (NULL != texture) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000514 const GrMatrix& hwMatrix = this->getHWSamplerMatrix(s);
515 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000516 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000517 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000518 !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000519
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000520 GrMatrix m = samplerMatrix;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000521 GrSamplerState::SampleMode mode =
522 drawState.getSampler(s).getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000523 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000524
bsalomon@google.com91961302011-05-09 18:39:58 +0000525 // ES doesn't allow you to pass true to the transpose param,
526 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000527 GrGLfloat mt[] = {
528 GrScalarToFloat(m[GrMatrix::kMScaleX]),
529 GrScalarToFloat(m[GrMatrix::kMSkewY]),
530 GrScalarToFloat(m[GrMatrix::kMPersp0]),
531 GrScalarToFloat(m[GrMatrix::kMSkewX]),
532 GrScalarToFloat(m[GrMatrix::kMScaleY]),
533 GrScalarToFloat(m[GrMatrix::kMPersp1]),
534 GrScalarToFloat(m[GrMatrix::kMTransX]),
535 GrScalarToFloat(m[GrMatrix::kMTransY]),
536 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000537 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000538
bsalomon@google.com91961302011-05-09 18:39:58 +0000539 if (GrGLProgram::kSetAsAttribute ==
540 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
541 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000542 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
543 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
544 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000545 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000546 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000547 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000548 this->recordHWSamplerMatrix(s, drawState.getSampler(s).getMatrix());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000549 }
550 }
junov@google.comf93e7172011-03-31 21:26:24 +0000551}
552
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000553void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000554
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000555 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000556 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.com91961302011-05-09 18:39:58 +0000557 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000558 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
559 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
560 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000561
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000562 GrScalar centerX1 = sampler.getRadial2CenterX1();
563 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000564
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000565 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000566
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000567 // when were in the degenerate (linear) case the second
568 // value will be INF but the program doesn't read it. (We
569 // use the same 6 uniforms even though we don't need them
570 // all in the linear case just to keep the code complexity
571 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000572 float values[6] = {
573 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000574 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000575 GrScalarToFloat(centerX1),
576 GrScalarToFloat(radius0),
577 GrScalarToFloat(GrMul(radius0, radius0)),
578 sampler.isRadial2PosRoot() ? 1.f : -1.f
579 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000580 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000581 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
582 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
583 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
584 }
585}
586
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000587void GrGpuGLShaders::flushConvolution(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000588 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000589 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
590 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000591 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
592 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000593 }
594 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
595 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000596 const GrGLTexture* texture =
597 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
598 float imageIncrement[2] = { 0 };
599 switch (sampler.getFilterDirection()) {
600 case GrSamplerState::kX_FilterDirection:
601 imageIncrement[0] = 1.0f / texture->width();
602 break;
603 case GrSamplerState::kY_FilterDirection:
604 imageIncrement[1] = 1.0f / texture->height();
605 break;
606 default:
607 GrCrash("Unknown filter direction.");
608 }
609 GL_CALL(Uniform2fv(imageIncrementUni, 1, imageIncrement));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000610 }
611}
612
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000613void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000614 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000615 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000616 const GrGLTexture* texture =
617 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000618 if (texture->width() != fProgramData->fTextureWidth[s] ||
619 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000620
bsalomon@google.com99621082011-11-15 16:47:16 +0000621 float texelSize[] = {1.f / texture->width(),
622 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000623 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000624 fProgramData->fTextureWidth[s] = texture->width();
625 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000626 }
627 }
junov@google.comf93e7172011-03-31 21:26:24 +0000628}
629
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000630void GrGpuGLShaders::flushColorMatrix() {
631 const ProgramDesc& desc = fCurrentProgram.getDesc();
632 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
633 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
634 if (GrGLProgram::kUnusedUniform != matrixUni
635 && GrGLProgram::kUnusedUniform != vecUni) {
636 const float* m = this->getDrawState().getColorMatrix();
637 GrGLfloat mt[] = {
638 m[0], m[5], m[10], m[15],
639 m[1], m[6], m[11], m[16],
640 m[2], m[7], m[12], m[17],
641 m[3], m[8], m[13], m[18],
642 };
643 static float scale = 1.0f / 255.0f;
644 GrGLfloat vec[] = {
645 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
646 };
647 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
648 GL_CALL(Uniform4fv(vecUni, 1, vec));
649 }
650}
651
Scroggo01b87ec2011-05-11 18:05:38 +0000652static const float ONE_OVER_255 = 1.f / 255.f;
653
654#define GR_COLOR_TO_VEC4(color) {\
655 GrColorUnpackR(color) * ONE_OVER_255,\
656 GrColorUnpackG(color) * ONE_OVER_255,\
657 GrColorUnpackB(color) * ONE_OVER_255,\
658 GrColorUnpackA(color) * ONE_OVER_255 \
659}
660
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000661void GrGpuGLShaders::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000662 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000663 const GrDrawState& drawState = this->getDrawState();
664
bsalomon@google.come79c8152012-03-29 19:07:12 +0000665 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000666 // color will be specified per-vertex as an attribute
667 // invalidate the const vertex attrib color
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000668 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000669 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000670 switch (desc.fColorInput) {
671 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000672 if (fHWDrawState.getColor() != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000673 // OpenGL ES only supports the float varieties of
674 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000675 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000676 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
677 c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000678 fHWDrawState.setColor(color);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000679 }
680 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000681 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000682 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000683 // OpenGL ES doesn't support unsigned byte varieties of
684 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000685 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000686 GrAssert(GrGLProgram::kUnusedUniform !=
687 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000688 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
689 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000690 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000691 }
692 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000693 case ProgramDesc::kSolidWhite_ColorInput:
694 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000695 break;
696 default:
697 GrCrash("Unknown color type.");
698 }
699 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000700 if (fProgramData->fUniLocations.fColorFilterUni
701 != GrGLProgram::kUnusedUniform
702 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000703 != drawState.getColorFilterColor()) {
704 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000705 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000706 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000707 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000708}
709
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000710void GrGpuGLShaders::flushCoverage(GrColor coverage) {
711 const ProgramDesc& desc = fCurrentProgram.getDesc();
712 const GrDrawState& drawState = this->getDrawState();
713
714
bsalomon@google.come79c8152012-03-29 19:07:12 +0000715 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000716 // coverage will be specified per-vertex as an attribute
717 // invalidate the const vertex attrib coverage
718 fHWDrawState.setCoverage4(GrColor_ILLEGAL);
719 } else {
720 switch (desc.fCoverageInput) {
721 case ProgramDesc::kAttribute_ColorInput:
722 if (fHWDrawState.getCoverage() != coverage) {
723 // OpenGL ES only supports the float varieties of
724 // glVertexAttrib
725 float c[] = GR_COLOR_TO_VEC4(coverage);
726 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
727 c));
728 fHWDrawState.setCoverage(coverage);
729 }
730 break;
731 case ProgramDesc::kUniform_ColorInput:
732 if (fProgramData->fCoverage != coverage) {
733 // OpenGL ES doesn't support unsigned byte varieties of
734 // glUniform
735 float c[] = GR_COLOR_TO_VEC4(coverage);
736 GrAssert(GrGLProgram::kUnusedUniform !=
737 fProgramData->fUniLocations.fCoverageUni);
738 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
739 1, c));
740 fProgramData->fCoverage = coverage;
741 }
742 break;
743 case ProgramDesc::kSolidWhite_ColorInput:
744 case ProgramDesc::kTransBlack_ColorInput:
745 break;
746 default:
747 GrCrash("Unknown coverage type.");
748 }
749 }
750}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000751
junov@google.comf93e7172011-03-31 21:26:24 +0000752bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
753 if (!flushGLStateCommon(type)) {
754 return false;
755 }
756
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000757 const GrDrawState& drawState = this->getDrawState();
758
junov@google.comf93e7172011-03-31 21:26:24 +0000759 if (fDirtyFlags.fRenderTargetChanged) {
760 // our coords are in pixel space and the GL matrices map to NDC
761 // so if the viewport changed, our matrix is now wrong.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000762 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
junov@google.comf93e7172011-03-31 21:26:24 +0000763 // we assume all shader matrices may be wrong after viewport changes
764 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000765 }
766
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000767 GrBlendCoeff srcCoeff;
768 GrBlendCoeff dstCoeff;
769 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
770 if (kSkipDraw_BlendOptFlag & blendOpts) {
771 return false;
772 }
773
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000774 GrCustomStage* customStages [GrDrawState::kNumStages];
775 this->buildProgram(type, blendOpts, dstCoeff, customStages);
776 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
777 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000778 if (NULL == fProgramData) {
779 GrAssert(!"Failed to create program!");
780 return false;
781 }
junov@google.comf93e7172011-03-31 21:26:24 +0000782
783 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000784 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000785 fHWProgramID = fProgramData->fProgramID;
786 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000787 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
788 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000789
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000790 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000791 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000792 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
793 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000794 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000795 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
796 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000797 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000798 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000799 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000800 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000801 }
802 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000803 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000804
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000805 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000806
tomhudson@google.com93813632011-10-27 20:21:16 +0000807 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000808 if (this->isStageEnabled(s)) {
809 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000810
bsalomon@google.com40d92932011-12-13 18:40:47 +0000811 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000812
bsalomon@google.com40d92932011-12-13 18:40:47 +0000813 this->flushConvolution(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000814
bsalomon@google.com40d92932011-12-13 18:40:47 +0000815 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000816
bsalomon@google.com40d92932011-12-13 18:40:47 +0000817 this->flushTextureDomain(s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000818
819 if (NULL != fProgramData->fCustomStage[s]) {
820 const GrSamplerState& sampler =
821 this->getDrawState().getSampler(s);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000822 const GrGLTexture* texture =
823 static_cast<const GrGLTexture*>(
824 this->getDrawState().getTexture(s));
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000825 fProgramData->fCustomStage[s]->setData(
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000826 this->glInterface(), sampler.getCustomStage(), texture);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000827 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000828 }
junov@google.comf93e7172011-03-31 21:26:24 +0000829 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000830 this->flushColorMatrix();
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000831 resetDirtyFlags();
junov@google.comf93e7172011-03-31 21:26:24 +0000832 return true;
833}
834
835void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000836}
837
bsalomon@google.com2717d562012-05-07 19:10:52 +0000838#if GR_TEXT_SCALAR_IS_USHORT
839 #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT
840 #define TEXT_COORDS_ARE_NORMALIZED 1
841#elif GR_TEXT_SCALAR_IS_FLOAT
842 #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT
843 #define TEXT_COORDS_ARE_NORMALIZED 0
844#elif GR_TEXT_SCALAR_IS_FIXED
845 #define TEXT_COORDS_GL_TYPE GR_GL_FIXED
846 #define TEXT_COORDS_ARE_NORMALIZED 0
847#else
848 #error "unknown GR_TEXT_SCALAR type"
849#endif
850
junov@google.comf93e7172011-03-31 21:26:24 +0000851void GrGpuGLShaders::setupGeometry(int* startVertex,
852 int* startIndex,
853 int vertexCount,
854 int indexCount) {
855
856 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000857 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000858 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000859 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000860
bsalomon@google.come79c8152012-03-29 19:07:12 +0000861 GrVertexLayout currLayout = this->getVertexLayout();
862
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000863 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000864 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000865 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000866 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000867 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000868 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000869 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000870 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000871 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000872 int oldEdgeOffset;
873
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000874 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
875 fHWGeometryState.fVertexLayout,
876 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000877 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000878 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000879 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000880 bool indexed = NULL != startIndex;
881
882 int extraVertexOffset;
883 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000884 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000885
886 GrGLenum scalarType;
887 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000888 if (currLayout & kTextFormat_VertexLayoutBit) {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000889 scalarType = TEXT_COORDS_GL_TYPE;
890 texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED);
junov@google.comf93e7172011-03-31 21:26:24 +0000891 } else {
bsalomon@google.com2717d562012-05-07 19:10:52 +0000892 GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT);
893 scalarType = GR_GL_FLOAT;
junov@google.comf93e7172011-03-31 21:26:24 +0000894 texCoordNorm = false;
895 }
896
897 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
898 *startVertex = 0;
899 if (indexed) {
900 *startIndex += extraIndexOffset;
901 }
902
903 // all the Pointers must be set if any of these are true
904 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
905 vertexOffset != fHWGeometryState.fVertexOffset ||
906 newStride != oldStride;
907
908 // position and tex coord offsets change if above conditions are true
909 // or the type/normalization changed based on text vs nontext type coords.
910 bool posAndTexChange = allOffsetsChange ||
bsalomon@google.com2717d562012-05-07 19:10:52 +0000911 (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) &&
junov@google.comf93e7172011-03-31 21:26:24 +0000912 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000913 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000914
915 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000916 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000917 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000918 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000919 fHWGeometryState.fVertexOffset = vertexOffset;
920 }
921
tomhudson@google.com93813632011-10-27 20:21:16 +0000922 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000923 if (newTexCoordOffsets[t] > 0) {
924 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000925 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000926 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000927 GL_CALL(EnableVertexAttribArray(idx));
928 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000929 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000930 } else if (posAndTexChange ||
931 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000932 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000933 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000934 }
935 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000936 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000937 }
938 }
939
940 if (newColorOffset > 0) {
941 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000942 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000943 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000944 GL_CALL(EnableVertexAttribArray(idx));
945 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000946 true, newStride, colorOffset));
947 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000948 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000949 true, newStride, colorOffset));
950 }
951 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000952 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000953 }
954
bsalomon@google.coma3108262011-10-10 14:08:47 +0000955 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000956 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000957 int idx = GrGLProgram::CoverageAttributeIdx();
958 if (oldCoverageOffset <= 0) {
959 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000960 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000961 true, newStride, coverageOffset));
962 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000963 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000964 true, newStride, coverageOffset));
965 }
966 } else if (oldCoverageOffset > 0) {
967 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
968 }
969
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000970 if (newEdgeOffset > 0) {
971 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
972 int idx = GrGLProgram::EdgeAttributeIdx();
973 if (oldEdgeOffset <= 0) {
974 GL_CALL(EnableVertexAttribArray(idx));
975 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
976 false, newStride, edgeOffset));
977 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
978 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
979 false, newStride, edgeOffset));
980 }
981 } else if (oldEdgeOffset > 0) {
982 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
983 }
984
bsalomon@google.come79c8152012-03-29 19:07:12 +0000985 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000986 fHWGeometryState.fArrayPtrsDirty = false;
987}
988
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000989namespace {
990
991void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
992 const GrSamplerState& sampler,
993 GrCustomStage** customStages,
994 GrGLProgram* program, int index) {
995 GrCustomStage* customStage = sampler.getCustomStage();
996 if (customStage) {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000997 GrProgramStageFactory* factory = customStage->getFactory();
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000998 stage->fCustomStageKey = factory->stageKey(customStage);
999 customStages[index] = customStage;
1000 } else {
1001 stage->fCustomStageKey = 0;
1002 customStages[index] = NULL;
1003 }
1004}
1005
1006}
1007
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001008void GrGpuGLShaders::buildProgram(GrPrimitiveType type,
1009 BlendOptFlags blendOpts,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001010 GrBlendCoeff dstCoeff,
1011 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001012 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001013 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +00001014
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001015 // This should already have been caught
1016 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
1017
1018 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
1019
1020 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
1021 kEmitCoverage_BlendOptFlag));
1022
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001023 // The descriptor is used as a cache key. Thus when a field of the
1024 // descriptor will not affect program generation (because of the vertex
1025 // layout in use or other descriptor field settings) it should be set
1026 // to a canonical value to avoid duplicate programs with different keys.
1027
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001028 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +00001029 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001030
1031 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
1032
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001033 bool requiresAttributeColors =
1034 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001035 bool requiresAttributeCoverage =
1036 !skipCoverage && SkToBool(desc.fVertexLayout &
1037 kCoverage_VertexLayoutBit);
1038
1039 // fColorInput/fCoverageInput records how colors are specified for the.
1040 // program. So we strip the bits from the layout to avoid false negatives
1041 // when searching for an existing program in the cache.
1042 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001043
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001044 desc.fColorFilterXfermode = skipColor ?
1045 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001046 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001047
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001048 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
1049
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001050 // no reason to do edge aa or look at per-vertex coverage if coverage is
1051 // ignored
1052 if (skipCoverage) {
1053 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
1054 kCoverage_VertexLayoutBit);
1055 }
1056
1057 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
1058 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
1059 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001060 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001061 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001062 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001063 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001064 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001065 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001066 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001067 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001068 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001069 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001070
1071 bool covIsSolidWhite = !requiresAttributeCoverage &&
1072 0xffffffff == drawState.getCoverage();
1073
1074 if (skipCoverage) {
1075 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
1076 } else if (covIsSolidWhite) {
1077 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
1078 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
1079 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
1080 } else {
1081 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
1082 }
junov@google.comf93e7172011-03-31 21:26:24 +00001083
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001084 int lastEnabledStage = -1;
1085
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001086 if (!skipCoverage && (desc.fVertexLayout &
1087 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001088 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001089 } else {
1090 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +00001091 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001092 }
1093
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001094 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001095 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001096
1097 stage.fOptFlags = 0;
1098 stage.setEnabled(this->isStageEnabled(s));
1099
1100 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
1101 skipCoverage;
1102
1103 if (!skip && stage.isEnabled()) {
1104 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001105 const GrGLTexture* texture =
1106 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001107 GrAssert(NULL != texture);
1108 const GrSamplerState& sampler = drawState.getSampler(s);
1109 // we matrix to invert when orientation is TopDown, so make sure
1110 // we aren't in that case before flagging as identity.
1111 if (TextureMatrixIsIdentity(texture, sampler)) {
1112 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
1113 } else if (!sampler.getMatrix().hasPerspective()) {
1114 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
1115 }
1116 switch (sampler.getSampleMode()) {
1117 case GrSamplerState::kNormal_SampleMode:
1118 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
1119 break;
1120 case GrSamplerState::kRadial_SampleMode:
1121 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
1122 break;
1123 case GrSamplerState::kRadial2_SampleMode:
1124 if (sampler.radial2IsDegenerate()) {
1125 stage.fCoordMapping =
1126 StageDesc::kRadial2GradientDegenerate_CoordMapping;
1127 } else {
1128 stage.fCoordMapping =
1129 StageDesc::kRadial2Gradient_CoordMapping;
1130 }
1131 break;
1132 case GrSamplerState::kSweep_SampleMode:
1133 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
1134 break;
1135 default:
1136 GrCrash("Unexpected sample mode!");
1137 break;
1138 }
1139
1140 switch (sampler.getFilter()) {
1141 // these both can use a regular texture2D()
1142 case GrSamplerState::kNearest_Filter:
1143 case GrSamplerState::kBilinear_Filter:
1144 stage.fFetchMode = StageDesc::kSingle_FetchMode;
1145 break;
1146 // performs 4 texture2D()s
1147 case GrSamplerState::k4x4Downsample_Filter:
1148 stage.fFetchMode = StageDesc::k2x2_FetchMode;
1149 break;
1150 // performs fKernelWidth texture2D()s
1151 case GrSamplerState::kConvolution_Filter:
1152 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
1153 break;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001154 case GrSamplerState::kDilate_Filter:
1155 stage.fFetchMode = StageDesc::kDilate_FetchMode;
1156 break;
1157 case GrSamplerState::kErode_Filter:
1158 stage.fFetchMode = StageDesc::kErode_FetchMode;
1159 break;
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001160 default:
1161 GrCrash("Unexpected filter!");
1162 break;
1163 }
1164
1165 if (sampler.hasTextureDomain()) {
1166 GrAssert(GrSamplerState::kClamp_WrapMode ==
1167 sampler.getWrapX() &&
1168 GrSamplerState::kClamp_WrapMode ==
1169 sampler.getWrapY());
1170 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
1171 }
1172
1173 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001174 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001175 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1176 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +00001177 // the shader must smear the single channel after
1178 // reading the texture
1179 if (this->glCaps().textureRedSupport()) {
1180 // we can use R8 textures so use kSmearRed
1181 stage.fInConfigFlags |=
1182 StageDesc::kSmearRed_InConfigFlag;
1183 } else {
1184 // we can use A8 textures so use kSmearAlpha
1185 stage.fInConfigFlags |=
1186 StageDesc::kSmearAlpha_InConfigFlag;
1187 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001188 } else if (sampler.swapsRAndB()) {
1189 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
1190 }
1191 }
1192 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001193 // The shader generator assumes that color channels are bytes
1194 // when rounding.
1195 GrAssert(4 == GrBytesPerPixel(texture->config()));
1196 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
1197 fUnpremulConversion) {
1198 stage.fInConfigFlags |=
1199 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
1200 } else {
1201 stage.fInConfigFlags |=
1202 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
1203 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001204 }
1205
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001206 if (sampler.getFilter() == GrSamplerState::kDilate_Filter ||
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001207 sampler.getFilter() == GrSamplerState::kErode_Filter) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001208 stage.fKernelWidth = sampler.getKernelWidth();
1209 } else {
1210 stage.fKernelWidth = 0;
1211 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001212
1213 setup_custom_stage(&stage, sampler, customStages,
1214 &fCurrentProgram, s);
1215
junov@google.comf93e7172011-03-31 21:26:24 +00001216 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001217 stage.fOptFlags = 0;
1218 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1219 stage.fInConfigFlags = 0;
1220 stage.fFetchMode = (StageDesc::FetchMode) 0;
1221 stage.fKernelWidth = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001222 stage.fCustomStageKey = 0;
1223 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +00001224 }
1225 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001226
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001227 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001228 // The shader generator assumes that color channels are bytes
1229 // when rounding.
1230 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
1231 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
1232 desc.fOutputConfig =
1233 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
1234 } else {
1235 desc.fOutputConfig =
1236 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
1237 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001238 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001239 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001240 }
1241
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001242 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001243
1244 // currently the experimental GS will only work with triangle prims
1245 // (and it doesn't do anything other than pass through values from
1246 // the VS to the FS anyway).
1247#if 0 && GR_GL_EXPERIMENTAL_GS
1248 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1249#endif
1250
bsalomon@google.coma3108262011-10-10 14:08:47 +00001251 // we want to avoid generating programs with different "first cov stage"
1252 // values when they would compute the same result.
1253 // We set field in the desc to kNumStages when either there are no
1254 // coverage stages or the distinction between coverage and color is
1255 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001256 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +00001257 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001258 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001259 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001260 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +00001261 }
1262
1263 // other coverage inputs
1264 if (!hasCoverage) {
1265 hasCoverage =
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001266 requiresAttributeCoverage ||
bsalomon@google.coma3108262011-10-10 14:08:47 +00001267 (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit);
1268 }
1269
1270 if (hasCoverage) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001271 // color filter is applied between color/coverage computation
1272 if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) {
bsalomon@google.coma3108262011-10-10 14:08:47 +00001273 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001274 }
1275
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001276 if (this->getCaps().fDualSourceBlendingSupport &&
1277 !(blendOpts & (kEmitCoverage_BlendOptFlag |
1278 kCoverageAsAlpha_BlendOptFlag))) {
1279 if (kZero_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001280 // write the coverage value to second color
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001281 desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001282 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001283 } else if (kSA_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001284 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1285 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001286 desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001287 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001288 } else if (kSC_BlendCoeff == dstCoeff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001289 // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially
1290 // cover
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001291 desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001292 desc.fFirstCoverageStage = firstCoverageStage;
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001293 }
1294 }
1295 }
junov@google.comf93e7172011-03-31 21:26:24 +00001296}