Jump to content
  • Advertisement
Sign in to follow this  
maxest

Faulty D3DCOMPILE_SKIP_OPTIMIZATION

This topic is 901 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys. I have this very simple shader:

    PS_OUTPUT output = (PS_OUTPUT)0;
    
    float4 sample1 = tex1.SampleLevel(pointClampSampler, input.texCoord, 0);
    output.color = sample1;
    
    float4 sample2 = tex2.SampleLevel(pointClampSampler, input.texCoord, 0);
    if (sample2.x < 0.0)
        sample2.x = 0.0;
    if (sample2.y < 0.0)
        sample2.y = 0.0;
    if (sample2.z < 0.0)
        sample2.z = 0.0;
    if (sample2.w < 0.0)
        sample2.w = 0.0;    
    output.color += sample2;
    
    return output;

What is important here is that tex2 can contain negative values which I don't want so that's why I do the four comparisons.

 

Now, when I enable D3DCOMPILE_SKIP_OPTIMIZATION these all four comparisons are in the assembly code. When I don't enable the option the compiler uses max to get zeroes. Sounds legit, right? The problem is however that depending on whether I enable D3DCOMPILE_SKIP_OPTIMIZATION or not I get different results. The variant with D3DCOMPILE_SKIP_OPTIMIZATION, surprisingly, works incorrectly. Isn't this a compiler bug?

 

I enclosed the two disassemblies. normal.txt is without D3DCOMPILE_SKIP_OPTIMIZATION whereas skip_optimization.txt is with D3DCOMPILE_SKIP_OPTIMIZATION.

Share this post


Link to post
Share on other sites
Advertisement

Hm, I think I've just solved the puzzle, to some extent. The value that are displayed differently, depending on the version, are NaNs, so apparently if's and max treat them differently, hence the difference in result.

Share this post


Link to post
Share on other sites

Quite possible, yes.

 

Comparing a NaN to any number will return false, so the values will remain NaN. However performing max(NaN, 0) is undefined, so may well return 0 if you're lucky.

Share this post


Link to post
Share on other sites
You should max() all 4 values at once with sample2 = max(sample2, 0);. As a side note. After getting rid of NaN.


L. Spiro

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!