shader-slang / slang

Making it easier to work with shaders
MIT License
1.97k stars 167 forks source link

Support constant folding in resource binding declarations #2174

Open apanteleev opened 2 years ago

apanteleev commented 2 years ago

Other compilers (DXC, FXC and glslang) support basic arithmetic in binding declarations, such as layout(binding = 1+2) resource or resource : register(t[1+2]). This is useful in complex shaders with manual layouts - although it may not be the preferred way of working with resources in Slang, this approach exists, and it would be nice to support it to make porting code to Slang easier.

Full example:

#define BASE 1
#define STRIDE 2

// GLSL version
layout(binding = BASE + (STRIDE * 0))
Texture2D tex1;

// HLSL version
Texture2D tex2 : register(t[BASE + (STRIDE * 1)]);

[numthreads(1,1,1)]
void main()
{
}

Slangc output:

test.slang(5): error 20001: unexpected '+', expected ','
layout(binding = BASE + (STRIDE * 0))
                      ^
test.slang(9): error 20001: unexpected '[', expected ')'
Texture2D tex2 : register(t[BASE + (STRIDE * 1)]);
natduca commented 9 months ago

Language enhancement, we're bucketing this to q3