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