shader-slang / slang

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

Does/should Slang allow left side cast? #4450

Open chaoticbob opened 1 week ago

chaoticbob commented 1 week ago

DXC allows for left side cast during assignment (copy), I'm getting this error when I compile the shader below:

shader.hlsl(36): error 30011: left of '=' is not an l-value.
    (Derived)dd2 = (Derived)dd;
                 ^
shader.hlsl(39): error 30019: expected an expression of type 'float', got 'DerivedAgain'
    (Base)dd2    = (Base)d;
          ^~~
shader.hlsl(39): error 33070: expected a function, got 'typeof(Base)'
    (Base)dd2    = (Base)d;
     ^~~~
shader.hlsl(39): error 30019: expected an expression of type 'float', got 'Derived'
    (Base)dd2    = (Base)d;
                         ^
shader.hlsl(39): error 33070: expected a function, got 'typeof(Base)'
    (Base)dd2    = (Base)d;

Does Slang support or have a syntax equivalent for left-side casting?

Shader

struct Base {
    float4 a;
    float4 b;
};

struct Derived : Base {
    float4 b;
    float4 c;
    Base   x;
};

struct DerivedAgain : Derived {
    float4 c;
    float4 d;
};

float4 main() : SV_Target {
    Derived d;
    d.a   = 1.;
    d.b   = 2.;
    d.c   = 3.;
    d.x.a = 4.;
    d.x.b = 5.;

    DerivedAgain dd;
    dd.a  = 6.;
    dd.b  = 7.;
    dd.c  = 8.;
    dd.d  = 9.;

    DerivedAgain dd2;

    // Left side cast to Derived
    (Derived)dd2 = (Derived)dd;

    // Left side cast to Base
    (Base)dd2    = (Base)d;

    return d.a + d.b + d.c + d.x.a + d.x.b + dd.a + dd.b + dd.c + dd.d;
}
csyonghe commented 1 week ago

Inheritance isn't currently supported. Using it can lead to undefined behavior. Because we don't support inheritance, left value casting is also not supported.

jkwak-work commented 2 days ago

@swoods-nv I think we should close this for now and keep track it as a long term task, "Implement inheritance".