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