Strange color output from Unorm method of SpriteBatch class


  • Getting close to wrapping up my game, I'm needing to modify the Spritebatch sample so that it supports animated sprites in all feature levels, DX 9_1, DX 9_3, DX 10_0 etc.

    Now, I have used the DirectX Control Panel to set the feature level for my application and found that it works in almost every level.  The level it struggles with is DX 9_3, where instancing is used.  But, my problem has nothing to do with animated sprites, instead my problem has to do with this method:

    unsigned int SpriteBatch::MakeUnorm(float4 color)
        unsigned int r = max(0, min(255, static_cast<unsigned int>(color.r * 255.0f)));
        unsigned int g = max(0, min(255, static_cast<unsigned int>(color.g * 255.0f)));
        unsigned int b = max(0, min(255, static_cast<unsigned int>(color.b * 255.0f)));
        unsigned int a = max(0, min(255, static_cast<unsigned int>(color.a * 255.0f)));
        return (a << 24) | (b << 16) | (g << 8) | r;

    This method converts the color fed into the pixel shader into an integer. (BTW, this method is used with all feature levels) So, the output of this method is this number 4294967295.  This is when the feature level is set to 9_3.

    But, when I set the feature level to something different like 10_1, this method returns: 3452816845.  Strange, same method.  I checked the parameter, it's the same, a float4(1.0f, 1.0f, 1.0f, 1.0f).

    Now when this integer is fed into the pixel shader, it causes the color to be transparent black, i.e. float4(0.0f, 0.0f, 0.0f, 0.0f), only on feature level 9_3.

    Very weird.

    Thursday, November 1, 2012 8:31 PM


  • Bit-wise operations are not 'natively' supported by Feature Level 9.x shaders, only FL 10.0 or later. You should probably find another way to do what you are trying to do more directly with HLSL, or have an alternative solution for FL 9x. vs. FL 10.x+

    Friday, November 2, 2012 10:46 PM