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