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