shader-slang / slang

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

Allow casting of 4-component vectors to 2x2 matrices and vice versa #4415

Closed chaoticbob closed 3 months ago

chaoticbob commented 3 months ago

This is for on-ramping. This is a pretty common trick to avoid having to pack and unpack a 4-component vectors to 2x2 matrices and vice versa. slang currently generates a very long error for this.

Shader

float4 main(float4 inputF : A, int4 inputI : B, uint4 inputU : C) : SV_Target {
  float2x2 matF1 = (             float2x2)inputF;
  float2x2 matF2 = (row_major    float2x2)inputF;
  float2x2 matF3 = (column_major float2x2)inputF;
  float4 f1 = (float4)matF1;
  float4 f2 = (float4)matF2;
  float4 f3 = (float4)matF3;

  int2x2 matI1 = (             int2x2)inputI;
  int2x2 matI2 = (row_major    int2x2)inputI;
  int2x2 matI3 = (column_major int2x2)inputI;
  int4 i1 = (int4)matI1;
  int4 i2 = (int4)matI2;
  int4 i3 = (int4)matI3;

  uint2x2 matU1 = (             uint2x2)inputU;
  uint2x2 matU2 = (row_major    uint2x2)inputU;
  uint2x2 matU3 = (column_major uint2x2)inputU;
  uint4 u1 = (uint4)matU1;
  uint4 u2 = (uint4)matU2;
  uint4 u3 = (uint4)matU3;

  return float4(f1.x, f2.x, f3.x, 0) +
         (float4)int4(i1.x, i2.x, i3.x, 0) +
         (float4)uint4(u1.x, u2.x, u3.x, 0);
}