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