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