shader-slang / slang

Making it easier to work with shaders
MIT License
1.82k stars 165 forks source link

hlsl syntax 64 bit buffer and image atomics for spirv #4120

Closed Ipotrick closed 2 months ago

Ipotrick commented 2 months ago

I am currently porting some code to be slang from glsl for a vulkan renderer and need 64 bit image and 64 bit buffer atomics. When i try to use interlocked ops. Example:

[[vk::binding(0,0)]] RWTexture2D<uint64_t> tex;

[shader("compute")]
[numthreads(1, 1, 1)]
void main()
{
    InterlockedAdd(tex[uint2(0,0)], uint64_t(0));
}

i get the following error:

SLANG [test] D:/repos/Daxa/tests/3_samples/1_mandelbrot/shaders/compute.slang(66): error 36107: entrypoint 'main' requires capability 'hlsl', which is incompatible with the current compilation target 'spirv_1_5 + compute'.
void main()
     ^~~~~~~~~~~~~~~~
D:/repos/Daxa/tests/3_samples/1_mandelbrot/shaders/compute.slang(68): note: see using of 'InterlockedAdd'
    InterlockedAdd(tex[uint2(0,0)], uint64_t(0));
    ^~~~~~~~~~~~~~
hlsl.meta.slang(7038): note: see definition of 'InterlockedAdd'
ArielG-NV commented 2 months ago

InterlockedAdd for 64bit types with images looks to only have a hlsl implementation:

[ForceInline]
void InterlockedAdd(__ref  int64_t dest,  int64_t value)
{
    __target_switch
    {
    case hlsl: __intrinsic_asm "InterlockedAdd";
    }
}

For now the glsl syntax variant of the operation should work as a workaround if only a spir-v/glsl target is needed: imageAtomicAdd

Ipotrick commented 2 months ago

How would i use imageAtomicAdd in the example code i have above? I dont understand.

ArielG-NV commented 2 months ago

I believe the following should work: imageAtomicAdd(tex, ivec2(0,0), uint64_t(0));

Ipotrick commented 2 months ago

slang does not recognize imageAromicAdd nor ivec2. Do i need to import a specific module for this to work?

ArielG-NV commented 2 months ago

the compiler flag -allow-glsl should enable GLSL syntax to work along-side HLSL syntax

Ipotrick commented 2 months ago

slang crashes when i try to use glsl syntax 😨

ArielG-NV commented 2 months ago

That is not the expected result.

Ipotrick commented 2 months ago
[[vk::binding(0,0)]] RWTexture2D<uint64_t> tex[];

[shader("compute")]
[numthreads(1, 1, 1)]
void main()
{
    imageAtomicAdd(tex[0], int2(0,0), uint64_t(0));
}

This is a repro for the crash. It does not happen when the images are not in an array. This does not cause a crash:

[[vk::binding(0,0)]] RWTexture2D<uint64_t> tex;

[shader("compute")]
[numthreads(1, 1, 1)]
void main()
{
    imageAtomicAdd(tex, int2(0,0), uint64_t(0));
}
csyonghe commented 2 months ago

import glsl; is another (preferred) way to use glsl flavored intrinsics. But we should look into the crash.

ArielG-NV commented 2 months ago

This probably should be broken into 2 issues: 1 for the crash 1 for merging glsl.meta<->hlsl.meta image atomic operations

Ipotrick commented 2 months ago

I ll make another issue for the crash