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