首页 > 解决方案 > 为什么在代码和环境中使用 cs_5_0 时出现错误“X3666 cs_4_0 不支持无人机类型”?

问题描述

我正在尝试通过复制此视频中的示例来编译计算着色器。

https://channel9.msdn.com/Blogs/gclassy/DirectCompute-Lecture-Series-120-Basics-of-DirectCompute-Application-Development

我在编译时收到以下错误消息:

Error X3666    cs_4_0 does not support typed UAVs

这个错误来自哪里?配置为 cs_5_0(在 VS2019 中的代码中)。这是代码的编译部分。

HRESULT DirectCompute::CreateShaders()
{
    HRESULT hr = S_OK;

    ID3DBlob* CS, * csError;

    hr = D3DCompileFromFile(L"ComputeShader.hlsl", NULL, NULL, "main", "cs_5_0", NULL, NULL,
        &CS, &csError);

    if (csError) {
        MessageBox(NULL, L"Failed compiling computer shader", L"Error", MB_OK);
        csError->Release();
    }

    hr = device->CreateComputeShader(
        CS->GetBufferPointer(),
        CS->GetBufferSize(),
        NULL,
        pComputeShader.GetAddressOf());

    context->CSSetShader(pComputeShader.Get(), NULL, 0);
    context->CSSetConstantBuffers(0, 1, pConstantBuffer.GetAddressOf());

    ID3D11ShaderResourceView* aSRViews[2] = { pBufferA_SRV.Get(), pBufferB_SRV.Get() };
    context->CSSetShaderResources(0, 2, aSRViews);

    ID3D11UnorderedAccessView* aUAViews[1] = { pBufferOut_UAV.Get() };
    context->CSSetUnorderedAccessViews(0, 1, aUAViews, NULL);


    context->Dispatch(matB_width, matA_height, 1);

    ID3D11ShaderResourceView* aSRViewsNULL[1] = { NULL };
    context->CSSetShaderResources(0, 1, aSRViewsNULL);

    ID3D11UnorderedAccessView* aUAViewsNULL[1] = { NULL };
    context->CSSetUnorderedAccessViews(0, 1, aUAViewsNULL, NULL);

    return hr;
}

如果移除着色器中的 RWBuffer 引用,则不会出错。

cbuffer SampleCB: register(b0)
{
    uint WidthA;
    uint HeightA;
    uint WidthB;
    uint HeightB;
    uint WidthOut;
    uint HeightOut;
};

struct SimpleBufType
{
    float val;
};

StructuredBuffer<SimpleBufType> MatrixA : register(t0);
Buffer<float> MatrixB: register(t1);

RWBuffer<float> Output : register(u0); //compiles if this is removed (as well as the output below)


[numthreads(1, 1, 1)]
void main( uint3 DTid : SV_DispatchThreadID )
{
    if (DTid.x < WidthB && DTid.y < HeightA)
    {
        float sum = 0;

        for (uint i = 0; i != WidthA; i++)
        {
            uint addrA = DTid.y * WidthA + i;
            uint addrB = DTid.x + i * WidthB;

            sum += MatrixA[addrA].val * MatrixB[addrB];
        }
        Output[DTid.y * WidthOut + DTid.x] = sum;
    }
}

我相信 Visual Studio 配置正确,我可以在另一个程序中使用着色器 5.0。

Visual Studio 2019 设置

知道是什么导致了错误吗?如何确保将 ComputeShader 定义为 cs_5_0 着色器?

标签: c++winapidirectxhlslcompute-shader

解决方案


切换了RWBufferforRWStructuredBuffer并且可以正常工作

仍在寻找有关错误消息的指示以及导致 RWBuffer 问题的原因。


推荐阅读