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