epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 10 | #include "GrBinHashKey.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 11 | #include "GrGLProgram.h" |
| 12 | #include "GrGpuGLShaders.h" |
| 13 | #include "GrGpuVertex.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 14 | #include "GrNoncopyable.h" |
| 15 | #include "GrStringBuilder.h" |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 16 | #include "GrRandom.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 17 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 18 | #define SKIP_CACHE_CHECK true |
| 19 | #define GR_UINT32_MAX static_cast<uint32_t>(-1) |
| 20 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 21 | #include "GrTHashCache.h" |
| 22 | |
| 23 | class GrGpuGLShaders::ProgramCache : public ::GrNoncopyable { |
| 24 | private: |
| 25 | class Entry; |
| 26 | |
| 27 | #if GR_DEBUG |
| 28 | typedef GrBinHashKey<Entry, 4> ProgramHashKey; // Flex the dynamic allocation muscle in debug |
| 29 | #else |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 30 | typedef GrBinHashKey<Entry, 64> ProgramHashKey; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
| 33 | class Entry : public ::GrNoncopyable { |
| 34 | public: |
| 35 | Entry() {} |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 36 | void copyAndTakeOwnership(Entry& entry) { |
| 37 | fProgramData.copyAndTakeOwnership(entry.fProgramData); |
| 38 | fKey.copyAndTakeOwnership(entry.fKey); // ownership transfer |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 39 | fLRUStamp = entry.fLRUStamp; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | public: |
| 43 | int compare(const ProgramHashKey& key) const { return fKey.compare(key); } |
| 44 | |
| 45 | public: |
| 46 | GrGLProgram::CachedData fProgramData; |
| 47 | ProgramHashKey fKey; |
| 48 | unsigned int fLRUStamp; |
| 49 | }; |
| 50 | |
| 51 | GrTHashTable<Entry, ProgramHashKey, 8> fHashCache; |
| 52 | |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 53 | // We may have kMaxEntries+1 shaders in the GL context because |
| 54 | // we create a new shader before evicting from the cache. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 55 | enum { |
| 56 | kMaxEntries = 32 |
| 57 | }; |
| 58 | Entry fEntries[kMaxEntries]; |
| 59 | int fCount; |
| 60 | unsigned int fCurrLRUStamp; |
| 61 | |
| 62 | public: |
| 63 | ProgramCache() |
| 64 | : fCount(0) |
| 65 | , fCurrLRUStamp(0) { |
| 66 | } |
| 67 | |
| 68 | ~ProgramCache() { |
| 69 | for (int i = 0; i < fCount; ++i) { |
| 70 | GrGpuGLShaders::DeleteProgram(&fEntries[i].fProgramData); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void abandon() { |
| 75 | fCount = 0; |
| 76 | } |
| 77 | |
| 78 | void invalidateViewMatrices() { |
| 79 | for (int i = 0; i < fCount; ++i) { |
| 80 | // set to illegal matrix |
| 81 | fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix(); |
| 82 | } |
| 83 | } |
| 84 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 85 | GrGLProgram::CachedData* getProgramData(const GrGLProgram& desc) { |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 86 | Entry newEntry; |
| 87 | while (newEntry.fKey.doPass()) { |
| 88 | desc.buildKey(newEntry.fKey); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 89 | } |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 90 | Entry* entry = fHashCache.find(newEntry.fKey); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 91 | if (NULL == entry) { |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 92 | if (!desc.genProgram(&newEntry.fProgramData)) { |
| 93 | return NULL; |
| 94 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 95 | if (fCount < kMaxEntries) { |
| 96 | entry = fEntries + fCount; |
| 97 | ++fCount; |
| 98 | } else { |
| 99 | GrAssert(kMaxEntries == fCount); |
| 100 | entry = fEntries; |
| 101 | for (int i = 1; i < kMaxEntries; ++i) { |
| 102 | if (fEntries[i].fLRUStamp < entry->fLRUStamp) { |
| 103 | entry = fEntries + i; |
| 104 | } |
| 105 | } |
| 106 | fHashCache.remove(entry->fKey, entry); |
| 107 | GrGpuGLShaders::DeleteProgram(&entry->fProgramData); |
| 108 | } |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 109 | entry->copyAndTakeOwnership(newEntry); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 110 | fHashCache.insert(entry->fKey, entry); |
| 111 | } |
| 112 | |
| 113 | entry->fLRUStamp = fCurrLRUStamp; |
| 114 | if (GR_UINT32_MAX == fCurrLRUStamp) { |
| 115 | // wrap around! just trash our LRU, one time hit. |
| 116 | for (int i = 0; i < fCount; ++i) { |
| 117 | fEntries[i].fLRUStamp = 0; |
| 118 | } |
| 119 | } |
| 120 | ++fCurrLRUStamp; |
| 121 | return &entry->fProgramData; |
| 122 | } |
| 123 | }; |
| 124 | |
junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 125 | void GrGpuGLShaders::abandonResources(){ |
| 126 | INHERITED::abandonResources(); |
| 127 | fProgramCache->abandon(); |
| 128 | } |
| 129 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 130 | void GrGpuGLShaders::DeleteProgram(CachedData* programData) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 131 | GR_GL(DeleteShader(programData->fVShaderID)); |
| 132 | GR_GL(DeleteShader(programData->fFShaderID)); |
| 133 | GR_GL(DeleteProgram(programData->fProgramID)); |
| 134 | GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));) |
| 135 | } |
| 136 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 137 | //////////////////////////////////////////////////////////////////////////////// |
| 138 | |
| 139 | namespace { |
| 140 | template <typename T> |
| 141 | T random_val(GrRandom* r, T count) { |
| 142 | return (T)(int)(r->nextF() * count); |
| 143 | } |
| 144 | }; |
| 145 | |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 146 | bool GrGpuGLShaders::programUnitTest() { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 147 | |
| 148 | static const int STAGE_OPTS[] = { |
| 149 | 0, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 150 | StageDesc::kNoPerspective_OptFlagBit, |
| 151 | StageDesc::kIdentity_CoordMapping |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 152 | }; |
| 153 | GrGLProgram program; |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 154 | ProgramDesc& pdesc = program.fProgramDesc; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 155 | |
| 156 | static const int NUM_TESTS = 512; |
| 157 | |
| 158 | // GrRandoms nextU() values have patterns in the low bits |
| 159 | // So using nextU() % array_count might never take some values. |
| 160 | GrRandom random; |
| 161 | for (int t = 0; t < NUM_TESTS; ++t) { |
| 162 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 163 | #if 0 |
| 164 | GrPrintf("\nTest Program %d\n-------------\n", t); |
| 165 | static const int stop = -1; |
| 166 | if (t == stop) { |
| 167 | int breakpointhere = 9; |
| 168 | } |
| 169 | #endif |
| 170 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 171 | pdesc.fVertexLayout = 0; |
| 172 | pdesc.fEmitsPointSize = random.nextF() > .5f; |
| 173 | float colorType = random.nextF(); |
| 174 | if (colorType < 1.f / 3.f) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 175 | pdesc.fColorType = ProgramDesc::kAttribute_ColorType; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 176 | } else if (colorType < 2.f / 3.f) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 177 | pdesc.fColorType = ProgramDesc::kUniform_ColorType; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 178 | } else { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 179 | pdesc.fColorType = ProgramDesc::kNone_ColorType; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 180 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 181 | |
| 182 | int idx = (int)(random.nextF() * (SkXfermode::kCoeffModesCnt)); |
| 183 | pdesc.fColorFilterXfermode = (SkXfermode::Mode)idx; |
| 184 | |
| 185 | idx = (int)(random.nextF() * (kNumStages+1)); |
| 186 | pdesc.fFirstCoverageStage = idx; |
| 187 | |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 188 | bool edgeAA = random.nextF() > .5f; |
| 189 | if (edgeAA) { |
| 190 | pdesc.fEdgeAANumEdges = random.nextF() * this->getMaxEdges() + 1; |
| 191 | pdesc.fEdgeAAConcave = random.nextF() > .5f; |
| 192 | } else { |
| 193 | pdesc.fEdgeAANumEdges = 0; |
| 194 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 195 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 196 | if (fDualSourceBlendingSupport) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 197 | pdesc.fDualSrcOutput = |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 198 | (ProgramDesc::DualSrcOutput) |
| 199 | (int)(random.nextF() * ProgramDesc::kDualSrcOutputCnt); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 200 | } else { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 201 | pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 202 | } |
| 203 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 204 | for (int s = 0; s < kNumStages; ++s) { |
| 205 | // enable the stage? |
| 206 | if (random.nextF() > .5f) { |
| 207 | // use separate tex coords? |
| 208 | if (random.nextF() > .5f) { |
| 209 | int t = (int)(random.nextF() * kMaxTexCoords); |
| 210 | pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t); |
| 211 | } else { |
| 212 | pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s); |
| 213 | } |
| 214 | } |
| 215 | // use text-formatted verts? |
| 216 | if (random.nextF() > .5f) { |
| 217 | pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit; |
| 218 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 219 | idx = (int)(random.nextF() * GR_ARRAY_COUNT(STAGE_OPTS)); |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 220 | StageDesc& stage = pdesc.fStages[s]; |
| 221 | stage.fOptFlags = STAGE_OPTS[idx]; |
| 222 | stage.fModulation = random_val(&random, StageDesc::kModulationCnt); |
| 223 | stage.fCoordMapping = random_val(&random, StageDesc::kCoordMappingCnt); |
| 224 | stage.fFetchMode = random_val(&random, StageDesc::kFetchModeCnt); |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 225 | // convolution shaders don't work with persp tex matrix |
| 226 | if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) { |
| 227 | stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit; |
| 228 | } |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 229 | stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout)); |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 230 | stage.fKernelWidth = 4 * random.nextF() + 2; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 231 | } |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 232 | CachedData cachedData; |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 233 | if (!program.genProgram(&cachedData)) { |
| 234 | return false; |
| 235 | } |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 236 | DeleteProgram(&cachedData); |
| 237 | bool again = false; |
| 238 | if (again) { |
| 239 | program.genProgram(&cachedData); |
| 240 | DeleteProgram(&cachedData); |
| 241 | } |
| 242 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 243 | return true; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 244 | } |
| 245 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 246 | GrGpuGLShaders::GrGpuGLShaders() { |
| 247 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 248 | resetContext(); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 249 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 250 | f4X4DownsampleFilterSupport = true; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 251 | if (GR_GL_SUPPORT_DESKTOP) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 252 | fDualSourceBlendingSupport = |
bsalomon@google.com | 2c17fcd | 2011-07-06 17:47:02 +0000 | [diff] [blame] | 253 | fGLVersion >= 3.3f || |
| 254 | this->hasExtension("GL_ARB_blend_func_extended"); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 255 | } else { |
| 256 | fDualSourceBlendingSupport = false; |
| 257 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 258 | |
| 259 | fProgramData = NULL; |
| 260 | fProgramCache = new ProgramCache(); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 261 | |
| 262 | #if 0 |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 263 | this->programUnitTest(); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 264 | #endif |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | GrGpuGLShaders::~GrGpuGLShaders() { |
| 268 | delete fProgramCache; |
| 269 | } |
| 270 | |
| 271 | const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 272 | GrAssert(fProgramData); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 273 | |
| 274 | if (GrGLProgram::kSetAsAttribute == |
| 275 | fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) { |
| 276 | return fHWDrawState.fSamplerStates[stage].getMatrix(); |
| 277 | } else { |
| 278 | return fProgramData->fTextureMatrices[stage]; |
| 279 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 283 | GrAssert(fProgramData); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 284 | if (GrGLProgram::kSetAsAttribute == |
| 285 | fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) { |
| 286 | fHWDrawState.fSamplerStates[stage].setMatrix(matrix); |
| 287 | } else { |
| 288 | fProgramData->fTextureMatrices[stage] = matrix; |
| 289 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void GrGpuGLShaders::resetContext() { |
| 293 | INHERITED::resetContext(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 294 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 295 | fHWGeometryState.fVertexLayout = 0; |
| 296 | fHWGeometryState.fVertexOffset = ~0; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 297 | GR_GL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx())); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 298 | for (int t = 0; t < kMaxTexCoords; ++t) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 299 | GR_GL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 300 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 301 | GR_GL(EnableVertexAttribArray(GrGLProgram::PositionAttributeIdx())); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 302 | |
| 303 | fHWProgramID = 0; |
| 304 | } |
| 305 | |
| 306 | void GrGpuGLShaders::flushViewMatrix() { |
| 307 | GrAssert(NULL != fCurrDrawState.fRenderTarget); |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 308 | GrMatrix m; |
| 309 | m.setAll( |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 310 | GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1, |
| 311 | 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1, |
| 312 | 0, 0, GrMatrix::I()[8]); |
| 313 | m.setConcat(m, fCurrDrawState.fViewMatrix); |
| 314 | |
| 315 | // ES doesn't allow you to pass true to the transpose param, |
| 316 | // so do our own transpose |
bsalomon@google.com | 27a4dc4 | 2011-05-16 13:14:03 +0000 | [diff] [blame] | 317 | GrGLfloat mt[] = { |
| 318 | GrScalarToFloat(m[GrMatrix::kMScaleX]), |
| 319 | GrScalarToFloat(m[GrMatrix::kMSkewY]), |
| 320 | GrScalarToFloat(m[GrMatrix::kMPersp0]), |
| 321 | GrScalarToFloat(m[GrMatrix::kMSkewX]), |
| 322 | GrScalarToFloat(m[GrMatrix::kMScaleY]), |
| 323 | GrScalarToFloat(m[GrMatrix::kMPersp1]), |
| 324 | GrScalarToFloat(m[GrMatrix::kMTransX]), |
| 325 | GrScalarToFloat(m[GrMatrix::kMTransY]), |
| 326 | GrScalarToFloat(m[GrMatrix::kMPersp2]) |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 327 | }; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 328 | |
| 329 | if (GrGLProgram::kSetAsAttribute == |
| 330 | fProgramData->fUniLocations.fViewMatrixUni) { |
| 331 | int baseIdx = GrGLProgram::ViewMatrixAttributeIdx(); |
| 332 | GR_GL(VertexAttrib4fv(baseIdx + 0, mt+0)); |
| 333 | GR_GL(VertexAttrib4fv(baseIdx + 1, mt+3)); |
| 334 | GR_GL(VertexAttrib4fv(baseIdx + 2, mt+6)); |
| 335 | } else { |
| 336 | GrAssert(GrGLProgram::kUnusedUniform != |
| 337 | fProgramData->fUniLocations.fViewMatrixUni); |
| 338 | GR_GL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni, |
| 339 | 1, false, mt)); |
| 340 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 341 | } |
| 342 | |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 343 | void GrGpuGLShaders::flushTextureDomain(int s) { |
| 344 | const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni; |
| 345 | if (GrGLProgram::kUnusedUniform != uni) { |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 346 | const GrRect &texDom = |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 347 | fCurrDrawState.fSamplerStates[s].getTextureDomain(); |
| 348 | |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 349 | if (((1 << s) & fDirtyFlags.fTextureChangedMask) || |
| 350 | fProgramData->fTextureDomain[s] != texDom) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 351 | |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 352 | fProgramData->fTextureDomain[s] = texDom; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 353 | |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 354 | float values[4] = { |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 355 | GrScalarToFloat(texDom.left()), |
| 356 | GrScalarToFloat(texDom.top()), |
| 357 | GrScalarToFloat(texDom.right()), |
| 358 | GrScalarToFloat(texDom.bottom()) |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 359 | }; |
| 360 | |
| 361 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; |
| 362 | GrGLTexture::Orientation orientation = texture->orientation(); |
| 363 | |
| 364 | // vertical flip if necessary |
| 365 | if (GrGLTexture::kBottomUp_Orientation == orientation) { |
| 366 | values[1] = 1.0f - values[1]; |
| 367 | values[3] = 1.0f - values[3]; |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 368 | // The top and bottom were just flipped, so correct the ordering |
| 369 | // of elements so that values = (l, t, r, b). |
| 370 | SkTSwap(values[1], values[3]); |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | values[0] *= SkScalarToFloat(texture->contentScaleX()); |
| 374 | values[2] *= SkScalarToFloat(texture->contentScaleX()); |
| 375 | values[1] *= SkScalarToFloat(texture->contentScaleY()); |
| 376 | values[3] *= SkScalarToFloat(texture->contentScaleY()); |
| 377 | |
| 378 | GR_GL(Uniform4fv(uni, 1, values)); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 379 | } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 383 | void GrGpuGLShaders::flushTextureMatrix(int s) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 384 | const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 385 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; |
| 386 | if (NULL != texture) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 387 | if (GrGLProgram::kUnusedUniform != uni && |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 388 | (((1 << s) & fDirtyFlags.fTextureChangedMask) || |
| 389 | getHWSamplerMatrix(s) != getSamplerMatrix(s))) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 390 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 391 | GrAssert(NULL != fCurrDrawState.fTextures[s]); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 392 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 393 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 394 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 395 | GrMatrix m = getSamplerMatrix(s); |
| 396 | GrSamplerState::SampleMode mode = |
| 397 | fCurrDrawState.fSamplerStates[s].getSampleMode(); |
| 398 | AdjustTextureMatrix(texture, mode, &m); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 399 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 400 | // ES doesn't allow you to pass true to the transpose param, |
| 401 | // so do our own transpose |
bsalomon@google.com | 27a4dc4 | 2011-05-16 13:14:03 +0000 | [diff] [blame] | 402 | GrGLfloat mt[] = { |
| 403 | GrScalarToFloat(m[GrMatrix::kMScaleX]), |
| 404 | GrScalarToFloat(m[GrMatrix::kMSkewY]), |
| 405 | GrScalarToFloat(m[GrMatrix::kMPersp0]), |
| 406 | GrScalarToFloat(m[GrMatrix::kMSkewX]), |
| 407 | GrScalarToFloat(m[GrMatrix::kMScaleY]), |
| 408 | GrScalarToFloat(m[GrMatrix::kMPersp1]), |
| 409 | GrScalarToFloat(m[GrMatrix::kMTransX]), |
| 410 | GrScalarToFloat(m[GrMatrix::kMTransY]), |
| 411 | GrScalarToFloat(m[GrMatrix::kMPersp2]) |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 412 | }; |
bsalomon@google.com | 27a4dc4 | 2011-05-16 13:14:03 +0000 | [diff] [blame] | 413 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 414 | if (GrGLProgram::kSetAsAttribute == |
| 415 | fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) { |
| 416 | int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s); |
| 417 | GR_GL(VertexAttrib4fv(baseIdx + 0, mt+0)); |
| 418 | GR_GL(VertexAttrib4fv(baseIdx + 1, mt+3)); |
| 419 | GR_GL(VertexAttrib4fv(baseIdx + 2, mt+6)); |
| 420 | } else { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 421 | GR_GL(UniformMatrix3fv(uni, 1, false, mt)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 422 | } |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 423 | recordHWSamplerMatrix(s, getSamplerMatrix(s)); |
| 424 | } |
| 425 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 426 | } |
| 427 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 428 | void GrGpuGLShaders::flushRadial2(int s) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 429 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 430 | const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni; |
| 431 | const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s]; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 432 | if (GrGLProgram::kUnusedUniform != uni && |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 433 | (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() || |
| 434 | fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() || |
| 435 | fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 436 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 437 | GrScalar centerX1 = sampler.getRadial2CenterX1(); |
| 438 | GrScalar radius0 = sampler.getRadial2Radius0(); |
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 | GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 441 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 442 | // when were in the degenerate (linear) case the second |
| 443 | // value will be INF but the program doesn't read it. (We |
| 444 | // use the same 6 uniforms even though we don't need them |
| 445 | // all in the linear case just to keep the code complexity |
| 446 | // down). |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 447 | float values[6] = { |
| 448 | GrScalarToFloat(a), |
| 449 | 1 / (2.f * values[0]), |
| 450 | GrScalarToFloat(centerX1), |
| 451 | GrScalarToFloat(radius0), |
| 452 | GrScalarToFloat(GrMul(radius0, radius0)), |
| 453 | sampler.isRadial2PosRoot() ? 1.f : -1.f |
| 454 | }; |
| 455 | GR_GL(Uniform1fv(uni, 6, values)); |
| 456 | fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1(); |
| 457 | fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0(); |
| 458 | fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot(); |
| 459 | } |
| 460 | } |
| 461 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 462 | void GrGpuGLShaders::flushConvolution(int s) { |
| 463 | const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s]; |
| 464 | int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni; |
| 465 | if (GrGLProgram::kUnusedUniform != kernelUni) { |
| 466 | GR_GL(Uniform1fv(kernelUni, sampler.getKernelWidth(), sampler.getKernel())); |
| 467 | } |
| 468 | int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni; |
| 469 | if (GrGLProgram::kUnusedUniform != imageIncrementUni) { |
| 470 | GR_GL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement())); |
| 471 | } |
| 472 | } |
| 473 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 474 | void GrGpuGLShaders::flushTexelSize(int s) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 475 | const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 476 | if (GrGLProgram::kUnusedUniform != uni) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 477 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; |
bsalomon@google.com | 0168afc | 2011-08-08 13:21:05 +0000 | [diff] [blame] | 478 | if (texture->allocatedWidth() != fProgramData->fTextureWidth[s] || |
| 479 | texture->allocatedHeight() != fProgramData->fTextureWidth[s]) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 480 | |
bsalomon@google.com | 0168afc | 2011-08-08 13:21:05 +0000 | [diff] [blame] | 481 | float texelSize[] = {1.f / texture->allocatedWidth(), |
| 482 | 1.f / texture->allocatedHeight()}; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 483 | GR_GL(Uniform2fv(uni, 1, texelSize)); |
| 484 | } |
| 485 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 486 | } |
| 487 | |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 488 | void GrGpuGLShaders::flushEdgeAAData() { |
| 489 | const int& uni = fProgramData->fUniLocations.fEdgesUni; |
| 490 | if (GrGLProgram::kUnusedUniform != uni) { |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 491 | int count = fCurrDrawState.fEdgeAANumEdges; |
| 492 | Edge edges[kMaxEdges]; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 493 | // Flip the edges in Y |
| 494 | float height = fCurrDrawState.fRenderTarget->height(); |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 495 | for (int i = 0; i < count; ++i) { |
| 496 | edges[i] = fCurrDrawState.fEdgeAAEdges[i]; |
| 497 | float b = edges[i].fY; |
| 498 | edges[i].fY = -b; |
| 499 | edges[i].fZ += b * height; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 500 | } |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 501 | GR_GL(Uniform3fv(uni, count, &edges[0].fX)); |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
Scroggo | 01b87ec | 2011-05-11 18:05:38 +0000 | [diff] [blame] | 505 | static const float ONE_OVER_255 = 1.f / 255.f; |
| 506 | |
| 507 | #define GR_COLOR_TO_VEC4(color) {\ |
| 508 | GrColorUnpackR(color) * ONE_OVER_255,\ |
| 509 | GrColorUnpackG(color) * ONE_OVER_255,\ |
| 510 | GrColorUnpackB(color) * ONE_OVER_255,\ |
| 511 | GrColorUnpackA(color) * ONE_OVER_255 \ |
| 512 | } |
| 513 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 514 | void GrGpuGLShaders::flushColor() { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 515 | const ProgramDesc& desc = fCurrentProgram.getDesc(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 516 | if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 517 | // color will be specified per-vertex as an attribute |
| 518 | // invalidate the const vertex attrib color |
| 519 | fHWDrawState.fColor = GrColor_ILLEGAL; |
| 520 | } else { |
| 521 | switch (desc.fColorType) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 522 | case ProgramDesc::kAttribute_ColorType: |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 523 | if (fHWDrawState.fColor != fCurrDrawState.fColor) { |
| 524 | // OpenGL ES only supports the float varities of glVertexAttrib |
Scroggo | 01b87ec | 2011-05-11 18:05:38 +0000 | [diff] [blame] | 525 | float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 526 | GR_GL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(), c)); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 527 | fHWDrawState.fColor = fCurrDrawState.fColor; |
| 528 | } |
| 529 | break; |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 530 | case ProgramDesc::kUniform_ColorType: |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 531 | if (fProgramData->fColor != fCurrDrawState.fColor) { |
| 532 | // OpenGL ES only supports the float varities of glVertexAttrib |
Scroggo | 01b87ec | 2011-05-11 18:05:38 +0000 | [diff] [blame] | 533 | float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 534 | GrAssert(GrGLProgram::kUnusedUniform != |
| 535 | fProgramData->fUniLocations.fColorUni); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 536 | GR_GL(Uniform4fv(fProgramData->fUniLocations.fColorUni, 1, c)); |
| 537 | fProgramData->fColor = fCurrDrawState.fColor; |
| 538 | } |
| 539 | break; |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 540 | case ProgramDesc::kNone_ColorType: |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 541 | GrAssert(0xffffffff == fCurrDrawState.fColor); |
| 542 | break; |
| 543 | default: |
| 544 | GrCrash("Unknown color type."); |
| 545 | } |
| 546 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 547 | if (fProgramData->fUniLocations.fColorFilterUni |
| 548 | != GrGLProgram::kUnusedUniform |
| 549 | && fProgramData->fColorFilterColor |
| 550 | != fCurrDrawState.fColorFilterColor) { |
Scroggo | 01b87ec | 2011-05-11 18:05:38 +0000 | [diff] [blame] | 551 | float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColorFilterColor); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 552 | GR_GL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c)); |
| 553 | fProgramData->fColorFilterColor = fCurrDrawState.fColorFilterColor; |
| 554 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 558 | bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) { |
| 559 | if (!flushGLStateCommon(type)) { |
| 560 | return false; |
| 561 | } |
| 562 | |
| 563 | if (fDirtyFlags.fRenderTargetChanged) { |
| 564 | // our coords are in pixel space and the GL matrices map to NDC |
| 565 | // so if the viewport changed, our matrix is now wrong. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 566 | fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 567 | // we assume all shader matrices may be wrong after viewport changes |
| 568 | fProgramCache->invalidateViewMatrices(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 569 | } |
| 570 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 571 | buildProgram(type); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 572 | fProgramData = fProgramCache->getProgramData(fCurrentProgram); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 573 | if (NULL == fProgramData) { |
| 574 | GrAssert(!"Failed to create program!"); |
| 575 | return false; |
| 576 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 577 | |
| 578 | if (fHWProgramID != fProgramData->fProgramID) { |
| 579 | GR_GL(UseProgram(fProgramData->fProgramID)); |
| 580 | fHWProgramID = fProgramData->fProgramID; |
| 581 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 582 | GrBlendCoeff srcCoeff = fCurrDrawState.fSrcBlend; |
| 583 | GrBlendCoeff dstCoeff = fCurrDrawState.fDstBlend; |
| 584 | |
| 585 | fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff); |
| 586 | this->flushBlend(type, srcCoeff, dstCoeff); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 587 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 588 | this->flushColor(); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 589 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 590 | GrMatrix* currViewMatrix; |
| 591 | if (GrGLProgram::kSetAsAttribute == |
| 592 | fProgramData->fUniLocations.fViewMatrixUni) { |
| 593 | currViewMatrix = &fHWDrawState.fViewMatrix; |
| 594 | } else { |
| 595 | currViewMatrix = &fProgramData->fViewMatrix; |
| 596 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 597 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 598 | if (*currViewMatrix != fCurrDrawState.fViewMatrix) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 599 | flushViewMatrix(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 600 | *currViewMatrix = fCurrDrawState.fViewMatrix; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | for (int s = 0; s < kNumStages; ++s) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 604 | this->flushTextureMatrix(s); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 605 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 606 | this->flushRadial2(s); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 607 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 608 | this->flushConvolution(s); |
| 609 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 610 | this->flushTexelSize(s); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 611 | |
| 612 | this->flushTextureDomain(s); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 613 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 614 | this->flushEdgeAAData(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 615 | resetDirtyFlags(); |
| 616 | return true; |
| 617 | } |
| 618 | |
| 619 | void GrGpuGLShaders::postDraw() { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | void GrGpuGLShaders::setupGeometry(int* startVertex, |
| 623 | int* startIndex, |
| 624 | int vertexCount, |
| 625 | int indexCount) { |
| 626 | |
| 627 | int newColorOffset; |
| 628 | int newTexCoordOffsets[kMaxTexCoords]; |
| 629 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 630 | GrGLsizei newStride = VertexSizeAndOffsetsByIdx( |
| 631 | this->getGeomSrc().fVertexLayout, |
| 632 | newTexCoordOffsets, |
| 633 | &newColorOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 634 | int oldColorOffset; |
| 635 | int oldTexCoordOffsets[kMaxTexCoords]; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 636 | GrGLsizei oldStride = VertexSizeAndOffsetsByIdx( |
| 637 | fHWGeometryState.fVertexLayout, |
| 638 | oldTexCoordOffsets, |
| 639 | &oldColorOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 640 | bool indexed = NULL != startIndex; |
| 641 | |
| 642 | int extraVertexOffset; |
| 643 | int extraIndexOffset; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 644 | this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 645 | |
| 646 | GrGLenum scalarType; |
| 647 | bool texCoordNorm; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 648 | if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 649 | scalarType = GrGLTextType; |
| 650 | texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED; |
| 651 | } else { |
| 652 | scalarType = GrGLType; |
| 653 | texCoordNorm = false; |
| 654 | } |
| 655 | |
| 656 | size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride; |
| 657 | *startVertex = 0; |
| 658 | if (indexed) { |
| 659 | *startIndex += extraIndexOffset; |
| 660 | } |
| 661 | |
| 662 | // all the Pointers must be set if any of these are true |
| 663 | bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty || |
| 664 | vertexOffset != fHWGeometryState.fVertexOffset || |
| 665 | newStride != oldStride; |
| 666 | |
| 667 | // position and tex coord offsets change if above conditions are true |
| 668 | // or the type/normalization changed based on text vs nontext type coords. |
| 669 | bool posAndTexChange = allOffsetsChange || |
| 670 | (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) && |
| 671 | (kTextFormat_VertexLayoutBit & |
| 672 | (fHWGeometryState.fVertexLayout ^ |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 673 | this->getGeomSrc().fVertexLayout))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 674 | |
| 675 | if (posAndTexChange) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 676 | int idx = GrGLProgram::PositionAttributeIdx(); |
| 677 | GR_GL(VertexAttribPointer(idx, 2, scalarType, false, newStride, |
| 678 | (GrGLvoid*)vertexOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 679 | fHWGeometryState.fVertexOffset = vertexOffset; |
| 680 | } |
| 681 | |
| 682 | for (int t = 0; t < kMaxTexCoords; ++t) { |
| 683 | if (newTexCoordOffsets[t] > 0) { |
| 684 | GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 685 | int idx = GrGLProgram::TexCoordAttributeIdx(t); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 686 | if (oldTexCoordOffsets[t] <= 0) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 687 | GR_GL(EnableVertexAttribArray(idx)); |
| 688 | GR_GL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
| 689 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 690 | } else if (posAndTexChange || |
| 691 | newTexCoordOffsets[t] != oldTexCoordOffsets[t]) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 692 | GR_GL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
| 693 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 694 | } |
| 695 | } else if (oldTexCoordOffsets[t] > 0) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 696 | GR_GL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
| 700 | if (newColorOffset > 0) { |
| 701 | GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 702 | int idx = GrGLProgram::ColorAttributeIdx(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 703 | if (oldColorOffset <= 0) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 704 | GR_GL(EnableVertexAttribArray(idx)); |
| 705 | GR_GL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 706 | true, newStride, colorOffset)); |
| 707 | } else if (allOffsetsChange || newColorOffset != oldColorOffset) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 708 | GR_GL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 709 | true, newStride, colorOffset)); |
| 710 | } |
| 711 | } else if (oldColorOffset > 0) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 712 | GR_GL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx())); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 713 | } |
| 714 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 715 | fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 716 | fHWGeometryState.fArrayPtrsDirty = false; |
| 717 | } |
| 718 | |
| 719 | void GrGpuGLShaders::buildProgram(GrPrimitiveType type) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 720 | ProgramDesc& desc = fCurrentProgram.fProgramDesc; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 721 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 722 | // Must initialize all fields or cache will have false negatives! |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 723 | desc.fVertexLayout = this->getGeomSrc().fVertexLayout; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 724 | |
| 725 | desc.fEmitsPointSize = kPoints_PrimitiveType == type; |
| 726 | |
| 727 | bool requiresAttributeColors = desc.fVertexLayout & kColor_VertexLayoutBit; |
| 728 | // fColorType records how colors are specified for the program. Strip |
| 729 | // the bit from the layout to avoid false negatives when searching for an |
| 730 | // existing program in the cache. |
| 731 | desc.fVertexLayout &= ~(kColor_VertexLayoutBit); |
| 732 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 733 | desc.fColorFilterXfermode = fCurrDrawState.fColorFilterXfermode; |
| 734 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 735 | #if GR_AGGRESSIVE_SHADER_OPTS |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 736 | if (!requiresAttributeColors && (0xffffffff == fCurrDrawState.fColor)) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 737 | desc.fColorType = ProgramDesc::kNone_ColorType; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 738 | } else |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 739 | #endif |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 740 | #if GR_GL_NO_CONSTANT_ATTRIBUTES |
| 741 | if (!requiresAttributeColors) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 742 | desc.fColorType = ProgramDesc::kUniform_ColorType; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 743 | } else |
| 744 | #endif |
| 745 | { |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 746 | if (requiresAttributeColors) {} // suppress unused var warning |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 747 | desc.fColorType = ProgramDesc::kAttribute_ColorType; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 748 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 749 | |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 750 | desc.fEdgeAANumEdges = fCurrDrawState.fEdgeAANumEdges; |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 751 | desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 && SkToBool(fCurrDrawState.fFlagBits & kEdgeAAConcave_StateBit); |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 752 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 753 | int lastEnabledStage = -1; |
| 754 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 755 | for (int s = 0; s < kNumStages; ++s) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 756 | StageDesc& stage = desc.fStages[s]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 757 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 758 | stage.fOptFlags = 0; |
| 759 | stage.setEnabled(this->isStageEnabled(s)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 760 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 761 | if (stage.isEnabled()) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 762 | lastEnabledStage = s; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 763 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; |
| 764 | GrAssert(NULL != texture); |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 765 | const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 766 | // we matrix to invert when orientation is TopDown, so make sure |
| 767 | // we aren't in that case before flagging as identity. |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 768 | if (TextureMatrixIsIdentity(texture, sampler)) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 769 | stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 770 | } else if (!getSamplerMatrix(s).hasPerspective()) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 771 | stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 772 | } |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 773 | switch (sampler.getSampleMode()) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 774 | case GrSamplerState::kNormal_SampleMode: |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 775 | stage.fCoordMapping = StageDesc::kIdentity_CoordMapping; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 776 | break; |
| 777 | case GrSamplerState::kRadial_SampleMode: |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 778 | stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 779 | break; |
| 780 | case GrSamplerState::kRadial2_SampleMode: |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 781 | if (sampler.radial2IsDegenerate()) { |
| 782 | stage.fCoordMapping = |
| 783 | StageDesc::kRadial2GradientDegenerate_CoordMapping; |
| 784 | } else { |
| 785 | stage.fCoordMapping = |
| 786 | StageDesc::kRadial2Gradient_CoordMapping; |
| 787 | } |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 788 | break; |
| 789 | case GrSamplerState::kSweep_SampleMode: |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 790 | stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 791 | break; |
| 792 | default: |
| 793 | GrCrash("Unexpected sample mode!"); |
| 794 | break; |
| 795 | } |
| 796 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 797 | switch (sampler.getFilter()) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 798 | // these both can use a regular texture2D() |
| 799 | case GrSamplerState::kNearest_Filter: |
| 800 | case GrSamplerState::kBilinear_Filter: |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 801 | stage.fFetchMode = StageDesc::kSingle_FetchMode; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 802 | break; |
| 803 | // performs 4 texture2D()s |
| 804 | case GrSamplerState::k4x4Downsample_Filter: |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 805 | stage.fFetchMode = StageDesc::k2x2_FetchMode; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 806 | break; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 807 | // performs fKernelWidth texture2D()s |
| 808 | case GrSamplerState::kConvolution_Filter: |
| 809 | stage.fFetchMode = StageDesc::kConvolution_FetchMode; |
| 810 | break; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 811 | default: |
| 812 | GrCrash("Unexpected filter!"); |
| 813 | break; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 814 | } |
| 815 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 816 | if (sampler.hasTextureDomain()) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 817 | GrAssert(GrSamplerState::kClamp_WrapMode == |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 818 | sampler.getWrapX() && |
| 819 | GrSamplerState::kClamp_WrapMode == |
| 820 | sampler.getWrapY()); |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 821 | stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 822 | } |
| 823 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 824 | if (GrPixelConfigIsAlphaOnly(texture->config())) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 825 | stage.fModulation = StageDesc::kAlpha_Modulation; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 826 | } else { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 827 | stage.fModulation = StageDesc::kColor_Modulation; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 828 | } |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 829 | if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) { |
| 830 | stage.fKernelWidth = sampler.getKernelWidth(); |
| 831 | } else { |
| 832 | stage.fKernelWidth = 0; |
| 833 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 834 | } else { |
| 835 | stage.fOptFlags = 0; |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 836 | stage.fCoordMapping = (StageDesc::CoordMapping)0; |
| 837 | stage.fModulation = (StageDesc::Modulation)0; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 838 | } |
| 839 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 840 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 841 | desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 842 | // use canonical value when coverage/color distinction won't affect |
| 843 | // generated code to prevent duplicate programs. |
| 844 | desc.fFirstCoverageStage = kNumStages; |
| 845 | if (fCurrDrawState.fFirstCoverageStage <= lastEnabledStage) { |
| 846 | // color filter is applied between color/coverage computation |
| 847 | if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) { |
| 848 | desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage; |
| 849 | } |
| 850 | |
| 851 | // We could consider cases where the final color is solid (0xff alpha) |
| 852 | // and the dst coeff can correctly be set to a non-dualsrc gl value. |
| 853 | // (e.g. solid draw, and dst coeff is kZero. It's correct to make |
| 854 | // the dst coeff be kISA. Or solid draw with kSA can be tweaked to be |
| 855 | // kOne). |
| 856 | if (fDualSourceBlendingSupport) { |
| 857 | if (kZero_BlendCoeff == fCurrDrawState.fDstBlend) { |
| 858 | // write the coverage value to second color |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 859 | desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 860 | desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage; |
| 861 | } else if (kSA_BlendCoeff == fCurrDrawState.fDstBlend) { |
| 862 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially |
| 863 | // cover |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 864 | desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 865 | desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage; |
| 866 | } else if (kSC_BlendCoeff == fCurrDrawState.fDstBlend) { |
| 867 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially |
| 868 | // cover |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 869 | desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 870 | desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage; |
| 871 | } |
| 872 | } |
| 873 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 874 | } |