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