Skip to content

Commit

Permalink
Fix current back buffer index being out of date in "present" event in…
Browse files Browse the repository at this point in the history
… D3D12/Vulkan
  • Loading branch information
crosire committed Sep 16, 2022
1 parent d559fe4 commit d2d9ae4
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 9 deletions.
11 changes: 7 additions & 4 deletions source/d3d12/d3d12_impl_swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ void reshade::d3d12::swapchain_impl::set_back_buffer_color_space(DXGI_COLOR_SPAC
{
_back_buffer_color_space = convert_color_space(type);
}
void reshade::d3d12::swapchain_impl::set_current_back_buffer_index(uint32_t index)
{
_swap_index = index;
}

bool reshade::d3d12::swapchain_impl::on_init()
{
Expand Down Expand Up @@ -97,6 +101,9 @@ bool reshade::d3d12::swapchain_impl::on_init()

assert(swap_desc.BufferUsage & DXGI_USAGE_RENDER_TARGET_OUTPUT);

// Initialize with the first back buffer index
_swap_index = _orig->GetCurrentBackBufferIndex();

#if RESHADE_ADDON
invoke_addon_event<addon_event::init_swapchain>(this);
#endif
Expand All @@ -122,10 +129,6 @@ void reshade::d3d12::swapchain_impl::on_reset()

void reshade::d3d12::swapchain_impl::on_present()
{
// There is no swap chain in d3d12on7
if (_orig != nullptr)
_swap_index = _orig->GetCurrentBackBufferIndex();

if (!is_initialized())
return;

Expand Down
1 change: 1 addition & 0 deletions source/d3d12/d3d12_impl_swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace reshade::d3d12
uint32_t get_current_back_buffer_index() const final;

void set_back_buffer_color_space(DXGI_COLOR_SPACE_TYPE type);
void set_current_back_buffer_index(uint32_t index);

bool on_init();
void on_reset();
Expand Down
3 changes: 3 additions & 0 deletions source/dll_main_test_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nCmdShow
if (!is_d3d12on7)
{
HR_CHECK(swapchain->GetBuffer(i, IID_PPV_ARGS(&backbuffers[i])));

const std::wstring debug_name = L"Back buffer " + std::to_wstring(i);
backbuffers[i]->SetName(debug_name.c_str());
}
else
{
Expand Down
4 changes: 4 additions & 0 deletions source/dxgi/dxgi_swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ void DXGISwapChain::runtime_present(UINT flags, [[maybe_unused]] const DXGI_PRES

void DXGISwapChain::handle_device_loss(HRESULT hr)
{
// Update current back buffer index after presentation
if (_direct3d_version == 12)
static_cast<reshade::d3d12::swapchain_impl *>(_impl)->set_current_back_buffer_index(static_cast<IDXGISwapChain3 *>(_orig)->GetCurrentBackBufferIndex());

if (!_impl->is_initialized())
return;

Expand Down
5 changes: 4 additions & 1 deletion source/vulkan/vulkan_hooks_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,9 @@ VkResult VKAPI_CALL vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPr
};
}

// TODO: Ideally this should already be set in 'vkAcquireNextImageKHR'
swapchain_impl->set_current_back_buffer_index(pPresentInfo->pImageIndices[i]);

reshade::invoke_addon_event<reshade::addon_event::present>(
queue_impl,
swapchain_impl,
Expand All @@ -998,7 +1001,7 @@ VkResult VKAPI_CALL vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPr
dirty_rect_count,
dirty_rect_count != 0 ? dirty_rects.p : nullptr);
#endif
swapchain_impl->on_present(queue, pPresentInfo->pImageIndices[i], const_cast<VkSemaphore *>(present_info.pWaitSemaphores), present_info.waitSemaphoreCount);
swapchain_impl->on_present(queue, const_cast<VkSemaphore *>(present_info.pWaitSemaphores), present_info.waitSemaphoreCount);
}
}

Expand Down
9 changes: 6 additions & 3 deletions source/vulkan/vulkan_impl_swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ uint32_t reshade::vulkan::swapchain_impl::get_current_back_buffer_index() const
return _swap_index;
}

void reshade::vulkan::swapchain_impl::set_current_back_buffer_index(uint32_t index)
{
_swap_index = index;
}

bool reshade::vulkan::swapchain_impl::on_init(VkSwapchainKHR swapchain, const VkSwapchainCreateInfoKHR &desc, HWND hwnd)
{
_orig = swapchain;
Expand Down Expand Up @@ -123,10 +128,8 @@ void reshade::vulkan::swapchain_impl::on_reset()
semaphore = VK_NULL_HANDLE;
}

void reshade::vulkan::swapchain_impl::on_present(VkQueue queue, const uint32_t swapchain_image_index, VkSemaphore *wait_semaphores, uint32_t &num_wait_semaphores)
void reshade::vulkan::swapchain_impl::on_present(VkQueue queue, VkSemaphore *wait_semaphores, uint32_t &num_wait_semaphores)
{
_swap_index = swapchain_image_index;

if (!is_initialized())
return;

Expand Down
4 changes: 3 additions & 1 deletion source/vulkan/vulkan_impl_swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ namespace reshade::vulkan
uint32_t get_back_buffer_count() const final;
uint32_t get_current_back_buffer_index() const final;

void set_current_back_buffer_index(uint32_t index);

bool on_init(VkSwapchainKHR swapchain, const VkSwapchainCreateInfoKHR &desc, HWND hwnd);
void on_reset();

void on_present(VkQueue queue, const uint32_t swapchain_image_index, VkSemaphore *wait_semaphores, uint32_t &num_wait_semaphores);
void on_present(VkQueue queue, VkSemaphore *wait_semaphores, uint32_t &num_wait_semaphores);

private:
uint32_t _swap_index = 0;
Expand Down

0 comments on commit d2d9ae4

Please sign in to comment.