void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { const GrBitmapTextGeoProc& btgp = args.fGP.cast<GrBitmapTextGeoProc>(); GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; // emit attributes varyingHandler->emitAttributes(btgp); const char* atlasSizeInvName; fAtlasSizeInvUniform = uniformHandler->addUniform(kVertex_GrShaderFlag, kFloat2_GrSLType, kHigh_GrSLPrecision, "AtlasSizeInv", &atlasSizeInvName); GrGLSLVarying uv(kFloat2_GrSLType); GrSLType texIdxType = args.fShaderCaps->integerSupport() ? kInt_GrSLType : kFloat_GrSLType; GrGLSLVarying texIdx(texIdxType); append_index_uv_varyings(args, btgp.inTextureCoords().name(), atlasSizeInvName, &uv, &texIdx, nullptr); GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; // Setup pass through color if (btgp.hasVertexColor()) { varyingHandler->addPassThroughAttribute(btgp.inColor(), args.fOutputColor); } else { this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, &fColorUniform); } // Setup position gpArgs->fPositionVar = btgp.inPosition().asShaderVar(); // emit transforms this->emitTransforms(vertBuilder, varyingHandler, uniformHandler, btgp.inPosition().asShaderVar(), btgp.localMatrix(), args.fFPCoordTransformHandler); fragBuilder->codeAppend("half4 texColor;"); append_multitexture_lookup(args, btgp.numTextureSamplers(), texIdx, uv.fsIn(), "texColor"); if (btgp.maskFormat() == kARGB_GrMaskFormat) { // modulate by color fragBuilder->codeAppendf("%s = %s * texColor;", args.fOutputColor, args.fOutputColor); fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage); } else { fragBuilder->codeAppendf("%s = texColor;", args.fOutputCoverage); } }
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, kHigh_GrSLPrecision, "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()->fName, 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 = 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 = abs(dFdy(%s.y));", st.fsIn()); #endif fragBuilder->codeAppendf("half2 offset = half2(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 = dFdx(%s);", st.fsIn()); fragBuilder->codeAppendf("half2 offset = %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 = dFdy(%s);", st.fsIn()); fragBuilder->codeAppendf("half2 offset = %s*half2(st_grad.y, -st_grad.x);", delta.fsIn()); #endif fragBuilder->codeAppend("half st_grad_len = length(st_grad);"); } else { fragBuilder->codeAppendf("half2 st = %s;\n", st.fsIn()); fragBuilder->codeAppend("half2 Jdx = dFdx(st);"); fragBuilder->codeAppend("half2 Jdy = dFdy(st);"); fragBuilder->codeAppendf("half2 offset = %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 = 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 = 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(dFdx(distance.r), 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*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(clamp((distance + half3(afwidth)) / half3(2.0 * afwidth), 0.0, 1.0), 1.0);", args.fOutputCoverage); } else { fragBuilder->codeAppendf( "%s = half4(smoothstep(half3(-afwidth), half3(afwidth), distance), 1.0);", args.fOutputCoverage); } }
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, kHigh_GrSLPrecision, "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()->fName, 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()->fName, dfPathEffect.matrix(), &fMatrixUniform); // emit transforms this->emitTransforms(vertBuilder, varyingHandler, uniformHandler, dfPathEffect.inPosition()->asShaderVar(), args.fFPCoordTransformHandler); } else { // Setup position this->writeOutputPosition(vertBuilder, gpArgs, dfPathEffect.inPosition()->fName); // 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 "*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 "*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 = 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 = 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*inversesqrt(dg_len2);"); fragBuilder->codeAppend("}"); fragBuilder->codeAppendf("half2 Jdx = dFdx(%s);", st.fsIn()); fragBuilder->codeAppendf("half2 Jdy = 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 = clamp((distance + afwidth) / (2.0 * afwidth), 0.0, 1.0);"); } else { fragBuilder->codeAppend("half val = smoothstep(-afwidth, afwidth, distance);"); } fragBuilder->codeAppendf("%s = half4(val);", args.fOutputCoverage); }