From 74b7df089aec44f5d316bfae2899c573d5b8e154 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 13 Apr 2022 00:53:35 +0200 Subject: [PATCH] Add property getter/setter for shader constant buffer --- .../EndToEndTests.cs | 17 +++-- .../Helpers/PixelShaderEffect.cs | 67 +++++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/tests/ComputeSharp.D2D1Interop.Tests/EndToEndTests.cs b/tests/ComputeSharp.D2D1Interop.Tests/EndToEndTests.cs index 5ad23f75..66f3cc9b 100644 --- a/tests/ComputeSharp.D2D1Interop.Tests/EndToEndTests.cs +++ b/tests/ComputeSharp.D2D1Interop.Tests/EndToEndTests.cs @@ -117,15 +117,24 @@ public class EndToEndTests + + + "; - fixed (char* p = xml) + fixed (char* pXml = xml) + fixed (char* pPropertyName = "Buffer") { + D2D1_PROPERTY_BINDING d2D1PropertyBinding; + d2D1PropertyBinding.propertyName = (ushort*)pPropertyName; + d2D1PropertyBinding.getFunction = (delegate* unmanaged)(delegate* unmanaged)&PixelShaderEffect.GetConstantBuffer; + d2D1PropertyBinding.setFunction = (delegate* unmanaged)(delegate* unmanaged)&PixelShaderEffect.SetConstantBuffer; + d2D1Factory2.Get()->RegisterEffectFromString( classId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(typeof(InvertShader).GUID)), - propertyXml: (ushort*)p, - bindings: null, - bindingsCount: 0, + propertyXml: (ushort*)pXml, + bindings: &d2D1PropertyBinding, + bindingsCount: 1, effectFactory: (delegate* unmanaged)(delegate* unmanaged)&PixelShaderEffect.Factory).Assert(); } diff --git a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.cs b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.cs index f31bcc75..8f29d0a7 100644 --- a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.cs +++ b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.cs @@ -50,6 +50,16 @@ internal unsafe partial struct PixelShaderEffect /// private PixelShaderTransform* pixelShaderTransform; + /// + /// The constant buffer data, if set. + /// + private byte* constantBuffer; + + /// + /// The size of , if set. + /// + private int constantBufferSize; + /// /// Creates and initializes a new instance. /// @@ -61,6 +71,8 @@ internal unsafe partial struct PixelShaderEffect @this->lpVtbl = Vtbl; @this->referenceCount = 1; @this->pixelShaderTransform = null; + @this->constantBuffer = null; + @this->constantBufferSize = 0; return @this; } @@ -83,6 +95,49 @@ internal unsafe partial struct PixelShaderEffect return S.S_OK; } + /// + [UnmanagedCallersOnly] + public static int GetConstantBuffer(IUnknown* effect, byte* data, uint dataSize, uint* actualSize) + { + PixelShaderEffect* @this = (PixelShaderEffect*)effect; + + if (@this->constantBufferSize == 0) + { + *actualSize = 0; + } + else + { + int bytesToCopy = Math.Min((int)dataSize, @this->constantBufferSize); + + Buffer.MemoryCopy(@this->constantBuffer, data, dataSize, bytesToCopy); + + *actualSize = (uint)bytesToCopy; + } + + return S.S_OK; + } + + /// + [UnmanagedCallersOnly] + public static int SetConstantBuffer(IUnknown* effect, byte* data, uint dataSize) + { + PixelShaderEffect* @this = (PixelShaderEffect*)effect; + + if (@this->constantBuffer is not null) + { + NativeMemory.Free(@this->constantBuffer); + } + + void* buffer = NativeMemory.Alloc(dataSize); + + Buffer.MemoryCopy(data, buffer, dataSize, dataSize); + + @this->constantBuffer = (byte*)buffer; + @this->constantBufferSize = (int)dataSize; + + return S.S_OK; + } + /// [UnmanagedCallersOnly] private static int QueryInterface(PixelShaderEffect* @this, Guid* riid, void** ppvObject) @@ -124,6 +179,11 @@ internal unsafe partial struct PixelShaderEffect { ((IUnknown*)@this->pixelShaderTransform)->Release(); + if (@this->constantBuffer is not null) + { + NativeMemory.Free(@this->constantBuffer); + } + NativeMemory.Free(@this); } @@ -158,6 +218,13 @@ internal unsafe partial struct PixelShaderEffect [UnmanagedCallersOnly] private static int PrepareForRender(PixelShaderEffect* @this, D2D1_CHANGE_TYPE changeType) { + if (@this->constantBuffer is not null) + { + return @this->pixelShaderTransform->D2D1DrawInfo->SetPixelShaderConstantBuffer( + buffer: @this->constantBuffer, + bufferCount: (uint)@this->constantBufferSize); + } + return S.S_OK; }