ComputeSharp
ComputeSharp copied to clipboard
Fixed size array support
As far as I know, hlsl supports fixed-size arrays. Are there any plans to support the use of fixed-size arrays in shaders?
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 🙂
You can support only new T[constant] statements. Or Span<T> variable= stackalloc T[constant].
You can support only new T[constant] statements. Or Span variable= stackalloc T[constant].
Or use Hlsl
intrinsics eg. float[] array = Hlsl.AllocConstSizeFloatArray(32);
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!
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.
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.