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