如何在 DirectX12 中读取子资源数据?

How to read subresource data in DirectX12?

我有一个 DDS 纹理,它是使用 Microsoft DDSTextureLoader 帮助程序库中的 CreateDDSTextureFromMemory12 函数创建的。纹理有 10 个 mipmap。我能够创建纹理并毫无问题地使用它。

我想做的是读取给定索引处特定 mipmap 的纹理数据。

我在这里尝试读取子资源索引 5 处的数据:

DirectX::CreateDDSTextureFromMemory12(
    g_device,
    g_cmd_list,
    &bytes.front(),
    file_size,
    texResource,
    tmpUploadHeap);

void* pData = nullptr;
texResource->ReadFromSubresource(pData, 64, 1, 5, nullptr);

但是我收到以下错误:

D3D12 ERROR: ID3D12Resource1::ID3D12Resource::ReadFromSubresource: 
ReadFromSubresource can not be called on a resource associated with a heap that has the CPU page properties of D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE. 
Heaps of the type D3D12_HEAP_TYPE_DEFAULT should be assumed to have these properties. 
[ RESOURCE_MANIPULATION ERROR #895: READFROMSUBRESOURCE_INVALIDRESOURCE]

最简单的方法是使用 github 上 DirectXTK12 项目的 LoadDDSTextureFromMemory。第5个参数returns一个std::vector.

中的所有子资源