1

我正在尝试从 DirectX11 计算着色器中的 texture2d 读取一些数据,但是,即使纹理对象填充了相同的浮点数,texture2D 对象的“加载”函数仍然返回 0。

这是一个 160 * 120 的纹理 DXGI_FORMAT_R32G32B32A32_FLOAT。以下代码是我创建此资源的方式:

HRESULT TestResources(ID3D11Device* pd3dDevice, ID3D11DeviceContext* pImmediateContext) {
    float *test = new float[4 * 80 * 60 * 4]; // 80 * 60, 4 channels, 1 big texture contains 4 80 * 60 subimage
    for (int i = 0; i < 4 * 80 * 60 * 4; i++) test[i] = 0.7f;

    HRESULT hr = S_OK;
    D3D11_TEXTURE2D_DESC RTtextureDesc;
    ZeroMemory(&RTtextureDesc, sizeof(D3D11_TEXTURE2D_DESC));
    RTtextureDesc.Width = 160;
    RTtextureDesc.Height = 120;
    RTtextureDesc.MipLevels = 1;
    RTtextureDesc.ArraySize = 1;
    RTtextureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;

    RTtextureDesc.SampleDesc.Count = 1;
    RTtextureDesc.SampleDesc.Quality = 0;
    RTtextureDesc.Usage = D3D11_USAGE_DYNAMIC;
    RTtextureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    RTtextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    RTtextureDesc.MiscFlags = 0;

    D3D11_SUBRESOURCE_DATA InitData;
    InitData.pSysMem = test;
    InitData.SysMemPitch = sizeof(float) * 4;
    V_RETURN(pd3dDevice->CreateTexture2D(&RTtextureDesc, &InitData, &m_pInputTex2Ds));
    //V_RETURN(pd3dDevice->CreateTexture2D(&RTtextureDesc, NULL, &m_pInputTex2Ds));

    D3D11_SHADER_RESOURCE_VIEW_DESC SRViewDesc;
    ZeroMemory(&SRViewDesc, sizeof(SRViewDesc));
    SRViewDesc.Format = RTtextureDesc.Format;
    SRViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
    SRViewDesc.Texture2D.MostDetailedMip = 0;
    SRViewDesc.Texture2D.MipLevels = 1;
    V_RETURN(pd3dDevice->CreateShaderResourceView(m_pInputTex2Ds, &SRViewDesc, &m_pInputTexSRV));

    delete[] test;
    return hr;
}

然后我尝试使用 X = Y = 2 和 Z = 1 运行调度,如下所示:

void ComputeShaderReduction::ExecuteComputeShader(ID3D11DeviceContext* pd3dImmediateContext, UINT uInputNum, ID3D11UnorderedAccessView** ppUAVInputs, UINT X, UINT Y, UINT Z) {
    pd3dImmediateContext->CSSetShader(m_pComputeShader, nullptr, 0);

    pd3dImmediateContext->CSSetShaderResources(0, 1, &m_pInputTexSRV); // test code

    pd3dImmediateContext->CSSetUnorderedAccessViews(0, uInputNum, ppUAVInputs, nullptr);
    //pd3dImmediateContext->CSSetUnorderedAccessViews(0, 1, &m_pGPUOutUAVs, nullptr);

    pd3dImmediateContext->UpdateSubresource(m_pConstBuf, 0, nullptr, &m_ConstBuf, 0, 0);
    pd3dImmediateContext->CSSetConstantBuffers(0, 1, &m_pConstBuf);
    pd3dImmediateContext->Dispatch(X, Y, Z);

    pd3dImmediateContext->CSSetShader(nullptr, nullptr, 0);

    ID3D11UnorderedAccessView* ppUAViewnullptr[1] = { nullptr };
    pd3dImmediateContext->CSSetUnorderedAccessViews(0, 1, ppUAViewnullptr, nullptr);

    ID3D11ShaderResourceView* ppSRVnullptr[1] = { nullptr };
    pd3dImmediateContext->CSSetShaderResources(0, 1, ppSRVnullptr);

    ID3D11Buffer* ppCBnullptr[1] = { nullptr };
    pd3dImmediateContext->CSSetConstantBuffers(0, 1, ppCBnullptr);
}

