You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用微软HLSL示例脚本遇位移类型歧义错误求助

解决HLSL中位移操作的类型歧义错误

错误原因

你遇到的ambiguous type for bit shift错误,本质是HLSL的类型规则导致的:代码里的1、2048这类整数字面量默认是有符号int类型,但和它们做位移运算的是uint(无符号整数)类型的变量/返回值,编译器无法确定应该用哪种类型执行位移操作,因此抛出这个错误。

修复方法

解决这个问题很简单,只需要明确字面量的类型,有两种方式:

  • 给字面量添加U后缀,标记为无符号整数
  • 把字面量强制转换为uint类型

针对错误行的具体修改

  1. 第39行错误:原代码uint mask = (1 << firstbithigh(i)) - 1;
    修改为(二选一即可):

    uint mask = (1U << firstbithigh(i)) - 1U;
    

    或者:

    uint mask = ((uint)1 << firstbithigh(i)) - (uint)1;
    
  2. 第53行错误:原代码uint k = 2048 << IN.groupIndex;
    修改为(二选一即可):

    uint k = 2048U << IN.groupIndex;
    

    或者:

    uint k = ((uint)2048) << IN.groupIndex;
    

修改后的完整代码

#include "cs.hlsli"
#include "bitonic_sort_rs.hlsli"
#include "indirect.hlsli"

ConstantBuffer<bitonic_sort_cb> cb                      : register(b0);
StructuredBuffer<uint> counterBuffer                    : register(t0);
RWStructuredBuffer<D3D12_DISPATCH_ARGUMENTS> dispatch   : register(u0);

uint nextPowerOfTwo(uint i)
{
    uint mask = (1U << firstbithigh(i)) - 1U;  // 已修复
    return (i + mask) & ~mask;
}

[numthreads(22, 1, 1)]
[RootSignature(BITONIC_SORT_RS)]
void main(cs_input IN)
{
    if (IN.groupIndex >= cb.maxNumIterations)
    {
        return;
    }

    uint listCount = counterBuffer[cb.counterOffset];
    uint k = 2048U << IN.groupIndex;  // 已修复

    // We need one more iteration every time the number of thread groups doubles.
    if (k > nextPowerOfTwo((listCount + 2047) & ~2047))
    {
        listCount = 0;
    }

    uint offset = IN.groupIndex * (IN.groupIndex + 1) / 2;

    // Generate outer sort dispatch arguments.
    for (uint j = k / 2; j > 1024; j /= 2)
    {
        // All of the groups of size 2j that are full.
        uint completeGroups = (listCount & ~(2 * j - 1)) / 2048;

        // Remaining items must only be sorted if there are more than j of them.
        uint partialGroups = ((uint)max(int(listCount - completeGroups * 2048 - j), 0) + 1023) / 1024;

        D3D12_DISPATCH_ARGUMENTS arguments = { completeGroups + partialGroups, 1, 1 };
        dispatch[offset] = arguments;

        ++offset;
    }

    // The inner sort always sorts all groups (rounded up to multiples of 2048).
    D3D12_DISPATCH_ARGUMENTS arguments = { (listCount + 2047) / 2048, 1, 1 };
    dispatch[offset] = arguments;
}

内容的提问来源于stack exchange,提问作者crazyname524

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 19:31:35