glslang icon indicating copy to clipboard operation
glslang copied to clipboard

[HLSL] Wrong precision of built-in functions

Open Programiao opened this issue 4 years ago • 6 comments

I have a problem while compiling HLSL to Spir-V. Built-in function's precision are not calculated by it's operands. Here is an example. http://shader-playground.timjones.io/0706b6081a3d0a341b373bbbf7923d9a

struct PSInput
{
	float4 color : COLOR;
};
min16float f(in min16float a, in min16float b)
{
    return max(a, b);
}
min16float f2(in min16float a)
{
    return a * a;
}
float4 PSMain(PSInput input) : SV_TARGET
{
    min16float a = input.color.a;
    min16float b = input.color.b;
    
    min16float c = f(a, b);
    min16float d = f2(max(a, b));
    return float4(c,d,c,c);
}

In the above code, all operations should have relaxed precision. Microsoft dxc compiler would mark them all to relaxed precision. But when using glslang, the two max results don't have relaxed precision decoration.

Programiao avatar Nov 03 '19 23:11 Programiao

min16float is currently steered to either float16 or just float, depending on whether 16-bit types are enabled, e.g., through --hlsl-enable-16bit-types.

johnkslang avatar Nov 06 '19 16:11 johnkslang

Yeah it is float, I didn't use --hlsl-enable-16bit-types option. But it's not equal to float, it should has RelaxedPrecision as decoration in vulkan code.

Programiao avatar Nov 11 '19 17:11 Programiao

We are facing the same problem. I experiment with the following code:

    half3 N     = half3(0.0, 0.0, 1.0);
    half3 L     = half3(0.0, 0.0, 1.0);
    half  NdotL = dot(N, L);

that generates the following error in the last line:

'=' :  cannot convert from ' temp float' to ' temp float16_t'

This happens because the result of dot is float, not half. The following change fixes the error:

    half  NdotL = half(dot(N, L));

Is there a chance this issue can be fixed? With all intrinsic converting halfs to floats, half floats are not very useful. I use GLSLang in the code and I set the EShMsgHlslEnable16BitTypes flag.

TheMostDiligent avatar Jun 19 '23 23:06 TheMostDiligent

It would be great if this long standing issue gets fixed. 🙏

speedym avatar Jun 20 '23 05:06 speedym

I would appreciate this being fixed, as well! For complete 16bit float support, am I correct that the low precision templated texture types are missing? When trying to parse a Texture2D<min16float4> I get the message error: 'basic type in texture' : Unimplemented

jspohr avatar Jun 22 '23 14:06 jspohr

Any ETA as of 2024? Still having the issue as well after all these years. Half-precision floats have their use on mobile :)

thomaspeer avatar Feb 03 '24 01:02 thomaspeer