Types of HLSL expressions

The rules for HLSL type conversions and promotions are not clearly spelled out in MSDN. Thankfully, it's very easy to lean on the compiler to figure out exactly what it's doing.

// Build with fxc on the path (eg from a Visual Studio Command Prompt Window)
// fxc.exe /T ps_5_0 sample.fxc

// Overloads. Let fxc figure this out for you.
float GetTypeKind(float v)  { return 0; }
float GetTypeKind(uint v)   { return 1; }
float GetTypeKind(double v) { return 2; }
float GetTypeKind(bool v)   { return 3; }
float GetTypeKind(int v)    { return 4; }

// Some handy variables, to make sure constant folding never goes too crazy.
float f;
double d;
bool b;

float4 main() : SV_Target {
  return float4(GetTypeKind(
    f+d // <- expression goes here
  ), 0, 0, 0);

This will produce output similar to this, which lets us know that a float plus a double yields a double.

... stuff you don't care about here ...
dcl_globalFlags refactoringAllowed
dcl_output o0.xyzw
mov o0.xyzw, l(2.000000,0,0,0)


Comments (0)

Skip to main content