Sergio0694/ComputeSharp

Field initializers are not generated into shader code

Kloizdena opened this issue · 1 comments

Description (optional)

I created a custom struct to hold some data. I want to fill and use this struct in my shader. The problem is, when I try to fill the values of the struct with field initializers, the generated code does not contain these instructions and there are no warnings or errors.

Reproduction Steps

Run the following code in a console app:

using ComputeSharp;

IComputeShader shader = new FieldInitializerTest();
shader.BuildHlslSource(out var builder, 0, 0, 0);
Console.WriteLine(builder.WrittenSpan.ToString());

[AutoConstructor]
public readonly partial struct FieldInitializerTest : IComputeShader
{
    private readonly ReadWriteBuffer<Ray> _buffer;

    public void Execute()
    {
        _buffer[ThreadIds.X] = new Ray()
        {
            Position = ThreadIds.X,
            Direction = ThreadIds.X,
        };
    }
}

public struct Ray
{
    public float2 Position, Direction;
}

Expected Behavior

The generated code should contain the field initializer instructions.

Actual Behavior

The following code is generated:

// ================================================
//                  AUTO GENERATED
// ================================================
// This shader was created by ComputeSharp.
// See: https://github.com/Sergio0694/ComputeSharp.

#define __GroupSize__get_X 0
#define __GroupSize__get_Y 0
#define __GroupSize__get_Z 0

struct Ray
{
    float2 Position;
    float2 Direction;
};

cbuffer _ : register(b0)
{
    uint __x;
    uint __y;
    uint __z;
}

RWStructuredBuffer<Ray> _buffer : 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)
    {
        _buffer[ThreadIds.x] = (Ray)0;
    }
}

System info

  • ComputeSharp.Dynamic NuGet version: 2.0.3
  • Operating system version: Microsoft Windows [Version 10.0.22621.2134]
  • CPU/GPU model (the latter is especially useful for shader issues): AMD Ryzen 7 5825U
  • Visual Studio version Version 17.7.0, .NET SDK version 7.0.400

Duplicate of #481, constructors/initializers are not supported yet. You can work around this by using a factory:

public struct Ray
{
    public float2 Position, Direction;

    public static Ray Create(int position, int direction)
    {
        Ray ray;
        ray.Position = position;
        ray.Direction = direction;

        return ray;
    }
}