From 3ebd92f276f7b119440ae8eaeea82468e9b90629 Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 25 Oct 2026 11:57:36 +0900 Subject: [PATCH] =?UTF-8?q?=E7=B8=81=E5=8F=96=E3=82=8A=E3=81=AE=E7=AF=84?= =?UTF-8?q?=E5=9B=B2=E3=82=92=E6=B1=BA=E3=82=81=E3=82=8B=E3=82=AB=E3=82=B9?= =?UTF-8?q?=E3=82=BF=E3=83=A0=E3=82=A8=E3=83=95=E3=82=A7=E3=82=AF=E3=83=88?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OutlineMaskCustomEffect.cs | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 PartialInnerOutline/OutlineMaskCustomEffect.cs diff --git a/PartialInnerOutline/OutlineMaskCustomEffect.cs b/PartialInnerOutline/OutlineMaskCustomEffect.cs new file mode 100644 index 0000000..c9dfb32 --- /dev/null +++ b/PartialInnerOutline/OutlineMaskCustomEffect.cs @@ -0,0 +1,124 @@ +using System.Numerics; +using System.Runtime.InteropServices; +using Vortice; +using Vortice.Direct2D1; +using YukkuriMovieMaker.Commons; +using YukkuriMovieMaker.Player.Video; + +namespace PartialInnerOutline; + +internal sealed class OutlineMaskCustomEffect(IGraphicsDevicesAndContext devices) + : D2D1CustomShaderEffectBase(Create(devices)) +{ + internal enum PropertyIndex + { + FeatherLow = 0, + FeatherInverseWidth, + Opacity, + BandOffset, + BandDirectionX, + BandDirectionY, + BandEdgeNear, + BandEdgeFar, + OutputInflation, + } + + public float FeatherLow { set => SetValue((int)PropertyIndex.FeatherLow, value); } + public float FeatherInverseWidth { set => SetValue((int)PropertyIndex.FeatherInverseWidth, value); } + public float Opacity { set => SetValue((int)PropertyIndex.Opacity, value); } + public float BandOffset { set => SetValue((int)PropertyIndex.BandOffset, value); } + public float BandDirectionX { set => SetValue((int)PropertyIndex.BandDirectionX, value); } + public float BandDirectionY { set => SetValue((int)PropertyIndex.BandDirectionY, value); } + public float BandEdgeNear { set => SetValue((int)PropertyIndex.BandEdgeNear, value); } + public float BandEdgeFar { set => SetValue((int)PropertyIndex.BandEdgeFar, value); } + public int OutputInflation { set => SetValue((int)PropertyIndex.OutputInflation, value); } + + [CustomEffect(1)] + internal sealed class EffectImpl : D2D1CustomShaderEffectImplBase + { + private ConstantBuffer _cb; + private int _outputInflation; + + [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.FeatherLow)] + public float FeatherLow { get => _cb.FeatherLow; set { _cb.FeatherLow = Sanitize(value, 0f, 1f); UpdateConstants(); } } + + [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.FeatherInverseWidth)] + public float FeatherInverseWidth { get => _cb.FeatherInverseWidth; set { _cb.FeatherInverseWidth = Sanitize(value, 0f, float.MaxValue); UpdateConstants(); } } + + [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.Opacity)] + public float Opacity { get => _cb.Opacity; set { _cb.Opacity = Sanitize(value, 0f, 1f); UpdateConstants(); } } + + [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.BandOffset)] + public float BandOffset { get => _cb.BandOffset; set { _cb.BandOffset = Sanitize(value, float.MinValue, float.MaxValue); UpdateConstants(); } } + + [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.BandDirectionX)] + public float BandDirectionX { get => _cb.BandDirectionX; set { _cb.BandDirectionX = Sanitize(value, -1f, 1f); UpdateConstants(); } } + + [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.BandDirectionY)] + public float BandDirectionY { get => _cb.BandDirectionY; set { _cb.BandDirectionY = Sanitize(value, -1f, 1f); UpdateConstants(); } } + + [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.BandEdgeNear)] + public float BandEdgeNear { get => _cb.BandEdgeNear; set { _cb.BandEdgeNear = Sanitize(value, 0f, float.MaxValue); UpdateConstants(); } } + + [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.BandEdgeFar)] + public float BandEdgeFar { get => _cb.BandEdgeFar; set { _cb.BandEdgeFar = Sanitize(value, 0f, float.MaxValue); UpdateConstants(); } } + + [CustomEffectProperty(PropertyType.Int32, (int)PropertyIndex.OutputInflation)] + public int OutputInflation { get => _outputInflation; set => _outputInflation = Math.Clamp(value, 0, PartialInnerOutlineSettings.MaximumBlurPadding); } + + public EffectImpl() : base(ShaderResourceUri.Get("OutlineMask")) + { + _cb.FeatherLow = (float)PartialInnerOutlineSettings.CoverageThreshold; + _cb.Opacity = 1f; + } + + private static float Sanitize(float value, float minimum, float maximum) + => float.IsNaN(value) ? 0f : Math.Clamp(value, minimum, maximum); + + protected override void UpdateConstants() + { + if (drawInformation is null) + return; + + try + { + drawInformation.SetPixelShaderConstantBuffer(_cb); + } + catch (Exception exception) + { + PartialInnerOutlineTelemetry.Report(exception); + throw; + } + } + + public override void MapInputRectsToOutputRect( + RawRect[] inputRects, + RawRect[] inputOpaqueSubRects, + out RawRect outputRect, + out RawRect outputOpaqueSubRect) + { + inputRect = ClampInputRect(inputRects[0]); + outputRect = inputRect.Inflate(_outputInflation); + outputOpaqueSubRect = default; + _cb.SourceBounds = new Vector4(inputRect.Left, inputRect.Top, inputRect.Right, inputRect.Bottom); + UpdateConstants(); + } + + public override void MapOutputRectToInputRects(RawRect outputRect, RawRect[] inputRects) + => inputRects[0] = outputRect; + + [StructLayout(LayoutKind.Sequential)] + private struct ConstantBuffer + { + public float FeatherLow; + public float FeatherInverseWidth; + public float Opacity; + public float BandOffset; + public float BandDirectionX; + public float BandDirectionY; + public float BandEdgeNear; + public float BandEdgeFar; + public Vector4 SourceBounds; + } + } +}