shader-slang / slang

Making it easier to work with shaders
MIT License
2.06k stars 177 forks source link

Support HLSL swizzle operations #4413

Closed chaoticbob closed 2 months ago

chaoticbob commented 3 months ago

This feature request is for on-ramping. HLSL has a swizzle syntax that allow constants, temporaries, and, variables to swizzle to match the type of the expression. The swizzled expression can only use components up to their highest dimension.

This swizzling is commonly used. It would be helpful to have these operations supported for on-ramping.

Shader: non-exhaustive list of swizzled expressions:

RWTexture1D<uint4>   MyRWTexture;
RWTexture1D<float4>  MyRWTextureF;
void main() {
    MyRWTexture[0] = 0.xxxx;
    MyRWTexture[1] = uint2(0, 1).xyxy;
    MyRWTexture[2] = uint3(0, 1, 2).xxyz;
    MyRWTexture[3] = uint4(0, 1, 2, 3).wxyz;

    MyRWTextureF[0] = 0.12f.xxxx;
    MyRWTextureF[1] = float2(0, 1).xyxy;
    MyRWTextureF[2] = float3(0, 1, 2).xxyz;
    MyRWTextureF[3] = float4(0, 1, 2, 3).wxyz;

    MyRWTextureF[0] = 0.25.xxxx;

    float  s1;
    float2 s2;
    float3 s3;
    float4 s4;
    MyRWTextureF[0] = s1.xxxx + 0.xxxx;
    MyRWTextureF[1] = s2.xyxy + float2(0, 1).xyxy;
    MyRWTextureF[2] = s3.xxyz + float3(0, 1, 2).zyxy;
    MyRWTextureF[3] = s4.wxyz + float4(0, 1, 2, 4).zyxw;
}
jkwak-work commented 3 months ago

It seems that the swizzling on vector types work fine already. But the swizzling on a scalar value is not working.

0.xxxx;
0.12f.xxx;

I am not sure if Slang should support it. @csyonghe for help.

jkwak-work commented 3 months ago

I take it back. The swizzling syntax on a scalar value seems to be working. Only problem is that integer type is recognized as a floating point type when the expression is like 0.xxx, and slang errors it out as an invalid floating type value. It works when the expression is (0).xxxx. I will see if I can fix this from the parser.