diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs index a894c2f2..0fcc7e0e 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs @@ -2,7 +2,7 @@ using System; using System.Runtime.CompilerServices; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; -using ComputeSharp.D2D1.Interop.Effects; +using ComputeSharp.D2D1.Interop; #if WINDOWS_UWP using ComputeSharp.D2D1.Uwp.Extensions; using ComputeSharp.D2D1.Uwp.Helpers; @@ -74,7 +74,7 @@ unsafe partial class PixelShaderEffect if (!this.isEffectFactoryRegistered) { ResourceManager.RegisterEffectFactory( - effectId: PixelShaderEffect.For.Instance.Id, + effectId: D2D1PixelShaderEffect.GetEffectId(), factory: new EffectFactory()); this.isEffectFactoryRegistered = true; diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs index 796bd28a..f63e388e 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -4,7 +4,6 @@ using System.Threading; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Interop; -using ComputeSharp.D2D1.Interop.Effects; #if WINDOWS_UWP using ComputeSharp.D2D1.Uwp.Extensions; using ComputeSharp.D2D1.Uwp.Helpers; @@ -262,7 +261,7 @@ unsafe partial class PixelShaderEffect { fixed (ID2D1Effect** d2D1Effect = this.d2D1Effect) { - Guid effectId = PixelShaderEffect.For.Instance.Id; + Guid effectId = D2D1PixelShaderEffect.GetEffectId(); using ComPtr d2D1DeviceContextEffective = default; using ComPtr d2D1DeviceContextLease = default; diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index 46941807..7e9dcbfb 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -188,7 +188,7 @@ public static unsafe class D2D1PixelShaderEffect d2D1PropertyBinding[D2D1PixelShaderEffectProperty.ResourceTextureManager14].setFunction = PixelShaderEffect.SetResourceTextureManager14; d2D1PropertyBinding[D2D1PixelShaderEffectProperty.ResourceTextureManager15].setFunction = PixelShaderEffect.SetResourceTextureManager15; - fixed (Guid* pGuid = &PixelShaderEffect.For.Instance.Id) + fixed (Guid* pGuid = &GetEffectId()) { // Register the effect ((ID2D1Factory1*)d2D1Factory1)->RegisterEffectFromString( @@ -196,10 +196,10 @@ public static unsafe class D2D1PixelShaderEffect propertyXml: (ushort*)pXml, bindings: d2D1PropertyBinding, bindingsCount: (uint)(D2D1PixelShaderEffectProperty.NumberOfAlwaysAvailableProperties + D2D1PixelShader.GetResourceTextureCount()), - effectFactory: PixelShaderEffect.For.Instance.Factory).Assert(); + effectFactory: PixelShaderEffect.Globals.Instance.Factory).Assert(); } - effectId = PixelShaderEffect.For.Instance.Id; + effectId = GetEffectId(); } } @@ -250,8 +250,8 @@ public static unsafe class D2D1PixelShaderEffect writer.Write(D2D1EffectRegistrationData.V1.BlobId); // Effect id and number of inputs - writer.Write(PixelShaderEffect.For.Instance.Id); - writer.Write(PixelShaderEffect.For.Instance.InputCount); + writer.Write(GetEffectId()); + writer.Write(D2D1PixelShader.GetInputCount()); // Retrieve the effect XML and include it into the registration blob using (D2D1EffectXmlFactory.EffectXml effectXml = D2D1EffectXmlFactory.GetXmlBuffer()) @@ -388,14 +388,14 @@ public static unsafe class D2D1PixelShaderEffect } // Effect factory - writer.Write((nint)PixelShaderEffect.For.Instance.Factory); + writer.Write((nint)PixelShaderEffect.Globals.Instance.Factory); byte[] registrationBlob = writer.WrittenSpan.ToArray(); writer.Dispose(); // Extract the effect id (the same that was encoded in the registration blob) - effectId = PixelShaderEffect.For.Instance.Id; + effectId = GetEffectId(); return registrationBlob; } @@ -419,7 +419,7 @@ public static unsafe class D2D1PixelShaderEffect default(ArgumentNullException).ThrowIfNull(d2D1DeviceContext); default(ArgumentNullException).ThrowIfNull(d2D1Effect); - fixed (Guid* pGuid = &PixelShaderEffect.For.Instance.Id) + fixed (Guid* pGuid = &GetEffectId()) { ((ID2D1DeviceContext*)d2D1DeviceContext)->CreateEffect( effectId: pGuid, diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs index d9eba121..bc728ef2 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs @@ -4,9 +4,6 @@ using System.Runtime.InteropServices; using ComputeSharp.D2D1.Descriptors; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; -#if !NET6_0_OR_GREATER -using RuntimeHelpers = ComputeSharp.NetStandard.RuntimeHelpers; -#endif namespace ComputeSharp.D2D1.Interop.Effects; @@ -21,210 +18,59 @@ internal unsafe partial struct PixelShaderEffect /// The resulting effect factory. /// The HRESULT for the operation. [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate int FactoryDelegate(IUnknown** effectImpl); + public delegate int FactoryDelegate(IUnknown** effectImpl); /// - /// A generic pixel shader implementation. + /// A base type with global values for pixel shader effects. /// - /// The type of shader. - public sealed class For - where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor + public abstract class Globals { /// - /// The wrapper for the shader factory. + /// Gets the wrapper for the shader factory. /// private readonly FactoryDelegate effectFactory; /// - /// The for the shader. - /// - /// - /// This field could have used to reduce some verbosity where it is - /// being used to pass its address to a native API, but the attribute is incompatible with collectible assemblies. - /// Because of that, it can't be used, as this project is explicitly meant to support plugin-like scenarios. - /// - private readonly Guid shaderId; - - /// - /// The size of the constant buffer for the shader. - /// - private readonly int constantBufferSize; - - /// - /// The number of inputs for the shader. - /// - private readonly int inputCount; - - /// - /// The buffer with the types of inputs for the shader. - /// - private readonly D2D1PixelShaderInputType* inputTypes; - - /// - /// The number of available input descriptions. - /// - private readonly int inputDescriptionCount; - - /// - /// The buffer with the available input descriptions for the shader. - /// - private readonly D2D1InputDescription* inputDescriptions; - - /// - /// The pixel options for the shader. - /// - private readonly D2D1PixelOptions pixelOptions; - - /// - /// The shader bytecode. - /// - private readonly byte* bytecode; - - /// - /// The size of . - /// - private readonly int bytecodeSize; - - /// - /// The buffer precision for the resulting output buffer. - /// - private readonly D2D1BufferPrecision bufferPrecision; - - /// - /// The channel depth for the resulting output buffer. - /// - private readonly D2D1ChannelDepth channelDepth; - - /// - /// The number of available resource texture descriptions. - /// - private readonly int resourceTextureDescriptionCount; - - /// - /// The buffer with the available resource texture descriptions for the shader. - /// - private readonly D2D1ResourceTextureDescription* resourceTextureDescriptions; - - /// - /// Creates a new instance with the specified parameters. + /// Creates a new instance with the specified parameters. /// /// The wrapper for the shader factory. - /// The for the shader. - /// The size of the constant buffer for the shader. - /// The number of inputs for the shader. - /// The buffer with the types of inputs for the shader. - /// The number of available input descriptions. - /// The buffer with the available input descriptions for the shader. - /// The pixel options for the shader. - /// The shader bytecode. - /// The size of . - /// The buffer precision for the resulting output buffer. - /// The channel depth for the resulting output buffer. - /// The number of available resource texture descriptions. - /// The buffer with the available resource texture descriptions for the shader. - private For( - FactoryDelegate effectFactory, - Guid shaderId, - int constantBufferSize, - int inputCount, - D2D1PixelShaderInputType* inputTypes, - int inputDescriptionCount, - D2D1InputDescription* inputDescriptions, - D2D1PixelOptions pixelOptions, - byte* bytecode, - int bytecodeSize, - D2D1BufferPrecision bufferPrecision, - D2D1ChannelDepth channelDepth, - int resourceTextureDescriptionCount, - D2D1ResourceTextureDescription* resourceTextureDescriptions) + protected Globals(FactoryDelegate effectFactory) { this.effectFactory = effectFactory; - this.shaderId = shaderId; - this.constantBufferSize = constantBufferSize; - this.inputCount = inputCount; - this.inputTypes = inputTypes; - this.inputDescriptionCount = inputDescriptionCount; - this.inputDescriptions = inputDescriptions; - this.pixelOptions = pixelOptions; - this.bytecode = bytecode; - this.bytecodeSize = bytecodeSize; - this.bufferPrecision = bufferPrecision; - this.channelDepth = channelDepth; - this.resourceTextureDescriptionCount = resourceTextureDescriptionCount; - this.resourceTextureDescriptions = resourceTextureDescriptions; } - /// - /// Gets the shared instance. - /// - public static For Instance { get; } = CreateInstance(); + /// + public abstract ref readonly Guid EffectId { get; } - /// - /// Creates a new instance. - /// - /// The initialized instance. - private static For CreateInstance() - { - // Load all shader properties - Guid shaderId = D2D1PixelShaderEffect.GetEffectId(); - int constantBufferSize = D2D1PixelShader.GetConstantBufferSize(); - D2D1BufferPrecision bufferPrecision = D2D1PixelShader.GetOutputBufferPrecision(); - D2D1ChannelDepth channelDepth = D2D1PixelShader.GetOutputBufferChannelDepth(); - D2D1PixelOptions pixelOptions = D2D1PixelShader.GetPixelOptions(); + /// + public abstract int ConstantBufferSize { get; } - // Prepare the inputs info - int inputCount = D2D1PixelShader.GetInputCount(); - D2D1PixelShaderInputType* inputTypes = (D2D1PixelShaderInputType*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For), sizeof(D2D1PixelShaderInputType) * inputCount); + /// + public abstract int InputCount { get; } - D2D1PixelShader.GetInputTypes().Span.CopyTo(new Span(inputTypes, inputCount)); + /// + public abstract int ResourceTextureCount { get; } - // Prepare the input descriptions - ReadOnlyMemory inputDescriptionsInfo = D2D1PixelShader.GetInputDescriptions(); - int inputDescriptionCount = inputDescriptionsInfo.Length; - D2D1InputDescription* inputDescriptions = (D2D1InputDescription*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For), sizeof(D2D1InputDescription) * inputDescriptionCount); + /// + public abstract ReadOnlyMemory InputTypes { get; } - inputDescriptionsInfo.Span.CopyTo(new Span(inputDescriptions, inputDescriptionCount)); + /// + public abstract ReadOnlyMemory InputDescriptions { get; } - // Prepare the resource texture descriptions - ReadOnlyMemory resourceTextureDescriptionsInfo = D2D1PixelShader.GetResourceTextureDescriptions(); - int resourceTextureDescriptionCount = resourceTextureDescriptionsInfo.Length; - D2D1ResourceTextureDescription* resourceTextureDescriptions = (D2D1ResourceTextureDescription*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For), sizeof(D2D1ResourceTextureDescription) * resourceTextureDescriptionCount); + /// + public abstract ReadOnlyMemory ResourceTextureDescriptions { get; } - resourceTextureDescriptionsInfo.Span.CopyTo(new Span(resourceTextureDescriptions, resourceTextureDescriptionCount)); + /// + public abstract D2D1PixelOptions PixelOptions { get; } - // Copy the bytecode to the target buffer - ReadOnlyMemory bytecodeInfo = D2D1PixelShader.LoadBytecode(); - int bytecodeSize = bytecodeInfo.Length; - byte* bytecode = (byte*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For), bytecodeSize); + /// + public abstract D2D1BufferPrecision BufferPrecision { get; } - bytecodeInfo.Span.CopyTo(new Span(bytecode, bytecodeSize)); + /// + public abstract D2D1ChannelDepth ChannelDepth { get; } - // Initialize the shared instance with the computed state - return new( - CreateEffect, - shaderId, - constantBufferSize, - inputCount, - inputTypes, - inputDescriptionCount, - inputDescriptions, - pixelOptions, - bytecode, - bytecodeSize, - bufferPrecision, - channelDepth, - resourceTextureDescriptionCount, - resourceTextureDescriptions); - } - - /// - /// Gets a reference to the id of the effect. - /// - public ref readonly Guid Id - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => ref this.shaderId; - } + /// + public abstract ReadOnlyMemory HlslBytecode { get; } /// /// Gets the factory for the current effect. @@ -242,32 +88,83 @@ internal unsafe partial struct PixelShaderEffect get => (void*)Marshal.GetFunctionPointerForDelegate(this.effectFactory); #endif } + } + + /// + /// A generic pixel shader implementation. + /// + /// The type of shader. + public sealed class Globals : Globals + where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor + { + /// + /// The HLSL bytecode (either precompiled, or the currently available one after compilation). + /// + private ReadOnlyMemory hlslBytecode; /// - /// Gets the number of inputs for the effect. + /// Creates a new instance for shaders of type . /// - public int InputCount => this.inputCount; + private Globals() + : base(CreateEffect) + { + } + + /// + /// Gets the shared instance to use. + /// + public static Globals Instance { get; } = new(); + + /// + public override ref readonly Guid EffectId => ref D2D1PixelShaderEffect.GetEffectId(); + + /// + public override int ConstantBufferSize => D2D1PixelShader.GetConstantBufferSize(); + + /// + public override int InputCount => D2D1PixelShader.GetInputCount(); + + /// + public override int ResourceTextureCount => D2D1PixelShader.GetResourceTextureCount(); + + /// + public override ReadOnlyMemory InputTypes => D2D1PixelShader.GetInputTypes(); + + /// + public override ReadOnlyMemory InputDescriptions => D2D1PixelShader.GetInputDescriptions(); + + /// + public override ReadOnlyMemory ResourceTextureDescriptions => D2D1PixelShader.GetResourceTextureDescriptions(); + + /// + public override D2D1PixelOptions PixelOptions => D2D1PixelShader.GetPixelOptions(); + + /// + public override D2D1BufferPrecision BufferPrecision => D2D1PixelShader.GetOutputBufferPrecision(); + + /// + public override D2D1ChannelDepth ChannelDepth => D2D1PixelShader.GetOutputBufferChannelDepth(); + + /// + public override ReadOnlyMemory HlslBytecode + { + get + { + // Fast path if we already have some bytecode (either precompiled, or already compiled) + if (this.hlslBytecode is ReadOnlyMemory { Length: > 0 } hlslBytecode) + { + return hlslBytecode; + } + + // If not, lazily compile the bytecode when requested + return this.hlslBytecode = D2D1PixelShader.LoadBytecode(); + } + } /// private static int CreateEffect(IUnknown** effectImpl) { - For instance = Instance; - - return PixelShaderEffect.Factory( - instance.shaderId, - instance.constantBufferSize, - instance.inputCount, - instance.inputTypes, - instance.inputDescriptionCount, - instance.inputDescriptions, - instance.pixelOptions, - instance.bytecode, - instance.bytecodeSize, - instance.bufferPrecision, - instance.channelDepth, - instance.resourceTextureDescriptionCount, - instance.resourceTextureDescriptions, - effectImpl); + return PixelShaderEffect.Factory(Instance, effectImpl); } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs index 21ebdd53..6dbb2ec5 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs @@ -110,7 +110,7 @@ partial struct PixelShaderEffect { @this = (PixelShaderEffect*)&((void**)@this)[-1]; - return (uint)@this->inputCount; + return (uint)@this->GetGlobals().InputCount; } /// @@ -119,7 +119,7 @@ partial struct PixelShaderEffect { @this = (PixelShaderEffect*)&((void**)@this)[-1]; - if (inputRectsCount != @this->inputCount) + if (inputRectsCount != @this->GetGlobals().InputCount) { return E.E_INVALIDARG; } @@ -136,6 +136,8 @@ partial struct PixelShaderEffect } else { + ReadOnlySpan inputTypes = @this->GetGlobals().InputTypes.Span; + // If no custom transform is used, apply the default mapping. In this case, the loop will // automatically handle cases where no inputs are defined. If inputs are present instead, // the default mapping will set the input rect for a simple input to be the same as the @@ -143,7 +145,7 @@ partial struct PixelShaderEffect // will handle clipping the inputs to the actual output rect area, if needed. for (uint i = 0; i < inputRectsCount; i++) { - switch (@this->inputTypes[i]) + switch (inputTypes[(int)i]) { case D2D1PixelShaderInputType.Simple: inputRects[i] = *outputRect; @@ -166,7 +168,7 @@ partial struct PixelShaderEffect { @this = (PixelShaderEffect*)&((void**)@this)[-1]; - if (inputRectCount != @this->inputCount) + if (inputRectCount != @this->GetGlobals().InputCount) { return E.E_INVALIDARG; } @@ -179,7 +181,7 @@ partial struct PixelShaderEffect HRESULT hresult = D2D1DrawInfoUpdateContextImpl.Factory( drawInfoUpdateContext: d2D1DrawInfoUpdateContext.GetAddressOf(), constantBuffer: @this->constantBuffer, - constantBufferSize: @this->constantBufferSize, + constantBufferSize: @this->GetGlobals().ConstantBufferSize, d2D1DrawInfo: @this->d2D1DrawInfo); if (!Windows.SUCCEEDED(hresult)) @@ -215,6 +217,8 @@ partial struct PixelShaderEffect } else { + ReadOnlySpan inputTypes = @this->GetGlobals().InputTypes.Span; + // If at least one input is present and no custom mapper is available, apply the default // mapping. In this case, the output rect should be the union of the input rects for all // the simple shader inputs, or infinite if there are no simple inputs. The input rects @@ -224,7 +228,7 @@ partial struct PixelShaderEffect for (uint i = 0; i < inputRectCount; i++) { - if (@this->inputTypes[i] == D2D1PixelShaderInputType.Simple) + if (inputTypes[(int)i] == D2D1PixelShaderInputType.Simple) { if (isUnionOfSimpleRectsEmpty) { @@ -259,7 +263,7 @@ partial struct PixelShaderEffect { @this = (PixelShaderEffect*)&((void**)@this)[-1]; - if (inputIndex >= (uint)@this->inputCount) + if (inputIndex >= (uint)@this->GetGlobals().InputCount) { return E.E_INVALIDARG; } @@ -278,7 +282,7 @@ partial struct PixelShaderEffect { // The default mapping in this scenario just needs to set the invalid output rect for simple inputs to // be the invalid input rect, and to set the invalid output rect for complex inputs to an infinite rect. - switch (@this->inputTypes[inputIndex]) + switch (@this->GetGlobals().InputTypes.Span[(int)inputIndex]) { case D2D1PixelShaderInputType.Simple: *invalidOutputRect = invalidInputRect; @@ -311,8 +315,11 @@ partial struct PixelShaderEffect @this->d2D1DrawInfo = drawInfo; + Guid shaderId = @this->GetGlobals().EffectId; + D2D1PixelOptions pixelOptions = @this->GetGlobals().PixelOptions; + // Set the pixel shader for the effect - HRESULT hresult = drawInfo->SetPixelShader(&@this->shaderId, (D2D1_PIXEL_OPTIONS)@this->pixelOptions); + HRESULT hresult = drawInfo->SetPixelShader(&shaderId, (D2D1_PIXEL_OPTIONS)pixelOptions); if (hresult != S.S_OK) { @@ -320,32 +327,30 @@ partial struct PixelShaderEffect } // If any input descriptions are present, set them - if (@this->inputDescriptionCount > 0) + foreach (ref readonly D2D1InputDescription inputDescription in @this->GetGlobals().InputDescriptions.Span) { - for (int i = 0; i < @this->inputDescriptionCount; i++) + D2D1_INPUT_DESCRIPTION d2D1InputDescription; + d2D1InputDescription.filter = (D2D1_FILTER)inputDescription.Filter; + d2D1InputDescription.levelOfDetailCount = (uint)inputDescription.LevelOfDetailCount; + + hresult = drawInfo->SetInputDescription((uint)inputDescription.Index, d2D1InputDescription); + + if (hresult != S.S_OK) { - ref D2D1InputDescription inputDescription = ref @this->inputDescriptions[i]; - - D2D1_INPUT_DESCRIPTION d2D1InputDescription; - d2D1InputDescription.filter = (D2D1_FILTER)inputDescription.Filter; - d2D1InputDescription.levelOfDetailCount = (uint)inputDescription.LevelOfDetailCount; - - hresult = drawInfo->SetInputDescription((uint)inputDescription.Index, d2D1InputDescription); - - if (hresult != S.S_OK) - { - return hresult; - } + return hresult; } } + D2D1BufferPrecision bufferPrecision = @this->GetGlobals().BufferPrecision; + D2D1ChannelDepth channelDepth = @this->GetGlobals().ChannelDepth; + // If a custom buffer precision or channel depth is requested, set it - if (@this->bufferPrecision != D2D1BufferPrecision.Unknown || - @this->channelDepth != D2D1ChannelDepth.Default) + if (bufferPrecision != D2D1BufferPrecision.Unknown || + channelDepth != D2D1ChannelDepth.Default) { hresult = drawInfo->SetOutputBuffer( - (D2D1_BUFFER_PRECISION)@this->bufferPrecision, - (D2D1_CHANNEL_DEPTH)@this->channelDepth); + (D2D1_BUFFER_PRECISION)bufferPrecision, + (D2D1_CHANNEL_DEPTH)channelDepth); if (hresult != S.S_OK) { diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs index 2450925e..6c70ad3d 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs @@ -86,17 +86,24 @@ internal unsafe partial struct PixelShaderEffect [UnmanagedCallersOnly] public static int Initialize(PixelShaderEffect* @this, ID2D1EffectContext* effectContext, ID2D1TransformGraph* transformGraph) { - int hresult = effectContext->LoadPixelShader( - shaderId: &@this->shaderId, - shaderBuffer: @this->bytecode, - shaderBufferCount: (uint)@this->bytecodeSize); + ReadOnlySpan bytecode = @this->GetGlobals().HlslBytecode.Span; + int hresult; - // If E_INVALIDARG was returned, try to check whether double precision support was requested when not available. This - // is only done to provide a more helpful error message to callers. If no error was returned, the behavior is the same. - // If any error is detected while trying to check for shader support, ignore the value and propagate the current HRESULT. - if (hresult == E.E_INVALIDARG && effectContext->IsShaderSupported(@this->bytecode, @this->bytecodeSize) == S.S_FALSE) + fixed (Guid* pShaderId = &@this->GetGlobals().EffectId) + fixed (byte* pBytecode = bytecode) { - hresult = D2DERR.D2DERR_INSUFFICIENT_DEVICE_CAPABILITIES; + hresult = effectContext->LoadPixelShader( + shaderId: pShaderId, + shaderBuffer: pBytecode, + shaderBufferCount: (uint)bytecode.Length); + + // If E_INVALIDARG was returned, try to check whether double precision support was requested when not available. This + // is only done to provide a more helpful error message to callers. If no error was returned, the behavior is the same. + // If any error is detected while trying to check for shader support, ignore the value and propagate the current HRESULT. + if (hresult == E.E_INVALIDARG && effectContext->IsShaderSupported(pBytecode, bytecode.Length) == S.S_FALSE) + { + hresult = D2DERR.D2DERR_INSUFFICIENT_DEVICE_CAPABILITIES; + } } // If loading the bytecode succeeded, set the transform node @@ -130,7 +137,7 @@ internal unsafe partial struct PixelShaderEffect int hresult = S.S_OK; // Validate the constant buffer - if (@this->constantBufferSize > 0 && + if (@this->GetGlobals().ConstantBufferSize > 0 && @this->constantBuffer is null) { return E.E_NOT_VALID_STATE; @@ -141,12 +148,14 @@ internal unsafe partial struct PixelShaderEffect { hresult = @this->d2D1DrawInfo->SetPixelShaderConstantBuffer( buffer: @this->constantBuffer, - bufferCount: (uint)@this->constantBufferSize); + bufferCount: (uint)@this->GetGlobals().ConstantBufferSize); } if (Windows.SUCCEEDED(hresult)) { - for (int i = 0; i < @this->resourceTextureDescriptionCount; i++) + ReadOnlySpan resourceTextureDescriptions = @this->GetGlobals().ResourceTextureDescriptions.Span; + + for (int i = 0; i < resourceTextureDescriptions.Length; i++) { using ComPtr resourceTextureManager = @this->resourceTextureManagerBuffer[i]; @@ -180,7 +189,7 @@ internal unsafe partial struct PixelShaderEffect break; } - ref readonly D2D1ResourceTextureDescription resourceTextureDescription = ref @this->resourceTextureDescriptions[i]; + ref readonly D2D1ResourceTextureDescription resourceTextureDescription = ref resourceTextureDescriptions[i]; // Set the ID2D1ResourceTexture object to the current index in the ID2D1DrawInfo object in use hresult = @this->d2D1DrawInfo->SetResourceTexture( diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs index 3eff1fa7..9abc2a11 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs @@ -120,7 +120,7 @@ unsafe partial struct PixelShaderEffect dataSize == 0 && actualSize is not null) { - *actualSize = (uint)@this->constantBufferSize; + *actualSize = (uint)@this->GetGlobals().ConstantBufferSize; return S.S_FALSE; } @@ -130,7 +130,7 @@ unsafe partial struct PixelShaderEffect return E.E_POINTER; } - if (dataSize < @this->constantBufferSize) + if (dataSize < @this->GetGlobals().ConstantBufferSize) { return E.E_NOT_SUFFICIENT_BUFFER; } @@ -142,11 +142,11 @@ unsafe partial struct PixelShaderEffect uint writtenBytes = 0; - if (@this->constantBufferSize > 0) + if (@this->GetGlobals().ConstantBufferSize > 0) { - Buffer.MemoryCopy(@this->constantBuffer, data, dataSize, @this->constantBufferSize); + Buffer.MemoryCopy(@this->constantBuffer, data, dataSize, @this->GetGlobals().ConstantBufferSize); - writtenBytes = (uint)@this->constantBufferSize; + writtenBytes = (uint)@this->GetGlobals().ConstantBufferSize; } if (actualSize is not null) @@ -168,7 +168,7 @@ unsafe partial struct PixelShaderEffect return E.E_POINTER; } - if (dataSize != (uint)@this->constantBufferSize) + if (dataSize != (uint)@this->GetGlobals().ConstantBufferSize) { return E.E_INVALIDARG; } @@ -284,7 +284,7 @@ unsafe partial struct PixelShaderEffect /// The for the operation. private int GetResourceTextureManagerAtIndex(int index, byte* data, uint dataSize, uint* actualSize) { - if (index >= this.resourceTextureDescriptionCount) + if (index >= GetGlobals().ResourceTextureCount) { return E.E_INVALIDARG; } @@ -325,7 +325,7 @@ unsafe partial struct PixelShaderEffect /// The for the operation. private int SetResourceTextureManagerAtIndex(int index, byte* data, uint dataSize) { - if (index >= this.resourceTextureDescriptionCount) + if (index >= GetGlobals().ResourceTextureCount) { return E.E_INVALIDARG; } @@ -371,7 +371,7 @@ unsafe partial struct PixelShaderEffect // Initialize the resource texture manager, if an effect context is available if (this.d2D1EffectContext is not null) { - uint dimensions = (uint)this.resourceTextureDescriptions[index].Dimensions; + uint dimensions = (uint)GetGlobals().ResourceTextureDescriptions.Span[index].Dimensions; result = resourceTextureManagerInternal.Get()->Initialize(this.d2D1EffectContext, &dimensions); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs index 3106e5df..32d6ec72 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs @@ -106,86 +106,15 @@ internal unsafe partial struct PixelShaderEffect private volatile int referenceCount; /// - /// The for the shader. + /// The for the instance in use. /// - private Guid shaderId; + private GCHandle globalsHandle; /// /// The constant buffer data, if set. /// private byte* constantBuffer; - /// - /// The size of . - /// - private int constantBufferSize; - - /// - /// The number of inputs for the shader. - /// - private int inputCount; - - /// - /// The buffer with the types of inputs for the shader. - /// - /// - /// This buffer is shared among effect instances, so it should not be released. It is - /// owned by , which will release it when the target type is unloaded. - /// - private D2D1PixelShaderInputType* inputTypes; - - /// - /// The number of available input descriptions. - /// - private int inputDescriptionCount; - - /// - /// The buffer with the available input descriptions for the shader. - /// - /// - /// This buffer is also shared among effect instances, so it should not be released. It is - /// owned by , which will release it when the target type is unloaded. - /// - private D2D1InputDescription* inputDescriptions; - - /// - /// The pixel options for the shader. - /// - private D2D1PixelOptions pixelOptions; - - /// - /// The shader bytecode. - /// - private byte* bytecode; - - /// - /// The size of . - /// - private int bytecodeSize; - - /// - /// The buffer precision for the resulting output buffer. - /// - private D2D1BufferPrecision bufferPrecision; - - /// - /// The channel depth for the resulting output buffer. - /// - private D2D1ChannelDepth channelDepth; - - /// - /// The number of available resource texture descriptions. - /// - private int resourceTextureDescriptionCount; - - /// - /// The buffer with the available resource texture descriptions for the shader. - /// - /// - /// This buffer is also shared among effect instances, like . - /// - private D2D1ResourceTextureDescription* resourceTextureDescriptions; - /// /// The instance to use, if any. /// @@ -209,45 +138,33 @@ internal unsafe partial struct PixelShaderEffect /// /// The factory method for . /// - /// The for the shader. - /// The size of the constant buffer for the shader. - /// The number of inputs for the shader. - /// The buffer with the types of inputs for the shader. - /// The number of available input descriptions. - /// The buffer with the available input descriptions for the shader. - /// The pixel options for the shader. - /// The shader bytecode. - /// The size of . - /// The buffer precision for the resulting output buffer. - /// The channel depth for the resulting output buffer. - /// The number of available resource texture descriptions. - /// The buffer with the available resource texture descriptions for the shader. + /// The instance to use. /// The resulting effect instance. - /// This always returns 0. - private static int Factory( - Guid shaderId, - int constantBufferSize, - int inputCount, - D2D1PixelShaderInputType* inputTypes, - int inputDescriptionCount, - D2D1InputDescription* inputDescriptions, - D2D1PixelOptions pixelOptions, - byte* bytecode, - int bytecodeSize, - D2D1BufferPrecision bufferPrecision, - D2D1ChannelDepth channelDepth, - int resourceTextureDescriptionCount, - D2D1ResourceTextureDescription* resourceTextureDescriptions, - IUnknown** effectImpl) + /// The for the operation. + private static int Factory(Globals globals, IUnknown** effectImpl) { - PixelShaderEffect* @this; + PixelShaderEffect* @this = null; + GCHandle globalsHandle = default; try { @this = (PixelShaderEffect*)NativeMemory.Alloc((nuint)sizeof(PixelShaderEffect)); + globalsHandle = GCHandle.Alloc(globals); } - catch (OutOfMemoryException) + catch (Exception) { + // Free the effect, if we have one + if (@this is not null) + { + NativeMemory.Free(@this); + } + + // Free the handle, if we have one + if (globalsHandle.IsAllocated) + { + globalsHandle.Free(); + } + *effectImpl = null; return E.E_OUTOFMEMORY; @@ -258,20 +175,8 @@ internal unsafe partial struct PixelShaderEffect @this->lpVtblForID2D1EffectImpl = VtblForID2D1EffectImpl; @this->lpVtblForID2D1DrawTransform = VtblForID2D1DrawTransform; @this->referenceCount = 1; - @this->shaderId = shaderId; + @this->globalsHandle = globalsHandle; @this->constantBuffer = null; - @this->constantBufferSize = constantBufferSize; - @this->inputCount = inputCount; - @this->inputTypes = inputTypes; - @this->inputDescriptionCount = inputDescriptionCount; - @this->inputDescriptions = inputDescriptions; - @this->pixelOptions = pixelOptions; - @this->bytecode = bytecode; - @this->bytecodeSize = bytecodeSize; - @this->bufferPrecision = bufferPrecision; - @this->channelDepth = channelDepth; - @this->resourceTextureDescriptionCount = resourceTextureDescriptionCount; - @this->resourceTextureDescriptions = resourceTextureDescriptions; @this->d2D1TransformMapper = null; @this->d2D1DrawInfo = null; @this->d2D1EffectContext = null; @@ -331,6 +236,14 @@ internal unsafe partial struct PixelShaderEffect if (referenceCount == 0) { + // Retrieve the number of resource textures before freeing the globals handle + int resourceTextureDescriptionCount = GetGlobals().ResourceTextureCount; + + if (this.globalsHandle.IsAllocated) + { + this.globalsHandle.Free(); + } + if (this.constantBuffer is not null) { NativeMemory.Free(this.constantBuffer); @@ -352,7 +265,7 @@ internal unsafe partial struct PixelShaderEffect } // Retrieve all possible resource texture managers in use and release the ones that had been assigned (from one of the property bindings) - for (int i = 0; i < this.resourceTextureDescriptionCount; i++) + for (int i = 0; i < resourceTextureDescriptionCount; i++) { ID2D1ResourceTextureManager* resourceTextureManager = this.resourceTextureManagerBuffer[i]; @@ -367,4 +280,14 @@ internal unsafe partial struct PixelShaderEffect return referenceCount; } + + /// + /// Gets the instance for the current effect. + /// + /// The instance for the current effect. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private Globals GetGlobals() + { + return Unsafe.As(this.globalsHandle.Target!); + } } \ No newline at end of file