diff --git a/src/gpu/graphite/Device.cpp b/src/gpu/graphite/Device.cpp index 2a15400ef5..bfb8266fdb 100644 --- a/src/gpu/graphite/Device.cpp +++ b/src/gpu/graphite/Device.cpp @@ -1833,6 +1833,7 @@ void Device::drawGeometry(const Transform& localToDevice, localToDevice.matrix(), clip.drawBounds().asSkRect(), fDC->colorInfo(), + this->ditherOrigin(), keyGenFlags, paint.color()}; auto keyResult = shading.toKey(keyContext); diff --git a/src/gpu/graphite/Device.h b/src/gpu/graphite/Device.h index 68875c1b33..b944dfabe9 100644 --- a/src/gpu/graphite/Device.h +++ b/src/gpu/graphite/Device.h @@ -275,6 +275,12 @@ public: int testingOnly_pendingRenderSteps() const; #endif + // Offset from this device's pixels to the global pixel grid, used to keep dithering in + // phase across layers. Zero when the device is not pixel-aligned to global. + SkIPoint ditherOrigin() const { + return this->isPixelAlignedToGlobal() ? this->getOrigin() : SkIPoint{0, 0}; + } + private: class IntersectionTreeSet; diff --git a/src/gpu/graphite/KeyContext.cpp b/src/gpu/graphite/KeyContext.cpp index fce686a2ef..685de06c98 100644 --- a/src/gpu/graphite/KeyContext.cpp +++ b/src/gpu/graphite/KeyContext.cpp @@ -37,6 +37,7 @@ KeyContext::KeyContext(skgpu::graphite::Recorder* recorder, const SkM44& local2Dev, const SkRect& clipDrawBounds, const SkColorInfo& dstColorInfo, + SkIPoint ditherOrigin, SkEnumBitMask initialFlags, const SkColor4f& paintColor) : fCaps(recorder->priv().caps()) @@ -48,6 +49,7 @@ KeyContext::KeyContext(skgpu::graphite::Recorder* recorder, , fRTEffectDict(recorder->priv().runtimeEffectDictionary()) , fLocal2Dev(local2Dev) , fClipDrawBounds(clipDrawBounds) + , fDitherOrigin(ditherOrigin) , fLocalMatrix(nullptr) , fDstColorInfo(dstColorInfo) , fKeyGenFlags(initialFlags) { @@ -66,6 +68,7 @@ KeyContext::KeyContext(const KeyContext& other, , fRTEffectDict(other.fRTEffectDict) , fLocal2Dev(other.fLocal2Dev) , fClipDrawBounds(other.fClipDrawBounds) + , fDitherOrigin(other.fDitherOrigin) , fLocalMatrix(other.fLocalMatrix) , fDstColorInfo(other.fDstColorInfo) , fPaintColor(other.fPaintColor) diff --git a/src/gpu/graphite/KeyContext.h b/src/gpu/graphite/KeyContext.h index 3c5c1ef126..1b845ff09a 100644 --- a/src/gpu/graphite/KeyContext.h +++ b/src/gpu/graphite/KeyContext.h @@ -11,6 +11,7 @@ #include "include/core/SkImageInfo.h" #include "include/core/SkM44.h" #include "include/core/SkMatrix.h" +#include "include/core/SkPoint.h" #include "include/core/SkRect.h" #include "include/private/SkEnumBitMask.h" #include "include/private/SkOnlyStackAllocatable.h" @@ -76,6 +77,7 @@ public: const SkM44& local2Dev, const SkRect& clipDrawBounds, const SkColorInfo& dstColorInfo, + SkIPoint ditherOrigin, SkEnumBitMask initialFlags, const SkColor4f& paintColor); @@ -131,6 +133,9 @@ public: const SkRect& clipDrawBounds() const { return fClipDrawBounds; } + // Offset from this device's pixels to the global pixel grid that dithering is anchored to. + SkIPoint ditherOrigin() const { return fDitherOrigin; } + private: // Fields which will not change over the course of building a paint key const Caps* fCaps; @@ -142,6 +147,7 @@ private: sk_sp fRTEffectDict; SkM44 fLocal2Dev; SkRect fClipDrawBounds; + SkIPoint fDitherOrigin = {0, 0}; protected: // Fields that can be modified while walking a paint's effects for a key diff --git a/src/gpu/graphite/KeyHelpers.cpp b/src/gpu/graphite/KeyHelpers.cpp index 650b0ae2b5..d85ebf38f7 100644 --- a/src/gpu/graphite/KeyHelpers.cpp +++ b/src/gpu/graphite/KeyHelpers.cpp @@ -915,7 +915,9 @@ void add_dither_uniform_data(const KeyContext& keyContext, const DitherShaderBlock::DitherData& ditherData) { BEGIN_WRITE_UNIFORMS(keyContext, BuiltInCodeSnippetID::kDitherShader) - keyContext.pipelineDataGatherer()->writeHalf(ditherData.fRange); + auto gatherer = keyContext.pipelineDataGatherer(); + gatherer->writeHalf(ditherData.fRange); + gatherer->write(SkPoint::Make(ditherData.fOrigin.x(), ditherData.fOrigin.y())); } } // anonymous namespace @@ -2805,7 +2807,9 @@ void AddDitherBlock(const KeyContext& keyContext, SkColorType ct) { return; } - DitherShaderBlock::DitherData data(skgpu::DitherRangeForConfig(ct), std::move(proxy)); + DitherShaderBlock::DitherData data(skgpu::DitherRangeForConfig(ct), + keyContext.ditherOrigin(), + std::move(proxy)); DitherShaderBlock::AddBlock(keyContext, data); } diff --git a/src/gpu/graphite/KeyHelpers.h b/src/gpu/graphite/KeyHelpers.h index 181fe81034..a1eb11a93c 100644 --- a/src/gpu/graphite/KeyHelpers.h +++ b/src/gpu/graphite/KeyHelpers.h @@ -11,6 +11,7 @@ #include "include/core/SkBitmap.h" #include "include/core/SkBlendMode.h" #include "include/core/SkM44.h" +#include "include/core/SkPoint.h" #include "include/core/SkPoint3.h" #include "include/core/SkSamplingOptions.h" #include "include/core/SkShader.h" @@ -208,11 +209,14 @@ struct CoordClampShaderBlock { struct DitherShaderBlock { struct DitherData { - DitherData(float range, sk_sp proxy) + DitherData(float range, SkIPoint origin, sk_sp proxy) : fRange(range) + , fOrigin(origin) , fLUTProxy(std::move(proxy)) {} float fRange; + // Offset from this device's pixels to the global pixel grid the dither is anchored to. + SkIPoint fOrigin; sk_sp fLUTProxy; }; diff --git a/src/gpu/graphite/PaintParams.cpp b/src/gpu/graphite/PaintParams.cpp index f4411e085b..064f308bf9 100644 --- a/src/gpu/graphite/PaintParams.cpp +++ b/src/gpu/graphite/PaintParams.cpp @@ -679,6 +679,7 @@ UniquePaintParamsID ShadingParams::validateOpacityOptimization(const KeyContext& keyContext.local2Dev(), keyContext.clipDrawBounds(), keyContext.dstColorInfo(), + keyContext.ditherOrigin(), keyContext.flags(), opaqueShading.fPaint.color()}; diff --git a/src/gpu/graphite/ShaderCodeDictionary.cpp b/src/gpu/graphite/ShaderCodeDictionary.cpp index 5d2babbb9d..c695989a41 100644 --- a/src/gpu/graphite/ShaderCodeDictionary.cpp +++ b/src/gpu/graphite/ShaderCodeDictionary.cpp @@ -1541,7 +1541,8 @@ ShaderCodeDictionary::ShaderCodeDictionary( /*name=*/"Dither", /*staticFn=*/"sk_dither", SnippetRequirementFlags::kPriorStageOutput, - /*uniforms=*/{{{ "range", SkSLType::kHalf }}}, + /*uniforms=*/{{{ "range", SkSLType::kHalf }, + { "ditherOrigin", SkSLType::kFloat2 }}}, /*texturesAndSamplers=*/{ { "ditherLUT" } } }; diff --git a/src/sksl/sksl_graphite_frag.sksl b/src/sksl/sksl_graphite_frag.sksl index 72cbfcc834..4ae4ba6829 100644 --- a/src/sksl/sksl_graphite_frag.sksl +++ b/src/sksl/sksl_graphite_frag.sksl @@ -743,10 +743,12 @@ $pure half4 sk_hw_yuv_no_swizzle_image_shader(float2 coords, yuvToRGBMatrix, yuvToRGBXlateAlphaParam.xyz); } -$pure half4 sk_dither(half4 colorIn, half range, sampler2D lut) { +$pure half4 sk_dither(half4 colorIn, half range, float2 ditherOrigin, sampler2D lut) { const float kImgSize = 8; - half value = sample(lut, sk_FragCoord.xy / kImgSize).r - 0.5; // undo the bias in the table + // ditherOrigin anchors the pattern to the global pixel grid rather than to this device, so a + // layer dithers in the same phase as the surface it is composited into. + half value = sample(lut, (sk_FragCoord.xy + ditherOrigin) / kImgSize).r - 0.5; // undo the bias // For each color channel, add the random offset to the channel value and then clamp // between 0 and alpha to keep the color premultiplied. return half4(clamp(colorIn.rgb + value * range, 0.0, colorIn.a), colorIn.a);