directx 12 – How to get data from ID3D12Resource data from gpu to cpu buffer?

I want to get cpu-readable buffer from D3D12 texture (dimension is D3D12_RESOURCE_DIMENSION_TEXTURE2D which can not directly accessed by cpu).
So I tried to copy the texture to an intermediate texture with a readback heap type.

Here is the code:

ID3D12Resource* newTex;
hr = GetD3D12Device()->CreateCommittedResource(
    &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK),
    D3D12_HEAP_FLAG_NONE,
    &CD3DX12_RESOURCE_DESC::Buffer(GetRequiredIntermediateSize(texture, 0, 1)),
    D3D12_RESOURCE_STATE_COPY_DEST,
    nullptr,
    IID_PPV_ARGS(&newTex));
if (hr < 0) {
    return;
}

D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
srcLocation.pResource = texture;
srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;

D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
dstLocation.pResource = newTex;
dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;

commandList_->CopyTextureRegion(&dstLocation, 0, 0, 0, &srcLocation, nullptr);
void* pData;
hr = newTex->Map(0, nullptr, &pData);
if (hr < 0) {
    return;
}

Here is the error log:

D3D12 ERROR: ID3D12CommandList::CopyTextureRegion: D3D12_SUBRESOURCE_FOOTPRINT::Format is not supported at the current feature level with the dimensionality implied by the D3D12_SUBRESOURCE_FOOTPRINT::Height and D3D12_SUBRESOURCE_FOOTPRINT::Depth. Format = UNKNOWN, Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D, Height = 0, Depth = 0, and FeatureLevel is D3D_FEATURE_LEVEL_12_2.

What am i missing? Or Is it practical to get the texture to a cpu-readable buffer?

Read more here: Source link