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