junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 8 | #include "GrGpuGL.h" |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame] | 10 | #include "effects/GrGradientEffects.h" |
| 11 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 12 | #include "GrCustomStage.h" |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 13 | #include "GrGLProgramStage.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 14 | #include "GrGpuVertex.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 15 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 16 | #define SKIP_CACHE_CHECK true |
| 17 | #define GR_UINT32_MAX static_cast<uint32_t>(-1) |
| 18 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 19 | void GrGpuGL::ProgramCache::Entry::copyAndTakeOwnership(Entry& entry) { |
| 20 | fProgramData.copyAndTakeOwnership(entry.fProgramData); |
| 21 | fKey = entry.fKey; // ownership transfer |
| 22 | fLRUStamp = entry.fLRUStamp; |
| 23 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 24 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 25 | GrGpuGL::ProgramCache::ProgramCache(const GrGLContextInfo& gl) |
| 26 | : fCount(0) |
| 27 | , fCurrLRUStamp(0) |
| 28 | , fGL(gl) { |
| 29 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 30 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 31 | GrGpuGL::ProgramCache::~ProgramCache() { |
| 32 | for (int i = 0; i < fCount; ++i) { |
| 33 | GrGpuGL::DeleteProgram(fGL.interface(), |
| 34 | &fEntries[i].fProgramData); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 35 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 36 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 37 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 38 | void GrGpuGL::ProgramCache::abandon() { |
| 39 | fCount = 0; |
| 40 | } |
| 41 | |
| 42 | void GrGpuGL::ProgramCache::invalidateViewMatrices() { |
| 43 | for (int i = 0; i < fCount; ++i) { |
| 44 | // set to illegal matrix |
| 45 | fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 46 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 47 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 48 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 49 | GrGLProgram::CachedData* GrGpuGL::ProgramCache::getProgramData( |
| 50 | const GrGLProgram& desc, |
| 51 | GrCustomStage** stages) { |
| 52 | Entry newEntry; |
| 53 | newEntry.fKey.setKeyData(desc.keyData()); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 54 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 55 | Entry* entry = fHashCache.find(newEntry.fKey); |
| 56 | if (NULL == entry) { |
| 57 | if (!desc.genProgram(fGL, stages, &newEntry.fProgramData)) { |
| 58 | return NULL; |
| 59 | } |
| 60 | if (fCount < kMaxEntries) { |
| 61 | entry = fEntries + fCount; |
| 62 | ++fCount; |
| 63 | } else { |
| 64 | GrAssert(kMaxEntries == fCount); |
| 65 | entry = fEntries; |
| 66 | for (int i = 1; i < kMaxEntries; ++i) { |
| 67 | if (fEntries[i].fLRUStamp < entry->fLRUStamp) { |
| 68 | entry = fEntries + i; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 69 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 70 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 71 | fHashCache.remove(entry->fKey, entry); |
| 72 | GrGpuGL::DeleteProgram(fGL.interface(), |
| 73 | &entry->fProgramData); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 74 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 75 | entry->copyAndTakeOwnership(newEntry); |
| 76 | fHashCache.insert(entry->fKey, entry); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 77 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 78 | |
| 79 | entry->fLRUStamp = fCurrLRUStamp; |
| 80 | if (GR_UINT32_MAX == fCurrLRUStamp) { |
| 81 | // wrap around! just trash our LRU, one time hit. |
| 82 | for (int i = 0; i < fCount; ++i) { |
| 83 | fEntries[i].fLRUStamp = 0; |
| 84 | } |
| 85 | } |
| 86 | ++fCurrLRUStamp; |
| 87 | return &entry->fProgramData; |
| 88 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 89 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 90 | void GrGpuGL::DeleteProgram(const GrGLInterface* gl, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 91 | CachedData* programData) { |
| 92 | GR_GL_CALL(gl, DeleteShader(programData->fVShaderID)); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 93 | if (programData->fGShaderID) { |
| 94 | GR_GL_CALL(gl, DeleteShader(programData->fGShaderID)); |
| 95 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 96 | GR_GL_CALL(gl, DeleteShader(programData->fFShaderID)); |
| 97 | GR_GL_CALL(gl, DeleteProgram(programData->fProgramID)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 98 | GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));) |
| 99 | } |
| 100 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 101 | //////////////////////////////////////////////////////////////////////////////// |
| 102 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 103 | void GrGpuGL::abandonResources(){ |
| 104 | INHERITED::abandonResources(); |
| 105 | fProgramCache->abandon(); |
| 106 | fHWProgramID = 0; |
| 107 | } |
| 108 | |
| 109 | //////////////////////////////////////////////////////////////////////////////// |
| 110 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 111 | #define GL_CALL(X) GR_GL_CALL(this->glInterface(), X) |
| 112 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 113 | void GrGpuGL::flushViewMatrix() { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 114 | const GrMatrix& vm = this->getDrawState().getViewMatrix(); |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 115 | if (!fProgramData->fViewMatrix.cheapEqualTo(vm)) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 116 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 117 | const GrRenderTarget* rt = this->getDrawState().getRenderTarget(); |
| 118 | GrAssert(NULL != rt); |
| 119 | GrMatrix m; |
| 120 | m.setAll( |
| 121 | GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1, |
| 122 | 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1, |
| 123 | 0, 0, GrMatrix::I()[8]); |
| 124 | m.setConcat(m, vm); |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 125 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 126 | // ES doesn't allow you to pass true to the transpose param, |
| 127 | // so do our own transpose |
| 128 | GrGLfloat mt[] = { |
| 129 | GrScalarToFloat(m[GrMatrix::kMScaleX]), |
| 130 | GrScalarToFloat(m[GrMatrix::kMSkewY]), |
| 131 | GrScalarToFloat(m[GrMatrix::kMPersp0]), |
| 132 | GrScalarToFloat(m[GrMatrix::kMSkewX]), |
| 133 | GrScalarToFloat(m[GrMatrix::kMScaleY]), |
| 134 | GrScalarToFloat(m[GrMatrix::kMPersp1]), |
| 135 | GrScalarToFloat(m[GrMatrix::kMTransX]), |
| 136 | GrScalarToFloat(m[GrMatrix::kMTransY]), |
| 137 | GrScalarToFloat(m[GrMatrix::kMPersp2]) |
| 138 | }; |
| 139 | |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 140 | GrAssert(GrGLProgram::kUnusedUniform != |
| 141 | fProgramData->fUniLocations.fViewMatrixUni); |
| 142 | GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni, |
| 143 | 1, false, mt)); |
| 144 | fProgramData->fViewMatrix = vm; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 145 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 146 | } |
| 147 | |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 148 | /////////////////////////////////////////////////////////////////////////////// |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 149 | |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 150 | // helpers for texture matrices |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 151 | |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 152 | void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture, |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 153 | GrMatrix* matrix) { |
| 154 | GrAssert(NULL != texture); |
| 155 | GrAssert(NULL != matrix); |
| 156 | GrGLTexture::Orientation orientation = texture->orientation(); |
| 157 | if (GrGLTexture::kBottomUp_Orientation == orientation) { |
| 158 | GrMatrix invY; |
| 159 | invY.setAll(GR_Scalar1, 0, 0, |
| 160 | 0, -GR_Scalar1, GR_Scalar1, |
| 161 | 0, 0, GrMatrix::I()[8]); |
| 162 | matrix->postConcat(invY); |
| 163 | } else { |
| 164 | GrAssert(GrGLTexture::kTopDown_Orientation == orientation); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 168 | bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture, |
| 169 | const GrSamplerState& sampler) { |
| 170 | GrAssert(NULL != texture); |
| 171 | if (!sampler.getMatrix().isIdentity()) { |
| 172 | return false; |
| 173 | } |
| 174 | GrGLTexture::Orientation orientation = texture->orientation(); |
| 175 | if (GrGLTexture::kBottomUp_Orientation == orientation) { |
| 176 | return false; |
| 177 | } else { |
| 178 | GrAssert(GrGLTexture::kTopDown_Orientation == orientation); |
| 179 | } |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | /////////////////////////////////////////////////////////////////////////////// |
| 184 | |
| 185 | void GrGpuGL::flushTextureMatrixAndDomain(int s) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 186 | const GrDrawState& drawState = this->getDrawState(); |
| 187 | const GrGLTexture* texture = |
| 188 | static_cast<const GrGLTexture*>(drawState.getTexture(s)); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 189 | if (NULL != texture) { |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 190 | |
| 191 | bool orientationChange = fProgramData->fTextureOrientation[s] != |
| 192 | texture->orientation(); |
| 193 | |
| 194 | const GrGLint& matrixUni = |
| 195 | fProgramData->fUniLocations.fStages[s].fTextureMatrixUni; |
| 196 | |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 197 | const GrMatrix& hwMatrix = fProgramData->fTextureMatrices[s]; |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 198 | const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix(); |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 199 | |
| 200 | if (GrGLProgram::kUnusedUniform != matrixUni && |
| 201 | (orientationChange || !hwMatrix.cheapEqualTo(samplerMatrix))) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 202 | |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 203 | GrMatrix m = samplerMatrix; |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame] | 204 | AdjustTextureMatrix(texture, &m); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 205 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 206 | // ES doesn't allow you to pass true to the transpose param, |
| 207 | // so do our own transpose |
bsalomon@google.com | 27a4dc4 | 2011-05-16 13:14:03 +0000 | [diff] [blame] | 208 | GrGLfloat mt[] = { |
| 209 | GrScalarToFloat(m[GrMatrix::kMScaleX]), |
| 210 | GrScalarToFloat(m[GrMatrix::kMSkewY]), |
| 211 | GrScalarToFloat(m[GrMatrix::kMPersp0]), |
| 212 | GrScalarToFloat(m[GrMatrix::kMSkewX]), |
| 213 | GrScalarToFloat(m[GrMatrix::kMScaleY]), |
| 214 | GrScalarToFloat(m[GrMatrix::kMPersp1]), |
| 215 | GrScalarToFloat(m[GrMatrix::kMTransX]), |
| 216 | GrScalarToFloat(m[GrMatrix::kMTransY]), |
| 217 | GrScalarToFloat(m[GrMatrix::kMPersp2]) |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 218 | }; |
bsalomon@google.com | 27a4dc4 | 2011-05-16 13:14:03 +0000 | [diff] [blame] | 219 | |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 220 | GL_CALL(UniformMatrix3fv(matrixUni, 1, false, mt)); |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 221 | fProgramData->fTextureMatrices[s] = samplerMatrix; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 222 | } |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 223 | |
| 224 | const GrGLint& domUni = |
| 225 | fProgramData->fUniLocations.fStages[s].fTexDomUni; |
| 226 | const GrRect &texDom = drawState.getSampler(s).getTextureDomain(); |
| 227 | if (GrGLProgram::kUnusedUniform != domUni && |
| 228 | (orientationChange ||fProgramData->fTextureDomain[s] != texDom)) { |
| 229 | |
| 230 | fProgramData->fTextureDomain[s] = texDom; |
| 231 | |
| 232 | float values[4] = { |
| 233 | GrScalarToFloat(texDom.left()), |
| 234 | GrScalarToFloat(texDom.top()), |
| 235 | GrScalarToFloat(texDom.right()), |
| 236 | GrScalarToFloat(texDom.bottom()) |
| 237 | }; |
| 238 | |
| 239 | // vertical flip if necessary |
| 240 | if (GrGLTexture::kBottomUp_Orientation == texture->orientation()) { |
| 241 | values[1] = 1.0f - values[1]; |
| 242 | values[3] = 1.0f - values[3]; |
| 243 | // The top and bottom were just flipped, so correct the ordering |
| 244 | // of elements so that values = (l, t, r, b). |
| 245 | SkTSwap(values[1], values[3]); |
| 246 | } |
| 247 | GL_CALL(Uniform4fv(domUni, 1, values)); |
| 248 | } |
| 249 | fProgramData->fTextureOrientation[s] = texture->orientation(); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 250 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 251 | } |
| 252 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 253 | void GrGpuGL::flushTexelSize(int s) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 254 | const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 255 | if (GrGLProgram::kUnusedUniform != uni) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 256 | const GrGLTexture* texture = |
| 257 | static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s)); |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 258 | if (texture->width() != fProgramData->fTextureWidth[s] || |
| 259 | texture->height() != fProgramData->fTextureHeight[s]) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 260 | |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 261 | float texelSize[] = {1.f / texture->width(), |
| 262 | 1.f / texture->height()}; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 263 | GL_CALL(Uniform2fv(uni, 1, texelSize)); |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 264 | fProgramData->fTextureWidth[s] = texture->width(); |
| 265 | fProgramData->fTextureHeight[s] = texture->height(); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 268 | } |
| 269 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 270 | void GrGpuGL::flushColorMatrix() { |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 271 | const ProgramDesc& desc = fCurrentProgram.getDesc(); |
| 272 | int matrixUni = fProgramData->fUniLocations.fColorMatrixUni; |
| 273 | int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni; |
| 274 | if (GrGLProgram::kUnusedUniform != matrixUni |
| 275 | && GrGLProgram::kUnusedUniform != vecUni) { |
| 276 | const float* m = this->getDrawState().getColorMatrix(); |
| 277 | GrGLfloat mt[] = { |
| 278 | m[0], m[5], m[10], m[15], |
| 279 | m[1], m[6], m[11], m[16], |
| 280 | m[2], m[7], m[12], m[17], |
| 281 | m[3], m[8], m[13], m[18], |
| 282 | }; |
| 283 | static float scale = 1.0f / 255.0f; |
| 284 | GrGLfloat vec[] = { |
| 285 | m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale, |
| 286 | }; |
| 287 | GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt)); |
| 288 | GL_CALL(Uniform4fv(vecUni, 1, vec)); |
| 289 | } |
| 290 | } |
| 291 | |
Scroggo | 01b87ec | 2011-05-11 18:05:38 +0000 | [diff] [blame] | 292 | static const float ONE_OVER_255 = 1.f / 255.f; |
| 293 | |
| 294 | #define GR_COLOR_TO_VEC4(color) {\ |
| 295 | GrColorUnpackR(color) * ONE_OVER_255,\ |
| 296 | GrColorUnpackG(color) * ONE_OVER_255,\ |
| 297 | GrColorUnpackB(color) * ONE_OVER_255,\ |
| 298 | GrColorUnpackA(color) * ONE_OVER_255 \ |
| 299 | } |
| 300 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 301 | void GrGpuGL::flushColor(GrColor color) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 302 | const ProgramDesc& desc = fCurrentProgram.getDesc(); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 303 | const GrDrawState& drawState = this->getDrawState(); |
| 304 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 305 | if (this->getVertexLayout() & kColor_VertexLayoutBit) { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 306 | // color will be specified per-vertex as an attribute |
| 307 | // invalidate the const vertex attrib color |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 308 | fHWConstAttribColor = GrColor_ILLEGAL; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 309 | } else { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 310 | switch (desc.fColorInput) { |
| 311 | case ProgramDesc::kAttribute_ColorInput: |
bsalomon@google.com | 255fa16 | 2012-05-21 21:44:59 +0000 | [diff] [blame] | 312 | if (fHWConstAttribColor != color) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 313 | // OpenGL ES only supports the float varieties of |
| 314 | // glVertexAttrib |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 315 | float c[] = GR_COLOR_TO_VEC4(color); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 316 | GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(), |
| 317 | c)); |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 318 | fHWConstAttribColor = color; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 319 | } |
| 320 | break; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 321 | case ProgramDesc::kUniform_ColorInput: |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 322 | if (fProgramData->fColor != color) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 323 | // OpenGL ES doesn't support unsigned byte varieties of |
| 324 | // glUniform |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 325 | float c[] = GR_COLOR_TO_VEC4(color); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 326 | GrAssert(GrGLProgram::kUnusedUniform != |
| 327 | fProgramData->fUniLocations.fColorUni); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 328 | GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni, |
| 329 | 1, c)); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 330 | fProgramData->fColor = color; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 331 | } |
| 332 | break; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 333 | case ProgramDesc::kSolidWhite_ColorInput: |
| 334 | case ProgramDesc::kTransBlack_ColorInput: |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 335 | break; |
| 336 | default: |
| 337 | GrCrash("Unknown color type."); |
| 338 | } |
| 339 | } |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 340 | if (fProgramData->fUniLocations.fColorFilterUni |
| 341 | != GrGLProgram::kUnusedUniform |
| 342 | && fProgramData->fColorFilterColor |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 343 | != drawState.getColorFilterColor()) { |
| 344 | float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor()); |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 345 | GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c)); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 346 | fProgramData->fColorFilterColor = drawState.getColorFilterColor(); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 347 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 348 | } |
| 349 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 350 | void GrGpuGL::flushCoverage(GrColor coverage) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 351 | const ProgramDesc& desc = fCurrentProgram.getDesc(); |
| 352 | const GrDrawState& drawState = this->getDrawState(); |
| 353 | |
| 354 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 355 | if (this->getVertexLayout() & kCoverage_VertexLayoutBit) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 356 | // coverage will be specified per-vertex as an attribute |
| 357 | // invalidate the const vertex attrib coverage |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 358 | fHWConstAttribCoverage = GrColor_ILLEGAL; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 359 | } else { |
| 360 | switch (desc.fCoverageInput) { |
| 361 | case ProgramDesc::kAttribute_ColorInput: |
bsalomon@google.com | 255fa16 | 2012-05-21 21:44:59 +0000 | [diff] [blame] | 362 | if (fHWConstAttribCoverage != coverage) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 363 | // OpenGL ES only supports the float varieties of |
| 364 | // glVertexAttrib |
| 365 | float c[] = GR_COLOR_TO_VEC4(coverage); |
| 366 | GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(), |
| 367 | c)); |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 368 | fHWConstAttribCoverage = coverage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 369 | } |
| 370 | break; |
| 371 | case ProgramDesc::kUniform_ColorInput: |
| 372 | if (fProgramData->fCoverage != coverage) { |
| 373 | // OpenGL ES doesn't support unsigned byte varieties of |
| 374 | // glUniform |
| 375 | float c[] = GR_COLOR_TO_VEC4(coverage); |
| 376 | GrAssert(GrGLProgram::kUnusedUniform != |
| 377 | fProgramData->fUniLocations.fCoverageUni); |
| 378 | GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni, |
| 379 | 1, c)); |
| 380 | fProgramData->fCoverage = coverage; |
| 381 | } |
| 382 | break; |
| 383 | case ProgramDesc::kSolidWhite_ColorInput: |
| 384 | case ProgramDesc::kTransBlack_ColorInput: |
| 385 | break; |
| 386 | default: |
| 387 | GrCrash("Unknown coverage type."); |
| 388 | } |
| 389 | } |
| 390 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 391 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 392 | bool GrGpuGL::flushGraphicsState(GrPrimitiveType type) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 393 | if (!flushGLStateCommon(type)) { |
| 394 | return false; |
| 395 | } |
| 396 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 397 | const GrDrawState& drawState = this->getDrawState(); |
| 398 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 399 | GrBlendCoeff srcCoeff; |
| 400 | GrBlendCoeff dstCoeff; |
| 401 | BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff); |
| 402 | if (kSkipDraw_BlendOptFlag & blendOpts) { |
| 403 | return false; |
| 404 | } |
| 405 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 406 | GrCustomStage* customStages [GrDrawState::kNumStages]; |
| 407 | this->buildProgram(type, blendOpts, dstCoeff, customStages); |
| 408 | fProgramData = fProgramCache->getProgramData(fCurrentProgram, |
| 409 | customStages); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 410 | if (NULL == fProgramData) { |
| 411 | GrAssert(!"Failed to create program!"); |
| 412 | return false; |
| 413 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 414 | |
| 415 | if (fHWProgramID != fProgramData->fProgramID) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 416 | GL_CALL(UseProgram(fProgramData->fProgramID)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 417 | fHWProgramID = fProgramData->fProgramID; |
| 418 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 419 | fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff); |
| 420 | this->flushBlend(type, srcCoeff, dstCoeff); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 421 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 422 | GrColor color; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 423 | GrColor coverage; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 424 | if (blendOpts & kEmitTransBlack_BlendOptFlag) { |
| 425 | color = 0; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 426 | coverage = 0; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 427 | } else if (blendOpts & kEmitCoverage_BlendOptFlag) { |
| 428 | color = 0xffffffff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 429 | coverage = drawState.getCoverage(); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 430 | } else { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 431 | color = drawState.getColor(); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 432 | coverage = drawState.getCoverage(); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 433 | } |
| 434 | this->flushColor(color); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 435 | this->flushCoverage(coverage); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 436 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 437 | this->flushViewMatrix(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 438 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 439 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 440 | if (this->isStageEnabled(s)) { |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 441 | this->flushTextureMatrixAndDomain(s); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 442 | |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 443 | this->flushTexelSize(s); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 444 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 445 | if (NULL != fProgramData->fCustomStage[s]) { |
| 446 | const GrSamplerState& sampler = |
| 447 | this->getDrawState().getSampler(s); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 448 | const GrGLTexture* texture = |
| 449 | static_cast<const GrGLTexture*>( |
| 450 | this->getDrawState().getTexture(s)); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 451 | fProgramData->fCustomStage[s]->setData( |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 452 | this->glInterface(), *texture, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 453 | *sampler.getCustomStage(), s); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 454 | } |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 455 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 456 | } |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 457 | this->flushColorMatrix(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 458 | return true; |
| 459 | } |
| 460 | |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 461 | #if GR_TEXT_SCALAR_IS_USHORT |
| 462 | #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT |
| 463 | #define TEXT_COORDS_ARE_NORMALIZED 1 |
| 464 | #elif GR_TEXT_SCALAR_IS_FLOAT |
| 465 | #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT |
| 466 | #define TEXT_COORDS_ARE_NORMALIZED 0 |
| 467 | #elif GR_TEXT_SCALAR_IS_FIXED |
| 468 | #define TEXT_COORDS_GL_TYPE GR_GL_FIXED |
| 469 | #define TEXT_COORDS_ARE_NORMALIZED 0 |
| 470 | #else |
| 471 | #error "unknown GR_TEXT_SCALAR type" |
| 472 | #endif |
| 473 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 474 | void GrGpuGL::setupGeometry(int* startVertex, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 475 | int* startIndex, |
| 476 | int vertexCount, |
| 477 | int indexCount) { |
| 478 | |
| 479 | int newColorOffset; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 480 | int newCoverageOffset; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 481 | int newTexCoordOffsets[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 482 | int newEdgeOffset; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 483 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 484 | GrVertexLayout currLayout = this->getVertexLayout(); |
| 485 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 486 | GrGLsizei newStride = VertexSizeAndOffsetsByIdx( |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 487 | currLayout, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 488 | newTexCoordOffsets, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 489 | &newColorOffset, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 490 | &newCoverageOffset, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 491 | &newEdgeOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 492 | int oldColorOffset; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 493 | int oldCoverageOffset; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 494 | int oldTexCoordOffsets[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 495 | int oldEdgeOffset; |
| 496 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 497 | GrGLsizei oldStride = VertexSizeAndOffsetsByIdx( |
| 498 | fHWGeometryState.fVertexLayout, |
| 499 | oldTexCoordOffsets, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 500 | &oldColorOffset, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 501 | &oldCoverageOffset, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 502 | &oldEdgeOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 503 | bool indexed = NULL != startIndex; |
| 504 | |
| 505 | int extraVertexOffset; |
| 506 | int extraIndexOffset; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 507 | this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 508 | |
| 509 | GrGLenum scalarType; |
| 510 | bool texCoordNorm; |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 511 | if (currLayout & kTextFormat_VertexLayoutBit) { |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 512 | scalarType = TEXT_COORDS_GL_TYPE; |
| 513 | texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 514 | } else { |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 515 | GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT); |
| 516 | scalarType = GR_GL_FLOAT; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 517 | texCoordNorm = false; |
| 518 | } |
| 519 | |
| 520 | size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride; |
| 521 | *startVertex = 0; |
| 522 | if (indexed) { |
| 523 | *startIndex += extraIndexOffset; |
| 524 | } |
| 525 | |
| 526 | // all the Pointers must be set if any of these are true |
| 527 | bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty || |
| 528 | vertexOffset != fHWGeometryState.fVertexOffset || |
| 529 | newStride != oldStride; |
| 530 | |
| 531 | // position and tex coord offsets change if above conditions are true |
| 532 | // or the type/normalization changed based on text vs nontext type coords. |
| 533 | bool posAndTexChange = allOffsetsChange || |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 534 | (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) && |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 535 | (kTextFormat_VertexLayoutBit & |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 536 | (fHWGeometryState.fVertexLayout ^ currLayout))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 537 | |
| 538 | if (posAndTexChange) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 539 | int idx = GrGLProgram::PositionAttributeIdx(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 540 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 541 | (GrGLvoid*)vertexOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 542 | fHWGeometryState.fVertexOffset = vertexOffset; |
| 543 | } |
| 544 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 545 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 546 | if (newTexCoordOffsets[t] > 0) { |
| 547 | GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 548 | int idx = GrGLProgram::TexCoordAttributeIdx(t); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 549 | if (oldTexCoordOffsets[t] <= 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 550 | GL_CALL(EnableVertexAttribArray(idx)); |
| 551 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 552 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 553 | } else if (posAndTexChange || |
| 554 | newTexCoordOffsets[t] != oldTexCoordOffsets[t]) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 555 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 556 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 557 | } |
| 558 | } else if (oldTexCoordOffsets[t] > 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 559 | GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | if (newColorOffset > 0) { |
| 564 | GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 565 | int idx = GrGLProgram::ColorAttributeIdx(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 566 | if (oldColorOffset <= 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 567 | GL_CALL(EnableVertexAttribArray(idx)); |
| 568 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 569 | true, newStride, colorOffset)); |
| 570 | } else if (allOffsetsChange || newColorOffset != oldColorOffset) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 571 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 572 | true, newStride, colorOffset)); |
| 573 | } |
| 574 | } else if (oldColorOffset > 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 575 | GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx())); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 576 | } |
| 577 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 578 | if (newCoverageOffset > 0) { |
bsalomon@google.com | e10f6fd | 2011-10-11 20:15:26 +0000 | [diff] [blame] | 579 | GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 580 | int idx = GrGLProgram::CoverageAttributeIdx(); |
| 581 | if (oldCoverageOffset <= 0) { |
| 582 | GL_CALL(EnableVertexAttribArray(idx)); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 583 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 584 | true, newStride, coverageOffset)); |
| 585 | } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 586 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 587 | true, newStride, coverageOffset)); |
| 588 | } |
| 589 | } else if (oldCoverageOffset > 0) { |
| 590 | GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx())); |
| 591 | } |
| 592 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 593 | if (newEdgeOffset > 0) { |
| 594 | GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset); |
| 595 | int idx = GrGLProgram::EdgeAttributeIdx(); |
| 596 | if (oldEdgeOffset <= 0) { |
| 597 | GL_CALL(EnableVertexAttribArray(idx)); |
| 598 | GL_CALL(VertexAttribPointer(idx, 4, scalarType, |
| 599 | false, newStride, edgeOffset)); |
| 600 | } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) { |
| 601 | GL_CALL(VertexAttribPointer(idx, 4, scalarType, |
| 602 | false, newStride, edgeOffset)); |
| 603 | } |
| 604 | } else if (oldEdgeOffset > 0) { |
| 605 | GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx())); |
| 606 | } |
| 607 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 608 | fHWGeometryState.fVertexLayout = currLayout; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 609 | fHWGeometryState.fArrayPtrsDirty = false; |
| 610 | } |
| 611 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 612 | namespace { |
| 613 | |
| 614 | void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage, |
| 615 | const GrSamplerState& sampler, |
| 616 | GrCustomStage** customStages, |
| 617 | GrGLProgram* program, int index) { |
| 618 | GrCustomStage* customStage = sampler.getCustomStage(); |
| 619 | if (customStage) { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 620 | const GrProgramStageFactory& factory = customStage->getFactory(); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 621 | stage->fCustomStageKey = factory.glStageKey(*customStage); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 622 | customStages[index] = customStage; |
| 623 | } else { |
| 624 | stage->fCustomStageKey = 0; |
| 625 | customStages[index] = NULL; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | } |
| 630 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 631 | void GrGpuGL::buildProgram(GrPrimitiveType type, |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 632 | BlendOptFlags blendOpts, |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 633 | GrBlendCoeff dstCoeff, |
| 634 | GrCustomStage** customStages) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 635 | ProgramDesc& desc = fCurrentProgram.fProgramDesc; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 636 | const GrDrawState& drawState = this->getDrawState(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 637 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 638 | // This should already have been caught |
| 639 | GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts)); |
| 640 | |
| 641 | bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag); |
| 642 | |
| 643 | bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag | |
| 644 | kEmitCoverage_BlendOptFlag)); |
| 645 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 646 | // The descriptor is used as a cache key. Thus when a field of the |
| 647 | // descriptor will not affect program generation (because of the vertex |
| 648 | // layout in use or other descriptor field settings) it should be set |
| 649 | // to a canonical value to avoid duplicate programs with different keys. |
| 650 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 651 | // Must initialize all fields or cache will have false negatives! |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 652 | desc.fVertexLayout = this->getVertexLayout(); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 653 | |
| 654 | desc.fEmitsPointSize = kPoints_PrimitiveType == type; |
| 655 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 656 | bool requiresAttributeColors = |
| 657 | !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 658 | bool requiresAttributeCoverage = |
| 659 | !skipCoverage && SkToBool(desc.fVertexLayout & |
| 660 | kCoverage_VertexLayoutBit); |
| 661 | |
| 662 | // fColorInput/fCoverageInput records how colors are specified for the. |
| 663 | // program. So we strip the bits from the layout to avoid false negatives |
| 664 | // when searching for an existing program in the cache. |
| 665 | desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 666 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 667 | desc.fColorFilterXfermode = skipColor ? |
| 668 | SkXfermode::kDst_Mode : |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 669 | drawState.getColorFilterMode(); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 670 | |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 671 | desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit); |
| 672 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 673 | // no reason to do edge aa or look at per-vertex coverage if coverage is |
| 674 | // ignored |
| 675 | if (skipCoverage) { |
| 676 | desc.fVertexLayout &= ~(kEdge_VertexLayoutBit | |
| 677 | kCoverage_VertexLayoutBit); |
| 678 | } |
| 679 | |
| 680 | bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag); |
| 681 | bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) || |
| 682 | (!requiresAttributeColors && |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 683 | 0xffffffff == drawState.getColor()); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 684 | if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 685 | desc.fColorInput = ProgramDesc::kTransBlack_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 686 | } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 687 | desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 688 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 689 | desc.fColorInput = ProgramDesc::kUniform_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 690 | } else { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 691 | desc.fColorInput = ProgramDesc::kAttribute_ColorInput; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 692 | } |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 693 | |
| 694 | bool covIsSolidWhite = !requiresAttributeCoverage && |
| 695 | 0xffffffff == drawState.getCoverage(); |
| 696 | |
| 697 | if (skipCoverage) { |
| 698 | desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput; |
| 699 | } else if (covIsSolidWhite) { |
| 700 | desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput; |
| 701 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) { |
| 702 | desc.fCoverageInput = ProgramDesc::kUniform_ColorInput; |
| 703 | } else { |
| 704 | desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput; |
| 705 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 706 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 707 | int lastEnabledStage = -1; |
| 708 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 709 | if (!skipCoverage && (desc.fVertexLayout & |
| 710 | GrDrawTarget::kEdge_VertexLayoutBit)) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 711 | desc.fVertexEdgeType = drawState.getVertexEdgeType(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 712 | } else { |
| 713 | // use canonical value when not set to avoid cache misses |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 714 | desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 715 | } |
| 716 | |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 717 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 718 | StageDesc& stage = desc.fStages[s]; |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 719 | |
| 720 | stage.fOptFlags = 0; |
| 721 | stage.setEnabled(this->isStageEnabled(s)); |
| 722 | |
| 723 | bool skip = s < drawState.getFirstCoverageStage() ? skipColor : |
| 724 | skipCoverage; |
| 725 | |
| 726 | if (!skip && stage.isEnabled()) { |
| 727 | lastEnabledStage = s; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 728 | const GrGLTexture* texture = |
| 729 | static_cast<const GrGLTexture*>(drawState.getTexture(s)); |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 730 | GrAssert(NULL != texture); |
| 731 | const GrSamplerState& sampler = drawState.getSampler(s); |
| 732 | // we matrix to invert when orientation is TopDown, so make sure |
| 733 | // we aren't in that case before flagging as identity. |
| 734 | if (TextureMatrixIsIdentity(texture, sampler)) { |
| 735 | stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit; |
| 736 | } else if (!sampler.getMatrix().hasPerspective()) { |
| 737 | stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit; |
| 738 | } |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 739 | |
| 740 | switch (sampler.getFilter()) { |
| 741 | // these both can use a regular texture2D() |
| 742 | case GrSamplerState::kNearest_Filter: |
| 743 | case GrSamplerState::kBilinear_Filter: |
| 744 | stage.fFetchMode = StageDesc::kSingle_FetchMode; |
| 745 | break; |
| 746 | // performs 4 texture2D()s |
| 747 | case GrSamplerState::k4x4Downsample_Filter: |
| 748 | stage.fFetchMode = StageDesc::k2x2_FetchMode; |
| 749 | break; |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 750 | default: |
| 751 | GrCrash("Unexpected filter!"); |
| 752 | break; |
| 753 | } |
| 754 | |
| 755 | if (sampler.hasTextureDomain()) { |
| 756 | GrAssert(GrSamplerState::kClamp_WrapMode == |
| 757 | sampler.getWrapX() && |
| 758 | GrSamplerState::kClamp_WrapMode == |
| 759 | sampler.getWrapY()); |
| 760 | stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit; |
| 761 | } |
| 762 | |
| 763 | stage.fInConfigFlags = 0; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 764 | if (!this->glCaps().textureSwizzleSupport()) { |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 765 | if (GrPixelConfigIsAlphaOnly(texture->config())) { |
| 766 | // if we don't have texture swizzle support then |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 767 | // the shader must smear the single channel after |
| 768 | // reading the texture |
| 769 | if (this->glCaps().textureRedSupport()) { |
| 770 | // we can use R8 textures so use kSmearRed |
| 771 | stage.fInConfigFlags |= |
| 772 | StageDesc::kSmearRed_InConfigFlag; |
| 773 | } else { |
| 774 | // we can use A8 textures so use kSmearAlpha |
| 775 | stage.fInConfigFlags |= |
| 776 | StageDesc::kSmearAlpha_InConfigFlag; |
| 777 | } |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 778 | } else if (sampler.swapsRAndB()) { |
| 779 | stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag; |
| 780 | } |
| 781 | } |
| 782 | if (GrPixelConfigIsUnpremultiplied(texture->config())) { |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 783 | // The shader generator assumes that color channels are bytes |
| 784 | // when rounding. |
| 785 | GrAssert(4 == GrBytesPerPixel(texture->config())); |
| 786 | if (kUpOnWrite_DownOnRead_UnpremulConversion == |
| 787 | fUnpremulConversion) { |
| 788 | stage.fInConfigFlags |= |
| 789 | StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag; |
| 790 | } else { |
| 791 | stage.fInConfigFlags |= |
| 792 | StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag; |
| 793 | } |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 794 | } |
| 795 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 796 | setup_custom_stage(&stage, sampler, customStages, |
| 797 | &fCurrentProgram, s); |
| 798 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 799 | } else { |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 800 | stage.fOptFlags = 0; |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 801 | stage.fInConfigFlags = 0; |
| 802 | stage.fFetchMode = (StageDesc::FetchMode) 0; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 803 | stage.fCustomStageKey = 0; |
| 804 | customStages[s] = NULL; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 805 | } |
| 806 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 807 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 808 | if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) { |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 809 | // The shader generator assumes that color channels are bytes |
| 810 | // when rounding. |
| 811 | GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config())); |
| 812 | if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) { |
| 813 | desc.fOutputConfig = |
| 814 | ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig; |
| 815 | } else { |
| 816 | desc.fOutputConfig = |
| 817 | ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig; |
| 818 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 819 | } else { |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 820 | desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 821 | } |
| 822 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 823 | desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 824 | |
| 825 | // currently the experimental GS will only work with triangle prims |
| 826 | // (and it doesn't do anything other than pass through values from |
| 827 | // the VS to the FS anyway). |
| 828 | #if 0 && GR_GL_EXPERIMENTAL_GS |
| 829 | desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport; |
| 830 | #endif |
| 831 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 832 | // we want to avoid generating programs with different "first cov stage" |
| 833 | // values when they would compute the same result. |
| 834 | // We set field in the desc to kNumStages when either there are no |
| 835 | // coverage stages or the distinction between coverage and color is |
| 836 | // immaterial. |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 837 | int firstCoverageStage = GrDrawState::kNumStages; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 838 | desc.fFirstCoverageStage = GrDrawState::kNumStages; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 839 | bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 840 | if (hasCoverage) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 841 | firstCoverageStage = drawState.getFirstCoverageStage(); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | // other coverage inputs |
| 845 | if (!hasCoverage) { |
| 846 | hasCoverage = |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 847 | requiresAttributeCoverage || |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 848 | (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit); |
| 849 | } |
| 850 | |
| 851 | if (hasCoverage) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 852 | // color filter is applied between color/coverage computation |
| 853 | if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 854 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 855 | } |
| 856 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 857 | if (this->getCaps().fDualSourceBlendingSupport && |
| 858 | !(blendOpts & (kEmitCoverage_BlendOptFlag | |
| 859 | kCoverageAsAlpha_BlendOptFlag))) { |
| 860 | if (kZero_BlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 861 | // write the coverage value to second color |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 862 | desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 863 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 864 | } else if (kSA_BlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 865 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially |
| 866 | // cover |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 867 | desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 868 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 869 | } else if (kSC_BlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 870 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially |
| 871 | // cover |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 872 | desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 873 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 877 | } |