0
$\begingroup$

I'm having a problem with loading texture in DX12 with DirectXTK. It seems to be loading all the metadata, since in PIX, I can see bound resource with correct width/height and format. But it's entirely black as if data wasn't actually copied to the resource:

PIX

Code:

// Resources
D3D12_DESCRIPTOR_HEAP_DESC srvHeapDesc = {};
srvHeapDesc.NumDescriptors = 1;
srvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
srvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
ThrowIfFailed(m_device->CreateDescriptorHeap(&srvHeapDesc, IID_PPV_ARGS(&m_srvHeap)));

ComPtr<ID3D12Resource> texture;
ComPtr<ID3D12Resource> uploadHeap;
const wchar_t* path = L"C:\some_path\example_image.png"; // Full file path

// Load file data and save to resource
std::unique_ptr<uint8_t[]> decodedData;
std::vector<D3D12_SUBRESOURCE_DATA> textureData;
D3D12_SUBRESOURCE_DATA textureDataSingle;
const CD3DX12_HEAP_PROPERTIES heapUpload = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);

HRESULT hr = LoadWICTextureFromFileEx(device, path, 0, flags, WIC_LOADER_DEFAULT, texture.ReleaseAndGetAddressOf(), decodedData, textureDataSingle);
ThrowIfFailed(hr);

const UINT64 uploadBufferSize = GetRequiredIntermediateSize(texture.Get(), 0, 1);
const CD3DX12_RESOURCE_DESC resourceDesc = CD3DX12_RESOURCE_DESC::Buffer(uploadBufferSize);

ThrowIfFailed(device->CreateCommittedResource(
    &heapUpload,
    D3D12_HEAP_FLAG_NONE,
    &resourceDesc,
    D3D12_RESOURCE_STATE_GENERIC_READ,
    nullptr,
    IID_PPV_ARGS(&uploadHeap)
));

UpdateSubresources(commandList.Get(), texture.Get(), uploadHeap.Get(), 0, 0, 1, &textureDataSingle);
const CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(texture.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
commandList->ResourceBarrier(1, &barrier);

// Create SRV
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = { texture->GetDesc().Format, D3D12_SRV_DIMENSION_TEXTURE2D, D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING };
srvDesc.Texture2D.MipLevels = 1;
CD3DX12_CPU_DESCRIPTOR_HANDLE srvHandle(srvHeap->GetCPUDescriptorHandleForHeapStart(), srvIndex, device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
device->CreateShaderResourceView(texture.Get(), &srvDesc, srvHandle);

// Binding
ID3D12DescriptorHeap* ppHeaps[] = { m_srvHeap.Get() };
m_commandList->SetDescriptorHeaps(_countof(ppHeaps), ppHeaps);
CD3DX12_GPU_DESCRIPTOR_HANDLE srvHandle0(m_srvHeap->GetGPUDescriptorHandleForHeapStart(), 0, m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
m_commandList->SetGraphicsRootDescriptorTable(3, srvHandle0);
$\endgroup$

1 Answer 1

0
$\begingroup$

The problem was that I didn't call "ExecuteCommandLists" at the very end of function declaring and creating resources. I did execute command lists every frame but first I clear it and then record per frame commands.

Not sure why other stuff worked so far i.e. CBVs, device creation.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.