shader-slang / slang

Making it easier to work with shaders
MIT License
1.78k stars 159 forks source link

`and` intrinsic does not support vector arguments #4434

Open chaoticbob opened 1 week ago

chaoticbob commented 1 week ago

Currently, Slang does not support vector types for the and intrinsic:

shader.hlsl(14): error 30019: expected an expression of type 'bool', got 'vector<bool,2>'
    n = and(m, l);
            ^
shader.hlsl(14): error 30019: expected an expression of type 'bool', got 'vector<bool,2>'
    n = and(m, l);
               ^
shader.hlsl(15): error 30019: expected an expression of type 'bool', got 'vector<bool,3>'
    q = and(o, p);
            ^
shader.hlsl(15): error 30019: expected an expression of type 'bool', got 'vector<bool,3>'
    q = and(o, p);
               ^
shader.hlsl(16): error 30019: expected an expression of type 'bool', got 'vector<bool,4>'
    t = and(r, s);
            ^
shader.hlsl(16): error 30019: expected an expression of type 'bool', got 'vector<bool,4>'
    t = and(r, s);
               ^
shader.hlsl(19): error 30019: expected an expression of type 'bool', got 'vector<bool,2>'
    n = and(n, true);
            ^
shader.hlsl20): error 30019: expected an expression of type 'bool', got 'vector<bool,3>'
    q = and(q, true);
            ^
shader.hlsl(21): error 30019: expected an expression of type 'bool', got 'vector<bool,4>'

To ease porting, it would be nice to have support for a vectorized and. It's reasonable to assume that the and intrinsic gets a decent amount of usage in cases where clarity is desired. Note that there is a special case for when the arguments are a vector and a scalar bool. The scalar bool is implicitly expanded to a vector. However, there is not any special cases for arguments of vectors of two different dimensions, so no truncation.

**Shader***

float4 main() : SV_Target
{
    bool a, b, c;
    c = and(a, b);

    bool1 i, j, k;
    bool2 l, m, n;
    bool3 o, p, q;
    bool4 r, s, t;
    k = and(i, j);
    n = and(m, l);
    q = and(o, p);
    t = and(r, s);

    k = and(k, true);
    n = and(n, true);
    q = and(q, true);
    t = and(t, true);

    bool2 tv = bool2(and(a, b), and(k.x, and(n.x, and(q.x, t.x))));   
    return tv.x ? float4(0, 0, 0, 0) : float4(1, 1, 1, 1);
}
ArielG-NV commented 1 week ago

https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/and--sm4---asm- (reference for task)