Graphics 将HLSL中的浮点转换为unorm

Graphics 将HLSL中的浮点转换为unorm,graphics,floating-point,type-conversion,hlsl,Graphics,Floating Point,Type Conversion,Hlsl,我正在尝试在像素着色器中实现我自己的深度模板功能 我有一个标准化的深度值,我想将其转换为整数,以便使用SM5 InterlockedMin()操作,如下所示: uint d_uint = (uint)(dnorm * 4294967295); uint d_uint_original = 0; InterlockedMin(depthmask[c], d_uint, d_uint_original); if (d_uint < d_uint_original) { //if true,

我正在尝试在像素着色器中实现我自己的深度模板功能

我有一个标准化的深度值,我想将其转换为整数,以便使用SM5 InterlockedMin()操作,如下所示:

uint d_uint = (uint)(dnorm * 4294967295);
uint d_uint_original = 0;

InterlockedMin(depthmask[c], d_uint, d_uint_original);

if (d_uint < 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;
}
uint d_uint=(uint)(dnorm*4294967295);
uint d_uint_original=0;
InterlocatedMin(深度任务[c],数据单元,数据单元原件);
如果(d_uint
其中
dnorm
是深度值

然而,
uint d_uint=(uint)(dnorm*4294967295)仅从0开始计算

我知道这一点,因为我可以在RenderDoc中查看缓冲区,并且只能看到着色器写入0。我还可以减少常数,比如说229496729,它会正确地写出它。如果我将其设置为2294967295,但它不会

我知道我被浮点量化问题绊倒了,但不知道怎么做

在整数数组中对归一化值进行编码的正确方法是什么?


我知道DX,也许我需要休息一下,但从文档中不清楚它们应该如何使用,特别是将其转换为UINT以便与联锁功能一起使用。

在您的情况下,由于我假设dnorm>0,您也可以简单地使用

uint depthAsUint = asuint(dnorm);
因为对于正浮点数,二进制表示满足比较运算符。 例如:

asuint(0.1f)
asuint(0.1f) < asuint(0.5f) = true