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