2017-02-26 42 views
0

我想實現我自己的深度模具就像在像素着色器的功能。轉換浮到unorm在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, 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; 
} 

哪裏dnorm是深度值。

然而,uint d_uint = (uint)(dnorm * 4294967295);永遠只能evaulates 0

我知道這是因爲我可以查看RenderDoc緩衝區,並看到着色器只寫0。我還可以減少不變,說229496729,它會它正確地寫。如果我將它設置爲2294967295但事實並非如此。

我知道我絆倒浮點量化的問題,但不知道怎麼辦。

什麼是編碼在一個整數數組的歸一化值的正確方法?

我知道DX的UNORM/SNORM的,也許我需要休息,但它不是從文件,他們應該如何使用,爲了明確轉換一個UINT明確與互鎖功能一起使用。

回答

0

在你的情況,因爲我想,dnorm> 0,你也可以簡單地使用

uint depthAsUint = asuint(dnorm); 

因爲正浮點數,二進制表示滿足比較運算。 例如:

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