123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931 |
- /*
- * Copyright 2013 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
- #include "include/gpu/GrTexture.h"
- #include "src/core/SkDistanceFieldGen.h"
- #include "src/gpu/GrCaps.h"
- #include "src/gpu/GrShaderCaps.h"
- #include "src/gpu/effects/GrAtlasedShaderHelpers.h"
- #include "src/gpu/effects/GrDistanceFieldGeoProc.h"
- #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
- #include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
- #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
- #include "src/gpu/glsl/GrGLSLUniformHandler.h"
- #include "src/gpu/glsl/GrGLSLUtil.h"
- #include "src/gpu/glsl/GrGLSLVarying.h"
- #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
- // Assuming a radius of a little less than the diagonal of the fragment
- #define SK_DistanceFieldAAFactor "0.65"
- class GrGLDistanceFieldA8TextGeoProc : public GrGLSLGeometryProcessor {
- public:
- GrGLDistanceFieldA8TextGeoProc() = default;
- void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
- const GrDistanceFieldA8TextGeoProc& dfTexEffect =
- args.fGP.cast<GrDistanceFieldA8TextGeoProc>();
- GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
- GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
- GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
- GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
- // emit attributes
- varyingHandler->emitAttributes(dfTexEffect);
- const char* atlasSizeInvName;
- fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
- kFloat2_GrSLType,
- "AtlasSizeInv",
- &atlasSizeInvName);
- #ifdef SK_GAMMA_APPLY_TO_A8
- // adjust based on gamma
- const char* distanceAdjustUniName = nullptr;
- // width, height, 1/(3*width)
- fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
- "DistanceAdjust", &distanceAdjustUniName);
- #endif
- // Setup pass through color
- varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
- // Setup position
- gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
- // emit transforms
- this->emitTransforms(vertBuilder,
- varyingHandler,
- uniformHandler,
- dfTexEffect.inPosition().asShaderVar(),
- dfTexEffect.localMatrix(),
- args.fFPCoordTransformHandler);
- // add varyings
- GrGLSLVarying uv(kFloat2_GrSLType);
- GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
- GrGLSLVarying texIdx(texIdxType);
- GrGLSLVarying st(kFloat2_GrSLType);
- append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
- &texIdx, &st);
- bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
- kUniformScale_DistanceFieldEffectMask;
- bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
- bool isGammaCorrect =
- SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
- bool isAliased =
- SkToBool(dfTexEffect.getFlags() & kAliased_DistanceFieldEffectFlag);
- // Use highp to work around aliasing issues
- fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
- fragBuilder->codeAppend("half4 texColor;");
- append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
- texIdx, "uv", "texColor");
- fragBuilder->codeAppend("half distance = "
- SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
- #ifdef SK_GAMMA_APPLY_TO_A8
- // adjust width based on gamma
- fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
- #endif
- fragBuilder->codeAppend("half afwidth;");
- if (isUniformScale) {
- // For uniform scale, we adjust for the effect of the transformation on the distance
- // by using the length of the gradient of the t coordinate in the y direction.
- // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
- // this gives us a smooth step across approximately one fragment
- #ifdef SK_VULKAN
- fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
- "*half(dFdx(%s.x)));", st.fsIn());
- #else
- // We use the y gradient because there is a bug in the Mali 400 in the x direction.
- fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
- "*half(dFdy(%s.y)));", st.fsIn());
- #endif
- } else if (isSimilarity) {
- // For similarity transform, we adjust the effect of the transformation on the distance
- // by using the length of the gradient of the texture coordinates. We use st coordinates
- // to ensure we're mapping 1:1 from texel space to pixel space.
- // We use the y gradient because there is a bug in the Mali 400 in the x direction.
- // this gives us a smooth step across approximately one fragment
- #ifdef SK_VULKAN
- fragBuilder->codeAppendf("half st_grad_len = length(half2(dFdx(%s)));", st.fsIn());
- #else
- // We use the y gradient because there is a bug in the Mali 400 in the x direction.
- fragBuilder->codeAppendf("half st_grad_len = length(half2(dFdy(%s)));", st.fsIn());
- #endif
- fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
- } else {
- // For general transforms, to determine the amount of correction we multiply a unit
- // vector pointing along the SDF gradient direction by the Jacobian of the st coords
- // (which is the inverse transform for this fragment) and take the length of the result.
- fragBuilder->codeAppend("half2 dist_grad = half2(float2(dFdx(distance), "
- "dFdy(distance)));");
- // the length of the gradient may be 0, so we need to check for this
- // this also compensates for the Adreno, which likes to drop tiles on division by 0
- fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
- fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
- fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
- fragBuilder->codeAppend("} else {");
- fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
- fragBuilder->codeAppend("}");
- fragBuilder->codeAppendf("half2 Jdx = half2(dFdx(%s));", st.fsIn());
- fragBuilder->codeAppendf("half2 Jdy = half2(dFdy(%s));", st.fsIn());
- fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
- fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
- // this gives us a smooth step across approximately one fragment
- fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
- }
- if (isAliased) {
- fragBuilder->codeAppend("half val = distance > 0 ? 1.0 : 0.0;");
- } else if (isGammaCorrect) {
- // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
- // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want
- // distance mapped linearly to coverage, so use a linear step:
- fragBuilder->codeAppend(
- "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
- } else {
- fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
- }
- fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
- }
- void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
- FPCoordTransformIter&& transformIter) override {
- const GrDistanceFieldA8TextGeoProc& dfa8gp = proc.cast<GrDistanceFieldA8TextGeoProc>();
- #ifdef SK_GAMMA_APPLY_TO_A8
- float distanceAdjust = dfa8gp.getDistanceAdjust();
- if (distanceAdjust != fDistanceAdjust) {
- fDistanceAdjust = distanceAdjust;
- pdman.set1f(fDistanceAdjustUni, distanceAdjust);
- }
- #endif
- const SkISize& atlasSize = dfa8gp.atlasSize();
- SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
- if (fAtlasSize != atlasSize) {
- pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
- fAtlasSize = atlasSize;
- }
- this->setTransformDataHelper(dfa8gp.localMatrix(), pdman, &transformIter);
- }
- static inline void GenKey(const GrGeometryProcessor& gp,
- const GrShaderCaps&,
- GrProcessorKeyBuilder* b) {
- const GrDistanceFieldA8TextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldA8TextGeoProc>();
- uint32_t key = dfTexEffect.getFlags();
- b->add32(key);
- b->add32(dfTexEffect.numTextureSamplers());
- }
- private:
- #ifdef SK_GAMMA_APPLY_TO_A8
- float fDistanceAdjust = -1.f;
- UniformHandle fDistanceAdjustUni;
- #endif
- SkISize fAtlasSize = {0, 0};
- UniformHandle fAtlasSizeInvUniform;
- typedef GrGLSLGeometryProcessor INHERITED;
- };
- ///////////////////////////////////////////////////////////////////////////////
- GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(const GrShaderCaps& caps,
- const sk_sp<GrTextureProxy>* proxies,
- int numProxies,
- const GrSamplerState& params,
- #ifdef SK_GAMMA_APPLY_TO_A8
- float distanceAdjust,
- #endif
- uint32_t flags,
- const SkMatrix& localMatrix)
- : INHERITED(kGrDistanceFieldA8TextGeoProc_ClassID)
- , fLocalMatrix(localMatrix)
- , fFlags(flags & kNonLCD_DistanceFieldEffectMask)
- #ifdef SK_GAMMA_APPLY_TO_A8
- , fDistanceAdjust(distanceAdjust)
- #endif
- {
- SkASSERT(numProxies <= kMaxTextures);
- SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
- if (flags & kPerspective_DistanceFieldEffectFlag) {
- fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
- } else {
- fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
- }
- fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType };
- fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
- caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
- this->setVertexAttributes(&fInPosition, 3);
- if (numProxies) {
- fAtlasSize = proxies[0]->isize();
- }
- for (int i = 0; i < numProxies; ++i) {
- SkASSERT(proxies[i]);
- SkASSERT(proxies[i]->isize() == fAtlasSize);
- fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params,
- proxies[i]->textureSwizzle());
- }
- this->setTextureSamplerCnt(numProxies);
- }
- void GrDistanceFieldA8TextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
- int numProxies,
- const GrSamplerState& params) {
- SkASSERT(numProxies <= kMaxTextures);
- if (!fTextureSamplers[0].isInitialized()) {
- fAtlasSize = proxies[0]->isize();
- }
- for (int i = 0; i < numProxies; ++i) {
- SkASSERT(proxies[i]);
- SkASSERT(proxies[i]->isize() == fAtlasSize);
- if (!fTextureSamplers[i].isInitialized()) {
- fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params,
- proxies[i]->textureSwizzle());
- }
- }
- this->setTextureSamplerCnt(numProxies);
- }
- void GrDistanceFieldA8TextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
- GrProcessorKeyBuilder* b) const {
- GrGLDistanceFieldA8TextGeoProc::GenKey(*this, caps, b);
- }
- GrGLSLPrimitiveProcessor*
- GrDistanceFieldA8TextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
- return new GrGLDistanceFieldA8TextGeoProc();
- }
- ///////////////////////////////////////////////////////////////////////////////
- GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldA8TextGeoProc);
- #if GR_TEST_UTILS
- sk_sp<GrGeometryProcessor> GrDistanceFieldA8TextGeoProc::TestCreate(GrProcessorTestData* d) {
- int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
- : GrProcessorUnitTest::kAlphaTextureIdx;
- sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
- d->textureProxy(texIdx),
- nullptr,
- nullptr,
- nullptr
- };
- GrSamplerState::WrapMode wrapModes[2];
- GrTest::TestWrapModes(d->fRandom, wrapModes);
- GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
- ? GrSamplerState::Filter::kBilerp
- : GrSamplerState::Filter::kNearest);
- uint32_t flags = 0;
- flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
- if (flags & kSimilarity_DistanceFieldEffectFlag) {
- flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
- }
- SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
- #ifdef SK_GAMMA_APPLY_TO_A8
- float lum = d->fRandom->nextF();
- #endif
- return GrDistanceFieldA8TextGeoProc::Make(*d->caps()->shaderCaps(),
- proxies, 1,
- samplerState,
- #ifdef SK_GAMMA_APPLY_TO_A8
- lum,
- #endif
- flags, localMatrix);
- }
- #endif
- ///////////////////////////////////////////////////////////////////////////////
- class GrGLDistanceFieldPathGeoProc : public GrGLSLGeometryProcessor {
- public:
- GrGLDistanceFieldPathGeoProc()
- : fMatrix(SkMatrix::InvalidMatrix())
- , fAtlasSize({0,0}) {
- }
- void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
- const GrDistanceFieldPathGeoProc& dfPathEffect =
- args.fGP.cast<GrDistanceFieldPathGeoProc>();
- GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
- GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
- GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
- GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
- // emit attributes
- varyingHandler->emitAttributes(dfPathEffect);
- const char* atlasSizeInvName;
- fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
- kFloat2_GrSLType,
- "AtlasSizeInv",
- &atlasSizeInvName);
- GrGLSLVarying uv(kFloat2_GrSLType);
- GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
- GrGLSLVarying texIdx(texIdxType);
- GrGLSLVarying st(kFloat2_GrSLType);
- append_index_uv_varyings(args, dfPathEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
- &texIdx, &st);
- // setup pass through color
- varyingHandler->addPassThroughAttribute(dfPathEffect.inColor(), args.fOutputColor);
- if (dfPathEffect.matrix().hasPerspective()) {
- // Setup position
- this->writeOutputPosition(vertBuilder,
- uniformHandler,
- gpArgs,
- dfPathEffect.inPosition().name(),
- dfPathEffect.matrix(),
- &fMatrixUniform);
- // emit transforms
- this->emitTransforms(vertBuilder,
- varyingHandler,
- uniformHandler,
- dfPathEffect.inPosition().asShaderVar(),
- args.fFPCoordTransformHandler);
- } else {
- // Setup position
- this->writeOutputPosition(vertBuilder, gpArgs, dfPathEffect.inPosition().name());
- // emit transforms
- this->emitTransforms(vertBuilder,
- varyingHandler,
- uniformHandler,
- dfPathEffect.inPosition().asShaderVar(),
- dfPathEffect.matrix(),
- args.fFPCoordTransformHandler);
- }
- // Use highp to work around aliasing issues
- fragBuilder->codeAppendf("float2 uv = %s;", uv.fsIn());
- fragBuilder->codeAppend("half4 texColor;");
- append_multitexture_lookup(args, dfPathEffect.numTextureSamplers(), texIdx, "uv",
- "texColor");
- fragBuilder->codeAppend("half distance = "
- SK_DistanceFieldMultiplier "*(texColor.r - " SK_DistanceFieldThreshold ");");
- fragBuilder->codeAppend("half afwidth;");
- bool isUniformScale = (dfPathEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
- kUniformScale_DistanceFieldEffectMask;
- bool isSimilarity = SkToBool(dfPathEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
- bool isGammaCorrect =
- SkToBool(dfPathEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
- if (isUniformScale) {
- // For uniform scale, we adjust for the effect of the transformation on the distance
- // by using the length of the gradient of the t coordinate in the y direction.
- // We use st coordinates to ensure we're mapping 1:1 from texel space to pixel space.
- // this gives us a smooth step across approximately one fragment
- #ifdef SK_VULKAN
- fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
- "*half(dFdx(%s.x)));", st.fsIn());
- #else
- // We use the y gradient because there is a bug in the Mali 400 in the x direction.
- fragBuilder->codeAppendf("afwidth = abs(" SK_DistanceFieldAAFactor
- "*half(dFdy(%s.y)));", st.fsIn());
- #endif
- } else if (isSimilarity) {
- // For similarity transform, we adjust the effect of the transformation on the distance
- // by using the length of the gradient of the texture coordinates. We use st coordinates
- // to ensure we're mapping 1:1 from texel space to pixel space.
- // this gives us a smooth step across approximately one fragment
- #ifdef SK_VULKAN
- fragBuilder->codeAppendf("half st_grad_len = half(length(dFdx(%s)));", st.fsIn());
- #else
- // We use the y gradient because there is a bug in the Mali 400 in the x direction.
- fragBuilder->codeAppendf("half st_grad_len = half(length(dFdy(%s)));", st.fsIn());
- #endif
- fragBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*st_grad_len);");
- } else {
- // For general transforms, to determine the amount of correction we multiply a unit
- // vector pointing along the SDF gradient direction by the Jacobian of the st coords
- // (which is the inverse transform for this fragment) and take the length of the result.
- fragBuilder->codeAppend("half2 dist_grad = half2(dFdx(distance), "
- "dFdy(distance));");
- // the length of the gradient may be 0, so we need to check for this
- // this also compensates for the Adreno, which likes to drop tiles on division by 0
- fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
- fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
- fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
- fragBuilder->codeAppend("} else {");
- fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
- fragBuilder->codeAppend("}");
- fragBuilder->codeAppendf("half2 Jdx = half2(dFdx(%s));", st.fsIn());
- fragBuilder->codeAppendf("half2 Jdy = half2(dFdy(%s));", st.fsIn());
- fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
- fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
- // this gives us a smooth step across approximately one fragment
- fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
- }
- // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
- // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
- // mapped linearly to coverage, so use a linear step:
- if (isGammaCorrect) {
- fragBuilder->codeAppend(
- "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
- } else {
- fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);");
- }
- fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage);
- }
- void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
- FPCoordTransformIter&& transformIter) override {
- const GrDistanceFieldPathGeoProc& dfpgp = proc.cast<GrDistanceFieldPathGeoProc>();
- if (dfpgp.matrix().hasPerspective() && !fMatrix.cheapEqualTo(dfpgp.matrix())) {
- fMatrix = dfpgp.matrix();
- float matrix[3 * 3];
- GrGLSLGetMatrix<3>(matrix, fMatrix);
- pdman.setMatrix3f(fMatrixUniform, matrix);
- }
- const SkISize& atlasSize = dfpgp.atlasSize();
- SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
- if (fAtlasSize != atlasSize) {
- pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
- fAtlasSize = atlasSize;
- }
- if (dfpgp.matrix().hasPerspective()) {
- this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
- } else {
- this->setTransformDataHelper(dfpgp.matrix(), pdman, &transformIter);
- }
- }
- static inline void GenKey(const GrGeometryProcessor& gp,
- const GrShaderCaps&,
- GrProcessorKeyBuilder* b) {
- const GrDistanceFieldPathGeoProc& dfTexEffect = gp.cast<GrDistanceFieldPathGeoProc>();
- uint32_t key = dfTexEffect.getFlags();
- key |= ComputePosKey(dfTexEffect.matrix()) << 16;
- b->add32(key);
- b->add32(dfTexEffect.matrix().hasPerspective());
- b->add32(dfTexEffect.numTextureSamplers());
- }
- private:
- SkMatrix fMatrix; // view matrix if perspective, local matrix otherwise
- UniformHandle fMatrixUniform;
- SkISize fAtlasSize;
- UniformHandle fAtlasSizeInvUniform;
- typedef GrGLSLGeometryProcessor INHERITED;
- };
- ///////////////////////////////////////////////////////////////////////////////
- GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(const GrShaderCaps& caps,
- const SkMatrix& matrix,
- bool wideColor,
- const sk_sp<GrTextureProxy>* proxies,
- int numProxies,
- const GrSamplerState& params,
- uint32_t flags)
- : INHERITED(kGrDistanceFieldPathGeoProc_ClassID)
- , fMatrix(matrix)
- , fFlags(flags & kNonLCD_DistanceFieldEffectMask) {
- SkASSERT(numProxies <= kMaxTextures);
- SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask));
- fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
- fInColor = MakeColorAttribute("inColor", wideColor);
- fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
- caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
- this->setVertexAttributes(&fInPosition, 3);
- if (numProxies) {
- fAtlasSize = proxies[0]->isize();
- }
- for (int i = 0; i < numProxies; ++i) {
- SkASSERT(proxies[i]);
- SkASSERT(proxies[i]->isize() == fAtlasSize);
- fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params,
- proxies[i]->textureSwizzle());
- }
- this->setTextureSamplerCnt(numProxies);
- }
- void GrDistanceFieldPathGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
- int numProxies,
- const GrSamplerState& params) {
- SkASSERT(numProxies <= kMaxTextures);
- if (!fTextureSamplers[0].isInitialized()) {
- fAtlasSize = proxies[0]->isize();
- }
- for (int i = 0; i < numProxies; ++i) {
- SkASSERT(proxies[i]);
- SkASSERT(proxies[i]->isize() == fAtlasSize);
- if (!fTextureSamplers[i].isInitialized()) {
- fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params,
- proxies[i]->textureSwizzle());
- }
- }
- this->setTextureSamplerCnt(numProxies);
- }
- void GrDistanceFieldPathGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
- GrProcessorKeyBuilder* b) const {
- GrGLDistanceFieldPathGeoProc::GenKey(*this, caps, b);
- }
- GrGLSLPrimitiveProcessor*
- GrDistanceFieldPathGeoProc::createGLSLInstance(const GrShaderCaps&) const {
- return new GrGLDistanceFieldPathGeoProc();
- }
- ///////////////////////////////////////////////////////////////////////////////
- GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldPathGeoProc);
- #if GR_TEST_UTILS
- sk_sp<GrGeometryProcessor> GrDistanceFieldPathGeoProc::TestCreate(GrProcessorTestData* d) {
- int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
- : GrProcessorUnitTest::kAlphaTextureIdx;
- sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
- d->textureProxy(texIdx),
- nullptr,
- nullptr,
- nullptr
- };
- GrSamplerState::WrapMode wrapModes[2];
- GrTest::TestWrapModes(d->fRandom, wrapModes);
- GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
- ? GrSamplerState::Filter::kBilerp
- : GrSamplerState::Filter::kNearest);
- uint32_t flags = 0;
- flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
- if (flags & kSimilarity_DistanceFieldEffectFlag) {
- flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
- }
- return GrDistanceFieldPathGeoProc::Make(*d->caps()->shaderCaps(),
- GrTest::TestMatrix(d->fRandom),
- d->fRandom->nextBool(),
- proxies, 1,
- samplerState,
- flags);
- }
- #endif
- ///////////////////////////////////////////////////////////////////////////////
- class GrGLDistanceFieldLCDTextGeoProc : public GrGLSLGeometryProcessor {
- public:
- GrGLDistanceFieldLCDTextGeoProc() : fAtlasSize({0, 0}) {
- fDistanceAdjust = GrDistanceFieldLCDTextGeoProc::DistanceAdjust::Make(1.0f, 1.0f, 1.0f);
- }
- void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{
- const GrDistanceFieldLCDTextGeoProc& dfTexEffect =
- args.fGP.cast<GrDistanceFieldLCDTextGeoProc>();
- GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
- GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
- GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
- // emit attributes
- varyingHandler->emitAttributes(dfTexEffect);
- const char* atlasSizeInvName;
- fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
- kFloat2_GrSLType,
- "AtlasSizeInv",
- &atlasSizeInvName);
- GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
- // setup pass through color
- varyingHandler->addPassThroughAttribute(dfTexEffect.inColor(), args.fOutputColor);
- // Setup position
- gpArgs->fPositionVar = dfTexEffect.inPosition().asShaderVar();
- // emit transforms
- this->emitTransforms(vertBuilder,
- varyingHandler,
- uniformHandler,
- dfTexEffect.inPosition().asShaderVar(),
- dfTexEffect.localMatrix(),
- args.fFPCoordTransformHandler);
- // set up varyings
- GrGLSLVarying uv(kFloat2_GrSLType);
- GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType;
- GrGLSLVarying texIdx(texIdxType);
- GrGLSLVarying st(kFloat2_GrSLType);
- append_index_uv_varyings(args, dfTexEffect.inTextureCoords().name(), atlasSizeInvName, &uv,
- &texIdx, &st);
- GrGLSLVarying delta(kFloat_GrSLType);
- varyingHandler->addVarying("Delta", &delta);
- if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) {
- vertBuilder->codeAppendf("%s = -%s.x/3.0;", delta.vsOut(), atlasSizeInvName);
- } else {
- vertBuilder->codeAppendf("%s = %s.x/3.0;", delta.vsOut(), atlasSizeInvName);
- }
- // add frag shader code
- bool isUniformScale = (dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask) ==
- kUniformScale_DistanceFieldEffectMask;
- bool isSimilarity = SkToBool(dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag);
- bool isGammaCorrect =
- SkToBool(dfTexEffect.getFlags() & kGammaCorrect_DistanceFieldEffectFlag);
- // create LCD offset adjusted by inverse of transform
- // Use highp to work around aliasing issues
- fragBuilder->codeAppendf("float2 uv = %s;\n", uv.fsIn());
- if (isUniformScale) {
- #ifdef SK_VULKAN
- fragBuilder->codeAppendf("half st_grad_len = half(abs(dFdx(%s.x)));", st.fsIn());
- #else
- // We use the y gradient because there is a bug in the Mali 400 in the x direction.
- fragBuilder->codeAppendf("half st_grad_len = half(abs(dFdy(%s.y)));", st.fsIn());
- #endif
- fragBuilder->codeAppendf("half2 offset = half2(half(st_grad_len*%s), 0.0);",
- delta.fsIn());
- } else if (isSimilarity) {
- // For a similarity matrix with rotation, the gradient will not be aligned
- // with the texel coordinate axes, so we need to calculate it.
- #ifdef SK_VULKAN
- fragBuilder->codeAppendf("half2 st_grad = half2(dFdx(%s));", st.fsIn());
- fragBuilder->codeAppendf("half2 offset = half(%s)*st_grad;", delta.fsIn());
- #else
- // We use dFdy because of a Mali 400 bug, and rotate -90 degrees to
- // get the gradient in the x direction.
- fragBuilder->codeAppendf("half2 st_grad = half2(dFdy(%s));", st.fsIn());
- fragBuilder->codeAppendf("half2 offset = half2(%s*float2(st_grad.y, -st_grad.x));",
- delta.fsIn());
- #endif
- fragBuilder->codeAppend("half st_grad_len = length(st_grad);");
- } else {
- fragBuilder->codeAppendf("half2 st = half2(%s);\n", st.fsIn());
- fragBuilder->codeAppend("half2 Jdx = half2(dFdx(st));");
- fragBuilder->codeAppend("half2 Jdy = half2(dFdy(st));");
- fragBuilder->codeAppendf("half2 offset = half2(half(%s))*Jdx;", delta.fsIn());
- }
- // sample the texture by index
- fragBuilder->codeAppend("half4 texColor;");
- append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
- texIdx, "uv", "texColor");
- // green is distance to uv center
- fragBuilder->codeAppend("half3 distance;");
- fragBuilder->codeAppend("distance.y = texColor.r;");
- // red is distance to left offset
- fragBuilder->codeAppend("half2 uv_adjusted = half2(uv) - offset;");
- append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
- texIdx, "uv_adjusted", "texColor");
- fragBuilder->codeAppend("distance.x = texColor.r;");
- // blue is distance to right offset
- fragBuilder->codeAppend("uv_adjusted = half2(uv) + offset;");
- append_multitexture_lookup(args, dfTexEffect.numTextureSamplers(),
- texIdx, "uv_adjusted", "texColor");
- fragBuilder->codeAppend("distance.z = texColor.r;");
- fragBuilder->codeAppend("distance = "
- "half3(" SK_DistanceFieldMultiplier ")*(distance - half3(" SK_DistanceFieldThreshold"));");
- // adjust width based on gamma
- const char* distanceAdjustUniName = nullptr;
- fDistanceAdjustUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType,
- "DistanceAdjust", &distanceAdjustUniName);
- fragBuilder->codeAppendf("distance -= %s;", distanceAdjustUniName);
- // To be strictly correct, we should compute the anti-aliasing factor separately
- // for each color component. However, this is only important when using perspective
- // transformations, and even then using a single factor seems like a reasonable
- // trade-off between quality and speed.
- fragBuilder->codeAppend("half afwidth;");
- if (isSimilarity) {
- // For similarity transform (uniform scale-only is a subset of this), we adjust for the
- // effect of the transformation on the distance by using the length of the gradient of
- // the texture coordinates. We use st coordinates to ensure we're mapping 1:1 from texel
- // space to pixel space.
- // this gives us a smooth step across approximately one fragment
- fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*st_grad_len;");
- } else {
- // For general transforms, to determine the amount of correction we multiply a unit
- // vector pointing along the SDF gradient direction by the Jacobian of the st coords
- // (which is the inverse transform for this fragment) and take the length of the result.
- fragBuilder->codeAppend("half2 dist_grad = half2(half(dFdx(distance.r)), "
- "half(dFdy(distance.r)));");
- // the length of the gradient may be 0, so we need to check for this
- // this also compensates for the Adreno, which likes to drop tiles on division by 0
- fragBuilder->codeAppend("half dg_len2 = dot(dist_grad, dist_grad);");
- fragBuilder->codeAppend("if (dg_len2 < 0.0001) {");
- fragBuilder->codeAppend("dist_grad = half2(0.7071, 0.7071);");
- fragBuilder->codeAppend("} else {");
- fragBuilder->codeAppend("dist_grad = dist_grad*half(inversesqrt(dg_len2));");
- fragBuilder->codeAppend("}");
- fragBuilder->codeAppend("half2 grad = half2(dist_grad.x*Jdx.x + dist_grad.y*Jdy.x,");
- fragBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_grad.y*Jdy.y);");
- // this gives us a smooth step across approximately one fragment
- fragBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);");
- }
- // The smoothstep falloff compensates for the non-linear sRGB response curve. If we are
- // doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want distance
- // mapped linearly to coverage, so use a linear step:
- if (isGammaCorrect) {
- fragBuilder->codeAppendf("%s = "
- "half4(saturate((distance + half3(afwidth)) / half3(2.0 * afwidth)), 1.0);",
- args.fOutputCoverage);
- } else {
- fragBuilder->codeAppendf(
- "%s = half4(smoothstep(half3(-afwidth), half3(afwidth), distance), 1.0);",
- args.fOutputCoverage);
- }
- }
- void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& processor,
- FPCoordTransformIter&& transformIter) override {
- SkASSERT(fDistanceAdjustUni.isValid());
- const GrDistanceFieldLCDTextGeoProc& dflcd = processor.cast<GrDistanceFieldLCDTextGeoProc>();
- GrDistanceFieldLCDTextGeoProc::DistanceAdjust wa = dflcd.getDistanceAdjust();
- if (wa != fDistanceAdjust) {
- pdman.set3f(fDistanceAdjustUni,
- wa.fR,
- wa.fG,
- wa.fB);
- fDistanceAdjust = wa;
- }
- const SkISize& atlasSize = dflcd.atlasSize();
- SkASSERT(SkIsPow2(atlasSize.fWidth) && SkIsPow2(atlasSize.fHeight));
- if (fAtlasSize != atlasSize) {
- pdman.set2f(fAtlasSizeInvUniform, 1.0f / atlasSize.fWidth, 1.0f / atlasSize.fHeight);
- fAtlasSize = atlasSize;
- }
- this->setTransformDataHelper(dflcd.localMatrix(), pdman, &transformIter);
- }
- static inline void GenKey(const GrGeometryProcessor& gp,
- const GrShaderCaps&,
- GrProcessorKeyBuilder* b) {
- const GrDistanceFieldLCDTextGeoProc& dfTexEffect = gp.cast<GrDistanceFieldLCDTextGeoProc>();
- uint32_t key = dfTexEffect.getFlags();
- b->add32(key);
- b->add32(dfTexEffect.numTextureSamplers());
- }
- private:
- GrDistanceFieldLCDTextGeoProc::DistanceAdjust fDistanceAdjust;
- UniformHandle fDistanceAdjustUni;
- SkISize fAtlasSize;
- UniformHandle fAtlasSizeInvUniform;
- typedef GrGLSLGeometryProcessor INHERITED;
- };
- ///////////////////////////////////////////////////////////////////////////////
- GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(const GrShaderCaps& caps,
- const sk_sp<GrTextureProxy>* proxies,
- int numProxies,
- const GrSamplerState& params,
- DistanceAdjust distanceAdjust,
- uint32_t flags,
- const SkMatrix& localMatrix)
- : INHERITED(kGrDistanceFieldLCDTextGeoProc_ClassID)
- , fLocalMatrix(localMatrix)
- , fDistanceAdjust(distanceAdjust)
- , fFlags(flags & kLCD_DistanceFieldEffectMask) {
- SkASSERT(numProxies <= kMaxTextures);
- SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag));
- if (fFlags & kPerspective_DistanceFieldEffectFlag) {
- fInPosition = {"inPosition", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
- } else {
- fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
- }
- fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
- fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
- caps.integerSupport() ? kUShort2_GrSLType : kFloat2_GrSLType};
- this->setVertexAttributes(&fInPosition, 3);
- if (numProxies) {
- fAtlasSize = proxies[0]->isize();
- }
- for (int i = 0; i < numProxies; ++i) {
- SkASSERT(proxies[i]);
- SkASSERT(proxies[i]->isize() == fAtlasSize);
- fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params,
- proxies[i]->textureSwizzle());
- }
- this->setTextureSamplerCnt(numProxies);
- }
- void GrDistanceFieldLCDTextGeoProc::addNewProxies(const sk_sp<GrTextureProxy>* proxies,
- int numProxies,
- const GrSamplerState& params) {
- SkASSERT(numProxies <= kMaxTextures);
- if (!fTextureSamplers[0].isInitialized()) {
- fAtlasSize = proxies[0]->isize();
- }
- for (int i = 0; i < numProxies; ++i) {
- SkASSERT(proxies[i]);
- SkASSERT(proxies[i]->isize() == fAtlasSize);
- if (!fTextureSamplers[i].isInitialized()) {
- fTextureSamplers[i].reset(proxies[i]->textureType(), proxies[i]->config(), params,
- proxies[i]->textureSwizzle());
- }
- }
- this->setTextureSamplerCnt(numProxies);
- }
- void GrDistanceFieldLCDTextGeoProc::getGLSLProcessorKey(const GrShaderCaps& caps,
- GrProcessorKeyBuilder* b) const {
- GrGLDistanceFieldLCDTextGeoProc::GenKey(*this, caps, b);
- }
- GrGLSLPrimitiveProcessor* GrDistanceFieldLCDTextGeoProc::createGLSLInstance(const GrShaderCaps&) const {
- return new GrGLDistanceFieldLCDTextGeoProc();
- }
- ///////////////////////////////////////////////////////////////////////////////
- GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextGeoProc);
- #if GR_TEST_UTILS
- sk_sp<GrGeometryProcessor> GrDistanceFieldLCDTextGeoProc::TestCreate(GrProcessorTestData* d) {
- int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
- GrProcessorUnitTest::kAlphaTextureIdx;
- sk_sp<GrTextureProxy> proxies[kMaxTextures] = {
- d->textureProxy(texIdx),
- nullptr,
- nullptr,
- nullptr
- };
- GrSamplerState::WrapMode wrapModes[2];
- GrTest::TestWrapModes(d->fRandom, wrapModes);
- GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
- ? GrSamplerState::Filter::kBilerp
- : GrSamplerState::Filter::kNearest);
- DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
- uint32_t flags = kUseLCD_DistanceFieldEffectFlag;
- flags |= d->fRandom->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0;
- if (flags & kSimilarity_DistanceFieldEffectFlag) {
- flags |= d->fRandom->nextBool() ? kScaleOnly_DistanceFieldEffectFlag : 0;
- }
- flags |= d->fRandom->nextBool() ? kBGR_DistanceFieldEffectFlag : 0;
- SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
- return GrDistanceFieldLCDTextGeoProc::Make(*d->caps()->shaderCaps(), proxies, 1, samplerState,
- wa, flags, localMatrix);
- }
- #endif
|