Skip to content

Commit

Permalink
Fix only 1 texture being available
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamashi committed Feb 28, 2022
1 parent 04f7046 commit 0e10d09
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/d3d12/D3D12_Functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ bool D3D12::Initialize(IDXGISwapChain* apSwapChain)

D3D12_DESCRIPTOR_HEAP_DESC srvdesc = {};
srvdesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
srvdesc.NumDescriptors = 2;
srvdesc.NumDescriptors = 200;
srvdesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
if (FAILED(m_pd3d12Device->CreateDescriptorHeap(&srvdesc, IID_PPV_ARGS(&m_pd3dSrvDescHeap))))
{
Expand Down
12 changes: 9 additions & 3 deletions src/d3d12/D3D12_Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,17 @@ ID3D12Device* D3D12::GetDevice() const
std::tuple<D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_GPU_DESCRIPTOR_HANDLE> D3D12::CreateTextureDescriptor()
{
const UINT handle_increment = m_pd3d12Device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
constexpr int descriptor_index = 1;
static std::atomic descriptor_index = 1;

const auto index = descriptor_index++;

if (index >= 200)
return {{}, {}};

D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle = m_pd3dSrvDescHeap->GetCPUDescriptorHandleForHeapStart();
cpuHandle.ptr += (handle_increment * descriptor_index);
cpuHandle.ptr += (handle_increment * index);
D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle = m_pd3dSrvDescHeap->GetGPUDescriptorHandleForHeapStart();
gpuHandle.ptr += (handle_increment * descriptor_index);
gpuHandle.ptr += (handle_increment * index);

return {cpuHandle, gpuHandle};
}
Expand Down
10 changes: 8 additions & 2 deletions src/scripting/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ std::shared_ptr<Texture> Texture::Load(const std::string& acPath)
if (d3d_device == nullptr)
return {};

auto [srvCpuHandle, srvGpuHandle] = CET::Get().GetD3D12().CreateTextureDescriptor();

if (srvCpuHandle.ptr == 0 || srvGpuHandle.ptr == 0)
{
spdlog::error("maximum number of textures reached!");
return {};
}

// Load from disk into a raw RGBA buffer
int image_width = 0;
int image_height = 0;
Expand Down Expand Up @@ -170,8 +178,6 @@ std::shared_ptr<Texture> Texture::Load(const std::string& acPath)
fence->Release();
uploadBuffer->Release();

auto [srvCpuHandle, srvGpuHandle] = CET::Get().GetD3D12().CreateTextureDescriptor();

// Create a shader resource view for the texture
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
ZeroMemory(&srvDesc, sizeof(srvDesc));
Expand Down

0 comments on commit 0e10d09

Please sign in to comment.