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