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