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