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