Skip to content

Commit 5c4d52e

Browse files
authored
Fix overlays init by using D3D11 device and context provided by the game's renderer (#829)
* Fix overlays init by using D3D11 device and context provided by the game's renderer Retrieving them from the swapchain is not the way and might cause unwanted side effects * Sync TiltedUI
1 parent 806fa93 commit 5c4d52e

File tree

8 files changed

+38
-45
lines changed

8 files changed

+38
-45
lines changed

Code/client/Games/Renderer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ ID3D11Device* BGSRenderer::GetDevice()
3030
return *(s_device.Get());
3131
}
3232

33+
// unused, never hooked
3334
static void HookPresent()
3435
{
3536
// TODO (Force): refactor this ..
@@ -38,8 +39,7 @@ static void HookPresent()
3839
return RealRenderPresent();
3940
}
4041

41-
// TODO (Force): handle lost devices
42-
42+
// unused, never hooked
4343
static bool HookCreateViewport(void* viewport, ViewportConfig* pConfig, WindowConfig* pWindowConfig, void* a4)
4444
{
4545
pConfig->name = "Skyrim Together | " BUILD_BRANCH "@" BUILD_COMMIT;

Code/client/Games/Skyrim/BSGraphics/BSGraphicsRenderer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ void Hook_Renderer_Init(Renderer* self, BSGraphics::RendererInitOSData* aOSData,
6060
// This how the game does it too
6161
g_RenderWindow = &self->Data.RenderWindowA[0];
6262

63-
g_sRs->OnDeviceCreation(self->Data.RenderWindowA[0].pSwapChain);
63+
const BSGraphics::RendererData& renderer = self->Data;
64+
65+
g_sRs->OnDeviceCreation(renderer.RenderWindowA[0].pSwapChain, renderer.pForwarder, renderer.pContext);
6466
}
6567

6668
void (*StopTimer)(int) = nullptr;

Code/client/Games/Skyrim/BSGraphics/BSGraphicsRenderer.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,21 @@ struct RendererData
7171
uint32_t uiNewWidth; // 0x0028
7272
uint32_t uiNewHeight; // 0x002C
7373
uint32_t uiPresentInterval; // 0x0030
74-
ID3D11Device* pDevice; // 0x0038
74+
ID3D11Device* pForwarder; // 0x0038
7575
ID3D11DeviceContext* pContext; // 0x0040
7676

77-
// UNSURE starting from here...
7877
BSGraphics::RendererWindow RenderWindowA[32]; // V
79-
BSGraphics::RenderTarget pRenderTargetsA[101];
80-
BSGraphics::DepthStencilTarget pDepthStencilTargetsA[13];
81-
BSGraphics::CubeMapRenderTarget pCubeMapRenderTargetsA[2];
82-
BSCriticalSection RendererLock;
83-
// Invalid...
84-
const char* pClassName;
85-
HINSTANCE__* hInstance;
78+
BSGraphics::RenderTarget pRenderTargetsA[114];
79+
BSGraphics::DepthStencilTarget pDepthStencilTargetsA[12];
80+
BSGraphics::CubeMapRenderTarget pCubeMapRenderTargetsA[1];
8681
};
8782

83+
static_assert(offsetof(RendererData, pForwarder) == 0x38);
84+
static_assert(offsetof(RendererData, RenderWindowA) == 0x48);
85+
static_assert(offsetof(RendererData, pRenderTargetsA) == 0xA48);
86+
static_assert(offsetof(RendererData, pDepthStencilTargetsA) == 0x1FA8);
87+
static_assert(offsetof(RendererData, pCubeMapRenderTargetsA) == 0x26C8);
88+
8889
struct Renderer
8990
{
9091
bool bSkipNextPresent;
@@ -97,8 +98,6 @@ struct RendererInitReturn
9798
HWND hwnd;
9899
};
99100

