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