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