From e8ad808f5a9da85cbe6a0600318dcd9ce2a1788d Mon Sep 17 00:00:00 2001 From: routersys Date: Tue, 23 Jun 2026 08:40:48 +0900 Subject: [PATCH] =?UTF-8?q?PSF=E3=83=93=E3=83=83=E3=83=88=E3=83=9E?= =?UTF-8?q?=E3=83=83=E3=83=97=E7=94=9F=E6=88=90=E3=81=A8D2D1=E7=95=B3?= =?UTF-8?q?=E3=81=BF=E8=BE=BC=E3=81=BF=E3=82=AB=E3=82=B9=E3=82=BF=E3=83=A0?= =?UTF-8?q?=E3=82=B7=E3=82=A7=E3=83=BC=E3=83=80=E3=83=BC=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Wave Optics/Rendering/PsfBitmapFactory.cs | 45 ++++++ Wave Optics/Rendering/ShaderResourceUri.cs | 6 + .../Rendering/WaveOpticsConvolutionEffect.cs | 133 ++++++++++++++++++ .../Shaders/WaveOpticsConvolution.hlsl | 55 ++++++++ 4 files changed, 239 insertions(+) create mode 100644 Wave Optics/Rendering/PsfBitmapFactory.cs create mode 100644 Wave Optics/Rendering/ShaderResourceUri.cs create mode 100644 Wave Optics/Rendering/WaveOpticsConvolutionEffect.cs create mode 100644 Wave Optics/Shaders/WaveOpticsConvolution.hlsl diff --git a/Wave Optics/Rendering/PsfBitmapFactory.cs b/Wave Optics/Rendering/PsfBitmapFactory.cs new file mode 100644 index 0000000..200ee25 --- /dev/null +++ b/Wave Optics/Rendering/PsfBitmapFactory.cs @@ -0,0 +1,45 @@ +using System.Runtime.InteropServices; +using Vortice.DCommon; +using Vortice.Direct2D1; +using Vortice.DXGI; +using Vortice.Mathematics; +using WaveOptics.Abstractions; + +namespace WaveOptics.Rendering; + +internal static class PsfBitmapFactory +{ + public static ID2D1Bitmap Create(ID2D1DeviceContext deviceContext, PsfKernel kernel) + { + var values = kernel.Values.Span; + var pixels = new float[values.Length * 4]; + for (var index = 0; index < values.Length; index++) + { + var value = (float)values[index]; + var offset = index * 4; + pixels[offset] = value; + pixels[offset + 1] = value; + pixels[offset + 2] = value; + pixels[offset + 3] = 1; + } + + var handle = GCHandle.Alloc(pixels, GCHandleType.Pinned); + try + { + var properties = new BitmapProperties1( + new PixelFormat(Format.R32G32B32A32_Float, Vortice.DCommon.AlphaMode.Ignore), + 96, + 96, + BitmapOptions.None); + return ((ID2D1DeviceContext1)deviceContext).CreateBitmap( + new SizeI(kernel.Size, kernel.Size), + handle.AddrOfPinnedObject(), + kernel.Size * 4 * sizeof(float), + properties); + } + finally + { + handle.Free(); + } + } +} diff --git a/Wave Optics/Rendering/ShaderResourceUri.cs b/Wave Optics/Rendering/ShaderResourceUri.cs new file mode 100644 index 0000000..4a5b154 --- /dev/null +++ b/Wave Optics/Rendering/ShaderResourceUri.cs @@ -0,0 +1,6 @@ +namespace WaveOptics.Rendering; + +internal static class ShaderResourceUri +{ + public static Uri Get(string shaderName) => new($"pack://application:,,,/WaveOptics;component/Shaders/{shaderName}.cso", UriKind.Absolute); +} diff --git a/Wave Optics/Rendering/WaveOpticsConvolutionEffect.cs b/Wave Optics/Rendering/WaveOpticsConvolutionEffect.cs new file mode 100644 index 0000000..37516ad --- /dev/null +++ b/Wave Optics/Rendering/WaveOpticsConvolutionEffect.cs @@ -0,0 +1,133 @@ +using System.Numerics; +using System.Runtime.InteropServices; +using Vortice; +using Vortice.Direct2D1; +using YukkuriMovieMaker.Commons; +using YukkuriMovieMaker.Player.Video; + +namespace WaveOptics.Rendering; + +internal sealed class WaveOpticsConvolutionEffect(IGraphicsDevicesAndContext devices) + : D2D1CustomShaderEffectBase(Create(devices)) +{ + public const int MaximumKernelSize = 31; + + public int KernelSize + { + get => GetIntValue((int)EffectImpl.Properties.KernelSize); + set => SetValue((int)EffectImpl.Properties.KernelSize, value); + } + + public float Amount + { + get => GetFloatValue((int)EffectImpl.Properties.Amount); + set => SetValue((int)EffectImpl.Properties.Amount, value); + } + + public float Gain + { + get => GetFloatValue((int)EffectImpl.Properties.Gain); + set => SetValue((int)EffectImpl.Properties.Gain, value); + } + + public void SetSource(ID2D1Image? image) => SetInput(0, image, true); + public void SetKernel(ID2D1Image? image) => SetInput(1, image, true); + + [CustomEffect(2)] + sealed class EffectImpl : D2D1CustomShaderEffectImplBase + { + ConstantBuffer constants; + RawRect kernelRect; + + [CustomEffectProperty(PropertyType.Int32, (int)Properties.KernelSize)] + public int KernelSize + { + get => constants.KernelSize; + set + { + var clamped = Math.Clamp(value, 1, MaximumKernelSize); + constants.KernelSize = (clamped & 1) == 0 ? clamped - 1 : clamped; + UpdateConstants(); + } + } + + [CustomEffectProperty(PropertyType.Float, (int)Properties.Amount)] + public float Amount + { + get => constants.Amount; + set + { + constants.Amount = float.IsFinite(value) ? Math.Clamp(value, 0, 1) : 0; + UpdateConstants(); + } + } + + [CustomEffectProperty(PropertyType.Float, (int)Properties.Gain)] + public float Gain + { + get => constants.Gain; + set + { + constants.Gain = float.IsFinite(value) ? Math.Clamp(value, 0, 4) : 1; + UpdateConstants(); + } + } + + public EffectImpl() : base(ShaderResourceUri.Get("WaveOpticsConvolution")) + { + constants.KernelSize = 1; + constants.Gain = 1; + } + + protected override void UpdateConstants() + { + drawInformation?.SetPixelShaderConstantBuffer(constants); + } + + public override void MapInputRectsToOutputRect(RawRect[] inputRects, RawRect[] inputOpaqueSubRects, out RawRect outputRect, out RawRect outputOpaqueSubRect) + { + inputRect = inputRects[0]; + kernelRect = inputRects[1]; + constants.InputBounds = new Vector4(inputRect.Left, inputRect.Top, inputRect.Right, inputRect.Bottom); + UpdateConstants(); + outputRect = Inflate(inputRect, GetRadius()); + outputOpaqueSubRect = default; + } + + public override void MapOutputRectToInputRects(RawRect outputRect, RawRect[] inputRects) + { + inputRects[0] = Inflate(outputRect, GetRadius()); + inputRects[1] = kernelRect; + } + + int GetRadius() => constants.Amount > 0 ? constants.KernelSize / 2 : 0; + + static RawRect Inflate(RawRect rect, int radius) + { + return new RawRect( + Saturate((long)rect.Left - radius), + Saturate((long)rect.Top - radius), + Saturate((long)rect.Right + radius), + Saturate((long)rect.Bottom + radius)); + } + + static int Saturate(long value) => (int)Math.Clamp(value, int.MinValue, int.MaxValue); + + [StructLayout(LayoutKind.Sequential)] + struct ConstantBuffer + { + public int KernelSize; + public float Amount; + public float Gain; + public float Padding; + public Vector4 InputBounds; + } + + public enum Properties + { + KernelSize, + Amount, + Gain, + } + } +} diff --git a/Wave Optics/Shaders/WaveOpticsConvolution.hlsl b/Wave Optics/Shaders/WaveOpticsConvolution.hlsl new file mode 100644 index 0000000..77f9840 --- /dev/null +++ b/Wave Optics/Shaders/WaveOpticsConvolution.hlsl @@ -0,0 +1,55 @@ +Texture2D InputTexture : register(t0); +Texture2D KernelTexture : register(t1); +SamplerState InputSampler : register(s0); + +static const int MaximumRadius = 15; + +cbuffer Constants : register(b0) +{ + int kernelSize : packoffset(c0.x); + float amount : packoffset(c0.y); + float gain : packoffset(c0.z); + float4 inputBounds : packoffset(c1); +}; + +float4 SampleInput(float2 uv, float2 scenePosition) +{ + if (scenePosition.x < inputBounds.x || scenePosition.y < inputBounds.y || + scenePosition.x >= inputBounds.z || scenePosition.y >= inputBounds.w) + return (float4)0; + return InputTexture.SampleLevel(InputSampler, uv, 0); +} + +float4 main( + float4 position : SV_POSITION, + float4 scenePosition : SCENE_POSITION, + float4 uv0 : TEXCOORD0, + float4 uv1 : TEXCOORD1 +) : SV_TARGET +{ + float4 source = SampleInput(uv0.xy, scenePosition.xy); + if (amount <= 0.0f || kernelSize <= 1) + return source; + + int radius = clamp(kernelSize / 2, 0, MaximumRadius); + float4 convolved = (float4)0; + + [loop] + for (int y = -MaximumRadius; y <= MaximumRadius; y++) + { + if (abs(y) > radius) + continue; + [loop] + for (int x = -MaximumRadius; x <= MaximumRadius; x++) + { + if (abs(x) > radius) + continue; + float weight = KernelTexture.Load(int3(x + radius, y + radius, 0)).r; + float2 offset = float2(x, y); + convolved += SampleInput(uv0.xy + offset * uv0.zw, scenePosition.xy + offset) * weight; + } + } + + convolved *= gain; + return lerp(source, convolved, amount); +}