ComputeSharp icon indicating copy to clipboard operation
ComputeSharp copied to clipboard

Fixed size array support

Open sselecirPyM opened this issue 3 years ago • 7 comments

As far as I know, hlsl supports fixed-size arrays. Are there any plans to support the use of fixed-size arrays in shaders?

sselecirPyM avatar Jun 03 '21 10:06 sselecirPyM

This is something I want to add, but need support for fixed size arrays added to C# first. They've prototyped it in last year's hackathon, but hasn't made it to an official C# release yet. Unfortunately I don't see this happening before at least C# 13. Will leave this open to track it though 🙂

Sergio0694 avatar Aug 18 '22 10:08 Sergio0694

You can support only new T[constant] statements. Or Span<T> variable= stackalloc T[constant].

sselecirPyM avatar Aug 20 '22 12:08 sselecirPyM

You can support only new T[constant] statements. Or Span variable= stackalloc T[constant].

Or use Hlsl intrinsics eg. float[] array = Hlsl.AllocConstSizeFloatArray(32);

harrison314 avatar Apr 02 '23 07:04 harrison314

Could also permit InlineArray-esque syntax, e.g.

[HlslArray(100)]
private struct Float4Len100 { public float4 element0; }

public float4 Execute()
{
    Float4Len100 array = default;
    for (int i = 0; i < 100; ++i) { array[i] = (float4)i; }
    ...
}

It's kind of ugly, but this way we don't have to wait 5 years for whatever support might be hopefully added to C#. And if/when the right syntax does come along, just switch over to that and nuke the old syntax.

This would unlock a lot of potential, the lack of this is currently limiting what I can do quite severely!

rickbrew avatar Jan 04 '24 01:01 rickbrew

Could also permit InlineArray-esque syntax, e.g.

I can see no reason not to support System.Runtime.CompilerServices.InlineArray, which already met our needs. implement something similar by ourselves will bring some syntax noise and a lot of extra work to do.

luoluo39 avatar Apr 07 '24 13:04 luoluo39

I can see no reason not to support System.Runtime.CompilerServices.InlineArray,

As @Sergio0694 explained it to me on Discord, it will still require an additional attribute so that the source generator knows when to kick in. So it would be

[InlineArray(100)]
[HlslArray]
private struct Float4Len100 { public float4 element0; }

And there is quite a bit of work in the generator that would be needed to enable all of this. Don't worry, I've been lobbying @Sergio0694 for this quite a bit -- he definitely knows how important this is :)

There's also the matter of how to unify this across other arrays in other contexts, or other array-like-things such as groupshared and RWStructuredBuffer<T> (e.g. resource textures). Or if that's even necessary / important.

rickbrew avatar Apr 07 '24 19:04 rickbrew