PSFビットマップ生成とD2D1畳み込みカスタムシェーダーを追加
このコミットが含まれているのは:
4ファイル変更
+239
検索条件に一致するファイルはありません
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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<EffectImpl>(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<EffectImpl>
|
||||
{
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする