junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2011 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "GrBinHashKey.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 18 | #include "GrGLProgram.h" |
| 19 | #include "GrGpuGLShaders.h" |
| 20 | #include "GrGpuVertex.h" |
| 21 | #include "GrMemory.h" |
| 22 | #include "GrNoncopyable.h" |
| 23 | #include "GrStringBuilder.h" |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 24 | #include "GrRandom.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 25 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 26 | #define SKIP_CACHE_CHECK true |
| 27 | #define GR_UINT32_MAX static_cast<uint32_t>(-1) |
| 28 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 29 | #include "GrTHashCache.h" |
| 30 | |
| 31 | class GrGpuGLShaders::ProgramCache : public ::GrNoncopyable { |
| 32 | private: |
| 33 | class Entry; |
| 34 | |
| 35 | #if GR_DEBUG |
| 36 | typedef GrBinHashKey<Entry, 4> ProgramHashKey; // Flex the dynamic allocation muscle in debug |
| 37 | #else |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 38 | typedef GrBinHashKey<Entry, 64> ProgramHashKey; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
| 41 | class Entry : public ::GrNoncopyable { |
| 42 | public: |
| 43 | Entry() {} |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 44 | void copyAndTakeOwnership(Entry& entry) { |
| 45 | fProgramData.copyAndTakeOwnership(entry.fProgramData); |
| 46 | fKey.copyAndTakeOwnership(entry.fKey); // ownership transfer |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 47 | fLRUStamp = entry.fLRUStamp; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | public: |
| 51 | int compare(const ProgramHashKey& key) const { return fKey.compare(key); } |
| 52 | |
| 53 | public: |
| 54 | GrGLProgram::CachedData fProgramData; |
| 55 | ProgramHashKey fKey; |
| 56 | unsigned int fLRUStamp; |
| 57 | }; |
| 58 | |
| 59 | GrTHashTable<Entry, ProgramHashKey, 8> fHashCache; |
| 60 | |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 61 | // We may have kMaxEntries+1 shaders in the GL context because |
| 62 | // we create a new shader before evicting from the cache. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 63 | enum { |
| 64 | kMaxEntries = 32 |
| 65 | }; |
| 66 | Entry fEntries[kMaxEntries]; |
| 67 | int fCount; |
| 68 | unsigned int fCurrLRUStamp; |
| 69 | |
| 70 | public: |
| 71 | ProgramCache() |
| 72 | : fCount(0) |
| 73 | , fCurrLRUStamp(0) { |
| 74 | } |
| 75 | |
| 76 | ~ProgramCache() { |
| 77 | for (int i = 0; i < fCount; ++i) { |
| 78 | GrGpuGLShaders::DeleteProgram(&fEntries[i].fProgramData); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void abandon() { |
| 83 | fCount = 0; |
| 84 | } |
| 85 | |
| 86 | void invalidateViewMatrices() { |
| 87 | for (int i = 0; i < fCount; ++i) { |
| 88 | // set to illegal matrix |
| 89 | fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix(); |
| 90 | } |
| 91 | } |
| 92 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 93 | GrGLProgram::CachedData* getProgramData(const GrGLProgram& desc) { |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 94 | Entry newEntry; |
| 95 | while (newEntry.fKey.doPass()) { |
| 96 | desc.buildKey(newEntry.fKey); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 97 | } |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 98 | Entry* entry = fHashCache.find(newEntry.fKey); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 99 | if (NULL == entry) { |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 100 | if (!desc.genProgram(&newEntry.fProgramData)) { |
| 101 | return NULL; |
| 102 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 103 | if (fCount < kMaxEntries) { |
| 104 | entry = fEntries + fCount; |
| 105 | ++fCount; |
| 106 | } else { |
| 107 | GrAssert(kMaxEntries == fCount); |
| 108 | entry = fEntries; |
| 109 | for (int i = 1; i < kMaxEntries; ++i) { |
| 110 | if (fEntries[i].fLRUStamp < entry->fLRUStamp) { |
| 111 | entry = fEntries + i; |
| 112 | } |
| 113 | } |
| 114 | fHashCache.remove(entry->fKey, entry); |
| 115 | GrGpuGLShaders::DeleteProgram(&entry->fProgramData); |
| 116 | } |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 117 | entry->copyAndTakeOwnership(newEntry); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 118 | fHashCache.insert(entry->fKey, entry); |
| 119 | } |
| 120 | |
| 121 | entry->fLRUStamp = fCurrLRUStamp; |
| 122 | if (GR_UINT32_MAX == fCurrLRUStamp) { |
| 123 | // wrap around! just trash our LRU, one time hit. |
| 124 | for (int i = 0; i < fCount; ++i) { |
| 125 | fEntries[i].fLRUStamp = 0; |
| 126 | } |
| 127 | } |
| 128 | ++fCurrLRUStamp; |
| 129 | return &entry->fProgramData; |
| 130 | } |
| 131 | }; |
| 132 | |
junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 133 | void GrGpuGLShaders::abandonResources(){ |
| 134 | INHERITED::abandonResources(); |
| 135 | fProgramCache->abandon(); |
| 136 | } |
| 137 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 138 | void GrGpuGLShaders::DeleteProgram(GrGLProgram::CachedData* programData) { |
| 139 | GR_GL(DeleteShader(programData->fVShaderID)); |
| 140 | GR_GL(DeleteShader(programData->fFShaderID)); |
| 141 | GR_GL(DeleteProgram(programData->fProgramID)); |
| 142 | GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));) |
| 143 | } |
| 144 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 145 | void GrGpuGLShaders::ProgramUnitTest() { |
| 146 | |
| 147 | static const int STAGE_OPTS[] = { |
| 148 | 0, |
| 149 | GrGLProgram::ProgramDesc::StageDesc::kNoPerspective_OptFlagBit, |
| 150 | GrGLProgram::ProgramDesc::StageDesc::kIdentity_CoordMapping |
| 151 | }; |
| 152 | static const GrGLProgram::ProgramDesc::StageDesc::Modulation STAGE_MODULATES[] = { |
| 153 | GrGLProgram::ProgramDesc::StageDesc::kColor_Modulation, |
| 154 | GrGLProgram::ProgramDesc::StageDesc::kAlpha_Modulation |
| 155 | }; |
| 156 | static const GrGLProgram::ProgramDesc::StageDesc::CoordMapping STAGE_COORD_MAPPINGS[] = { |
| 157 | GrGLProgram::ProgramDesc::StageDesc::kIdentity_CoordMapping, |
| 158 | GrGLProgram::ProgramDesc::StageDesc::kRadialGradient_CoordMapping, |
| 159 | GrGLProgram::ProgramDesc::StageDesc::kSweepGradient_CoordMapping, |
| 160 | GrGLProgram::ProgramDesc::StageDesc::kRadial2Gradient_CoordMapping |
| 161 | }; |
| 162 | static const GrGLProgram::ProgramDesc::StageDesc::FetchMode FETCH_MODES[] = { |
| 163 | GrGLProgram::ProgramDesc::StageDesc::kSingle_FetchMode, |
| 164 | GrGLProgram::ProgramDesc::StageDesc::k2x2_FetchMode, |
| 165 | }; |
| 166 | GrGLProgram program; |
| 167 | GrGLProgram::ProgramDesc& pdesc = program.fProgramDesc; |
| 168 | |
| 169 | static const int NUM_TESTS = 512; |
| 170 | |
| 171 | // GrRandoms nextU() values have patterns in the low bits |
| 172 | // So using nextU() % array_count might never take some values. |
| 173 | GrRandom random; |
| 174 | for (int t = 0; t < NUM_TESTS; ++t) { |
| 175 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 176 | #if 0 |
| 177 | GrPrintf("\nTest Program %d\n-------------\n", t); |
| 178 | static const int stop = -1; |
| 179 | if (t == stop) { |
| 180 | int breakpointhere = 9; |
| 181 | } |
| 182 | #endif |
| 183 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 184 | pdesc.fVertexLayout = 0; |
| 185 | pdesc.fEmitsPointSize = random.nextF() > .5f; |
| 186 | float colorType = random.nextF(); |
| 187 | if (colorType < 1.f / 3.f) { |
| 188 | pdesc.fColorType = GrGLProgram::ProgramDesc::kAttribute_ColorType; |
| 189 | } else if (colorType < 2.f / 3.f) { |
| 190 | pdesc.fColorType = GrGLProgram::ProgramDesc::kUniform_ColorType; |
| 191 | } else { |
| 192 | pdesc.fColorType = GrGLProgram::ProgramDesc::kNone_ColorType; |
| 193 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 194 | |
| 195 | int idx = (int)(random.nextF() * (SkXfermode::kCoeffModesCnt)); |
| 196 | pdesc.fColorFilterXfermode = (SkXfermode::Mode)idx; |
| 197 | |
| 198 | idx = (int)(random.nextF() * (kNumStages+1)); |
| 199 | pdesc.fFirstCoverageStage = idx; |
| 200 | |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 201 | pdesc.fEdgeAANumEdges = (random.nextF() * (getMaxEdges() + 1)); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 202 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 203 | if (fDualSourceBlendingSupport) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 204 | pdesc.fDualSrcOutput = |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 205 | (GrGLProgram::ProgramDesc::DualSrcOutput) |
| 206 | (int)(random.nextF() * GrGLProgram::ProgramDesc::kDualSrcOutputCnt); |
| 207 | } else { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 208 | pdesc.fDualSrcOutput = |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 209 | GrGLProgram::ProgramDesc::kNone_DualSrcOutput; |
| 210 | } |
| 211 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 212 | for (int s = 0; s < kNumStages; ++s) { |
| 213 | // enable the stage? |
| 214 | if (random.nextF() > .5f) { |
| 215 | // use separate tex coords? |
| 216 | if (random.nextF() > .5f) { |
| 217 | int t = (int)(random.nextF() * kMaxTexCoords); |
| 218 | pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t); |
| 219 | } else { |
| 220 | pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s); |
| 221 | } |
| 222 | } |
| 223 | // use text-formatted verts? |
| 224 | if (random.nextF() > .5f) { |
| 225 | pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit; |
| 226 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 227 | idx = (int)(random.nextF() * GR_ARRAY_COUNT(STAGE_OPTS)); |
| 228 | pdesc.fStages[s].fOptFlags = STAGE_OPTS[idx]; |
| 229 | idx = (int)(random.nextF() * GR_ARRAY_COUNT(STAGE_MODULATES)); |
| 230 | pdesc.fStages[s].fModulation = STAGE_MODULATES[idx]; |
| 231 | idx = (int)(random.nextF() * GR_ARRAY_COUNT(STAGE_COORD_MAPPINGS)); |
| 232 | pdesc.fStages[s].fCoordMapping = STAGE_COORD_MAPPINGS[idx]; |
| 233 | idx = (int)(random.nextF() * GR_ARRAY_COUNT(FETCH_MODES)); |
| 234 | pdesc.fStages[s].fFetchMode = FETCH_MODES[idx]; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 235 | pdesc.fStages[s].setEnabled(VertexUsesStage(s, pdesc.fVertexLayout)); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 236 | } |
| 237 | GrGLProgram::CachedData cachedData; |
| 238 | program.genProgram(&cachedData); |
| 239 | DeleteProgram(&cachedData); |
| 240 | bool again = false; |
| 241 | if (again) { |
| 242 | program.genProgram(&cachedData); |
| 243 | DeleteProgram(&cachedData); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 248 | GrGpuGLShaders::GrGpuGLShaders() { |
| 249 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 250 | resetContext(); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 251 | int major, minor; |
| 252 | gl_version(&major, &minor); |
| 253 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 254 | f4X4DownsampleFilterSupport = true; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 255 | if (GR_GL_SUPPORT_DESKTOP) { |
| 256 | fDualSourceBlendingSupport = |
| 257 | major > 3 ||(3 == major && 3 <= minor) || |
| 258 | has_gl_extension("GL_ARB_blend_func_extended"); |
| 259 | } else { |
| 260 | fDualSourceBlendingSupport = false; |
| 261 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 262 | |
| 263 | fProgramData = NULL; |
| 264 | fProgramCache = new ProgramCache(); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 265 | |
| 266 | #if 0 |
| 267 | ProgramUnitTest(); |
| 268 | #endif |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | GrGpuGLShaders::~GrGpuGLShaders() { |
| 272 | delete fProgramCache; |
| 273 | } |
| 274 | |
| 275 | const GrMatrix& GrGpuGLShaders::getHWSamplerMatrix(int stage) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 276 | GrAssert(fProgramData); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 277 | |
| 278 | if (GrGLProgram::kSetAsAttribute == |
| 279 | fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) { |
| 280 | return fHWDrawState.fSamplerStates[stage].getMatrix(); |
| 281 | } else { |
| 282 | return fProgramData->fTextureMatrices[stage]; |
| 283 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void GrGpuGLShaders::recordHWSamplerMatrix(int stage, const GrMatrix& matrix) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 287 | GrAssert(fProgramData); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 288 | if (GrGLProgram::kSetAsAttribute == |
| 289 | fProgramData->fUniLocations.fStages[stage].fTextureMatrixUni) { |
| 290 | fHWDrawState.fSamplerStates[stage].setMatrix(matrix); |
| 291 | } else { |
| 292 | fProgramData->fTextureMatrices[stage] = matrix; |
| 293 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void GrGpuGLShaders::resetContext() { |
| 297 | INHERITED::resetContext(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 298 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 299 | fHWGeometryState.fVertexLayout = 0; |
| 300 | fHWGeometryState.fVertexOffset = ~0; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 301 | GR_GL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx())); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 302 | for (int t = 0; t < kMaxTexCoords; ++t) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 303 | GR_GL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 304 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 305 | GR_GL(EnableVertexAttribArray(GrGLProgram::PositionAttributeIdx())); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 306 | |
| 307 | fHWProgramID = 0; |
| 308 | } |
| 309 | |
| 310 | void GrGpuGLShaders::flushViewMatrix() { |
| 311 | GrAssert(NULL != fCurrDrawState.fRenderTarget); |
bsalomon@google.com | cc4dac3 | 2011-05-10 13:52:42 +0000 | [diff] [blame] | 312 | GrMatrix m; |
| 313 | m.setAll( |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 314 | GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1, |
| 315 | 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1, |
| 316 | 0, 0, GrMatrix::I()[8]); |
| 317 | m.setConcat(m, fCurrDrawState.fViewMatrix); |
| 318 | |
| 319 | // ES doesn't allow you to pass true to the transpose param, |
| 320 | // so do our own transpose |
bsalomon@google.com | 27a4dc4 | 2011-05-16 13:14:03 +0000 | [diff] [blame] | 321 | GrGLfloat mt[] = { |
| 322 | GrScalarToFloat(m[GrMatrix::kMScaleX]), |
| 323 | GrScalarToFloat(m[GrMatrix::kMSkewY]), |
| 324 | GrScalarToFloat(m[GrMatrix::kMPersp0]), |
| 325 | GrScalarToFloat(m[GrMatrix::kMSkewX]), |
| 326 | GrScalarToFloat(m[GrMatrix::kMScaleY]), |
| 327 | GrScalarToFloat(m[GrMatrix::kMPersp1]), |
| 328 | GrScalarToFloat(m[GrMatrix::kMTransX]), |
| 329 | GrScalarToFloat(m[GrMatrix::kMTransY]), |
| 330 | GrScalarToFloat(m[GrMatrix::kMPersp2]) |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 331 | }; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 332 | |
| 333 | if (GrGLProgram::kSetAsAttribute == |
| 334 | fProgramData->fUniLocations.fViewMatrixUni) { |
| 335 | int baseIdx = GrGLProgram::ViewMatrixAttributeIdx(); |
| 336 | GR_GL(VertexAttrib4fv(baseIdx + 0, mt+0)); |
| 337 | GR_GL(VertexAttrib4fv(baseIdx + 1, mt+3)); |
| 338 | GR_GL(VertexAttrib4fv(baseIdx + 2, mt+6)); |
| 339 | } else { |
| 340 | GrAssert(GrGLProgram::kUnusedUniform != |
| 341 | fProgramData->fUniLocations.fViewMatrixUni); |
| 342 | GR_GL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni, |
| 343 | 1, false, mt)); |
| 344 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 345 | } |
| 346 | |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 347 | void GrGpuGLShaders::flushTextureDomain(int s) { |
| 348 | const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni; |
| 349 | if (GrGLProgram::kUnusedUniform != uni) { |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 350 | const GrRect &texDom = |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 351 | fCurrDrawState.fSamplerStates[s].getTextureDomain(); |
| 352 | |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 353 | if (((1 << s) & fDirtyFlags.fTextureChangedMask) || |
| 354 | fProgramData->fTextureDomain[s] != texDom) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 355 | |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 356 | fProgramData->fTextureDomain[s] = texDom; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 357 | |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 358 | float values[4] = { |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 359 | GrScalarToFloat(texDom.left()), |
| 360 | GrScalarToFloat(texDom.top()), |
| 361 | GrScalarToFloat(texDom.right()), |
| 362 | GrScalarToFloat(texDom.bottom()) |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 363 | }; |
| 364 | |
| 365 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; |
| 366 | GrGLTexture::Orientation orientation = texture->orientation(); |
| 367 | |
| 368 | // vertical flip if necessary |
| 369 | if (GrGLTexture::kBottomUp_Orientation == orientation) { |
| 370 | values[1] = 1.0f - values[1]; |
| 371 | values[3] = 1.0f - values[3]; |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 372 | // The top and bottom were just flipped, so correct the ordering |
| 373 | // of elements so that values = (l, t, r, b). |
| 374 | SkTSwap(values[1], values[3]); |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | values[0] *= SkScalarToFloat(texture->contentScaleX()); |
| 378 | values[2] *= SkScalarToFloat(texture->contentScaleX()); |
| 379 | values[1] *= SkScalarToFloat(texture->contentScaleY()); |
| 380 | values[3] *= SkScalarToFloat(texture->contentScaleY()); |
| 381 | |
| 382 | GR_GL(Uniform4fv(uni, 1, values)); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 383 | } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 387 | void GrGpuGLShaders::flushTextureMatrix(int s) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 388 | const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 389 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; |
| 390 | if (NULL != texture) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 391 | if (GrGLProgram::kUnusedUniform != uni && |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 392 | (((1 << s) & fDirtyFlags.fTextureChangedMask) || |
| 393 | getHWSamplerMatrix(s) != getSamplerMatrix(s))) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 394 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 395 | GrAssert(NULL != fCurrDrawState.fTextures[s]); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 396 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 397 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 398 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 399 | GrMatrix m = getSamplerMatrix(s); |
| 400 | GrSamplerState::SampleMode mode = |
| 401 | fCurrDrawState.fSamplerStates[s].getSampleMode(); |
| 402 | AdjustTextureMatrix(texture, mode, &m); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 403 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 404 | // ES doesn't allow you to pass true to the transpose param, |
| 405 | // so do our own transpose |
bsalomon@google.com | 27a4dc4 | 2011-05-16 13:14:03 +0000 | [diff] [blame] | 406 | GrGLfloat mt[] = { |
| 407 | GrScalarToFloat(m[GrMatrix::kMScaleX]), |
| 408 | GrScalarToFloat(m[GrMatrix::kMSkewY]), |
| 409 | GrScalarToFloat(m[GrMatrix::kMPersp0]), |
| 410 | GrScalarToFloat(m[GrMatrix::kMSkewX]), |
| 411 | GrScalarToFloat(m[GrMatrix::kMScaleY]), |
| 412 | GrScalarToFloat(m[GrMatrix::kMPersp1]), |
| 413 | GrScalarToFloat(m[GrMatrix::kMTransX]), |
| 414 | GrScalarToFloat(m[GrMatrix::kMTransY]), |
| 415 | GrScalarToFloat(m[GrMatrix::kMPersp2]) |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 416 | }; |
bsalomon@google.com | 27a4dc4 | 2011-05-16 13:14:03 +0000 | [diff] [blame] | 417 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 418 | if (GrGLProgram::kSetAsAttribute == |
| 419 | fProgramData->fUniLocations.fStages[s].fTextureMatrixUni) { |
| 420 | int baseIdx = GrGLProgram::TextureMatrixAttributeIdx(s); |
| 421 | GR_GL(VertexAttrib4fv(baseIdx + 0, mt+0)); |
| 422 | GR_GL(VertexAttrib4fv(baseIdx + 1, mt+3)); |
| 423 | GR_GL(VertexAttrib4fv(baseIdx + 2, mt+6)); |
| 424 | } else { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 425 | GR_GL(UniformMatrix3fv(uni, 1, false, mt)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 426 | } |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 427 | recordHWSamplerMatrix(s, getSamplerMatrix(s)); |
| 428 | } |
| 429 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 430 | } |
| 431 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 432 | void GrGpuGLShaders::flushRadial2(int s) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 433 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 434 | const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni; |
| 435 | const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s]; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 436 | if (GrGLProgram::kUnusedUniform != uni && |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 437 | (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() || |
| 438 | fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() || |
| 439 | fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 440 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 441 | GrScalar centerX1 = sampler.getRadial2CenterX1(); |
| 442 | GrScalar radius0 = sampler.getRadial2Radius0(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 443 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 444 | GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 445 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 446 | float values[6] = { |
| 447 | GrScalarToFloat(a), |
| 448 | 1 / (2.f * values[0]), |
| 449 | GrScalarToFloat(centerX1), |
| 450 | GrScalarToFloat(radius0), |
| 451 | GrScalarToFloat(GrMul(radius0, radius0)), |
| 452 | sampler.isRadial2PosRoot() ? 1.f : -1.f |
| 453 | }; |
| 454 | GR_GL(Uniform1fv(uni, 6, values)); |
| 455 | fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1(); |
| 456 | fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0(); |
| 457 | fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot(); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | void GrGpuGLShaders::flushTexelSize(int s) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 462 | const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 463 | if (GrGLProgram::kUnusedUniform != uni) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 464 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; |
| 465 | if (texture->allocWidth() != fProgramData->fTextureWidth[s] || |
| 466 | texture->allocHeight() != fProgramData->fTextureWidth[s]) { |
| 467 | |
| 468 | float texelSize[] = {1.f / texture->allocWidth(), |
| 469 | 1.f / texture->allocHeight()}; |
| 470 | GR_GL(Uniform2fv(uni, 1, texelSize)); |
| 471 | } |
| 472 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 473 | } |
| 474 | |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 475 | void GrGpuGLShaders::flushEdgeAAData() { |
| 476 | const int& uni = fProgramData->fUniLocations.fEdgesUni; |
| 477 | if (GrGLProgram::kUnusedUniform != uni) { |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 478 | int count = fCurrDrawState.fEdgeAANumEdges; |
| 479 | Edge edges[kMaxEdges]; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 480 | // Flip the edges in Y |
| 481 | float height = fCurrDrawState.fRenderTarget->height(); |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 482 | for (int i = 0; i < count; ++i) { |
| 483 | edges[i] = fCurrDrawState.fEdgeAAEdges[i]; |
| 484 | float b = edges[i].fY; |
| 485 | edges[i].fY = -b; |
| 486 | edges[i].fZ += b * height; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 487 | } |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 488 | GR_GL(Uniform3fv(uni, count, &edges[0].fX)); |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Scroggo | 01b87ec | 2011-05-11 18:05:38 +0000 | [diff] [blame] | 492 | static const float ONE_OVER_255 = 1.f / 255.f; |
| 493 | |
| 494 | #define GR_COLOR_TO_VEC4(color) {\ |
| 495 | GrColorUnpackR(color) * ONE_OVER_255,\ |
| 496 | GrColorUnpackG(color) * ONE_OVER_255,\ |
| 497 | GrColorUnpackB(color) * ONE_OVER_255,\ |
| 498 | GrColorUnpackA(color) * ONE_OVER_255 \ |
| 499 | } |
| 500 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 501 | void GrGpuGLShaders::flushColor() { |
| 502 | const GrGLProgram::ProgramDesc& desc = fCurrentProgram.getDesc(); |
| 503 | if (fGeometrySrc.fVertexLayout & kColor_VertexLayoutBit) { |
| 504 | // color will be specified per-vertex as an attribute |
| 505 | // invalidate the const vertex attrib color |
| 506 | fHWDrawState.fColor = GrColor_ILLEGAL; |
| 507 | } else { |
| 508 | switch (desc.fColorType) { |
| 509 | case GrGLProgram::ProgramDesc::kAttribute_ColorType: |
| 510 | if (fHWDrawState.fColor != fCurrDrawState.fColor) { |
| 511 | // OpenGL ES only supports the float varities of glVertexAttrib |
Scroggo | 01b87ec | 2011-05-11 18:05:38 +0000 | [diff] [blame] | 512 | float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 513 | GR_GL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(), c)); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 514 | fHWDrawState.fColor = fCurrDrawState.fColor; |
| 515 | } |
| 516 | break; |
| 517 | case GrGLProgram::ProgramDesc::kUniform_ColorType: |
| 518 | if (fProgramData->fColor != fCurrDrawState.fColor) { |
| 519 | // OpenGL ES only supports the float varities of glVertexAttrib |
Scroggo | 01b87ec | 2011-05-11 18:05:38 +0000 | [diff] [blame] | 520 | float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColor); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 521 | GrAssert(GrGLProgram::kUnusedUniform != |
| 522 | fProgramData->fUniLocations.fColorUni); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 523 | GR_GL(Uniform4fv(fProgramData->fUniLocations.fColorUni, 1, c)); |
| 524 | fProgramData->fColor = fCurrDrawState.fColor; |
| 525 | } |
| 526 | break; |
| 527 | case GrGLProgram::ProgramDesc::kNone_ColorType: |
| 528 | GrAssert(0xffffffff == fCurrDrawState.fColor); |
| 529 | break; |
| 530 | default: |
| 531 | GrCrash("Unknown color type."); |
| 532 | } |
| 533 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 534 | if (fProgramData->fUniLocations.fColorFilterUni |
| 535 | != GrGLProgram::kUnusedUniform |
| 536 | && fProgramData->fColorFilterColor |
| 537 | != fCurrDrawState.fColorFilterColor) { |
Scroggo | 01b87ec | 2011-05-11 18:05:38 +0000 | [diff] [blame] | 538 | float c[] = GR_COLOR_TO_VEC4(fCurrDrawState.fColorFilterColor); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 539 | GR_GL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c)); |
| 540 | fProgramData->fColorFilterColor = fCurrDrawState.fColorFilterColor; |
| 541 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 545 | bool GrGpuGLShaders::flushGraphicsState(GrPrimitiveType type) { |
| 546 | if (!flushGLStateCommon(type)) { |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | if (fDirtyFlags.fRenderTargetChanged) { |
| 551 | // our coords are in pixel space and the GL matrices map to NDC |
| 552 | // so if the viewport changed, our matrix is now wrong. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 553 | fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 554 | // we assume all shader matrices may be wrong after viewport changes |
| 555 | fProgramCache->invalidateViewMatrices(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 556 | } |
| 557 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 558 | buildProgram(type); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 559 | fProgramData = fProgramCache->getProgramData(fCurrentProgram); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 560 | if (NULL == fProgramData) { |
| 561 | GrAssert(!"Failed to create program!"); |
| 562 | return false; |
| 563 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 564 | |
| 565 | if (fHWProgramID != fProgramData->fProgramID) { |
| 566 | GR_GL(UseProgram(fProgramData->fProgramID)); |
| 567 | fHWProgramID = fProgramData->fProgramID; |
| 568 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 569 | GrBlendCoeff srcCoeff = fCurrDrawState.fSrcBlend; |
| 570 | GrBlendCoeff dstCoeff = fCurrDrawState.fDstBlend; |
| 571 | |
| 572 | fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff); |
| 573 | this->flushBlend(type, srcCoeff, dstCoeff); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 574 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 575 | this->flushColor(); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 576 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 577 | GrMatrix* currViewMatrix; |
| 578 | if (GrGLProgram::kSetAsAttribute == |
| 579 | fProgramData->fUniLocations.fViewMatrixUni) { |
| 580 | currViewMatrix = &fHWDrawState.fViewMatrix; |
| 581 | } else { |
| 582 | currViewMatrix = &fProgramData->fViewMatrix; |
| 583 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 584 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 585 | if (*currViewMatrix != fCurrDrawState.fViewMatrix) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 586 | flushViewMatrix(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 587 | *currViewMatrix = fCurrDrawState.fViewMatrix; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | for (int s = 0; s < kNumStages; ++s) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 591 | this->flushTextureMatrix(s); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 592 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 593 | this->flushRadial2(s); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 594 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 595 | this->flushTexelSize(s); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 596 | |
| 597 | this->flushTextureDomain(s); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 598 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 599 | this->flushEdgeAAData(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 600 | resetDirtyFlags(); |
| 601 | return true; |
| 602 | } |
| 603 | |
| 604 | void GrGpuGLShaders::postDraw() { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | void GrGpuGLShaders::setupGeometry(int* startVertex, |
| 608 | int* startIndex, |
| 609 | int vertexCount, |
| 610 | int indexCount) { |
| 611 | |
| 612 | int newColorOffset; |
| 613 | int newTexCoordOffsets[kMaxTexCoords]; |
| 614 | |
| 615 | GrGLsizei newStride = VertexSizeAndOffsetsByIdx(fGeometrySrc.fVertexLayout, |
| 616 | newTexCoordOffsets, |
| 617 | &newColorOffset); |
| 618 | int oldColorOffset; |
| 619 | int oldTexCoordOffsets[kMaxTexCoords]; |
| 620 | GrGLsizei oldStride = VertexSizeAndOffsetsByIdx(fHWGeometryState.fVertexLayout, |
| 621 | oldTexCoordOffsets, |
| 622 | &oldColorOffset); |
| 623 | bool indexed = NULL != startIndex; |
| 624 | |
| 625 | int extraVertexOffset; |
| 626 | int extraIndexOffset; |
| 627 | setBuffers(indexed, &extraVertexOffset, &extraIndexOffset); |
| 628 | |
| 629 | GrGLenum scalarType; |
| 630 | bool texCoordNorm; |
| 631 | if (fGeometrySrc.fVertexLayout & kTextFormat_VertexLayoutBit) { |
| 632 | scalarType = GrGLTextType; |
| 633 | texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED; |
| 634 | } else { |
| 635 | scalarType = GrGLType; |
| 636 | texCoordNorm = false; |
| 637 | } |
| 638 | |
| 639 | size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride; |
| 640 | *startVertex = 0; |
| 641 | if (indexed) { |
| 642 | *startIndex += extraIndexOffset; |
| 643 | } |
| 644 | |
| 645 | // all the Pointers must be set if any of these are true |
| 646 | bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty || |
| 647 | vertexOffset != fHWGeometryState.fVertexOffset || |
| 648 | newStride != oldStride; |
| 649 | |
| 650 | // position and tex coord offsets change if above conditions are true |
| 651 | // or the type/normalization changed based on text vs nontext type coords. |
| 652 | bool posAndTexChange = allOffsetsChange || |
| 653 | (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) && |
| 654 | (kTextFormat_VertexLayoutBit & |
| 655 | (fHWGeometryState.fVertexLayout ^ |
| 656 | fGeometrySrc.fVertexLayout))); |
| 657 | |
| 658 | if (posAndTexChange) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 659 | int idx = GrGLProgram::PositionAttributeIdx(); |
| 660 | GR_GL(VertexAttribPointer(idx, 2, scalarType, false, newStride, |
| 661 | (GrGLvoid*)vertexOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 662 | fHWGeometryState.fVertexOffset = vertexOffset; |
| 663 | } |
| 664 | |
| 665 | for (int t = 0; t < kMaxTexCoords; ++t) { |
| 666 | if (newTexCoordOffsets[t] > 0) { |
| 667 | GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 668 | int idx = GrGLProgram::TexCoordAttributeIdx(t); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 669 | if (oldTexCoordOffsets[t] <= 0) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 670 | GR_GL(EnableVertexAttribArray(idx)); |
| 671 | GR_GL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
| 672 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 673 | } else if (posAndTexChange || |
| 674 | newTexCoordOffsets[t] != oldTexCoordOffsets[t]) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 675 | GR_GL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
| 676 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 677 | } |
| 678 | } else if (oldTexCoordOffsets[t] > 0) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 679 | GR_GL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
| 683 | if (newColorOffset > 0) { |
| 684 | GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 685 | int idx = GrGLProgram::ColorAttributeIdx(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 686 | if (oldColorOffset <= 0) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 687 | GR_GL(EnableVertexAttribArray(idx)); |
| 688 | GR_GL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 689 | true, newStride, colorOffset)); |
| 690 | } else if (allOffsetsChange || newColorOffset != oldColorOffset) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 691 | GR_GL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 692 | true, newStride, colorOffset)); |
| 693 | } |
| 694 | } else if (oldColorOffset > 0) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 695 | GR_GL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx())); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | fHWGeometryState.fVertexLayout = fGeometrySrc.fVertexLayout; |
| 699 | fHWGeometryState.fArrayPtrsDirty = false; |
| 700 | } |
| 701 | |
| 702 | void GrGpuGLShaders::buildProgram(GrPrimitiveType type) { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 703 | GrGLProgram::ProgramDesc& desc = fCurrentProgram.fProgramDesc; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 704 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 705 | // Must initialize all fields or cache will have false negatives! |
| 706 | desc.fVertexLayout = fGeometrySrc.fVertexLayout; |
| 707 | |
| 708 | desc.fEmitsPointSize = kPoints_PrimitiveType == type; |
| 709 | |
| 710 | bool requiresAttributeColors = desc.fVertexLayout & kColor_VertexLayoutBit; |
| 711 | // fColorType records how colors are specified for the program. Strip |
| 712 | // the bit from the layout to avoid false negatives when searching for an |
| 713 | // existing program in the cache. |
| 714 | desc.fVertexLayout &= ~(kColor_VertexLayoutBit); |
| 715 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 716 | desc.fColorFilterXfermode = fCurrDrawState.fColorFilterXfermode; |
| 717 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 718 | #if GR_AGGRESSIVE_SHADER_OPTS |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 719 | if (!requiresAttributeColors && (0xffffffff == fCurrDrawState.fColor)) { |
junov@google.com | c97db4c | 2011-04-29 17:25:42 +0000 | [diff] [blame] | 720 | desc.fColorType = GrGLProgram::ProgramDesc::kNone_ColorType; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 721 | } else |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 722 | #endif |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 723 | #if GR_GL_NO_CONSTANT_ATTRIBUTES |
| 724 | if (!requiresAttributeColors) { |
| 725 | desc.fColorType = GrGLProgram::ProgramDesc::kUniform_ColorType; |
| 726 | } else |
| 727 | #endif |
| 728 | { |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 729 | if (requiresAttributeColors) {} // suppress unused var warning |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 730 | desc.fColorType = GrGLProgram::ProgramDesc::kAttribute_ColorType; |
| 731 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 732 | |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 733 | desc.fEdgeAANumEdges = fCurrDrawState.fEdgeAANumEdges; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 734 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 735 | int lastEnabledStage = -1; |
| 736 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 737 | for (int s = 0; s < kNumStages; ++s) { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 738 | GrGLProgram::ProgramDesc::StageDesc& stage = desc.fStages[s]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 739 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 740 | stage.fOptFlags = 0; |
| 741 | stage.setEnabled(this->isStageEnabled(s)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 742 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 743 | if (stage.isEnabled()) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 744 | lastEnabledStage = s; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 745 | GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; |
| 746 | GrAssert(NULL != texture); |
| 747 | // we matrix to invert when orientation is TopDown, so make sure |
| 748 | // we aren't in that case before flagging as identity. |
| 749 | if (TextureMatrixIsIdentity(texture, fCurrDrawState.fSamplerStates[s])) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 750 | stage.fOptFlags |= GrGLProgram::ProgramDesc::StageDesc::kIdentityMatrix_OptFlagBit; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 751 | } else if (!getSamplerMatrix(s).hasPerspective()) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 752 | stage.fOptFlags |= GrGLProgram::ProgramDesc::StageDesc::kNoPerspective_OptFlagBit; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 753 | } |
| 754 | switch (fCurrDrawState.fSamplerStates[s].getSampleMode()) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 755 | case GrSamplerState::kNormal_SampleMode: |
| 756 | stage.fCoordMapping = GrGLProgram::ProgramDesc::StageDesc::kIdentity_CoordMapping; |
| 757 | break; |
| 758 | case GrSamplerState::kRadial_SampleMode: |
| 759 | stage.fCoordMapping = GrGLProgram::ProgramDesc::StageDesc::kRadialGradient_CoordMapping; |
| 760 | break; |
| 761 | case GrSamplerState::kRadial2_SampleMode: |
| 762 | stage.fCoordMapping = GrGLProgram::ProgramDesc::StageDesc::kRadial2Gradient_CoordMapping; |
| 763 | break; |
| 764 | case GrSamplerState::kSweep_SampleMode: |
| 765 | stage.fCoordMapping = GrGLProgram::ProgramDesc::StageDesc::kSweepGradient_CoordMapping; |
| 766 | break; |
| 767 | default: |
| 768 | GrCrash("Unexpected sample mode!"); |
| 769 | break; |
| 770 | } |
| 771 | |
| 772 | switch (fCurrDrawState.fSamplerStates[s].getFilter()) { |
| 773 | // these both can use a regular texture2D() |
| 774 | case GrSamplerState::kNearest_Filter: |
| 775 | case GrSamplerState::kBilinear_Filter: |
| 776 | stage.fFetchMode = GrGLProgram::ProgramDesc::StageDesc::kSingle_FetchMode; |
| 777 | break; |
| 778 | // performs 4 texture2D()s |
| 779 | case GrSamplerState::k4x4Downsample_Filter: |
| 780 | stage.fFetchMode = GrGLProgram::ProgramDesc::StageDesc::k2x2_FetchMode; |
| 781 | break; |
| 782 | default: |
| 783 | GrCrash("Unexpected filter!"); |
| 784 | break; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 785 | } |
| 786 | |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 787 | if (fCurrDrawState.fSamplerStates[s].hasTextureDomain()) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 788 | GrAssert(GrSamplerState::kClamp_WrapMode == |
| 789 | fCurrDrawState.fSamplerStates[s].getWrapX() && |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 790 | GrSamplerState::kClamp_WrapMode == |
| 791 | fCurrDrawState.fSamplerStates[s].getWrapY()); |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 792 | stage.fOptFlags |= |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 793 | GrGLProgram::ProgramDesc::StageDesc:: |
| 794 | kCustomTextureDomain_OptFlagBit; |
| 795 | } |
| 796 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 797 | if (GrPixelConfigIsAlphaOnly(texture->config())) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 798 | stage.fModulation = GrGLProgram::ProgramDesc::StageDesc::kAlpha_Modulation; |
| 799 | } else { |
| 800 | stage.fModulation = GrGLProgram::ProgramDesc::StageDesc::kColor_Modulation; |
| 801 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 802 | } else { |
| 803 | stage.fOptFlags = 0; |
| 804 | stage.fCoordMapping = (GrGLProgram::ProgramDesc::StageDesc::CoordMapping)0; |
| 805 | stage.fModulation = (GrGLProgram::ProgramDesc::StageDesc::Modulation)0; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 806 | } |
| 807 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 808 | |
| 809 | desc.fDualSrcOutput = GrGLProgram::ProgramDesc::kNone_DualSrcOutput; |
| 810 | // use canonical value when coverage/color distinction won't affect |
| 811 | // generated code to prevent duplicate programs. |
| 812 | desc.fFirstCoverageStage = kNumStages; |
| 813 | if (fCurrDrawState.fFirstCoverageStage <= lastEnabledStage) { |
| 814 | // color filter is applied between color/coverage computation |
| 815 | if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) { |
| 816 | desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage; |
| 817 | } |
| 818 | |
| 819 | // We could consider cases where the final color is solid (0xff alpha) |
| 820 | // and the dst coeff can correctly be set to a non-dualsrc gl value. |
| 821 | // (e.g. solid draw, and dst coeff is kZero. It's correct to make |
| 822 | // the dst coeff be kISA. Or solid draw with kSA can be tweaked to be |
| 823 | // kOne). |
| 824 | if (fDualSourceBlendingSupport) { |
| 825 | if (kZero_BlendCoeff == fCurrDrawState.fDstBlend) { |
| 826 | // write the coverage value to second color |
| 827 | desc.fDualSrcOutput = |
| 828 | GrGLProgram::ProgramDesc::kCoverage_DualSrcOutput; |
| 829 | desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage; |
| 830 | } else if (kSA_BlendCoeff == fCurrDrawState.fDstBlend) { |
| 831 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially |
| 832 | // cover |
| 833 | desc.fDualSrcOutput = |
| 834 | GrGLProgram::ProgramDesc::kCoverageISA_DualSrcOutput; |
| 835 | desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage; |
| 836 | } else if (kSC_BlendCoeff == fCurrDrawState.fDstBlend) { |
| 837 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially |
| 838 | // cover |
| 839 | desc.fDualSrcOutput = |
| 840 | GrGLProgram::ProgramDesc::kCoverageISC_DualSrcOutput; |
| 841 | desc.fFirstCoverageStage = fCurrDrawState.fFirstCoverageStage; |
| 842 | } |
| 843 | } |
| 844 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 845 | } |