Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precision issue with generated HLSL and float literal #779

Open
rickbrew opened this issue Mar 10, 2024 · 2 comments
Open

Precision issue with generated HLSL and float literal #779

rickbrew opened this issue Mar 10, 2024 · 2 comments
Labels
bug 🐛 Something isn't working untriaged 🧰 A new issue that needs initial triage

Comments

@rickbrew
Copy link
Collaborator

rickbrew commented Mar 10, 2024

This shader:

[D2DInputCount(0)]
[D2DGeneratedPixelShaderDescriptor]
internal readonly partial struct BadShader
    : ID2D1PixelShader
{
    public float4 Execute()
    {
        return new float4(256, 60000, 131072.65f, 1.0f);
    }
}

generates this HLSL:

        /// <inheritdoc/>
        [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", "3.0.0.0")]
        [global::System.Diagnostics.DebuggerNonUserCode]
        [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
        static string global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<BadShader>.HlslSource =>
            """
            #define D2D_INPUT_COUNT 0

            #include "d2d1effecthelpers.hlsli"

            D2D_PS_ENTRY(Execute)
            {
                return float4(256, 60000, 131072.66, 1.0);
            }
            """;

Notice how the float4 in the C# has 131072.65f but the HLSL says 131072.66.

If I change to 131072.64f then it correctly emits 131072.64.

@rickbrew rickbrew added bug 🐛 Something isn't working untriaged 🧰 A new issue that needs initial triage labels Mar 10, 2024
@rickbrew
Copy link
Collaborator Author

Now, to be fair, these two numbers do end up rounding to the same value, as evidenced by running this in the immediate window in VS:

131072.65f.ToString("N16")
"131,072.6562500000000000"
131072.66f.ToString("N16")
"131,072.6562500000000000"

But, I would still expect the generated HLSL to faithfully represent the C# code for something like this.

OR, I would expect it to emit 131072.65625 for both values

@rickbrew
Copy link
Collaborator Author

This came about while I was investigating #780 .

I don't think this issue (#779) is a high priority bug. HLSL is emitted that does not match the C# source, but it does produce the correct float value at runtime.

Instead, I consider this to be a debugging hazard: the HLSL will not match the C#, and it will be confusing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working untriaged 🧰 A new issue that needs initial triage
Projects
None yet
Development

No branches or pull requests

1 participant