shader-slang / slang

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

disallow non-basic types as vector elements #3180

Open expipiplus1 opened 10 months ago

expipiplus1 commented 10 months ago
//TEST(smoke,compute):COMPARE_COMPUTE(filecheck-buffer=CHECK):-cpu -shaderobj
// CHECK:      1

// This test tests that assigning to a subscript of a unary swizzle works

//TEST_INPUT:ubuffer(data=[0], stride=4):out,name=outputBuffer
RWStructuredBuffer<float> outputBuffer;

[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
    vector<vector<int, 2>, 2> v;
    v.x[0] = 1;
    outputBuffer[dispatchThreadID.x] = v.x[0];
}

See also https://github.com/shader-slang/slang/issues/320

sriramm-nv commented 3 weeks ago

Ran this test with slang-test and found the following IR generated:

func %computeMain       : Func(Void, Vec(UInt, 3 : Int))
{
block %3(
                [nameHint("dispatchThreadID")]
                [semantic("SV_DispatchThreadID", 0 : Int)]
                param %dispatchThreadID : Vec(UInt, 3 : Int)):
        [nameHint("dispatchThreadID")]
        let  %dispatchThreadID1 : Vec(UInt, 3 : Int)    = DebugVar(%1, 9 : UInt, 6 : UInt, 0 : UInt)
        DebugValue(%dispatchThreadID1, %dispatchThreadID)
        DebugLine(%1, 11 : UInt, 11 : UInt, 5 : UInt, 6 : UInt)
        DebugLine(%1, 11 : UInt, 11 : UInt, 5 : UInt, 6 : UInt)
        DebugLine(%1, 11 : UInt, 11 : UInt, 5 : UInt, 6 : UInt)
        [DebugLocation(%1, 11 : UInt, 31 : UInt)]
        [nameHint("v")]
        let  %v : Vec(Vec(Int, 2 : Int), 2 : Int)       = DebugVar(%1, 11 : UInt, 31 : UInt)
        [DebugLocation(%1, 11 : UInt, 31 : UInt)]
        [nameHint("v")]
        let  %v1        : Ptr(Vec(Vec(Int, 2 : Int), 2 : Int))  = var
        DebugLine(%1, 12 : UInt, 12 : UInt, 5 : UInt, 6 : UInt)
        let  %4 : Ptr(Vec(Int, 2 : Int))        = getElementPtr(%v1, 0 : Int)
        let  %5 : Ptr(Int)      = getElementPtr(%4, 0 : Int)
        store(%5, 1 : Int)
        DebugValue(%v, 1 : Int, 0 : Int, 0 : Int)
        DebugLine(%1, 13 : UInt, 13 : UInt, 5 : UInt, 6 : UInt)
        let  %6 : UInt  = swizzle(%dispatchThreadID, 0 : Int)
        let  %7 : Ptr(Float)    = rwstructuredBufferGetElementPtr(%outputBuffer, %6)
        let  %8 : Vec(Vec(Int, 2 : Int), 2 : Int)       = load(%v1)
        let  %9 : Vec(Int, 2 : Int)     = swizzle(%8, 0 : Int)
        let  %10        : Int   = getElement(%9, 0 : Int)
        let  %11        : Float = castIntToFloat(%10)
        store(%7, %11)
        return_val(void_constant)
}

And fails with the error:

tests/bugs/unary-swizzle-subscript.slang(13): error 55200: 'vector<vector<int,2>,2>' is not a supported builtin type for the target.
    outputBuffer[dispatchThreadID.x] = v.x[0];
                                         ^
sriramm-nv commented 2 weeks ago

Based on discussion on the PR#4277, this is going to be prevented from the user side. and a suitable test will be added to capture this behavior.