diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.CreateFromConstantBuffer.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.CreateFromConstantBuffer.Syntax.cs index 14511432..00c2161f 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.CreateFromConstantBuffer.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.CreateFromConstantBuffer.Syntax.cs @@ -24,7 +24,7 @@ partial class D2DPixelShaderDescriptorGenerator writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]"); - writer.WriteLine($"readonly unsafe {typeName} global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.CreateFromConstantBuffer(global::System.ReadOnlySpan data)"); + writer.WriteLine($"static unsafe {typeName} global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.CreateFromConstantBuffer(global::System.ReadOnlySpan data)"); using (writer.WriteBlock()) { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectId.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectId.Syntax.cs index 7d4f8284..6c6c2fa8 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectId.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectId.Syntax.cs @@ -19,7 +19,7 @@ partial class D2DPixelShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly ref readonly global::System.Guid global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.EffectId"); + writer.WriteLine($"static ref readonly global::System.Guid global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.EffectId"); using (writer.WriteBlock()) { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectMetadata.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectMetadata.Syntax.cs index 954b3166..83caf155 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectMetadata.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectMetadata.Syntax.cs @@ -61,7 +61,7 @@ partial class D2DPixelShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.Write($"readonly string? global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{qualifiedName}>.{propertyName} => "); + writer.Write($"static string? global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{qualifiedName}>.{propertyName} => "); // Append null or the metadata value as a string literal if (metadataValue is null) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.Syntax.cs index 4dc54c8a..fe88365a 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.Syntax.cs @@ -22,7 +22,7 @@ partial class D2DPixelShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly ComputeSharp.D2D1.D2D1ShaderProfile global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ShaderProfile => global::ComputeSharp.D2D1.D2D1ShaderProfile.{info.HlslInfoKey.ShaderProfile};"); + writer.WriteLine($"static ComputeSharp.D2D1.D2D1ShaderProfile global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ShaderProfile => global::ComputeSharp.D2D1.D2D1ShaderProfile.{info.HlslInfoKey.ShaderProfile};"); } /// @@ -42,7 +42,7 @@ partial class D2DPixelShaderDescriptorGenerator writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly ComputeSharp.D2D1.D2D1CompileOptions global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.CompileOptions => {compileOptionsExpression};"); + writer.WriteLine($"static ComputeSharp.D2D1.D2D1CompileOptions global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.CompileOptions => {compileOptionsExpression};"); } /// @@ -54,7 +54,7 @@ partial class D2DPixelShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.Write($"readonly global::System.ReadOnlyMemory global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.HlslBytecode => "); + writer.Write($"static global::System.ReadOnlyMemory global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.HlslBytecode => "); // If there is no bytecode, just return a default expression. // Otherwise, return the memory manager backed memory instance. diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.Syntax.cs index b9650bea..9addce80 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.Syntax.cs @@ -19,7 +19,7 @@ partial class D2DPixelShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly string global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.HlslSource =>"); + writer.WriteLine($"static string global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.HlslSource =>"); writer.IncreaseIndent(); writer.WriteLine("\"\"\""); writer.Write(info.HlslInfoKey.HlslSource, isMultiline: true); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs index d1f19417..ce1f1096 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputDescriptions.Syntax.cs @@ -21,7 +21,7 @@ partial class D2DPixelShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.Write($"readonly global::System.ReadOnlyMemory global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.InputDescriptions => "); + writer.Write($"static global::System.ReadOnlyMemory global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.InputDescriptions => "); // If there are no input descriptions, just return a default expression. // Otherwise, return the shared array with cached input descriptions. diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.Syntax.cs index f38586d1..0829cdc5 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.InputTypes.Syntax.cs @@ -19,7 +19,7 @@ partial class D2DPixelShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.Write($"readonly global::System.ReadOnlyMemory global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.InputTypes => "); + writer.Write($"static global::System.ReadOnlyMemory global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.InputTypes => "); // If there are no inputs, simply return a default expression. Otherwise, create // a ReadOnlyMemory instance from the memory manager. diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.Syntax.cs index 42d8754b..4a7d6503 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.LoadConstantBuffer.Syntax.cs @@ -24,7 +24,7 @@ partial class D2DPixelShaderDescriptorGenerator writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]"); - writer.WriteLine($"readonly unsafe void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.LoadConstantBuffer(in {typeName} shader, ref TLoader loader)"); + writer.WriteLine($"static unsafe void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.LoadConstantBuffer(in {typeName} shader, ref TLoader loader)"); using (writer.WriteBlock()) { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.NumericProperties.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.NumericProperties.Syntax.cs index 0efc43fd..332705c4 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.NumericProperties.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.NumericProperties.Syntax.cs @@ -21,7 +21,7 @@ partial class D2DPixelShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ConstantBufferSize => {info.ConstantBufferSizeInBytes};"); + writer.WriteLine($"static int global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ConstantBufferSize => {info.ConstantBufferSizeInBytes};"); } /// @@ -33,7 +33,7 @@ partial class D2DPixelShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.InputCount => {info.InputTypes.Length};"); + writer.WriteLine($"static int global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.InputCount => {info.InputTypes.Length};"); } /// @@ -45,7 +45,7 @@ partial class D2DPixelShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ResourceTextureCount => {info.ResourceTextureDescriptions.Length};"); + writer.WriteLine($"static int global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ResourceTextureCount => {info.ResourceTextureDescriptions.Length};"); } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.OutputBuffer.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.OutputBuffer.Syntax.cs index 3767bb78..5a860c53 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.OutputBuffer.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.OutputBuffer.Syntax.cs @@ -28,7 +28,7 @@ partial class D2DPixelShaderDescriptorGenerator writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly ComputeSharp.D2D1.D2D1BufferPrecision global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.BufferPrecision => {bufferPrecisionExpression};"); + writer.WriteLine($"static ComputeSharp.D2D1.D2D1BufferPrecision global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.BufferPrecision => {bufferPrecisionExpression};"); } /// @@ -47,7 +47,7 @@ partial class D2DPixelShaderDescriptorGenerator writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly ComputeSharp.D2D1.D2D1ChannelDepth global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ChannelDepth => {channelDepthExpression};"); + writer.WriteLine($"static ComputeSharp.D2D1.D2D1ChannelDepth global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ChannelDepth => {channelDepthExpression};"); } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.PixelOptions.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.PixelOptions.Syntax.cs index a469a342..6f0cf8b5 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.PixelOptions.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.PixelOptions.Syntax.cs @@ -26,7 +26,7 @@ partial class D2DPixelShaderDescriptorGenerator writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly ComputeSharp.D2D1.D2D1PixelOptions global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.PixelOptions => {pixelOptionsExpression};"); + writer.WriteLine($"static ComputeSharp.D2D1.D2D1PixelOptions global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.PixelOptions => {pixelOptionsExpression};"); } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ResourceTextureDescriptions.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ResourceTextureDescriptions.Syntax.cs index d0bb8a8b..1ec6ee18 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ResourceTextureDescriptions.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ResourceTextureDescriptions.Syntax.cs @@ -21,7 +21,7 @@ partial class D2DPixelShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.Write($"readonly global::System.ReadOnlyMemory global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ResourceTextureDescriptions => "); + writer.Write($"static global::System.ReadOnlyMemory global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ResourceTextureDescriptions => "); // If there are no resource texture descriptions, just return a default expression. // Otherwise, return a memory instance from the generated static readonly array. diff --git a/src/ComputeSharp.D2D1.WinUI/Extensions/ID2D1EffectExtensions.cs b/src/ComputeSharp.D2D1.WinUI/Extensions/ID2D1EffectExtensions.cs index e7901037..28fa310d 100644 --- a/src/ComputeSharp.D2D1.WinUI/Extensions/ID2D1EffectExtensions.cs +++ b/src/ComputeSharp.D2D1.WinUI/Extensions/ID2D1EffectExtensions.cs @@ -25,7 +25,7 @@ internal static unsafe class ID2D1EffectExtensions public static T GetConstantBuffer(this ref ID2D1Effect d2D1Effect) where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - int constantBufferSize = D2D1PixelShader.GetConstantBufferSize(); + int constantBufferSize = T.ConstantBufferSize; // Special cases the constant buffer type being empty. If that's the case, there is no // need to try to retrieve the constant buffer from the underlying effect anyway, and @@ -67,7 +67,7 @@ internal static unsafe class ID2D1EffectExtensions // avoid crashing when someone's just setting an empty constant buffer, which is // unnecessary but still valid (eg. a stateless shader). This could be the case if // someone was eg. binding or using some other automated system to set constant buffer. - if (D2D1PixelShader.GetConstantBufferSize() == 0) + if (T.ConstantBufferSize == 0) { return; } diff --git a/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs b/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs index b1ee3d9a..d1b23d9a 100644 --- a/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs +++ b/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs @@ -2,7 +2,6 @@ using System; using System.Runtime.CompilerServices; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; -using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.WinUI.Extensions; using ComputeSharp.D2D1.WinUI.Helpers; using ComputeSharp.Win32; @@ -63,7 +62,7 @@ unsafe partial class PixelShaderEffect if (!this.isEffectFactoryRegistered) { ResourceManager.RegisterEffectFactory( - effectId: D2D1PixelShaderEffect.GetEffectId(), + effectId: T.EffectId, factory: new EffectFactory()); this.isEffectFactoryRegistered = true; diff --git a/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.ICanvasImageInterop.cs index df2a995a..0d729c98 100644 --- a/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -250,7 +250,7 @@ unsafe partial class PixelShaderEffect { fixed (ID2D1Effect** d2D1Effect = this.d2D1Effect) { - Guid effectId = D2D1PixelShaderEffect.GetEffectId(); + Guid effectId = T.EffectId; using ComPtr d2D1DeviceContextEffective = default; using ComPtr d2D1DeviceContextLease = default; diff --git a/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs b/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs index 3cd6822c..cfd1947b 100644 --- a/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs +++ b/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs @@ -223,11 +223,9 @@ partial class PixelShaderEffect /// The bitmask of indices for resource textures in the target shader. private static int GetIndexBitmask() { - Unsafe.SkipInit(out T shader); - int indexBitmask = 0; - foreach (ref readonly D2D1ResourceTextureDescription description in shader.ResourceTextureDescriptions.Span) + foreach (ref readonly D2D1ResourceTextureDescription description in T.ResourceTextureDescriptions.Span) { indexBitmask |= 1 << description.Index; } @@ -241,9 +239,7 @@ partial class PixelShaderEffect /// The collection of valid indices for the current effect. private static ImmutableArray GetIndices() { - Unsafe.SkipInit(out T shader); - - ReadOnlySpan descriptions = shader.ResourceTextureDescriptions.Span; + ReadOnlySpan descriptions = T.ResourceTextureDescriptions.Span; // Skip the array allocation if there are no resource texture descriptions if (descriptions.IsEmpty) diff --git a/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.Properties.SourceCollection.cs b/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.Properties.SourceCollection.cs index 2b063129..688292de 100644 --- a/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.Properties.SourceCollection.cs +++ b/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.Properties.SourceCollection.cs @@ -3,7 +3,6 @@ using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; using System.Runtime.CompilerServices; -using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.WinUI.Buffers; using ComputeSharp.D2D1.WinUI.Collections; using Windows.Graphics.Effects; @@ -41,7 +40,7 @@ partial class PixelShaderEffect public int Count { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => D2D1PixelShader.GetInputCount(); + get => T.InputCount; } /// @@ -49,13 +48,13 @@ partial class PixelShaderEffect { get { - default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, D2D1PixelShader.GetInputCount(), nameof(index)); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, T.InputCount, nameof(index)); return Owner.GetSource(index); } set { - default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, D2D1PixelShader.GetInputCount(), nameof(index)); + default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, T.InputCount, nameof(index)); Owner.SetSource(value, index); } @@ -210,7 +209,7 @@ partial class PixelShaderEffect /// The collection of valid indices for the current effect. private static ImmutableArray GetIndices() { - int inputCount = D2D1PixelShader.GetInputCount(); + int inputCount = T.InputCount; if (inputCount == 0) { diff --git a/src/ComputeSharp.D2D1/Interfaces/Descriptors/ID2D1PixelShaderDescriptor{T}.cs b/src/ComputeSharp.D2D1/Interfaces/Descriptors/ID2D1PixelShaderDescriptor{T}.cs index ac06889c..fd010abc 100644 --- a/src/ComputeSharp.D2D1/Interfaces/Descriptors/ID2D1PixelShaderDescriptor{T}.cs +++ b/src/ComputeSharp.D2D1/Interfaces/Descriptors/ID2D1PixelShaderDescriptor{T}.cs @@ -18,7 +18,7 @@ public interface ID2D1PixelShaderDescriptor /// /// This only applies to effects created from . /// - ref readonly Guid EffectId { get; } + static abstract ref readonly Guid EffectId { get; } /// /// Gets the display name of the D2D effect using this shader, if specified. @@ -26,7 +26,7 @@ public interface ID2D1PixelShaderDescriptor /// /// This only applies to effects created from . /// - string? EffectDisplayName { get; } + static abstract string? EffectDisplayName { get; } /// /// Gets the description of the D2D effect using this shader, if specified. @@ -34,7 +34,7 @@ public interface ID2D1PixelShaderDescriptor /// /// This only applies to effects created from . /// - string? EffectDescription { get; } + static abstract string? EffectDescription { get; } /// /// Gets the category of the D2D effect using this shader, if specified. @@ -42,7 +42,7 @@ public interface ID2D1PixelShaderDescriptor /// /// This only applies to effects created from . /// - string? EffectCategory { get; } + static abstract string? EffectCategory { get; } /// /// Gets the author of the D2D effect using this shader, if specified. @@ -50,52 +50,52 @@ public interface ID2D1PixelShaderDescriptor /// /// This only applies to effects created from . /// - string? EffectAuthor { get; } + static abstract string? EffectAuthor { get; } /// /// Gets the size in bytes of the constant buffer for the current shader. /// - int ConstantBufferSize { get; } + static abstract int ConstantBufferSize { get; } /// /// Gets the number of inputs for the current shader. /// - int InputCount { get; } + static abstract int InputCount { get; } /// /// Gets the number of resource textures for the current shader. /// - int ResourceTextureCount { get; } + static abstract int ResourceTextureCount { get; } /// /// Gets the input types for the current shader. /// - ReadOnlyMemory InputTypes { get; } + static abstract ReadOnlyMemory InputTypes { get; } /// /// Gets the input descriptions for the current shader. /// - ReadOnlyMemory InputDescriptions { get; } + static abstract ReadOnlyMemory InputDescriptions { get; } /// /// Gets the resource texture descriptions for the shader. /// - ReadOnlyMemory ResourceTextureDescriptions { get; } + static abstract ReadOnlyMemory ResourceTextureDescriptions { get; } /// /// Gets the pixel options for the current shader. /// - D2D1PixelOptions PixelOptions { get; } + static abstract D2D1PixelOptions PixelOptions { get; } /// /// Gets the buffer precision for the shader. /// - D2D1BufferPrecision BufferPrecision { get; } + static abstract D2D1BufferPrecision BufferPrecision { get; } /// /// Gets the channel depth for the shader. /// - D2D1ChannelDepth ChannelDepth { get; } + static abstract D2D1ChannelDepth ChannelDepth { get; } /// /// Gets the shader profile for the current shader. @@ -103,7 +103,7 @@ public interface ID2D1PixelShaderDescriptor /// /// This shader profile is either explicitly set by users, or the default one for D2D1 shaders. /// - D2D1ShaderProfile ShaderProfile { get; } + static abstract D2D1ShaderProfile ShaderProfile { get; } /// /// Gets the compile options for the current shader. @@ -111,12 +111,12 @@ public interface ID2D1PixelShaderDescriptor /// /// This compile options are either explicitly set by users, or the default one for D2D1 shaders. /// - D2D1CompileOptions CompileOptions { get; } + static abstract D2D1CompileOptions CompileOptions { get; } /// /// Gets the HLSL source code for the current shader instance. /// - string HlslSource { get; } + static abstract string HlslSource { get; } /// /// Gets the HLSL bytecode for the current shader, if available. @@ -124,14 +124,14 @@ public interface ID2D1PixelShaderDescriptor /// /// If no precompiled HLSL bytecode is available, the returned instance will be empty. /// - ReadOnlyMemory HlslBytecode { get; } + static abstract ReadOnlyMemory HlslBytecode { get; } /// /// Creates a new shader instance from a constant buffer. /// /// The input constant buffer to read. /// The input buffer must be retrieved from . - T CreateFromConstantBuffer(ReadOnlySpan buffer); + static abstract T CreateFromConstantBuffer(ReadOnlySpan buffer); /// /// Loads the constant buffer of a given input shader. @@ -139,6 +139,6 @@ public interface ID2D1PixelShaderDescriptor /// The type of data loader being used. /// The input shader to load the constant buffer for. /// The instance to use to load the constant buffer. - void LoadConstantBuffer(in T shader, ref TLoader loader) + static abstract void LoadConstantBuffer(in T shader, ref TLoader loader) where TLoader : struct, ID2D1ConstantBufferLoader; } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1DrawInfoUpdateContext{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1DrawInfoUpdateContext{T}.cs index a0064dae..58c5dda7 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1DrawInfoUpdateContext{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1DrawInfoUpdateContext{T}.cs @@ -1,5 +1,4 @@ using System.Buffers; -using System.Runtime.CompilerServices; using ComputeSharp.D2D1.Descriptors; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; @@ -45,9 +44,7 @@ public readonly unsafe ref struct D2D1DrawInfoUpdateContext this.d2D1DrawInfoUpdateContext->GetConstantBuffer(pBuffer, constantBufferSize).Assert(); } - Unsafe.SkipInit(out T shader); - - shader = shader.CreateFromConstantBuffer(buffer); + T shader = T.CreateFromConstantBuffer(buffer); ArrayPool.Shared.Return(buffer); @@ -62,6 +59,6 @@ public readonly unsafe ref struct D2D1DrawInfoUpdateContext { D2D1DrawInfoUpdateContextConstantBufferLoader dataLoader = new(this.d2D1DrawInfoUpdateContext); - Unsafe.AsRef(in shader).LoadConstantBuffer(in shader, ref dataLoader); + T.LoadConstantBuffer(in shader, ref dataLoader); } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index f6e18354..b5d8b4c8 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -20,9 +20,7 @@ public static class D2D1PixelShader public static int GetConstantBufferSize() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - - return shader.ConstantBufferSize; + return T.ConstantBufferSize; } /// @@ -33,9 +31,7 @@ public static class D2D1PixelShader public static int GetInputCount() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - - return shader.InputCount; + return T.InputCount; } /// @@ -46,9 +42,7 @@ public static class D2D1PixelShader public static int GetResourceTextureCount() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - - return shader.ResourceTextureCount; + return T.ResourceTextureCount; } /// @@ -59,9 +53,7 @@ public static class D2D1PixelShader public static ReadOnlyMemory GetInputTypes() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - - return shader.InputTypes; + return T.InputTypes; } /// @@ -72,9 +64,7 @@ public static class D2D1PixelShader public static ReadOnlyMemory GetInputDescriptions() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - - return shader.InputDescriptions; + return T.InputDescriptions; } /// @@ -85,9 +75,7 @@ public static class D2D1PixelShader public static ReadOnlyMemory GetResourceTextureDescriptions() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - - return shader.ResourceTextureDescriptions; + return T.ResourceTextureDescriptions; } /// @@ -98,9 +86,7 @@ public static class D2D1PixelShader public static D2D1PixelOptions GetPixelOptions() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - - return shader.PixelOptions; + return T.PixelOptions; } /// @@ -111,9 +97,7 @@ public static class D2D1PixelShader public static D2D1BufferPrecision GetOutputBufferPrecision() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - - return shader.BufferPrecision; + return T.BufferPrecision; } /// @@ -124,9 +108,7 @@ public static class D2D1PixelShader public static D2D1ChannelDepth GetOutputBufferChannelDepth() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - - return shader.ChannelDepth; + return T.ChannelDepth; } /// @@ -144,13 +126,11 @@ public static class D2D1PixelShader public static ReadOnlyMemory LoadBytecode() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - // This method is the one used by the built-in ID2D1Effect, so it has special optimization // to help both performance and especially trimming. That is, we first check whether the // HLSL code is already available, which should get completely inlined when using the // generated code. This allows the entire fallback path below to be trimmed out. - if (shader.HlslBytecode is ReadOnlyMemory { Length: > 0 } hlslBytecode) + if (T.HlslBytecode is ReadOnlyMemory { Length: > 0 } hlslBytecode) { return hlslBytecode; } @@ -334,16 +314,12 @@ public static class D2D1PixelShader out D2D1CompileOptions effectiveCompileOptions) where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - - string hlslSource = shader.HlslSource; - // Set the effective profile and option to the requested ones or the default values - effectiveShaderProfile = requestedShaderProfile ?? shader.ShaderProfile; - effectiveCompileOptions = requestedCompileOptions ?? shader.CompileOptions; + effectiveShaderProfile = requestedShaderProfile ?? T.ShaderProfile; + effectiveCompileOptions = requestedCompileOptions ?? T.CompileOptions; // Compile the shader with the current settings - using ComPtr dynamicBytecode = D3DCompiler.Compile(hlslSource.AsSpan(), effectiveShaderProfile, effectiveCompileOptions); + using ComPtr dynamicBytecode = D3DCompiler.Compile(T.HlslSource, effectiveShaderProfile, effectiveCompileOptions); byte* bytecodePtr = (byte*)dynamicBytecode.Get()->GetBufferPointer(); int bytecodeSize = (int)dynamicBytecode.Get()->GetBufferSize(); @@ -367,15 +343,13 @@ public static class D2D1PixelShader out D2D1CompileOptions effectiveCompileOptions) where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - // Check whether there is precompiled bytecode we can use - if (shader.HlslBytecode is ReadOnlyMemory { Length: > 0 } hlslBytecode && - (requestedShaderProfile is null || shader.ShaderProfile == requestedShaderProfile.GetValueOrDefault()) && - (requestedCompileOptions is null || shader.CompileOptions == requestedCompileOptions.GetValueOrDefault())) + if (T.HlslBytecode is ReadOnlyMemory { Length: > 0 } hlslBytecode && + (requestedShaderProfile is null || T.ShaderProfile == requestedShaderProfile.GetValueOrDefault()) && + (requestedCompileOptions is null || T.CompileOptions == requestedCompileOptions.GetValueOrDefault())) { - effectiveShaderProfile = shader.ShaderProfile; - effectiveCompileOptions = shader.CompileOptions; + effectiveShaderProfile = T.ShaderProfile; + effectiveCompileOptions = T.CompileOptions; return hlslBytecode; } @@ -398,7 +372,7 @@ public static class D2D1PixelShader { D2D1ByteArrayConstantBufferLoader dataLoader = default; - Unsafe.AsRef(in shader).LoadConstantBuffer(in shader, ref dataLoader); + T.LoadConstantBuffer(in shader, ref dataLoader); return dataLoader.GetResultingDispatchData(); } @@ -437,7 +411,7 @@ public static class D2D1PixelShader { D2D1ByteBufferConstantBufferLoader dataLoader = new(buffer, span.Length); - Unsafe.AsRef(in shader).LoadConstantBuffer(in shader, ref dataLoader); + T.LoadConstantBuffer(in shader, ref dataLoader); return dataLoader.TryGetWrittenBytes(out bytesWritten); } @@ -458,7 +432,7 @@ public static class D2D1PixelShader D2D1DrawInfoConstantBufferLoader dataLoader = new((ID2D1DrawInfo*)d2D1DrawInfo); - Unsafe.AsRef(in shader).LoadConstantBuffer(in shader, ref dataLoader); + T.LoadConstantBuffer(in shader, ref dataLoader); } /// @@ -490,11 +464,9 @@ public static class D2D1PixelShader public static bool TryCreateFromConstantBuffer(ReadOnlySpan span, out T shader) where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out shader); - - if (span.Length >= shader.ConstantBufferSize) + if (span.Length >= T.ConstantBufferSize) { - shader = shader.CreateFromConstantBuffer(span); + shader = T.CreateFromConstantBuffer(span); return true; } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index 1edea605..efd78524 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -1,6 +1,5 @@ using System; using System.Buffers; -using System.Runtime.CompilerServices; using System.Text; using ComputeSharp.D2D1.Descriptors; using ComputeSharp.D2D1.Extensions; @@ -26,9 +25,7 @@ public static unsafe class D2D1PixelShaderEffect public static ref readonly Guid GetEffectId() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - - return ref shader.EffectId; + return ref T.EffectId; } /// @@ -39,9 +36,7 @@ public static unsafe class D2D1PixelShaderEffect public static string? GetEffectDisplayName() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - - return shader.EffectDisplayName; + return T.EffectDisplayName; } /// @@ -52,9 +47,7 @@ public static unsafe class D2D1PixelShaderEffect public static string? GetEffectDescription() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - - return shader.EffectDescription; + return T.EffectDescription; } /// @@ -65,9 +58,7 @@ public static unsafe class D2D1PixelShaderEffect public static string? GetEffectCategory() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - - return shader.EffectCategory; + return T.EffectCategory; } /// @@ -78,9 +69,7 @@ public static unsafe class D2D1PixelShaderEffect public static string? GetEffectAuthor() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - - return shader.EffectAuthor; + return T.EffectAuthor; } /// @@ -194,7 +183,7 @@ public static unsafe class D2D1PixelShaderEffect classId: pGuid, propertyXml: (ushort*)pXml, bindings: d2D1PropertyBinding, - bindingsCount: (uint)(D2D1PixelShaderEffectProperty.NumberOfAlwaysAvailableProperties + D2D1PixelShader.GetResourceTextureCount()), + bindingsCount: (uint)(D2D1PixelShaderEffectProperty.NumberOfAlwaysAvailableProperties + T.ResourceTextureCount), effectFactory: PixelShaderEffect.Globals.Instance.Factory).Assert(); } @@ -249,8 +238,8 @@ public static unsafe class D2D1PixelShaderEffect writer.Write(D2D1EffectRegistrationData.V1.BlobId); // Effect id and number of inputs - writer.Write(GetEffectId()); - writer.Write(D2D1PixelShader.GetInputCount()); + writer.Write(T.EffectId); + writer.Write(T.InputCount); // Retrieve the effect XML and include it into the registration blob using (D2D1EffectXmlFactory.EffectXml effectXml = D2D1EffectXmlFactory.GetXmlBuffer()) @@ -272,7 +261,7 @@ public static unsafe class D2D1PixelShaderEffect } // Bindings - writer.Write(D2D1PixelShaderEffectProperty.NumberOfAlwaysAvailableProperties + D2D1PixelShader.GetResourceTextureCount()); + writer.Write(D2D1PixelShaderEffectProperty.NumberOfAlwaysAvailableProperties + T.ResourceTextureCount); writer.Write("ConstantBuffer"u8); writer.Write((byte)'\0'); writer.Write((nint)(delegate* unmanaged)&PixelShaderEffect.GetConstantBufferImpl); @@ -283,7 +272,7 @@ public static unsafe class D2D1PixelShaderEffect writer.Write((nint)(delegate* unmanaged)&PixelShaderEffect.SetTransformMapperImpl); // Add all resource texture manager property bindings - for (int i = 0; i < D2D1PixelShader.GetResourceTextureCount(); i++) + for (int i = 0; i < T.ResourceTextureCount; i++) { switch (i) { @@ -441,7 +430,7 @@ public static unsafe class D2D1PixelShaderEffect D2D1EffectConstantBufferLoader dataLoader = new((ID2D1Effect*)d2D1Effect); - Unsafe.AsRef(in shader).LoadConstantBuffer(in shader, ref dataLoader); + T.LoadConstantBuffer(in shader, ref dataLoader); } /// diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs index 29696e99..164b45f2 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ReflectionServices.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.CompilerServices; using ComputeSharp.D2D1.Descriptors; using ComputeSharp.D2D1.Extensions; using ComputeSharp.Win32; @@ -27,8 +26,6 @@ public static class D2D1ReflectionServices public static unsafe D2D1ShaderInfo GetShaderInfo() where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - Unsafe.SkipInit(out T shader); - ReadOnlyMemory hlslBytecode = D2D1PixelShader.LoadBytecode(); using ComPtr d3D11ShaderReflection = default; @@ -52,7 +49,7 @@ public static class D2D1ReflectionServices return new( CompilerVersion: new string(d3D11ShaderDescription.Creator), - HlslSource: shader.HlslSource, + HlslSource: T.HlslSource, HlslBytecode: hlslBytecode, ConstantBufferCount: d3D11ShaderDescription.ConstantBuffers, BoundResourceCount: d3D11ShaderDescription.BoundResources, diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Globals{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Globals{T}.cs index 851056b1..008ca967 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Globals{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Globals{T}.cs @@ -107,34 +107,34 @@ internal unsafe partial struct PixelShaderEffect public static Globals Instance { get; } = new(); /// - public override ref readonly Guid EffectId => ref D2D1PixelShaderEffect.GetEffectId(); + public override ref readonly Guid EffectId => ref T.EffectId; /// - public override int ConstantBufferSize => D2D1PixelShader.GetConstantBufferSize(); + public override int ConstantBufferSize => T.ConstantBufferSize; /// - public override int InputCount => D2D1PixelShader.GetInputCount(); + public override int InputCount => T.InputCount; /// - public override int ResourceTextureCount => D2D1PixelShader.GetResourceTextureCount(); + public override int ResourceTextureCount => T.ResourceTextureCount; /// - public override ReadOnlyMemory InputTypes => D2D1PixelShader.GetInputTypes(); + public override ReadOnlyMemory InputTypes => T.InputTypes; /// - public override ReadOnlyMemory InputDescriptions => D2D1PixelShader.GetInputDescriptions(); + public override ReadOnlyMemory InputDescriptions => T.InputDescriptions; /// - public override ReadOnlyMemory ResourceTextureDescriptions => D2D1PixelShader.GetResourceTextureDescriptions(); + public override ReadOnlyMemory ResourceTextureDescriptions => T.ResourceTextureDescriptions; /// - public override D2D1PixelOptions PixelOptions => D2D1PixelShader.GetPixelOptions(); + public override D2D1PixelOptions PixelOptions => T.PixelOptions; /// - public override D2D1BufferPrecision BufferPrecision => D2D1PixelShader.GetOutputBufferPrecision(); + public override D2D1BufferPrecision BufferPrecision => T.BufferPrecision; /// - public override D2D1ChannelDepth ChannelDepth => D2D1PixelShader.GetOutputBufferChannelDepth(); + public override D2D1ChannelDepth ChannelDepth => T.ChannelDepth; /// public override ReadOnlyMemory HlslBytecode diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1EffectXmlFactory.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1EffectXmlFactory.cs index 1e322f48..912c7553 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1EffectXmlFactory.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1EffectXmlFactory.cs @@ -26,12 +26,12 @@ internal static unsafe class D2D1EffectXmlFactory where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { // Compute all necessary values first, outside of the lock - string? displayName = D2D1PixelShaderEffect.GetEffectDisplayName(); - string? description = D2D1PixelShaderEffect.GetEffectDescription(); - string? category = D2D1PixelShaderEffect.GetEffectCategory(); - string? author = D2D1PixelShaderEffect.GetEffectAuthor(); - int inputCount = D2D1PixelShader.GetInputCount(); - int resourceTextureCount = D2D1PixelShader.GetResourceTextureCount(); + string? displayName = T.EffectDisplayName; + string? description = T.EffectDescription; + string? category = T.EffectCategory; + string? author = T.EffectAuthor; + int inputCount = T.InputCount; + int resourceTextureCount = T.ResourceTextureCount; StringBuilder builder = XmlBuilder; diff --git a/src/ComputeSharp.Dxc/Interop/ReflectionServices.cs b/src/ComputeSharp.Dxc/Interop/ReflectionServices.cs index 297a8ba0..60bb70a5 100644 --- a/src/ComputeSharp.Dxc/Interop/ReflectionServices.cs +++ b/src/ComputeSharp.Dxc/Interop/ReflectionServices.cs @@ -43,9 +43,7 @@ public static partial class ReflectionServices private static unsafe ShaderInfo GetNonGenericShaderInfo() where T : struct, IComputeShaderDescriptor { - Unsafe.SkipInit(out T shader); - - ReadOnlyMemory hlslBytecode = shader.HlslBytecode; + ReadOnlyMemory hlslBytecode = T.HlslBytecode; using ComPtr dxcUtils = default; @@ -77,7 +75,7 @@ public static partial class ReflectionServices return new( CompilerVersion: new string(d3D12ShaderDescription.Creator), - HlslSource: shader.HlslSource, + HlslSource: T.HlslSource, HlslBytecode: hlslBytecode, ConstantBufferCount: d3D12ShaderDescription.ConstantBuffers, BoundResourceCount: d3D12ShaderDescription.BoundResources, diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs index 7a8669d7..87ed9c4d 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs @@ -23,7 +23,7 @@ partial class ComputeShaderDescriptorGenerator writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]"); - writer.WriteLine($"readonly void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadConstantBuffer(in {typeName} shader, ref TLoader loader, int x, int y, int z)"); + writer.WriteLine($"static void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadConstantBuffer(in {typeName} shader, ref TLoader loader, int x, int y, int z)"); using (writer.WriteBlock()) { @@ -99,7 +99,7 @@ partial class ComputeShaderDescriptorGenerator writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadGraphicsResources(in {typeName} shader, ref TLoader loader)"); + writer.WriteLine($"static void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadGraphicsResources(in {typeName} shader, ref TLoader loader)"); using (writer.WriteBlock()) { diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs index dcee6b1c..15b1ee5c 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.Syntax.cs @@ -20,7 +20,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.Write($"readonly global::System.ReadOnlyMemory global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.HlslBytecode => "); + writer.Write($"static global::System.ReadOnlyMemory global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.HlslBytecode => "); // Just like with the D2D1 generator, only return the MemoryManager if we do have HLSL bytecode if (info.HlslInfo is not HlslBytecodeInfo.Success) @@ -119,41 +119,5 @@ partial class ComputeShaderDescriptorGenerator callbacks.Add(Callback); } - - /// - /// Writes the ThreadsX property. - /// - /// The input instance with gathered shader info. - /// The instance to write into. - public static void WriteThreadsXSyntax(ShaderInfo info, IndentedTextWriter writer) - { - writer.WriteLine("/// "); - writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsX => {info.ThreadsX};"); - } - - /// - /// Writes the ThreadsY property. - /// - /// The input instance with gathered shader info. - /// The instance to write into. - public static void WriteThreadsYSyntax(ShaderInfo info, IndentedTextWriter writer) - { - writer.WriteLine("/// "); - writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsY => {info.ThreadsY};"); - } - - /// - /// Writes the ThreadsZ property. - /// - /// The input instance with gathered shader info. - /// The instance to write into. - public static void WriteThreadsZSyntax(ShaderInfo info, IndentedTextWriter writer) - { - writer.WriteLine("/// "); - writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsZ => {info.ThreadsZ};"); - } } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.Syntax.cs index fdb68dae..1dd1c909 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.Syntax.cs @@ -19,7 +19,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly string global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.HlslSource =>"); + writer.WriteLine($"static string global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.HlslSource =>"); writer.IncreaseIndent(); writer.WriteLine("\"\"\""); writer.Write(info.HlslInfoKey.HlslSource, isMultiline: true); diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.MetadataProperties.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.MetadataProperties.Syntax.cs index c13050d1..a4eabb67 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.MetadataProperties.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.MetadataProperties.Syntax.cs @@ -21,7 +21,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ConstantBufferSize => {info.ConstantBufferSizeInBytes};"); + writer.WriteLine($"static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ConstantBufferSize => {info.ConstantBufferSizeInBytes};"); } /// @@ -33,7 +33,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly bool global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.IsStaticSamplerRequired => {info.IsSamplerUsed.ToString().ToLowerInvariant()};"); + writer.WriteLine($"static bool global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.IsStaticSamplerRequired => {info.IsSamplerUsed.ToString().ToLowerInvariant()};"); } } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.NumThreads.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.NumThreads.Syntax.cs index deafb48c..d5ede4da 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.NumThreads.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.NumThreads.Syntax.cs @@ -19,7 +19,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsX => {info.ThreadsX};"); + writer.WriteLine($"static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsX => {info.ThreadsX};"); } /// @@ -31,7 +31,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsY => {info.ThreadsY};"); + writer.WriteLine($"static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsY => {info.ThreadsY};"); } /// @@ -43,7 +43,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsZ => {info.ThreadsZ};"); + writer.WriteLine($"static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsZ => {info.ThreadsZ};"); } } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.Syntax.cs index 4a86db94..56e237c1 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ResourceDescriptorRanges.Syntax.cs @@ -19,7 +19,7 @@ partial class ComputeShaderDescriptorGenerator { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); - writer.Write($"readonly global::System.ReadOnlyMemory global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ResourceDescriptorRanges => "); + writer.Write($"static global::System.ReadOnlyMemory global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ResourceDescriptorRanges => "); // If there are no declared resources, just return an empty collection if (info.ResourceDescriptors.IsEmpty) diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs index ce59ce78..c4a64b2c 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs @@ -144,9 +144,9 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener { using ImmutableArrayBuilder> declaredMembers = new(); - declaredMembers.Add(HlslBytecode.WriteThreadsXSyntax); - declaredMembers.Add(HlslBytecode.WriteThreadsYSyntax); - declaredMembers.Add(HlslBytecode.WriteThreadsZSyntax); + declaredMembers.Add(NumThreads.WriteThreadsXSyntax); + declaredMembers.Add(NumThreads.WriteThreadsYSyntax); + declaredMembers.Add(NumThreads.WriteThreadsZSyntax); declaredMembers.Add(MetadataProperties.WriteConstantBufferSizeSyntax); declaredMembers.Add(MetadataProperties.WriteIsStaticSamplerRequiredSyntax); declaredMembers.Add(ResourceDescriptorRanges.WriteSyntax); diff --git a/src/ComputeSharp/Core/Interfaces/Descriptors/IComputeShaderDescriptor{T}.cs b/src/ComputeSharp/Core/Interfaces/Descriptors/IComputeShaderDescriptor{T}.cs index 55a550b6..2f1ae452 100644 --- a/src/ComputeSharp/Core/Interfaces/Descriptors/IComputeShaderDescriptor{T}.cs +++ b/src/ComputeSharp/Core/Interfaces/Descriptors/IComputeShaderDescriptor{T}.cs @@ -13,19 +13,19 @@ public interface IComputeShaderDescriptor where T : struct { /// - /// The number of threads in each thread group for the X axis. + /// Gets the number of threads in each thread group for the X axis. /// - int ThreadsX { get; } + static abstract int ThreadsX { get; } /// - /// The number of threads in each thread group for the Y axis. + /// Gets the number of threads in each thread group for the Y axis. /// - int ThreadsY { get; } + static abstract int ThreadsY { get; } /// - /// The number of threads in each thread group for the Z axis. + /// Gets the number of threads in each thread group for the Z axis. /// - int ThreadsZ { get; } + static abstract int ThreadsZ { get; } /// /// Gets the size in bytes of the constant buffer for the current shader. @@ -35,7 +35,7 @@ public interface IComputeShaderDescriptor /// ID3D12GraphicsCommandList::SetComputeRoot32BitConstants, /// so the size must be a multiple of the size of a DWORD value (ie. 4 bytes). /// - int ConstantBufferSize { get; } + static abstract int ConstantBufferSize { get; } /// /// Gets whether the shader requires a static sampler being declared and initialized. @@ -43,22 +43,22 @@ public interface IComputeShaderDescriptor /// /// When requested, a static sampler with linear sampling and mirror addressing will be bound to register(s). /// - bool IsStaticSamplerRequired { get; } + static abstract bool IsStaticSamplerRequired { get; } /// /// Gets the resource descriptor ranges for all resources that should be bound to this shader. /// - ReadOnlyMemory ResourceDescriptorRanges { get; } + static abstract ReadOnlyMemory ResourceDescriptorRanges { get; } /// /// Gets the HLSL source code for the current shader instance. /// - string HlslSource { get; } + static abstract string HlslSource { get; } /// /// Gets the HLSL bytecode for the current shader. /// - ReadOnlyMemory HlslBytecode { get; } + static abstract ReadOnlyMemory HlslBytecode { get; } /// /// Loads the constant buffer of a given input shader. @@ -69,7 +69,7 @@ public interface IComputeShaderDescriptor /// The number of iterations to run on the X axis. /// The number of iterations to run on the Y axis. /// The number of iterations to run on the Z axis. - void LoadConstantBuffer(in T shader, ref TLoader loader, int x, int y, int z) + static abstract void LoadConstantBuffer(in T shader, ref TLoader loader, int x, int y, int z) where TLoader : struct, IConstantBufferLoader; /// @@ -78,6 +78,6 @@ public interface IComputeShaderDescriptor /// The type of graphics resource loader being used. /// The input shader to load the graphics resources for. /// The instance to use to load the data. - void LoadGraphicsResources(in T shader, ref TLoader loader) + static abstract void LoadGraphicsResources(in T shader, ref TLoader loader) where TLoader : struct, IGraphicsResourceLoader; } \ No newline at end of file diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index 468a8995..cc50e76b 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -157,9 +157,9 @@ public struct ComputeContext : IDisposable, IAsyncDisposable default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(y); default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(z); - int groupsX = Math.DivRem(x, shader.ThreadsX, out int modX) + (modX == 0 ? 0 : 1); - int groupsY = Math.DivRem(y, shader.ThreadsY, out int modY) + (modY == 0 ? 0 : 1); - int groupsZ = Math.DivRem(z, shader.ThreadsZ, out int modZ) + (modZ == 0 ? 0 : 1); + int groupsX = Math.DivRem(x, T.ThreadsX, out int modX) + (modX == 0 ? 0 : 1); + int groupsY = Math.DivRem(y, T.ThreadsY, out int modY) + (modY == 0 ? 0 : 1); + int groupsZ = Math.DivRem(z, T.ThreadsZ, out int modZ) + (modZ == 0 ? 0 : 1); default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION); default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); @@ -173,11 +173,11 @@ public struct ComputeContext : IDisposable, IAsyncDisposable D3D12GraphicsCommandListConstantBufferLoader dataLoader = new(commandList.D3D12GraphicsCommandList); - shader.LoadConstantBuffer(in shader, ref dataLoader, x, y, z); + T.LoadConstantBuffer(in shader, ref dataLoader, x, y, z); D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device, rootParameterOffset: 1); - shader.LoadGraphicsResources(in shader, ref graphicsResourceLoader); + T.LoadGraphicsResources(in shader, ref graphicsResourceLoader); commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, (uint)groupsZ); } @@ -197,8 +197,8 @@ public struct ComputeContext : IDisposable, IAsyncDisposable int x = texture.Width; int y = texture.Height; - int groupsX = Math.DivRem(x, shader.ThreadsX, out int modX) + (modX == 0 ? 0 : 1); - int groupsY = Math.DivRem(y, shader.ThreadsY, out int modY) + (modY == 0 ? 0 : 1); + int groupsX = Math.DivRem(x, T.ThreadsX, out int modX) + (modX == 0 ? 0 : 1); + int groupsY = Math.DivRem(y, T.ThreadsY, out int modY) + (modY == 0 ? 0 : 1); default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION); default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); @@ -211,11 +211,11 @@ public struct ComputeContext : IDisposable, IAsyncDisposable D3D12GraphicsCommandListConstantBufferLoader constantBufferLoader = new(commandList.D3D12GraphicsCommandList); - shader.LoadConstantBuffer(in shader, ref constantBufferLoader, x, y, 1); + T.LoadConstantBuffer(in shader, ref constantBufferLoader, x, y, 1); D3D12GraphicsCommandListGraphicsResourceLoader graphicsResourceLoader = new(commandList.D3D12GraphicsCommandList, this.device, rootParameterOffset: 2); - shader.LoadGraphicsResources(in shader, ref graphicsResourceLoader); + T.LoadGraphicsResources(in shader, ref graphicsResourceLoader); // Load the implicit output texture commandList.D3D12GraphicsCommandList->SetComputeRootDescriptorTable( diff --git a/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs b/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs index 0a2d8c94..72438276 100644 --- a/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs +++ b/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs @@ -65,9 +65,7 @@ internal static unsafe class PipelineDataLoader /// The allocated instance. private static void CreateD3D12RootSignature(ID3D12Device* d3D12Device, ID3D12RootSignature** d3D12RootSignature) { - Unsafe.SkipInit(out T shader); - - ReadOnlySpan resourceDescriptorRanges = shader.ResourceDescriptorRanges.Span; + ReadOnlySpan resourceDescriptorRanges = T.ResourceDescriptorRanges.Span; D3D12_DESCRIPTOR_RANGE1[]? d3D12DescriptorRangesArray = null; @@ -94,13 +92,13 @@ internal static unsafe class PipelineDataLoader } // Calculate the actual size of the constant buffer in DWORD values - int root32BitConstantCount = shader.ConstantBufferSize / sizeof(int); + int root32BitConstantCount = T.ConstantBufferSize / sizeof(int); // Create the D3D12 root signature d3D12Device->CreateRootSignature( root32BitConstantCount, d3D12DescriptorRanges, - shader.IsStaticSamplerRequired, + T.IsStaticSamplerRequired, d3D12RootSignature); // Return the array to the pool if no exceptions have been thrown @@ -118,11 +116,9 @@ internal static unsafe class PipelineDataLoader /// The resulting instance. private static void CreateD3D12PipelineState(ID3D12Device* d3D12Device, ID3D12RootSignature* d3D12RootSignature, ID3D12PipelineState** d3D12PipelineState) { - Unsafe.SkipInit(out T shader); - - fixed (byte* hlslBytecodePtr = shader.HlslBytecode.Span) + fixed (byte* hlslBytecodePtr = T.HlslBytecode.Span) { - D3D12_SHADER_BYTECODE d3D12ShaderBytecode = new(hlslBytecodePtr, (nuint)shader.HlslBytecode.Length); + D3D12_SHADER_BYTECODE d3D12ShaderBytecode = new(hlslBytecodePtr, (nuint)T.HlslBytecode.Length); d3D12Device->CreateComputePipelineState(d3D12RootSignature, d3D12ShaderBytecode, d3D12PipelineState); } diff --git a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs index 105203c4..0b57e733 100644 --- a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs +++ b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs @@ -1,5 +1,7 @@ +using System; using System.Runtime.CompilerServices; using ComputeSharp.Descriptors; +using ComputeSharp.Interop; using ComputeSharp.Tests.Internals.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -29,18 +31,18 @@ public partial class ShaderDataLoaderTests CapturedResourceShader shader = new(buffer); DebugDispatchDataLoader dataLoader = new(); - ((IComputeShaderDescriptor)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); + LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); - Assert.AreEqual(12, ((IComputeShaderDescriptor)shader).ConstantBufferSize); + Assert.AreEqual(12, ConstantBufferSize()); Assert.IsNotNull(dataLoader.ConstantBuffer); Assert.AreEqual(12, dataLoader.ConstantBuffer.Length); Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); Assert.AreEqual(222, Unsafe.As(ref dataLoader.ConstantBuffer[4])); Assert.AreEqual(333, Unsafe.As(ref dataLoader.ConstantBuffer[8])); - ((IComputeShaderDescriptor)shader).LoadGraphicsResources(in shader, ref dataLoader); + LoadGraphicsResources(in shader, ref dataLoader); - Assert.AreEqual(1, ((IComputeShaderDescriptor)shader).ResourceDescriptorRanges.Length); + Assert.AreEqual(1, ResourceDescriptorRanges().Length); Assert.AreSame(buffer, dataLoader.GraphicsResources[0].Resource); Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); } @@ -70,9 +72,9 @@ public partial class ShaderDataLoaderTests MultipleResourcesAndPrimitivesShader shader = new(buffer0, buffer1, 1, 22, 77); DebugDispatchDataLoader dataLoader = new(); - ((IComputeShaderDescriptor)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); + LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); - Assert.AreEqual(24, ((IComputeShaderDescriptor)shader).ConstantBufferSize); + Assert.AreEqual(24, ConstantBufferSize()); Assert.IsNotNull(dataLoader.ConstantBuffer); Assert.AreEqual(24, dataLoader.ConstantBuffer.Length); Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); @@ -82,9 +84,9 @@ public partial class ShaderDataLoaderTests Assert.AreEqual(22, Unsafe.As(ref dataLoader.ConstantBuffer[16])); Assert.AreEqual(77, Unsafe.As(ref dataLoader.ConstantBuffer[20])); - ((IComputeShaderDescriptor)shader).LoadGraphicsResources(in shader, ref dataLoader); + LoadGraphicsResources(in shader, ref dataLoader); - Assert.AreEqual(2, ((IComputeShaderDescriptor)shader).ResourceDescriptorRanges.Length); + Assert.AreEqual(2, ResourceDescriptorRanges().Length); Assert.AreSame(buffer0, dataLoader.GraphicsResources[0].Resource); Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); Assert.AreSame(buffer1, dataLoader.GraphicsResources[1].Resource); @@ -117,9 +119,9 @@ public partial class ShaderDataLoaderTests ScalarAndVectorTypesShader shader = new(buffer, new(55, 44, 888), 22, 77, new(3.14, 6.28), 42, 9999); DebugDispatchDataLoader dataLoader = new(); - ((IComputeShaderDescriptor)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); + LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); - Assert.AreEqual(72, ((IComputeShaderDescriptor)shader).ConstantBufferSize); + Assert.AreEqual(72, ConstantBufferSize()); Assert.IsNotNull(dataLoader.ConstantBuffer); Assert.AreEqual(72, dataLoader.ConstantBuffer.Length); Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); @@ -135,9 +137,9 @@ public partial class ShaderDataLoaderTests Assert.AreEqual(42, Unsafe.As(ref dataLoader.ConstantBuffer[64])); Assert.AreEqual(9999, Unsafe.As(ref dataLoader.ConstantBuffer[68])); - ((IComputeShaderDescriptor)shader).LoadGraphicsResources(in shader, ref dataLoader); + LoadGraphicsResources(in shader, ref dataLoader); - Assert.AreEqual(1, ((IComputeShaderDescriptor)shader).ResourceDescriptorRanges.Length); + Assert.AreEqual(1, ResourceDescriptorRanges().Length); Assert.AreSame(buffer, dataLoader.GraphicsResources[0].Resource); Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); } @@ -179,9 +181,9 @@ public partial class ShaderDataLoaderTests d: 9999); DebugDispatchDataLoader dataLoader = new(); - ((IComputeShaderDescriptor)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); + LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); - Assert.AreEqual(124, ((IComputeShaderDescriptor)shader).ConstantBufferSize); + Assert.AreEqual(124, ConstantBufferSize()); Assert.IsNotNull(dataLoader.ConstantBuffer); Assert.AreEqual(124, dataLoader.ConstantBuffer.Length); Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); @@ -208,9 +210,9 @@ public partial class ShaderDataLoaderTests Assert.AreEqual(44, Unsafe.As(ref dataLoader.ConstantBuffer[116])); Assert.AreEqual(9999, Unsafe.As(ref dataLoader.ConstantBuffer[120])); - ((IComputeShaderDescriptor)shader).LoadGraphicsResources(in shader, ref dataLoader); + LoadGraphicsResources(in shader, ref dataLoader); - Assert.AreEqual(1, ((IComputeShaderDescriptor)shader).ResourceDescriptorRanges.Length); + Assert.AreEqual(1, ResourceDescriptorRanges().Length); Assert.AreSame(buffer, dataLoader.GraphicsResources[0].Resource); Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); } @@ -259,9 +261,9 @@ public partial class ShaderDataLoaderTests d: 9999)); DebugDispatchDataLoader dataLoader = new(); - ((IComputeShaderDescriptor)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); + LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); - Assert.AreEqual(124, ((IComputeShaderDescriptor)shader).ConstantBufferSize); + Assert.AreEqual(124, ConstantBufferSize()); Assert.IsNotNull(dataLoader.ConstantBuffer); Assert.AreEqual(124, dataLoader.ConstantBuffer.Length); Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); @@ -288,9 +290,9 @@ public partial class ShaderDataLoaderTests Assert.AreEqual(44, Unsafe.As(ref dataLoader.ConstantBuffer[116])); Assert.AreEqual(9999, Unsafe.As(ref dataLoader.ConstantBuffer[120])); - ((IComputeShaderDescriptor)shader).LoadGraphicsResources(in shader, ref dataLoader); + LoadGraphicsResources(in shader, ref dataLoader); - Assert.AreEqual(1, ((IComputeShaderDescriptor)shader).ResourceDescriptorRanges.Length); + Assert.AreEqual(1, ResourceDescriptorRanges().Length); Assert.AreSame(buffer, dataLoader.GraphicsResources[0].Resource); Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); } @@ -363,9 +365,9 @@ public partial class ShaderDataLoaderTests b: new(333.3f, 444.4f)))); DebugDispatchDataLoader dataLoader = new(); - ((IComputeShaderDescriptor)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); + LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); - Assert.AreEqual(188, ((IComputeShaderDescriptor)shader).ConstantBufferSize); + Assert.AreEqual(188, ConstantBufferSize()); Assert.IsNotNull(dataLoader.ConstantBuffer); Assert.AreEqual(188, dataLoader.ConstantBuffer.Length); Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); @@ -405,9 +407,9 @@ public partial class ShaderDataLoaderTests Assert.AreEqual(333.3f, Unsafe.As(ref dataLoader.ConstantBuffer[180])); Assert.AreEqual(444.4f, Unsafe.As(ref dataLoader.ConstantBuffer[184])); - ((IComputeShaderDescriptor)shader).LoadGraphicsResources(in shader, ref dataLoader); + LoadGraphicsResources(in shader, ref dataLoader); - Assert.AreEqual(1, ((IComputeShaderDescriptor)shader).ResourceDescriptorRanges.Length); + Assert.AreEqual(1, ResourceDescriptorRanges().Length); Assert.AreSame(buffer, dataLoader.GraphicsResources[0].Resource); Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); } @@ -445,9 +447,9 @@ public partial class ShaderDataLoaderTests AmbiguousNamesShader shader = new(a, b, c, x, y, z, 7777, 8888, 9999); DebugDispatchDataLoader dataLoader = new(); - ((IComputeShaderDescriptor)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); + LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333); - Assert.AreEqual(24, ((IComputeShaderDescriptor)shader).ConstantBufferSize); + Assert.AreEqual(24, ConstantBufferSize()); Assert.IsNotNull(dataLoader.ConstantBuffer); Assert.AreEqual(24, dataLoader.ConstantBuffer.Length); Assert.AreEqual(111, Unsafe.As(ref dataLoader.ConstantBuffer[0])); @@ -457,9 +459,9 @@ public partial class ShaderDataLoaderTests Assert.AreEqual(8888, Unsafe.As(ref dataLoader.ConstantBuffer[16])); Assert.AreEqual(9999, Unsafe.As(ref dataLoader.ConstantBuffer[20])); - ((IComputeShaderDescriptor)shader).LoadGraphicsResources(in shader, ref dataLoader); + LoadGraphicsResources(in shader, ref dataLoader); - Assert.AreEqual(6, ((IComputeShaderDescriptor)shader).ResourceDescriptorRanges.Length); + Assert.AreEqual(6, ResourceDescriptorRanges().Length); Assert.AreSame(a, dataLoader.GraphicsResources[0].Resource); Assert.AreEqual(0u, dataLoader.GraphicsResources[0].Index); Assert.AreSame(b, dataLoader.GraphicsResources[1].Resource); @@ -473,4 +475,34 @@ public partial class ShaderDataLoaderTests Assert.AreSame(z, dataLoader.GraphicsResources[5].Resource); Assert.AreEqual(5u, dataLoader.GraphicsResources[5].Index); } + + /// + private static int ConstantBufferSize() + where T : struct, IComputeShaderDescriptor + { + return T.ConstantBufferSize; + } + + /// + private static ReadOnlyMemory ResourceDescriptorRanges() + where T : struct, IComputeShaderDescriptor + { + return T.ResourceDescriptorRanges; + } + + /// + private static void LoadConstantBuffer(in T shader, ref TLoader loader, int x, int y, int z) + where T : struct, IComputeShaderDescriptor + where TLoader : struct, IConstantBufferLoader + { + T.LoadConstantBuffer(in shader, ref loader, x, y, z); + } + + /// + private static void LoadGraphicsResources(in T shader, ref TLoader loader) + where T : struct, IComputeShaderDescriptor + where TLoader : struct, IGraphicsResourceLoader + { + T.LoadGraphicsResources(in shader, ref loader); + } } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index 80ef6827..79a8b2a8 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -1,3 +1,4 @@ +using System; using ComputeSharp; using ComputeSharp.Descriptors; using ComputeSharp.Interop; @@ -15,9 +16,15 @@ namespace ComputeSharp.Tests [TestMethod] public void ReflectionBytecode() { + static ReadOnlyMemory GetHlslBytecode() + where T : struct, IComputeShaderDescriptor + { + return T.HlslBytecode; + } + ShaderInfo shaderInfo = ReflectionServices.GetShaderInfo(); - CollectionAssert.AreEqual(((IComputeShaderDescriptor)default(ReservedKeywordsShader)).HlslBytecode.ToArray(), shaderInfo.HlslBytecode.ToArray()); + CollectionAssert.AreEqual(GetHlslBytecode().ToArray(), shaderInfo.HlslBytecode.ToArray()); } [TestMethod] diff --git a/tests/ComputeSharp.Tests/ThreadGroupSizeTests.cs b/tests/ComputeSharp.Tests/ThreadGroupSizeTests.cs index 58aa2a45..8f135864 100644 --- a/tests/ComputeSharp.Tests/ThreadGroupSizeTests.cs +++ b/tests/ComputeSharp.Tests/ThreadGroupSizeTests.cs @@ -10,21 +10,16 @@ public partial class ThreadGroupSizeTests [TestMethod] public unsafe void Verify_ThreadGroupSize() { - Assert.AreEqual(64, ((IComputeShaderDescriptor)default(DispatchXShader)).ThreadsX); - Assert.AreEqual(1, ((IComputeShaderDescriptor)default(DispatchXShader)).ThreadsY); - Assert.AreEqual(1, ((IComputeShaderDescriptor)default(DispatchXShader)).ThreadsZ); + static (int X, int Y, int Z) GetNumThreads() + where T : struct, IComputeShaderDescriptor + { + return (T.ThreadsX, T.ThreadsY, T.ThreadsZ); + } - Assert.AreEqual(8, ((IComputeShaderDescriptor)default(DispatchXYShader)).ThreadsX); - Assert.AreEqual(8, ((IComputeShaderDescriptor)default(DispatchXYShader)).ThreadsY); - Assert.AreEqual(1, ((IComputeShaderDescriptor)default(DispatchXYShader)).ThreadsZ); - - Assert.AreEqual(4, ((IComputeShaderDescriptor)default(DispatchXYZShader)).ThreadsX); - Assert.AreEqual(4, ((IComputeShaderDescriptor)default(DispatchXYZShader)).ThreadsY); - Assert.AreEqual(4, ((IComputeShaderDescriptor)default(DispatchXYZShader)).ThreadsZ); - - Assert.AreEqual(11, ((IComputeShaderDescriptor)default(DispatchCustomShader)).ThreadsX); - Assert.AreEqual(14, ((IComputeShaderDescriptor)default(DispatchCustomShader)).ThreadsY); - Assert.AreEqual(6, ((IComputeShaderDescriptor)default(DispatchCustomShader)).ThreadsZ); + Assert.AreEqual((64, 1, 1), GetNumThreads()); + Assert.AreEqual((8, 8, 1), GetNumThreads()); + Assert.AreEqual((4, 4, 4), GetNumThreads()); + Assert.AreEqual((11, 14, 6), GetNumThreads()); } [AutoConstructor]