Search code examples
c++shaderdirectxhlslcompute-shader

Count elements in texture


I have a 3D texture of 32-bit unsigned integers initialized with zeroes. It is defined as follows:

D3D11_TEXTURE3D_DESC description{};
description.Format = DXGI_FORMAT_R32_UINT;
description.Usage = D3D11_USAGE_DEFAULT;
description.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
description.CpuAccessFlags = 0;
description.MipLevels = 1;
description.Width = ...;
description.Height = ...;
description.Depth = ...;

I am writing to this texture in a compute shader to set a bit on specified position if a certain condition is fulfilled:

RWTexture3D<uint> txOutput : register(u0)

cbuffer InputBuffer : register(b0)
{
    uint position;
    /** other elements **/
}

#define SET_BIT(value, position) value |= (1U << position)

[numthreads(8, 8, 8)]
void main(uint3 threadID : SV_DispatchThreadID)
{
    if(/** some condition **/)
    {
        uint value = txOutput[threadID];
        SET_BIT(value, position);
        txOutput[threadID] = value;
    }
}

I need to know how many elements of this texture is filled at a certain bit position in a code behind in C++. How could this be done?


Solution

  • You will have to read back the texture to the cpu with the ID3D11DeviceContext::Map API https://learn.microsoft.com/en-us/windows/win32/api/d3d11/nf-d3d11-id3d11devicecontext-map

    You will get out a void* you will cast to uint32_t* which will point to the start of your data.

    You need to get better a looking up the DirectX documentation, its really quite good documentation. There are a lot harder things you will need to find in the documentation if you keep doing 3D graphics.