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