diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index 8be43323..b04c95b4 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -26,6 +26,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// The instance for the current resource. /// + /// This field is dynamically accessed when loading shader dispatch data. internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; /// diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 7b1645d7..d4f85208 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -29,6 +29,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// The instance for the current resource. /// + /// This field is dynamically accessed when loading shader dispatch data. internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; /// diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs index 1396b16c..7ade5bc9 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs @@ -50,8 +50,8 @@ namespace ComputeSharp.Shaders.Mappings /// A indicating whether the input is in fact a known buffer type. [Pure] public static bool IsKnownBufferType(Type type) => IsConstantBufferType(type) || - IsReadOnlyBufferType(type) || - IsReadWriteBufferType(type); + IsReadOnlyResourceType(type) || + IsReadWriteResourceType(type); /// /// Checks whether or not the input type is a value. @@ -63,21 +63,37 @@ namespace ComputeSharp.Shaders.Mappings type.GetGenericTypeDefinition() == typeof(ConstantBuffer<>); /// - /// Checks whether or not the input type is a value. + /// Checks whether or not the input type is a or value. /// /// The input instance to check. - /// A indicating whether the input is in fact a instance. + /// A indicating whether the input is a readonly resource type. [Pure] - public static bool IsReadOnlyBufferType(Type type) => type.IsGenericType && - type.GetGenericTypeDefinition() == typeof(ReadOnlyBuffer<>); + public static bool IsReadOnlyResourceType(Type type) + { + if (!type.IsGenericType) return false; + + Type genericType = type.GetGenericTypeDefinition(); + + return + genericType == typeof(ReadOnlyBuffer<>) || + genericType == typeof(ReadOnlyTexture2D<>); + } /// - /// Checks whether or not the input type is a value. + /// Checks whether or not the input type is a or value. /// /// The input instance to check. - /// A indicating whether the input is in fact a instance. + /// A indicating whether the input is a writeable resource type. [Pure] - public static bool IsReadWriteBufferType(Type type) => type.IsGenericType && - type.GetGenericTypeDefinition() == typeof(ReadWriteBuffer<>); + public static bool IsReadWriteResourceType(Type type) + { + if (!type.IsGenericType) return false; + + Type genericType = type.GetGenericTypeDefinition(); + + return + genericType == typeof(ReadWriteBuffer<>) || + genericType == typeof(ReadWriteTexture2D<>); + } } } diff --git a/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs b/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs index 1db89f72..72ee091e 100644 --- a/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs +++ b/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs @@ -60,14 +60,14 @@ namespace ComputeSharp.Shaders.Renderer builder.AppendLine('}'); // Buffers - foreach (var buffer in info.HslsBuffersInfo) + foreach (var buffer in info.HlslResourceInfo) { builder.AppendLine(); switch (buffer) { // Constant buffer go to cbuffer fields with a dummy local - case HlslBufferInfo.Constant _: + case HlslResourceInfo.Constant _: builder.Append("cbuffer _"); builder.Append(buffer.FieldName); builder.Append(" : register(b"); @@ -83,10 +83,10 @@ namespace ComputeSharp.Shaders.Renderer break; // Structured buffer have the same syntax but different id - case HlslBufferInfo.ReadOnly _: + case HlslResourceInfo.ReadOnly _: char registerId = 't'; goto StructuredBuffer; - case HlslBufferInfo.ReadWrite _: + case HlslResourceInfo.ReadWrite _: registerId = 'u'; StructuredBuffer: builder.Append(buffer.FieldType); diff --git a/src/ComputeSharp.Shaders/Renderer/Models/HlslBufferInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/HlslResourceInfo.cs similarity index 57% rename from src/ComputeSharp.Shaders/Renderer/Models/HlslBufferInfo.cs rename to src/ComputeSharp.Shaders/Renderer/Models/HlslResourceInfo.cs index 5b9b90bb..f89cb615 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/HlslBufferInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/HlslResourceInfo.cs @@ -1,32 +1,34 @@ namespace ComputeSharp.Shaders.Renderer.Models { /// - /// A model that contains info on a shader buffer field. + /// A model that contains info on a shader typed resource field. /// /// The type of the current field in the HLSL shader. /// The name to use for the current field. - internal abstract record HlslBufferInfo(string FieldType, string FieldName, int BufferIndex) + internal abstract record HlslResourceInfo(string FieldType, string FieldName, int BufferIndex) : CapturedFieldInfo(FieldType, FieldName) { - /// /// /// A model that represents a captured constant buffer in a shader. /// + /// public sealed record Constant(string FieldType, string FieldName, int BufferIndex) - : HlslBufferInfo(FieldType, FieldName, BufferIndex); + : HlslResourceInfo(FieldType, FieldName, BufferIndex); - /// /// - /// A model that represents a captured readonly buffer in a shader. + /// A model that represents a captured readonly typed resource in a shader. + /// Maps to either or . /// - public sealed record ReadOnly(string FieldType, string FieldName, int BufferIndex) - : HlslBufferInfo(FieldType, FieldName, BufferIndex); - /// + public sealed record ReadOnly(string FieldType, string FieldName, int BufferIndex) + : HlslResourceInfo(FieldType, FieldName, BufferIndex); + /// /// A model that represents a captured writeable buffer in a shader. + /// Maps to either or . /// + /// public sealed record ReadWrite(string FieldType, string FieldName, int BufferIndex) - : HlslBufferInfo(FieldType, FieldName, BufferIndex); + : HlslResourceInfo(FieldType, FieldName, BufferIndex); } } diff --git a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs index 3ae877df..efaa10db 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs @@ -12,7 +12,7 @@ namespace ComputeSharp.Shaders.Renderer.Models /// /// Gets or sets the list of captured buffers being present in the current shader. /// - public IReadOnlyList BuffersList { get; init; } + public IReadOnlyList BuffersList { get; init; } /// /// Gets or sets the list of captured variables being present in the current shader. diff --git a/src/ComputeSharp.Shaders/Translation/IShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/IShaderLoader.cs index 9a4724a7..1a8b5be0 100644 --- a/src/ComputeSharp.Shaders/Translation/IShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/IShaderLoader.cs @@ -14,9 +14,9 @@ namespace ComputeSharp.Shaders.Translation public string EntryPoint { get; } /// - /// Gets the collection of items for the shader fields. + /// Gets the collection of items for the shader fields. /// - public IReadOnlyList HslsBuffersInfo { get; } + public IReadOnlyList HlslResourceInfo { get; } /// /// Gets the collection of items for the shader fields. diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs index 7da9b114..33fa3804 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs @@ -114,14 +114,14 @@ namespace ComputeSharp.Shaders.Translation nameof(NativeObject.ThrowIfDisposed), BindingFlags.Instance | BindingFlags.NonPublic)!, null); - // Call Buffer.ThrowIfDeviceMismatch(GraphicsDevice) + // Call ThrowIfDeviceMismatch(GraphicsDevice) il.Emit(OpCodes.Dup); il.Emit(OpCodes.Ldarg_1); il.EmitCall(OpCodes.Callvirt, fieldInfo.FieldType.GetMethod( nameof(Buffer.ThrowIfDeviceMismatch), BindingFlags.Instance | BindingFlags.NonPublic)!, null); - // Access Buffer.D3D12GpuDescriptorHandle + // Access D3D12GpuDescriptorHandle il.EmitReadMember(fieldInfo.FieldType.GetField( nameof(Buffer.D3D12GpuDescriptorHandle), BindingFlags.Instance | BindingFlags.NonPublic)!); diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs index 5411b172..989207a2 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs @@ -47,9 +47,9 @@ namespace ComputeSharp.Shaders.Translation private readonly List d3D12DescriptorRanges1 = new(); /// - /// The with the items for the shader fields. + /// The with the items for the shader fields. /// - private readonly List hlslBuffersInfo = new(); + private readonly List hlslResourceInfo = new(); /// /// The with the items for the shader fields. @@ -85,7 +85,7 @@ namespace ComputeSharp.Shaders.Translation public string EntryPoint { get; private set; } /// - public IReadOnlyList HslsBuffersInfo => this.hlslBuffersInfo; + public IReadOnlyList HlslResourceInfo => this.hlslResourceInfo; /// public IReadOnlyList FieldsInfo => this.fieldsInfo; @@ -156,32 +156,29 @@ namespace ComputeSharp.Shaders.Translation Type fieldType = fieldInfo.FieldType; (string hlslName, string hlslType) = Attribute.Fields[fieldInfo.Name]; - // Constant buffer if (HlslKnownTypes.IsConstantBufferType(fieldType)) { D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, this.constantBuffersCount); this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1); this.capturedFields.Add(fieldInfo); - this.hlslBuffersInfo.Add(new HlslBufferInfo.Constant(hlslType, hlslName, (int)this.constantBuffersCount++)); + this.hlslResourceInfo.Add(new HlslResourceInfo.Constant(hlslType, hlslName, (int)this.constantBuffersCount++)); } - else if (HlslKnownTypes.IsReadOnlyBufferType(fieldType)) + else if (HlslKnownTypes.IsReadOnlyResourceType(fieldType)) { - // Root parameter for a readonly buffer D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, this.readOnlyBuffersCount); this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1); this.capturedFields.Add(fieldInfo); - this.hlslBuffersInfo.Add(new HlslBufferInfo.ReadOnly(hlslType, hlslName, (int)this.readOnlyBuffersCount++)); + this.hlslResourceInfo.Add(new HlslResourceInfo.ReadOnly(hlslType, hlslName, (int)this.readOnlyBuffersCount++)); } - else if (HlslKnownTypes.IsReadWriteBufferType(fieldType)) + else if (HlslKnownTypes.IsReadWriteResourceType(fieldType)) { - // Root parameter for a read write buffer D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, this.readWriteBuffersCount); this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1); this.capturedFields.Add(fieldInfo); - this.hlslBuffersInfo.Add(new HlslBufferInfo.ReadWrite(hlslType, hlslName, (int)this.readWriteBuffersCount++)); + this.hlslResourceInfo.Add(new HlslResourceInfo.ReadWrite(hlslType, hlslName, (int)this.readWriteBuffersCount++)); } else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType)) { diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs index b5cb0deb..f80986f1 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs @@ -120,7 +120,7 @@ namespace ComputeSharp.SourceGenerators yield return new[] { fieldSymbol.Name, mapping ?? fieldSymbol.Name, typeName }; // Track the type of items in the current buffer - if (HlslKnownTypes.IsStructuredBufferType(typeSymbol.GetFullMetadataName())) + if (HlslKnownTypes.IsTypedResourceType(typeSymbol.GetFullMetadataName())) { types.Add((INamedTypeSymbol)typeSymbol.TypeArguments[0]); } diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index 39cfc05a..5b3b9299 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -57,15 +57,17 @@ namespace ComputeSharp.SourceGenerators.Mappings }); /// - /// Checks whether or not a given type name matches a structured buffer type. + /// Checks whether or not a given type name matches a typed resource type. /// /// The input type name to check. - /// Whether or not represents a structured buffer type. - public static bool IsStructuredBufferType(string typeName) + /// Whether or not represents a typed resource type. + public static bool IsTypedResourceType(string typeName) { return typeName == "ComputeSharp.ReadOnlyBuffer`1" || - typeName == "ComputeSharp.ReadWriteBuffer`1"; + typeName == "ComputeSharp.ReadWriteBuffer`1" || + typeName == "ComputeSharp.ReadOnlyTexture2D`1" || + typeName == "ComputeSharp.ReadWriteTexture2D`1"; } /// @@ -83,7 +85,7 @@ namespace ComputeSharp.SourceGenerators.Mappings string typeName = typeSymbol.GetFullMetadataName(); // Special case for the structured buffer types - if (IsStructuredBufferType(typeName)) + if (IsTypedResourceType(typeName)) { string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments[0]).GetFullMetadataName(); @@ -98,6 +100,8 @@ namespace ComputeSharp.SourceGenerators.Mappings { "ComputeSharp.ReadOnlyBuffer`1" => $"StructuredBuffer<{mapped}>", "ComputeSharp.ReadWriteBuffer`1" => $"RWStructuredBuffer<{mapped}>", + "ComputeSharp.ReadOnlyTexture2D`1" => $"Texture2D<{mapped}>", + "ComputeSharp.ReadWriteTexture2D`1" => $"RWTexture2D<{mapped}>", _ => throw new ArgumentException() }; } diff --git a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs index 51a6ab80..303397c2 100644 --- a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs @@ -231,7 +231,7 @@ namespace ComputeSharp public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[] array, int width, int height) where T : unmanaged { - return device.AllocateReadOnlyTexture2D(array, width, height); + return device.AllocateReadOnlyTexture2D(array.AsSpan(), width, height); } /// @@ -317,7 +317,7 @@ namespace ComputeSharp public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[] array, int width, int height) where T : unmanaged { - return device.AllocateReadWriteTexture2D(array, width, height); + return device.AllocateReadWriteTexture2D(array.AsSpan(), width, height); } ///