从 HLSL 中的 double 读取位

问题描述 投票:0回答:1

我正在尝试将我的位库从 C# 转换为 HLSL,但我无法转换 2 个在双精度上使用按位运算符的函数

    static public int getBits(int start, int end, long value)
    {
        long mask = ((1L << (end - start + 1)) - 1) << start;
        int result = (int)((value & mask) >> start);

        return result;
    }

    static public int getBits(int start, int end, long value)
    {
        long mask = ((1L << (end - start + 1)) - 1) << start;
        int result = (int)((value & mask) >> start);

        return result;
    }

我已经尝试过了

int getBits(int start, int end, double value)
{
    uint mask = ((1 << (end - start + 1)) - 1) << start;
    uint valueAsUintLow, valueAsUintHigh;
    asuint(value, valueAsUintLow, valueAsUintHigh); // Extract the low and high 32 bits of value

    uint maskedValueLow = valueAsUintLow & mask; // Get the masked bits from the low bits
    uint maskedValueHigh = valueAsUintHigh & (mask >> 32); // Get the masked bits from the high bits

    int result = (int)(maskedValueLow | (maskedValueHigh << start)); // Combine the low and high bits

    return result;
}

但它总是返回错误的int

我希望它返回与 C# 脚本返回的内容相同的内容

bit-manipulation bit hlsl
1个回答
0
投票

在您的 C# 代码中,您正在对 Long 进行按位运算,该 Long 是一个基于 2 存储的整数。在您的 HSLS 代码中,您正在对使用浮点表示法存储的 double 进行按位运算。您试图操纵底层位,但这些底层位并不相同,这就是为什么您会得到不同的结果。

它们至少都需要是整数,但为了获得一致的结果,您可能需要将 C# 值设为 Int32,将 HSLS 值设为 int。

© www.soinside.com 2019 - 2024. All rights reserved.