我编写了一个非常简单的 CS 着色器来尝试获取 texture2d 中的数据并将其输出。所以,计算着色器看起来像这样:

#define subimg_dim_x 80
#define subimg_dim_y 60

Texture2D<float4> BufferIn : register(t0);
StructuredBuffer<float> Test: register(t1);
RWStructuredBuffer<float> BufferOut : register(u0);

groupshared float sdata[subimg_dim_x];

[numthreads(subimg_dim_x, 1, 1)]
void CSMain(uint3 DTid : SV_DispatchThreadID,
            uint3 threadIdx : SV_GroupThreadID,
            uint3 groupIdx  : SV_GroupID) {

    sdata[threadIdx.x] = 0.0;
    GroupMemoryBarrierWithGroupSync();

    if (threadIdx.x == 0) {
        float4 num = BufferIn.Load(uint3(groupIdx.x, groupIdx.y, 1));
        //BufferOut[groupIdx.y * 2 + groupIdx.x] = 2.0; //This one gives me 2.0 as output in the console
        BufferOut[groupIdx.y * 2 + groupIdx.x] = num.x; //This one keeps giving me 0.0 and in the texture, r = g = b = a = 0.7 or x = y = z = w = 0.7, so it suppose to print 0.7 in the console.
    }
    GroupMemoryBarrierWithGroupSync();
}

我认为我在 CPU 端打印 CS 着色器结果的方式是正确的。

void ComputeShaderReduction::CopyToCPUBuffer(ID3D11Device* pdevice, ID3D11DeviceContext* pd3dImmediateContext, ID3D11Buffer* pGPUOutBufs) {
    D3D11_BUFFER_DESC desc;
    ZeroMemory(&desc, sizeof(desc));
    pGPUOutBufs->GetDesc(&desc);
    desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
    desc.Usage = D3D11_USAGE_STAGING;
    desc.BindFlags = 0;
    desc.MiscFlags = 0;
    if (!m_pCPUOutBufs && SUCCEEDED(pdevice->CreateBuffer(&desc, nullptr, &m_pCPUOutBufs))) {
        pd3dImmediateContext->CopyResource(m_pCPUOutBufs, pGPUOutBufs);
    }
    else pd3dImmediateContext->CopyResource(m_pCPUOutBufs, pGPUOutBufs);

    D3D11_MAPPED_SUBRESOURCE MappedResource;
    float *p;
    pd3dImmediateContext->Map(m_pCPUOutBufs, 0, D3D11_MAP_READ, 0, &MappedResource);
    p = (float*)MappedResource.pData;
    for (int i = 0; i < 4; i++) printf("%d %f\n", i, p[i]);
    pd3dImmediateContext->Unmap(m_pCPUOutBufs, 0);
    printf("\n");
}

绑定到 UAV 的缓冲区只有 4 个元素。所以,如果我的 texture2d 中的所有浮点数都是 0.7,我应该在 CopyToCPUBuffer 函数中打印 4 个 0.7s 而不是 0.0s。

有谁知道我的代码可能有什么问题,或者有人可以为我提供一个完整的示例或教程,说明如何在计算着色器中正确读取 DirectX 11 texture2d 的数据?

提前致谢。

4

1 回答 1

1

以下是错误的开始。输入数据的 Pitch 是纹理每行的字节数,而不是每个像素。

InitData.SysMemPitch = sizeof(float) * 4;

第二:

float4 num = BufferIn.Load(uint3(groupIdx.x, groupIdx.y, 1));

您正在尝试从纹理的第二个 mip 加载数据,它只有 1 个 mip 级别。

于 2015-04-12T15:40:25.560 回答