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 | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 54 | Entry fEntries[kMaxEntries]; |
| 55 | int fCount; |
| 56 | unsigned int fCurrLRUStamp; |
| 57 | const GrGLInterface* fGL; |
| 58 | GrGLProgram::GLSLVersion fGLSLVersion; |
| 59 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 60 | public: |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 61 | ProgramCache(const GrGLInterface* gl, |
| 62 | GrGLProgram::GLSLVersion glslVersion) |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 63 | : fCount(0) |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 64 | , fCurrLRUStamp(0) |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 65 | , fGL(gl) |
| 66 | , fGLSLVersion(glslVersion) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | ~ProgramCache() { |
| 70 | for (int i = 0; i < fCount; ++i) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 71 | GrGpuGLShaders::DeleteProgram(fGL, &fEntries[i].fProgramData); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | void abandon() { |
| 76 | fCount = 0; |
| 77 | } |
| 78 | |
| 79 | void invalidateViewMatrices() { |
| 80 | for (int i = 0; i < fCount; ++i) { |
| 81 | // set to illegal matrix |
| 82 | fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix(); |
| 83 | } |
| 84 | } |
| 85 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 86 | GrGLProgram::CachedData* getProgramData(const GrGLProgram& desc) { |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 87 | Entry newEntry; |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 88 | newEntry.fKey.setKeyData(desc.keyData()); |
| 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 | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 92 | if (!desc.genProgram(fGL, fGLSLVersion, &newEntry.fProgramData)) { |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 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); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 107 | GrGpuGLShaders::DeleteProgram(fGL, &entry->fProgramData); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 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 | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 130 | void GrGpuGLShaders::DeleteProgram(const GrGLInterface* gl, |
| 131 | CachedData* programData) { |
| 132 | GR_GL_CALL(gl, DeleteShader(programData->fVShaderID)); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 133 | if (programData->fGShaderID) { |
| 134 | GR_GL_CALL(gl, DeleteShader(programData->fGShaderID)); |
| 135 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 136 | GR_GL_CALL(gl, DeleteShader(programData->fFShaderID)); |
| 137 | GR_GL_CALL(gl, DeleteProgram(programData->fProgramID)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 138 | GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));) |
| 139 | } |
| 140 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 141 | //////////////////////////////////////////////////////////////////////////////// |
| 142 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 143 | #define GL_CALL(X) GR_GL_CALL(this->glInterface(), X) |
| 144 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 145 | namespace { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 146 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 147 | GrGLProgram::GLSLVersion get_glsl_version(GrGLBinding binding, |
| 148 | const GrGLInterface* gl) { |
| 149 | GrGLSLVersion ver = GrGLGetGLSLVersion(gl); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 150 | switch (binding) { |
| 151 | case kDesktop_GrGLBinding: |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 152 | GrAssert(ver >= GR_GLSL_VER(1,10)); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 153 | if (ver >= GR_GLSL_VER(1,50)) { |
| 154 | return GrGLProgram::k150_GLSLVersion; |
| 155 | } else if (ver >= GR_GLSL_VER(1,30)) { |
| 156 | return GrGLProgram::k130_GLSLVersion; |
| 157 | } else { |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 158 | return GrGLProgram::k110_GLSLVersion; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 159 | } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 160 | case kES2_GrGLBinding: |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 161 | // version 1.00 of ES GLSL based on ver 1.20 of desktop GLSL |
| 162 | GrAssert(ver >= GR_GL_VER(1,00)); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 163 | return GrGLProgram::k110_GLSLVersion; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 164 | default: |
| 165 | GrCrash("Attempting to get GLSL version in unknown or fixed-" |
| 166 | "function GL binding."); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 167 | return GrGLProgram::k110_GLSLVersion; // suppress warning |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 168 | } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | template <typename T> |
| 172 | T random_val(GrRandom* r, T count) { |
| 173 | return (T)(int)(r->nextF() * count); |
| 174 | } |
| 175 | |
| 176 | } |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 177 | |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 178 | bool GrGpuGLShaders::programUnitTest() { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 179 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 180 | GrGLProgram::GLSLVersion glslVersion = |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 181 | get_glsl_version(this->glBinding(), this->glInterface()); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 182 | static const int STAGE_OPTS[] = { |
| 183 | 0, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 184 | StageDesc::kNoPerspective_OptFlagBit, |
| 185 | StageDesc::kIdentity_CoordMapping |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 186 | }; |
| 187 | GrGLProgram program; |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 188 | ProgramDesc& pdesc = program.fProgramDesc; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 189 | |
| 190 | static const int NUM_TESTS = 512; |
| 191 | |
| 192 | // GrRandoms nextU() values have patterns in the low bits |
| 193 | // So using nextU() % array_count might never take some values. |
| 194 | GrRandom random; |
| 195 | for (int t = 0; t < NUM_TESTS; ++t) { |
| 196 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 197 | #if 0 |
| 198 | GrPrintf("\nTest Program %d\n-------------\n", t); |
| 199 | static const int stop = -1; |
| 200 | if (t == stop) { |
| 201 | int breakpointhere = 9; |
| 202 | } |
| 203 | #endif |
| 204 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 205 | pdesc.fVertexLayout = 0; |
| 206 | pdesc.fEmitsPointSize = random.nextF() > .5f; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame^] | 207 | pdesc.fColorInput = static_cast<int>(random.nextF() * |
| 208 | ProgramDesc::kColorInputCnt); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 209 | |
| 210 | int idx = (int)(random.nextF() * (SkXfermode::kCoeffModesCnt)); |
| 211 | pdesc.fColorFilterXfermode = (SkXfermode::Mode)idx; |
| 212 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 213 | idx = (int)(random.nextF() * (GrDrawState::kNumStages + 1)); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 214 | pdesc.fFirstCoverageStage = idx; |
| 215 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 216 | pdesc.fVertexLayout |= (random.nextF() > .5f) ? |
| 217 | GrDrawTarget::kCoverage_VertexLayoutBit : |
| 218 | 0; |
| 219 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 220 | #if GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | 6f92f18 | 2011-09-29 15:05:48 +0000 | [diff] [blame] | 221 | pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport && |
| 222 | random.nextF() > .5f; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 223 | #endif |
| 224 | |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 225 | bool edgeAA = random.nextF() > .5f; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 226 | if (edgeAA) { |
| 227 | bool vertexEdgeAA = random.nextF() > .5f; |
| 228 | if (vertexEdgeAA) { |
| 229 | pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 230 | if (this->getCaps().fShaderDerivativeSupport) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 231 | pdesc.fVertexEdgeType = random.nextF() > 0.5f ? |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 232 | GrDrawState::kHairQuad_EdgeType : |
| 233 | GrDrawState::kHairLine_EdgeType; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 234 | } else { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 235 | pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 236 | } |
| 237 | pdesc.fEdgeAANumEdges = 0; |
| 238 | } else { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 239 | pdesc.fEdgeAANumEdges = static_cast<int>(1 + random.nextF() * |
| 240 | this->getMaxEdges()); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 241 | pdesc.fEdgeAAConcave = random.nextF() > .5f; |
| 242 | } |
| 243 | } else { |
| 244 | pdesc.fEdgeAANumEdges = 0; |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 245 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 246 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 247 | if (this->getCaps().fDualSourceBlendingSupport) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 248 | pdesc.fDualSrcOutput = |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 249 | (ProgramDesc::DualSrcOutput) |
| 250 | (int)(random.nextF() * ProgramDesc::kDualSrcOutputCnt); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 251 | } else { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 252 | pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 253 | } |
| 254 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 255 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 256 | // enable the stage? |
| 257 | if (random.nextF() > .5f) { |
| 258 | // use separate tex coords? |
| 259 | if (random.nextF() > .5f) { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 260 | int t = (int)(random.nextF() * GrDrawState::kMaxTexCoords); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 261 | pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t); |
| 262 | } else { |
| 263 | pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s); |
| 264 | } |
| 265 | } |
| 266 | // use text-formatted verts? |
| 267 | if (random.nextF() > .5f) { |
| 268 | pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit; |
| 269 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 270 | idx = (int)(random.nextF() * GR_ARRAY_COUNT(STAGE_OPTS)); |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 271 | StageDesc& stage = pdesc.fStages[s]; |
| 272 | stage.fOptFlags = STAGE_OPTS[idx]; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame^] | 273 | stage.fInputConfig = random_val(&random, StageDesc::kInputConfigCnt); |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 274 | stage.fCoordMapping = random_val(&random, StageDesc::kCoordMappingCnt); |
| 275 | stage.fFetchMode = random_val(&random, StageDesc::kFetchModeCnt); |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 276 | // convolution shaders don't work with persp tex matrix |
| 277 | if (stage.fFetchMode == StageDesc::kConvolution_FetchMode) { |
| 278 | stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit; |
| 279 | } |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 280 | stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout)); |
bsalomon@google.com | 9d12f5c | 2011-09-29 18:08:18 +0000 | [diff] [blame] | 281 | stage.fKernelWidth = static_cast<int8_t>(4 * random.nextF() + 2); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 282 | } |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 283 | CachedData cachedData; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 284 | if (!program.genProgram(this->glInterface(), |
| 285 | glslVersion, |
| 286 | &cachedData)) { |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 287 | return false; |
| 288 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 289 | DeleteProgram(this->glInterface(), &cachedData); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 290 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 291 | return true; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 292 | } |
| 293 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 294 | namespace { |
| 295 | GrGLBinding get_binding_in_use(const GrGLInterface* gl) { |
| 296 | if (gl->supportsDesktop()) { |
| 297 | return kDesktop_GrGLBinding; |
| 298 | } else { |
| 299 | GrAssert(gl->supportsES2()); |
| 300 | return kES2_GrGLBinding; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | GrGpuGLShaders::GrGpuGLShaders(const GrGLInterface* gl) |
| 306 | : GrGpuGL(gl, get_binding_in_use(gl)) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 307 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 308 | GrGLProgram::GLSLVersion glslVersion = |
| 309 | get_glsl_version(this->glBinding(), gl); |
| 310 | |
| 311 | // Enable supported shader-releated caps |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 312 | fCaps.fShaderSupport = true; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 313 | fCaps.fSupportPerVertexCoverage = true; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 314 | if (kDesktop_GrGLBinding == this->glBinding()) { |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 315 | fCaps.fDualSourceBlendingSupport = |
bsalomon@google.com | c82b889 | 2011-09-22 14:10:33 +0000 | [diff] [blame] | 316 | this->glVersion() >= GR_GL_VER(3,3) || |
bsalomon@google.com | 2c17fcd | 2011-07-06 17:47:02 +0000 | [diff] [blame] | 317 | this->hasExtension("GL_ARB_blend_func_extended"); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 318 | fCaps.fShaderDerivativeSupport = true; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 319 | // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS |
| 320 | fCaps.fGeometryShaderSupport = |
| 321 | this->glVersion() >= GR_GL_VER(3,2) && |
| 322 | glslVersion >= GrGLProgram::k150_GLSLVersion; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 323 | } else { |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 324 | fCaps.fShaderDerivativeSupport = |
bsalomon@google.com | 1f221a7 | 2011-08-23 20:54:07 +0000 | [diff] [blame] | 325 | this->hasExtension("GL_OES_standard_derivatives"); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 326 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 327 | |
bsalomon@google.com | b5b5eaf | 2011-10-19 13:25:46 +0000 | [diff] [blame] | 328 | GR_GL_GetIntegerv(gl, GR_GL_MAX_VERTEX_ATTRIBS, &fMaxVertexAttribs); |
| 329 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 330 | fProgramData = NULL; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 331 | fProgramCache = new ProgramCache(gl, glslVersion); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 332 | |
| 333 | #if 0 |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 334 | this->programUnitTest(); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 335 | #endif |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | GrGpuGLShaders::~GrGpuGLShaders() { |
| 339 | delete fProgramCache; |
| 340 | } |
| 341 | |
| 342 | const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 343 | GrAssert(fProgramData); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 344 | |
| 345 | if (GrGLProgram::kSetAsAttribute == |
| 346 | fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) { |
| 347 | return fHWDrawState.fSamplerStates[stage].getMatrix(); |
| 348 | } else { |
| 349 | return fProgramData->fTextureMatrices[stage]; |
| 350 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 354 | GrAssert(fProgramData); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 355 | if (GrGLProgram::kSetAsAttribute == |
| 356 | fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) { |
| 357 | fHWDrawState.fSamplerStates[stage].setMatrix(matrix); |
| 358 | } else { |
| 359 | fProgramData->fTextureMatrices[stage] = matrix; |
| 360 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 361 | } |
| 362 | |
bsalomon@google.com | 1bf1c21 | 2011-11-05 12:18:58 +0000 | [diff] [blame] | 363 | void GrGpuGLShaders::onResetContext() { |
| 364 | INHERITED::onResetContext(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 365 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 366 | fHWGeometryState.fVertexOffset = ~0; |
bsalomon@google.com | b5b5eaf | 2011-10-19 13:25:46 +0000 | [diff] [blame] | 367 | |
| 368 | // Third party GL code may have left vertex attributes enabled. Some GL |
| 369 | // implementations (osmesa) may read vetex attributes that are not required |
| 370 | // by the current shader. Therefore, we have to ensure that only the |
| 371 | // attributes we require for the current draw are enabled or we may cause an |
| 372 | // invalid read. |
| 373 | |
| 374 | // Disable all vertex layout bits so that next flush will assume all |
| 375 | // optional vertex attributes are disabled. |
| 376 | fHWGeometryState.fVertexLayout = 0; |
| 377 | |
| 378 | // We always use the this attribute and assume it is always enabled. |
| 379 | int posAttrIdx = GrGLProgram::PositionAttributeIdx(); |
| 380 | GL_CALL(EnableVertexAttribArray(posAttrIdx)); |
| 381 | // Disable all other vertex attributes. |
| 382 | for (int va = 0; va < fMaxVertexAttribs; ++va) { |
| 383 | if (va != posAttrIdx) { |
| 384 | GL_CALL(DisableVertexAttribArray(va)); |
| 385 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 386 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 387 | |
| 388 | fHWProgramID = 0; |
| 389 | } |
| 390 | |
| 391 | void GrGpuGLShaders::flushViewMatrix() { |
| 392 | GrAssert(NULL != fCurrDrawState.fRenderTarget); |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 393 | GrMatrix m; |
| 394 | m.setAll( |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 395 | GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1, |
| 396 | 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1, |
| 397 | 0, 0, GrMatrix::I()[8]); |
| 398 | m.setConcat(m, fCurrDrawState.fViewMatrix); |
| 399 | |
| 400 | // ES doesn't allow you to pass true to the transpose param, |
| 401 | // so do our own transpose |
bsalomon@google.com | 27a4dc4 | 2011-05-16 13:14:03 +0000 | [diff] [blame] | 402 | GrGLfloat mt[] = { |
| 403 | GrScalarToFloat(m[GrMatrix::kMScaleX]), |
| 404 | GrScalarToFloat(m[GrMatrix::kMSkewY]), |
| 405 | GrScalarToFloat(m[GrMatrix::kMPersp0]), |
| 406 | GrScalarToFloat(m[GrMatrix::kMSkewX]), |
| 407 | GrScalarToFloat(m[GrMatrix::kMScaleY]), |
| 408 | GrScalarToFloat(m[GrMatrix::kMPersp1]), |
| 409 | GrScalarToFloat(m[GrMatrix::kMTransX]), |
| 410 | GrScalarToFloat(m[GrMatrix::kMTransY]), |
| 411 | GrScalarToFloat(m[GrMatrix::kMPersp2]) |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 412 | }; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 413 | |
| 414 | if (GrGLProgram::kSetAsAttribute == |
| 415 | fProgramData->fUniLocations.fViewMatrixUni) { |
| 416 | int baseIdx = GrGLProgram::ViewMatrixAttributeIdx(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 417 | GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0)); |
| 418 | GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3)); |
| 419 | GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 420 | } else { |
| 421 | GrAssert(GrGLProgram::kUnusedUniform != |
| 422 | fProgramData->fUniLocations.fViewMatrixUni); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 423 | GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni, |
| 424 | 1, false, mt)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 425 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 426 | } |
| 427 | |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 428 | void GrGpuGLShaders::flushTextureDomain(int s) { |
| 429 | const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni; |
| 430 | if (GrGLProgram::kUnusedUniform != uni) { |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 431 | const GrRect &texDom = |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 432 | fCurrDrawState.fSamplerStates[s].getTextureDomain(); |
| 433 | |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 434 | if (((1 << s) & fDirtyFlags.fTextureChangedMask) || |
| 435 | fProgramData->fTextureDomain[s] != texDom) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 436 | |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 437 | fProgramData->fTextureDomain[s] = texDom; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 438 | |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 439 | float values[4] = { |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 440 | GrScalarToFloat(texDom.left()), |
| 441 | GrScalarToFloat(texDom.top()), |
| 442 | GrScalarToFloat(texDom.right()), |
| 443 | GrScalarToFloat(texDom.bottom()) |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 444 | }; |
| 445 | |
| 446 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; |
| 447 | GrGLTexture::Orientation orientation = texture->orientation(); |
| 448 | |
| 449 | // vertical flip if necessary |
| 450 | if (GrGLTexture::kBottomUp_Orientation == orientation) { |
| 451 | values[1] = 1.0f - values[1]; |
| 452 | values[3] = 1.0f - values[3]; |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 453 | // The top and bottom were just flipped, so correct the ordering |
| 454 | // of elements so that values = (l, t, r, b). |
| 455 | SkTSwap(values[1], values[3]); |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | values[0] *= SkScalarToFloat(texture->contentScaleX()); |
| 459 | values[2] *= SkScalarToFloat(texture->contentScaleX()); |
| 460 | values[1] *= SkScalarToFloat(texture->contentScaleY()); |
| 461 | values[3] *= SkScalarToFloat(texture->contentScaleY()); |
| 462 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 463 | GL_CALL(Uniform4fv(uni, 1, values)); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 464 | } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 468 | void GrGpuGLShaders::flushTextureMatrix(int s) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 469 | const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 470 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; |
| 471 | if (NULL != texture) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 472 | if (GrGLProgram::kUnusedUniform != uni && |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 473 | (((1 << s) & fDirtyFlags.fTextureChangedMask) || |
| 474 | getHWSamplerMatrix(s) != getSamplerMatrix(s))) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 475 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 476 | GrAssert(NULL != fCurrDrawState.fTextures[s]); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 477 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 478 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 479 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 480 | GrMatrix m = getSamplerMatrix(s); |
| 481 | GrSamplerState::SampleMode mode = |
| 482 | fCurrDrawState.fSamplerStates[s].getSampleMode(); |
| 483 | AdjustTextureMatrix(texture, mode, &m); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 484 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 485 | // ES doesn't allow you to pass true to the transpose param, |
| 486 | // so do our own transpose |
bsalomon@google.com | 27a4dc4 | 2011-05-16 13:14:03 +0000 | [diff] [blame] | 487 | GrGLfloat mt[] = { |
| 488 | GrScalarToFloat(m[GrMatrix::kMScaleX]), |
| 489 | GrScalarToFloat(m[GrMatrix::kMSkewY]), |
| 490 | GrScalarToFloat(m[GrMatrix::kMPersp0]), |
| 491 | GrScalarToFloat(m[GrMatrix::kMSkewX]), |
| 492 | GrScalarToFloat(m[GrMatrix::kMScaleY]), |
| 493 | GrScalarToFloat(m[GrMatrix::kMPersp1]), |
| 494 | GrScalarToFloat(m[GrMatrix::kMTransX]), |
| 495 | GrScalarToFloat(m[GrMatrix::kMTransY]), |
| 496 | GrScalarToFloat(m[GrMatrix::kMPersp2]) |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 497 | }; |
bsalomon@google.com | 27a4dc4 | 2011-05-16 13:14:03 +0000 | [diff] [blame] | 498 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 499 | if (GrGLProgram::kSetAsAttribute == |
| 500 | fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) { |
| 501 | int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 502 | GL_CALL(VertexAttrib4fv(baseIdx + 0, mt+0)); |
| 503 | GL_CALL(VertexAttrib4fv(baseIdx + 1, mt+3)); |
| 504 | GL_CALL(VertexAttrib4fv(baseIdx + 2, mt+6)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 505 | } else { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 506 | GL_CALL(UniformMatrix3fv(uni, 1, false, mt)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 507 | } |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 508 | recordHWSamplerMatrix(s, getSamplerMatrix(s)); |
| 509 | } |
| 510 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 511 | } |
| 512 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 513 | void GrGpuGLShaders::flushRadial2(int s) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 514 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 515 | const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni; |
| 516 | const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s]; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 517 | if (GrGLProgram::kUnusedUniform != uni && |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 518 | (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() || |
| 519 | fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() || |
| 520 | fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 521 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 522 | GrScalar centerX1 = sampler.getRadial2CenterX1(); |
| 523 | GrScalar radius0 = sampler.getRadial2Radius0(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 524 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 525 | GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 526 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 527 | // when were in the degenerate (linear) case the second |
| 528 | // value will be INF but the program doesn't read it. (We |
| 529 | // use the same 6 uniforms even though we don't need them |
| 530 | // all in the linear case just to keep the code complexity |
| 531 | // down). |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 532 | float values[6] = { |
| 533 | GrScalarToFloat(a), |
| 534 | 1 / (2.f * values[0]), |
| 535 | GrScalarToFloat(centerX1), |
| 536 | GrScalarToFloat(radius0), |
| 537 | GrScalarToFloat(GrMul(radius0, radius0)), |
| 538 | sampler.isRadial2PosRoot() ? 1.f : -1.f |
| 539 | }; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 540 | GL_CALL(Uniform1fv(uni, 6, values)); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 541 | fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1(); |
| 542 | fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0(); |
| 543 | fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot(); |
| 544 | } |
| 545 | } |
| 546 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 547 | void GrGpuGLShaders::flushConvolution(int s) { |
| 548 | const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s]; |
| 549 | int kernelUni = fProgramData->fUniLocations.fStages[s].fKernelUni; |
| 550 | if (GrGLProgram::kUnusedUniform != kernelUni) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 551 | GL_CALL(Uniform1fv(kernelUni, sampler.getKernelWidth(), |
| 552 | sampler.getKernel())); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 553 | } |
| 554 | int imageIncrementUni = fProgramData->fUniLocations.fStages[s].fImageIncrementUni; |
| 555 | if (GrGLProgram::kUnusedUniform != imageIncrementUni) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 556 | GL_CALL(Uniform2fv(imageIncrementUni, 1, sampler.getImageIncrement())); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 560 | void GrGpuGLShaders::flushTexelSize(int s) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 561 | const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 562 | if (GrGLProgram::kUnusedUniform != uni) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 563 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; |
bsalomon@google.com | 0168afc | 2011-08-08 13:21:05 +0000 | [diff] [blame] | 564 | if (texture->allocatedWidth() != fProgramData->fTextureWidth[s] || |
| 565 | texture->allocatedHeight() != fProgramData->fTextureWidth[s]) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 566 | |
bsalomon@google.com | 0168afc | 2011-08-08 13:21:05 +0000 | [diff] [blame] | 567 | float texelSize[] = {1.f / texture->allocatedWidth(), |
| 568 | 1.f / texture->allocatedHeight()}; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 569 | GL_CALL(Uniform2fv(uni, 1, texelSize)); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 570 | } |
| 571 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 572 | } |
| 573 | |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 574 | void GrGpuGLShaders::flushEdgeAAData() { |
| 575 | const int& uni = fProgramData->fUniLocations.fEdgesUni; |
| 576 | if (GrGLProgram::kUnusedUniform != uni) { |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 577 | int count = fCurrDrawState.fEdgeAANumEdges; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 578 | GrDrawState::Edge edges[GrDrawState::kMaxEdges]; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 579 | // Flip the edges in Y |
bsalomon@google.com | 9d12f5c | 2011-09-29 18:08:18 +0000 | [diff] [blame] | 580 | float height = |
| 581 | static_cast<float>(fCurrDrawState.fRenderTarget->height()); |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 582 | for (int i = 0; i < count; ++i) { |
| 583 | edges[i] = fCurrDrawState.fEdgeAAEdges[i]; |
| 584 | float b = edges[i].fY; |
| 585 | edges[i].fY = -b; |
| 586 | edges[i].fZ += b * height; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 587 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 588 | GL_CALL(Uniform3fv(uni, count, &edges[0].fX)); |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
Scroggo | 01b87ec | 2011-05-11 18:05:38 +0000 | [diff] [blame] | 592 | static const float ONE_OVER_255 = 1.f / 255.f; |
| 593 | |
| 594 | #define GR_COLOR_TO_VEC4(color) {\ |
| 595 | GrColorUnpackR(color) * ONE_OVER_255,\ |
| 596 | GrColorUnpackG(color) * ONE_OVER_255,\ |
| 597 | GrColorUnpackB(color) * ONE_OVER_255,\ |
| 598 | GrColorUnpackA(color) * ONE_OVER_255 \ |
| 599 | } |
| 600 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 601 | void GrGpuGLShaders::flushColor(GrColor color) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 602 | const ProgramDesc& desc = fCurrentProgram.getDesc(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 603 | if (this->getGeomSrc().fVertexLayout & kColor_VertexLayoutBit) { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 604 | // color will be specified per-vertex as an attribute |
| 605 | // invalidate the const vertex attrib color |
| 606 | fHWDrawState.fColor = GrColor_ILLEGAL; |
| 607 | } else { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame^] | 608 | switch (desc.fColorInput) { |
| 609 | case ProgramDesc::kAttribute_ColorInput: |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 610 | if (fHWDrawState.fColor != color) { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 611 | // OpenGL ES only supports the float varities of glVertexAttrib |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 612 | float c[] = GR_COLOR_TO_VEC4(color); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 613 | GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(), |
| 614 | c)); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 615 | fHWDrawState.fColor = color; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 616 | } |
| 617 | break; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame^] | 618 | case ProgramDesc::kUniform_ColorInput: |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 619 | if (fProgramData->fColor != color) { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 620 | // OpenGL ES only supports the float varities of glVertexAttrib |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 621 | float c[] = GR_COLOR_TO_VEC4(color); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 622 | GrAssert(GrGLProgram::kUnusedUniform != |
| 623 | fProgramData->fUniLocations.fColorUni); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 624 | GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni, |
| 625 | 1, c)); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 626 | fProgramData->fColor = color; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 627 | } |
| 628 | break; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame^] | 629 | case ProgramDesc::kSolidWhite_ColorInput: |
| 630 | case ProgramDesc::kTransBlack_ColorInput: |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 631 | break; |
| 632 | default: |
| 633 | GrCrash("Unknown color type."); |
| 634 | } |
| 635 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 636 | if (fProgramData->fUniLocations.fColorFilterUni |
| 637 | != GrGLProgram::kUnusedUniform |
| 638 | && fProgramData->fColorFilterColor |
| 639 | != fCurrDrawState.fColorFilterColor) { |
Scroggo | 01b87ec | 2011-05-11 18:05:38 +0000 | [diff] [blame] | 640 | float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColorFilterColor); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 641 | GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c)); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 642 | fProgramData->fColorFilterColor = fCurrDrawState.fColorFilterColor; |
| 643 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 647 | bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) { |
| 648 | if (!flushGLStateCommon(type)) { |
| 649 | return false; |
| 650 | } |
| 651 | |
| 652 | if (fDirtyFlags.fRenderTargetChanged) { |
| 653 | // our coords are in pixel space and the GL matrices map to NDC |
| 654 | // so if the viewport changed, our matrix is now wrong. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 655 | fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 656 | // we assume all shader matrices may be wrong after viewport changes |
| 657 | fProgramCache->invalidateViewMatrices(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 658 | } |
| 659 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 660 | GrBlendCoeff srcCoeff; |
| 661 | GrBlendCoeff dstCoeff; |
| 662 | BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff); |
| 663 | if (kSkipDraw_BlendOptFlag & blendOpts) { |
| 664 | return false; |
| 665 | } |
| 666 | |
| 667 | this->buildProgram(type, blendOpts, dstCoeff); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 668 | fProgramData = fProgramCache->getProgramData(fCurrentProgram); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 669 | if (NULL == fProgramData) { |
| 670 | GrAssert(!"Failed to create program!"); |
| 671 | return false; |
| 672 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 673 | |
| 674 | if (fHWProgramID != fProgramData->fProgramID) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 675 | GL_CALL(UseProgram(fProgramData->fProgramID)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 676 | fHWProgramID = fProgramData->fProgramID; |
| 677 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 678 | fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff); |
| 679 | this->flushBlend(type, srcCoeff, dstCoeff); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 680 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 681 | GrColor color; |
| 682 | if (blendOpts & kEmitTransBlack_BlendOptFlag) { |
| 683 | color = 0; |
| 684 | } else if (blendOpts & kEmitCoverage_BlendOptFlag) { |
| 685 | color = 0xffffffff; |
| 686 | } else { |
| 687 | color = fCurrDrawState.fColor; |
| 688 | } |
| 689 | this->flushColor(color); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 690 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 691 | GrMatrix* currViewMatrix; |
| 692 | if (GrGLProgram::kSetAsAttribute == |
| 693 | fProgramData->fUniLocations.fViewMatrixUni) { |
| 694 | currViewMatrix = &fHWDrawState.fViewMatrix; |
| 695 | } else { |
| 696 | currViewMatrix = &fProgramData->fViewMatrix; |
| 697 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 698 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 699 | if (*currViewMatrix != fCurrDrawState.fViewMatrix) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 700 | flushViewMatrix(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 701 | *currViewMatrix = fCurrDrawState.fViewMatrix; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 702 | } |
| 703 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 704 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 705 | this->flushTextureMatrix(s); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 706 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 707 | this->flushRadial2(s); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 708 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 709 | this->flushConvolution(s); |
| 710 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 711 | this->flushTexelSize(s); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 712 | |
| 713 | this->flushTextureDomain(s); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 714 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 715 | this->flushEdgeAAData(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 716 | resetDirtyFlags(); |
| 717 | return true; |
| 718 | } |
| 719 | |
| 720 | void GrGpuGLShaders::postDraw() { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | void GrGpuGLShaders::setupGeometry(int* startVertex, |
| 724 | int* startIndex, |
| 725 | int vertexCount, |
| 726 | int indexCount) { |
| 727 | |
| 728 | int newColorOffset; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 729 | int newCoverageOffset; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 730 | int newTexCoordOffsets[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 731 | int newEdgeOffset; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 732 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 733 | GrGLsizei newStride = VertexSizeAndOffsetsByIdx( |
| 734 | this->getGeomSrc().fVertexLayout, |
| 735 | newTexCoordOffsets, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 736 | &newColorOffset, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 737 | &newCoverageOffset, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 738 | &newEdgeOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 739 | int oldColorOffset; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 740 | int oldCoverageOffset; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 741 | int oldTexCoordOffsets[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 742 | int oldEdgeOffset; |
| 743 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 744 | GrGLsizei oldStride = VertexSizeAndOffsetsByIdx( |
| 745 | fHWGeometryState.fVertexLayout, |
| 746 | oldTexCoordOffsets, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 747 | &oldColorOffset, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 748 | &oldCoverageOffset, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 749 | &oldEdgeOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 750 | bool indexed = NULL != startIndex; |
| 751 | |
| 752 | int extraVertexOffset; |
| 753 | int extraIndexOffset; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 754 | this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 755 | |
| 756 | GrGLenum scalarType; |
| 757 | bool texCoordNorm; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 758 | if (this->getGeomSrc().fVertexLayout & kTextFormat_VertexLayoutBit) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 759 | scalarType = GrGLTextType; |
| 760 | texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED; |
| 761 | } else { |
| 762 | scalarType = GrGLType; |
| 763 | texCoordNorm = false; |
| 764 | } |
| 765 | |
| 766 | size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride; |
| 767 | *startVertex = 0; |
| 768 | if (indexed) { |
| 769 | *startIndex += extraIndexOffset; |
| 770 | } |
| 771 | |
| 772 | // all the Pointers must be set if any of these are true |
| 773 | bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty || |
| 774 | vertexOffset != fHWGeometryState.fVertexOffset || |
| 775 | newStride != oldStride; |
| 776 | |
| 777 | // position and tex coord offsets change if above conditions are true |
| 778 | // or the type/normalization changed based on text vs nontext type coords. |
| 779 | bool posAndTexChange = allOffsetsChange || |
| 780 | (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) && |
| 781 | (kTextFormat_VertexLayoutBit & |
| 782 | (fHWGeometryState.fVertexLayout ^ |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 783 | this->getGeomSrc().fVertexLayout))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 784 | |
| 785 | if (posAndTexChange) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 786 | int idx = GrGLProgram::PositionAttributeIdx(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 787 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 788 | (GrGLvoid*)vertexOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 789 | fHWGeometryState.fVertexOffset = vertexOffset; |
| 790 | } |
| 791 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 792 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 793 | if (newTexCoordOffsets[t] > 0) { |
| 794 | GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 795 | int idx = GrGLProgram::TexCoordAttributeIdx(t); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 796 | if (oldTexCoordOffsets[t] <= 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 797 | GL_CALL(EnableVertexAttribArray(idx)); |
| 798 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 799 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 800 | } else if (posAndTexChange || |
| 801 | newTexCoordOffsets[t] != oldTexCoordOffsets[t]) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 802 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 803 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 804 | } |
| 805 | } else if (oldTexCoordOffsets[t] > 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 806 | GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | |
| 810 | if (newColorOffset > 0) { |
| 811 | GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 812 | int idx = GrGLProgram::ColorAttributeIdx(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 813 | if (oldColorOffset <= 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 814 | GL_CALL(EnableVertexAttribArray(idx)); |
| 815 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 816 | true, newStride, colorOffset)); |
| 817 | } else if (allOffsetsChange || newColorOffset != oldColorOffset) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 818 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 819 | true, newStride, colorOffset)); |
| 820 | } |
| 821 | } else if (oldColorOffset > 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 822 | GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx())); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 823 | } |
| 824 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 825 | if (newCoverageOffset > 0) { |
bsalomon@google.com | e10f6fd | 2011-10-11 20:15:26 +0000 | [diff] [blame] | 826 | // bind a single channel, they should all have the same value. |
| 827 | GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 828 | int idx = GrGLProgram::CoverageAttributeIdx(); |
| 829 | if (oldCoverageOffset <= 0) { |
| 830 | GL_CALL(EnableVertexAttribArray(idx)); |
| 831 | GL_CALL(VertexAttribPointer(idx, 1, GR_GL_UNSIGNED_BYTE, |
| 832 | true, newStride, coverageOffset)); |
| 833 | } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) { |
| 834 | GL_CALL(VertexAttribPointer(idx, 1, GR_GL_UNSIGNED_BYTE, |
| 835 | true, newStride, coverageOffset)); |
| 836 | } |
| 837 | } else if (oldCoverageOffset > 0) { |
| 838 | GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx())); |
| 839 | } |
| 840 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 841 | if (newEdgeOffset > 0) { |
| 842 | GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset); |
| 843 | int idx = GrGLProgram::EdgeAttributeIdx(); |
| 844 | if (oldEdgeOffset <= 0) { |
| 845 | GL_CALL(EnableVertexAttribArray(idx)); |
| 846 | GL_CALL(VertexAttribPointer(idx, 4, scalarType, |
| 847 | false, newStride, edgeOffset)); |
| 848 | } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) { |
| 849 | GL_CALL(VertexAttribPointer(idx, 4, scalarType, |
| 850 | false, newStride, edgeOffset)); |
| 851 | } |
| 852 | } else if (oldEdgeOffset > 0) { |
| 853 | GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx())); |
| 854 | } |
| 855 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 856 | fHWGeometryState.fVertexLayout = this->getGeomSrc().fVertexLayout; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 857 | fHWGeometryState.fArrayPtrsDirty = false; |
| 858 | } |
| 859 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 860 | void GrGpuGLShaders::buildProgram(GrPrimitiveType type, |
| 861 | BlendOptFlags blendOpts, |
| 862 | GrBlendCoeff dstCoeff) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 863 | ProgramDesc& desc = fCurrentProgram.fProgramDesc; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 864 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 865 | // This should already have been caught |
| 866 | GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts)); |
| 867 | |
| 868 | bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag); |
| 869 | |
| 870 | bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag | |
| 871 | kEmitCoverage_BlendOptFlag)); |
| 872 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 873 | // The descriptor is used as a cache key. Thus when a field of the |
| 874 | // descriptor will not affect program generation (because of the vertex |
| 875 | // layout in use or other descriptor field settings) it should be set |
| 876 | // to a canonical value to avoid duplicate programs with different keys. |
| 877 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 878 | // Must initialize all fields or cache will have false negatives! |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 879 | desc.fVertexLayout = this->getGeomSrc().fVertexLayout; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 880 | |
| 881 | desc.fEmitsPointSize = kPoints_PrimitiveType == type; |
| 882 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 883 | bool requiresAttributeColors = |
| 884 | !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit); |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame^] | 885 | // fColorInput records how colors are specified for the program. Strip |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 886 | // the bit from the layout to avoid false negatives when searching for an |
| 887 | // existing program in the cache. |
| 888 | desc.fVertexLayout &= ~(kColor_VertexLayoutBit); |
| 889 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 890 | desc.fColorFilterXfermode = skipColor ? |
| 891 | SkXfermode::kDst_Mode : |
| 892 | fCurrDrawState.fColorFilterXfermode; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 893 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 894 | // no reason to do edge aa or look at per-vertex coverage if coverage is |
| 895 | // ignored |
| 896 | if (skipCoverage) { |
| 897 | desc.fVertexLayout &= ~(kEdge_VertexLayoutBit | |
| 898 | kCoverage_VertexLayoutBit); |
| 899 | } |
| 900 | |
| 901 | bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag); |
| 902 | bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) || |
| 903 | (!requiresAttributeColors && |
| 904 | 0xffffffff == fCurrDrawState.fColor); |
| 905 | if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame^] | 906 | desc.fColorInput = ProgramDesc::kTransBlack_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 907 | } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame^] | 908 | desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 909 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame^] | 910 | desc.fColorInput = ProgramDesc::kUniform_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 911 | } else { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame^] | 912 | desc.fColorInput = ProgramDesc::kAttribute_ColorInput; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 913 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 914 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 915 | desc.fEdgeAANumEdges = skipCoverage ? 0 : fCurrDrawState.fEdgeAANumEdges; |
| 916 | desc.fEdgeAAConcave = desc.fEdgeAANumEdges > 0 && |
| 917 | SkToBool(fCurrDrawState.fFlagBits & |
| 918 | kEdgeAAConcave_StateBit); |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 919 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 920 | int lastEnabledStage = -1; |
| 921 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 922 | if (!skipCoverage && (desc.fVertexLayout & |
| 923 | GrDrawTarget::kEdge_VertexLayoutBit)) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 924 | desc.fVertexEdgeType = fCurrDrawState.fVertexEdgeType; |
| 925 | } else { |
| 926 | // use canonical value when not set to avoid cache misses |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 927 | desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 928 | } |
| 929 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 930 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 931 | StageDesc& stage = desc.fStages[s]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 932 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 933 | stage.fOptFlags = 0; |
| 934 | stage.setEnabled(this->isStageEnabled(s)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 935 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 936 | bool skip = s < fCurrDrawState.fFirstCoverageStage ? skipColor : |
| 937 | skipCoverage; |
| 938 | |
| 939 | if (!skip && stage.isEnabled()) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 940 | lastEnabledStage = s; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 941 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; |
| 942 | GrAssert(NULL != texture); |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 943 | const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 944 | // we matrix to invert when orientation is TopDown, so make sure |
| 945 | // we aren't in that case before flagging as identity. |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 946 | if (TextureMatrixIsIdentity(texture, sampler)) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 947 | stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 948 | } else if (!getSamplerMatrix(s).hasPerspective()) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 949 | stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 950 | } |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 951 | switch (sampler.getSampleMode()) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 952 | case GrSamplerState::kNormal_SampleMode: |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 953 | stage.fCoordMapping = StageDesc::kIdentity_CoordMapping; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 954 | break; |
| 955 | case GrSamplerState::kRadial_SampleMode: |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 956 | stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 957 | break; |
| 958 | case GrSamplerState::kRadial2_SampleMode: |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 959 | if (sampler.radial2IsDegenerate()) { |
| 960 | stage.fCoordMapping = |
| 961 | StageDesc::kRadial2GradientDegenerate_CoordMapping; |
| 962 | } else { |
| 963 | stage.fCoordMapping = |
| 964 | StageDesc::kRadial2Gradient_CoordMapping; |
| 965 | } |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 966 | break; |
| 967 | case GrSamplerState::kSweep_SampleMode: |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 968 | stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 969 | break; |
| 970 | default: |
| 971 | GrCrash("Unexpected sample mode!"); |
| 972 | break; |
| 973 | } |
| 974 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 975 | switch (sampler.getFilter()) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 976 | // these both can use a regular texture2D() |
| 977 | case GrSamplerState::kNearest_Filter: |
| 978 | case GrSamplerState::kBilinear_Filter: |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 979 | stage.fFetchMode = StageDesc::kSingle_FetchMode; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 980 | break; |
| 981 | // performs 4 texture2D()s |
| 982 | case GrSamplerState::k4x4Downsample_Filter: |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 983 | stage.fFetchMode = StageDesc::k2x2_FetchMode; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 984 | break; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 985 | // performs fKernelWidth texture2D()s |
| 986 | case GrSamplerState::kConvolution_Filter: |
| 987 | stage.fFetchMode = StageDesc::kConvolution_FetchMode; |
| 988 | break; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 989 | default: |
| 990 | GrCrash("Unexpected filter!"); |
| 991 | break; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 992 | } |
| 993 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 994 | if (sampler.hasTextureDomain()) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 995 | GrAssert(GrSamplerState::kClamp_WrapMode == |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 996 | sampler.getWrapX() && |
| 997 | GrSamplerState::kClamp_WrapMode == |
| 998 | sampler.getWrapY()); |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 999 | stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame^] | 1002 | if (!this->glCaps().fTextureSwizzle && |
| 1003 | GrPixelConfigIsAlphaOnly(texture->config())) { |
| 1004 | // if we don't have texture swizzle support then |
| 1005 | // the shader must do an alpha smear after reading |
| 1006 | // the texture |
| 1007 | stage.fInputConfig = StageDesc::kAlphaOnly_InputConfig; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1008 | } else { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame^] | 1009 | stage.fInputConfig = StageDesc::kColor_InputConfig; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1010 | } |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1011 | if (sampler.getFilter() == GrSamplerState::kConvolution_Filter) { |
| 1012 | stage.fKernelWidth = sampler.getKernelWidth(); |
| 1013 | } else { |
| 1014 | stage.fKernelWidth = 0; |
| 1015 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1016 | } else { |
| 1017 | stage.fOptFlags = 0; |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 1018 | stage.fCoordMapping = (StageDesc::CoordMapping)0; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame^] | 1019 | stage.fInputConfig = (StageDesc::InputConfig)0; |
| 1020 | stage.fFetchMode = (StageDesc::FetchMode) 0; |
| 1021 | stage.fKernelWidth = 0; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1022 | } |
| 1023 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1024 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 1025 | desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1026 | |
| 1027 | // currently the experimental GS will only work with triangle prims |
| 1028 | // (and it doesn't do anything other than pass through values from |
| 1029 | // the VS to the FS anyway). |
| 1030 | #if 0 && GR_GL_EXPERIMENTAL_GS |
| 1031 | desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport; |
| 1032 | #endif |
| 1033 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1034 | // we want to avoid generating programs with different "first cov stage" |
| 1035 | // values when they would compute the same result. |
| 1036 | // We set field in the desc to kNumStages when either there are no |
| 1037 | // coverage stages or the distinction between coverage and color is |
| 1038 | // immaterial. |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1039 | int firstCoverageStage = GrDrawState::kNumStages; |
| 1040 | desc.fFirstCoverageStage = GrDrawState::kNumStages; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1041 | bool hasCoverage = fCurrDrawState.fFirstCoverageStage <= lastEnabledStage; |
| 1042 | if (hasCoverage) { |
| 1043 | firstCoverageStage = fCurrDrawState.fFirstCoverageStage; |
| 1044 | } |
| 1045 | |
| 1046 | // other coverage inputs |
| 1047 | if (!hasCoverage) { |
| 1048 | hasCoverage = |
| 1049 | desc.fEdgeAANumEdges || |
| 1050 | (desc.fVertexLayout & GrDrawTarget::kCoverage_VertexLayoutBit) || |
| 1051 | (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit); |
| 1052 | } |
| 1053 | |
| 1054 | if (hasCoverage) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1055 | // color filter is applied between color/coverage computation |
| 1056 | if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1057 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1060 | if (this->getCaps().fDualSourceBlendingSupport && |
| 1061 | !(blendOpts & (kEmitCoverage_BlendOptFlag | |
| 1062 | kCoverageAsAlpha_BlendOptFlag))) { |
| 1063 | if (kZero_BlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1064 | // write the coverage value to second color |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 1065 | desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1066 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1067 | } else if (kSA_BlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1068 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially |
| 1069 | // cover |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 1070 | desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1071 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1072 | } else if (kSC_BlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1073 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially |
| 1074 | // cover |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 1075 | desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1076 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1080 | } |