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) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 392 | const GrDrawState& drawState = this->getDrawState(); |
| 393 | |
bsalomon@google.com | c96cb3a | 2012-06-04 19:31:00 +0000 | [diff] [blame^] | 394 | // GrGpu::setupClipAndFlushState should have already checked this |
| 395 | // and bailed if not true. |
| 396 | GrAssert(NULL != drawState.getRenderTarget()); |
| 397 | |
| 398 | this->flushStencil(); |
| 399 | this->flushAAState(type); |
| 400 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 401 | GrBlendCoeff srcCoeff; |
| 402 | GrBlendCoeff dstCoeff; |
| 403 | BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff); |
| 404 | if (kSkipDraw_BlendOptFlag & blendOpts) { |
| 405 | return false; |
| 406 | } |
| 407 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 408 | GrCustomStage* customStages [GrDrawState::kNumStages]; |
| 409 | this->buildProgram(type, blendOpts, dstCoeff, customStages); |
| 410 | fProgramData = fProgramCache->getProgramData(fCurrentProgram, |
| 411 | customStages); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 412 | if (NULL == fProgramData) { |
| 413 | GrAssert(!"Failed to create program!"); |
| 414 | return false; |
| 415 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 416 | |
| 417 | if (fHWProgramID != fProgramData->fProgramID) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 418 | GL_CALL(UseProgram(fProgramData->fProgramID)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 419 | fHWProgramID = fProgramData->fProgramID; |
| 420 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 421 | fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff); |
| 422 | this->flushBlend(type, srcCoeff, dstCoeff); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 423 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 424 | GrColor color; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 425 | GrColor coverage; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 426 | if (blendOpts & kEmitTransBlack_BlendOptFlag) { |
| 427 | color = 0; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 428 | coverage = 0; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 429 | } else if (blendOpts & kEmitCoverage_BlendOptFlag) { |
| 430 | color = 0xffffffff; |
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 | } else { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 433 | color = drawState.getColor(); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 434 | coverage = drawState.getCoverage(); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 435 | } |
| 436 | this->flushColor(color); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 437 | this->flushCoverage(coverage); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 438 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 439 | this->flushViewMatrix(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 440 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 441 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 442 | if (this->isStageEnabled(s)) { |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 443 | |
bsalomon@google.com | c96cb3a | 2012-06-04 19:31:00 +0000 | [diff] [blame^] | 444 | |
| 445 | #if GR_DEBUG |
| 446 | // check for circular rendering |
| 447 | GrAssert(NULL == drawState.getRenderTarget() || |
| 448 | NULL == drawState.getTexture(s) || |
| 449 | drawState.getTexture(s)->asRenderTarget() != |
| 450 | drawState.getRenderTarget()); |
| 451 | #endif |
| 452 | |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 453 | this->flushBoundTextureAndParams(s); |
| 454 | |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 455 | this->flushTextureMatrixAndDomain(s); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 456 | |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 457 | this->flushTexelSize(s); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 458 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 459 | if (NULL != fProgramData->fCustomStage[s]) { |
| 460 | const GrSamplerState& sampler = |
| 461 | this->getDrawState().getSampler(s); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 462 | const GrGLTexture* texture = |
| 463 | static_cast<const GrGLTexture*>( |
| 464 | this->getDrawState().getTexture(s)); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 465 | fProgramData->fCustomStage[s]->setData( |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 466 | this->glInterface(), *texture, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 467 | *sampler.getCustomStage(), s); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 468 | } |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 469 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 470 | } |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 471 | this->flushColorMatrix(); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 472 | |
| 473 | GrIRect* rect = NULL; |
| 474 | GrIRect clipBounds; |
| 475 | if (drawState.isClipState() && |
| 476 | fClip.hasConservativeBounds()) { |
| 477 | fClip.getConservativeBounds().roundOut(&clipBounds); |
| 478 | rect = &clipBounds; |
| 479 | } |
| 480 | // This must come after textures are flushed because a texture may need |
| 481 | // to be msaa-resolved (which will modify bound FBO state). |
| 482 | this->flushRenderTarget(rect); |
| 483 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 484 | return true; |
| 485 | } |
| 486 | |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 487 | #if GR_TEXT_SCALAR_IS_USHORT |
| 488 | #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT |
| 489 | #define TEXT_COORDS_ARE_NORMALIZED 1 |
| 490 | #elif GR_TEXT_SCALAR_IS_FLOAT |
| 491 | #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT |
| 492 | #define TEXT_COORDS_ARE_NORMALIZED 0 |
| 493 | #elif GR_TEXT_SCALAR_IS_FIXED |
| 494 | #define TEXT_COORDS_GL_TYPE GR_GL_FIXED |
| 495 | #define TEXT_COORDS_ARE_NORMALIZED 0 |
| 496 | #else |
| 497 | #error "unknown GR_TEXT_SCALAR type" |
| 498 | #endif |
| 499 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 500 | void GrGpuGL::setupGeometry(int* startVertex, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 501 | int* startIndex, |
| 502 | int vertexCount, |
| 503 | int indexCount) { |
| 504 | |
| 505 | int newColorOffset; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 506 | int newCoverageOffset; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 507 | int newTexCoordOffsets[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 508 | int newEdgeOffset; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 509 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 510 | GrVertexLayout currLayout = this->getVertexLayout(); |
| 511 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 512 | GrGLsizei newStride = VertexSizeAndOffsetsByIdx( |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 513 | currLayout, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 514 | newTexCoordOffsets, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 515 | &newColorOffset, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 516 | &newCoverageOffset, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 517 | &newEdgeOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 518 | int oldColorOffset; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 519 | int oldCoverageOffset; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 520 | int oldTexCoordOffsets[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 521 | int oldEdgeOffset; |
| 522 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 523 | GrGLsizei oldStride = VertexSizeAndOffsetsByIdx( |
| 524 | fHWGeometryState.fVertexLayout, |
| 525 | oldTexCoordOffsets, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 526 | &oldColorOffset, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 527 | &oldCoverageOffset, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 528 | &oldEdgeOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 529 | bool indexed = NULL != startIndex; |
| 530 | |
| 531 | int extraVertexOffset; |
| 532 | int extraIndexOffset; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 533 | this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 534 | |
| 535 | GrGLenum scalarType; |
| 536 | bool texCoordNorm; |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 537 | if (currLayout & kTextFormat_VertexLayoutBit) { |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 538 | scalarType = TEXT_COORDS_GL_TYPE; |
| 539 | texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 540 | } else { |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 541 | GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT); |
| 542 | scalarType = GR_GL_FLOAT; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 543 | texCoordNorm = false; |
| 544 | } |
| 545 | |
| 546 | size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride; |
| 547 | *startVertex = 0; |
| 548 | if (indexed) { |
| 549 | *startIndex += extraIndexOffset; |
| 550 | } |
| 551 | |
| 552 | // all the Pointers must be set if any of these are true |
| 553 | bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty || |
| 554 | vertexOffset != fHWGeometryState.fVertexOffset || |
| 555 | newStride != oldStride; |
| 556 | |
| 557 | // position and tex coord offsets change if above conditions are true |
| 558 | // or the type/normalization changed based on text vs nontext type coords. |
| 559 | bool posAndTexChange = allOffsetsChange || |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 560 | (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) && |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 561 | (kTextFormat_VertexLayoutBit & |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 562 | (fHWGeometryState.fVertexLayout ^ currLayout))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 563 | |
| 564 | if (posAndTexChange) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 565 | int idx = GrGLProgram::PositionAttributeIdx(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 566 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 567 | (GrGLvoid*)vertexOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 568 | fHWGeometryState.fVertexOffset = vertexOffset; |
| 569 | } |
| 570 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 571 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 572 | if (newTexCoordOffsets[t] > 0) { |
| 573 | GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 574 | int idx = GrGLProgram::TexCoordAttributeIdx(t); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 575 | if (oldTexCoordOffsets[t] <= 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 576 | GL_CALL(EnableVertexAttribArray(idx)); |
| 577 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 578 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 579 | } else if (posAndTexChange || |
| 580 | newTexCoordOffsets[t] != oldTexCoordOffsets[t]) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 581 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 582 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 583 | } |
| 584 | } else if (oldTexCoordOffsets[t] > 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 585 | GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
| 589 | if (newColorOffset > 0) { |
| 590 | GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 591 | int idx = GrGLProgram::ColorAttributeIdx(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 592 | if (oldColorOffset <= 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 593 | GL_CALL(EnableVertexAttribArray(idx)); |
| 594 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 595 | true, newStride, colorOffset)); |
| 596 | } else if (allOffsetsChange || newColorOffset != oldColorOffset) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 597 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 598 | true, newStride, colorOffset)); |
| 599 | } |
| 600 | } else if (oldColorOffset > 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 601 | GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx())); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 602 | } |
| 603 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 604 | if (newCoverageOffset > 0) { |
bsalomon@google.com | e10f6fd | 2011-10-11 20:15:26 +0000 | [diff] [blame] | 605 | GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 606 | int idx = GrGLProgram::CoverageAttributeIdx(); |
| 607 | if (oldCoverageOffset <= 0) { |
| 608 | GL_CALL(EnableVertexAttribArray(idx)); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 609 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 610 | true, newStride, coverageOffset)); |
| 611 | } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 612 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 613 | true, newStride, coverageOffset)); |
| 614 | } |
| 615 | } else if (oldCoverageOffset > 0) { |
| 616 | GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx())); |
| 617 | } |
| 618 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 619 | if (newEdgeOffset > 0) { |
| 620 | GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset); |
| 621 | int idx = GrGLProgram::EdgeAttributeIdx(); |
| 622 | if (oldEdgeOffset <= 0) { |
| 623 | GL_CALL(EnableVertexAttribArray(idx)); |
| 624 | GL_CALL(VertexAttribPointer(idx, 4, scalarType, |
| 625 | false, newStride, edgeOffset)); |
| 626 | } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) { |
| 627 | GL_CALL(VertexAttribPointer(idx, 4, scalarType, |
| 628 | false, newStride, edgeOffset)); |
| 629 | } |
| 630 | } else if (oldEdgeOffset > 0) { |
| 631 | GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx())); |
| 632 | } |
| 633 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 634 | fHWGeometryState.fVertexLayout = currLayout; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 635 | fHWGeometryState.fArrayPtrsDirty = false; |
| 636 | } |
| 637 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 638 | namespace { |
| 639 | |
| 640 | void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage, |
| 641 | const GrSamplerState& sampler, |
| 642 | GrCustomStage** customStages, |
| 643 | GrGLProgram* program, int index) { |
| 644 | GrCustomStage* customStage = sampler.getCustomStage(); |
| 645 | if (customStage) { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 646 | const GrProgramStageFactory& factory = customStage->getFactory(); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 647 | stage->fCustomStageKey = factory.glStageKey(*customStage); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 648 | customStages[index] = customStage; |
| 649 | } else { |
| 650 | stage->fCustomStageKey = 0; |
| 651 | customStages[index] = NULL; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | } |
| 656 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 657 | void GrGpuGL::buildProgram(GrPrimitiveType type, |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 658 | BlendOptFlags blendOpts, |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 659 | GrBlendCoeff dstCoeff, |
| 660 | GrCustomStage** customStages) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 661 | ProgramDesc& desc = fCurrentProgram.fProgramDesc; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 662 | const GrDrawState& drawState = this->getDrawState(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 663 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 664 | // This should already have been caught |
| 665 | GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts)); |
| 666 | |
| 667 | bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag); |
| 668 | |
| 669 | bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag | |
| 670 | kEmitCoverage_BlendOptFlag)); |
| 671 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 672 | // The descriptor is used as a cache key. Thus when a field of the |
| 673 | // descriptor will not affect program generation (because of the vertex |
| 674 | // layout in use or other descriptor field settings) it should be set |
| 675 | // to a canonical value to avoid duplicate programs with different keys. |
| 676 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 677 | // Must initialize all fields or cache will have false negatives! |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 678 | desc.fVertexLayout = this->getVertexLayout(); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 679 | |
| 680 | desc.fEmitsPointSize = kPoints_PrimitiveType == type; |
| 681 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 682 | bool requiresAttributeColors = |
| 683 | !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 684 | bool requiresAttributeCoverage = |
| 685 | !skipCoverage && SkToBool(desc.fVertexLayout & |
| 686 | kCoverage_VertexLayoutBit); |
| 687 | |
| 688 | // fColorInput/fCoverageInput records how colors are specified for the. |
| 689 | // program. So we strip the bits from the layout to avoid false negatives |
| 690 | // when searching for an existing program in the cache. |
| 691 | desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 692 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 693 | desc.fColorFilterXfermode = skipColor ? |
| 694 | SkXfermode::kDst_Mode : |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 695 | drawState.getColorFilterMode(); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 696 | |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 697 | desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit); |
| 698 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 699 | // no reason to do edge aa or look at per-vertex coverage if coverage is |
| 700 | // ignored |
| 701 | if (skipCoverage) { |
| 702 | desc.fVertexLayout &= ~(kEdge_VertexLayoutBit | |
| 703 | kCoverage_VertexLayoutBit); |
| 704 | } |
| 705 | |
| 706 | bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag); |
| 707 | bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) || |
| 708 | (!requiresAttributeColors && |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 709 | 0xffffffff == drawState.getColor()); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 710 | if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 711 | desc.fColorInput = ProgramDesc::kTransBlack_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 712 | } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 713 | desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 714 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 715 | desc.fColorInput = ProgramDesc::kUniform_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 716 | } else { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 717 | desc.fColorInput = ProgramDesc::kAttribute_ColorInput; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 718 | } |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 719 | |
| 720 | bool covIsSolidWhite = !requiresAttributeCoverage && |
| 721 | 0xffffffff == drawState.getCoverage(); |
| 722 | |
| 723 | if (skipCoverage) { |
| 724 | desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput; |
| 725 | } else if (covIsSolidWhite) { |
| 726 | desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput; |
| 727 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) { |
| 728 | desc.fCoverageInput = ProgramDesc::kUniform_ColorInput; |
| 729 | } else { |
| 730 | desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput; |
| 731 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 732 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 733 | int lastEnabledStage = -1; |
| 734 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 735 | if (!skipCoverage && (desc.fVertexLayout & |
| 736 | GrDrawTarget::kEdge_VertexLayoutBit)) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 737 | desc.fVertexEdgeType = drawState.getVertexEdgeType(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 738 | } else { |
| 739 | // use canonical value when not set to avoid cache misses |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 740 | desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 741 | } |
| 742 | |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 743 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 744 | StageDesc& stage = desc.fStages[s]; |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 745 | |
| 746 | stage.fOptFlags = 0; |
| 747 | stage.setEnabled(this->isStageEnabled(s)); |
| 748 | |
| 749 | bool skip = s < drawState.getFirstCoverageStage() ? skipColor : |
| 750 | skipCoverage; |
| 751 | |
| 752 | if (!skip && stage.isEnabled()) { |
| 753 | lastEnabledStage = s; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 754 | const GrGLTexture* texture = |
| 755 | static_cast<const GrGLTexture*>(drawState.getTexture(s)); |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 756 | GrAssert(NULL != texture); |
| 757 | const GrSamplerState& sampler = drawState.getSampler(s); |
| 758 | // we matrix to invert when orientation is TopDown, so make sure |
| 759 | // we aren't in that case before flagging as identity. |
| 760 | if (TextureMatrixIsIdentity(texture, sampler)) { |
| 761 | stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit; |
| 762 | } else if (!sampler.getMatrix().hasPerspective()) { |
| 763 | stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit; |
| 764 | } |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 765 | |
| 766 | switch (sampler.getFilter()) { |
| 767 | // these both can use a regular texture2D() |
| 768 | case GrSamplerState::kNearest_Filter: |
| 769 | case GrSamplerState::kBilinear_Filter: |
| 770 | stage.fFetchMode = StageDesc::kSingle_FetchMode; |
| 771 | break; |
| 772 | // performs 4 texture2D()s |
| 773 | case GrSamplerState::k4x4Downsample_Filter: |
| 774 | stage.fFetchMode = StageDesc::k2x2_FetchMode; |
| 775 | break; |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 776 | default: |
| 777 | GrCrash("Unexpected filter!"); |
| 778 | break; |
| 779 | } |
| 780 | |
| 781 | if (sampler.hasTextureDomain()) { |
| 782 | GrAssert(GrSamplerState::kClamp_WrapMode == |
| 783 | sampler.getWrapX() && |
| 784 | GrSamplerState::kClamp_WrapMode == |
| 785 | sampler.getWrapY()); |
| 786 | stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit; |
| 787 | } |
| 788 | |
| 789 | stage.fInConfigFlags = 0; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 790 | if (!this->glCaps().textureSwizzleSupport()) { |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 791 | if (GrPixelConfigIsAlphaOnly(texture->config())) { |
| 792 | // if we don't have texture swizzle support then |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 793 | // the shader must smear the single channel after |
| 794 | // reading the texture |
| 795 | if (this->glCaps().textureRedSupport()) { |
| 796 | // we can use R8 textures so use kSmearRed |
| 797 | stage.fInConfigFlags |= |
| 798 | StageDesc::kSmearRed_InConfigFlag; |
| 799 | } else { |
| 800 | // we can use A8 textures so use kSmearAlpha |
| 801 | stage.fInConfigFlags |= |
| 802 | StageDesc::kSmearAlpha_InConfigFlag; |
| 803 | } |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 804 | } else if (sampler.swapsRAndB()) { |
| 805 | stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag; |
| 806 | } |
| 807 | } |
| 808 | if (GrPixelConfigIsUnpremultiplied(texture->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(texture->config())); |
| 812 | if (kUpOnWrite_DownOnRead_UnpremulConversion == |
| 813 | fUnpremulConversion) { |
| 814 | stage.fInConfigFlags |= |
| 815 | StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag; |
| 816 | } else { |
| 817 | stage.fInConfigFlags |= |
| 818 | StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag; |
| 819 | } |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 820 | } |
| 821 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 822 | setup_custom_stage(&stage, sampler, customStages, |
| 823 | &fCurrentProgram, s); |
| 824 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 825 | } else { |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 826 | stage.fOptFlags = 0; |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 827 | stage.fInConfigFlags = 0; |
| 828 | stage.fFetchMode = (StageDesc::FetchMode) 0; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 829 | stage.fCustomStageKey = 0; |
| 830 | customStages[s] = NULL; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 833 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 834 | if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) { |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 835 | // The shader generator assumes that color channels are bytes |
| 836 | // when rounding. |
| 837 | GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config())); |
| 838 | if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) { |
| 839 | desc.fOutputConfig = |
| 840 | ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig; |
| 841 | } else { |
| 842 | desc.fOutputConfig = |
| 843 | ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig; |
| 844 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 845 | } else { |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 846 | desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 847 | } |
| 848 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 849 | desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 850 | |
| 851 | // currently the experimental GS will only work with triangle prims |
| 852 | // (and it doesn't do anything other than pass through values from |
| 853 | // the VS to the FS anyway). |
| 854 | #if 0 && GR_GL_EXPERIMENTAL_GS |
| 855 | desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport; |
| 856 | #endif |
| 857 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 858 | // we want to avoid generating programs with different "first cov stage" |
| 859 | // values when they would compute the same result. |
| 860 | // We set field in the desc to kNumStages when either there are no |
| 861 | // coverage stages or the distinction between coverage and color is |
| 862 | // immaterial. |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 863 | int firstCoverageStage = GrDrawState::kNumStages; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 864 | desc.fFirstCoverageStage = GrDrawState::kNumStages; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 865 | bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 866 | if (hasCoverage) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 867 | firstCoverageStage = drawState.getFirstCoverageStage(); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | // other coverage inputs |
| 871 | if (!hasCoverage) { |
| 872 | hasCoverage = |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 873 | requiresAttributeCoverage || |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 874 | (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit); |
| 875 | } |
| 876 | |
| 877 | if (hasCoverage) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 878 | // color filter is applied between color/coverage computation |
| 879 | if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 880 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 881 | } |
| 882 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 883 | if (this->getCaps().fDualSourceBlendingSupport && |
| 884 | !(blendOpts & (kEmitCoverage_BlendOptFlag | |
| 885 | kCoverageAsAlpha_BlendOptFlag))) { |
| 886 | if (kZero_BlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 887 | // write the coverage value to second color |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 888 | desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 889 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 890 | } else if (kSA_BlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 891 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially |
| 892 | // cover |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 893 | desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 894 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 895 | } else if (kSC_BlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 896 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially |
| 897 | // cover |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 898 | desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 899 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 903 | } |