diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs index 4fe6dbaf..132b0e5d 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #if !NET6_0_OR_GREATER @@ -76,6 +77,7 @@ unsafe partial struct PixelShaderEffect } else { + // TODO: handle case with buffer not large enough int bytesToCopy = Math.Min((int)dataSize, @this->constantBufferSize); Buffer.MemoryCopy(@this->constantBuffer, data, dataSize, bytesToCopy); @@ -117,7 +119,35 @@ unsafe partial struct PixelShaderEffect /// The for the operation. private int GetResourceTextureManagerAtIndex(int index, byte* data, uint dataSize, uint* actualSize) { - // TODO: implement GetResourceTextureIndex + if (!IsResourceTextureManagerIndexValid(index)) + { + return E.E_INVALIDARG; + } + + if (data is null || actualSize is null) + { + return E.E_POINTER; + } + + if (dataSize < sizeof(void*)) + { + return E.E_INVALIDARG; + } + + ID2D1ResourceTextureManager* resourceTextureManager = this.resourceTextureManagerBuffer[index]; + + if (resourceTextureManager is not null) + { + _ = ((IUnknown*)resourceTextureManager)->AddRef(); + + *(void**)data = resourceTextureManager; + } + else + { + *(void**)data = null; + } + + *actualSize = (uint)sizeof(void*); return S.S_OK; } @@ -131,8 +161,72 @@ unsafe partial struct PixelShaderEffect /// The for the operation. private int SetResourceTextureManagerAtIndex(int index, byte* data, uint dataSize) { - // TODO: implement SetResourceTextureIndex + if (!IsResourceTextureManagerIndexValid(index)) + { + return E.E_INVALIDARG; + } + + if (data is null) + { + return E.E_POINTER; + } + + if (dataSize != (uint)sizeof(void*)) + { + return E.E_INVALIDARG; + } + + ID2D1ResourceTextureManager* resourceTextureManager = null; + + // Check that the input object implements ID2D1ResourceTextureManager + int result = ((IUnknown*)*(void**)data)->QueryInterface( + riid: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in ID2D1ResourceTextureManager.Guid)), + ppvObject: (void**)&resourceTextureManager); + + if (result != S.S_OK) + { + return result; + } + + ID2D1ResourceTextureManagerInternal* resourceTextureManagerInternal = null; + + // Then, also check that it implements ID2D1ResourceTextureManagerInternal + result = ((IUnknown*)*(void**)data)->QueryInterface( + riid: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in ID2D1ResourceTextureManagerInternal.Guid)), + ppvObject: (void**)&resourceTextureManagerInternal); + + if (result != S.S_OK) + { + // If the internal interface is not present, fail (but first release the public interface) + _ = ((IUnknown*)resourceTextureManager)->Release(); + + return result; + } + + // If the object is valid, release the internal interface (it was retrieved just to check it was present) + _ = ((IUnknown*)resourceTextureManagerInternal)->Release(); + + // Store the resource texture manager into the buffer + this.resourceTextureManagerBuffer[index] = resourceTextureManager; return S.S_OK; } + + /// + /// Checks whether a given index for a resource texture manager is valid for the current effect. + /// + /// The resource texture manager index to validate. + /// Whether or not is valid for the current effect. + private bool IsResourceTextureManagerIndexValid(int index) + { + foreach (ref readonly D2D1ResourceTextureDescription resourceTextureDescription in new ReadOnlySpan(this.resourceTextureDescriptions, this.resourceTextureDescriptionCount)) + { + if (resourceTextureDescription.Index == index) + { + return true; + } + } + + return false; + } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs index 96b46719..b31caa95 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs @@ -2,6 +2,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; +using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #if NET6_0_OR_GREATER @@ -209,6 +210,11 @@ internal unsafe partial struct PixelShaderEffect /// private ID2D1EffectContext* d2D1EffectContext; + /// + /// The resource texture managers for the current instance. + /// + private ResourceTextureManagerBuffer resourceTextureManagerBuffer; + /// /// The factory method for . /// @@ -277,7 +283,9 @@ internal unsafe partial struct PixelShaderEffect @this->d2D1DrawInfo = null; @this->d2D1EffectContext = null; - * effectImpl = (IUnknown*)@this; + @this->resourceTextureManagerBuffer.AsSpan().Clear(); + + *effectImpl = (IUnknown*)@this; return S.S_OK; } @@ -348,6 +356,18 @@ internal unsafe partial struct PixelShaderEffect _ = this.d2D1EffectContext->Release(); } + // Use the list of resource texture descriptions to see the indices that might have accepted a resource texture manager. + // Then, retrieve all of them and release the ones that had been assigned (from one of the property bindings). + foreach (ref readonly D2D1ResourceTextureDescription resourceTextureDescription in new ReadOnlySpan(this.resourceTextureDescriptions, this.resourceTextureDescriptionCount)) + { + ID2D1ResourceTextureManager* resourceTextureManager = resourceTextureManagerBuffer[resourceTextureDescription.Index]; + + if (resourceTextureManager is not null) + { + _ = ((IUnknown*)resourceTextureManager)->Release(); + } + } + NativeMemory.Free(Unsafe.AsPointer(ref this)); }