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