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