Adopt static abstracts in ComputeSharp (and related projects)
このコミットが含まれているのは:
1 親
786efaf38f
コミット
91f78969b1
14ファイル変更
+119
-127
検索条件に一致するファイルはありません
@@ -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,
|
||||
|
||||
+2
-2
@@ -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())
|
||||
{
|
||||
|
||||
+1
-37
@@ -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};");
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -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);
|
||||
|
||||
+2
-2
@@ -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()};");
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -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};");
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -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)
|
||||
|
||||
@@ -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,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;
|
||||
}
|
||||
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<CapturedResourceShader>)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333);
|
||||
LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333);
|
||||
|
||||
Assert.AreEqual(12, ((IComputeShaderDescriptor<CapturedResourceShader>)shader).ConstantBufferSize);
|
||||
Assert.AreEqual(12, ConstantBufferSize<CapturedResourceShader>());
|
||||
Assert.IsNotNull(dataLoader.ConstantBuffer);
|
||||
Assert.AreEqual(12, dataLoader.ConstantBuffer.Length);
|
||||
Assert.AreEqual(111, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[0]));
|
||||
Assert.AreEqual(222, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[4]));
|
||||
Assert.AreEqual(333, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[8]));
|
||||
|
||||
((IComputeShaderDescriptor<CapturedResourceShader>)shader).LoadGraphicsResources(in shader, ref dataLoader);
|
||||
LoadGraphicsResources(in shader, ref dataLoader);
|
||||
|
||||
Assert.AreEqual(1, ((IComputeShaderDescriptor<CapturedResourceShader>)shader).ResourceDescriptorRanges.Length);
|
||||
Assert.AreEqual(1, ResourceDescriptorRanges<CapturedResourceShader>().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<MultipleResourcesAndPrimitivesShader>)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333);
|
||||
LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333);
|
||||
|
||||
Assert.AreEqual(24, ((IComputeShaderDescriptor<MultipleResourcesAndPrimitivesShader>)shader).ConstantBufferSize);
|
||||
Assert.AreEqual(24, ConstantBufferSize<MultipleResourcesAndPrimitivesShader>());
|
||||
Assert.IsNotNull(dataLoader.ConstantBuffer);
|
||||
Assert.AreEqual(24, dataLoader.ConstantBuffer.Length);
|
||||
Assert.AreEqual(111, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[0]));
|
||||
@@ -82,9 +84,9 @@ public partial class ShaderDataLoaderTests
|
||||
Assert.AreEqual(22, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[16]));
|
||||
Assert.AreEqual(77, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[20]));
|
||||
|
||||
((IComputeShaderDescriptor<MultipleResourcesAndPrimitivesShader>)shader).LoadGraphicsResources(in shader, ref dataLoader);
|
||||
LoadGraphicsResources(in shader, ref dataLoader);
|
||||
|
||||
Assert.AreEqual(2, ((IComputeShaderDescriptor<MultipleResourcesAndPrimitivesShader>)shader).ResourceDescriptorRanges.Length);
|
||||
Assert.AreEqual(2, ResourceDescriptorRanges<MultipleResourcesAndPrimitivesShader>().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<ScalarAndVectorTypesShader>)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333);
|
||||
LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333);
|
||||
|
||||
Assert.AreEqual(72, ((IComputeShaderDescriptor<ScalarAndVectorTypesShader>)shader).ConstantBufferSize);
|
||||
Assert.AreEqual(72, ConstantBufferSize<ScalarAndVectorTypesShader>());
|
||||
Assert.IsNotNull(dataLoader.ConstantBuffer);
|
||||
Assert.AreEqual(72, dataLoader.ConstantBuffer.Length);
|
||||
Assert.AreEqual(111, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[0]));
|
||||
@@ -135,9 +137,9 @@ public partial class ShaderDataLoaderTests
|
||||
Assert.AreEqual(42, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[64]));
|
||||
Assert.AreEqual(9999, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[68]));
|
||||
|
||||
((IComputeShaderDescriptor<ScalarAndVectorTypesShader>)shader).LoadGraphicsResources(in shader, ref dataLoader);
|
||||
LoadGraphicsResources(in shader, ref dataLoader);
|
||||
|
||||
Assert.AreEqual(1, ((IComputeShaderDescriptor<ScalarAndVectorTypesShader>)shader).ResourceDescriptorRanges.Length);
|
||||
Assert.AreEqual(1, ResourceDescriptorRanges<ScalarAndVectorTypesShader>().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<ScalarVectorAndMatrixTypesShader>)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333);
|
||||
LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333);
|
||||
|
||||
Assert.AreEqual(124, ((IComputeShaderDescriptor<ScalarVectorAndMatrixTypesShader>)shader).ConstantBufferSize);
|
||||
Assert.AreEqual(124, ConstantBufferSize<ScalarVectorAndMatrixTypesShader>());
|
||||
Assert.IsNotNull(dataLoader.ConstantBuffer);
|
||||
Assert.AreEqual(124, dataLoader.ConstantBuffer.Length);
|
||||
Assert.AreEqual(111, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[0]));
|
||||
@@ -208,9 +210,9 @@ public partial class ShaderDataLoaderTests
|
||||
Assert.AreEqual(44, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[116]));
|
||||
Assert.AreEqual(9999, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[120]));
|
||||
|
||||
((IComputeShaderDescriptor<ScalarVectorAndMatrixTypesShader>)shader).LoadGraphicsResources(in shader, ref dataLoader);
|
||||
LoadGraphicsResources(in shader, ref dataLoader);
|
||||
|
||||
Assert.AreEqual(1, ((IComputeShaderDescriptor<ScalarVectorAndMatrixTypesShader>)shader).ResourceDescriptorRanges.Length);
|
||||
Assert.AreEqual(1, ResourceDescriptorRanges<ScalarVectorAndMatrixTypesShader>().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<FlatCustomTypeShader>)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333);
|
||||
LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333);
|
||||
|
||||
Assert.AreEqual(124, ((IComputeShaderDescriptor<FlatCustomTypeShader>)shader).ConstantBufferSize);
|
||||
Assert.AreEqual(124, ConstantBufferSize<FlatCustomTypeShader>());
|
||||
Assert.IsNotNull(dataLoader.ConstantBuffer);
|
||||
Assert.AreEqual(124, dataLoader.ConstantBuffer.Length);
|
||||
Assert.AreEqual(111, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[0]));
|
||||
@@ -288,9 +290,9 @@ public partial class ShaderDataLoaderTests
|
||||
Assert.AreEqual(44, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[116]));
|
||||
Assert.AreEqual(9999, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[120]));
|
||||
|
||||
((IComputeShaderDescriptor<FlatCustomTypeShader>)shader).LoadGraphicsResources(in shader, ref dataLoader);
|
||||
LoadGraphicsResources(in shader, ref dataLoader);
|
||||
|
||||
Assert.AreEqual(1, ((IComputeShaderDescriptor<FlatCustomTypeShader>)shader).ResourceDescriptorRanges.Length);
|
||||
Assert.AreEqual(1, ResourceDescriptorRanges<FlatCustomTypeShader>().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<NestedCustomTypesShader>)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333);
|
||||
LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333);
|
||||
|
||||
Assert.AreEqual(188, ((IComputeShaderDescriptor<NestedCustomTypesShader>)shader).ConstantBufferSize);
|
||||
Assert.AreEqual(188, ConstantBufferSize<NestedCustomTypesShader>());
|
||||
Assert.IsNotNull(dataLoader.ConstantBuffer);
|
||||
Assert.AreEqual(188, dataLoader.ConstantBuffer.Length);
|
||||
Assert.AreEqual(111, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[0]));
|
||||
@@ -405,9 +407,9 @@ public partial class ShaderDataLoaderTests
|
||||
Assert.AreEqual(333.3f, Unsafe.As<byte, float>(ref dataLoader.ConstantBuffer[180]));
|
||||
Assert.AreEqual(444.4f, Unsafe.As<byte, float>(ref dataLoader.ConstantBuffer[184]));
|
||||
|
||||
((IComputeShaderDescriptor<NestedCustomTypesShader>)shader).LoadGraphicsResources(in shader, ref dataLoader);
|
||||
LoadGraphicsResources(in shader, ref dataLoader);
|
||||
|
||||
Assert.AreEqual(1, ((IComputeShaderDescriptor<NestedCustomTypesShader>)shader).ResourceDescriptorRanges.Length);
|
||||
Assert.AreEqual(1, ResourceDescriptorRanges<NestedCustomTypesShader>().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<AmbiguousNamesShader>)shader).LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333);
|
||||
LoadConstantBuffer(in shader, ref dataLoader, 111, 222, 333);
|
||||
|
||||
Assert.AreEqual(24, ((IComputeShaderDescriptor<AmbiguousNamesShader>)shader).ConstantBufferSize);
|
||||
Assert.AreEqual(24, ConstantBufferSize<AmbiguousNamesShader>());
|
||||
Assert.IsNotNull(dataLoader.ConstantBuffer);
|
||||
Assert.AreEqual(24, dataLoader.ConstantBuffer.Length);
|
||||
Assert.AreEqual(111, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[0]));
|
||||
@@ -457,9 +459,9 @@ public partial class ShaderDataLoaderTests
|
||||
Assert.AreEqual(8888, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[16]));
|
||||
Assert.AreEqual(9999, Unsafe.As<byte, int>(ref dataLoader.ConstantBuffer[20]));
|
||||
|
||||
((IComputeShaderDescriptor<AmbiguousNamesShader>)shader).LoadGraphicsResources(in shader, ref dataLoader);
|
||||
LoadGraphicsResources(in shader, ref dataLoader);
|
||||
|
||||
Assert.AreEqual(6, ((IComputeShaderDescriptor<AmbiguousNamesShader>)shader).ResourceDescriptorRanges.Length);
|
||||
Assert.AreEqual(6, ResourceDescriptorRanges<AmbiguousNamesShader>().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);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IComputeShaderDescriptor{T}.ConstantBufferSize"/>
|
||||
private static int ConstantBufferSize<T>()
|
||||
where T : struct, IComputeShaderDescriptor<T>
|
||||
{
|
||||
return T.ConstantBufferSize;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IComputeShaderDescriptor{T}.ResourceDescriptorRanges"/>
|
||||
private static ReadOnlyMemory<ResourceDescriptorRange> ResourceDescriptorRanges<T>()
|
||||
where T : struct, IComputeShaderDescriptor<T>
|
||||
{
|
||||
return T.ResourceDescriptorRanges;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IComputeShaderDescriptor{T}.LoadConstantBuffer"/>
|
||||
private static void LoadConstantBuffer<T, TLoader>(in T shader, ref TLoader loader, int x, int y, int z)
|
||||
where T : struct, IComputeShaderDescriptor<T>
|
||||
where TLoader : struct, IConstantBufferLoader
|
||||
{
|
||||
T.LoadConstantBuffer(in shader, ref loader, x, y, z);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IComputeShaderDescriptor{T}.LoadGraphicsResources"/>
|
||||
private static void LoadGraphicsResources<T, TLoader>(in T shader, ref TLoader loader)
|
||||
where T : struct, IComputeShaderDescriptor<T>
|
||||
where TLoader : struct, IGraphicsResourceLoader
|
||||
{
|
||||
T.LoadGraphicsResources(in shader, ref loader);
|
||||
}
|
||||
}
|
||||
@@ -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<byte> GetHlslBytecode<T>()
|
||||
where T : struct, IComputeShaderDescriptor<T>
|
||||
{
|
||||
return T.HlslBytecode;
|
||||
}
|
||||
|
||||
ShaderInfo shaderInfo = ReflectionServices.GetShaderInfo<ReservedKeywordsShader>();
|
||||
|
||||
CollectionAssert.AreEqual(((IComputeShaderDescriptor<ReservedKeywordsShader>)default(ReservedKeywordsShader)).HlslBytecode.ToArray(), shaderInfo.HlslBytecode.ToArray());
|
||||
CollectionAssert.AreEqual(GetHlslBytecode<ReservedKeywordsShader>().ToArray(), shaderInfo.HlslBytecode.ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
||||
@@ -10,21 +10,16 @@ public partial class ThreadGroupSizeTests
|
||||
[TestMethod]
|
||||
public unsafe void Verify_ThreadGroupSize()
|
||||
{
|
||||
Assert.AreEqual(64, ((IComputeShaderDescriptor<DispatchXShader>)default(DispatchXShader)).ThreadsX);
|
||||
Assert.AreEqual(1, ((IComputeShaderDescriptor<DispatchXShader>)default(DispatchXShader)).ThreadsY);
|
||||
Assert.AreEqual(1, ((IComputeShaderDescriptor<DispatchXShader>)default(DispatchXShader)).ThreadsZ);
|
||||
static (int X, int Y, int Z) GetNumThreads<T>()
|
||||
where T : struct, IComputeShaderDescriptor<T>
|
||||
{
|
||||
return (T.ThreadsX, T.ThreadsY, T.ThreadsZ);
|
||||
}
|
||||
|
||||
Assert.AreEqual(8, ((IComputeShaderDescriptor<DispatchXYShader>)default(DispatchXYShader)).ThreadsX);
|
||||
Assert.AreEqual(8, ((IComputeShaderDescriptor<DispatchXYShader>)default(DispatchXYShader)).ThreadsY);
|
||||
Assert.AreEqual(1, ((IComputeShaderDescriptor<DispatchXYShader>)default(DispatchXYShader)).ThreadsZ);
|
||||
|
||||
Assert.AreEqual(4, ((IComputeShaderDescriptor<DispatchXYZShader>)default(DispatchXYZShader)).ThreadsX);
|
||||
Assert.AreEqual(4, ((IComputeShaderDescriptor<DispatchXYZShader>)default(DispatchXYZShader)).ThreadsY);
|
||||
Assert.AreEqual(4, ((IComputeShaderDescriptor<DispatchXYZShader>)default(DispatchXYZShader)).ThreadsZ);
|
||||
|
||||
Assert.AreEqual(11, ((IComputeShaderDescriptor<DispatchCustomShader>)default(DispatchCustomShader)).ThreadsX);
|
||||
Assert.AreEqual(14, ((IComputeShaderDescriptor<DispatchCustomShader>)default(DispatchCustomShader)).ThreadsY);
|
||||
Assert.AreEqual(6, ((IComputeShaderDescriptor<DispatchCustomShader>)default(DispatchCustomShader)).ThreadsZ);
|
||||
Assert.AreEqual((64, 1, 1), GetNumThreads<DispatchXShader>());
|
||||
Assert.AreEqual((8, 8, 1), GetNumThreads<DispatchXYShader>());
|
||||
Assert.AreEqual((4, 4, 4), GetNumThreads<DispatchXYZShader>());
|
||||
Assert.AreEqual((11, 14, 6), GetNumThreads<DispatchCustomShader>());
|
||||
}
|
||||
|
||||
[AutoConstructor]
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする