Merge pull request #666 from Sergio0694/dev/d2d-generator-tests

Tweak source generators, add full output tests
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-11-23 12:54:43 +01:00
コミット 7c6d691fee
8ファイル変更
+2835 -28

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

+12 -14
ファイルの表示
@@ -80,31 +80,29 @@ internal static partial class BlasHelpers
/// <summary>
/// Kernel for <see cref="FullyConnectedForwardGpu"/>.
/// </summary>
[AutoConstructor]
[ThreadGroupSize(DefaultThreadGroupSizes.XYZ)]
[GeneratedComputeShaderDescriptor]
public readonly partial struct FullyConnectedForwardKernel : IComputeShader
public readonly partial struct FullyConnectedForwardKernel(
int n,
int m,
int p,
ReadOnlyBuffer<float> x,
ReadOnlyBuffer<float> w,
ReadOnlyBuffer<float> b,
ReadWriteBuffer<float> y) : IComputeShader
{
private readonly int n;
private readonly int m;
private readonly int p;
private readonly ReadOnlyBuffer<float> x;
private readonly ReadOnlyBuffer<float> w;
private readonly ReadOnlyBuffer<float> b;
private readonly ReadWriteBuffer<float> y;
/// <inheritdoc/>
public void Execute()
{
int x_offset = (ThreadIds.X * this.n * this.p) + (ThreadIds.Y * this.m);
int x_offset = (ThreadIds.X * n * p) + (ThreadIds.Y * m);
float result = 0f;
for (int k = 0; k < this.m; k++)
for (int k = 0; k < m; k++)
{
result += this.x[x_offset + k] * this.w[(k * this.p) + ThreadIds.Z];
result += x[x_offset + k] * w[(k * p) + ThreadIds.Z];
}
this.y[(ThreadIds.X * this.n * this.p) + (ThreadIds.Y * this.p) + ThreadIds.Z] = result + this.b[ThreadIds.Z];
y[(ThreadIds.X * n * p) + (ThreadIds.Y * p) + ThreadIds.Z] = result + b[ThreadIds.Z];
}
}
}
+6 -2
ファイルの表示
@@ -155,8 +155,12 @@ internal sealed class IndentedTextWriter : IDisposable
}
else
{
// Write the current line
WriteRawText(content[..newLineIndex]);
ReadOnlySpan<char> line = content[..newLineIndex];
// Write the current line (if it's empty, we can skip writing the text entirely).
// This ensures that raw multiline string literals with blank lines don't have
// extra whitespace at the start of those lines, which would otherwise happen.
WriteIf(!line.IsEmpty, line);
WriteLine();
// Move past the new line character (the result could be an empty span)
+16 -8
ファイルの表示
@@ -6,6 +6,7 @@ using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
using ComputeSharp.SourceGeneration.Models;
using ComputeSharp.SourceGeneration.SyntaxProcessors;
using ComputeSharp.SourceGeneration.SyntaxRewriters;
using ComputeSharp.SourceGenerators.SyntaxRewriters;
using Microsoft.CodeAnalysis;
@@ -122,9 +123,16 @@ partial class ComputeShaderDescriptorGenerator
bool hlslResourceFound = false;
foreach (IFieldSymbol fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>())
foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers())
{
if (fieldSymbol.IsStatic)
// Skip constants and static fields, we only care about instance ones here
if (memberSymbol is not IFieldSymbol { IsConst: false, IsStatic: false } fieldSymbol)
{
continue;
}
// Try to get the actual field name
if (!ConstantBufferSyntaxProcessor.TryGetFieldAccessorName(fieldSymbol, out string? fieldName, out _))
{
continue;
}
@@ -134,13 +142,13 @@ partial class ComputeShaderDescriptorGenerator
// Group shared fields must be static
if (attribute is not null)
{
diagnostics.Add(InvalidGroupSharedFieldDeclaration, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name);
diagnostics.Add(InvalidGroupSharedFieldDeclaration, fieldSymbol, structDeclarationSymbol, fieldName);
}
// Captured fields must be named type symbols
if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol)
{
diagnostics.Add(InvalidShaderField, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type);
diagnostics.Add(InvalidShaderField, fieldSymbol, structDeclarationSymbol, fieldName, fieldSymbol.Type);
continue;
}
@@ -148,7 +156,7 @@ partial class ComputeShaderDescriptorGenerator
string metadataName = typeSymbol.GetFullyQualifiedMetadataName();
string typeName = HlslKnownTypes.GetMappedName(typeSymbol);
_ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping);
_ = HlslKnownKeywords.TryGetMappedName(fieldName, out string? mapping);
// Allowed fields must be either resources, unmanaged values or delegates
if (HlslKnownTypes.IsTypedResourceType(metadataName))
@@ -162,13 +170,13 @@ partial class ComputeShaderDescriptorGenerator
}
// Add the current mapping for the name (if the name used a reserved keyword)
resources.Add((metadataName, mapping ?? fieldSymbol.Name, typeName));
resources.Add((metadataName, mapping ?? fieldName, typeName));
}
else if (!typeSymbol.IsUnmanagedType &&
typeSymbol.TypeKind != TypeKind.Delegate)
{
// Shaders can only capture valid HLSL resource types or unmanaged types
diagnostics.Add(InvalidShaderField, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name, typeSymbol);
diagnostics.Add(InvalidShaderField, fieldSymbol, structDeclarationSymbol, fieldName, typeSymbol);
continue;
}
@@ -180,7 +188,7 @@ partial class ComputeShaderDescriptorGenerator
types.Add(typeSymbol);
}
values.Add((mapping ?? fieldSymbol.Name, typeName));
values.Add((mapping ?? fieldName, typeName));
}
}
@@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Basic.Reference.Assemblies.Net80" Version="1.4.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
ファイル差分が大きすぎるため表示されません。 差分を読み込み
@@ -46,7 +46,7 @@ internal sealed class CSharpAnalyzerWithLanguageVersionTest<TAnalyzer> : CSharpA
/// <inheritdoc cref="AnalyzerVerifier{TAnalyzer, TTest, TVerifier}.VerifyAnalyzerAsync"/>
/// <param name="source">The source code to analyze.</param>
/// <param name="languageVersion">The language version to use to run the test.</param>
public static Task VerifyAnalyzerAsync(string source, LanguageVersion languageVersion = LanguageVersion.CSharp11)
public static Task VerifyAnalyzerAsync(string source, LanguageVersion languageVersion = LanguageVersion.CSharp12)
{
CSharpAnalyzerWithLanguageVersionTest<TAnalyzer> test = new(languageVersion) { TestCode = source };
+939
ファイルの表示
@@ -0,0 +1,939 @@
extern alias Core;
extern alias D3D12;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Basic.Reference.Assemblies;
using ComputeSharp.SourceGenerators;
using ComputeSharp.Tests.SourceGenerators.Helpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.Tests.SourceGenerators;
[TestClass]
public class Test_D2DPixelShaderSourceGenerator
{
[TestMethod]
public async Task SimpleShader_ComputeShader()
{
const string source = """
using ComputeSharp;
using float2 = global::ComputeSharp.Float2;
using float3 = global::ComputeSharp.Float3;
using float4 = global::ComputeSharp.Float4;
namespace MyNamespace;
[ThreadGroupSize(DefaultThreadGroupSizes.XY)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct MyShader : IComputeShader
{
private readonly IReadWriteNormalizedTexture2D<float4> texture;
private readonly float time;
public void Execute()
{
float2 uv = ThreadIds.Normalized.XY;
float3 col = 0.5f + 0.5f * Hlsl.Cos(time + new float3(uv, uv.X) + new float3(0, 2, 4));
texture[ThreadIds.XY] = new float4(col, 1f);
}
}
""";
const string result = """"
// <auto-generated/>
#pragma warning disable
namespace MyNamespace
{
/// <inheritdoc cref="MyShader"/>
partial struct MyShader : global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>
{
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.ThreadsX => 8;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.ThreadsY => 8;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.ThreadsZ => 1;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.ConstantBufferSize => 16;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static bool global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.IsStaticSamplerRequired => false;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static global::System.ReadOnlyMemory<global::ComputeSharp.Interop.ResourceDescriptorRange> global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.ResourceDescriptorRanges => global::ComputeSharp.Generated.Data.ResourceDescriptorRanges;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static string global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.HlslSource =>
"""
// ================================================
// AUTO GENERATED
// ================================================
// This shader was created by ComputeSharp.
// See: https://github.com/Sergio0694/ComputeSharp.
#define __GroupSize__get_X 8
#define __GroupSize__get_Y 8
#define __GroupSize__get_Z 1
cbuffer _ : register(b0)
{
uint __x;
uint __y;
uint __z;
float time;
}
RWTexture2D<unorm float4> __reserved__texture : register(u0);
[NumThreads(__GroupSize__get_X, __GroupSize__get_Y, __GroupSize__get_Z)]
void Execute(uint3 ThreadIds : SV_DispatchThreadID)
{
if (ThreadIds.x < __x && ThreadIds.y < __y && ThreadIds.z < __z)
{
float2 uv = float2(ThreadIds.x, ThreadIds.y) / float2(max(1, int2(__x, __y) - 1));
float3 col = 0.5 + 0.5 * cos(time + float3(uv, uv.x) + float3(0, 2, 4));
__reserved__texture[ThreadIds.xy] = float4(col, 1.0);
}
}
""";
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static global::System.ReadOnlyMemory<byte> global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.HlslBytecode => global::ComputeSharp.Generated.HlslBytecodeMemoryManager.Instance.Memory;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Runtime.CompilerServices.SkipLocalsInit]
static unsafe void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.LoadConstantBuffer<TLoader>(in MyShader shader, ref TLoader loader, int x, int y, int z)
{
global::ComputeSharp.Generated.ConstantBufferMarshaller.FromManaged(in shader, out global::ComputeSharp.Generated.ConstantBuffer buffer);
buffer.__x = x;
buffer.__y = y;
buffer.__z = z;
loader.LoadConstantBuffer(new global::System.ReadOnlySpan<byte>(&buffer, sizeof(global::ComputeSharp.Generated.ConstantBuffer)));
}
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.LoadGraphicsResources<TLoader>(in MyShader shader, ref TLoader loader)
{
global::ComputeSharp.Generated.GraphicsResourcesLoader.LoadGraphicsResources(in shader, ref loader);
}
}
}
namespace ComputeSharp.Generated
{
using global::System;
using global::System.Buffers;
using global::System.CodeDom.Compiler;
using global::System.Diagnostics;
using global::System.Diagnostics.CodeAnalysis;
using global::System.Runtime.CompilerServices;
using global::System.Runtime.InteropServices;
using global::ComputeSharp.Descriptors;
using global::ComputeSharp.Interop;
/// <summary>
/// A type representing the constant buffer native layout for <see cref="global::MyNamespace.MyShader"/>.
/// </summary>
[GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
[StructLayout(LayoutKind.Explicit, Size = 16)]
file struct ConstantBuffer
{
/// <summary>The artificial field backing <see cref="DispatchSize.X"/>.</summary>
[FieldOffset(0)]
public int __x;
/// <summary>The artificial field backing <see cref="DispatchSize.Y"/>.</summary>
[FieldOffset(4)]
public int __y;
/// <summary>The artificial field backing <see cref="DispatchSize.Z"/>.</summary>
[FieldOffset(8)]
public int __z;
/// <inheritdoc cref="global::MyNamespace.MyShader.time"/>
[FieldOffset(12)]
public float time;
}
/// <summary>
/// A type containing marshalling logic for the constant buffer in shaders of type <see cref="global::MyNamespace.MyShader"/>.
/// </summary>
[GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
file static class ConstantBufferMarshaller
{
/// <summary>
/// Marshals managed <see cref="global::MyNamespace.MyShader"/> instances to native constant buffer data.
/// </summary>
/// <param name="buffer">The input native constant buffer.</param>
/// <returns>The marshalled <see cref="global::MyNamespace.MyShader"/> instance.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[SkipLocalsInit]
public static void FromManaged(in global::MyNamespace.MyShader shader, out ConstantBuffer buffer)
{
Unsafe.SkipInit(out buffer.__x);
Unsafe.SkipInit(out buffer.__y);
Unsafe.SkipInit(out buffer.__z);
buffer.time = shader.time();
}
/// <inheritdoc cref="global::MyNamespace.MyShader.time"/>
/// <param name="value">The input <see cref="global::MyNamespace.MyShader"/> value.</param>
/// <returns>A mutable reference to <see cref="global::MyNamespace.MyShader.time"/>.</returns>
[UnsafeAccessor(UnsafeAccessorKind.Field)]
private static extern ref float time(this ref readonly global::MyNamespace.MyShader value);
}
/// <summary>
/// A type containing loading logic for graphics resources in shaders of type <see cref="global::MyNamespace.MyShader"/>.
/// </summary>
[GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
file static class GraphicsResourcesLoader
{
/// <inheritdoc cref="IComputeShaderDescriptor{T}.LoadGraphicsResources"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LoadGraphicsResources<TLoader>(in global::MyNamespace.MyShader shader, ref TLoader loader)
where TLoader : struct, IGraphicsResourceLoader
{
loader.LoadGraphicsResource(shader.texture(), 0);
}
/// <inheritdoc cref="global::MyNamespace.MyShader.texture"/>
/// <param name="value">The input <see cref="global::MyNamespace.MyShader"/> value.</param>
/// <returns>A reference to <see cref="global::MyNamespace.MyShader.texture"/>.</returns>
[UnsafeAccessor(UnsafeAccessorKind.Field)]
private static extern ref readonly IReadWriteNormalizedTexture2D<float4> texture(this ref readonly global::MyNamespace.MyShader value);
}
/// <summary>
/// A container type for additional data needed by the shader.
/// </summary>
[GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
file static class Data
{
/// <summary>The singleton <see cref="ResourceDescriptorRange"/> array instance.</summary>
public static readonly ResourceDescriptorRange[] ResourceDescriptorRanges =
[
new ResourceDescriptorRange(ResourceDescriptorRangeType.UnorderedAccessView, 0)
];
}
/// <summary>
/// <see cref="MemoryManager{T}"/> implementation to get the HLSL bytecode.
/// </summary>
[GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
file sealed class HlslBytecodeMemoryManager : MemoryManager<byte>
{
/// <summary>The singleton <see cref="HlslBytecodeMemoryManager"/> instance to use.</summary>
public static readonly HlslBytecodeMemoryManager Instance = new();
/// <summary>The RVA data with the HLSL bytecode.</summary>
private static ReadOnlySpan<byte> Data => [0x44, 0x58, 0x42, 0x43, 0x85, 0xAB, 0x89, 0xA8, 0x68, 0x86, 0x6C, 0x2B, 0x90, 0xB8, 0x38, 0x75, 0x15, 0x08, 0xEE, 0x86, 0x01, 0x00, 0x00, 0x00, 0xA0, 0x0E, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00, 0x5C, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0xEC, 0x00, 0x00, 0x00, 0x88, 0x07, 0x00, 0x00, 0xA4, 0x07, 0x00, 0x00, 0x53, 0x46, 0x49, 0x30, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49, 0x53, 0x47, 0x31, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x4F, 0x53, 0x47, 0x31, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x50, 0x53, 0x56, 0x30, 0x78, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x54, 0x41, 0x54, 0x94, 0x06, 0x00, 0x00, 0x60, 0x00, 0x05, 0x00, 0xA5, 0x01, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x7C, 0x06, 0x00, 0x00, 0x42, 0x43, 0xC0, 0xDE, 0x21, 0x0C, 0x00, 0x00, 0x9C, 0x01, 0x00, 0x00, 0x0B, 0x82, 0x20, 0x00, 0x02, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x07, 0x81, 0x23, 0x91, 0x41, 0xC8, 0x04, 0x49, 0x06, 0x10, 0x32, 0x39, 0x92, 0x01, 0x84, 0x0C, 0x25, 0x05, 0x08, 0x19, 0x1E, 0x04, 0x8B, 0x62, 0x80, 0x14, 0x45, 0x02, 0x42, 0x92, 0x0B, 0x42, 0xA4, 0x10, 0x32, 0x14, 0x38, 0x08, 0x18, 0x4B, 0x0A, 0x32, 0x52, 0x88, 0x48, 0x90, 0x14, 0x20, 0x43, 0x46, 0x88, 0xA5, 0x00, 0x19, 0x32, 0x42, 0xE4, 0x48, 0x0E, 0x90, 0x91, 0x22, 0xC4, 0x50, 0x41, 0x51, 0x81, 0x8C, 0xE1, 0x83, 0xE5, 0x8A, 0x04, 0x29, 0x46, 0x06, 0x51, 0x18, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x1B, 0x8C, 0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x40, 0x02, 0xA8, 0x0D, 0x84, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x20, 0x6D, 0x30, 0x86, 0xFF, 0xFF, 0xFF, 0xFF, 0x1F, 0x00, 0x09, 0xA8, 0x00, 0x49, 0x18, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x13, 0x82, 0x60, 0x42, 0x20, 0x4C, 0x08, 0x06, 0x00, 0x00, 0x00, 0x00, 0x89, 0x20, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x32, 0x22, 0x48, 0x09, 0x20, 0x64, 0x85, 0x04, 0x93, 0x22, 0xA4, 0x84, 0x04, 0x93, 0x22, 0xE3, 0x84, 0xA1, 0x90, 0x14, 0x12, 0x4C, 0x8A, 0x8C, 0x0B, 0x84, 0xA4, 0x4C, 0x10, 0x7C, 0x23, 0x00, 0x25, 0x00, 0x14, 0xE6, 0x08, 0xC0, 0xA0, 0x0C, 0x63, 0x0C, 0x22, 0x85, 0x18, 0x63, 0x0C, 0x32, 0x33, 0x00, 0x65, 0x40, 0x03, 0x51, 0x9A, 0x23, 0x40, 0x68, 0xDD, 0x33, 0x5C, 0xFE, 0x84, 0x3D, 0x84, 0xE4, 0x87, 0x40, 0x33, 0x2C, 0x04, 0x0A, 0x58, 0x59, 0xC0, 0x68, 0x63, 0x0C, 0x84, 0x10, 0x5A, 0xE4, 0x8E, 0x1A, 0x2E, 0x7F, 0xC2, 0x1E, 0x42, 0xF2, 0xB9, 0x8D, 0x2A, 0x56, 0x62, 0xF2, 0x8B, 0xDB, 0x46, 0x04, 0x42, 0x08, 0x15, 0x02, 0x8E, 0x36, 0x28, 0x1E, 0x35, 0x5C, 0xFE, 0x84, 0x3D, 0x84, 0xE4, 0x73, 0x1B, 0x55, 0xAC, 0xC4, 0xE4, 0x23, 0xB7, 0x8D, 0x88, 0x31, 0xC6, 0x28, 0xC4, 0x1C, 0x6D, 0x10, 0x9D, 0x23, 0x08, 0x8A, 0xD1, 0xC6, 0x1A, 0xC3, 0xD2, 0x1D, 0x08, 0x18, 0x46, 0x20, 0x90, 0x99, 0xC8, 0x60, 0x1C, 0xD8, 0x21, 0x1C, 0xE6, 0x61, 0x1E, 0xDC, 0x40, 0x16, 0x6E, 0x81, 0x16, 0xCA, 0x01, 0x1F, 0xE8, 0xA1, 0x1E, 0xE4, 0xA1, 0x1C, 0xE4, 0x80, 0x14, 0xF8, 0xC0, 0x1E, 0xCA, 0x61, 0x1C, 0xE8, 0xE1, 0x1D, 0xE4, 0x81, 0x0F, 0xCC, 0x81, 0x1D, 0xDE, 0x21, 0x1C, 0xE8, 0x81, 0x0D, 0xC0, 0x80, 0x0E, 0xFC, 0x00, 0x0C, 0xFC, 0x00, 0x05, 0x9A, 0xF6, 0x05, 0x3F, 0x22, 0xC6, 0x18, 0x88, 0x3A, 0x00, 0x00, 0x13, 0x14, 0x72, 0xC0, 0x87, 0x74, 0x60, 0x87, 0x36, 0x68, 0x87, 0x79, 0x68, 0x03, 0x72, 0xC0, 0x87, 0x0D, 0xAF, 0x50, 0x0E, 0x6D, 0xD0, 0x0E, 0x7A, 0x50, 0x0E, 0x6D, 0x00, 0x0F, 0x7A, 0x30, 0x07, 0x72, 0xA0, 0x07, 0x73, 0x20, 0x07, 0x6D, 0x90, 0x0E, 0x71, 0xA0, 0x07, 0x73, 0x20, 0x07, 0x6D, 0x90, 0x0E, 0x78, 0xA0, 0x07, 0x73, 0x20, 0x07, 0x6D, 0x90, 0x0E, 0x71, 0x60, 0x07, 0x7A, 0x30, 0x07, 0x72, 0xD0, 0x06, 0xE9, 0x30, 0x07, 0x72, 0xA0, 0x07, 0x73, 0x20, 0x07, 0x6D, 0x90, 0x0E, 0x76, 0x40, 0x07, 0x7A, 0x60, 0x07, 0x74, 0xD0, 0x06, 0xE6, 0x10, 0x07, 0x76, 0xA0, 0x07, 0x73, 0x20, 0x07, 0x6D, 0x60, 0x0E, 0x73, 0x20, 0x07, 0x7A, 0x30, 0x07, 0x72, 0xD0, 0x06, 0xE6, 0x60, 0x07, 0x74, 0xA0, 0x07, 0x76, 0x40, 0x07, 0x6D, 0xE0, 0x0E, 0x78, 0xA0, 0x07, 0x71, 0x60, 0x07, 0x7A, 0x30, 0x07, 0x72, 0xA0, 0x07, 0x76, 0x40, 0x07, 0x43, 0x9E, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86, 0x3C, 0x04, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x79, 0x0C, 0x20, 0x00, 0x02, 0x00行の続きは省略
/// <inheritdoc/>
public override unsafe Span<byte> GetSpan()
{
return new(Unsafe.AsPointer(ref MemoryMarshal.GetReference(Data)), Data.Length);
}
/// <inheritdoc/>
public override Memory<byte> Memory
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => CreateMemory(Data.Length);
}
/// <inheritdoc/>
public override unsafe MemoryHandle Pin(int elementIndex)
{
return new(Unsafe.AsPointer(ref Unsafe.AsRef(in Data[elementIndex])), pinnable: this);
}
/// <inheritdoc/>
public override void Unpin()
{
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
}
}
}
"""";
await VerifyGeneratedDiagnosticsAsync(source, ("MyNamespace.MyShader.g.cs", result));
}
[TestMethod]
public async Task SimpleShader_PixelShader()
{
const string source = """
using ComputeSharp;
using float2 = global::ComputeSharp.Float2;
using float3 = global::ComputeSharp.Float3;
using float4 = global::ComputeSharp.Float4;
namespace MyNamespace;
[ThreadGroupSize(DefaultThreadGroupSizes.XY)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct MyShader : IComputeShader<float4>
{
private readonly float time;
public float4 Execute()
{
float2 uv = ThreadIds.Normalized.XY;
float3 col = 0.5f + 0.5f * Hlsl.Cos(time + new float3(uv, uv.X) + new float3(0, 2, 4));
return new(col, 1f);
}
}
""";
const string result = """"
// <auto-generated/>
#pragma warning disable
namespace MyNamespace
{
/// <inheritdoc cref="MyShader"/>
partial struct MyShader : global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>
{
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.ThreadsX => 8;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.ThreadsY => 8;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.ThreadsZ => 1;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.ConstantBufferSize => 12;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static bool global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.IsStaticSamplerRequired => false;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static global::System.ReadOnlyMemory<global::ComputeSharp.Interop.ResourceDescriptorRange> global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.ResourceDescriptorRanges => global::ComputeSharp.Generated.Data.ResourceDescriptorRanges;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static string global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.HlslSource =>
"""
// ================================================
// AUTO GENERATED
// ================================================
// This shader was created by ComputeSharp.
// See: https://github.com/Sergio0694/ComputeSharp.
#define __GroupSize__get_X 8
#define __GroupSize__get_Y 8
#define __GroupSize__get_Z 1
cbuffer _ : register(b0)
{
uint __x;
uint __y;
float time;
}
RWTexture2D<unorm float4> __outputTexture : register(u0);
[NumThreads(__GroupSize__get_X, __GroupSize__get_Y, __GroupSize__get_Z)]
void Execute(uint3 ThreadIds : SV_DispatchThreadID)
{
if (ThreadIds.x < __x && ThreadIds.y < __y)
{
float2 uv = float2(ThreadIds.x, ThreadIds.y) / float2(max(1, int2(__x, __y) - 1));
float3 col = 0.5 + 0.5 * cos(time + float3(uv, uv.x) + float3(0, 2, 4));
{
__outputTexture[ThreadIds.xy] = float4(col, 1.0);
return;
}
}
}
""";
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static global::System.ReadOnlyMemory<byte> global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.HlslBytecode => global::ComputeSharp.Generated.HlslBytecodeMemoryManager.Instance.Memory;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Runtime.CompilerServices.SkipLocalsInit]
static unsafe void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.LoadConstantBuffer<TLoader>(in MyShader shader, ref TLoader loader, int x, int y, int z)
{
global::ComputeSharp.Generated.ConstantBufferMarshaller.FromManaged(in shader, out global::ComputeSharp.Generated.ConstantBuffer buffer);
buffer.__x = x;
buffer.__y = y;
loader.LoadConstantBuffer(new global::System.ReadOnlySpan<byte>(&buffer, sizeof(global::ComputeSharp.Generated.ConstantBuffer)));
}
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.LoadGraphicsResources<TLoader>(in MyShader shader, ref TLoader loader)
{
}
}
}
namespace ComputeSharp.Generated
{
using global::System;
using global::System.Buffers;
using global::System.CodeDom.Compiler;
using global::System.Diagnostics;
using global::System.Diagnostics.CodeAnalysis;
using global::System.Runtime.CompilerServices;
using global::System.Runtime.InteropServices;
using global::ComputeSharp.Interop;
/// <summary>
/// A type representing the constant buffer native layout for <see cref="global::MyNamespace.MyShader"/>.
/// </summary>
[GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
[StructLayout(LayoutKind.Explicit, Size = 12)]
file struct ConstantBuffer
{
/// <summary>The artificial field backing <see cref="DispatchSize.X"/>.</summary>
[FieldOffset(0)]
public int __x;
/// <summary>The artificial field backing <see cref="DispatchSize.Y"/>.</summary>
[FieldOffset(4)]
public int __y;
/// <inheritdoc cref="global::MyNamespace.MyShader.time"/>
[FieldOffset(8)]
public float time;
}
/// <summary>
/// A type containing marshalling logic for the constant buffer in shaders of type <see cref="global::MyNamespace.MyShader"/>.
/// </summary>
[GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
file static class ConstantBufferMarshaller
{
/// <summary>
/// Marshals managed <see cref="global::MyNamespace.MyShader"/> instances to native constant buffer data.
/// </summary>
/// <param name="buffer">The input native constant buffer.</param>
/// <returns>The marshalled <see cref="global::MyNamespace.MyShader"/> instance.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[SkipLocalsInit]
public static void FromManaged(in global::MyNamespace.MyShader shader, out ConstantBuffer buffer)
{
Unsafe.SkipInit(out buffer.__x);
Unsafe.SkipInit(out buffer.__y);
buffer.time = shader.time();
}
/// <inheritdoc cref="global::MyNamespace.MyShader.time"/>
/// <param name="value">The input <see cref="global::MyNamespace.MyShader"/> value.</param>
/// <returns>A mutable reference to <see cref="global::MyNamespace.MyShader.time"/>.</returns>
[UnsafeAccessor(UnsafeAccessorKind.Field)]
private static extern ref float time(this ref readonly global::MyNamespace.MyShader value);
}
/// <summary>
/// A container type for additional data needed by the shader.
/// </summary>
[GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
file static class Data
{
/// <summary>The singleton <see cref="ResourceDescriptorRange"/> array instance.</summary>
public static readonly ResourceDescriptorRange[] ResourceDescriptorRanges =
[
new ResourceDescriptorRange(ResourceDescriptorRangeType.UnorderedAccessView, 0)
];
}
/// <summary>
/// <see cref="MemoryManager{T}"/> implementation to get the HLSL bytecode.
/// </summary>
[GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
file sealed class HlslBytecodeMemoryManager : MemoryManager<byte>
{
/// <summary>The singleton <see cref="HlslBytecodeMemoryManager"/> instance to use.</summary>
public static readonly HlslBytecodeMemoryManager Instance = new();
/// <summary>The RVA data with the HLSL bytecode.</summary>
private static ReadOnlySpan<byte> Data => [0x44, 0x58, 0x42, 0x43, 0xE2, 0xA1, 0x14, 0x53, 0xC8, 0x8F, 0xCF, 0x58, 0x57, 0x3E, 0x4A, 0xC2, 0x7F, 0xDD, 0xFE, 0x93, 0x01, 0x00, 0x00, 0x00, 0x60, 0x0E, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00, 0x5C, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0xEC, 0x00, 0x00, 0x00, 0x64, 0x07, 0x00, 0x00, 0x80, 0x07, 0x00, 0x00, 0x53, 0x46, 0x49, 0x30, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49, 0x53, 0x47, 0x31, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x4F, 0x53, 0x47, 0x31, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x50, 0x53, 0x56, 0x30, 0x78, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x54, 0x41, 0x54, 0x70, 0x06, 0x00, 0x00, 0x60, 0x00, 0x05, 0x00, 0x9C, 0x01, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x58, 0x06, 0x00, 0x00, 0x42, 0x43, 0xC0, 0xDE, 0x21, 0x0C, 0x00, 0x00, 0x93, 0x01, 0x00, 0x00, 0x0B, 0x82, 0x20, 0x00, 0x02, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x07, 0x81, 0x23, 0x91, 0x41, 0xC8, 0x04, 0x49, 0x06, 0x10, 0x32, 0x39, 0x92, 0x01, 0x84, 0x0C, 0x25, 0x05, 0x08, 0x19, 0x1E, 0x04, 0x8B, 0x62, 0x80, 0x14, 0x45, 0x02, 0x42, 0x92, 0x0B, 0x42, 0xA4, 0x10, 0x32, 0x14, 0x38, 0x08, 0x18, 0x4B, 0x0A, 0x32, 0x52, 0x88, 0x48, 0x90, 0x14, 0x20, 0x43, 0x46, 0x88, 0xA5, 0x00, 0x19, 0x32, 0x42, 0xE4, 0x48, 0x0E, 0x90, 0x91, 0x22, 0xC4, 0x50, 0x41, 0x51, 0x81, 0x8C, 0xE1, 0x83, 0xE5, 0x8A, 0x04, 0x29, 0x46, 0x06, 0x51, 0x18, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x1B, 0x8C, 0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x40, 0x02, 0xA8, 0x0D, 0x84, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x20, 0x6D, 0x30, 0x86, 0xFF, 0xFF, 0xFF, 0xFF, 0x1F, 0x00, 0x09, 0xA8, 0x00, 0x49, 0x18, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x13, 0x82, 0x60, 0x42, 0x20, 0x4C, 0x08, 0x06, 0x00, 0x00, 0x00, 0x00, 0x89, 0x20, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x32, 0x22, 0x48, 0x09, 0x20, 0x64, 0x85, 0x04, 0x93, 0x22, 0xA4, 0x84, 0x04, 0x93, 0x22, 0xE3, 0x84, 0xA1, 0x90, 0x14, 0x12, 0x4C, 0x8A, 0x8C, 0x0B, 0x84, 0xA4, 0x4C, 0x10, 0x7C, 0x23, 0x00, 0x25, 0x00, 0x14, 0xE6, 0x08, 0xC0, 0xA0, 0x0C, 0x63, 0x0C, 0x22, 0x85, 0x18, 0x63, 0x0C, 0x32, 0x33, 0x00, 0x65, 0x40, 0x03, 0x51, 0x9A, 0x23, 0x40, 0x68, 0xDD, 0x33, 0x5C, 0xFE, 0x84, 0x3D, 0x84, 0xE4, 0x87, 0x40, 0x33, 0x2C, 0x04, 0x0A, 0x58, 0x59, 0xC0, 0x68, 0x63, 0x0C, 0x84, 0x10, 0x5A, 0xE4, 0x8E, 0x1A, 0x2E, 0x7F, 0xC2, 0x1E, 0x42, 0xF2, 0xB9, 0x8D, 0x2A, 0x56, 0x62, 0xF2, 0x8B, 0xDB, 0x46, 0x04, 0x42, 0x08, 0x15, 0x02, 0x8E, 0x36, 0x28, 0x1E, 0x35, 0x5C, 0xFE, 0x84, 0x3D, 0x84, 0xE4, 0x73, 0x1B, 0x55, 0xAC, 0xC4, 0xE4, 0x23, 0xB7, 0x8D, 0x88, 0x31, 0xC6, 0x28, 0xC4, 0x1C, 0x6D, 0x10, 0x9D, 0x23, 0x08, 0x8A, 0xD1, 0xC6, 0x1A, 0xC3, 0xD2, 0x1D, 0x08, 0x18, 0x46, 0x20, 0x90, 0x99, 0xC8, 0x60, 0x1C, 0xD8, 0x21, 0x1C, 0xE6, 0x61, 0x1E, 0xDC, 0x40, 0x16, 0x6E, 0x81, 0x16, 0xCA, 0x01, 0x1F, 0xE8, 0xA1, 0x1E, 0xE4, 0xA1, 0x1C, 0xE4, 0x80, 0x14, 0xF8, 0xC0, 0x1E, 0xCA, 0x61, 0x1C, 0xE8, 0xE1, 0x1D, 0xE4, 0x81, 0x0F, 0xCC, 0x81, 0x1D, 0xDE, 0x21, 0x1C, 0xE8, 0x81, 0x0D, 0xC0, 0x80, 0x0E, 0xFC, 0x00, 0x0C, 0xFC, 0x00, 0x05, 0x9A, 0xF6, 0x05, 0x3F, 0x1A, 0xC6, 0x40, 0xD4, 0x01, 0x00, 0x00, 0x13, 0x14, 0x72, 0xC0, 0x87, 0x74, 0x60, 0x87, 0x36, 0x68, 0x87, 0x79, 0x68, 0x03, 0x72, 0xC0, 0x87, 0x0D, 0xAF, 0x50, 0x0E, 0x6D, 0xD0, 0x0E, 0x7A, 0x50, 0x0E, 0x6D, 0x00, 0x0F, 0x7A, 0x30, 0x07, 0x72, 0xA0, 0x07, 0x73, 0x20, 0x07, 0x6D, 0x90, 0x0E, 0x71, 0xA0, 0x07, 0x73, 0x20, 0x07, 0x6D, 0x90, 0x0E, 0x78, 0xA0, 0x07, 0x73, 0x20, 0x07, 0x6D, 0x90, 0x0E, 0x71, 0x60, 0x07, 0x7A, 0x30, 0x07, 0x72, 0xD0, 0x06, 0xE9, 0x30, 0x07, 0x72, 0xA0, 0x07, 0x73, 0x20, 0x07, 0x6D, 0x90, 0x0E, 0x76, 0x40, 0x07, 0x7A, 0x60, 0x07, 0x74, 0xD0, 0x06, 0xE6, 0x10, 0x07, 0x76, 0xA0, 0x07, 0x73, 0x20, 0x07, 0x6D, 0x60, 0x0E, 0x73, 0x20, 0x07, 0x7A, 0x30, 0x07, 0x72, 0xD0, 0x06, 0xE6, 0x60, 0x07, 0x74, 0xA0, 0x07, 0x76, 0x40, 0x07, 0x6D, 0xE0, 0x0E, 0x78, 0xA0, 0x07, 0x71, 0x60, 0x07, 0x7A, 0x30, 0x07, 0x72, 0xA0, 0x07, 0x76, 0x40, 0x07, 0x43, 0x9E, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86, 0x3C, 0x04, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x79, 0x0C, 0x20, 0x00, 0x02, 0x00行の続きは省略
/// <inheritdoc/>
public override unsafe Span<byte> GetSpan()
{
return new(Unsafe.AsPointer(ref MemoryMarshal.GetReference(Data)), Data.Length);
}
/// <inheritdoc/>
public override Memory<byte> Memory
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => CreateMemory(Data.Length);
}
/// <inheritdoc/>
public override unsafe MemoryHandle Pin(int elementIndex)
{
return new(Unsafe.AsPointer(ref Unsafe.AsRef(in Data[elementIndex])), pinnable: this);
}
/// <inheritdoc/>
public override void Unpin()
{
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
}
}
}
"""";
await VerifyGeneratedDiagnosticsAsync(source, ("MyNamespace.MyShader.g.cs", result));
}
[TestMethod]
public async Task SimpleShader_WithPrimaryConstructorParameters()
{
const string source = """
using ComputeSharp;
using float2 = global::ComputeSharp.Float2;
using float3 = global::ComputeSharp.Float3;
using float4 = global::ComputeSharp.Float4;
namespace MyNamespace;
[ThreadGroupSize(DefaultThreadGroupSizes.XY)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct MyShader(
IReadWriteNormalizedTexture2D<float4> texture,
float time) : IComputeShader
{
public void Execute()
{
float2 uv = ThreadIds.Normalized.XY;
float3 col = 0.5f + 0.5f * Hlsl.Cos(time + new float3(uv, uv.X) + new float3(0, 2, 4));
texture[ThreadIds.XY] = new float4(col, 1f);
}
}
""";
const string result = """"
// <auto-generated/>
#pragma warning disable
namespace MyNamespace
{
/// <inheritdoc cref="MyShader"/>
partial struct MyShader : global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>
{
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.ThreadsX => 8;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.ThreadsY => 8;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.ThreadsZ => 1;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static int global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.ConstantBufferSize => 16;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static bool global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.IsStaticSamplerRequired => false;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static global::System.ReadOnlyMemory<global::ComputeSharp.Interop.ResourceDescriptorRange> global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.ResourceDescriptorRanges => global::ComputeSharp.Generated.Data.ResourceDescriptorRanges;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static string global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.HlslSource =>
"""
// ================================================
// AUTO GENERATED
// ================================================
// This shader was created by ComputeSharp.
// See: https://github.com/Sergio0694/ComputeSharp.
#define __GroupSize__get_X 8
#define __GroupSize__get_Y 8
#define __GroupSize__get_Z 1
cbuffer _ : register(b0)
{
uint __x;
uint __y;
uint __z;
float time;
}
RWTexture2D<unorm float4> __reserved__texture : register(u0);
[NumThreads(__GroupSize__get_X, __GroupSize__get_Y, __GroupSize__get_Z)]
void Execute(uint3 ThreadIds : SV_DispatchThreadID)
{
if (ThreadIds.x < __x && ThreadIds.y < __y && ThreadIds.z < __z)
{
float2 uv = float2(ThreadIds.x, ThreadIds.y) / float2(max(1, int2(__x, __y) - 1));
float3 col = 0.5 + 0.5 * cos(time + float3(uv, uv.x) + float3(0, 2, 4));
__reserved__texture[ThreadIds.xy] = float4(col, 1.0);
}
}
""";
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static global::System.ReadOnlyMemory<byte> global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.HlslBytecode => global::ComputeSharp.Generated.HlslBytecodeMemoryManager.Instance.Memory;
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Runtime.CompilerServices.SkipLocalsInit]
static unsafe void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.LoadConstantBuffer<TLoader>(in MyShader shader, ref TLoader loader, int x, int y, int z)
{
global::ComputeSharp.Generated.ConstantBufferMarshaller.FromManaged(in shader, out global::ComputeSharp.Generated.ConstantBuffer buffer);
buffer.__x = x;
buffer.__y = y;
buffer.__z = z;
loader.LoadConstantBuffer(new global::System.ReadOnlySpan<byte>(&buffer, sizeof(global::ComputeSharp.Generated.ConstantBuffer)));
}
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
static void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<MyShader>.LoadGraphicsResources<TLoader>(in MyShader shader, ref TLoader loader)
{
global::ComputeSharp.Generated.GraphicsResourcesLoader.LoadGraphicsResources(in shader, ref loader);
}
}
}
namespace ComputeSharp.Generated
{
using global::System;
using global::System.Buffers;
using global::System.CodeDom.Compiler;
using global::System.Diagnostics;
using global::System.Diagnostics.CodeAnalysis;
using global::System.Runtime.CompilerServices;
using global::System.Runtime.InteropServices;
using global::ComputeSharp.Descriptors;
using global::ComputeSharp.Interop;
/// <summary>
/// A type representing the constant buffer native layout for <see cref="global::MyNamespace.MyShader"/>.
/// </summary>
[GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
[StructLayout(LayoutKind.Explicit, Size = 16)]
file struct ConstantBuffer
{
/// <summary>The artificial field backing <see cref="DispatchSize.X"/>.</summary>
[FieldOffset(0)]
public int __x;
/// <summary>The artificial field backing <see cref="DispatchSize.Y"/>.</summary>
[FieldOffset(4)]
public int __y;
/// <summary>The artificial field backing <see cref="DispatchSize.Z"/>.</summary>
[FieldOffset(8)]
public int __z;
/// <summary>The unspeakable field "time" of <see cref="global::MyNamespace.MyShader"/>.</summary>
[FieldOffset(12)]
public float time;
}
/// <summary>
/// A type containing marshalling logic for the constant buffer in shaders of type <see cref="global::MyNamespace.MyShader"/>.
/// </summary>
[GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
file static class ConstantBufferMarshaller
{
/// <summary>
/// Marshals managed <see cref="global::MyNamespace.MyShader"/> instances to native constant buffer data.
/// </summary>
/// <param name="buffer">The input native constant buffer.</param>
/// <returns>The marshalled <see cref="global::MyNamespace.MyShader"/> instance.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[SkipLocalsInit]
public static void FromManaged(in global::MyNamespace.MyShader shader, out ConstantBuffer buffer)
{
Unsafe.SkipInit(out buffer.__x);
Unsafe.SkipInit(out buffer.__y);
Unsafe.SkipInit(out buffer.__z);
buffer.time = shader.time();
}
/// <summary>Gets a reference to the unspeakable field "time" of type <see cref="global::MyNamespace.MyShader"/>.</summary>
/// <param name="value">The input <see cref="global::MyNamespace.MyShader"/> value.</param>
/// <returns>A mutable reference to the unspeakable field "time".</returns>
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<time>P")]
private static extern ref float time(this ref readonly global::MyNamespace.MyShader value);
}
/// <summary>
/// A type containing loading logic for graphics resources in shaders of type <see cref="global::MyNamespace.MyShader"/>.
/// </summary>
[GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
file static class GraphicsResourcesLoader
{
/// <inheritdoc cref="IComputeShaderDescriptor{T}.LoadGraphicsResources"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LoadGraphicsResources<TLoader>(in global::MyNamespace.MyShader shader, ref TLoader loader)
where TLoader : struct, IGraphicsResourceLoader
{
loader.LoadGraphicsResource(shader.texture(), 0);
}
/// <summary>Gets a reference to the unspeakable field "texture" of type <see cref="global::MyNamespace.MyShader"/>.</summary>
/// <param name="value">The input <see cref="global::MyNamespace.MyShader"/> value.</param>
/// <returns>A reference to the unspeakable field "texture".</returns>
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<texture>P")]
private static extern ref readonly IReadWriteNormalizedTexture2D<float4> texture(this ref readonly global::MyNamespace.MyShader value);
}
/// <summary>
/// A container type for additional data needed by the shader.
/// </summary>
[GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
file static class Data
{
/// <summary>The singleton <see cref="ResourceDescriptorRange"/> array instance.</summary>
public static readonly ResourceDescriptorRange[] ResourceDescriptorRanges =
[
new ResourceDescriptorRange(ResourceDescriptorRangeType.UnorderedAccessView, 0)
];
}
/// <summary>
/// <see cref="MemoryManager{T}"/> implementation to get the HLSL bytecode.
/// </summary>
[GeneratedCode("ComputeSharp.ComputeShaderDescriptorGenerator", <ASSEMBLY_VERSION>)]
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
file sealed class HlslBytecodeMemoryManager : MemoryManager<byte>
{
/// <summary>The singleton <see cref="HlslBytecodeMemoryManager"/> instance to use.</summary>
public static readonly HlslBytecodeMemoryManager Instance = new();
/// <summary>The RVA data with the HLSL bytecode.</summary>
private static ReadOnlySpan<byte> Data => [0x44, 0x58, 0x42, 0x43, 0x85, 0xAB, 0x89, 0xA8, 0x68, 0x86, 0x6C, 0x2B, 0x90, 0xB8, 0x38, 0x75, 0x15, 0x08, 0xEE, 0x86, 0x01, 0x00, 0x00, 0x00, 0xA0, 0x0E, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00, 0x5C, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0xEC, 0x00, 0x00, 0x00, 0x88, 0x07, 0x00, 0x00, 0xA4, 0x07, 0x00, 0x00, 0x53, 0x46, 0x49, 0x30, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49, 0x53, 0x47, 0x31, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x4F, 0x53, 0x47, 0x31, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x50, 0x53, 0x56, 0x30, 0x78, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x54, 0x41, 0x54, 0x94, 0x06, 0x00, 0x00, 0x60, 0x00, 0x05, 0x00, 0xA5, 0x01, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x7C, 0x06, 0x00, 0x00, 0x42, 0x43, 0xC0, 0xDE, 0x21, 0x0C, 0x00, 0x00, 0x9C, 0x01, 0x00, 0x00, 0x0B, 0x82, 0x20, 0x00, 0x02, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x07, 0x81, 0x23, 0x91, 0x41, 0xC8, 0x04, 0x49, 0x06, 0x10, 0x32, 0x39, 0x92, 0x01, 0x84, 0x0C, 0x25, 0x05, 0x08, 0x19, 0x1E, 0x04, 0x8B, 0x62, 0x80, 0x14, 0x45, 0x02, 0x42, 0x92, 0x0B, 0x42, 0xA4, 0x10, 0x32, 0x14, 0x38, 0x08, 0x18, 0x4B, 0x0A, 0x32, 0x52, 0x88, 0x48, 0x90, 0x14, 0x20, 0x43, 0x46, 0x88, 0xA5, 0x00, 0x19, 0x32, 0x42, 0xE4, 0x48, 0x0E, 0x90, 0x91, 0x22, 0xC4, 0x50, 0x41, 0x51, 0x81, 0x8C, 0xE1, 0x83, 0xE5, 0x8A, 0x04, 0x29, 0x46, 0x06, 0x51, 0x18, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x1B, 0x8C, 0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x40, 0x02, 0xA8, 0x0D, 0x84, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x20, 0x6D, 0x30, 0x86, 0xFF, 0xFF, 0xFF, 0xFF, 0x1F, 0x00, 0x09, 0xA8, 0x00, 0x49, 0x18, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x13, 0x82, 0x60, 0x42, 0x20, 0x4C, 0x08, 0x06, 0x00, 0x00, 0x00, 0x00, 0x89, 0x20, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x32, 0x22, 0x48, 0x09, 0x20, 0x64, 0x85, 0x04, 0x93, 0x22, 0xA4, 0x84, 0x04, 0x93, 0x22, 0xE3, 0x84, 0xA1, 0x90, 0x14, 0x12, 0x4C, 0x8A, 0x8C, 0x0B, 0x84, 0xA4, 0x4C, 0x10, 0x7C, 0x23, 0x00, 0x25, 0x00, 0x14, 0xE6, 0x08, 0xC0, 0xA0, 0x0C, 0x63, 0x0C, 0x22, 0x85, 0x18, 0x63, 0x0C, 0x32, 0x33, 0x00, 0x65, 0x40, 0x03, 0x51, 0x9A, 0x23, 0x40, 0x68, 0xDD, 0x33, 0x5C, 0xFE, 0x84, 0x3D, 0x84, 0xE4, 0x87, 0x40, 0x33, 0x2C, 0x04, 0x0A, 0x58, 0x59, 0xC0, 0x68, 0x63, 0x0C, 0x84, 0x10, 0x5A, 0xE4, 0x8E, 0x1A, 0x2E, 0x7F, 0xC2, 0x1E, 0x42, 0xF2, 0xB9, 0x8D, 0x2A, 0x56, 0x62, 0xF2, 0x8B, 0xDB, 0x46, 0x04, 0x42, 0x08, 0x15, 0x02, 0x8E, 0x36, 0x28, 0x1E, 0x35, 0x5C, 0xFE, 0x84, 0x3D, 0x84, 0xE4, 0x73, 0x1B, 0x55, 0xAC, 0xC4, 0xE4, 0x23, 0xB7, 0x8D, 0x88, 0x31, 0xC6, 0x28, 0xC4, 0x1C, 0x6D, 0x10, 0x9D, 0x23, 0x08, 0x8A, 0xD1, 0xC6, 0x1A, 0xC3, 0xD2, 0x1D, 0x08, 0x18, 0x46, 0x20, 0x90, 0x99, 0xC8, 0x60, 0x1C, 0xD8, 0x21, 0x1C, 0xE6, 0x61, 0x1E, 0xDC, 0x40, 0x16, 0x6E, 0x81, 0x16, 0xCA, 0x01, 0x1F, 0xE8, 0xA1, 0x1E, 0xE4, 0xA1, 0x1C, 0xE4, 0x80, 0x14, 0xF8, 0xC0, 0x1E, 0xCA, 0x61, 0x1C, 0xE8, 0xE1, 0x1D, 0xE4, 0x81, 0x0F, 0xCC, 0x81, 0x1D, 0xDE, 0x21, 0x1C, 0xE8, 0x81, 0x0D, 0xC0, 0x80, 0x0E, 0xFC, 0x00, 0x0C, 0xFC, 0x00, 0x05, 0x9A, 0xF6, 0x05, 0x3F, 0x22, 0xC6, 0x18, 0x88, 0x3A, 0x00, 0x00, 0x13, 0x14, 0x72, 0xC0, 0x87, 0x74, 0x60, 0x87, 0x36, 0x68, 0x87, 0x79, 0x68, 0x03, 0x72, 0xC0, 0x87, 0x0D, 0xAF, 0x50, 0x0E, 0x6D, 0xD0, 0x0E, 0x7A, 0x50, 0x0E, 0x6D, 0x00, 0x0F, 0x7A, 0x30, 0x07, 0x72, 0xA0, 0x07, 0x73, 0x20, 0x07, 0x6D, 0x90, 0x0E, 0x71, 0xA0, 0x07, 0x73, 0x20, 0x07, 0x6D, 0x90, 0x0E, 0x78, 0xA0, 0x07, 0x73, 0x20, 0x07, 0x6D, 0x90, 0x0E, 0x71, 0x60, 0x07, 0x7A, 0x30, 0x07, 0x72, 0xD0, 0x06, 0xE9, 0x30, 0x07, 0x72, 0xA0, 0x07, 0x73, 0x20, 0x07, 0x6D, 0x90, 0x0E, 0x76, 0x40, 0x07, 0x7A, 0x60, 0x07, 0x74, 0xD0, 0x06, 0xE6, 0x10, 0x07, 0x76, 0xA0, 0x07, 0x73, 0x20, 0x07, 0x6D, 0x60, 0x0E, 0x73, 0x20, 0x07, 0x7A, 0x30, 0x07, 0x72, 0xD0, 0x06, 0xE6, 0x60, 0x07, 0x74, 0xA0, 0x07, 0x76, 0x40, 0x07, 0x6D, 0xE0, 0x0E, 0x78, 0xA0, 0x07, 0x71, 0x60, 0x07, 0x7A, 0x30, 0x07, 0x72, 0xA0, 0x07, 0x76, 0x40, 0x07, 0x43, 0x9E, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86, 0x3C, 0x04, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x79, 0x0C, 0x20, 0x00, 0x02, 0x00行の続きは省略
/// <inheritdoc/>
public override unsafe Span<byte> GetSpan()
{
return new(Unsafe.AsPointer(ref MemoryMarshal.GetReference(Data)), Data.Length);
}
/// <inheritdoc/>
public override Memory<byte> Memory
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => CreateMemory(Data.Length);
}
/// <inheritdoc/>
public override unsafe MemoryHandle Pin(int elementIndex)
{
return new(Unsafe.AsPointer(ref Unsafe.AsRef(in Data[elementIndex])), pinnable: this);
}
/// <inheritdoc/>
public override void Unpin()
{
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
}
}
}
"""";
await VerifyGeneratedDiagnosticsAsync(source, ("MyNamespace.MyShader.g.cs", result));
}
/// <summary>
/// Verifies the output of a source generator.
/// </summary>
/// <param name="source">The input source to process.</param>
/// <param name="result">The expected source to be generated.</param>
/// <returns>The task for the operation.</returns>
private static async Task VerifyGeneratedDiagnosticsAsync(string source, (string Filename, string Source) result)
{
// Also validate all analyzers
await CSharpAnalyzerWithLanguageVersionTest<InvalidComputeContextCopyAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<InvalidGeneratedComputeShaderDescriptorAttributeTargetAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<MissingAllowUnsafeBlocksCompilationOptionAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<MissingComputeShaderDescriptorOnComputeShaderAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzer>.VerifyAnalyzerAsync(source);
await CSharpAnalyzerWithLanguageVersionTest<NotAccessibleGeneratedComputeShaderDescriptorAttributeTargetAnalyzer>.VerifyAnalyzerAsync(source);
// Get all assembly references for the .NET TFM and ComputeSharp
IEnumerable<MetadataReference> metadataReferences =
[
.. Net80.References.All,
MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Hlsl).Assembly.Location),
MetadataReference.CreateFromFile(typeof(D3D12::ComputeSharp.IComputeShader).Assembly.Location)
];
// Parse the source text (C# 12)
SyntaxTree sourceTree = CSharpSyntaxTree.ParseText(
source,
CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp12));
// Create the original compilation
CSharpCompilation compilation = CSharpCompilation.Create(
"original",
new SyntaxTree[] { sourceTree },
metadataReferences,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true));
// Create the generator driver with the shader generator
GeneratorDriver driver = CSharpGeneratorDriver.Create(new ComputeShaderDescriptorGenerator()).WithUpdatedParseOptions((CSharpParseOptions)sourceTree.Options);
// Run all source generators on the input source code
_ = driver.RunGeneratorsAndUpdateCompilation(compilation, out Compilation outputCompilation, out ImmutableArray<Diagnostic> diagnostics);
// Ensure that no diagnostics were generated
CollectionAssert.AreEquivalent(Array.Empty<Diagnostic>(), diagnostics);
// Update the assembly version using the version from the assembly of the input generators.
// This allows the tests to not need updates whenever the version of the MVVM Toolkit changes.
string expectedText = result.Source.Replace("<ASSEMBLY_VERSION>", $"\"{typeof(ComputeShaderDescriptorGenerator).Assembly.GetName().Version}\"");
string actualText = outputCompilation.SyntaxTrees.Single(tree => Path.GetFileName(tree.FilePath) == result.Filename).ToString();
Assert.AreEqual(expectedText, actualText);
}
}
+8 -3
ファイルの表示
@@ -9,15 +9,20 @@ namespace ComputeSharp.SwapChain.Shaders.Compute;
[AutoConstructor]
[ThreadGroupSize(DefaultThreadGroupSizes.XY)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct HelloWorld : IComputeShader<float4>
internal readonly partial struct HelloWorld : IComputeShader
{
/// <summary>
/// The target texture.
/// </summary>
private readonly IReadWriteNormalizedTexture2D<float4> texture;
/// <summary>
/// The current time since the start of the application.
/// </summary>
private readonly float time;
/// <inheritdoc/>
public float4 Execute()
public void Execute()
{
// Normalized screen space UV coordinates from 0.0 to 1.0
float2 uv = ThreadIds.Normalized.XY;
@@ -26,6 +31,6 @@ internal readonly partial struct HelloWorld : IComputeShader<float4>
float3 col = 0.5f + 0.5f * Hlsl.Cos(time + new float3(uv, uv.X) + new float3(0, 2, 4));
// Output to screen
return new(col, 1f);
texture[ThreadIds.XY] = new float4(col, 1f);
}
}