From ad20d6c2df000a3ef70b630fd9bb19656c7f40f7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 11 Aug 2022 19:35:04 +0200 Subject: [PATCH] Add D2D1ResourceTextureManager type --- .../Shaders/Interop/D2D1ExtendMode.cs | 28 +++ .../Shaders/Interop/D2D1PixelShaderEffect.cs | 20 +-- .../Interop/D2D1ResourceTextureManager.cs | 165 ++++++++++++++++++ 3 files changed, 201 insertions(+), 12 deletions(-) create mode 100644 src/ComputeSharp.D2D1/Shaders/Interop/D2D1ExtendMode.cs create mode 100644 src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ExtendMode.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ExtendMode.cs new file mode 100644 index 00000000..0ecb3e2b --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ExtendMode.cs @@ -0,0 +1,28 @@ +using static TerraFX.Interop.DirectX.D2D1_EXTEND_MODE; + +namespace ComputeSharp.D2D1; + +/// +/// Specifies how a brush paints areas outside of its normal content area. +/// +/// +/// This type exposes the available values in . +/// +public enum D2D1ExtendMode +{ + /// + /// Repeat the edge pixels of the brush's content for all regions outside the normal content area. + /// + Clamp = (int)D2D1_EXTEND_MODE_CLAMP, + + /// + /// Repeat the brush's content. + /// + Wrap = (int)D2D1_EXTEND_MODE_WRAP, + + /// + /// The same as , except that alternate tiles of the brush's + /// content are flipped (the brush's normal content is drawn untransformed). + /// + Mirror = (int)D2D1_EXTEND_MODE_MIRROR +} diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index 969aa1c4..101affef 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -1,9 +1,6 @@ using System; using System.Globalization; using System.Runtime.CompilerServices; -#if !NET6_0_OR_GREATER -using System.Runtime.InteropServices; -#endif using System.Text; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Helpers; @@ -11,7 +8,6 @@ using ComputeSharp.D2D1.Interop.Effects; using ComputeSharp.D2D1.Shaders.Interop.Buffers; using ComputeSharp.D2D1.Shaders.Loaders; using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; #pragma warning disable CS0618 @@ -30,7 +26,7 @@ public static unsafe class D2D1PixelShaderEffect /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . - public static unsafe void RegisterForD2D1Factory1(void* d2D1Factory1, out Guid effectId) + public static void RegisterForD2D1Factory1(void* d2D1Factory1, out Guid effectId) where T : unmanaged, ID2D1PixelShader { RegisterForD2D1Factory1(d2D1Factory1, null, out effectId); @@ -45,7 +41,7 @@ public static unsafe class D2D1PixelShaderEffect /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . - public static unsafe void RegisterForD2D1Factory1(void* d2D1Factory1, out Guid effectId) + public static void RegisterForD2D1Factory1(void* d2D1Factory1, out Guid effectId) where T : unmanaged, ID2D1PixelShader where TMapper : class, ID2D1TransformMapper, new() { @@ -61,7 +57,7 @@ public static unsafe class D2D1PixelShaderEffect /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . - public static unsafe void RegisterForD2D1Factory1(void* d2D1Factory1, Func>? mapperFactory, out Guid effectId) + public static void RegisterForD2D1Factory1(void* d2D1Factory1, Func>? mapperFactory, out Guid effectId) where T : unmanaged, ID2D1PixelShader { effectId = default; @@ -243,7 +239,7 @@ public static unsafe class D2D1PixelShaderEffect /// A blob containing serialized information that can be used to register an effect. /// Thrown if an effect is registered multiple times with different properties. /// For more info and for details on the binary format, see . - public static unsafe ReadOnlyMemory GetRegistrationBlob(out Guid effectId) + public static ReadOnlyMemory GetRegistrationBlob(out Guid effectId) where T : unmanaged, ID2D1PixelShader { return GetRegistrationBlob(null, out effectId); @@ -258,7 +254,7 @@ public static unsafe class D2D1PixelShaderEffect /// A blob containing serialized information that can be used to register an effect. /// Thrown if an effect is registered multiple times with different properties. /// For more info and for details on the binary format, see . - public static unsafe ReadOnlyMemory GetRegistrationBlob(out Guid effectId) + public static ReadOnlyMemory GetRegistrationBlob(out Guid effectId) where T : unmanaged, ID2D1PixelShader where TMapper : class, ID2D1TransformMapper, new() { @@ -305,7 +301,7 @@ public static unsafe class D2D1PixelShaderEffect /// For more info, see . /// /// - public static unsafe ReadOnlyMemory GetRegistrationBlob(Func>? mapperFactory, out Guid effectId) + public static ReadOnlyMemory GetRegistrationBlob(Func>? mapperFactory, out Guid effectId) where T : unmanaged, ID2D1PixelShader { effectId = default; @@ -491,7 +487,7 @@ public static unsafe class D2D1PixelShaderEffect /// to use (or an overload), and use that blob to register an effect. /// /// For more info, see . - public static unsafe void CreateFromD2D1DeviceContext(void* d2D1DeviceContext, void** d2D1Effect) + public static void CreateFromD2D1DeviceContext(void* d2D1DeviceContext, void** d2D1Effect) where T : unmanaged, ID2D1PixelShader { if (!PixelShaderEffect.For.TryGetId(out Guid id)) @@ -511,7 +507,7 @@ public static unsafe class D2D1PixelShaderEffect /// The input D2D1 pixel shader to set the contant buffer for. /// A pointer to the ID2D1Effect instance to use. /// For more info, see . - public static unsafe void SetConstantBufferForD2D1Effect(in T shader, void* d2D1Effect) + public static void SetConstantBufferForD2D1Effect(in T shader, void* d2D1Effect) where T : unmanaged, ID2D1PixelShader { D2D1EffectDispatchDataLoader dataLoader = new((ID2D1Effect*)d2D1Effect); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs new file mode 100644 index 00000000..bf3cb985 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs @@ -0,0 +1,165 @@ +using System; +using System.Runtime.InteropServices; +using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; +using TerraFX.Interop.DirectX; + +namespace ComputeSharp.D2D1.Interop; + +/// +/// Provides an ID2D1ResourceTextureManager implementation, which can be used to manage resources in +/// an effect created with and share them across multiple effect instances. +/// +/// +/// +/// The built-in ID2D1Effect +/// implementation provided by uses resource texture manager objects to handle resource textures. +/// +/// +/// These managers are COM objects implementing the following interface: +/// +/// [uuid(3C4FC7E4-A419-46CA-B5F6-66EB4FF18D64)] +/// interface ID2D1ResourceTextureManager : IUnknown +/// { +/// HRESULT CreateResourceTexture( +/// [in, optional] const GUID *resourceId, +/// [in] const D2D1_RESOURCE_TEXTURE_PROPERTIES *resourceTextureProperties, +/// [in, optional] const BYTE *data, +/// [in, optional] const UINT32 *strides, +/// UINT32 dataSize); +/// +/// HRESULT Update( +/// [in, optional] const UINT32 *minimumExtents, +/// [in, optional] const UINT32 *maximimumExtents, +/// [in] const UINT32 *strides, +/// UINT32 dimensions, +/// [in] const BYTE *data, +/// UINT32 dataCount); +/// }; +/// +/// +/// +/// These can be used to create resource textures, pass them to effects, and also share resources among different effects. +/// +/// +/// This interface is implemented by ComputeSharp.D2D1, and it can be used through the APIs in . +/// That is, is first use to create an ID2D1ResourceTextureManager instance. Then, +/// can be used to initialize the resource texture held by the manager. The manager can also be +/// assigned to an effect at any time, using the available property indices from . The +/// API can also be used to update texture data after the initial creation. +/// +/// +/// The ID2D1ResourceTextureManager contract allows callers to create a resource texture at any time even before the effect +/// has been initialized and the manager assigned to it. In that case, the manager will buffer the data internally, and will defer +/// the creation of the actual resource texture until when it's possible for it to do so, at which point it will also free up the buffer. +/// +/// +/// If there was a need to have for a custom resource manager, in order for external implementations to be accepted by the effect objects +/// created by , the ID2D1ResourceTextureManagerInternal interface also needs to be implemented: +/// +/// [uuid(5CBB1024-8EA1-4689-81BF-8AD190B5EF5D)] +/// interface ID2D1ResourceTextureManager : IUnknown +/// { +/// HRESULT Initialize([in] ID2D1EffectContext *effectContext); +/// +/// HRESULT GetResourceTexture([out] ID2D1ResourceTexture **resourceTexture); +/// }; +/// +/// +/// +[Guid("3C4FC7E4-A419-46CA-B5F6-66EB4FF18D64")] +public static unsafe class D2D1ResourceTextureManager +{ + /// + /// Creates a new ID2D1ResourceTextureManager instance. + /// + /// A pointer to the resulting ID2D1ResourceTextureManager instance. + public static void CreateResourceTextureManager(void** resourceTextureManager) + { + // TODO: validate argument and return + + _ = ResourceTextureManager.Factory((ResourceTextureManager**)resourceTextureManager); + } + + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static void CreateResourceTexture( + void* resourceTextureManager, + Guid resourceId, + ReadOnlySpan extents, + D2D1BufferPrecision bufferPrecision, + D2D1ChannelDepth channelDepth, + D2D1Filter filter, + ReadOnlySpan extendModes, + ReadOnlySpan data, + ReadOnlySpan strides) + { + // TODO: validate argument and return + + fixed (uint* pExtents = extents) + fixed (D2D1ExtendMode* pExtendModes = extendModes) + fixed (byte* pData = data) + fixed (uint* pStrides = strides) + { + D2D1_RESOURCE_TEXTURE_PROPERTIES d2D1ResourceTextureProperties; + d2D1ResourceTextureProperties.extents = pExtents; + d2D1ResourceTextureProperties.dimensions = (uint)extents.Length; + d2D1ResourceTextureProperties.bufferPrecision = (D2D1_BUFFER_PRECISION)bufferPrecision; + d2D1ResourceTextureProperties.channelDepth = (D2D1_CHANNEL_DEPTH)channelDepth; + d2D1ResourceTextureProperties.filter = (D2D1_FILTER)filter; + d2D1ResourceTextureProperties.extendModes = (D2D1_EXTEND_MODE*)pExtendModes; + + _ = ((delegate* unmanaged[Stdcall])(*(void***)resourceTextureManager)[3])( + resourceTextureManager, + &resourceId, + &d2D1ResourceTextureProperties, + pData, + pStrides, + (uint)data.Length); + } + } + + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static void UpdateResourceTexture( + void* resourceTextureManager, + ReadOnlySpan minimumExtents, + ReadOnlySpan maximumExtents, + ReadOnlySpan strides, + uint dimensions, + ReadOnlySpan data) + { + // TODO: validate argument and return + + fixed (uint* pMinimumExtents = minimumExtents) + fixed (uint* pMaximumExtents = maximumExtents) + fixed (uint* pStrides = strides) + fixed (byte* pData = data) + { + _ = ((delegate* unmanaged[Stdcall])(*(void***)resourceTextureManager)[4])( + resourceTextureManager, + pMinimumExtents, + pMaximumExtents, + pStrides, + dimensions, + pData, + (uint)data.Length); + } + } +}