libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
gfx_dxgi.h
Go to the documentation of this file.
1#pragma once
2#if defined(ENABLE_DX11) || defined(ENABLE_DX12)
3
4#include <memory>
5#include "gfx_rendering_api.h"
7
8#include <functional>
9
10#include <dxgi1_2.h>
11
12namespace Ship {
13class Config;
14class FileDrop;
15class ConsoleVariable;
16} // namespace Ship
17
18namespace Fast {
19class Fast3dGui;
20
28 public:
30 GfxWindowBackendDXGI(std::shared_ptr<Ship::Config> config = nullptr,
31 std::shared_ptr<Ship::FileDrop> fileDrop = nullptr,
32 std::shared_ptr<Ship::ConsoleVariable> consoleVariable = nullptr,
33 std::shared_ptr<Fast::Fast3dGui> fast3dGui = nullptr);
35
38 void Init(const char* gameName, const char* apiName, bool startFullScreen, uint32_t width, uint32_t height,
39 int32_t posX, int32_t posY) override;
40 void Close() override;
41 void SetKeyboardCallbacks(bool (*onKeyDown)(int scancode), bool (*onKeyUp)(int scancode),
42 void (*onnAllKeysUp)()) override;
43 void SetMouseCallbacks(bool (*onMouseButtonDown)(int btn), bool (*onnMouseButtonUp)(int btn)) override;
44 void SetFullscreenChangedCallback(void (*onnFullscreenChanged)(bool is_now_fullscreen)) override;
45 void SetFullscreen(bool fullscreen) override;
46 void GetActiveWindowRefreshRate(uint32_t* refreshRate) override;
47 void SetCursorVisibility(bool visability) override;
48 void SetMousePos(int32_t posX, int32_t posY) override;
49 void GetMousePos(int32_t* x, int32_t* y) override;
50 void GetMouseDelta(int32_t* x, int32_t* y) override;
51 void GetMouseWheel(float* x, float* y) override;
52 bool GetMouseState(uint32_t btn) override;
53 void SetMouseCapture(bool capture) override;
54 bool IsMouseCaptured() override;
55 void GetDimensions(uint32_t* width, uint32_t* height, int32_t* posX, int32_t* posY) override;
56 void SetDimensions(uint32_t width, uint32_t height, int32_t posX, int32_t posY) override;
58 void HandleEvents() override;
59 bool IsFrameReady() override;
60 void SwapBuffersBegin() override;
61 void SwapBuffersEnd() override;
62 double GetTime() override;
64 void SetTargetFps(int fps) override;
65 void SetMaxFrameLatency(int latency) override;
66 const char* GetKeyName(int scancode) override;
67 bool CanDisableVsync() override;
68 bool IsRunning() override;
69 void Destroy() override;
70 bool IsFullscreen() override;
76 IDXGISwapChain1* GetSwapChain();
77 // These need to be public to be accessible in the window callback
79 void CreateSwapChain(IUnknown* mDevice, std::function<void()>&& before_destroy_fn);
81 void CreateFactoryAndDevice(bool debug, int d3d_version, class GfxRenderingAPIDX11* self,
82 bool (*createFunc)(class GfxRenderingAPIDX11* self, bool SoftwareRenderer));
83 void OnKeydown(WPARAM wParam, LPARAM lParam);
84 void OnKeyup(WPARAM wParam, LPARAM lParam);
85 void OnMouseButtonDown(int btn);
86 void OnMouseButtonUp(int btn);
90
91 std::tuple<HMONITOR, RECT, BOOL> mMonitor; // 0: Handle, 1: Display Monitor Rect, 2: Is_Primary
92 uint32_t current_width, current_height; // Width and height of client areas
93 std::vector<std::tuple<HMONITOR, RECT, BOOL>> monitor_list;
94 int32_t mPosX, mPosY; // Screen coordinates
96 double mDisplayPeriod; // (1000 / dxgi.mDetectedHz) in ms
97 void (*mOnAllKeysUp)(void);
99 float mMouseWheel[2];
104
105 // These need to be public to be accessible in the window callback
106 std::shared_ptr<Ship::FileDrop> mFileDrop;
107 std::shared_ptr<Ship::ConsoleVariable> mConsoleVariable;
108 std::shared_ptr<Fast::Fast3dGui> mFast3dGui;
109
110 private:
111 void LoadDxgi();
112 void ApplyMaxFrameLatency(bool first);
113 void ToggleBorderlessWindowFullScreen(bool enable, bool callCallback);
114
115 HWND h_wnd;
116 // These four only apply in windowed mode.
117
118 HMODULE dxgi_module;
119 HRESULT(__stdcall* CreateDXGIFactory1)(REFIID riid, void** factory);
120 HRESULT(__stdcall* CreateDXGIFactory2)(UINT flags, REFIID iid, void** factory);
121
122 bool mLastMaximizedState;
123
124 bool mDXGI11_4;
125 Microsoft::WRL::ComPtr<IDXGIFactory2> mFactory;
126 Microsoft::WRL::ComPtr<IDXGISwapChain1> swap_chain;
127 HANDLE mWaitableObject;
128 Microsoft::WRL::ComPtr<IUnknown> mSwapChainDevice; // D3D11 Device or D3D12 Command Queue
129 std::function<void()> mBeforeDestroySwapChainFn;
130 uint64_t mFrameTimeStamp; // in units of 1/FRAME_INTERVAL_NS_DENOMINATOR nanoseconds
131 std::map<UINT, DXGI_FRAME_STATISTICS> mFrameStats;
132 std::set<std::pair<UINT, UINT>> mPendingFrameStats;
133 bool mDroppedFrame;
134 bool mZeroLatency;
135 uint32_t mMaxFrameLatency;
136 uint32_t mAppliedMaxFrameLatency;
137 HANDLE mTimer;
138 bool mTearingSupport;
139 bool mMousePressed[5];
140 LARGE_INTEGER mPreviousPresentTime;
141
142 RAWINPUTDEVICE mRawInputDevice[1];
143 POINT mPrevMouseCursorPos;
144
145 std::shared_ptr<Ship::Config> mConfig;
146};
147
148} // namespace Fast
149
150#ifdef DECLARE_GFX_DXGI_FUNCTIONS
154void ThrowIfFailed(HRESULT res);
155
159void ThrowIfFailed(HRESULT res, HWND h_wnd, const char* message);
160#endif
161
162#endif
Direct3D 11 implementation of the Fast3D rendering backend.
Definition gfx_direct3d_common.h:105
DXGI/Win32 window backend used by the Fast3D renderer on Windows.
Definition gfx_dxgi.h:27
double GetTime() override
void CreateFactoryAndDevice(bool debug, int d3d_version, class GfxRenderingAPIDX11 *self, bool(*createFunc)(class GfxRenderingAPIDX11 *self, bool SoftwareRenderer))
Creates the DXGI factory/device and invokes the renderer-side creation callback.
bool IsMouseCaptured() override
bool mIsMouseCaptured
Definition gfx_dxgi.h:100
double mDetectedHz
Definition gfx_dxgi.h:95
void SetMousePos(int32_t posX, int32_t posY) override
float mMouseWheel[2]
Definition gfx_dxgi.h:99
std::tuple< HMONITOR, RECT, BOOL > mMonitor
Definition gfx_dxgi.h:91
void CreateSwapChain(IUnknown *mDevice, std::function< void()> &&before_destroy_fn)
Creates/recreates the swap chain and runs a pre-destroy callback when replacing it.
void SetMouseCallbacks(bool(*onMouseButtonDown)(int btn), bool(*onnMouseButtonUp)(int btn)) override
void SetDimensions(uint32_t width, uint32_t height, int32_t posX, int32_t posY) override
bool mHasMousePosition
Definition gfx_dxgi.h:103
uint32_t current_width
Definition gfx_dxgi.h:92
void SetFullscreenChangedCallback(void(*onnFullscreenChanged)(bool is_now_fullscreen)) override
void GetActiveWindowRefreshRate(uint32_t *refreshRate) override
void SetMouseCapture(bool capture) override
double mDisplayPeriod
Definition gfx_dxgi.h:96
Ship::WindowRect GetPrimaryMonitorRect() override
HWND GetWindowHandle()
Returns the native Win32 window handle.
void SetMaxFrameLatency(int latency) override
int32_t mPosX
Definition gfx_dxgi.h:94
void OnMouseButtonDown(int btn)
void SetFullscreen(bool fullscreen) override
void(* mOnAllKeysUp)(void)
Definition gfx_dxgi.h:97
void GetDimensions(uint32_t *width, uint32_t *height, int32_t *posX, int32_t *posY) override
bool GetMouseState(uint32_t btn) override
void SwapBuffersEnd() override
IDXGISwapChain1 * GetSwapChain()
Returns the active DXGI swap chain.
void HandleEvents() override
std::shared_ptr< Fast::Fast3dGui > mFast3dGui
Definition gfx_dxgi.h:108
void GetMousePos(int32_t *x, int32_t *y) override
void OnMouseButtonUp(int btn)
bool IsRunning() override
bool IsFullscreen() override
void GetMouseDelta(int32_t *x, int32_t *y) override
bool mInFocus
Definition gfx_dxgi.h:102
void OnKeyup(WPARAM wParam, LPARAM lParam)
void Init(const char *gameName, const char *apiName, bool startFullScreen, uint32_t width, uint32_t height, int32_t posX, int32_t posY) override
void OnKeydown(WPARAM wParam, LPARAM lParam)
bool CanDisableVsync() override
bool IsFrameReady() override
int32_t mPosY
Definition gfx_dxgi.h:94
void SetCursorVisibility(bool visability) override
std::shared_ptr< Ship::FileDrop > mFileDrop
Definition gfx_dxgi.h:106
std::shared_ptr< Ship::ConsoleVariable > mConsoleVariable
Definition gfx_dxgi.h:107
void SetTargetFps(int fps) override
POINT mRawMouseDeltaBuf
Definition gfx_dxgi.h:98
void GetMouseWheel(float *x, float *y) override
const char * GetKeyName(int scancode) override
GfxWindowBackendDXGI(std::shared_ptr< Ship::Config > config=nullptr, std::shared_ptr< Ship::FileDrop > fileDrop=nullptr, std::shared_ptr< Ship::ConsoleVariable > consoleVariable=nullptr, std::shared_ptr< Fast::Fast3dGui > fast3dGui=nullptr)
Constructs the backend with optional shared engine dependencies.
bool mIsMouseHovered
Definition gfx_dxgi.h:101
std::vector< std::tuple< HMONITOR, RECT, BOOL > > monitor_list
Definition gfx_dxgi.h:93
void SetKeyboardCallbacks(bool(*onKeyDown)(int scancode), bool(*onKeyUp)(int scancode), void(*onnAllKeysUp)()) override
uint32_t current_height
Definition gfx_dxgi.h:92
void SwapBuffersBegin() override
Definition gfx_window_manager_api.h:7
void ThrowIfFailed(HRESULT res)
Throws an exception when an HRESULT indicates failure.
Definition gfx_direct3d_common.h:19
Core namespace for the libultraship engine framework.
Definition gfx_direct3d_common.h:14
Screen-space rectangle in integer pixels.
Definition Window.h:38