Add generic support for PixelShaderEffect
このコミットが含まれているのは:
1 親
74b7df089a
コミット
65a03349de
3ファイル変更
+139
-22
検索条件に一致するファイルはありません
@@ -127,15 +127,19 @@ public class EndToEndTests
|
||||
{
|
||||
D2D1_PROPERTY_BINDING d2D1PropertyBinding;
|
||||
d2D1PropertyBinding.propertyName = (ushort*)pPropertyName;
|
||||
d2D1PropertyBinding.getFunction = (delegate* unmanaged<IUnknown*, byte*, uint, uint*, HRESULT>)(delegate* unmanaged<IUnknown*, byte*, uint, uint*, int>)&PixelShaderEffect.GetConstantBuffer;
|
||||
d2D1PropertyBinding.setFunction = (delegate* unmanaged<IUnknown*, byte*, uint, HRESULT>)(delegate* unmanaged<IUnknown*, byte*, uint, int>)&PixelShaderEffect.SetConstantBuffer;
|
||||
d2D1PropertyBinding.getFunction =
|
||||
(delegate* unmanaged<IUnknown*, byte*, uint, uint*, HRESULT>)
|
||||
(delegate* unmanaged<IUnknown*, byte*, uint, uint*, int>)&PixelShaderEffect.GetConstantBuffer;
|
||||
d2D1PropertyBinding.setFunction =
|
||||
(delegate* unmanaged<IUnknown*, byte*, uint, HRESULT>)
|
||||
(delegate* unmanaged<IUnknown*, byte*, uint, int>)&PixelShaderEffect.SetConstantBuffer;
|
||||
|
||||
d2D1Factory2.Get()->RegisterEffectFromString(
|
||||
classId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(typeof(InvertShader).GUID)),
|
||||
propertyXml: (ushort*)pXml,
|
||||
bindings: &d2D1PropertyBinding,
|
||||
bindingsCount: 1,
|
||||
effectFactory: (delegate* unmanaged<IUnknown**, HRESULT>)(delegate* unmanaged<IUnknown**, int>)&PixelShaderEffect.Factory).Assert();
|
||||
effectFactory: PixelShaderEffect.For<InvertShader>.Factory).Assert();
|
||||
}
|
||||
|
||||
using ComPtr<ID2D1Effect> d2D1Effect = default;
|
||||
@@ -145,6 +149,10 @@ public class EndToEndTests
|
||||
effectId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(typeof(InvertShader).GUID)),
|
||||
effect: d2D1Effect.GetAddressOf()).Assert();
|
||||
|
||||
float number = 1;
|
||||
|
||||
d2D1Effect.Get()->SetValue(0, D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BLOB, (byte*)&number, sizeof(float)).Assert();
|
||||
|
||||
d2D1Effect.Get()->SetInput(0, (ID2D1Image*)d2D1BitmapSource.Get());
|
||||
|
||||
d2D1DeviceContext.Get()->BeginDraw();
|
||||
@@ -199,12 +207,14 @@ public class EndToEndTests
|
||||
[D2DInputCount(1)]
|
||||
[D2DInputSimple(0)]
|
||||
[D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader50)]
|
||||
public readonly partial struct InvertShader : ID2D1PixelShader
|
||||
public partial struct InvertShader : ID2D1PixelShader
|
||||
{
|
||||
public float number;
|
||||
|
||||
public float4 Execute()
|
||||
{
|
||||
float4 color = D2D1.GetInput(0);
|
||||
float3 rgb = Hlsl.Saturate(1 - color.RGB);
|
||||
float3 rgb = Hlsl.Saturate(this.number - color.RGB);
|
||||
|
||||
return new(rgb, 1);
|
||||
}
|
||||
|
||||
@@ -60,6 +60,16 @@ partial struct PixelShaderEffect
|
||||
/// </summary>
|
||||
private ID2D1DrawInfo* d2D1DrawInfo;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="Guid"/> for the shader.
|
||||
/// </summary>
|
||||
private Guid shaderId;
|
||||
|
||||
/// <summary>
|
||||
/// The number of inputs for the shader.
|
||||
/// </summary>
|
||||
private int numberOfInputs;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ID2D1DrawInfo"/> instance currently in use.
|
||||
/// </summary>
|
||||
@@ -68,8 +78,10 @@ partial struct PixelShaderEffect
|
||||
/// <summary>
|
||||
/// Creates and initializes a new <see cref="PixelShaderTransform"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="shaderId">The <see cref="Guid"/> for the shader.</param>
|
||||
/// <param name="numberOfInpputs">The number of inputs for the shader.</param>
|
||||
/// <returns>A new <see cref="PixelShaderTransform"/> instance.</returns>
|
||||
public static PixelShaderTransform* Create()
|
||||
public static PixelShaderTransform* Create(Guid shaderId, int numberOfInpputs)
|
||||
{
|
||||
PixelShaderTransform* @this = (PixelShaderTransform*)NativeMemory.Alloc((nuint)sizeof(PixelShaderTransform));
|
||||
|
||||
@@ -77,6 +89,8 @@ partial struct PixelShaderEffect
|
||||
@this->referenceCount = 1;
|
||||
@this->inputRect = default;
|
||||
@this->d2D1DrawInfo = null;
|
||||
@this->shaderId = shaderId;
|
||||
@this->numberOfInputs = numberOfInpputs;
|
||||
|
||||
return @this;
|
||||
}
|
||||
@@ -132,7 +146,7 @@ partial struct PixelShaderEffect
|
||||
[UnmanagedCallersOnly]
|
||||
private static uint GetInputCount(PixelShaderTransform* @this)
|
||||
{
|
||||
return 1;
|
||||
return (uint)@this->numberOfInputs;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ID2D1DrawTransform.MapOutputRectToInputRects"/>
|
||||
@@ -182,7 +196,7 @@ partial struct PixelShaderEffect
|
||||
{
|
||||
@this->d2D1DrawInfo = drawInfo;
|
||||
|
||||
return drawInfo->SetPixelShader((Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(typeof(InvertShader).GUID)));
|
||||
return drawInfo->SetPixelShader(&@this->shaderId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,71 @@ namespace ComputeSharp.D2D1Interop.Tests.Helpers;
|
||||
/// </summary>
|
||||
internal unsafe partial struct PixelShaderEffect
|
||||
{
|
||||
/// <summary>
|
||||
/// A generic pixel shader implementation.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of shader.</typeparam>
|
||||
public static class For<T>
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="Guid"/> for the shader.
|
||||
/// </summary>
|
||||
private static readonly Guid shaderId;
|
||||
|
||||
/// <summary>
|
||||
/// The shader bytecode.
|
||||
/// </summary>
|
||||
private static readonly byte* bytecode;
|
||||
|
||||
/// <summary>
|
||||
/// The size of <see cref="bytecode"/>.
|
||||
/// </summary>
|
||||
private static readonly int bytecodeSize;
|
||||
|
||||
/// <summary>
|
||||
/// The number of inputs for the shader.
|
||||
/// </summary>
|
||||
private static readonly int numberOfInputs;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="FactoryDelegate"/> wrapper for the shader factory.
|
||||
/// </summary>
|
||||
private static readonly FactoryDelegate EffectFactory = CreateEffect;
|
||||
|
||||
/// <summary>
|
||||
/// The static constructor for <see cref="For{T}"/>.
|
||||
/// </summary>
|
||||
static For()
|
||||
{
|
||||
shaderId = typeof(T).GUID;
|
||||
|
||||
ReadOnlyMemory<byte> buffer = D2D1InteropServices.LoadShaderBytecode<T>();
|
||||
|
||||
bytecode = (byte*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For<T>), buffer.Length);
|
||||
|
||||
buffer.Span.CopyTo(new Span<byte>(bytecode, buffer.Length));
|
||||
|
||||
bytecodeSize = buffer.Length;
|
||||
|
||||
numberOfInputs = ((D2DInputCountAttribute[])typeof(T).GetCustomAttributes(typeof(D2DInputCountAttribute), false))[0].Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the factory for the current effect.
|
||||
/// </summary>
|
||||
public static delegate* unmanaged<IUnknown**, HRESULT> Factory
|
||||
{
|
||||
get => (delegate* unmanaged<IUnknown**, HRESULT>)Marshal.GetFunctionPointerForDelegate(EffectFactory);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="FactoryDelegate"/>
|
||||
private static int CreateEffect(IUnknown** effectImpl)
|
||||
{
|
||||
return PixelShaderEffect.Factory(shaderId, bytecode, bytecodeSize, numberOfInputs, effectImpl);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The shared vtable pointer for <see cref="PixelShaderEffect"/> instances.
|
||||
/// </summary>
|
||||
@@ -60,6 +125,26 @@ internal unsafe partial struct PixelShaderEffect
|
||||
/// </summary>
|
||||
private int constantBufferSize;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="Guid"/> for the shader.
|
||||
/// </summary>
|
||||
private Guid shaderId;
|
||||
|
||||
/// <summary>
|
||||
/// The shader bytecode.
|
||||
/// </summary>
|
||||
private byte* bytecode;
|
||||
|
||||
/// <summary>
|
||||
/// The size of <see cref="bytecode"/>.
|
||||
/// </summary>
|
||||
private int bytecodeSize;
|
||||
|
||||
/// <summary>
|
||||
/// The number of inputs for the shader.
|
||||
/// </summary>
|
||||
private int numberOfInputs;
|
||||
|
||||
/// <summary>
|
||||
/// Creates and initializes a new <see cref="PixelShaderEffect"/> instance.
|
||||
/// </summary>
|
||||
@@ -77,18 +162,33 @@ internal unsafe partial struct PixelShaderEffect
|
||||
return @this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A wrapper for an effect factory.
|
||||
/// </summary>
|
||||
/// <param name="effectImpl">The resulting effect factory.</param>
|
||||
/// <returns>The <c>HRESULT</c> for the operation.</returns>
|
||||
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
|
||||
private delegate int FactoryDelegate(IUnknown** effectImpl);
|
||||
|
||||
/// <summary>
|
||||
/// The factory method for <see cref="ID2D1Factory2.RegisterEffectFromString"/>.
|
||||
/// </summary>
|
||||
/// <param name="shaderId">The <see cref="Guid"/> for the shader.</param>
|
||||
/// <param name="bytecode">The shader bytecode.</param>
|
||||
/// <param name="effectImpl">The resulting effect instance.</param>
|
||||
/// <param name="bytecodeSize">The size of <paramref name="bytecode"/>.</param>
|
||||
/// <param name="numberOfInpputs">The number of inputs for the shader.</param>
|
||||
/// <returns>This always returns <c>0</c>.</returns>
|
||||
[UnmanagedCallersOnly]
|
||||
public static int Factory(IUnknown** effectImpl)
|
||||
private static int Factory(Guid shaderId, byte* bytecode, int bytecodeSize, int numberOfInpputs, IUnknown** effectImpl)
|
||||
{
|
||||
PixelShaderEffect* pixelShaderEffect = Create();
|
||||
PixelShaderTransform* pixelShaderTransform = PixelShaderTransform.Create();
|
||||
PixelShaderTransform* pixelShaderTransform = PixelShaderTransform.Create(shaderId, numberOfInpputs);
|
||||
|
||||
pixelShaderEffect->pixelShaderTransform = pixelShaderTransform;
|
||||
pixelShaderEffect->shaderId = shaderId;
|
||||
pixelShaderEffect->bytecode = bytecode;
|
||||
pixelShaderEffect->bytecodeSize = bytecodeSize;
|
||||
pixelShaderEffect->numberOfInputs = numberOfInpputs;
|
||||
|
||||
*effectImpl = (IUnknown*)pixelShaderEffect;
|
||||
|
||||
@@ -194,17 +294,10 @@ internal unsafe partial struct PixelShaderEffect
|
||||
[UnmanagedCallersOnly]
|
||||
private static int Initialize(PixelShaderEffect* @this, ID2D1EffectContext* effectContext, ID2D1TransformGraph* transformGraph)
|
||||
{
|
||||
ReadOnlyMemory<byte> bytecode = D2D1InteropServices.LoadShaderBytecode<InvertShader>();
|
||||
|
||||
int hresult;
|
||||
|
||||
fixed (byte* bytecodePtr = bytecode.Span)
|
||||
{
|
||||
hresult = effectContext->LoadPixelShader(
|
||||
shaderId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(typeof(InvertShader).GUID)),
|
||||
shaderBuffer: bytecodePtr,
|
||||
shaderBufferCount: (uint)bytecode.Length);
|
||||
}
|
||||
int hresult = effectContext->LoadPixelShader(
|
||||
shaderId: &@this->shaderId,
|
||||
shaderBuffer: @this->bytecode,
|
||||
shaderBufferCount: (uint)@this->bytecodeSize);
|
||||
|
||||
if (Windows.SUCCEEDED(hresult))
|
||||
{
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする