Adopt static abstracts in ComputeSharp (and related projects)

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-18 20:50:44 +02:00
1 親 786efaf38f
コミット 91f78969b1
14ファイル変更
+119 -127

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

+2 -4
ファイルの表示
@@ -43,9 +43,7 @@ public static partial class ReflectionServices
private static unsafe ShaderInfo GetNonGenericShaderInfo<T>()
where T : struct, IComputeShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
ReadOnlyMemory<byte> hlslBytecode = shader.HlslBytecode;
ReadOnlyMemory<byte> hlslBytecode = T.HlslBytecode;
using ComPtr<IDxcUtils> 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,
@@ -23,7 +23,7 @@ partial class ComputeShaderDescriptorGenerator
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]");
writer.WriteLine($"readonly void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadConstantBuffer<TLoader>(in {typeName} shader, ref TLoader loader, int x, int y, int z)");
writer.WriteLine($"static void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadConstantBuffer<TLoader>(in {typeName} shader, ref TLoader loader, int x, int y, int z)");
using (writer.WriteBlock())
{
@@ -99,7 +99,7 @@ partial class ComputeShaderDescriptorGenerator
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine($"readonly void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadGraphicsResources<TLoader>(in {typeName} shader, ref TLoader loader)");
writer.WriteLine($"static void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadGraphicsResources<TLoader>(in {typeName} shader, ref TLoader loader)");
using (writer.WriteBlock())
{
@@ -20,7 +20,7 @@ partial class ComputeShaderDescriptorGenerator
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.Write($"readonly global::System.ReadOnlyMemory<byte> global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.HlslBytecode => ");
writer.Write($"static global::System.ReadOnlyMemory<byte> global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.HlslBytecode => ");
// Just like with the D2D1 generator, only return the MemoryManager<T> if we do have HLSL bytecode
if (info.HlslInfo is not HlslBytecodeInfo.Success)
@@ -119,41 +119,5 @@ partial class ComputeShaderDescriptorGenerator
callbacks.Add(Callback);
}
/// <summary>
/// Writes the <c>ThreadsX</c> property.
/// </summary>
/// <param name="info">The input <see cref="ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteThreadsXSyntax(ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsX => {info.ThreadsX};");
}
/// <summary>
/// Writes the <c>ThreadsY</c> property.
/// </summary>
/// <param name="info">The input <see cref="ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteThreadsYSyntax(ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsY => {info.ThreadsY};");
}
/// <summary>
/// Writes the <c>ThreadsZ</c> property.
/// </summary>
/// <param name="info">The input <see cref="ShaderInfo"/> instance with gathered shader info.</param>
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
public static void WriteThreadsZSyntax(ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine($"readonly int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ThreadsZ => {info.ThreadsZ};");
}
}
}
@@ -19,7 +19,7 @@ partial class ComputeShaderDescriptorGenerator
{
writer.WriteLine("/// <inheritdoc/>");
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);
@@ -21,7 +21,7 @@ partial class ComputeShaderDescriptorGenerator
{
writer.WriteLine("/// <inheritdoc/>");
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};");
}
/// <summary>
@@ -33,7 +33,7 @@ partial class ComputeShaderDescriptorGenerator
{
writer.WriteLine("/// <inheritdoc/>");
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()};");
}
}
}
@@ -19,7 +19,7 @@ partial class ComputeShaderDescriptorGenerator
{
writer.WriteLine("/// <inheritdoc/>");
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};");
}
/// <summary>
@@ -31,7 +31,7 @@ partial class ComputeShaderDescriptorGenerator
{
writer.WriteLine("/// <inheritdoc/>");
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};");
}
/// <summary>
@@ -43,7 +43,7 @@ partial class ComputeShaderDescriptorGenerator
{
writer.WriteLine("/// <inheritdoc/>");
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};");
}
}
}
@@ -19,7 +19,7 @@ partial class ComputeShaderDescriptorGenerator
{
writer.WriteLine("/// <inheritdoc/>");
writer.WriteGeneratedAttributes(GeneratorName);
writer.Write($"readonly global::System.ReadOnlyMemory<global::ComputeSharp.Interop.ResourceDescriptorRange> global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ResourceDescriptorRanges => ");
writer.Write($"static global::System.ReadOnlyMemory<global::ComputeSharp.Interop.ResourceDescriptorRange> 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)
+3 -3
ファイルの表示
@@ -144,9 +144,9 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
{
using ImmutableArrayBuilder<IndentedTextWriter.Callback<ShaderInfo>> 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);
+13 -13
ファイルの表示
@@ -13,19 +13,19 @@ public interface IComputeShaderDescriptor<T>
where T : struct
{
/// <summary>
/// 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.
/// </summary>
int ThreadsX { get; }
static abstract int ThreadsX { get; }
/// <summary>
/// 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.
/// </summary>
int ThreadsY { get; }
static abstract int ThreadsY { get; }
/// <summary>
/// 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.
/// </summary>
int ThreadsZ { get; }
static abstract int ThreadsZ { get; }
/// <summary>
/// Gets the size in bytes of the constant buffer for the current shader.
@@ -35,7 +35,7 @@ public interface IComputeShaderDescriptor<T>
/// <see href="https://learn.microsoft.com/windows/win32/api/d3d12/nf-d3d12-id3d12graphicscommandlist-setcomputeroot32bitconstants"><c>ID3D12GraphicsCommandList::SetComputeRoot32BitConstants</c></see>,
/// so the size must be a multiple of the size of a DWORD value (ie. 4 bytes).
/// </remarks>
int ConstantBufferSize { get; }
static abstract int ConstantBufferSize { get; }
/// <summary>
/// Gets whether the shader requires a static sampler being declared and initialized.
@@ -43,22 +43,22 @@ public interface IComputeShaderDescriptor<T>
/// <remarks>
/// When requested, a static sampler with linear sampling and mirror addressing will be bound to <c>register(s)</c>.
/// </remarks>
bool IsStaticSamplerRequired { get; }
static abstract bool IsStaticSamplerRequired { get; }
/// <summary>
/// Gets the resource descriptor ranges for all resources that should be bound to this shader.
/// </summary>
ReadOnlyMemory<ResourceDescriptorRange> ResourceDescriptorRanges { get; }
static abstract ReadOnlyMemory<ResourceDescriptorRange> ResourceDescriptorRanges { get; }
/// <summary>
/// Gets the HLSL source code for the current shader instance.
/// </summary>
string HlslSource { get; }
static abstract string HlslSource { get; }
/// <summary>
/// Gets the HLSL bytecode for the current shader.
/// </summary>
ReadOnlyMemory<byte> HlslBytecode { get; }
static abstract ReadOnlyMemory<byte> HlslBytecode { get; }
/// <summary>
/// Loads the constant buffer of a given input shader.
@@ -69,7 +69,7 @@ public interface IComputeShaderDescriptor<T>
/// <param name="x">The number of iterations to run on the X axis.</param>
/// <param name="y">The number of iterations to run on the Y axis.</param>
/// <param name="z">The number of iterations to run on the Z axis.</param>
void LoadConstantBuffer<TLoader>(in T shader, ref TLoader loader, int x, int y, int z)
static abstract void LoadConstantBuffer<TLoader>(in T shader, ref TLoader loader, int x, int y, int z)
where TLoader : struct, IConstantBufferLoader;
/// <summary>
@@ -78,6 +78,6 @@ public interface IComputeShaderDescriptor<T>
/// <typeparam name="TLoader">The type of graphics resource loader being used.</typeparam>
/// <param name="shader">The input shader to load the graphics resources for.</param>
/// <param name="loader">The <typeparamref name="TLoader"/> instance to use to load the data.</param>
void LoadGraphicsResources<TLoader>(in T shader, ref TLoader loader)
static abstract void LoadGraphicsResources<TLoader>(in T shader, ref TLoader loader)
where TLoader : struct, IGraphicsResourceLoader;
}
+9 -9
ファイルの表示
@@ -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(
+5 -9
ファイルの表示
@@ -65,9 +65,7 @@ internal static unsafe class PipelineDataLoader<T>
/// <param name="d3D12RootSignature">The allocated <see cref="ID3D12RootSignature"/> instance.</param>
private static void CreateD3D12RootSignature(ID3D12Device* d3D12Device, ID3D12RootSignature** d3D12RootSignature)
{
Unsafe.SkipInit(out T shader);
ReadOnlySpan<ResourceDescriptorRange> resourceDescriptorRanges = shader.ResourceDescriptorRanges.Span;
ReadOnlySpan<ResourceDescriptorRange> resourceDescriptorRanges = T.ResourceDescriptorRanges.Span;
D3D12_DESCRIPTOR_RANGE1[]? d3D12DescriptorRangesArray = null;
@@ -94,13 +92,13 @@ internal static unsafe class PipelineDataLoader<T>
}
// 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<T>
/// <param name="d3D12PipelineState">The resulting <see cref="ID3D12PipelineState"/> instance.</param>
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);
}