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