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