From 5cca651dd59190b4fab419ff64e2698bcf5d9d2e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 3 Apr 2022 20:23:42 +0200 Subject: [PATCH] Add D2DEmbeddedBytecodeAttribute types --- .../D2DEmbeddedBytecodeAttribute.cs | 48 +++++++++++++ .../Attributes/Profiles/D2D1ShaderProfile.cs | 69 +++++++++++++++++++ .../Helpers/ThrowHelper.cs | 14 ++++ .../Shaders/Interop/D2D1ReflectionServices.cs | 3 +- .../Shaders/Interop/D2D1ShaderInfo.cs | 2 +- .../Core/Dispatch/DispatchAxis.cs | 2 +- 6 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 src/ComputeSharp.D2D1Interop/Attributes/D2DEmbeddedBytecodeAttribute.cs create mode 100644 src/ComputeSharp.D2D1Interop/Attributes/Profiles/D2D1ShaderProfile.cs diff --git a/src/ComputeSharp.D2D1Interop/Attributes/D2DEmbeddedBytecodeAttribute.cs b/src/ComputeSharp.D2D1Interop/Attributes/D2DEmbeddedBytecodeAttribute.cs new file mode 100644 index 00000000..a568aa3b --- /dev/null +++ b/src/ComputeSharp.D2D1Interop/Attributes/D2DEmbeddedBytecodeAttribute.cs @@ -0,0 +1,48 @@ +using System; +using ComputeSharp.D2D1Interop.Helpers; + +namespace ComputeSharp.D2D1Interop; + +/// +/// An attribute that indicates that a given D2D1 shader should be precompiled at build time and embedded +/// directly into the containing assembly as static bytecode, to avoid compiling it at runtime. +/// +/// This attribute can be used to annotate shader types as follows: +/// +/// // A compute shader that is dispatched on a target buffer +/// [D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader50)] +/// struct MyShader : ID2D1PixelShader +/// { +/// } +/// +/// +/// +/// +/// The runtime compilation will automatically be skipped if the shader bytecode is then retrieved using a matching profile, +/// otherwise the usual runtime compilation will be used as fallback (or an exception will be thrown, depending on the API). +/// +[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] +public sealed class D2DEmbeddedBytecodeAttribute : Attribute +{ + /// + /// Creates a new instance with the specified parameters. + /// + /// The target shader profile to use to compile the shader. + public D2DEmbeddedBytecodeAttribute(D2D1ShaderProfile shaderProfile) + { + ShaderProfile = shaderProfile switch + { + D2D1ShaderProfile.PixelShader40 or + D2D1ShaderProfile.PixelShader40Level91 or + D2D1ShaderProfile.PixelShader40Level93 or + D2D1ShaderProfile.PixelShader41 or + D2D1ShaderProfile.PixelShader50 => shaderProfile, + _ => ThrowHelper.ThrowArgumentException(nameof(shaderProfile), "Invalid shader profile value.") + }; + } + + /// + /// Gets the number of threads in each thread group for the X axis + /// + public D2D1ShaderProfile ShaderProfile { get; } +} diff --git a/src/ComputeSharp.D2D1Interop/Attributes/Profiles/D2D1ShaderProfile.cs b/src/ComputeSharp.D2D1Interop/Attributes/Profiles/D2D1ShaderProfile.cs new file mode 100644 index 00000000..d2454180 --- /dev/null +++ b/src/ComputeSharp.D2D1Interop/Attributes/Profiles/D2D1ShaderProfile.cs @@ -0,0 +1,69 @@ +namespace ComputeSharp.D2D1Interop; + +/// +/// A to be used with to indicate the shader profile to use to precompile a shader. +/// +public enum D2D1ShaderProfile +{ + /// + /// Indicates a D2D1 pixel shader compiled for Direct3D 9.1 feature level. + /// + /// This has the following features: + /// + /// 64 arithmetic and 32 texture instructions. + /// 12 temporary registers. + /// 4 levels of dependent reads. + /// + /// + /// + /// + /// For more info, see . + /// + PixelShader40Level91, + + /// + /// Indicates a D2D1 pixel shader compiled for Direct3D 9.3 feature level. + /// + /// This has the following features: + /// + /// 512 instructions. + /// 32 temporary registers. + /// Static flow control (max depth of 4). + /// Dynamic flow control (max depth of 24). + /// D3DPS20CAPS_ARBITRARYSWIZZLE. + /// D3DPS20CAPS_GRADIENTINSTRUCTIONS. + /// D3DPS20CAPS_PREDICATION. + /// D3DPS20CAPS_NODEPENDENTREADLIMIT. + /// D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT. + /// + /// + /// + /// + /// For more info, see . + /// + PixelShader40Level93, + + /// + /// Indicates a D2D1 pixel shader compiled for Direct3D 10.0 feature level. + /// + /// + /// For more info, see . + /// + PixelShader40, + + /// + /// Indicates a D2D1 pixel shader compiled for Direct3D 10.1 feature level. + /// + /// + /// For more info, see . + /// + PixelShader41, + + /// + /// Indicates a D2D1 pixel shader compiled for Direct3D 11.0 and 11.1 feature levels. + /// + /// + /// For more info, see . + /// + PixelShader50 +} diff --git a/src/ComputeSharp.D2D1Interop/Helpers/ThrowHelper.cs b/src/ComputeSharp.D2D1Interop/Helpers/ThrowHelper.cs index e9e0fab1..f39bf501 100644 --- a/src/ComputeSharp.D2D1Interop/Helpers/ThrowHelper.cs +++ b/src/ComputeSharp.D2D1Interop/Helpers/ThrowHelper.cs @@ -8,6 +8,20 @@ namespace ComputeSharp.D2D1Interop.Helpers; /// internal static partial class ThrowHelper { + /// + /// Throws a new . + /// + /// The type of expected result. + /// The argument name. + /// The message to include in the exception. + /// Thrown with the specified parameters. + /// This method always throws, so it actually never returns a value. + [DoesNotReturn] + public static T ThrowArgumentException(string? name, string? message) + { + throw new ArgumentException(message, name); + } + /// /// Throws a new . /// diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ReflectionServices.cs b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ReflectionServices.cs index 1691f883..339a2c0a 100644 --- a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ReflectionServices.cs +++ b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ReflectionServices.cs @@ -1,7 +1,6 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.D2D1Interop; using ComputeSharp.D2D1Interop.Extensions; using ComputeSharp.Shaders.Dispatching; using TerraFX.Interop.DirectX; @@ -9,7 +8,7 @@ using TerraFX.Interop.Windows; #pragma warning disable CS0618 -namespace ComputeSharp.Interop; +namespace ComputeSharp.D2D1Interop.Interop; /// /// Provides methods to extract reflection info on D2D1 shaders generated using this library. diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ShaderInfo.cs b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ShaderInfo.cs index d8bafea7..35faaa15 100644 --- a/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ShaderInfo.cs +++ b/src/ComputeSharp.D2D1Interop/Shaders/Interop/D2D1ShaderInfo.cs @@ -1,4 +1,4 @@ -namespace ComputeSharp.Interop; +namespace ComputeSharp.D2D1Interop.Interop; /// /// A model representing reflection info for a given D2D1 shader. diff --git a/src/ComputeSharp/Core/Dispatch/DispatchAxis.cs b/src/ComputeSharp/Core/Dispatch/DispatchAxis.cs index 8dbe1294..7e95cd93 100644 --- a/src/ComputeSharp/Core/Dispatch/DispatchAxis.cs +++ b/src/ComputeSharp/Core/Dispatch/DispatchAxis.cs @@ -1,7 +1,7 @@ namespace ComputeSharp; /// -/// A to be used with to indicate the dispatch +/// A to be used with to indicate the dispatch axis to precompile a shader for. /// public enum DispatchAxis {