100-
static_assert(offsetof(RendererData, pDevice) == 56);
101-
102101
// former ViewportConfig
103102
struct RendererInitOSData
104103
{

Code/client/Services/Generic/ImguiService.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,13 @@ ImguiService::~ImguiService() noexcept
2121

2222
void ImguiService::Create(RenderSystemD3D11* apRenderSystem, HWND aHwnd)
2323
{
24-
ID3D11Device* d3dDevice = nullptr;
25-
ID3D11DeviceContext* d3dContext = nullptr;
26-
2724
m_imDriver.Initialize(static_cast<void*>(aHwnd));
2825

2926
// init platform
3027
if (!ImGui_ImplWin32_Init(aHwnd))
3128
spdlog::error("Failed to initialize Imgui-Win32");
3229

33-
apRenderSystem->GetSwapChain()->GetDevice(__uuidof(d3dDevice), reinterpret_cast<void**>(&d3dDevice));
34-
d3dDevice->GetImmediateContext(&d3dContext);
35-
36-
ImGui_ImplDX11_Init(d3dDevice, d3dContext);
30+
ImGui_ImplDX11_Init(apRenderSystem->GetDevice(), apRenderSystem->GetDeviceContext());
3731
}
3832

3933
void ImguiService::Render() const

Code/client/Services/Generic/OverlayService.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ struct D3D11RenderProvider final : OverlayApp::RenderProvider, OverlayRenderHand
6060
[[nodiscard]] HWND GetWindow() override { return m_pRenderSystem->GetWindow(); }
6161

6262
[[nodiscard]] IDXGISwapChain* GetSwapChain() const noexcept override { return m_pRenderSystem->GetSwapChain(); }
63+
[[nodiscard]] ID3D11Device* GetDevice() const noexcept override { return m_pRenderSystem->GetDevice(); }
64+
[[nodiscard]] ID3D11DeviceContext* GetDeviceContext() const noexcept override { return m_pRenderSystem->GetDeviceContext(); }
6365

6466
private:
6567
RenderSystemD3D11* m_pRenderSystem;

Code/client/Systems/RenderSystemD3D11.cpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,23 @@
66
#include <Services/OverlayService.h>
77
#include <Services/ImguiService.h>
88

9-
#include <D3D11Hook.hpp>
109
#include <d3d11.h>
1110

1211
RenderSystemD3D11::RenderSystemD3D11(OverlayService& aOverlay, ImguiService& aImguiService)
1312
: m_pSwapChain(nullptr)
13+
, m_pDevice(nullptr)
14+
, m_pDeviceContext(nullptr)
1415
, m_overlay(aOverlay)
1516
, m_imguiService(aImguiService)
1617
{
17-
auto& d3d11 = TiltedPhoques::D3D11Hook::Get();
18-
19-
m_createConnection = d3d11.OnCreate.Connect(std::bind(&RenderSystemD3D11::OnDeviceCreation, this, std::placeholders::_1));
20-
m_resetConnection = d3d11.OnLost.Connect(std::bind(&RenderSystemD3D11::OnReset, this, std::placeholders::_1));
18+
// Note: D3D11Hook is not utilized in the codebase
2119

20+
// auto& d3d11 = TiltedPhoques::D3D11Hook::Get();
21+
// m_createConnection = d3d11.OnCreate.Connect(std::bind(&RenderSystemD3D11::OnDeviceCreation, this, std::placeholders::_2));
22+
// m_resetConnection = d3d11.OnLost.Connect(std::bind(&RenderSystemD3D11::OnReset, this, std::placeholders::_1));
2223
// m_renderConnection = d3d11.OnPresent.Connect(std::bind(&RenderSystemD3D11::OnRender, this, std::placeholders::_1));
2324
}
2425

25-
RenderSystemD3D11::~RenderSystemD3D11()
26-
{
27-
auto& d3d11 = TiltedPhoques::D3D11Hook::Get();
28-
29-
d3d11.OnCreate.Disconnect(m_createConnection);
30-
// d3d11.OnPresent.Disconnect(m_renderConnection);
31-
d3d11.OnLost.Disconnect(m_resetConnection);
32-
}
33-
3426
HWND RenderSystemD3D11::GetWindow() const
3527
{
3628
DXGI_SWAP_CHAIN_DESC desc{};
@@ -42,14 +34,11 @@ HWND RenderSystemD3D11::GetWindow() const
4234
return desc.OutputWindow;
4335
}
4436

45-
IDXGISwapChain* RenderSystemD3D11::GetSwapChain() const
46-
{
47-
return m_pSwapChain;
48-
}
49-
50-
void RenderSystemD3D11::OnDeviceCreation(IDXGISwapChain* apSwapChain)
37+
void RenderSystemD3D11::OnDeviceCreation(IDXGISwapChain* apSwapChain, ID3D11Device* apDevice, ID3D11DeviceContext* apContext)
5138
{
5239
m_pSwapChain = apSwapChain;
40+
m_pDevice = apDevice;
41+
m_pDeviceContext = apContext;
5342

5443
m_imguiService.Create(this, GetWindow());
5544
m_overlay.Create(this);

Code/client/Systems/RenderSystemD3D11.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
struct ImguiService;
44
struct IDXGISwapChain;
5+
struct ID3D11Device;
6+
struct ID3D11DeviceContext;
57
struct OverlayService;
68

79
/**
@@ -10,20 +12,25 @@ struct OverlayService;
1012
struct RenderSystemD3D11
1113
{
1214
RenderSystemD3D11(OverlayService& aOverlay, ImguiService& aImguiService);
13-
~RenderSystemD3D11();
15+
~RenderSystemD3D11() = default;
1416

1517
TP_NOCOPYMOVE(RenderSystemD3D11);
1618

1719
[[nodiscard]] HWND GetWindow() const;
18-
[[nodiscard]] IDXGISwapChain* GetSwapChain() const;
20+
[[nodiscard]] IDXGISwapChain* GetSwapChain() const { return m_pSwapChain; };
21+
[[nodiscard]] ID3D11Device* GetDevice() const { return m_pDevice; };
22+
[[nodiscard]] ID3D11DeviceContext* GetDeviceContext() const { return m_pDeviceContext; };
1923

2024
// to make yamashi mad
21-
void OnDeviceCreation(IDXGISwapChain* apSwapChain);
25+
void OnDeviceCreation(IDXGISwapChain* apSwapChain, ID3D11Device* apDevice, ID3D11DeviceContext* apContext);
2226
void OnRender();
2327
void OnReset(IDXGISwapChain* apSwapChain);
2428

2529
private:
2630
IDXGISwapChain* m_pSwapChain;
31+
ID3D11Device* m_pDevice;
32+
ID3D11DeviceContext* m_pDeviceContext;
33+
2734
OverlayService& m_overlay;
2835
ImguiService& m_imguiService;
2936
size_t m_createConnection;

0 commit comments

Comments
 (0)