Enabled loading/dispatch with texture resources

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-03 18:47:36 +01:00
1 親 ca16a746b9
コミット 71e5c80d84
12ファイル変更
+69 -48

検索条件に一致するファイルはありません

+1
ファイルの表示
@@ -26,6 +26,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// <summary>
/// The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
/// <remarks>This field is dynamically accessed when loading shader dispatch data.</remarks>
internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle;
/// <summary>
+1
ファイルの表示
@@ -29,6 +29,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// <summary>
/// The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
/// <remarks>This field is dynamically accessed when loading shader dispatch data.</remarks>
internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle;
/// <summary>
+26 -10
ファイルの表示
@@ -50,8 +50,8 @@ namespace ComputeSharp.Shaders.Mappings
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is in fact a known buffer type.</returns>
[Pure]
public static bool IsKnownBufferType(Type type) => IsConstantBufferType(type) ||
IsReadOnlyBufferType(type) ||
IsReadWriteBufferType(type);
IsReadOnlyResourceType(type) ||
IsReadWriteResourceType(type);
/// <summary>
/// Checks whether or not the input type is a <see cref="ConstantBuffer{T}"/> value.
@@ -63,21 +63,37 @@ namespace ComputeSharp.Shaders.Mappings
type.GetGenericTypeDefinition() == typeof(ConstantBuffer<>);
/// <summary>
/// Checks whether or not the input type is a <see cref="ReadOnlyBuffer{T}"/> value.
/// Checks whether or not the input type is a <see cref="ReadOnlyBuffer{T}"/> or <see cref="ReadOnlyTexture2D{T}"/> value.
/// </summary>
/// <param name="type">The input <see cref="Type"/> instance to check.</param>
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is in fact a <see cref="ReadOnlyBuffer{T}"/> instance.</returns>
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is a readonly resource type.</returns>
[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<>);
}
/// <summary>
/// Checks whether or not the input type is a <see cref="ReadWriteBuffer{T}"/> value.
/// Checks whether or not the input type is a <see cref="ReadWriteBuffer{T}"/> or <see cref="ReadWriteTexture2D{T}"/> value.
/// </summary>
/// <param name="type">The input <see cref="Type"/> instance to check.</param>
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is in fact a <see cref="ReadWriteBuffer{T}"/> instance.</returns>
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is a writeable resource type.</returns>
[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<>);
}
}
}
+4 -4
ファイルの表示
@@ -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);
@@ -1,32 +1,34 @@
namespace ComputeSharp.Shaders.Renderer.Models
{
/// <summary>
/// A model that contains info on a shader buffer field.
/// A model that contains info on a shader typed resource field.
/// </summary>
/// <param name="FieldType">The type of the current field in the HLSL shader.</param>
/// <param name="FieldName">The name to use for the current field.</param>
internal abstract record HlslBufferInfo(string FieldType, string FieldName, int BufferIndex)
internal abstract record HlslResourceInfo(string FieldType, string FieldName, int BufferIndex)
: CapturedFieldInfo(FieldType, FieldName)
{
/// <inheritdoc/>
/// <summary>
/// A model that represents a captured constant buffer in a shader.
/// </summary>
/// <inheritdoc/>
public sealed record Constant(string FieldType, string FieldName, int BufferIndex)
: HlslBufferInfo(FieldType, FieldName, BufferIndex);
: HlslResourceInfo(FieldType, FieldName, BufferIndex);
/// <inheritdoc/>
/// <summary>
/// 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 <see cref="ComputeSharp.ReadOnlyBuffer{T}"/> or <see cref="ComputeSharp.ReadOnlyTexture2D{T}"/>.
/// </summary>
public sealed record ReadOnly(string FieldType, string FieldName, int BufferIndex)
: HlslBufferInfo(FieldType, FieldName, BufferIndex);
/// <inheritdoc/>
public sealed record ReadOnly(string FieldType, string FieldName, int BufferIndex)
: HlslResourceInfo(FieldType, FieldName, BufferIndex);
/// <summary>
/// A model that represents a captured writeable buffer in a shader.
/// Maps to either <see cref="ComputeSharp.ReadWriteBuffer{T}"/> or <see cref="ComputeSharp.ReadWriteTexture2D{T}"/>.
/// </summary>
/// <inheritdoc/>
public sealed record ReadWrite(string FieldType, string FieldName, int BufferIndex)
: HlslBufferInfo(FieldType, FieldName, BufferIndex);
: HlslResourceInfo(FieldType, FieldName, BufferIndex);
}
}
+1 -1
ファイルの表示
@@ -12,7 +12,7 @@ namespace ComputeSharp.Shaders.Renderer.Models
/// <summary>
/// Gets or sets the list of captured buffers being present in the current shader.
/// </summary>
public IReadOnlyList<HlslBufferInfo> BuffersList { get; init; }
public IReadOnlyList<HlslResourceInfo> BuffersList { get; init; }
/// <summary>
/// Gets or sets the list of captured variables being present in the current shader.
+2 -2
ファイルの表示
@@ -14,9 +14,9 @@ namespace ComputeSharp.Shaders.Translation
public string EntryPoint { get; }
/// <summary>
/// Gets the collection of <see cref="HlslBufferInfo"/> items for the shader fields.
/// Gets the collection of <see cref="Renderer.Models.HlslResourceInfo"/> items for the shader fields.
/// </summary>
public IReadOnlyList<HlslBufferInfo> HslsBuffersInfo { get; }
public IReadOnlyList<HlslResourceInfo> HlslResourceInfo { get; }
/// <summary>
/// Gets the collection of <see cref="CapturedFieldInfo"/> items for the shader fields.
+2 -2
ファイルの表示
@@ -114,14 +114,14 @@ namespace ComputeSharp.Shaders.Translation
nameof(NativeObject.ThrowIfDisposed),
BindingFlags.Instance | BindingFlags.NonPublic)!, null);
// Call Buffer<T>.ThrowIfDeviceMismatch(GraphicsDevice)
// Call ThrowIfDeviceMismatch(GraphicsDevice)
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Ldarg_1);
il.EmitCall(OpCodes.Callvirt, fieldInfo.FieldType.GetMethod(
nameof(Buffer<byte>.ThrowIfDeviceMismatch),
BindingFlags.Instance | BindingFlags.NonPublic)!, null);
// Access Buffer<T>.D3D12GpuDescriptorHandle
// Access D3D12GpuDescriptorHandle
il.EmitReadMember(fieldInfo.FieldType.GetField(
nameof(Buffer<byte>.D3D12GpuDescriptorHandle),
BindingFlags.Instance | BindingFlags.NonPublic)!);
+8 -11
ファイルの表示
@@ -47,9 +47,9 @@ namespace ComputeSharp.Shaders.Translation
private readonly List<D3D12_DESCRIPTOR_RANGE1> d3D12DescriptorRanges1 = new();
/// <summary>
/// The <see cref="List{T}"/> with the <see cref="HlslBufferInfo"/> items for the shader fields.
/// The <see cref="List{T}"/> with the <see cref="Renderer.Models.HlslResourceInfo"/> items for the shader fields.
/// </summary>
private readonly List<HlslBufferInfo> hlslBuffersInfo = new();
private readonly List<HlslResourceInfo> hlslResourceInfo = new();
/// <summary>
/// The <see cref="List{T}"/> with the <see cref="CapturedFieldInfo"/> items for the shader fields.
@@ -85,7 +85,7 @@ namespace ComputeSharp.Shaders.Translation
public string EntryPoint { get; private set; }
/// <inheritdoc/>
public IReadOnlyList<HlslBufferInfo> HslsBuffersInfo => this.hlslBuffersInfo;
public IReadOnlyList<HlslResourceInfo> HlslResourceInfo => this.hlslResourceInfo;
/// <inheritdoc/>
public IReadOnlyList<CapturedFieldInfo> 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))
{
+1 -1
ファイルの表示
@@ -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]);
}
+9 -5
ファイルの表示
@@ -57,15 +57,17 @@ namespace ComputeSharp.SourceGenerators.Mappings
});
/// <summary>
/// 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.
/// </summary>
/// <param name="typeName">The input type name to check.</param>
/// <returns>Whether or not <paramref name="typeName"/> represents a structured buffer type.</returns>
public static bool IsStructuredBufferType(string typeName)
/// <returns>Whether or not <paramref name="typeName"/> represents a typed resource type.</returns>
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";
}
/// <summary>
@@ -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()
};
}
+2 -2
ファイルの表示
@@ -231,7 +231,7 @@ namespace ComputeSharp
public static ReadOnlyTexture2D<T> AllocateReadOnlyTexture2D<T>(this GraphicsDevice device, T[] array, int width, int height)
where T : unmanaged
{
return device.AllocateReadOnlyTexture2D(array, width, height);
return device.AllocateReadOnlyTexture2D<T>(array.AsSpan(), width, height);
}
/// <summary>
@@ -317,7 +317,7 @@ namespace ComputeSharp
public static ReadWriteTexture2D<T> AllocateReadWriteTexture2D<T>(this GraphicsDevice device, T[] array, int width, int height)
where T : unmanaged
{
return device.AllocateReadWriteTexture2D(array, width, height);
return device.AllocateReadWriteTexture2D<T>(array.AsSpan(), width, height);
}
/// <summary>