Skip to content

Commit

Permalink
Add "reshade_present" event and API to get window handle from swapchain
Browse files Browse the repository at this point in the history
  • Loading branch information
crosire committed Jan 16, 2022
1 parent bad57c2 commit 5b49eff
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 3 deletions.
11 changes: 11 additions & 0 deletions include/reshade_api_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ namespace reshade::api
/// </summary>
/// <param name="index">Index of the back buffer. This has to be between zero and the value returned by <see cref="get_back_buffer_count"/>.</param>
virtual resource get_back_buffer(uint32_t index) = 0;

/// <summary>
/// Gets the number of back buffer resources in this swap chain.
/// </summary>
Expand All @@ -954,5 +955,15 @@ namespace reshade::api
/// Gets the index of the back buffer resource that can currently be rendered into.
/// </summary>
virtual uint32_t get_current_back_buffer_index() const = 0;

/// <summary>
/// Gets the HWND of the window this swap chain was created with, or <see langword="nullptr"/> if none exists.
/// </summary>
virtual void *get_window_handle() const = 0;

/// <summary>
/// Gets the description of the back buffer resources in this swap chain.
/// </summary>
inline resource_desc get_back_buffer_resource_desc() { return get_device()->get_resource_desc(get_current_back_buffer()); }
};
}
13 changes: 10 additions & 3 deletions include/reshade_events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ namespace reshade
/// <item><description>IDXGIFactory2::CreateSwapChain(...)</description></item>
/// <item><description>vkCreateSwapchainKHR</description></item>
/// </list>
/// <para>Callback function signature: <c>bool (api::resource_desc &amp;buffer_desc, void *hwnd)</c></para>
/// <para>Callback function signature: <c>bool (api::resource_desc &amp;back_buffer_desc, void *window_handle)</c></para>
/// </summary>
/// <remarks>
/// To overwrite the swap chain description, modify <c>buffer_desc</c> in the callback and return <see langword="true"/>, otherwise return <see langword="false"/>.
/// To overwrite the swap chain description, modify <c>back_buffer_desc</c> in the callback and return <see langword="true"/>, otherwise return <see langword="false"/>.
/// Is not called in OpenGL (since it is not possible to influence swap chain creation there).
/// </remarks>
create_swapchain,
Expand Down Expand Up @@ -1390,6 +1390,12 @@ namespace reshade
/// </summary>
present,

/// <summary>
/// Called after ReShade has rendered its overlay.
/// <para>Callback function signature: <c>void (api::command_queue *queue, api::effect_runtime *runtime)</c></para>
/// </summary>
reshade_present,

/// <summary>
/// Called right before ReShade effects are rendered.
/// <para>Callback function signature: <c>void (api::effect_runtime *runtime, api::command_list *cmd_list, api::resource_view rtv, api::resource_view rtv_srgb)</c></para>
Expand Down Expand Up @@ -1451,7 +1457,7 @@ namespace reshade
RESHADE_DEFINE_ADDON_EVENT_TRAITS(addon_event::destroy_command_queue, void, api::command_queue *queue);

RESHADE_DEFINE_ADDON_EVENT_TRAITS(addon_event::init_swapchain, void, api::swapchain *swapchain);
RESHADE_DEFINE_ADDON_EVENT_TRAITS(addon_event::create_swapchain, bool, api::resource_desc &buffer_desc, void *hwnd);
RESHADE_DEFINE_ADDON_EVENT_TRAITS(addon_event::create_swapchain, bool, api::resource_desc &back_buffer_desc, void *window_handle);
RESHADE_DEFINE_ADDON_EVENT_TRAITS(addon_event::destroy_swapchain, void, api::swapchain *swapchain);

RESHADE_DEFINE_ADDON_EVENT_TRAITS(addon_event::init_effect_runtime, void, api::effect_runtime *runtime);
Expand Down Expand Up @@ -1540,6 +1546,7 @@ namespace reshade

RESHADE_DEFINE_ADDON_EVENT_TRAITS(addon_event::present, void, api::command_queue *queue, api::swapchain *swapchain);

