junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 8 | #include "GrGpuGL.h" |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 10 | #include "GrBinHashKey.h" |
| 11 | #include "effects/GrConvolutionEffect.h" |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 12 | #include "GrCustomStage.h" |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 13 | #include "GrGLProgramStage.h" |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 14 | #include "GrGLSL.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 15 | #include "GrGpuVertex.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 16 | #include "GrNoncopyable.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 17 | #include "GrProgramStageFactory.h" |
| 18 | #include "GrRandom.h" |
| 19 | #include "GrStringBuilder.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 20 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 21 | #define SKIP_CACHE_CHECK true |
| 22 | #define GR_UINT32_MAX static_cast<uint32_t>(-1) |
| 23 | |
tomhudson@google.com | dd182cb | 2012-02-10 21:01:00 +0000 | [diff] [blame] | 24 | #include "../GrTHashCache.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 25 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 26 | class GrGpuGL::ProgramCache : public ::GrNoncopyable { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 27 | private: |
| 28 | class Entry; |
| 29 | |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 30 | typedef GrBinHashKey<Entry, GrGLProgram::kProgramKeySize> ProgramHashKey; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 31 | |
| 32 | class Entry : public ::GrNoncopyable { |
| 33 | public: |
| 34 | Entry() {} |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 35 | void copyAndTakeOwnership(Entry& entry) { |
| 36 | fProgramData.copyAndTakeOwnership(entry.fProgramData); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 37 | fKey = entry.fKey; // ownership transfer |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 38 | fLRUStamp = entry.fLRUStamp; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 39 | } |
| 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.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 52 | // We may have kMaxEntries+1 shaders in the GL context because |
| 53 | // we create a new shader before evicting from the cache. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 54 | enum { |
| 55 | kMaxEntries = 32 |
| 56 | }; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 57 | Entry fEntries[kMaxEntries]; |
| 58 | int fCount; |
| 59 | unsigned int fCurrLRUStamp; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 60 | const GrGLContextInfo& fGL; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 61 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 62 | public: |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 63 | ProgramCache(const GrGLContextInfo& gl) |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 64 | : fCount(0) |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 65 | , fCurrLRUStamp(0) |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 66 | , fGL(gl) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | ~ProgramCache() { |
| 70 | for (int i = 0; i < fCount; ++i) { |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 71 | GrGpuGL::DeleteProgram(fGL.interface(), |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 72 | &fEntries[i].fProgramData); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 73 | } |
| 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.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 87 | GrGLProgram::CachedData* getProgramData(const GrGLProgram& desc, |
| 88 | GrCustomStage** stages) { |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 89 | Entry newEntry; |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 90 | newEntry.fKey.setKeyData(desc.keyData()); |
| 91 | |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 92 | Entry* entry = fHashCache.find(newEntry.fKey); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 93 | if (NULL == entry) { |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 94 | if (!desc.genProgram(fGL, stages, &newEntry.fProgramData)) { |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 95 | return NULL; |
| 96 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 97 | 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.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 109 | GrGpuGL::DeleteProgram(fGL.interface(), |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 110 | &entry->fProgramData); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 111 | } |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 112 | entry->copyAndTakeOwnership(newEntry); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 113 | 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 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 128 | void GrGpuGL::DeleteProgram(const GrGLInterface* gl, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 129 | CachedData* programData) { |
| 130 | GR_GL_CALL(gl, DeleteShader(programData->fVShaderID)); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 131 | if (programData->fGShaderID) { |
| 132 | GR_GL_CALL(gl, DeleteShader(programData->fGShaderID)); |
| 133 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 134 | GR_GL_CALL(gl, DeleteShader(programData->fFShaderID)); |
| 135 | GR_GL_CALL(gl, DeleteProgram(programData->fProgramID)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 136 | GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));) |
| 137 | } |
| 138 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 139 | //////////////////////////////////////////////////////////////////////////////// |
| 140 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 141 | void GrGpuGL::createProgramCache() { |
| 142 | fProgramData = NULL; |
| 143 | fProgramCache = new ProgramCache(this->glContextInfo()); |
| 144 | } |
| 145 | |
| 146 | void GrGpuGL::deleteProgramCache() { |
| 147 | delete fProgramCache; |
| 148 | fProgramCache = NULL; |
| 149 | fProgramData = NULL; |
| 150 | } |
| 151 | |
| 152 | void GrGpuGL::abandonResources(){ |
| 153 | INHERITED::abandonResources(); |
| 154 | fProgramCache->abandon(); |
| 155 | fHWProgramID = 0; |
| 156 | } |
| 157 | |
| 158 | //////////////////////////////////////////////////////////////////////////////// |
| 159 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 160 | #define GL_CALL(X) GR_GL_CALL(this->glInterface(), X) |
| 161 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 162 | namespace { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 163 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 164 | // GrRandoms nextU() values have patterns in the low bits |
| 165 | // So using nextU() % array_count might never take some values. |
| 166 | int random_int(GrRandom* r, int count) { |
| 167 | return (int)(r->nextF() * count); |
| 168 | } |
| 169 | |
| 170 | // min is inclusive, max is exclusive |
| 171 | int random_int(GrRandom* r, int min, int max) { |
| 172 | return (int)(r->nextF() * (max-min)) + min; |
| 173 | } |
| 174 | |
| 175 | bool random_bool(GrRandom* r) { |
| 176 | return r->nextF() > .5f; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | } |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 180 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 181 | bool GrGpuGL::programUnitTest() { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 182 | |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 183 | GrGLSLGeneration glslGeneration = |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 184 | GrGetGLSLGeneration(this->glBinding(), this->glInterface()); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 185 | static const int STAGE_OPTS[] = { |
| 186 | 0, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 187 | StageDesc::kNoPerspective_OptFlagBit, |
| 188 | StageDesc::kIdentity_CoordMapping |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 189 | }; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 190 | static const int IN_CONFIG_FLAGS[] = { |
| 191 | StageDesc::kNone_InConfigFlag, |
| 192 | StageDesc::kSwapRAndB_InConfigFlag, |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 193 | StageDesc::kSwapRAndB_InConfigFlag | |
| 194 | StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag, |
| 195 | StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag, |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 196 | StageDesc::kSmearAlpha_InConfigFlag, |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 197 | StageDesc::kSmearRed_InConfigFlag, |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 198 | }; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 199 | GrGLProgram program; |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 200 | ProgramDesc& pdesc = program.fProgramDesc; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 201 | |
| 202 | static const int NUM_TESTS = 512; |
| 203 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 204 | GrRandom random; |
| 205 | for (int t = 0; t < NUM_TESTS; ++t) { |
| 206 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 207 | #if 0 |
| 208 | GrPrintf("\nTest Program %d\n-------------\n", t); |
| 209 | static const int stop = -1; |
| 210 | if (t == stop) { |
| 211 | int breakpointhere = 9; |
| 212 | } |
| 213 | #endif |
| 214 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 215 | pdesc.fVertexLayout = 0; |
| 216 | pdesc.fEmitsPointSize = random.nextF() > .5f; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 217 | pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 218 | pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 219 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 220 | pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 221 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 222 | pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 223 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 224 | pdesc.fVertexLayout |= random_bool(&random) ? |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 225 | GrDrawTarget::kCoverage_VertexLayoutBit : |
| 226 | 0; |
| 227 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 228 | #if GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | 6f92f18 | 2011-09-29 15:05:48 +0000 | [diff] [blame] | 229 | pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport && |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 230 | random_bool(&random); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 231 | #endif |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 232 | pdesc.fOutputConfig = random_int(&random, ProgramDesc::kOutputConfigCnt); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 233 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 234 | bool edgeAA = random_bool(&random); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 235 | if (edgeAA) { |
bsalomon@google.com | 7ffe681 | 2012-05-11 17:32:43 +0000 | [diff] [blame] | 236 | pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit; |
| 237 | if (this->getCaps().fShaderDerivativeSupport) { |
| 238 | pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 239 | } else { |
bsalomon@google.com | 7ffe681 | 2012-05-11 17:32:43 +0000 | [diff] [blame] | 240 | pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 241 | } |
| 242 | } else { |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 243 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 244 | |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 245 | pdesc.fColorMatrixEnabled = random_bool(&random); |
| 246 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 247 | if (this->getCaps().fDualSourceBlendingSupport) { |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 248 | pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 249 | } else { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 250 | pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 251 | } |
| 252 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 253 | GrCustomStage* customStages[GrDrawState::kNumStages]; |
| 254 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 255 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 256 | // enable the stage? |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 257 | if (random_bool(&random)) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 258 | // use separate tex coords? |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 259 | if (random_bool(&random)) { |
| 260 | int t = random_int(&random, GrDrawState::kMaxTexCoords); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 261 | pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t); |
| 262 | } else { |
| 263 | pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s); |
| 264 | } |
| 265 | } |
| 266 | // use text-formatted verts? |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 267 | if (random_bool(&random)) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 268 | pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit; |
| 269 | } |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 270 | StageDesc& stage = pdesc.fStages[s]; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 271 | |
| 272 | stage.fCustomStageKey = 0; |
| 273 | customStages[s] = NULL; |
| 274 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 275 | stage.fOptFlags = STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))]; |
| 276 | stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))]; |
| 277 | stage.fCoordMapping = random_int(&random, StageDesc::kCoordMappingCnt); |
| 278 | stage.fFetchMode = random_int(&random, StageDesc::kFetchModeCnt); |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 279 | // convolution shaders don't work with persp tex matrix |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 280 | if (stage.fFetchMode == StageDesc::kConvolution_FetchMode || |
| 281 | stage.fFetchMode == StageDesc::kDilate_FetchMode || |
| 282 | stage.fFetchMode == StageDesc::kErode_FetchMode) { |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 283 | stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit; |
| 284 | } |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 285 | stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout)); |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 286 | static const uint32_t kMulByAlphaMask = |
| 287 | StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag | |
| 288 | StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag; |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 289 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 290 | switch (stage.fFetchMode) { |
| 291 | case StageDesc::kSingle_FetchMode: |
| 292 | stage.fKernelWidth = 0; |
| 293 | break; |
| 294 | case StageDesc::kConvolution_FetchMode: |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 295 | case StageDesc::kDilate_FetchMode: |
| 296 | case StageDesc::kErode_FetchMode: |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 297 | stage.fKernelWidth = random_int(&random, 2, 8); |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 298 | stage.fInConfigFlags &= ~kMulByAlphaMask; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 299 | break; |
| 300 | case StageDesc::k2x2_FetchMode: |
| 301 | stage.fKernelWidth = 0; |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 302 | stage.fInConfigFlags &= ~kMulByAlphaMask; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 303 | break; |
| 304 | } |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 305 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 306 | // TODO: is there a more elegant way to express this? |
| 307 | if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) { |
| 308 | int direction = random_int(&random, 2); |
tomhudson@google.com | f1d8806 | 2012-05-10 12:43:21 +0000 | [diff] [blame] | 309 | float kernel[MAX_KERNEL_WIDTH]; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 310 | for (int i = 0; i < stage.fKernelWidth; i++) { |
| 311 | kernel[i] = random.nextF(); |
| 312 | } |
| 313 | customStages[s] = new GrConvolutionEffect( |
| 314 | (GrSamplerState::FilterDirection)direction, |
| 315 | stage.fKernelWidth, kernel); |
| 316 | stage.fCustomStageKey = |
bsalomon@google.com | 16fd21b | 2012-05-21 21:18:13 +0000 | [diff] [blame] | 317 | customStages[s]->getFactory().glStageKey(customStages[s]); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 318 | } |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 319 | } |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 320 | CachedData cachedData; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 321 | if (!program.genProgram(this->glContextInfo(), customStages, |
| 322 | &cachedData)) { |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 323 | return false; |
| 324 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 325 | DeleteProgram(this->glInterface(), &cachedData); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 326 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 327 | return true; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 328 | } |
| 329 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 330 | void GrGpuGL::flushViewMatrix() { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 331 | const GrMatrix& vm = this->getDrawState().getViewMatrix(); |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 332 | if (!fProgramData->fViewMatrix.cheapEqualTo(vm)) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 333 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 334 | const GrRenderTarget* rt = this->getDrawState().getRenderTarget(); |
| 335 | GrAssert(NULL != rt); |
| 336 | GrMatrix m; |
| 337 | m.setAll( |
| 338 | GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1, |
| 339 | 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1, |
| 340 | 0, 0, GrMatrix::I()[8]); |
| 341 | m.setConcat(m, vm); |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 342 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 343 | // ES doesn't allow you to pass true to the transpose param, |
| 344 | // so do our own transpose |
| 345 | GrGLfloat mt[] = { |
| 346 | GrScalarToFloat(m[GrMatrix::kMScaleX]), |
| 347 | GrScalarToFloat(m[GrMatrix::kMSkewY]), |
| 348 | GrScalarToFloat(m[GrMatrix::kMPersp0]), |
| 349 | GrScalarToFloat(m[GrMatrix::kMSkewX]), |
| 350 | GrScalarToFloat(m[GrMatrix::kMScaleY]), |
| 351 | GrScalarToFloat(m[GrMatrix::kMPersp1]), |
| 352 | GrScalarToFloat(m[GrMatrix::kMTransX]), |
| 353 | GrScalarToFloat(m[GrMatrix::kMTransY]), |
| 354 | GrScalarToFloat(m[GrMatrix::kMPersp2]) |
| 355 | }; |
| 356 | |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 357 | GrAssert(GrGLProgram::kUnusedUniform != |
| 358 | fProgramData->fUniLocations.fViewMatrixUni); |
| 359 | GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni, |
| 360 | 1, false, mt)); |
| 361 | fProgramData->fViewMatrix = vm; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 362 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 363 | } |
| 364 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 365 | void GrGpuGL::flushTextureDomain(int s) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 366 | const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 367 | const GrDrawState& drawState = this->getDrawState(); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 368 | if (GrGLProgram::kUnusedUniform != uni) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 369 | const GrRect &texDom = drawState.getSampler(s).getTextureDomain(); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 370 | |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 371 | if (((1 << s) & fDirtyFlags.fTextureChangedMask) || |
| 372 | fProgramData->fTextureDomain[s] != texDom) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 373 | |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 374 | fProgramData->fTextureDomain[s] = texDom; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 375 | |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 376 | float values[4] = { |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 377 | GrScalarToFloat(texDom.left()), |
| 378 | GrScalarToFloat(texDom.top()), |
| 379 | GrScalarToFloat(texDom.right()), |
| 380 | GrScalarToFloat(texDom.bottom()) |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 381 | }; |
| 382 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 383 | const GrGLTexture* texture = |
| 384 | static_cast<const GrGLTexture*>(drawState.getTexture(s)); |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 385 | GrGLTexture::Orientation orientation = texture->orientation(); |
| 386 | |
| 387 | // vertical flip if necessary |
| 388 | if (GrGLTexture::kBottomUp_Orientation == orientation) { |
| 389 | values[1] = 1.0f - values[1]; |
| 390 | values[3] = 1.0f - values[3]; |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 391 | // The top and bottom were just flipped, so correct the ordering |
| 392 | // of elements so that values = (l, t, r, b). |
| 393 | SkTSwap(values[1], values[3]); |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 394 | } |
| 395 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 396 | GL_CALL(Uniform4fv(uni, 1, values)); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 397 | } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 401 | void GrGpuGL::flushTextureMatrix(int s) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 402 | const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 403 | const GrDrawState& drawState = this->getDrawState(); |
| 404 | const GrGLTexture* texture = |
| 405 | static_cast<const GrGLTexture*>(drawState.getTexture(s)); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 406 | if (NULL != texture) { |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 407 | const GrMatrix& hwMatrix = fProgramData->fTextureMatrices[s]; |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 408 | const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 409 | if (GrGLProgram::kUnusedUniform != uni && |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 410 | (((1 << s) & fDirtyFlags.fTextureChangedMask) || |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 411 | !hwMatrix.cheapEqualTo(samplerMatrix))) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 412 | |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 413 | GrMatrix m = samplerMatrix; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 414 | GrSamplerState::SampleMode mode = |
| 415 | drawState.getSampler(s).getSampleMode(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 416 | AdjustTextureMatrix(texture, mode, &m); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 417 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 418 | // ES doesn't allow you to pass true to the transpose param, |
| 419 | // so do our own transpose |
bsalomon@google.com | 27a4dc4 | 2011-05-16 13:14:03 +0000 | [diff] [blame] | 420 | GrGLfloat mt[] = { |
| 421 | GrScalarToFloat(m[GrMatrix::kMScaleX]), |
| 422 | GrScalarToFloat(m[GrMatrix::kMSkewY]), |
| 423 | GrScalarToFloat(m[GrMatrix::kMPersp0]), |
| 424 | GrScalarToFloat(m[GrMatrix::kMSkewX]), |
| 425 | GrScalarToFloat(m[GrMatrix::kMScaleY]), |
| 426 | GrScalarToFloat(m[GrMatrix::kMPersp1]), |
| 427 | GrScalarToFloat(m[GrMatrix::kMTransX]), |
| 428 | GrScalarToFloat(m[GrMatrix::kMTransY]), |
| 429 | GrScalarToFloat(m[GrMatrix::kMPersp2]) |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 430 | }; |
bsalomon@google.com | 27a4dc4 | 2011-05-16 13:14:03 +0000 | [diff] [blame] | 431 | |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 432 | GL_CALL(UniformMatrix3fv(uni, 1, false, mt)); |
| 433 | fProgramData->fTextureMatrices[s] = samplerMatrix; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 436 | } |
| 437 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 438 | void GrGpuGL::flushRadial2(int s) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 439 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 440 | const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 441 | const GrSamplerState& sampler = this->getDrawState().getSampler(s); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 442 | if (GrGLProgram::kUnusedUniform != uni && |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 443 | (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() || |
| 444 | fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() || |
| 445 | fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 446 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 447 | GrScalar centerX1 = sampler.getRadial2CenterX1(); |
| 448 | GrScalar radius0 = sampler.getRadial2Radius0(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 449 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 450 | GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 451 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 452 | // when were in the degenerate (linear) case the second |
| 453 | // value will be INF but the program doesn't read it. (We |
| 454 | // use the same 6 uniforms even though we don't need them |
| 455 | // all in the linear case just to keep the code complexity |
| 456 | // down). |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 457 | float values[6] = { |
| 458 | GrScalarToFloat(a), |
bsalomon@google.com | 2cdfade | 2011-11-23 16:53:42 +0000 | [diff] [blame] | 459 | 1 / (2.f * GrScalarToFloat(a)), |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 460 | GrScalarToFloat(centerX1), |
| 461 | GrScalarToFloat(radius0), |
| 462 | GrScalarToFloat(GrMul(radius0, radius0)), |
| 463 | sampler.isRadial2PosRoot() ? 1.f : -1.f |
| 464 | }; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 465 | GL_CALL(Uniform1fv(uni, 6, values)); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 466 | fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1(); |
| 467 | fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0(); |
| 468 | fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot(); |
| 469 | } |
| 470 | } |
| 471 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 472 | void GrGpuGL::flushConvolution(int s) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 473 | const GrSamplerState& sampler = this->getDrawState().getSampler(s); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 474 | int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni; |
| 475 | if (GrGLProgram::kUnusedUniform != kernelUni) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 476 | GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(), |
| 477 | sampler.getKernel())); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 478 | } |
| 479 | int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni; |
| 480 | if (GrGLProgram::kUnusedUniform != imageIncrementUni) { |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 481 | const GrGLTexture* texture = |
| 482 | static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s)); |
| 483 | float imageIncrement[2] = { 0 }; |
| 484 | switch (sampler.getFilterDirection()) { |
| 485 | case GrSamplerState::kX_FilterDirection: |
| 486 | imageIncrement[0] = 1.0f / texture->width(); |
| 487 | break; |
| 488 | case GrSamplerState::kY_FilterDirection: |
| 489 | imageIncrement[1] = 1.0f / texture->height(); |
| 490 | break; |
| 491 | default: |
| 492 | GrCrash("Unknown filter direction."); |
| 493 | } |
| 494 | GL_CALL(Uniform2fv(imageIncrementUni, 1, imageIncrement)); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 498 | void GrGpuGL::flushTexelSize(int s) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 499 | const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 500 | if (GrGLProgram::kUnusedUniform != uni) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 501 | const GrGLTexture* texture = |
| 502 | static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s)); |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 503 | if (texture->width() != fProgramData->fTextureWidth[s] || |
| 504 | texture->height() != fProgramData->fTextureHeight[s]) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 505 | |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 506 | float texelSize[] = {1.f / texture->width(), |
| 507 | 1.f / texture->height()}; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 508 | GL_CALL(Uniform2fv(uni, 1, texelSize)); |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 509 | fProgramData->fTextureWidth[s] = texture->width(); |
| 510 | fProgramData->fTextureHeight[s] = texture->height(); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 511 | } |
| 512 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 513 | } |
| 514 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 515 | void GrGpuGL::flushColorMatrix() { |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 516 | const ProgramDesc& desc = fCurrentProgram.getDesc(); |
| 517 | int matrixUni = fProgramData->fUniLocations.fColorMatrixUni; |
| 518 | int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni; |
| 519 | if (GrGLProgram::kUnusedUniform != matrixUni |
| 520 | && GrGLProgram::kUnusedUniform != vecUni) { |
| 521 | const float* m = this->getDrawState().getColorMatrix(); |
| 522 | GrGLfloat mt[] = { |
| 523 | m[0], m[5], m[10], m[15], |
| 524 | m[1], m[6], m[11], m[16], |
| 525 | m[2], m[7], m[12], m[17], |
| 526 | m[3], m[8], m[13], m[18], |
| 527 | }; |
| 528 | static float scale = 1.0f / 255.0f; |
| 529 | GrGLfloat vec[] = { |
| 530 | m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale, |
| 531 | }; |
| 532 | GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt)); |
| 533 | GL_CALL(Uniform4fv(vecUni, 1, vec)); |
| 534 | } |
| 535 | } |
| 536 | |
Scroggo | 01b87ec | 2011-05-11 18:05:38 +0000 | [diff] [blame] | 537 | static const float ONE_OVER_255 = 1.f / 255.f; |
| 538 | |
| 539 | #define GR_COLOR_TO_VEC4(color) {\ |
| 540 | GrColorUnpackR(color) * ONE_OVER_255,\ |
| 541 | GrColorUnpackG(color) * ONE_OVER_255,\ |
| 542 | GrColorUnpackB(color) * ONE_OVER_255,\ |
| 543 | GrColorUnpackA(color) * ONE_OVER_255 \ |
| 544 | } |
| 545 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 546 | void GrGpuGL::flushColor(GrColor color) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 547 | const ProgramDesc& desc = fCurrentProgram.getDesc(); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 548 | const GrDrawState& drawState = this->getDrawState(); |
| 549 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 550 | if (this->getVertexLayout() & kColor_VertexLayoutBit) { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 551 | // color will be specified per-vertex as an attribute |
| 552 | // invalidate the const vertex attrib color |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 553 | fHWConstAttribColor = GrColor_ILLEGAL; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 554 | } else { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 555 | switch (desc.fColorInput) { |
| 556 | case ProgramDesc::kAttribute_ColorInput: |
bsalomon@google.com | 255fa16 | 2012-05-21 21:44:59 +0000 | [diff] [blame] | 557 | if (fHWConstAttribColor != color) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 558 | // OpenGL ES only supports the float varieties of |
| 559 | // glVertexAttrib |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 560 | float c[] = GR_COLOR_TO_VEC4(color); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 561 | GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(), |
| 562 | c)); |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 563 | fHWConstAttribColor = color; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 564 | } |
| 565 | break; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 566 | case ProgramDesc::kUniform_ColorInput: |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 567 | if (fProgramData->fColor != color) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 568 | // OpenGL ES doesn't support unsigned byte varieties of |
| 569 | // glUniform |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 570 | float c[] = GR_COLOR_TO_VEC4(color); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 571 | GrAssert(GrGLProgram::kUnusedUniform != |
| 572 | fProgramData->fUniLocations.fColorUni); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 573 | GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni, |
| 574 | 1, c)); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 575 | fProgramData->fColor = color; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 576 | } |
| 577 | break; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 578 | case ProgramDesc::kSolidWhite_ColorInput: |
| 579 | case ProgramDesc::kTransBlack_ColorInput: |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 580 | break; |
| 581 | default: |
| 582 | GrCrash("Unknown color type."); |
| 583 | } |
| 584 | } |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 585 | if (fProgramData->fUniLocations.fColorFilterUni |
| 586 | != GrGLProgram::kUnusedUniform |
| 587 | && fProgramData->fColorFilterColor |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 588 | != drawState.getColorFilterColor()) { |
| 589 | float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor()); |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 590 | GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c)); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 591 | fProgramData->fColorFilterColor = drawState.getColorFilterColor(); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 592 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 593 | } |
| 594 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 595 | void GrGpuGL::flushCoverage(GrColor coverage) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 596 | const ProgramDesc& desc = fCurrentProgram.getDesc(); |
| 597 | const GrDrawState& drawState = this->getDrawState(); |
| 598 | |
| 599 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 600 | if (this->getVertexLayout() & kCoverage_VertexLayoutBit) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 601 | // coverage will be specified per-vertex as an attribute |
| 602 | // invalidate the const vertex attrib coverage |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 603 | fHWConstAttribCoverage = GrColor_ILLEGAL; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 604 | } else { |
| 605 | switch (desc.fCoverageInput) { |
| 606 | case ProgramDesc::kAttribute_ColorInput: |
bsalomon@google.com | 255fa16 | 2012-05-21 21:44:59 +0000 | [diff] [blame] | 607 | if (fHWConstAttribCoverage != coverage) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 608 | // OpenGL ES only supports the float varieties of |
| 609 | // glVertexAttrib |
| 610 | float c[] = GR_COLOR_TO_VEC4(coverage); |
| 611 | GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(), |
| 612 | c)); |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 613 | fHWConstAttribCoverage = coverage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 614 | } |
| 615 | break; |
| 616 | case ProgramDesc::kUniform_ColorInput: |
| 617 | if (fProgramData->fCoverage != coverage) { |
| 618 | // OpenGL ES doesn't support unsigned byte varieties of |
| 619 | // glUniform |
| 620 | float c[] = GR_COLOR_TO_VEC4(coverage); |
| 621 | GrAssert(GrGLProgram::kUnusedUniform != |
| 622 | fProgramData->fUniLocations.fCoverageUni); |
| 623 | GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni, |
| 624 | 1, c)); |
| 625 | fProgramData->fCoverage = coverage; |
| 626 | } |
| 627 | break; |
| 628 | case ProgramDesc::kSolidWhite_ColorInput: |
| 629 | case ProgramDesc::kTransBlack_ColorInput: |
| 630 | break; |
| 631 | default: |
| 632 | GrCrash("Unknown coverage type."); |
| 633 | } |
| 634 | } |
| 635 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 636 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 637 | bool GrGpuGL::flushGraphicsState(GrPrimitiveType type) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 638 | if (!flushGLStateCommon(type)) { |
| 639 | return false; |
| 640 | } |
| 641 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 642 | const GrDrawState& drawState = this->getDrawState(); |
| 643 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 644 | if (fDirtyFlags.fRenderTargetChanged) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 645 | // we assume all shader matrices may be wrong after viewport changes |
| 646 | fProgramCache->invalidateViewMatrices(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 647 | } |
| 648 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 649 | GrBlendCoeff srcCoeff; |
| 650 | GrBlendCoeff dstCoeff; |
| 651 | BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff); |
| 652 | if (kSkipDraw_BlendOptFlag & blendOpts) { |
| 653 | return false; |
| 654 | } |
| 655 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 656 | GrCustomStage* customStages [GrDrawState::kNumStages]; |
| 657 | this->buildProgram(type, blendOpts, dstCoeff, customStages); |
| 658 | fProgramData = fProgramCache->getProgramData(fCurrentProgram, |
| 659 | customStages); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 660 | if (NULL == fProgramData) { |
| 661 | GrAssert(!"Failed to create program!"); |
| 662 | return false; |
| 663 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 664 | |
| 665 | if (fHWProgramID != fProgramData->fProgramID) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 666 | GL_CALL(UseProgram(fProgramData->fProgramID)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 667 | fHWProgramID = fProgramData->fProgramID; |
| 668 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 669 | fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff); |
| 670 | this->flushBlend(type, srcCoeff, dstCoeff); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 671 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 672 | GrColor color; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 673 | GrColor coverage; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 674 | if (blendOpts & kEmitTransBlack_BlendOptFlag) { |
| 675 | color = 0; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 676 | coverage = 0; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 677 | } else if (blendOpts & kEmitCoverage_BlendOptFlag) { |
| 678 | color = 0xffffffff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 679 | coverage = drawState.getCoverage(); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 680 | } else { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 681 | color = drawState.getColor(); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 682 | coverage = drawState.getCoverage(); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 683 | } |
| 684 | this->flushColor(color); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 685 | this->flushCoverage(coverage); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 686 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 687 | this->flushViewMatrix(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 688 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 689 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 690 | if (this->isStageEnabled(s)) { |
| 691 | this->flushTextureMatrix(s); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 692 | |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 693 | this->flushRadial2(s); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 694 | |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 695 | this->flushConvolution(s); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 696 | |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 697 | this->flushTexelSize(s); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 698 | |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 699 | this->flushTextureDomain(s); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 700 | |
| 701 | if (NULL != fProgramData->fCustomStage[s]) { |
| 702 | const GrSamplerState& sampler = |
| 703 | this->getDrawState().getSampler(s); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 704 | const GrGLTexture* texture = |
| 705 | static_cast<const GrGLTexture*>( |
| 706 | this->getDrawState().getTexture(s)); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 707 | fProgramData->fCustomStage[s]->setData( |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 708 | this->glInterface(), *texture, |
| 709 | sampler.getCustomStage(), s); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 710 | } |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 711 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 712 | } |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 713 | this->flushColorMatrix(); |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 714 | resetDirtyFlags(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 715 | return true; |
| 716 | } |
| 717 | |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 718 | #if GR_TEXT_SCALAR_IS_USHORT |
| 719 | #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT |
| 720 | #define TEXT_COORDS_ARE_NORMALIZED 1 |
| 721 | #elif GR_TEXT_SCALAR_IS_FLOAT |
| 722 | #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT |
| 723 | #define TEXT_COORDS_ARE_NORMALIZED 0 |
| 724 | #elif GR_TEXT_SCALAR_IS_FIXED |
| 725 | #define TEXT_COORDS_GL_TYPE GR_GL_FIXED |
| 726 | #define TEXT_COORDS_ARE_NORMALIZED 0 |
| 727 | #else |
| 728 | #error "unknown GR_TEXT_SCALAR type" |
| 729 | #endif |
| 730 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 731 | void GrGpuGL::setupGeometry(int* startVertex, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 732 | int* startIndex, |
| 733 | int vertexCount, |
| 734 | int indexCount) { |
| 735 | |
| 736 | int newColorOffset; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 737 | int newCoverageOffset; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 738 | int newTexCoordOffsets[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 739 | int newEdgeOffset; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 740 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 741 | GrVertexLayout currLayout = this->getVertexLayout(); |
| 742 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 743 | GrGLsizei newStride = VertexSizeAndOffsetsByIdx( |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 744 | currLayout, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 745 | newTexCoordOffsets, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 746 | &newColorOffset, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 747 | &newCoverageOffset, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 748 | &newEdgeOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 749 | int oldColorOffset; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 750 | int oldCoverageOffset; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 751 | int oldTexCoordOffsets[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 752 | int oldEdgeOffset; |
| 753 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 754 | GrGLsizei oldStride = VertexSizeAndOffsetsByIdx( |
| 755 | fHWGeometryState.fVertexLayout, |
| 756 | oldTexCoordOffsets, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 757 | &oldColorOffset, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 758 | &oldCoverageOffset, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 759 | &oldEdgeOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 760 | bool indexed = NULL != startIndex; |
| 761 | |
| 762 | int extraVertexOffset; |
| 763 | int extraIndexOffset; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 764 | this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 765 | |
| 766 | GrGLenum scalarType; |
| 767 | bool texCoordNorm; |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 768 | if (currLayout & kTextFormat_VertexLayoutBit) { |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 769 | scalarType = TEXT_COORDS_GL_TYPE; |
| 770 | texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 771 | } else { |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 772 | GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT); |
| 773 | scalarType = GR_GL_FLOAT; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 774 | texCoordNorm = false; |
| 775 | } |
| 776 | |
| 777 | size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride; |
| 778 | *startVertex = 0; |
| 779 | if (indexed) { |
| 780 | *startIndex += extraIndexOffset; |
| 781 | } |
| 782 | |
| 783 | // all the Pointers must be set if any of these are true |
| 784 | bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty || |
| 785 | vertexOffset != fHWGeometryState.fVertexOffset || |
| 786 | newStride != oldStride; |
| 787 | |
| 788 | // position and tex coord offsets change if above conditions are true |
| 789 | // or the type/normalization changed based on text vs nontext type coords. |
| 790 | bool posAndTexChange = allOffsetsChange || |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 791 | (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) && |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 792 | (kTextFormat_VertexLayoutBit & |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 793 | (fHWGeometryState.fVertexLayout ^ currLayout))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 794 | |
| 795 | if (posAndTexChange) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 796 | int idx = GrGLProgram::PositionAttributeIdx(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 797 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 798 | (GrGLvoid*)vertexOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 799 | fHWGeometryState.fVertexOffset = vertexOffset; |
| 800 | } |
| 801 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 802 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 803 | if (newTexCoordOffsets[t] > 0) { |
| 804 | GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 805 | int idx = GrGLProgram::TexCoordAttributeIdx(t); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 806 | if (oldTexCoordOffsets[t] <= 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 807 | GL_CALL(EnableVertexAttribArray(idx)); |
| 808 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 809 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 810 | } else if (posAndTexChange || |
| 811 | newTexCoordOffsets[t] != oldTexCoordOffsets[t]) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 812 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 813 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 814 | } |
| 815 | } else if (oldTexCoordOffsets[t] > 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 816 | GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | |
| 820 | if (newColorOffset > 0) { |
| 821 | GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 822 | int idx = GrGLProgram::ColorAttributeIdx(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 823 | if (oldColorOffset <= 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 824 | GL_CALL(EnableVertexAttribArray(idx)); |
| 825 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 826 | true, newStride, colorOffset)); |
| 827 | } else if (allOffsetsChange || newColorOffset != oldColorOffset) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 828 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 829 | true, newStride, colorOffset)); |
| 830 | } |
| 831 | } else if (oldColorOffset > 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 832 | GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx())); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 833 | } |
| 834 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 835 | if (newCoverageOffset > 0) { |
bsalomon@google.com | e10f6fd | 2011-10-11 20:15:26 +0000 | [diff] [blame] | 836 | GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 837 | int idx = GrGLProgram::CoverageAttributeIdx(); |
| 838 | if (oldCoverageOffset <= 0) { |
| 839 | GL_CALL(EnableVertexAttribArray(idx)); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 840 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 841 | true, newStride, coverageOffset)); |
| 842 | } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 843 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 844 | true, newStride, coverageOffset)); |
| 845 | } |
| 846 | } else if (oldCoverageOffset > 0) { |
| 847 | GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx())); |
| 848 | } |
| 849 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 850 | if (newEdgeOffset > 0) { |
| 851 | GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset); |
| 852 | int idx = GrGLProgram::EdgeAttributeIdx(); |
| 853 | if (oldEdgeOffset <= 0) { |
| 854 | GL_CALL(EnableVertexAttribArray(idx)); |
| 855 | GL_CALL(VertexAttribPointer(idx, 4, scalarType, |
| 856 | false, newStride, edgeOffset)); |
| 857 | } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) { |
| 858 | GL_CALL(VertexAttribPointer(idx, 4, scalarType, |
| 859 | false, newStride, edgeOffset)); |
| 860 | } |
| 861 | } else if (oldEdgeOffset > 0) { |
| 862 | GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx())); |
| 863 | } |
| 864 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 865 | fHWGeometryState.fVertexLayout = currLayout; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 866 | fHWGeometryState.fArrayPtrsDirty = false; |
| 867 | } |
| 868 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 869 | namespace { |
| 870 | |
| 871 | void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage, |
| 872 | const GrSamplerState& sampler, |
| 873 | GrCustomStage** customStages, |
| 874 | GrGLProgram* program, int index) { |
| 875 | GrCustomStage* customStage = sampler.getCustomStage(); |
| 876 | if (customStage) { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 877 | const GrProgramStageFactory& factory = customStage->getFactory(); |
bsalomon@google.com | 16fd21b | 2012-05-21 21:18:13 +0000 | [diff] [blame] | 878 | stage->fCustomStageKey = factory.glStageKey(customStage); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 879 | customStages[index] = customStage; |
| 880 | } else { |
| 881 | stage->fCustomStageKey = 0; |
| 882 | customStages[index] = NULL; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | } |
| 887 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame^] | 888 | void GrGpuGL::buildProgram(GrPrimitiveType type, |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 889 | BlendOptFlags blendOpts, |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 890 | GrBlendCoeff dstCoeff, |
| 891 | GrCustomStage** customStages) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 892 | ProgramDesc& desc = fCurrentProgram.fProgramDesc; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 893 | const GrDrawState& drawState = this->getDrawState(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 894 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 895 | // This should already have been caught |
| 896 | GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts)); |
| 897 | |
| 898 | bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag); |
| 899 | |
| 900 | bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag | |
| 901 | kEmitCoverage_BlendOptFlag)); |
| 902 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 903 | // The descriptor is used as a cache key. Thus when a field of the |
| 904 | // descriptor will not affect program generation (because of the vertex |
| 905 | // layout in use or other descriptor field settings) it should be set |
| 906 | // to a canonical value to avoid duplicate programs with different keys. |
| 907 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 908 | // Must initialize all fields or cache will have false negatives! |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 909 | desc.fVertexLayout = this->getVertexLayout(); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 910 | |
| 911 | desc.fEmitsPointSize = kPoints_PrimitiveType == type; |
| 912 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 913 | bool requiresAttributeColors = |
| 914 | !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 915 | bool requiresAttributeCoverage = |
| 916 | !skipCoverage && SkToBool(desc.fVertexLayout & |
| 917 | kCoverage_VertexLayoutBit); |
| 918 | |
| 919 | // fColorInput/fCoverageInput records how colors are specified for the. |
| 920 | // program. So we strip the bits from the layout to avoid false negatives |
| 921 | // when searching for an existing program in the cache. |
| 922 | desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 923 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 924 | desc.fColorFilterXfermode = skipColor ? |
| 925 | SkXfermode::kDst_Mode : |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 926 | drawState.getColorFilterMode(); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 927 | |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 928 | desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit); |
| 929 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 930 | // no reason to do edge aa or look at per-vertex coverage if coverage is |
| 931 | // ignored |
| 932 | if (skipCoverage) { |
| 933 | desc.fVertexLayout &= ~(kEdge_VertexLayoutBit | |
| 934 | kCoverage_VertexLayoutBit); |
| 935 | } |
| 936 | |
| 937 | bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag); |
| 938 | bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) || |
| 939 | (!requiresAttributeColors && |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 940 | 0xffffffff == drawState.getColor()); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 941 | if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 942 | desc.fColorInput = ProgramDesc::kTransBlack_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 943 | } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 944 | desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 945 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 946 | desc.fColorInput = ProgramDesc::kUniform_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 947 | } else { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 948 | desc.fColorInput = ProgramDesc::kAttribute_ColorInput; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 949 | } |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 950 | |
| 951 | bool covIsSolidWhite = !requiresAttributeCoverage && |
| 952 | 0xffffffff == drawState.getCoverage(); |
| 953 | |
| 954 | if (skipCoverage) { |
| 955 | desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput; |
| 956 | } else if (covIsSolidWhite) { |
| 957 | desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput; |
| 958 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) { |
| 959 | desc.fCoverageInput = ProgramDesc::kUniform_ColorInput; |
| 960 | } else { |
| 961 | desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput; |
| 962 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 963 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 964 | int lastEnabledStage = -1; |
| 965 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 966 | if (!skipCoverage && (desc.fVertexLayout & |
| 967 | GrDrawTarget::kEdge_VertexLayoutBit)) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 968 | desc.fVertexEdgeType = drawState.getVertexEdgeType(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 969 | } else { |
| 970 | // use canonical value when not set to avoid cache misses |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 971 | desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 972 | } |
| 973 | |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 974 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 975 | StageDesc& stage = desc.fStages[s]; |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 976 | |
| 977 | stage.fOptFlags = 0; |
| 978 | stage.setEnabled(this->isStageEnabled(s)); |
| 979 | |
| 980 | bool skip = s < drawState.getFirstCoverageStage() ? skipColor : |
| 981 | skipCoverage; |
| 982 | |
| 983 | if (!skip && stage.isEnabled()) { |
| 984 | lastEnabledStage = s; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 985 | const GrGLTexture* texture = |
| 986 | static_cast<const GrGLTexture*>(drawState.getTexture(s)); |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 987 | GrAssert(NULL != texture); |
| 988 | const GrSamplerState& sampler = drawState.getSampler(s); |
| 989 | // we matrix to invert when orientation is TopDown, so make sure |
| 990 | // we aren't in that case before flagging as identity. |
| 991 | if (TextureMatrixIsIdentity(texture, sampler)) { |
| 992 | stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit; |
| 993 | } else if (!sampler.getMatrix().hasPerspective()) { |
| 994 | stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit; |
| 995 | } |
| 996 | switch (sampler.getSampleMode()) { |
| 997 | case GrSamplerState::kNormal_SampleMode: |
| 998 | stage.fCoordMapping = StageDesc::kIdentity_CoordMapping; |
| 999 | break; |
| 1000 | case GrSamplerState::kRadial_SampleMode: |
| 1001 | stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping; |
| 1002 | break; |
| 1003 | case GrSamplerState::kRadial2_SampleMode: |
| 1004 | if (sampler.radial2IsDegenerate()) { |
| 1005 | stage.fCoordMapping = |
| 1006 | StageDesc::kRadial2GradientDegenerate_CoordMapping; |
| 1007 | } else { |
| 1008 | stage.fCoordMapping = |
| 1009 | StageDesc::kRadial2Gradient_CoordMapping; |
| 1010 | } |
| 1011 | break; |
| 1012 | case GrSamplerState::kSweep_SampleMode: |
| 1013 | stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping; |
| 1014 | break; |
| 1015 | default: |
| 1016 | GrCrash("Unexpected sample mode!"); |
| 1017 | break; |
| 1018 | } |
| 1019 | |
| 1020 | switch (sampler.getFilter()) { |
| 1021 | // these both can use a regular texture2D() |
| 1022 | case GrSamplerState::kNearest_Filter: |
| 1023 | case GrSamplerState::kBilinear_Filter: |
| 1024 | stage.fFetchMode = StageDesc::kSingle_FetchMode; |
| 1025 | break; |
| 1026 | // performs 4 texture2D()s |
| 1027 | case GrSamplerState::k4x4Downsample_Filter: |
| 1028 | stage.fFetchMode = StageDesc::k2x2_FetchMode; |
| 1029 | break; |
| 1030 | // performs fKernelWidth texture2D()s |
| 1031 | case GrSamplerState::kConvolution_Filter: |
| 1032 | stage.fFetchMode = StageDesc::kConvolution_FetchMode; |
| 1033 | break; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 1034 | case GrSamplerState::kDilate_Filter: |
| 1035 | stage.fFetchMode = StageDesc::kDilate_FetchMode; |
| 1036 | break; |
| 1037 | case GrSamplerState::kErode_Filter: |
| 1038 | stage.fFetchMode = StageDesc::kErode_FetchMode; |
| 1039 | break; |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 1040 | default: |
| 1041 | GrCrash("Unexpected filter!"); |
| 1042 | break; |
| 1043 | } |
| 1044 | |
| 1045 | if (sampler.hasTextureDomain()) { |
| 1046 | GrAssert(GrSamplerState::kClamp_WrapMode == |
| 1047 | sampler.getWrapX() && |
| 1048 | GrSamplerState::kClamp_WrapMode == |
| 1049 | sampler.getWrapY()); |
| 1050 | stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit; |
| 1051 | } |
| 1052 | |
| 1053 | stage.fInConfigFlags = 0; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1054 | if (!this->glCaps().textureSwizzleSupport()) { |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 1055 | if (GrPixelConfigIsAlphaOnly(texture->config())) { |
| 1056 | // if we don't have texture swizzle support then |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 1057 | // the shader must smear the single channel after |
| 1058 | // reading the texture |
| 1059 | if (this->glCaps().textureRedSupport()) { |
| 1060 | // we can use R8 textures so use kSmearRed |
| 1061 | stage.fInConfigFlags |= |
| 1062 | StageDesc::kSmearRed_InConfigFlag; |
| 1063 | } else { |
| 1064 | // we can use A8 textures so use kSmearAlpha |
| 1065 | stage.fInConfigFlags |= |
| 1066 | StageDesc::kSmearAlpha_InConfigFlag; |
| 1067 | } |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 1068 | } else if (sampler.swapsRAndB()) { |
| 1069 | stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag; |
| 1070 | } |
| 1071 | } |
| 1072 | if (GrPixelConfigIsUnpremultiplied(texture->config())) { |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1073 | // The shader generator assumes that color channels are bytes |
| 1074 | // when rounding. |
| 1075 | GrAssert(4 == GrBytesPerPixel(texture->config())); |
| 1076 | if (kUpOnWrite_DownOnRead_UnpremulConversion == |
| 1077 | fUnpremulConversion) { |
| 1078 | stage.fInConfigFlags |= |
| 1079 | StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag; |
| 1080 | } else { |
| 1081 | stage.fInConfigFlags |= |
| 1082 | StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag; |
| 1083 | } |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 1086 | if (sampler.getFilter() == GrSamplerState::kDilate_Filter || |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 1087 | sampler.getFilter() == GrSamplerState::kErode_Filter) { |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 1088 | stage.fKernelWidth = sampler.getKernelWidth(); |
| 1089 | } else { |
| 1090 | stage.fKernelWidth = 0; |
| 1091 | } |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1092 | |
| 1093 | setup_custom_stage(&stage, sampler, customStages, |
| 1094 | &fCurrentProgram, s); |
| 1095 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1096 | } else { |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 1097 | stage.fOptFlags = 0; |
| 1098 | stage.fCoordMapping = (StageDesc::CoordMapping) 0; |
| 1099 | stage.fInConfigFlags = 0; |
| 1100 | stage.fFetchMode = (StageDesc::FetchMode) 0; |
| 1101 | stage.fKernelWidth = 0; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1102 | stage.fCustomStageKey = 0; |
| 1103 | customStages[s] = NULL; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1104 | } |
| 1105 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1106 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1107 | if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) { |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1108 | // The shader generator assumes that color channels are bytes |
| 1109 | // when rounding. |
| 1110 | GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config())); |
| 1111 | if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) { |
| 1112 | desc.fOutputConfig = |
| 1113 | ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig; |
| 1114 | } else { |
| 1115 | desc.fOutputConfig = |
| 1116 | ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig; |
| 1117 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1118 | } else { |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1119 | desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 1122 | desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1123 | |
| 1124 | // currently the experimental GS will only work with triangle prims |
| 1125 | // (and it doesn't do anything other than pass through values from |
| 1126 | // the VS to the FS anyway). |
| 1127 | #if 0 && GR_GL_EXPERIMENTAL_GS |
| 1128 | desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport; |
| 1129 | #endif |
| 1130 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1131 | // we want to avoid generating programs with different "first cov stage" |
| 1132 | // values when they would compute the same result. |
| 1133 | // We set field in the desc to kNumStages when either there are no |
| 1134 | // coverage stages or the distinction between coverage and color is |
| 1135 | // immaterial. |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 1136 | int firstCoverageStage = GrDrawState::kNumStages; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1137 | desc.fFirstCoverageStage = GrDrawState::kNumStages; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1138 | bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1139 | if (hasCoverage) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1140 | firstCoverageStage = drawState.getFirstCoverageStage(); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | // other coverage inputs |
| 1144 | if (!hasCoverage) { |
| 1145 | hasCoverage = |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 1146 | requiresAttributeCoverage || |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1147 | (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit); |
| 1148 | } |
| 1149 | |
| 1150 | if (hasCoverage) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1151 | // color filter is applied between color/coverage computation |
| 1152 | if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1153 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1156 | if (this->getCaps().fDualSourceBlendingSupport && |
| 1157 | !(blendOpts & (kEmitCoverage_BlendOptFlag | |
| 1158 | kCoverageAsAlpha_BlendOptFlag))) { |
| 1159 | if (kZero_BlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1160 | // write the coverage value to second color |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 1161 | desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1162 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1163 | } else if (kSA_BlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1164 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially |
| 1165 | // cover |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 1166 | desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1167 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1168 | } else if (kSC_BlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1169 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially |
| 1170 | // cover |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 1171 | desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1172 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1176 | } |