blob: 1fcfbbee2bdcb3687185524678194e6aa52306a6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
junov@google.comf93e7172011-03-31 21:26:24 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
junov@google.comf93e7172011-03-31 21:26:24 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000010#include "../GrBinHashKey.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.comdd182cb2012-02-10 21:01:00 +000016#include "../GrGpuVertex.h"
junov@google.comf93e7172011-03-31 21:26:24 +000017#include "GrNoncopyable.h"
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000018#include "../GrStringBuilder.h"
19#include "../GrRandom.h"
junov@google.comf93e7172011-03-31 21:26:24 +000020
junov@google.comf93e7172011-03-31 21:26:24 +000021#define SKIP_CACHE_CHECK true
22#define GR_UINT32_MAX static_cast<uint32_t>(-1)
23
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000024#include "../GrTHashCache.h"
junov@google.comf93e7172011-03-31 21:26:24 +000025
26class GrGpuGLShaders::ProgramCache : public ::GrNoncopyable {
27private:
28 class Entry;
29
junov@google.comf7c00f62011-08-18 18:15:16 +000030 typedef GrBinHashKey<Entry, GrGLProgram::kProgramKeySize> ProgramHashKey;
junov@google.comf93e7172011-03-31 21:26:24 +000031
32 class Entry : public ::GrNoncopyable {
33 public:
34 Entry() {}
junov@google.comf93e7172011-03-31 21:26:24 +000035 void copyAndTakeOwnership(Entry& entry) {
36 fProgramData.copyAndTakeOwnership(entry.fProgramData);
junov@google.comf7c00f62011-08-18 18:15:16 +000037 fKey = entry.fKey; // ownership transfer
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000038 fLRUStamp = entry.fLRUStamp;
junov@google.comf93e7172011-03-31 21:26:24 +000039 }
40
41 public:
42 int compare(const ProgramHashKey& key) const { return fKey.compare(key); }
43
44 public:
45 GrGLProgram::CachedData fProgramData;
46 ProgramHashKey fKey;
47 unsigned int fLRUStamp;
48 };
49
50 GrTHashTable<Entry, ProgramHashKey, 8> fHashCache;
51
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000052 // We may have kMaxEntries+1 shaders in the GL context because
53 // we create a new shader before evicting from the cache.
junov@google.comf93e7172011-03-31 21:26:24 +000054 enum {
55 kMaxEntries = 32
56 };
bsalomon@google.com4fa66942011-09-20 19:06:12 +000057 Entry fEntries[kMaxEntries];
58 int fCount;
59 unsigned int fCurrLRUStamp;
bsalomon@google.com96399942012-02-13 14:39:16 +000060 const GrGLContextInfo& fGL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000061
junov@google.comf93e7172011-03-31 21:26:24 +000062public:
bsalomon@google.com96399942012-02-13 14:39:16 +000063 ProgramCache(const GrGLContextInfo& gl)
junov@google.comf93e7172011-03-31 21:26:24 +000064 : fCount(0)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000065 , fCurrLRUStamp(0)
bsalomon@google.com96399942012-02-13 14:39:16 +000066 , fGL(gl) {
junov@google.comf93e7172011-03-31 21:26:24 +000067 }
68
69 ~ProgramCache() {
70 for (int i = 0; i < fCount; ++i) {
bsalomon@google.com96399942012-02-13 14:39:16 +000071 GrGpuGLShaders::DeleteProgram(fGL.interface(),
72 &fEntries[i].fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +000073 }
74 }
75
76 void abandon() {
77 fCount = 0;
78 }
79
80 void invalidateViewMatrices() {
81 for (int i = 0; i < fCount; ++i) {
82 // set to illegal matrix
83 fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix();
84 }
85 }
86
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000087 GrGLProgram::CachedData* getProgramData(const GrGLProgram& desc,
88 GrCustomStage** stages) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000089 Entry newEntry;
junov@google.comf7c00f62011-08-18 18:15:16 +000090 newEntry.fKey.setKeyData(desc.keyData());
91
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000092 Entry* entry = fHashCache.find(newEntry.fKey);
junov@google.comf93e7172011-03-31 21:26:24 +000093 if (NULL == entry) {
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000094 if (!desc.genProgram(fGL, stages, &newEntry.fProgramData)) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +000095 return NULL;
96 }
junov@google.comf93e7172011-03-31 21:26:24 +000097 if (fCount < kMaxEntries) {
98 entry = fEntries + fCount;
99 ++fCount;
100 } else {
101 GrAssert(kMaxEntries == fCount);
102 entry = fEntries;
103 for (int i = 1; i < kMaxEntries; ++i) {
104 if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
105 entry = fEntries + i;
106 }
107 }
108 fHashCache.remove(entry->fKey, entry);
bsalomon@google.com96399942012-02-13 14:39:16 +0000109 GrGpuGLShaders::DeleteProgram(fGL.interface(),
110 &entry->fProgramData);
junov@google.comf93e7172011-03-31 21:26:24 +0000111 }
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000112 entry->copyAndTakeOwnership(newEntry);
junov@google.comf93e7172011-03-31 21:26:24 +0000113 fHashCache.insert(entry->fKey, entry);
114 }
115
116 entry->fLRUStamp = fCurrLRUStamp;
117 if (GR_UINT32_MAX == fCurrLRUStamp) {
118 // wrap around! just trash our LRU, one time hit.
119 for (int i = 0; i < fCount; ++i) {
120 fEntries[i].fLRUStamp = 0;
121 }
122 }
123 ++fCurrLRUStamp;
124 return &entry->fProgramData;
125 }
126};
127
junov@google.com53a55842011-06-08 22:55:10 +0000128void GrGpuGLShaders::abandonResources(){
129 INHERITED::abandonResources();
130 fProgramCache->abandon();
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000131 fHWProgramID = 0;
junov@google.com53a55842011-06-08 22:55:10 +0000132}
133
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000134void GrGpuGLShaders::DeleteProgram(const GrGLInterface* gl,
135 CachedData* programData) {
136 GR_GL_CALL(gl, DeleteShader(programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000137 if (programData->fGShaderID) {
138 GR_GL_CALL(gl, DeleteShader(programData->fGShaderID));
139 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000140 GR_GL_CALL(gl, DeleteShader(programData->fFShaderID));
141 GR_GL_CALL(gl, DeleteProgram(programData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000142 GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));)
143}
144
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000145////////////////////////////////////////////////////////////////////////////////
146
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000147#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
148
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000149namespace {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000150
bsalomon@google.com74b98712011-11-11 19:46:16 +0000151// GrRandoms nextU() values have patterns in the low bits
152// So using nextU() % array_count might never take some values.
153int random_int(GrRandom* r, int count) {
154 return (int)(r->nextF() * count);
155}
156
157// min is inclusive, max is exclusive
158int random_int(GrRandom* r, int min, int max) {
159 return (int)(r->nextF() * (max-min)) + min;
160}
161
162bool random_bool(GrRandom* r) {
163 return r->nextF() > .5f;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000164}
165
166}
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000167
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000168bool GrGpuGLShaders::programUnitTest() {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000169
tomhudson@google.com086e5352011-12-08 14:44:10 +0000170 GrGLSLGeneration glslGeneration =
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000171 GrGetGLSLGeneration(this->glBinding(), this->glInterface());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000172 static const int STAGE_OPTS[] = {
173 0,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000174 StageDesc::kNoPerspective_OptFlagBit,
175 StageDesc::kIdentity_CoordMapping
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000176 };
bsalomon@google.com74b98712011-11-11 19:46:16 +0000177 static const int IN_CONFIG_FLAGS[] = {
178 StageDesc::kNone_InConfigFlag,
179 StageDesc::kSwapRAndB_InConfigFlag,
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000180 StageDesc::kSwapRAndB_InConfigFlag |
181 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag,
182 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000183 StageDesc::kSmearAlpha_InConfigFlag,
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000184 StageDesc::kSmearRed_InConfigFlag,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000185 };
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000186 GrGLProgram program;
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000187 ProgramDesc& pdesc = program.fProgramDesc;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000188
189 static const int NUM_TESTS = 512;
190
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000191 GrRandom random;
192 for (int t = 0; t < NUM_TESTS; ++t) {
193
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000194#if 0
195 GrPrintf("\nTest Program %d\n-------------\n", t);
196 static const int stop = -1;
197 if (t == stop) {
198 int breakpointhere = 9;
199 }
200#endif
201
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000202 pdesc.fVertexLayout = 0;
203 pdesc.fEmitsPointSize = random.nextF() > .5f;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000204 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000205 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000206
bsalomon@google.com74b98712011-11-11 19:46:16 +0000207 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000208
bsalomon@google.com74b98712011-11-11 19:46:16 +0000209 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000210
bsalomon@google.com74b98712011-11-11 19:46:16 +0000211 pdesc.fVertexLayout |= random_bool(&random) ?
bsalomon@google.coma3108262011-10-10 14:08:47 +0000212 GrDrawTarget::kCoverage_VertexLayoutBit :
213 0;
214
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000215#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com6f92f182011-09-29 15:05:48 +0000216 pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport &&
bsalomon@google.com74b98712011-11-11 19:46:16 +0000217 random_bool(&random);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000218#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000219 pdesc.fOutputConfig = random_int(&random, ProgramDesc::kOutputConfigCnt);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000220
bsalomon@google.com74b98712011-11-11 19:46:16 +0000221 bool edgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000222 if (edgeAA) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000223 bool vertexEdgeAA = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000224 if (vertexEdgeAA) {
225 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000226 if (this->getCaps().fShaderDerivativeSupport) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000227 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000228 } else {
tomhudson@google.com93813632011-10-27 20:21:16 +0000229 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000230 }
231 pdesc.fEdgeAANumEdges = 0;
232 } else {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000233 pdesc.fEdgeAANumEdges = random_int(&random, 1, this->getMaxEdges());
234 pdesc.fEdgeAAConcave = random_bool(&random);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000235 }
236 } else {
237 pdesc.fEdgeAANumEdges = 0;
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000238 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000239
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000240 pdesc.fColorMatrixEnabled = random_bool(&random);
241
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000242 if (this->getCaps().fDualSourceBlendingSupport) {
bsalomon@google.com74b98712011-11-11 19:46:16 +0000243 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000244 } else {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000245 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000246 }
247
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000248 GrCustomStage* customStages[GrDrawState::kNumStages];
249
tomhudson@google.com93813632011-10-27 20:21:16 +0000250 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000251 // enable the stage?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000252 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000253 // use separate tex coords?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000254 if (random_bool(&random)) {
255 int t = random_int(&random, GrDrawState::kMaxTexCoords);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000256 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
257 } else {
258 pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s);
259 }
260 }
261 // use text-formatted verts?
bsalomon@google.com74b98712011-11-11 19:46:16 +0000262 if (random_bool(&random)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000263 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
264 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000265 StageDesc& stage = pdesc.fStages[s];
bsalomon@google.com74b98712011-11-11 19:46:16 +0000266 stage.fOptFlags = STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
267 stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];
268 stage.fCoordMapping = random_int(&random, StageDesc::kCoordMappingCnt);
269 stage.fFetchMode = random_int(&random, StageDesc::kFetchModeCnt);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000270 // convolution shaders don't work with persp tex matrix
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000271 if (stage.fFetchMode == StageDesc::kConvolution_FetchMode ||
272 stage.fFetchMode == StageDesc::kDilate_FetchMode ||
273 stage.fFetchMode == StageDesc::kErode_FetchMode) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000274 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
275 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000276 stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout));
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000277 static const uint32_t kMulByAlphaMask =
278 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
279 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000280
bsalomon@google.com74b98712011-11-11 19:46:16 +0000281 switch (stage.fFetchMode) {
282 case StageDesc::kSingle_FetchMode:
283 stage.fKernelWidth = 0;
284 break;
285 case StageDesc::kConvolution_FetchMode:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000286 case StageDesc::kDilate_FetchMode:
287 case StageDesc::kErode_FetchMode:
bsalomon@google.com74b98712011-11-11 19:46:16 +0000288 stage.fKernelWidth = random_int(&random, 2, 8);
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000289 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000290 break;
291 case StageDesc::k2x2_FetchMode:
292 stage.fKernelWidth = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000293 stage.fInConfigFlags &= ~kMulByAlphaMask;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000294 break;
295 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000296
297 stage.fCustomStageKey = 0;
298 customStages[s] = NULL;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000299 }
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000300 CachedData cachedData;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000301 if (!program.genProgram(this->glContextInfo(), customStages,
302 &cachedData)) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000303 return false;
304 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000305 DeleteProgram(this->glInterface(), &cachedData);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000306 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000307 return true;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000308}
309
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000310GrGpuGLShaders::GrGpuGLShaders(const GrGLContextInfo& ctxInfo)
311 : GrGpuGL(ctxInfo) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000312
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000313 // Enable supported shader-related caps
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000314 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000315 fCaps.fDualSourceBlendingSupport =
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000316 this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000317 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000318 fCaps.fShaderDerivativeSupport = true;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000319 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
320 fCaps.fGeometryShaderSupport =
321 this->glVersion() >= GR_GL_VER(3,2) &&
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000322 this->glslGeneration() >= k150_GrGLSLGeneration;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000323 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000324 fCaps.fShaderDerivativeSupport =
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000325 this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000326 }
junov@google.comf93e7172011-03-31 21:26:24 +0000327
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000328 GR_GL_GetIntegerv(this->glInterface(),
329 GR_GL_MAX_VERTEX_ATTRIBS,
330 &fMaxVertexAttribs);
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000331
junov@google.comf93e7172011-03-31 21:26:24 +0000332 fProgramData = NULL;
bsalomon@google.com96399942012-02-13 14:39:16 +0000333 fProgramCache = new ProgramCache(this->glContextInfo());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000334
335#if 0
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000336 this->programUnitTest();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000337#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000338}
339
340GrGpuGLShaders::~GrGpuGLShaders() {
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000341
342 if (fProgramData && 0 != fHWProgramID) {
343 // detach the current program so there is no confusion on OpenGL's part
344 // that we want it to be deleted
345 SkASSERT(fHWProgramID == fProgramData->fProgramID);
346 GL_CALL(UseProgram(0));
347 }
junov@google.comf93e7172011-03-31 21:26:24 +0000348 delete fProgramCache;
349}
350
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000351const GrMatrix& GrGpuGLShaders::getHWViewMatrix() {
352 GrAssert(fProgramData);
353
354 if (GrGLProgram::kSetAsAttribute ==
355 fProgramData->fUniLocations.fViewMatrixUni) {
356 return fHWDrawState.getViewMatrix();
357 } else {
358 return fProgramData->fViewMatrix;
359 }
360}
361
362void GrGpuGLShaders::recordHWViewMatrix(const GrMatrix& matrix) {
363 GrAssert(fProgramData);
364 if (GrGLProgram::kSetAsAttribute ==
365 fProgramData->fUniLocations.fViewMatrixUni) {
366 fHWDrawState.setViewMatrix(matrix);
367 } else {
368 fProgramData->fViewMatrix = matrix;
369 }
370}
371
junov@google.comf93e7172011-03-31 21:26:24 +0000372const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) {
junov@google.comf93e7172011-03-31 21:26:24 +0000373 GrAssert(fProgramData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000374
375 if (GrGLProgram::kSetAsAttribute ==
376 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000377 return fHWDrawState.getSampler(stage).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000378 } else {
379 return fProgramData->fTextureMatrices[stage];
380 }
junov@google.comf93e7172011-03-31 21:26:24 +0000381}
382
383void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) {
junov@google.comf93e7172011-03-31 21:26:24 +0000384 GrAssert(fProgramData);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000385 if (GrGLProgram::kSetAsAttribute ==
bsalomon@google.com91961302011-05-09 18:39:58 +0000386 fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000387 *fHWDrawState.sampler(stage)->matrix() = matrix;
bsalomon@google.com91961302011-05-09 18:39:58 +0000388 } else {
389 fProgramData->fTextureMatrices[stage] = matrix;
390 }
junov@google.comf93e7172011-03-31 21:26:24 +0000391}
392
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000393void GrGpuGLShaders::onResetContext() {
394 INHERITED::onResetContext();
junov@google.comf93e7172011-03-31 21:26:24 +0000395
junov@google.comf93e7172011-03-31 21:26:24 +0000396 fHWGeometryState.fVertexOffset = ~0;
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +0000397
398 // Third party GL code may have left vertex attributes enabled. Some GL
399 // implementations (osmesa) may read vetex attributes that are not required
400 // by the current shader. Therefore, we have to ensure that only the
401 // attributes we require for the current draw are enabled or we may cause an
402 // invalid read.
403
404 // Disable all vertex layout bits so that next flush will assume all
405 // optional vertex attributes are disabled.
406 fHWGeometryState.fVertexLayout = 0;
407
408 // We always use the this attribute and assume it is always enabled.
409 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
410 GL_CALL(EnableVertexAttribArray(posAttrIdx));
411 // Disable all other vertex attributes.
412 for (int va = 0; va < fMaxVertexAttribs; ++va) {
413 if (va != posAttrIdx) {
414 GL_CALL(DisableVertexAttribArray(va));
415 }
junov@google.comf93e7172011-03-31 21:26:24 +0000416 }
junov@google.comf93e7172011-03-31 21:26:24 +0000417
418 fHWProgramID = 0;
419}
420
421void GrGpuGLShaders::flushViewMatrix() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000422 const GrMatrix& vm = this->getDrawState().getViewMatrix();
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000423 if (!GrGpuGLShaders::getHWViewMatrix().cheapEqualTo(vm)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000424
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000425 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
426 GrAssert(NULL != rt);
427 GrMatrix m;
428 m.setAll(
429 GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1,
430 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1,
431 0, 0, GrMatrix::I()[8]);
432 m.setConcat(m, vm);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000433
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000434 // ES doesn't allow you to pass true to the transpose param,
435 // so do our own transpose
436 GrGLfloat mt[] = {
437 GrScalarToFloat(m[GrMatrix::kMScaleX]),
438 GrScalarToFloat(m[GrMatrix::kMSkewY]),
439 GrScalarToFloat(m[GrMatrix::kMPersp0]),
440 GrScalarToFloat(m[GrMatrix::kMSkewX]),
441 GrScalarToFloat(m[GrMatrix::kMScaleY]),
442 GrScalarToFloat(m[GrMatrix::kMPersp1]),
443 GrScalarToFloat(m[GrMatrix::kMTransX]),
444 GrScalarToFloat(m[GrMatrix::kMTransY]),
445 GrScalarToFloat(m[GrMatrix::kMPersp2])
446 };
447
448 if (GrGLProgram::kSetAsAttribute ==
449 fProgramData->fUniLocations.fViewMatrixUni) {
450 int baseIdx = GrGLProgram::ViewMatrixAttributeIdx();
451 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
452 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
453 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
454 } else {
455 GrAssert(GrGLProgram::kUnusedUniform !=
456 fProgramData->fUniLocations.fViewMatrixUni);
457 GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni,
458 1, false, mt));
459 }
460 this->recordHWViewMatrix(vm);
bsalomon@google.com91961302011-05-09 18:39:58 +0000461 }
junov@google.comf93e7172011-03-31 21:26:24 +0000462}
463
junov@google.com6acc9b32011-05-16 18:32:07 +0000464void GrGpuGLShaders::flushTextureDomain(int s) {
465 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000466 const GrDrawState& drawState = this->getDrawState();
junov@google.com6acc9b32011-05-16 18:32:07 +0000467 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000468 const GrRect &texDom = drawState.getSampler(s).getTextureDomain();
junov@google.com6acc9b32011-05-16 18:32:07 +0000469
twiz@google.com76b82742011-06-02 20:30:02 +0000470 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
471 fProgramData->fTextureDomain[s] != texDom) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000472
junov@google.com2f839402011-05-24 15:13:01 +0000473 fProgramData->fTextureDomain[s] = texDom;
junov@google.com6acc9b32011-05-16 18:32:07 +0000474
junov@google.com2f839402011-05-24 15:13:01 +0000475 float values[4] = {
twiz@google.com76b82742011-06-02 20:30:02 +0000476 GrScalarToFloat(texDom.left()),
477 GrScalarToFloat(texDom.top()),
478 GrScalarToFloat(texDom.right()),
479 GrScalarToFloat(texDom.bottom())
junov@google.com2f839402011-05-24 15:13:01 +0000480 };
481
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000482 const GrGLTexture* texture =
483 static_cast<const GrGLTexture*>(drawState.getTexture(s));
junov@google.com2f839402011-05-24 15:13:01 +0000484 GrGLTexture::Orientation orientation = texture->orientation();
485
486 // vertical flip if necessary
487 if (GrGLTexture::kBottomUp_Orientation == orientation) {
488 values[1] = 1.0f - values[1];
489 values[3] = 1.0f - values[3];
twiz@google.com76b82742011-06-02 20:30:02 +0000490 // The top and bottom were just flipped, so correct the ordering
491 // of elements so that values = (l, t, r, b).
492 SkTSwap(values[1], values[3]);
junov@google.com2f839402011-05-24 15:13:01 +0000493 }
494
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000495 GL_CALL(Uniform4fv(uni, 1, values));
junov@google.com6acc9b32011-05-16 18:32:07 +0000496 }
junov@google.com6acc9b32011-05-16 18:32:07 +0000497 }
498}
499
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000500void GrGpuGLShaders::flushTextureMatrix(int s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000501 const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000502 const GrDrawState& drawState = this->getDrawState();
503 const GrGLTexture* texture =
504 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000505 if (NULL != texture) {
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000506 const GrMatrix& hwMatrix = this->getHWSamplerMatrix(s);
507 const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix();
bsalomon@google.com91961302011-05-09 18:39:58 +0000508 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000509 (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000510 !hwMatrix.cheapEqualTo(samplerMatrix))) {
junov@google.comf93e7172011-03-31 21:26:24 +0000511
bsalomon@google.com8fe84b52012-03-26 15:24:27 +0000512 GrMatrix m = samplerMatrix;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000513 GrSamplerState::SampleMode mode =
514 drawState.getSampler(s).getSampleMode();
bsalomon@google.com91961302011-05-09 18:39:58 +0000515 AdjustTextureMatrix(texture, mode, &m);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000516
bsalomon@google.com91961302011-05-09 18:39:58 +0000517 // ES doesn't allow you to pass true to the transpose param,
518 // so do our own transpose
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000519 GrGLfloat mt[] = {
520 GrScalarToFloat(m[GrMatrix::kMScaleX]),
521 GrScalarToFloat(m[GrMatrix::kMSkewY]),
522 GrScalarToFloat(m[GrMatrix::kMPersp0]),
523 GrScalarToFloat(m[GrMatrix::kMSkewX]),
524 GrScalarToFloat(m[GrMatrix::kMScaleY]),
525 GrScalarToFloat(m[GrMatrix::kMPersp1]),
526 GrScalarToFloat(m[GrMatrix::kMTransX]),
527 GrScalarToFloat(m[GrMatrix::kMTransY]),
528 GrScalarToFloat(m[GrMatrix::kMPersp2])
bsalomon@google.com91961302011-05-09 18:39:58 +0000529 };
bsalomon@google.com27a4dc42011-05-16 13:14:03 +0000530
bsalomon@google.com91961302011-05-09 18:39:58 +0000531 if (GrGLProgram::kSetAsAttribute ==
532 fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) {
533 int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000534 GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0));
535 GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3));
536 GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6));
bsalomon@google.com91961302011-05-09 18:39:58 +0000537 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000538 GL_CALL(UniformMatrix3fv(uni, 1, false, mt));
bsalomon@google.com91961302011-05-09 18:39:58 +0000539 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000540 this->recordHWSamplerMatrix(s, drawState.getSampler(s).getMatrix());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000541 }
542 }
junov@google.comf93e7172011-03-31 21:26:24 +0000543}
544
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000545void GrGpuGLShaders::flushRadial2(int s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000546
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000547 const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000548 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
bsalomon@google.com91961302011-05-09 18:39:58 +0000549 if (GrGLProgram::kUnusedUniform != uni &&
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000550 (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() ||
551 fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() ||
552 fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) {
junov@google.comf93e7172011-03-31 21:26:24 +0000553
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000554 GrScalar centerX1 = sampler.getRadial2CenterX1();
555 GrScalar radius0 = sampler.getRadial2Radius0();
junov@google.comf93e7172011-03-31 21:26:24 +0000556
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000557 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
junov@google.comf93e7172011-03-31 21:26:24 +0000558
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000559 // when were in the degenerate (linear) case the second
560 // value will be INF but the program doesn't read it. (We
561 // use the same 6 uniforms even though we don't need them
562 // all in the linear case just to keep the code complexity
563 // down).
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000564 float values[6] = {
565 GrScalarToFloat(a),
bsalomon@google.com2cdfade2011-11-23 16:53:42 +0000566 1 / (2.f * GrScalarToFloat(a)),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000567 GrScalarToFloat(centerX1),
568 GrScalarToFloat(radius0),
569 GrScalarToFloat(GrMul(radius0, radius0)),
570 sampler.isRadial2PosRoot() ? 1.f : -1.f
571 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000572 GL_CALL(Uniform1fv(uni, 6, values));
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000573 fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1();
574 fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0();
575 fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot();
576 }
577}
578
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000579void GrGpuGLShaders::flushConvolution(int s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000580 const GrSamplerState& sampler = this->getDrawState().getSampler(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000581 int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni;
582 if (GrGLProgram::kUnusedUniform != kernelUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000583 GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(),
584 sampler.getKernel()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000585 }
586 int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni;
587 if (GrGLProgram::kUnusedUniform != imageIncrementUni) {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000588 const GrGLTexture* texture =
589 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
590 float imageIncrement[2] = { 0 };
591 switch (sampler.getFilterDirection()) {
592 case GrSamplerState::kX_FilterDirection:
593 imageIncrement[0] = 1.0f / texture->width();
594 break;
595 case GrSamplerState::kY_FilterDirection:
596 imageIncrement[1] = 1.0f / texture->height();
597 break;
598 default:
599 GrCrash("Unknown filter direction.");
600 }
601 GL_CALL(Uniform2fv(imageIncrementUni, 1, imageIncrement));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000602 }
603}
604
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000605void GrGpuGLShaders::flushTexelSize(int s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000606 const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000607 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000608 const GrGLTexture* texture =
609 static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s));
bsalomon@google.com99621082011-11-15 16:47:16 +0000610 if (texture->width() != fProgramData->fTextureWidth[s] ||
611 texture->height() != fProgramData->fTextureHeight[s]) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000612
bsalomon@google.com99621082011-11-15 16:47:16 +0000613 float texelSize[] = {1.f / texture->width(),
614 1.f / texture->height()};
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000615 GL_CALL(Uniform2fv(uni, 1, texelSize));
bsalomon@google.com99621082011-11-15 16:47:16 +0000616 fProgramData->fTextureWidth[s] = texture->width();
617 fProgramData->fTextureHeight[s] = texture->height();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000618 }
619 }
junov@google.comf93e7172011-03-31 21:26:24 +0000620}
621
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000622void GrGpuGLShaders::flushEdgeAAData() {
623 const int& uni = fProgramData->fUniLocations.fEdgesUni;
624 if (GrGLProgram::kUnusedUniform != uni) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000625 int count = this->getDrawState().getNumAAEdges();
tomhudson@google.com93813632011-10-27 20:21:16 +0000626 GrDrawState::Edge edges[GrDrawState::kMaxEdges];
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000627 // Flip the edges in Y
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000628 float height =
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000629 static_cast<float>(this->getDrawState().getRenderTarget()->height());
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000630 for (int i = 0; i < count; ++i) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000631 edges[i] = this->getDrawState().getAAEdges()[i];
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000632 float b = edges[i].fY;
633 edges[i].fY = -b;
634 edges[i].fZ += b * height;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000635 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000636 GL_CALL(Uniform3fv(uni, count, &edges[0].fX));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000637 }
638}
639
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000640void GrGpuGLShaders::flushColorMatrix() {
641 const ProgramDesc& desc = fCurrentProgram.getDesc();
642 int matrixUni = fProgramData->fUniLocations.fColorMatrixUni;
643 int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni;
644 if (GrGLProgram::kUnusedUniform != matrixUni
645 && GrGLProgram::kUnusedUniform != vecUni) {
646 const float* m = this->getDrawState().getColorMatrix();
647 GrGLfloat mt[] = {
648 m[0], m[5], m[10], m[15],
649 m[1], m[6], m[11], m[16],
650 m[2], m[7], m[12], m[17],
651 m[3], m[8], m[13], m[18],
652 };
653 static float scale = 1.0f / 255.0f;
654 GrGLfloat vec[] = {
655 m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale,
656 };
657 GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt));
658 GL_CALL(Uniform4fv(vecUni, 1, vec));
659 }
660}
661
Scroggo01b87ec2011-05-11 18:05:38 +0000662static const float ONE_OVER_255 = 1.f / 255.f;
663
664#define GR_COLOR_TO_VEC4(color) {\
665 GrColorUnpackR(color) * ONE_OVER_255,\
666 GrColorUnpackG(color) * ONE_OVER_255,\
667 GrColorUnpackB(color) * ONE_OVER_255,\
668 GrColorUnpackA(color) * ONE_OVER_255 \
669}
670
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000671void GrGpuGLShaders::flushColor(GrColor color) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000672 const ProgramDesc& desc = fCurrentProgram.getDesc();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000673 const GrDrawState& drawState = this->getDrawState();
674
bsalomon@google.come79c8152012-03-29 19:07:12 +0000675 if (this->getVertexLayout() & kColor_VertexLayoutBit) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000676 // color will be specified per-vertex as an attribute
677 // invalidate the const vertex attrib color
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000678 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000679 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000680 switch (desc.fColorInput) {
681 case ProgramDesc::kAttribute_ColorInput:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000682 if (fHWDrawState.getColor() != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000683 // OpenGL ES only supports the float varieties of
684 // glVertexAttrib
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000685 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000686 GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(),
687 c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000688 fHWDrawState.setColor(color);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000689 }
690 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000691 case ProgramDesc::kUniform_ColorInput:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000692 if (fProgramData->fColor != color) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000693 // OpenGL ES doesn't support unsigned byte varieties of
694 // glUniform
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000695 float c[] = GR_COLOR_TO_VEC4(color);
bsalomon@google.com91961302011-05-09 18:39:58 +0000696 GrAssert(GrGLProgram::kUnusedUniform !=
697 fProgramData->fUniLocations.fColorUni);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000698 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni,
699 1, c));
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000700 fProgramData->fColor = color;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000701 }
702 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000703 case ProgramDesc::kSolidWhite_ColorInput:
704 case ProgramDesc::kTransBlack_ColorInput:
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000705 break;
706 default:
707 GrCrash("Unknown color type.");
708 }
709 }
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000710 if (fProgramData->fUniLocations.fColorFilterUni
711 != GrGLProgram::kUnusedUniform
712 && fProgramData->fColorFilterColor
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000713 != drawState.getColorFilterColor()) {
714 float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor());
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000715 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000716 fProgramData->fColorFilterColor = drawState.getColorFilterColor();
Scroggo97c88c22011-05-11 14:05:25 +0000717 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000718}
719
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000720void GrGpuGLShaders::flushCoverage(GrColor coverage) {
721 const ProgramDesc& desc = fCurrentProgram.getDesc();
722 const GrDrawState& drawState = this->getDrawState();
723
724
bsalomon@google.come79c8152012-03-29 19:07:12 +0000725 if (this->getVertexLayout() & kCoverage_VertexLayoutBit) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000726 // coverage will be specified per-vertex as an attribute
727 // invalidate the const vertex attrib coverage
728 fHWDrawState.setCoverage4(GrColor_ILLEGAL);
729 } else {
730 switch (desc.fCoverageInput) {
731 case ProgramDesc::kAttribute_ColorInput:
732 if (fHWDrawState.getCoverage() != coverage) {
733 // OpenGL ES only supports the float varieties of
734 // glVertexAttrib
735 float c[] = GR_COLOR_TO_VEC4(coverage);
736 GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(),
737 c));
738 fHWDrawState.setCoverage(coverage);
739 }
740 break;
741 case ProgramDesc::kUniform_ColorInput:
742 if (fProgramData->fCoverage != coverage) {
743 // OpenGL ES doesn't support unsigned byte varieties of
744 // glUniform
745 float c[] = GR_COLOR_TO_VEC4(coverage);
746 GrAssert(GrGLProgram::kUnusedUniform !=
747 fProgramData->fUniLocations.fCoverageUni);
748 GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni,
749 1, c));
750 fProgramData->fCoverage = coverage;
751 }
752 break;
753 case ProgramDesc::kSolidWhite_ColorInput:
754 case ProgramDesc::kTransBlack_ColorInput:
755 break;
756 default:
757 GrCrash("Unknown coverage type.");
758 }
759 }
760}
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000761
junov@google.comf93e7172011-03-31 21:26:24 +0000762bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) {
763 if (!flushGLStateCommon(type)) {
764 return false;
765 }
766
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000767 const GrDrawState& drawState = this->getDrawState();
768
junov@google.comf93e7172011-03-31 21:26:24 +0000769 if (fDirtyFlags.fRenderTargetChanged) {
770 // our coords are in pixel space and the GL matrices map to NDC
771 // so if the viewport changed, our matrix is now wrong.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000772 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
junov@google.comf93e7172011-03-31 21:26:24 +0000773 // we assume all shader matrices may be wrong after viewport changes
774 fProgramCache->invalidateViewMatrices();
junov@google.comf93e7172011-03-31 21:26:24 +0000775 }
776
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000777 GrBlendCoeff srcCoeff;
778 GrBlendCoeff dstCoeff;
779 BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff);
780 if (kSkipDraw_BlendOptFlag & blendOpts) {
781 return false;
782 }
783
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000784 GrCustomStage* customStages [GrDrawState::kNumStages];
785 this->buildProgram(type, blendOpts, dstCoeff, customStages);
786 fProgramData = fProgramCache->getProgramData(fCurrentProgram,
787 customStages);
bsalomon@google.com91961302011-05-09 18:39:58 +0000788 if (NULL == fProgramData) {
789 GrAssert(!"Failed to create program!");
790 return false;
791 }
junov@google.comf93e7172011-03-31 21:26:24 +0000792
793 if (fHWProgramID != fProgramData->fProgramID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000794 GL_CALL(UseProgram(fProgramData->fProgramID));
junov@google.comf93e7172011-03-31 21:26:24 +0000795 fHWProgramID = fProgramData->fProgramID;
796 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000797 fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff);
798 this->flushBlend(type, srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000799
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000800 GrColor color;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000801 GrColor coverage;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000802 if (blendOpts & kEmitTransBlack_BlendOptFlag) {
803 color = 0;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000804 coverage = 0;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000805 } else if (blendOpts & kEmitCoverage_BlendOptFlag) {
806 color = 0xffffffff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000807 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000808 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000809 color = drawState.getColor();
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000810 coverage = drawState.getCoverage();
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000811 }
812 this->flushColor(color);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000813 this->flushCoverage(coverage);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000814
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000815 this->flushViewMatrix();
junov@google.comf93e7172011-03-31 21:26:24 +0000816
tomhudson@google.com93813632011-10-27 20:21:16 +0000817 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com40d92932011-12-13 18:40:47 +0000818 if (this->isStageEnabled(s)) {
819 this->flushTextureMatrix(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000820
bsalomon@google.com40d92932011-12-13 18:40:47 +0000821 this->flushRadial2(s);
junov@google.comf93e7172011-03-31 21:26:24 +0000822
bsalomon@google.com40d92932011-12-13 18:40:47 +0000823 this->flushConvolution(s);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000824
bsalomon@google.com40d92932011-12-13 18:40:47 +0000825 this->flushTexelSize(s);
junov@google.com6acc9b32011-05-16 18:32:07 +0000826
bsalomon@google.com40d92932011-12-13 18:40:47 +0000827 this->flushTextureDomain(s);
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000828
829 if (NULL != fProgramData->fCustomStage[s]) {
830 const GrSamplerState& sampler =
831 this->getDrawState().getSampler(s);
832 fProgramData->fCustomStage[s]->setData(
833 this->glInterface(), sampler.getCustomStage());
834 }
bsalomon@google.com40d92932011-12-13 18:40:47 +0000835 }
junov@google.comf93e7172011-03-31 21:26:24 +0000836 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000837 this->flushEdgeAAData();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000838 this->flushColorMatrix();
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000839 resetDirtyFlags();
junov@google.comf93e7172011-03-31 21:26:24 +0000840 return true;
841}
842
843void GrGpuGLShaders::postDraw() {
junov@google.comf93e7172011-03-31 21:26:24 +0000844}
845
846void GrGpuGLShaders::setupGeometry(int* startVertex,
847 int* startIndex,
848 int vertexCount,
849 int indexCount) {
850
851 int newColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000852 int newCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000853 int newTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000854 int newEdgeOffset;
junov@google.comf93e7172011-03-31 21:26:24 +0000855
bsalomon@google.come79c8152012-03-29 19:07:12 +0000856 GrVertexLayout currLayout = this->getVertexLayout();
857
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000858 GrGLsizei newStride = VertexSizeAndOffsetsByIdx(
bsalomon@google.come79c8152012-03-29 19:07:12 +0000859 currLayout,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000860 newTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000861 &newColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000862 &newCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000863 &newEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000864 int oldColorOffset;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000865 int oldCoverageOffset;
tomhudson@google.com93813632011-10-27 20:21:16 +0000866 int oldTexCoordOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000867 int oldEdgeOffset;
868
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000869 GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(
870 fHWGeometryState.fVertexLayout,
871 oldTexCoordOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000872 &oldColorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000873 &oldCoverageOffset,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000874 &oldEdgeOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000875 bool indexed = NULL != startIndex;
876
877 int extraVertexOffset;
878 int extraIndexOffset;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000879 this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset);
junov@google.comf93e7172011-03-31 21:26:24 +0000880
881 GrGLenum scalarType;
882 bool texCoordNorm;
bsalomon@google.come79c8152012-03-29 19:07:12 +0000883 if (currLayout & kTextFormat_VertexLayoutBit) {
junov@google.comf93e7172011-03-31 21:26:24 +0000884 scalarType = GrGLTextType;
885 texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED;
886 } else {
887 scalarType = GrGLType;
888 texCoordNorm = false;
889 }
890
891 size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride;
892 *startVertex = 0;
893 if (indexed) {
894 *startIndex += extraIndexOffset;
895 }
896
897 // all the Pointers must be set if any of these are true
898 bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty ||
899 vertexOffset != fHWGeometryState.fVertexOffset ||
900 newStride != oldStride;
901
902 // position and tex coord offsets change if above conditions are true
903 // or the type/normalization changed based on text vs nontext type coords.
904 bool posAndTexChange = allOffsetsChange ||
905 (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) &&
906 (kTextFormat_VertexLayoutBit &
bsalomon@google.come79c8152012-03-29 19:07:12 +0000907 (fHWGeometryState.fVertexLayout ^ currLayout)));
junov@google.comf93e7172011-03-31 21:26:24 +0000908
909 if (posAndTexChange) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000910 int idx = GrGLProgram::PositionAttributeIdx();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000911 GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride,
bsalomon@google.com91961302011-05-09 18:39:58 +0000912 (GrGLvoid*)vertexOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000913 fHWGeometryState.fVertexOffset = vertexOffset;
914 }
915
tomhudson@google.com93813632011-10-27 20:21:16 +0000916 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
junov@google.comf93e7172011-03-31 21:26:24 +0000917 if (newTexCoordOffsets[t] > 0) {
918 GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000919 int idx = GrGLProgram::TexCoordAttributeIdx(t);
junov@google.comf93e7172011-03-31 21:26:24 +0000920 if (oldTexCoordOffsets[t] <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000921 GL_CALL(EnableVertexAttribArray(idx));
922 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000923 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000924 } else if (posAndTexChange ||
925 newTexCoordOffsets[t] != oldTexCoordOffsets[t]) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000926 GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm,
bsalomon@google.com91961302011-05-09 18:39:58 +0000927 newStride, texCoordOffset));
junov@google.comf93e7172011-03-31 21:26:24 +0000928 }
929 } else if (oldTexCoordOffsets[t] > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000930 GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t)));
junov@google.comf93e7172011-03-31 21:26:24 +0000931 }
932 }
933
934 if (newColorOffset > 0) {
935 GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset);
bsalomon@google.com91961302011-05-09 18:39:58 +0000936 int idx = GrGLProgram::ColorAttributeIdx();
junov@google.comf93e7172011-03-31 21:26:24 +0000937 if (oldColorOffset <= 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000938 GL_CALL(EnableVertexAttribArray(idx));
939 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000940 true, newStride, colorOffset));
941 } else if (allOffsetsChange || newColorOffset != oldColorOffset) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000942 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
junov@google.comf93e7172011-03-31 21:26:24 +0000943 true, newStride, colorOffset));
944 }
945 } else if (oldColorOffset > 0) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000946 GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx()));
junov@google.comf93e7172011-03-31 21:26:24 +0000947 }
948
bsalomon@google.coma3108262011-10-10 14:08:47 +0000949 if (newCoverageOffset > 0) {
bsalomon@google.come10f6fd2011-10-11 20:15:26 +0000950 GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000951 int idx = GrGLProgram::CoverageAttributeIdx();
952 if (oldCoverageOffset <= 0) {
953 GL_CALL(EnableVertexAttribArray(idx));
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000954 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000955 true, newStride, coverageOffset));
956 } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000957 GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000958 true, newStride, coverageOffset));
959 }
960 } else if (oldCoverageOffset > 0) {
961 GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx()));
962 }
963
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000964 if (newEdgeOffset > 0) {
965 GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset);
966 int idx = GrGLProgram::EdgeAttributeIdx();
967 if (oldEdgeOffset <= 0) {
968 GL_CALL(EnableVertexAttribArray(idx));
969 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
970 false, newStride, edgeOffset));
971 } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) {
972 GL_CALL(VertexAttribPointer(idx, 4, scalarType,
973 false, newStride, edgeOffset));
974 }
975 } else if (oldEdgeOffset > 0) {
976 GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx()));
977 }
978
bsalomon@google.come79c8152012-03-29 19:07:12 +0000979 fHWGeometryState.fVertexLayout = currLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000980 fHWGeometryState.fArrayPtrsDirty = false;
981}
982
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000983namespace {
984
985void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage,
986 const GrSamplerState& sampler,
987 GrCustomStage** customStages,
988 GrGLProgram* program, int index) {
989 GrCustomStage* customStage = sampler.getCustomStage();
990 if (customStage) {
991 GrGLProgramStageFactory* factory = customStage->getGLFactory();
992 stage->fCustomStageKey = factory->stageKey(customStage);
993 customStages[index] = customStage;
994 } else {
995 stage->fCustomStageKey = 0;
996 customStages[index] = NULL;
997 }
998}
999
1000}
1001
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001002void GrGpuGLShaders::buildProgram(GrPrimitiveType type,
1003 BlendOptFlags blendOpts,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001004 GrBlendCoeff dstCoeff,
1005 GrCustomStage** customStages) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001006 ProgramDesc& desc = fCurrentProgram.fProgramDesc;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001007 const GrDrawState& drawState = this->getDrawState();
junov@google.comf93e7172011-03-31 21:26:24 +00001008
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001009 // This should already have been caught
1010 GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts));
1011
1012 bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
1013
1014 bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag |
1015 kEmitCoverage_BlendOptFlag));
1016
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001017 // The descriptor is used as a cache key. Thus when a field of the
1018 // descriptor will not affect program generation (because of the vertex
1019 // layout in use or other descriptor field settings) it should be set
1020 // to a canonical value to avoid duplicate programs with different keys.
1021
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001022 // Must initialize all fields or cache will have false negatives!
bsalomon@google.come79c8152012-03-29 19:07:12 +00001023 desc.fVertexLayout = this->getVertexLayout();
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001024
1025 desc.fEmitsPointSize = kPoints_PrimitiveType == type;
1026
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001027 bool requiresAttributeColors =
1028 !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit);
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001029 bool requiresAttributeCoverage =
1030 !skipCoverage && SkToBool(desc.fVertexLayout &
1031 kCoverage_VertexLayoutBit);
1032
1033 // fColorInput/fCoverageInput records how colors are specified for the.
1034 // program. So we strip the bits from the layout to avoid false negatives
1035 // when searching for an existing program in the cache.
1036 desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit);
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001037
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001038 desc.fColorFilterXfermode = skipColor ?
1039 SkXfermode::kDst_Mode :
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001040 drawState.getColorFilterMode();
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001041
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001042 desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit);
1043
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001044 // no reason to do edge aa or look at per-vertex coverage if coverage is
1045 // ignored
1046 if (skipCoverage) {
1047 desc.fVertexLayout &= ~(kEdge_VertexLayoutBit |
1048 kCoverage_VertexLayoutBit);
1049 }
1050
1051 bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag);
1052 bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) ||
1053 (!requiresAttributeColors &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001054 0xffffffff == drawState.getColor());
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001055 if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001056 desc.fColorInput = ProgramDesc::kTransBlack_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001057 } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001058 desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001059 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001060 desc.fColorInput = ProgramDesc::kUniform_ColorInput;
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001061 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001062 desc.fColorInput = ProgramDesc::kAttribute_ColorInput;
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001063 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001064
1065 bool covIsSolidWhite = !requiresAttributeCoverage &&
1066 0xffffffff == drawState.getCoverage();
1067
1068 if (skipCoverage) {
1069 desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput;
1070 } else if (covIsSolidWhite) {
1071 desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput;
1072 } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) {
1073 desc.fCoverageInput = ProgramDesc::kUniform_ColorInput;
1074 } else {
1075 desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput;
1076 }
junov@google.comf93e7172011-03-31 21:26:24 +00001077
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001078 desc.fEdgeAANumEdges = skipCoverage ? 0 : drawState.getNumAAEdges();
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001079 desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001080 drawState.isConcaveEdgeAAState();
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +00001081
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001082 int lastEnabledStage = -1;
1083
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001084 if (!skipCoverage && (desc.fVertexLayout &
1085 GrDrawTarget::kEdge_VertexLayoutBit)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001086 desc.fVertexEdgeType = drawState.getVertexEdgeType();
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001087 } else {
1088 // use canonical value when not set to avoid cache misses
tomhudson@google.com93813632011-10-27 20:21:16 +00001089 desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001090 }
1091
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001092 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001093 StageDesc& stage = desc.fStages[s];
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001094
1095 stage.fOptFlags = 0;
1096 stage.setEnabled(this->isStageEnabled(s));
1097
1098 bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
1099 skipCoverage;
1100
1101 if (!skip && stage.isEnabled()) {
1102 lastEnabledStage = s;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001103 const GrGLTexture* texture =
1104 static_cast<const GrGLTexture*>(drawState.getTexture(s));
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001105 GrAssert(NULL != texture);
1106 const GrSamplerState& sampler = drawState.getSampler(s);
1107 // we matrix to invert when orientation is TopDown, so make sure
1108 // we aren't in that case before flagging as identity.
1109 if (TextureMatrixIsIdentity(texture, sampler)) {
1110 stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
1111 } else if (!sampler.getMatrix().hasPerspective()) {
1112 stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
1113 }
1114 switch (sampler.getSampleMode()) {
1115 case GrSamplerState::kNormal_SampleMode:
1116 stage.fCoordMapping = StageDesc::kIdentity_CoordMapping;
1117 break;
1118 case GrSamplerState::kRadial_SampleMode:
1119 stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping;
1120 break;
1121 case GrSamplerState::kRadial2_SampleMode:
1122 if (sampler.radial2IsDegenerate()) {
1123 stage.fCoordMapping =
1124 StageDesc::kRadial2GradientDegenerate_CoordMapping;
1125 } else {
1126 stage.fCoordMapping =
1127 StageDesc::kRadial2Gradient_CoordMapping;
1128 }
1129 break;
1130 case GrSamplerState::kSweep_SampleMode:
1131 stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping;
1132 break;
1133 default:
1134 GrCrash("Unexpected sample mode!");
1135 break;
1136 }
1137
1138 switch (sampler.getFilter()) {
1139 // these both can use a regular texture2D()
1140 case GrSamplerState::kNearest_Filter:
1141 case GrSamplerState::kBilinear_Filter:
1142 stage.fFetchMode = StageDesc::kSingle_FetchMode;
1143 break;
1144 // performs 4 texture2D()s
1145 case GrSamplerState::k4x4Downsample_Filter:
1146 stage.fFetchMode = StageDesc::k2x2_FetchMode;
1147 break;
1148 // performs fKernelWidth texture2D()s
1149 case GrSamplerState::kConvolution_Filter:
1150 stage.fFetchMode = StageDesc::kConvolution_FetchMode;
1151 break;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001152 case GrSamplerState::kDilate_Filter:
1153 stage.fFetchMode = StageDesc::kDilate_FetchMode;
1154 break;
1155 case GrSamplerState::kErode_Filter:
1156 stage.fFetchMode = StageDesc::kErode_FetchMode;
1157 break;
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001158 default:
1159 GrCrash("Unexpected filter!");
1160 break;
1161 }
1162
1163 if (sampler.hasTextureDomain()) {
1164 GrAssert(GrSamplerState::kClamp_WrapMode ==
1165 sampler.getWrapX() &&
1166 GrSamplerState::kClamp_WrapMode ==
1167 sampler.getWrapY());
1168 stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit;
1169 }
1170
1171 stage.fInConfigFlags = 0;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001172 if (!this->glCaps().textureSwizzleSupport()) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001173 if (GrPixelConfigIsAlphaOnly(texture->config())) {
1174 // if we don't have texture swizzle support then
robertphillips@google.com443e5a52012-04-30 20:01:21 +00001175 // the shader must smear the single channel after
1176 // reading the texture
1177 if (this->glCaps().textureRedSupport()) {
1178 // we can use R8 textures so use kSmearRed
1179 stage.fInConfigFlags |=
1180 StageDesc::kSmearRed_InConfigFlag;
1181 } else {
1182 // we can use A8 textures so use kSmearAlpha
1183 stage.fInConfigFlags |=
1184 StageDesc::kSmearAlpha_InConfigFlag;
1185 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001186 } else if (sampler.swapsRAndB()) {
1187 stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag;
1188 }
1189 }
1190 if (GrPixelConfigIsUnpremultiplied(texture->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001191 // The shader generator assumes that color channels are bytes
1192 // when rounding.
1193 GrAssert(4 == GrBytesPerPixel(texture->config()));
1194 if (kUpOnWrite_DownOnRead_UnpremulConversion ==
1195 fUnpremulConversion) {
1196 stage.fInConfigFlags |=
1197 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
1198 } else {
1199 stage.fInConfigFlags |=
1200 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag;
1201 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001202 }
1203
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001204 if (sampler.getFilter() == GrSamplerState::kConvolution_Filter ||
1205 sampler.getFilter() == GrSamplerState::kDilate_Filter ||
1206 sampler.getFilter() == GrSamplerState::kErode_Filter) {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001207 stage.fKernelWidth = sampler.getKernelWidth();
1208 } else {
1209 stage.fKernelWidth = 0;
1210 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001211
1212 setup_custom_stage(&stage, sampler, customStages,
1213 &fCurrentProgram, s);
1214
junov@google.comf93e7172011-03-31 21:26:24 +00001215 } else {
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001216 stage.fOptFlags = 0;
1217 stage.fCoordMapping = (StageDesc::CoordMapping) 0;
1218 stage.fInConfigFlags = 0;
1219 stage.fFetchMode = (StageDesc::FetchMode) 0;
1220 stage.fKernelWidth = 0;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001221 stage.fCustomStageKey = 0;
1222 customStages[s] = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +00001223 }
1224 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001225
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001226 if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001227 // The shader generator assumes that color channels are bytes
1228 // when rounding.
1229 GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config()));
1230 if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) {
1231 desc.fOutputConfig =
1232 ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig;
1233 } else {
1234 desc.fOutputConfig =
1235 ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig;
1236 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001237 } else {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001238 desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001239 }
1240
bsalomon@google.com1e257a52011-07-06 19:52:16 +00001241 desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001242
1243 // currently the experimental GS will only work with triangle prims
1244 // (and it doesn't do anything other than pass through values from
1245 // the VS to the FS anyway).
1246#if 0 && GR_GL_EXPERIMENTAL_GS
1247 desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport;
1248#endif
1249
bsalomon@google.coma3108262011-10-10 14:08:47 +00001250 // we want to avoid generating programs with different "first cov stage"
1251 // values when they would compute the same result.
1252 // We set field in the desc to kNumStages when either there are no
1253 // coverage stages or the distinction between coverage and color is
1254 // immaterial.
bsalomon@google.com88939ae2011-12-14 15:58:11 +00001255 int firstCoverageStage = GrDrawState::kNumStages;
tomhudson@google.com93813632011-10-27 20:21:16 +00001256 desc.fFirstCoverageStage = GrDrawState::kNumStages;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001257 bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage;
bsalomon@google.coma3108262011-10-10 14:08:47 +00001258 if (hasCoverage) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001259 firstCoverageStage = drawState.getFirstCoverageStage();
bsalomon@google.coma3108262011-10-10 14:08:47 +00001260 }
1261
1262 // other coverage inputs
1263 if (!hasCoverage) {
1264 hasCoverage =
1265 desc.fEdgeAANumEdges ||
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}