commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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. |
| 6 | */ |
| 7 | |
| 8 | #include "GrGLProgramEffects.h" |
| 9 | #include "GrDrawEffect.h" |
| 10 | #include "gl/GrGLEffect.h" |
| 11 | #include "gl/GrGLShaderBuilder.h" |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 12 | #include "gl/GrGLVertexEffect.h" |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 13 | #include "gl/GrGpuGL.h" |
| 14 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 15 | typedef GrGLProgramEffects::TransformedCoords TransformedCoords; |
| 16 | typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray; |
| 17 | typedef GrGLProgramEffects::TextureSampler TextureSampler; |
| 18 | typedef GrGLProgramEffects::TextureSamplerArray TextureSamplerArray; |
| 19 | |
| 20 | /** |
| 21 | * We specialize the vertex code for each of these matrix types. |
| 22 | */ |
| 23 | enum MatrixType { |
egdaniel | 4c6443e | 2014-06-24 13:43:12 -0700 | [diff] [blame] | 24 | kNoPersp_MatrixType = 0, |
| 25 | kGeneral_MatrixType = 1, |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | /** |
| 29 | * The key for an individual coord transform is made up of a matrix type and a bit that |
| 30 | * indicates the source of the input coords. |
| 31 | */ |
| 32 | enum { |
egdaniel | 4c6443e | 2014-06-24 13:43:12 -0700 | [diff] [blame] | 33 | kMatrixTypeKeyBits = 1, |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 34 | kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1, |
| 35 | kPositionCoords_Flag = (1 << kMatrixTypeKeyBits), |
| 36 | kTransformKeyBits = kMatrixTypeKeyBits + 1, |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | /** |
| 42 | * Do we need to either map r,g,b->a or a->r. configComponentMask indicates which channels are |
| 43 | * present in the texture's config. swizzleComponentMask indicates the channels present in the |
| 44 | * shader swizzle. |
| 45 | */ |
| 46 | inline bool swizzle_requires_alpha_remapping(const GrGLCaps& caps, |
| 47 | uint32_t configComponentMask, |
| 48 | uint32_t swizzleComponentMask) { |
| 49 | if (caps.textureSwizzleSupport()) { |
| 50 | // Any remapping is handled using texture swizzling not shader modifications. |
| 51 | return false; |
| 52 | } |
| 53 | // check if the texture is alpha-only |
| 54 | if (kA_GrColorComponentFlag == configComponentMask) { |
| 55 | if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleComponentMask)) { |
| 56 | // we must map the swizzle 'a's to 'r'. |
| 57 | return true; |
| 58 | } |
| 59 | if (kRGB_GrColorComponentFlags & swizzleComponentMask) { |
| 60 | // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that |
| 61 | // alpha-only textures smear alpha across all four channels when read. |
| 62 | return true; |
| 63 | } |
| 64 | } |
| 65 | return false; |
| 66 | } |
| 67 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 68 | /** |
| 69 | * Retrieves the matrix type from transformKey for the transform at transformIdx. |
| 70 | */ |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 71 | MatrixType get_matrix_type(uint32_t transformKey, int transformIdx) { |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 72 | return static_cast<MatrixType>( |
| 73 | (transformKey >> (kTransformKeyBits * transformIdx)) & kMatrixTypeKeyMask); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 74 | } |
| 75 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 76 | /** |
| 77 | * Retrieves the source coords from transformKey for the transform at transformIdx. It may not be |
| 78 | * the same coordinate set as the original GrCoordTransform if the position and local coords are |
| 79 | * identical for this program. |
| 80 | */ |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 81 | GrCoordSet get_source_coords(uint32_t transformKey, int transformIdx) { |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 82 | return (transformKey >> (kTransformKeyBits * transformIdx)) & kPositionCoords_Flag ? |
| 83 | kPosition_GrCoordSet : |
| 84 | kLocal_GrCoordSet; |
| 85 | } |
| 86 | |
| 87 | /** |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 88 | * Retrieves the final matrix that a transform needs to apply to its source coords. |
| 89 | */ |
| 90 | SkMatrix get_transform_matrix(const GrDrawEffect& drawEffect, int transformIdx) { |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 91 | const GrCoordTransform& coordTransform = drawEffect.effect()->coordTransform(transformIdx); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 92 | SkMatrix combined; |
| 93 | if (kLocal_GrCoordSet == coordTransform.sourceCoords() && |
| 94 | !drawEffect.programHasExplicitLocalCoords()) { |
| 95 | combined.setConcat(coordTransform.getMatrix(), drawEffect.getCoordChangeMatrix()); |
| 96 | } else { |
| 97 | combined = coordTransform.getMatrix(); |
| 98 | } |
| 99 | if (coordTransform.reverseY()) { |
| 100 | // combined.postScale(1,-1); |
| 101 | // combined.postTranslate(0,1); |
| 102 | combined.set(SkMatrix::kMSkewY, |
| 103 | combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]); |
| 104 | combined.set(SkMatrix::kMScaleY, |
| 105 | combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]); |
| 106 | combined.set(SkMatrix::kMTransY, |
| 107 | combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]); |
| 108 | } |
| 109 | return combined; |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | //////////////////////////////////////////////////////////////////////////////// |
| 115 | |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 116 | bool GrGLProgramEffects::GenEffectMetaKey(const GrDrawEffect& drawEffect, const GrGLCaps& caps, |
| 117 | GrEffectKeyBuilder* b) { |
| 118 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 119 | uint32_t textureKey = GrGLProgramEffects::GenTextureKey(drawEffect, caps); |
| 120 | uint32_t transformKey = GrGLProgramEffects::GenTransformKey(drawEffect); |
| 121 | uint32_t attribKey = GrGLProgramEffects::GenAttribKey(drawEffect); |
bsalomon | 929f29a | 2014-07-17 07:55:11 -0700 | [diff] [blame] | 122 | uint32_t classID = drawEffect.effect()->getFactory().effectClassID(); |
| 123 | |
| 124 | // Currently we allow 16 bits for each of the above portions of the meta-key. Fail if they |
| 125 | // don't fit. |
| 126 | static const uint32_t kMetaKeyInvalidMask = ~((uint32_t) SK_MaxU16); |
| 127 | if ((textureKey | transformKey | attribKey | classID) & kMetaKeyInvalidMask) { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | uint32_t* key = b->add32n(2); |
| 132 | key[0] = (textureKey << 16 | transformKey); |
| 133 | key[1] = (classID << 16 | attribKey); |
| 134 | return true; |
| 135 | } |
| 136 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 137 | uint32_t GrGLProgramEffects::GenAttribKey(const GrDrawEffect& drawEffect) { |
| 138 | uint32_t key = 0; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 139 | int numAttributes = drawEffect.getVertexAttribIndexCount(); |
| 140 | SkASSERT(numAttributes <= 2); |
| 141 | const int* attributeIndices = drawEffect.getVertexAttribIndices(); |
| 142 | for (int a = 0; a < numAttributes; ++a) { |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 143 | uint32_t value = attributeIndices[a] << 3 * a; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 144 | SkASSERT(0 == (value & key)); // keys for each attribute ought not to overlap |
| 145 | key |= value; |
| 146 | } |
| 147 | return key; |
| 148 | } |
| 149 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 150 | uint32_t GrGLProgramEffects::GenTransformKey(const GrDrawEffect& drawEffect) { |
| 151 | uint32_t totalKey = 0; |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 152 | int numTransforms = drawEffect.effect()->numTransforms(); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 153 | for (int t = 0; t < numTransforms; ++t) { |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 154 | uint32_t key = 0; |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 155 | const GrCoordTransform& coordTransform = drawEffect.effect()->coordTransform(t); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 156 | SkMatrix::TypeMask type0 = coordTransform.getMatrix().getType(); |
| 157 | SkMatrix::TypeMask type1; |
| 158 | if (kLocal_GrCoordSet == coordTransform.sourceCoords()) { |
| 159 | type1 = drawEffect.getCoordChangeMatrix().getType(); |
| 160 | } else { |
| 161 | if (drawEffect.programHasExplicitLocalCoords()) { |
| 162 | // We only make the key indicate that device coords are referenced when the local coords |
| 163 | // are not actually determined by positions. Otherwise the local coords var and position |
| 164 | // var are identical. |
| 165 | key |= kPositionCoords_Flag; |
| 166 | } |
| 167 | type1 = SkMatrix::kIdentity_Mask; |
| 168 | } |
| 169 | |
| 170 | int combinedTypes = type0 | type1; |
| 171 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 172 | if (SkMatrix::kPerspective_Mask & combinedTypes) { |
| 173 | key |= kGeneral_MatrixType; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 174 | } else { |
egdaniel | 4c6443e | 2014-06-24 13:43:12 -0700 | [diff] [blame] | 175 | key |= kNoPersp_MatrixType; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 176 | } |
| 177 | key <<= kTransformKeyBits * t; |
| 178 | SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap |
| 179 | totalKey |= key; |
| 180 | } |
| 181 | return totalKey; |
| 182 | } |
| 183 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 184 | uint32_t GrGLProgramEffects::GenTextureKey(const GrDrawEffect& drawEffect, const GrGLCaps& caps) { |
| 185 | uint32_t key = 0; |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 186 | int numTextures = drawEffect.effect()->numTextures(); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 187 | for (int t = 0; t < numTextures; ++t) { |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 188 | const GrTextureAccess& access = drawEffect.effect()->textureAccess(t); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 189 | uint32_t configComponentMask = GrPixelConfigComponentMask(access.getTexture()->config()); |
| 190 | if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.swizzleMask())) { |
| 191 | key |= 1 << t; |
| 192 | } |
| 193 | } |
| 194 | return key; |
| 195 | } |
| 196 | |
| 197 | GrGLProgramEffects::~GrGLProgramEffects() { |
| 198 | int numEffects = fGLEffects.count(); |
| 199 | for (int e = 0; e < numEffects; ++e) { |
| 200 | SkDELETE(fGLEffects[e]); |
| 201 | } |
| 202 | } |
| 203 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 204 | void GrGLProgramEffects::emitSamplers(GrGLShaderBuilder* builder, |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 205 | const GrEffect* effect, |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 206 | TextureSamplerArray* outSamplers) { |
| 207 | SkTArray<Sampler, true>& samplers = fSamplers.push_back(); |
| 208 | int numTextures = effect->numTextures(); |
| 209 | samplers.push_back_n(numTextures); |
| 210 | SkString name; |
| 211 | for (int t = 0; t < numTextures; ++t) { |
| 212 | name.printf("Sampler%d", t); |
| 213 | samplers[t].fUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 214 | kSampler2D_GrSLType, |
| 215 | name.c_str()); |
| 216 | SkNEW_APPEND_TO_TARRAY(outSamplers, TextureSampler, |
| 217 | (samplers[t].fUniform, effect->textureAccess(t))); |
| 218 | } |
| 219 | } |
| 220 | |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 221 | void GrGLProgramEffects::initSamplers(const GrGLUniformManager& uniformManager, int* texUnitIdx) { |
| 222 | int numEffects = fGLEffects.count(); |
| 223 | SkASSERT(numEffects == fSamplers.count()); |
| 224 | for (int e = 0; e < numEffects; ++e) { |
| 225 | SkTArray<Sampler, true>& samplers = fSamplers[e]; |
| 226 | int numSamplers = samplers.count(); |
| 227 | for (int s = 0; s < numSamplers; ++s) { |
| 228 | SkASSERT(samplers[s].fUniform.isValid()); |
| 229 | uniformManager.setSampler(samplers[s].fUniform, *texUnitIdx); |
| 230 | samplers[s].fTextureUnit = (*texUnitIdx)++; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 235 | void GrGLProgramEffects::bindTextures(GrGpuGL* gpu, const GrEffect* effect, int effectIdx) { |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 236 | const SkTArray<Sampler, true>& samplers = fSamplers[effectIdx]; |
| 237 | int numSamplers = samplers.count(); |
| 238 | SkASSERT(numSamplers == effect->numTextures()); |
| 239 | for (int s = 0; s < numSamplers; ++s) { |
| 240 | SkASSERT(samplers[s].fTextureUnit >= 0); |
| 241 | const GrTextureAccess& textureAccess = effect->textureAccess(s); |
| 242 | gpu->bindTexture(samplers[s].fTextureUnit, |
| 243 | textureAccess.getParams(), |
| 244 | static_cast<GrGLTexture*>(textureAccess.getTexture())); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | //////////////////////////////////////////////////////////////////////////////// |
| 249 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 250 | void GrGLVertexProgramEffects::emitEffect(GrGLFullShaderBuilder* builder, |
| 251 | const GrEffectStage& stage, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 252 | const GrEffectKey& key, |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 253 | const char* outColor, |
| 254 | const char* inColor, |
| 255 | int stageIndex) { |
| 256 | GrDrawEffect drawEffect(stage, fHasExplicitLocalCoords); |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 257 | const GrEffect* effect = stage.getEffect(); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 258 | SkSTArray<2, TransformedCoords> coords(effect->numTransforms()); |
| 259 | SkSTArray<4, TextureSampler> samplers(effect->numTextures()); |
| 260 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 261 | this->emitAttributes(builder, stage); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 262 | this->emitTransforms(builder, drawEffect, &coords); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 263 | this->emitSamplers(builder, effect, &samplers); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 264 | |
| 265 | GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 266 | fGLEffects.push_back(glEffect); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 267 | |
| 268 | // Enclose custom code in a block to avoid namespace conflicts |
| 269 | SkString openBrace; |
| 270 | openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name()); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 271 | builder->vsCodeAppend(openBrace.c_str()); |
| 272 | builder->fsCodeAppend(openBrace.c_str()); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 273 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 274 | if (glEffect->isVertexEffect()) { |
| 275 | GrGLVertexEffect* vertexEffect = static_cast<GrGLVertexEffect*>(glEffect); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 276 | vertexEffect->emitCode(builder, drawEffect, key, outColor, inColor, coords, samplers); |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 277 | } else { |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 278 | glEffect->emitCode(builder, drawEffect, key, outColor, inColor, coords, samplers); |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 279 | } |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 280 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 281 | builder->vsCodeAppend("\t}\n"); |
| 282 | builder->fsCodeAppend("\t}\n"); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 283 | } |
| 284 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 285 | void GrGLVertexProgramEffects::emitAttributes(GrGLFullShaderBuilder* builder, |
| 286 | const GrEffectStage& stage) { |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 287 | int numAttributes = stage.getVertexAttribIndexCount(); |
| 288 | const int* attributeIndices = stage.getVertexAttribIndices(); |
| 289 | for (int a = 0; a < numAttributes; ++a) { |
| 290 | // TODO: Make addAttribute mangle the name. |
| 291 | SkString attributeName("aAttr"); |
| 292 | attributeName.appendS32(attributeIndices[a]); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 293 | builder->addEffectAttribute(attributeIndices[a], |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 294 | stage.getEffect()->vertexAttribType(a), |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 295 | attributeName); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 299 | void GrGLVertexProgramEffects::emitTransforms(GrGLFullShaderBuilder* builder, |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 300 | const GrDrawEffect& drawEffect, |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 301 | TransformedCoordsArray* outCoords) { |
| 302 | SkTArray<Transform, true>& transforms = fTransforms.push_back(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 303 | uint32_t totalKey = GenTransformKey(drawEffect); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 304 | int numTransforms = drawEffect.effect()->numTransforms(); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 305 | transforms.push_back_n(numTransforms); |
| 306 | for (int t = 0; t < numTransforms; t++) { |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 307 | GrSLType varyingType = kVoid_GrSLType; |
| 308 | const char* uniName; |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 309 | switch (get_matrix_type(totalKey, t)) { |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 310 | case kNoPersp_MatrixType: |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 311 | uniName = "StageMatrix"; |
| 312 | varyingType = kVec2f_GrSLType; |
| 313 | break; |
| 314 | case kGeneral_MatrixType: |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 315 | uniName = "StageMatrix"; |
| 316 | varyingType = kVec3f_GrSLType; |
| 317 | break; |
| 318 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 319 | SkFAIL("Unexpected key."); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 320 | } |
| 321 | SkString suffixedUniName; |
egdaniel | 4c6443e | 2014-06-24 13:43:12 -0700 | [diff] [blame] | 322 | if (0 != t) { |
| 323 | suffixedUniName.append(uniName); |
| 324 | suffixedUniName.appendf("_%i", t); |
| 325 | uniName = suffixedUniName.c_str(); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 326 | } |
egdaniel | 4c6443e | 2014-06-24 13:43:12 -0700 | [diff] [blame] | 327 | transforms[t].fHandle = builder->addUniform(GrGLShaderBuilder::kVertex_Visibility, |
| 328 | kMat33f_GrSLType, |
| 329 | uniName, |
| 330 | &uniName); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 331 | |
| 332 | const char* varyingName = "MatrixCoord"; |
| 333 | SkString suffixedVaryingName; |
| 334 | if (0 != t) { |
| 335 | suffixedVaryingName.append(varyingName); |
| 336 | suffixedVaryingName.appendf("_%i", t); |
| 337 | varyingName = suffixedVaryingName.c_str(); |
| 338 | } |
| 339 | const char* vsVaryingName; |
| 340 | const char* fsVaryingName; |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 341 | builder->addVarying(varyingType, varyingName, &vsVaryingName, &fsVaryingName); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 342 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 343 | const GrGLShaderVar& coords = kPosition_GrCoordSet == get_source_coords(totalKey, t) ? |
| 344 | builder->positionAttribute() : |
| 345 | builder->localCoordsAttribute(); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 346 | // varying = matrix * coords (logically) |
egdaniel | 4c6443e | 2014-06-24 13:43:12 -0700 | [diff] [blame] | 347 | SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == varyingType); |
| 348 | if (kVec2f_GrSLType == varyingType) { |
| 349 | builder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n", |
| 350 | vsVaryingName, uniName, coords.c_str()); |
| 351 | } else { |
| 352 | builder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n", |
| 353 | vsVaryingName, uniName, coords.c_str()); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 354 | } |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 355 | SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords, |
| 356 | (SkString(fsVaryingName), varyingType)); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 360 | void GrGLVertexProgramEffects::setData(GrGpuGL* gpu, |
| 361 | const GrGLUniformManager& uniformManager, |
| 362 | const GrEffectStage* effectStages[]) { |
| 363 | int numEffects = fGLEffects.count(); |
| 364 | SkASSERT(numEffects == fTransforms.count()); |
| 365 | SkASSERT(numEffects == fSamplers.count()); |
| 366 | for (int e = 0; e < numEffects; ++e) { |
| 367 | GrDrawEffect drawEffect(*effectStages[e], fHasExplicitLocalCoords); |
| 368 | fGLEffects[e]->setData(uniformManager, drawEffect); |
| 369 | this->setTransformData(uniformManager, drawEffect, e); |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 370 | this->bindTextures(gpu, drawEffect.effect(), e); |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 373 | |
| 374 | void GrGLVertexProgramEffects::setTransformData(const GrGLUniformManager& uniformManager, |
| 375 | const GrDrawEffect& drawEffect, |
| 376 | int effectIdx) { |
| 377 | SkTArray<Transform, true>& transforms = fTransforms[effectIdx]; |
| 378 | int numTransforms = transforms.count(); |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 379 | SkASSERT(numTransforms == drawEffect.effect()->numTransforms()); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 380 | for (int t = 0; t < numTransforms; ++t) { |
egdaniel | 4c6443e | 2014-06-24 13:43:12 -0700 | [diff] [blame] | 381 | SkASSERT(transforms[t].fHandle.isValid()); |
| 382 | const SkMatrix& matrix = get_transform_matrix(drawEffect, t); |
| 383 | if (!transforms[t].fCurrentValue.cheapEqualTo(matrix)) { |
| 384 | uniformManager.setSkMatrix(transforms[t].fHandle, matrix); |
| 385 | transforms[t].fCurrentValue = matrix; |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | GrGLVertexProgramEffectsBuilder::GrGLVertexProgramEffectsBuilder(GrGLFullShaderBuilder* builder, |
| 391 | int reserveCount) |
| 392 | : fBuilder(builder) |
| 393 | , fProgramEffects(SkNEW_ARGS(GrGLVertexProgramEffects, |
| 394 | (reserveCount, fBuilder->hasExplicitLocalCoords()))) { |
| 395 | } |
| 396 | |
| 397 | void GrGLVertexProgramEffectsBuilder::emitEffect(const GrEffectStage& stage, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 398 | const GrEffectKey& key, |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 399 | const char* outColor, |
| 400 | const char* inColor, |
| 401 | int stageIndex) { |
| 402 | SkASSERT(NULL != fProgramEffects.get()); |
| 403 | fProgramEffects->emitEffect(fBuilder, stage, key, outColor, inColor, stageIndex); |
| 404 | } |
| 405 | |
| 406 | //////////////////////////////////////////////////////////////////////////////// |
| 407 | |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 408 | void GrGLPathTexGenProgramEffects::emitEffect(GrGLFragmentOnlyShaderBuilder* builder, |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 409 | const GrEffectStage& stage, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 410 | const GrEffectKey& key, |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 411 | const char* outColor, |
| 412 | const char* inColor, |
| 413 | int stageIndex) { |
| 414 | GrDrawEffect drawEffect(stage, false); |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 415 | const GrEffect* effect = stage.getEffect(); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 416 | SkSTArray<2, TransformedCoords> coords(effect->numTransforms()); |
| 417 | SkSTArray<4, TextureSampler> samplers(effect->numTextures()); |
| 418 | |
| 419 | SkASSERT(0 == stage.getVertexAttribIndexCount()); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 420 | this->setupPathTexGen(builder, drawEffect, &coords); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 421 | this->emitSamplers(builder, effect, &samplers); |
| 422 | |
| 423 | GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect); |
| 424 | fGLEffects.push_back(glEffect); |
| 425 | |
| 426 | // Enclose custom code in a block to avoid namespace conflicts |
| 427 | SkString openBrace; |
| 428 | openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name()); |
| 429 | builder->fsCodeAppend(openBrace.c_str()); |
| 430 | |
| 431 | SkASSERT(!glEffect->isVertexEffect()); |
| 432 | glEffect->emitCode(builder, drawEffect, key, outColor, inColor, coords, samplers); |
| 433 | |
| 434 | builder->fsCodeAppend("\t}\n"); |
| 435 | } |
| 436 | |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 437 | void GrGLPathTexGenProgramEffects::setupPathTexGen(GrGLFragmentOnlyShaderBuilder* builder, |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 438 | const GrDrawEffect& drawEffect, |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 439 | TransformedCoordsArray* outCoords) { |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 440 | int numTransforms = drawEffect.effect()->numTransforms(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 441 | uint32_t totalKey = GenTransformKey(drawEffect); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 442 | int texCoordIndex = builder->addTexCoordSets(numTransforms); |
| 443 | SkNEW_APPEND_TO_TARRAY(&fTransforms, Transforms, (totalKey, texCoordIndex)); |
| 444 | SkString name; |
| 445 | for (int t = 0; t < numTransforms; ++t) { |
| 446 | GrSLType type = kGeneral_MatrixType == get_matrix_type(totalKey, t) ? |
| 447 | kVec3f_GrSLType : |
| 448 | kVec2f_GrSLType; |
| 449 | name.printf("%s(gl_TexCoord[%i])", GrGLSLTypeString(type), texCoordIndex++); |
| 450 | SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords, (name, type)); |
| 451 | } |
| 452 | } |
| 453 | |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 454 | void GrGLPathTexGenProgramEffects::setData(GrGpuGL* gpu, |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 455 | const GrGLUniformManager& uniformManager, |
| 456 | const GrEffectStage* effectStages[]) { |
| 457 | int numEffects = fGLEffects.count(); |
| 458 | SkASSERT(numEffects == fTransforms.count()); |
| 459 | SkASSERT(numEffects == fSamplers.count()); |
| 460 | for (int e = 0; e < numEffects; ++e) { |
| 461 | GrDrawEffect drawEffect(*effectStages[e], false); |
| 462 | fGLEffects[e]->setData(uniformManager, drawEffect); |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 463 | this->setPathTexGenState(gpu, drawEffect, e); |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 464 | this->bindTextures(gpu, drawEffect.effect(), e); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 468 | void GrGLPathTexGenProgramEffects::setPathTexGenState(GrGpuGL* gpu, |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 469 | const GrDrawEffect& drawEffect, |
| 470 | int effectIdx) { |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 471 | uint32_t totalKey = fTransforms[effectIdx].fTransformKey; |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 472 | int texCoordIndex = fTransforms[effectIdx].fTexCoordIndex; |
bsalomon | f99f884 | 2014-07-07 11:54:23 -0700 | [diff] [blame] | 473 | int numTransforms = drawEffect.effect()->numTransforms(); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 474 | for (int t = 0; t < numTransforms; ++t) { |
| 475 | switch (get_matrix_type(totalKey, t)) { |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 476 | case kNoPersp_MatrixType: { |
| 477 | const SkMatrix& transform = get_transform_matrix(drawEffect, t); |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 478 | gpu->enablePathTexGen(texCoordIndex++, |
| 479 | GrGpuGL::kST_PathTexGenComponents, |
| 480 | transform); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 481 | break; |
| 482 | } |
| 483 | case kGeneral_MatrixType: { |
| 484 | const SkMatrix& transform = get_transform_matrix(drawEffect, t); |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 485 | gpu->enablePathTexGen(texCoordIndex++, |
| 486 | GrGpuGL::kSTR_PathTexGenComponents, |
| 487 | transform); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 488 | break; |
| 489 | } |
| 490 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 491 | SkFAIL("Unexpected matrixs type."); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 496 | GrGLPathTexGenProgramEffectsBuilder::GrGLPathTexGenProgramEffectsBuilder( |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 497 | GrGLFragmentOnlyShaderBuilder* builder, |
| 498 | int reserveCount) |
| 499 | : fBuilder(builder) |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 500 | , fProgramEffects(SkNEW_ARGS(GrGLPathTexGenProgramEffects, (reserveCount))) { |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 501 | } |
| 502 | |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 503 | void GrGLPathTexGenProgramEffectsBuilder::emitEffect(const GrEffectStage& stage, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 504 | const GrEffectKey& key, |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 505 | const char* outColor, |
| 506 | const char* inColor, |
| 507 | int stageIndex) { |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 508 | SkASSERT(NULL != fProgramEffects.get()); |
| 509 | fProgramEffects->emitEffect(fBuilder, stage, key, outColor, inColor, stageIndex); |
| 510 | } |