RESHADE_DEFINE_ADDON_EVENT_TRAITS(addon_event::reshade_present, void, api::command_queue *queue, api::effect_runtime *runtime);
RESHADE_DEFINE_ADDON_EVENT_TRAITS(addon_event::reshade_begin_effects, void, api::effect_runtime *runtime, api::command_list *cmd_list, api::resource_view rtv, api::resource_view rtv_srgb);
RESHADE_DEFINE_ADDON_EVENT_TRAITS(addon_event::reshade_finish_effects, void, api::effect_runtime *runtime, api::command_list *cmd_list, api::resource_view rtv, api::resource_view rtv_srgb);
RESHADE_DEFINE_ADDON_EVENT_TRAITS(addon_event::reshade_reloaded_effects, void, api::effect_runtime *runtime);
Expand Down
4 changes: 4 additions & 0 deletions source/d3d12/d3d12_command_queue_downlevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ HRESULT STDMETHODCALLTYPE D3D12CommandQueueDownlevel::Present(ID3D12GraphicsComm

swapchain_impl::on_present(pSourceTex2D, hWindow);

#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::reshade_present>(_parent_queue, this);
#endif

_parent_queue->flush_immediate_command_list();

// Get original command list pointer from proxy object
Expand Down
6 changes: 6 additions & 0 deletions source/d3d9/d3d9_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ HRESULT STDMETHODCALLTYPE Direct3DDevice9::Present(const RECT *pSourceRect, cons
reshade::invoke_addon_event<reshade::addon_event::present>(this, _implicit_swapchain);
#endif
_implicit_swapchain->on_present();
#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::reshade_present>(this, _implicit_swapchain);
#endif
}

return _orig->Present(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
Expand Down Expand Up @@ -2186,6 +2189,9 @@ HRESULT STDMETHODCALLTYPE Direct3DDevice9::PresentEx(const RECT *pSourceRect, co
reshade::invoke_addon_event<reshade::addon_event::present>(this, _implicit_swapchain);
#endif
_implicit_swapchain->on_present();
#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::reshade_present>(this, _implicit_swapchain);
#endif
}

assert(_extended_interface);
Expand Down
3 changes: 3 additions & 0 deletions source/d3d9/d3d9_swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ HRESULT STDMETHODCALLTYPE Direct3DSwapChain9::Present(const RECT *pSourceRect, c
reshade::invoke_addon_event<reshade::addon_event::present>(_device, this);
#endif
swapchain_impl::on_present();
#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::reshade_present>(_device, this);
#endif
}

return _orig->Present(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
Expand Down
9 changes: 9 additions & 0 deletions source/dxgi/dxgi_swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,27 @@ void DXGISwapChain::runtime_present(UINT flags)
reshade::invoke_addon_event<reshade::addon_event::present>(static_cast<D3D10Device *>(static_cast<ID3D10Device *>(_direct3d_device)), _impl);
#endif
static_cast<reshade::d3d10::swapchain_impl *>(_impl)->on_present();
#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::reshade_present>(static_cast<D3D10Device *>(static_cast<ID3D10Device *>(_direct3d_device)), _impl);
#endif
break;
case 11:
#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::present>(static_cast<D3D11Device *>(static_cast<ID3D11Device *>(_direct3d_device))->_immediate_context, _impl);
#endif
static_cast<reshade::d3d11::swapchain_impl *>(_impl)->on_present();
#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::reshade_present>(static_cast<D3D11Device *>(static_cast<ID3D11Device *>(_direct3d_device))->_immediate_context, _impl);
#endif
break;
case 12:
#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::present>(static_cast<D3D12CommandQueue *>(_direct3d_command_queue), _impl);
#endif
static_cast<reshade::d3d12::swapchain_impl *>(_impl)->on_present();
#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::reshade_present>(static_cast<D3D12CommandQueue *>(_direct3d_command_queue), _impl);
#endif
static_cast<D3D12CommandQueue *>(_direct3d_command_queue)->flush_immediate_command_list();
break;
}
Expand Down
2 changes: 2 additions & 0 deletions source/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace reshade
/// <returns>A pointer to the input manager for the <paramref name="window"/>.</returns>
static std::shared_ptr<input> register_window(window_handle window);

window_handle get_window_handle() const { return _window; }

bool is_key_down(unsigned int keycode) const;
bool is_key_pressed(unsigned int keycode) const;
bool is_key_pressed(unsigned int keycode, bool ctrl, bool shift, bool alt, bool force_modifiers = false) const;
Expand Down
4 changes: 4 additions & 0 deletions source/opengl/opengl_hooks_wgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,10 @@ HOOK_EXPORT BOOL WINAPI wglSwapBuffers(HDC hdc)

// Assume that the correct OpenGL context is still current here
runtime->on_present();

#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::reshade_present>(runtime, runtime);
#endif
}

return trampoline(hdc);
Expand Down
1 change: 1 addition & 0 deletions source/opengl/opengl_impl_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,7 @@ static bool create_shader_module(GLenum type, const reshade::api::shader_desc &d
else
{
assert(desc.code_size <= static_cast<size_t>(std::numeric_limits<GLsizei>::max()));
assert(desc.entry_point != nullptr);

glShaderBinary(1, &shader_object, GL_SPIR_V_BINARY, desc.code, static_cast<GLsizei>(desc.code_size));
glSpecializeShader(shader_object, desc.entry_point, desc.spec_constants, desc.spec_constant_ids, desc.spec_constant_values);
Expand Down
20 changes: 20 additions & 0 deletions source/openvr/openvr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ static vr::EVRCompositorError on_vr_submit_d3d10(vr::IVRCompositor *compositor,

s_vr_swapchain->on_present();

#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::reshade_present>(device_proxy, s_vr_swapchain);
#endif

const auto target_texture = reinterpret_cast<ID3D10Texture2D *>(s_vr_swapchain->get_current_back_buffer().handle);

// The left and right eye were copied side-by-side to a single texture in 'on_vr_submit', so set bounds accordingly
Expand Down Expand Up @@ -130,6 +134,10 @@ static vr::EVRCompositorError on_vr_submit_d3d11(vr::IVRCompositor *compositor,

s_vr_swapchain->on_present();

#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::reshade_present>(device_proxy->_immediate_context, s_vr_swapchain);
#endif

const auto target_texture = reinterpret_cast<ID3D11Texture2D *>(s_vr_swapchain->get_current_back_buffer().handle);

// The left and right eye were copied side-by-side to a single texture in 'on_vr_submit', so set bounds accordingly
Expand Down Expand Up @@ -177,6 +185,10 @@ static vr::EVRCompositorError on_vr_submit_d3d12(vr::IVRCompositor *compositor,

s_vr_swapchain->on_present();

#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::reshade_present>(command_queue_proxy.get(), s_vr_swapchain);
#endif

command_queue_proxy->flush_immediate_command_list();

lock.unlock();
Expand Down Expand Up @@ -228,6 +240,10 @@ static vr::EVRCompositorError on_vr_submit_opengl(vr::IVRCompositor *compositor,

s_vr_swapchain->on_present();

#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::reshade_present>(g_current_context, s_vr_swapchain);
#endif

const GLuint target_rbo = s_vr_swapchain->get_current_back_buffer().handle & 0xFFFFFFFF;

// Target object created in 'on_vr_submit' is a 2D texture
Expand Down Expand Up @@ -286,6 +302,10 @@ static vr::EVRCompositorError on_vr_submit_vulkan(vr::IVRCompositor *compositor,

s_vr_swapchain->on_present();

#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::reshade_present>(queue, s_vr_swapchain);
#endif

queue->flush_immediate_command_list();

vr::VRVulkanTextureData_t target_texture = *texture;
Expand Down
5 changes: 5 additions & 0 deletions source/runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ namespace reshade
/// </summary>
api::command_queue *get_command_queue() final { return _graphics_queue; }

/// <summary>
/// Gets the handle of the window this swap chain was created with.
/// </summary>
virtual void *get_window_handle() const override;

/// <summary>
/// Gets the path to the configuration file used by this effect runtime.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions source/runtime_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#include "input.hpp"
#include <cassert>

reshade::input::window_handle reshade::runtime::get_window_handle() const
{
return _input != nullptr ? _input->get_window_handle() : nullptr;
}

bool reshade::runtime::is_key_down(uint32_t keycode) const
{
return _input != nullptr && _input->is_key_down(keycode);
Expand Down
3 changes: 3 additions & 0 deletions source/vulkan/vulkan_hooks_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,9 @@ VkResult VKAPI_CALL vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPr
reshade::invoke_addon_event<reshade::addon_event::present>(queue_impl, swapchain_impl);
#endif
swapchain_impl->on_present(queue, pPresentInfo->pImageIndices[i], wait_semaphores);
#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::reshade_present>(queue_impl, swapchain_impl);
#endif
}
}

Expand Down
2 changes: 2 additions & 0 deletions source/vulkan/vulkan_impl_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,8 @@ bool reshade::vulkan::device_impl::create_pipeline(const api::pipeline_desc &des
}
bool reshade::vulkan::device_impl::create_shader_module(VkShaderStageFlagBits stage, const api::shader_desc &desc, VkPipelineShaderStageCreateInfo &stage_info, VkSpecializationInfo &spec_info, std::vector<VkSpecializationMapEntry> &spec_map)
{
assert(desc.entry_point != nullptr);

spec_map.reserve(desc.spec_constants);
for (uint32_t i = 0; i < desc.spec_constants; ++i)
spec_map.push_back(VkSpecializationMapEntry { desc.spec_constant_ids[i], i * 4, sizeof(uint32_t) });
Expand Down

0 comments on commit 5b49eff

Please sign in to comment.