title: Convert float to unorm in HLSL
body: <p>I am trying to implement my own depth stencil like functionality in a pixel shader.</p>
  
  <p>I have a normalised depth value, and I want to convert it to an integer in order to use the SM5 InterlockedMin() operation, like so:</p>
  
  <pre><code>uint d_uint = (uint)(dnorm * 4294967295);
  uint d_uint_original = 0;
  
  InterlockedMin(depthmask[c], d_uint, d_uint_original);
  
  if (d_uint &lt; d_uint_original) { //if true, we will have written a new depth into the mask, so write the element to the field
      field[c].x = d;
      field[c].yzw = i.n;
  }
  </code></pre>
  
  <p>Where <code>dnorm</code> is the depth value.</p>
  
  <p>However, <code>uint d_uint = (uint)(dnorm * 4294967295);</code> only ever evaulates to 0.</p>
  
  <p>I know this because I can view the buffer in RenderDoc and see the shader only writes 0. I can also reduce the constant, to say 229496729, and it will write it correctly. If I set it to 2294967295 however it does not.</p>
  
  <p>I know I am tripping over a floating point quantisation issue, but don't know how.</p>
  
  <p><strong>What is the correct way to encode a normalised value in an integer array?</strong></p>
  
  <p>I am aware of DX's <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd607323(v=vs.85).aspx" rel="nofollow noreferrer">UNORM/SNORM</a>, and maybe I need a break, but its not clear from that documentation how they should be used, specifically converting one to UINT in order to be used with the interlocked functions.</p>