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