From d0ce47eae60d1794fe65d2cbbc4b9461f4b21099 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Fri, 2 Dec 2022 19:11:43 -0800 Subject: [PATCH 1/8] Add GLFW WSI --- include/native/wsi/native_glfw.h | 25 +++++ include/native/wsi/native_wsi.h | 2 + meson.build | 14 ++- src/dxvk/meson.build | 4 + src/dxvk/platform/dxvk_glfw_exts.cpp | 57 ++++++++++ src/vulkan/vulkan_loader.h | 4 +- src/wsi/glfw/wsi_monitor_glfw.cpp | 160 +++++++++++++++++++++++++++ src/wsi/glfw/wsi_platform_glfw.h | 23 ++++ src/wsi/glfw/wsi_window_glfw.cpp | 144 ++++++++++++++++++++++++ src/wsi/meson.build | 8 ++ src/wsi/wsi_platform.h | 2 + 11 files changed, 437 insertions(+), 6 deletions(-) create mode 100644 include/native/wsi/native_glfw.h create mode 100644 src/dxvk/platform/dxvk_glfw_exts.cpp create mode 100644 src/wsi/glfw/wsi_monitor_glfw.cpp create mode 100644 src/wsi/glfw/wsi_platform_glfw.h create mode 100644 src/wsi/glfw/wsi_window_glfw.cpp diff --git a/include/native/wsi/native_glfw.h b/include/native/wsi/native_glfw.h new file mode 100644 index 00000000000..e4946e02c6b --- /dev/null +++ b/include/native/wsi/native_glfw.h @@ -0,0 +1,25 @@ +#include + +#include + +namespace dxvk::wsi { + + inline GLFWwindow* fromHwnd(HWND hWindow) { + return reinterpret_cast(hWindow); + } + + inline HWND toHwnd(GLFWwindow* pWindow) { + return reinterpret_cast(pWindow); + } + + // Offset so null HMONITORs go to -1 + inline int32_t fromHmonitor(HMONITOR hMonitor) { + return static_cast(reinterpret_cast(hMonitor)) - 1; + } + + // Offset so -1 display id goes to 0 == NULL + inline HMONITOR toHmonitor(int32_t displayId) { + return reinterpret_cast(static_cast(displayId + 1)); + } + +} \ No newline at end of file diff --git a/include/native/wsi/native_wsi.h b/include/native/wsi/native_wsi.h index 00a299060bf..cfb64f12d3e 100644 --- a/include/native/wsi/native_wsi.h +++ b/include/native/wsi/native_wsi.h @@ -4,6 +4,8 @@ #error You shouldnt be using this code path. #elif DXVK_WSI_SDL2 #include "wsi/native_sdl2.h" +#elif DXVK_WSI_GLFW +#include "wsi/native_glfw.h" #else #error Unknown wsi! #endif \ No newline at end of file diff --git a/meson.build b/meson.build index 12d37300a6b..8e1f0ca0c09 100644 --- a/meson.build +++ b/meson.build @@ -103,8 +103,6 @@ if platform == 'windows' dxvk_name_prefix = '' compiler_args += ['-DDXVK_WSI_WIN32'] else - lib_sdl2 = cpp.find_library('SDL2') - wrc = find_program('touch') wrc_generator = generator(wrc, output : [ '@BASENAME@_ignored.h' ], arguments : [ '@OUTPUT@' ] ) @@ -114,9 +112,17 @@ else './include/native/directx' ] - dxvk_wsi = 'sdl2' + dxvk_wsi = 'glfw' + + if dxvk_wsi == 'sdl2' + lib_sdl2 = cpp.find_library('SDL2') + compiler_args += ['-DDXVK_WSI_SDL2'] + elif dxvk_wsi == 'glfw' + lib_glfw = cpp.find_library('glfw') + compiler_args += ['-DDXVK_WSI_GLFW'] + endif + dxvk_name_prefix = 'libdxvk_' - compiler_args += ['-DDXVK_WSI_SDL2'] link_args += [ '-static-libgcc', diff --git a/src/dxvk/meson.build b/src/dxvk/meson.build index 387bc4d45ac..678ab268b5a 100644 --- a/src/dxvk/meson.build +++ b/src/dxvk/meson.build @@ -127,6 +127,10 @@ elif dxvk_wsi == 'sdl2' dxvk_src += [ 'platform/dxvk_sdl2_exts.cpp' ] +elif dxvk_wsi == 'glfw' + dxvk_src += [ + 'platform/dxvk_glfw_exts.cpp' + ] endif dxvk_extra_deps = [ dependency('threads') ] diff --git a/src/dxvk/platform/dxvk_glfw_exts.cpp b/src/dxvk/platform/dxvk_glfw_exts.cpp new file mode 100644 index 00000000000..014125132d9 --- /dev/null +++ b/src/dxvk/platform/dxvk_glfw_exts.cpp @@ -0,0 +1,57 @@ +#include "../dxvk_platform_exts.h" + +#define GLFW_INCLUDE_VULKAN +#include + +namespace dxvk { + + DxvkPlatformExts DxvkPlatformExts::s_instance; + + std::string_view DxvkPlatformExts::getName() { + return "GLFW WSI"; + } + + DxvkNameSet DxvkPlatformExts::getInstanceExtensions() { + if (!glfwVulkanSupported()) + throw DxvkError(str::format("GLFW WSI: Vulkan is not supported in any capacity!")); + + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + GLFWwindow* window = glfwCreateWindow(1, 1, "Dummy Window", nullptr, nullptr); + + if (window == nullptr) + throw DxvkError(str::format("GLFW WSI: Unable to create dummy window")); + + uint32_t extensionCount = 0; + const char** extensionArray = glfwGetRequiredInstanceExtensions(&extensionCount); + + if (extensionCount == 0) + throw DxvkError(str::format("GLFW WSI: Failed to get required instance extensions")); + + DxvkNameSet names; + for (uint32_t i = 0; i < extensionCount; ++i) { + names.add(extensionArray[i]); + } + + glfwDestroyWindow(window); + + return names; + } + + + DxvkNameSet DxvkPlatformExts::getDeviceExtensions( + uint32_t adapterId) { + return DxvkNameSet(); + } + + + void DxvkPlatformExts::initInstanceExtensions() { + + } + + + void DxvkPlatformExts::initDeviceExtensions( + const DxvkInstance* instance) { + + } + +} \ No newline at end of file diff --git a/src/vulkan/vulkan_loader.h b/src/vulkan/vulkan_loader.h index 653cf3975ba..42f63ddd1e0 100644 --- a/src/vulkan/vulkan_loader.h +++ b/src/vulkan/vulkan_loader.h @@ -135,8 +135,8 @@ namespace dxvk::vk { #endif #ifdef VK_USE_PLATFORM_WIN32_KHR - VULKAN_FN(vkCreateWin32SurfaceKHR); - VULKAN_FN(vkGetPhysicalDeviceWin32PresentationSupportKHR); +// VULKAN_FN(vkCreateWin32SurfaceKHR); +// VULKAN_FN(vkGetPhysicalDeviceWin32PresentationSupportKHR); #endif VULKAN_FN(vkDestroySurfaceKHR); diff --git a/src/wsi/glfw/wsi_monitor_glfw.cpp b/src/wsi/glfw/wsi_monitor_glfw.cpp new file mode 100644 index 00000000000..1cb5aca7aac --- /dev/null +++ b/src/wsi/glfw/wsi_monitor_glfw.cpp @@ -0,0 +1,160 @@ +#include "../wsi_monitor.h" + +#include "wsi/native_wsi.h" +#include "wsi_platform_glfw.h" + +#include "../../util/util_string.h" +#include "../../util/log/log.h" + +#include +#include + +namespace dxvk::wsi { + + HMONITOR getDefaultMonitor() { + return enumMonitors(0); + } + + + HMONITOR enumMonitors(uint32_t index) { + return isDisplayValid(int32_t(index)) + ? toHmonitor(index) + : nullptr; + } + + bool getDisplayName( + HMONITOR hMonitor, + WCHAR (&Name)[32]) { + const int32_t displayId = fromHmonitor(hMonitor); + + if (!isDisplayValid(displayId)) + return false; + + std::wstringstream nameStream; + nameStream << LR"(\\.\DISPLAY)" << (displayId + 1); + + std::wstring name = nameStream.str(); + + std::memset(Name, 0, sizeof(Name)); + name.copy(Name, name.length(), 0); + + return true; + } + + + bool getDesktopCoordinates( + HMONITOR hMonitor, + RECT *pRect) { + const int32_t displayId = fromHmonitor(hMonitor); + + if (!isDisplayValid(displayId)) + return false; + + int32_t displayCount = 0; + GLFWmonitor **monitors = glfwGetMonitors(&displayCount); + GLFWmonitor *monitor = monitors[displayId]; + + int32_t x; + int32_t y; + int32_t w; + int32_t h; + glfwGetMonitorWorkarea(monitor, &x, &y, &w, &h); + + pRect->left = x; + pRect->top = y; + pRect->right = x + w; + pRect->bottom = y + h; + + return true; + } + + static inline uint32_t roundToNextPow2(uint32_t num) { + if (num-- == 0) + return 0; + + num |= num >> 1; + num |= num >> 2; + num |= num >> 4; + num |= num >> 8; + num |= num >> 16; + + return ++num; + } + + + static inline void convertMode(const GLFWvidmode &mode, WsiMode *pMode) { + pMode->width = uint32_t(mode.width); + pMode->height = uint32_t(mode.height); + pMode->refreshRate = WsiRational{uint32_t(mode.refreshRate) * 1000, 1000}; + // BPP should always be a power of two + // to match Windows behaviour of including padding. + pMode->bitsPerPixel = roundToNextPow2(mode.blueBits + mode.redBits + mode.greenBits); + pMode->interlaced = false; + } + + + bool getDisplayMode( + HMONITOR hMonitor, + uint32_t ModeNumber, + WsiMode *pMode) { + const int32_t displayId = fromHmonitor(hMonitor); + int32_t displayCount = 0; + GLFWmonitor **monitors = glfwGetMonitors(&displayCount); + GLFWmonitor *monitor = monitors[displayId]; + + if (!isDisplayValid(displayId)) + return false; + + int32_t count = 0; + const GLFWvidmode *modes = glfwGetVideoModes(monitor, &count); + + convertMode(modes[ModeNumber], pMode); + + return true; + } + + + bool getCurrentDisplayMode( + HMONITOR hMonitor, + WsiMode *pMode) { + const int32_t displayId = fromHmonitor(hMonitor); + + if (!isDisplayValid(displayId)) + return false; + + int32_t displayCount = 0; + GLFWmonitor **monitors = glfwGetMonitors(&displayCount); + GLFWmonitor *monitor = monitors[displayId]; + + auto mode = glfwGetVideoMode(monitor); + + convertMode(*mode, pMode); + + return true; + } + + + bool getDesktopDisplayMode( + HMONITOR hMonitor, + WsiMode *pMode) { + const int32_t displayId = fromHmonitor(hMonitor); + + if (!isDisplayValid(displayId)) + return false; + + int32_t displayCount = 0; + GLFWmonitor **monitors = glfwGetMonitors(&displayCount); + GLFWmonitor *monitor = monitors[displayId]; + + //TODO: actually implement this properly, currently we just grab the current one + convertMode(*glfwGetVideoMode(monitor), pMode); + + return true; + } + + std::vector getMonitorEdid(HMONITOR hMonitor) { + Logger::err("getMonitorEdid not implemented on this platform."); + return {}; + } + +} \ No newline at end of file diff --git a/src/wsi/glfw/wsi_platform_glfw.h b/src/wsi/glfw/wsi_platform_glfw.h new file mode 100644 index 00000000000..c8912d22bd9 --- /dev/null +++ b/src/wsi/glfw/wsi_platform_glfw.h @@ -0,0 +1,23 @@ +#pragma once + +#define GLFW_INCLUDE_VULKAN +#include + +#include "../wsi_monitor.h" + +namespace dxvk::wsi { + + /** + * \brief Impl-dependent state + */ + struct DxvkWindowState { + }; + + inline bool isDisplayValid(int32_t displayId) { + int32_t displayCount = 0; + glfwGetMonitors(&displayCount); + + return displayId < displayCount && displayId >= 0; + } + +} \ No newline at end of file diff --git a/src/wsi/glfw/wsi_window_glfw.cpp b/src/wsi/glfw/wsi_window_glfw.cpp new file mode 100644 index 00000000000..1175e95038e --- /dev/null +++ b/src/wsi/glfw/wsi_window_glfw.cpp @@ -0,0 +1,144 @@ +#include "../wsi_window.h" + +#include "native/wsi/native_wsi.h" +#include "wsi_platform_glfw.h" + +#include "../../util/util_string.h" +#include "../../util/log/log.h" + +#include +#define GLFW_INCLUDE_VULKAN +#include + +namespace dxvk::wsi { + + void getWindowSize( + HWND hWindow, + uint32_t *pWidth, + uint32_t *pHeight) { + GLFWwindow *window = fromHwnd(hWindow); + + int32_t w, h; + glfwGetWindowSize(window, &w, &h); + + if (pWidth) + *pWidth = uint32_t(w); + + if (pHeight) + *pHeight = uint32_t(h); + } + + + void resizeWindow( + HWND hWindow, + DxvkWindowState *pState, + uint32_t Width, + uint32_t Height) { + GLFWwindow *window = fromHwnd(hWindow); + + glfwSetWindowSize(window, int32_t(Width), int32_t(Height)); + } + + + bool setWindowMode( + HMONITOR hMonitor, + HWND hWindow, + const WsiMode& pMode) { + const int32_t displayId = fromHmonitor(hMonitor); + GLFWwindow *window = fromHwnd(hWindow); + + if (!isDisplayValid(displayId)) + return false; + + int32_t displayCount = 0; + GLFWmonitor **monitors = glfwGetMonitors(&displayCount); + GLFWmonitor *monitor = monitors[displayId]; + + GLFWvidmode wantedMode = {}; + wantedMode.width = pMode.width; + wantedMode.height = pMode.height; + wantedMode.refreshRate = pMode.refreshRate.numerator != 0 + ? pMode.refreshRate.numerator / pMode.refreshRate.denominator + : 0; + // TODO: Implement lookup format for bitsPerPixel here. + + glfwSetWindowMonitor(window, monitor, 0, 0, wantedMode.width, wantedMode.width, wantedMode.refreshRate); + + return true; + } + + bool enterFullscreenMode( + HMONITOR hMonitor, + HWND hWindow, + DxvkWindowState *pState, + bool ModeSwitch) { + const int32_t displayId = fromHmonitor(hMonitor); + GLFWwindow *window = fromHwnd(hWindow); + + if (!isDisplayValid(displayId)) + return false; + + GLFWmonitor *monitor = glfwGetPrimaryMonitor(); + auto videoMode = glfwGetVideoMode(monitor); + + // TODO: Set this on the correct monitor. + // Docs aren't clear on this... + glfwSetWindowMonitor(window, monitor, 0, 0, videoMode->width, videoMode->height, videoMode->refreshRate); + + return true; + } + + + bool leaveFullscreenMode( + HWND hWindow, + DxvkWindowState *pState, + bool restoreCoordinates) { + GLFWwindow *window = fromHwnd(hWindow); + + GLFWmonitor *monitor = glfwGetPrimaryMonitor(); + auto videoMode = glfwGetVideoMode(monitor); + glfwSetWindowMonitor(window, nullptr, 0, 0, videoMode->width, videoMode->height, videoMode->refreshRate); + + return true; + } + + + bool restoreDisplayMode() { + // Don't need to do anything with GLFW here. + return true; + } + + + HMONITOR getWindowMonitor(HWND hWindow) { + // TODO: implement this with glfwGetWindowMonitor + // (or maybe not? glfwGetWindowMonitor only seems to reference *fullscreen* windows) + // GLFWwindow *window = fromHwnd(hWindow); + const int32_t displayId = 0; + + return toHmonitor(displayId); + } + + + bool isWindow(HWND hWindow) { + GLFWwindow *window = fromHwnd(hWindow); + return window != nullptr; + } + + void updateFullscreenWindow( + HMONITOR hMonitor, + HWND hWindow, + bool forceTopmost) { + // Don't need to do anything with GLFW here. + } + + VkResult createSurface( + HWND hWindow, + PFN_vkGetInstanceProcAddr pfnVkGetInstanceProcAddr, + VkInstance instance, + VkSurfaceKHR *pSurface) { + GLFWwindow *window = fromHwnd(hWindow); + + return glfwCreateWindowSurface(instance, window, nullptr, pSurface); + } + +} \ No newline at end of file diff --git a/src/wsi/meson.build b/src/wsi/meson.build index 17efa26b24a..27603ceab5f 100644 --- a/src/wsi/meson.build +++ b/src/wsi/meson.build @@ -12,12 +12,20 @@ wsi_sdl2_src = [ 'sdl2/wsi_window_sdl2.cpp', ] +wsi_glfw_src = [ + 'glfw/wsi_monitor_glfw.cpp', + 'glfw/wsi_window_glfw.cpp', +] + if dxvk_wsi == 'win32' wsi_src = wsi_common_src + wsi_win32_src wsi_deps = [ dep_displayinfo ] elif dxvk_wsi == 'sdl2' wsi_src = wsi_common_src + wsi_sdl2_src wsi_deps = [ dep_displayinfo, lib_sdl2 ] +elif dxvk_wsi == 'glfw' + wsi_src = wsi_common_src + wsi_glfw_src + wsi_deps = [ dep_displayinfo, lib_glfw ] else error('Unknown wsi') endif diff --git a/src/wsi/wsi_platform.h b/src/wsi/wsi_platform.h index 2cbcd2292f2..38b5c5aa12f 100644 --- a/src/wsi/wsi_platform.h +++ b/src/wsi/wsi_platform.h @@ -4,4 +4,6 @@ #include "win32/wsi_platform_win32.h" #elif defined(DXVK_WSI_SDL2) #include "sdl2/wsi_platform_sdl2.h" +#elif defined(DXVK_WSI_GLFW) +#include "glfw/wsi_platform_glfw.h" #endif From 399ea21e490375b875a63d442d61b185fb893405 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Mon, 5 Dec 2022 11:27:48 -0800 Subject: [PATCH 2/8] Fix formatting --- src/dxvk/platform/dxvk_glfw_exts.cpp | 66 ++++----- src/wsi/glfw/wsi_monitor_glfw.cpp | 210 +++++++++++++-------------- src/wsi/glfw/wsi_platform_glfw.h | 22 +-- src/wsi/glfw/wsi_window_glfw.cpp | 192 ++++++++++++------------ 4 files changed, 245 insertions(+), 245 deletions(-) diff --git a/src/dxvk/platform/dxvk_glfw_exts.cpp b/src/dxvk/platform/dxvk_glfw_exts.cpp index 014125132d9..e6e57f620a8 100644 --- a/src/dxvk/platform/dxvk_glfw_exts.cpp +++ b/src/dxvk/platform/dxvk_glfw_exts.cpp @@ -5,53 +5,53 @@ namespace dxvk { - DxvkPlatformExts DxvkPlatformExts::s_instance; + DxvkPlatformExts DxvkPlatformExts::s_instance; - std::string_view DxvkPlatformExts::getName() { - return "GLFW WSI"; - } + std::string_view DxvkPlatformExts::getName() { + return "GLFW WSI"; + } - DxvkNameSet DxvkPlatformExts::getInstanceExtensions() { - if (!glfwVulkanSupported()) - throw DxvkError(str::format("GLFW WSI: Vulkan is not supported in any capacity!")); + DxvkNameSet DxvkPlatformExts::getInstanceExtensions() { + if (!glfwVulkanSupported()) + throw DxvkError(str::format("GLFW WSI: Vulkan is not supported in any capacity!")); - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - GLFWwindow* window = glfwCreateWindow(1, 1, "Dummy Window", nullptr, nullptr); - - if (window == nullptr) - throw DxvkError(str::format("GLFW WSI: Unable to create dummy window")); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + GLFWwindow* window = glfwCreateWindow(1, 1, "Dummy Window", nullptr, nullptr); - uint32_t extensionCount = 0; - const char** extensionArray = glfwGetRequiredInstanceExtensions(&extensionCount); - - if (extensionCount == 0) - throw DxvkError(str::format("GLFW WSI: Failed to get required instance extensions")); + if (window == nullptr) + throw DxvkError(str::format("GLFW WSI: Unable to create dummy window")); - DxvkNameSet names; - for (uint32_t i = 0; i < extensionCount; ++i) { - names.add(extensionArray[i]); - } + uint32_t extensionCount = 0; + const char** extensionArray = glfwGetRequiredInstanceExtensions(&extensionCount); - glfwDestroyWindow(window); + if (extensionCount == 0) + throw DxvkError(str::format("GLFW WSI: Failed to get required instance extensions")); - return names; + DxvkNameSet names; + for (uint32_t i = 0; i < extensionCount; ++i) { + names.add(extensionArray[i]); } - - DxvkNameSet DxvkPlatformExts::getDeviceExtensions( - uint32_t adapterId) { - return DxvkNameSet(); - } + glfwDestroyWindow(window); + + return names; + } - void DxvkPlatformExts::initInstanceExtensions() { + DxvkNameSet DxvkPlatformExts::getDeviceExtensions( + uint32_t adapterId) { + return DxvkNameSet(); + } - } + void DxvkPlatformExts::initInstanceExtensions() { + //Nothing needs to be done here on GLFW + } - void DxvkPlatformExts::initDeviceExtensions( - const DxvkInstance* instance) { - } + void DxvkPlatformExts::initDeviceExtensions( + const DxvkInstance* instance) { + //Nothing needs to be done here on GLFW + } } \ No newline at end of file diff --git a/src/wsi/glfw/wsi_monitor_glfw.cpp b/src/wsi/glfw/wsi_monitor_glfw.cpp index 1cb5aca7aac..99bac84df3a 100644 --- a/src/wsi/glfw/wsi_monitor_glfw.cpp +++ b/src/wsi/glfw/wsi_monitor_glfw.cpp @@ -11,150 +11,150 @@ namespace dxvk::wsi { - HMONITOR getDefaultMonitor() { - return enumMonitors(0); - } + HMONITOR getDefaultMonitor() { + return enumMonitors(0); + } - HMONITOR enumMonitors(uint32_t index) { - return isDisplayValid(int32_t(index)) - ? toHmonitor(index) - : nullptr; - } + HMONITOR enumMonitors(uint32_t index) { + return isDisplayValid(int32_t(index)) + ? toHmonitor(index) + : nullptr; + } - bool getDisplayName( - HMONITOR hMonitor, - WCHAR (&Name)[32]) { - const int32_t displayId = fromHmonitor(hMonitor); + bool getDisplayName( + HMONITOR hMonitor, + WCHAR (&Name)[32]) { + const int32_t displayId = fromHmonitor(hMonitor); - if (!isDisplayValid(displayId)) - return false; + if (!isDisplayValid(displayId)) + return false; - std::wstringstream nameStream; - nameStream << LR"(\\.\DISPLAY)" << (displayId + 1); + std::wstringstream nameStream; + nameStream << LR"(\\.\DISPLAY)" << (displayId + 1); - std::wstring name = nameStream.str(); + std::wstring name = nameStream.str(); - std::memset(Name, 0, sizeof(Name)); - name.copy(Name, name.length(), 0); + std::memset(Name, 0, sizeof(Name)); + name.copy(Name, name.length(), 0); - return true; - } + return true; + } - bool getDesktopCoordinates( - HMONITOR hMonitor, - RECT *pRect) { - const int32_t displayId = fromHmonitor(hMonitor); + bool getDesktopCoordinates( + HMONITOR hMonitor, + RECT *pRect) { + const int32_t displayId = fromHmonitor(hMonitor); - if (!isDisplayValid(displayId)) - return false; + if (!isDisplayValid(displayId)) + return false; - int32_t displayCount = 0; - GLFWmonitor **monitors = glfwGetMonitors(&displayCount); - GLFWmonitor *monitor = monitors[displayId]; + int32_t displayCount = 0; + GLFWmonitor **monitors = glfwGetMonitors(&displayCount); + GLFWmonitor *monitor = monitors[displayId]; - int32_t x; - int32_t y; - int32_t w; - int32_t h; - glfwGetMonitorWorkarea(monitor, &x, &y, &w, &h); + int32_t x; + int32_t y; + int32_t w; + int32_t h; + glfwGetMonitorWorkarea(monitor, &x, &y, &w, &h); - pRect->left = x; - pRect->top = y; - pRect->right = x + w; - pRect->bottom = y + h; + pRect->left = x; + pRect->top = y; + pRect->right = x + w; + pRect->bottom = y + h; - return true; - } + return true; + } - static inline uint32_t roundToNextPow2(uint32_t num) { - if (num-- == 0) - return 0; + static inline uint32_t roundToNextPow2(uint32_t num) { + if (num-- == 0) + return 0; - num |= num >> 1; - num |= num >> 2; - num |= num >> 4; - num |= num >> 8; - num |= num >> 16; + num |= num >> 1; + num |= num >> 2; + num |= num >> 4; + num |= num >> 8; + num |= num >> 16; - return ++num; - } + return ++num; + } - static inline void convertMode(const GLFWvidmode &mode, WsiMode *pMode) { - pMode->width = uint32_t(mode.width); - pMode->height = uint32_t(mode.height); - pMode->refreshRate = WsiRational{uint32_t(mode.refreshRate) * 1000, 1000}; - // BPP should always be a power of two - // to match Windows behaviour of including padding. - pMode->bitsPerPixel = roundToNextPow2(mode.blueBits + mode.redBits + mode.greenBits); - pMode->interlaced = false; - } + static inline void convertMode(const GLFWvidmode &mode, WsiMode *pMode) { + pMode->width = uint32_t(mode.width); + pMode->height = uint32_t(mode.height); + pMode->refreshRate = WsiRational{uint32_t(mode.refreshRate) * 1000, 1000}; + // BPP should always be a power of two + // to match Windows behaviour of including padding. + pMode->bitsPerPixel = roundToNextPow2(mode.blueBits + mode.redBits + mode.greenBits); + pMode->interlaced = false; + } - bool getDisplayMode( - HMONITOR hMonitor, - uint32_t ModeNumber, - WsiMode *pMode) { - const int32_t displayId = fromHmonitor(hMonitor); - int32_t displayCount = 0; - GLFWmonitor **monitors = glfwGetMonitors(&displayCount); - GLFWmonitor *monitor = monitors[displayId]; + bool getDisplayMode( + HMONITOR hMonitor, + uint32_t ModeNumber, + WsiMode *pMode) { + const int32_t displayId = fromHmonitor(hMonitor); + int32_t displayCount = 0; + GLFWmonitor **monitors = glfwGetMonitors(&displayCount); + GLFWmonitor *monitor = monitors[displayId]; - if (!isDisplayValid(displayId)) - return false; + if (!isDisplayValid(displayId)) + return false; - int32_t count = 0; - const GLFWvidmode *modes = glfwGetVideoModes(monitor, &count); + int32_t count = 0; + const GLFWvidmode *modes = glfwGetVideoModes(monitor, &count); - convertMode(modes[ModeNumber], pMode); + convertMode(modes[ModeNumber], pMode); - return true; - } + return true; + } - bool getCurrentDisplayMode( - HMONITOR hMonitor, - WsiMode *pMode) { - const int32_t displayId = fromHmonitor(hMonitor); + bool getCurrentDisplayMode( + HMONITOR hMonitor, + WsiMode *pMode) { + const int32_t displayId = fromHmonitor(hMonitor); - if (!isDisplayValid(displayId)) - return false; + if (!isDisplayValid(displayId)) + return false; - int32_t displayCount = 0; - GLFWmonitor **monitors = glfwGetMonitors(&displayCount); - GLFWmonitor *monitor = monitors[displayId]; + int32_t displayCount = 0; + GLFWmonitor **monitors = glfwGetMonitors(&displayCount); + GLFWmonitor *monitor = monitors[displayId]; - auto mode = glfwGetVideoMode(monitor); + auto mode = glfwGetVideoMode(monitor); - convertMode(*mode, pMode); + convertMode(*mode, pMode); - return true; - } + return true; + } - bool getDesktopDisplayMode( - HMONITOR hMonitor, - WsiMode *pMode) { - const int32_t displayId = fromHmonitor(hMonitor); + bool getDesktopDisplayMode( + HMONITOR hMonitor, + WsiMode *pMode) { + const int32_t displayId = fromHmonitor(hMonitor); - if (!isDisplayValid(displayId)) - return false; + if (!isDisplayValid(displayId)) + return false; - int32_t displayCount = 0; - GLFWmonitor **monitors = glfwGetMonitors(&displayCount); - GLFWmonitor *monitor = monitors[displayId]; + int32_t displayCount = 0; + GLFWmonitor **monitors = glfwGetMonitors(&displayCount); + GLFWmonitor *monitor = monitors[displayId]; - //TODO: actually implement this properly, currently we just grab the current one - convertMode(*glfwGetVideoMode(monitor), pMode); + //TODO: actually implement this properly, currently we just grab the current one + convertMode(*glfwGetVideoMode(monitor), pMode); - return true; - } + return true; + } - std::vector getMonitorEdid(HMONITOR hMonitor) { - Logger::err("getMonitorEdid not implemented on this platform."); - return {}; - } + std::vector getMonitorEdid(HMONITOR hMonitor) { + Logger::err("getMonitorEdid not implemented on this platform."); + return {}; + } } \ No newline at end of file diff --git a/src/wsi/glfw/wsi_platform_glfw.h b/src/wsi/glfw/wsi_platform_glfw.h index c8912d22bd9..6f71deeaa35 100644 --- a/src/wsi/glfw/wsi_platform_glfw.h +++ b/src/wsi/glfw/wsi_platform_glfw.h @@ -7,17 +7,17 @@ namespace dxvk::wsi { - /** - * \brief Impl-dependent state - */ - struct DxvkWindowState { - }; - - inline bool isDisplayValid(int32_t displayId) { - int32_t displayCount = 0; - glfwGetMonitors(&displayCount); + /** + * \brief Impl-dependent state + */ + struct DxvkWindowState { + }; + + inline bool isDisplayValid(int32_t displayId) { + int32_t displayCount = 0; + glfwGetMonitors(&displayCount); - return displayId < displayCount && displayId >= 0; - } + return displayId < displayCount && displayId >= 0; + } } \ No newline at end of file diff --git a/src/wsi/glfw/wsi_window_glfw.cpp b/src/wsi/glfw/wsi_window_glfw.cpp index 1175e95038e..f2598275418 100644 --- a/src/wsi/glfw/wsi_window_glfw.cpp +++ b/src/wsi/glfw/wsi_window_glfw.cpp @@ -12,133 +12,133 @@ namespace dxvk::wsi { - void getWindowSize( - HWND hWindow, - uint32_t *pWidth, - uint32_t *pHeight) { - GLFWwindow *window = fromHwnd(hWindow); + void getWindowSize( + HWND hWindow, + uint32_t *pWidth, + uint32_t *pHeight) { + GLFWwindow *window = fromHwnd(hWindow); - int32_t w, h; - glfwGetWindowSize(window, &w, &h); + int32_t w, h; + glfwGetWindowSize(window, &w, &h); - if (pWidth) - *pWidth = uint32_t(w); + if (pWidth) + *pWidth = uint32_t(w); - if (pHeight) - *pHeight = uint32_t(h); - } + if (pHeight) + *pHeight = uint32_t(h); + } - void resizeWindow( - HWND hWindow, - DxvkWindowState *pState, - uint32_t Width, - uint32_t Height) { - GLFWwindow *window = fromHwnd(hWindow); + void resizeWindow( + HWND hWindow, + DxvkWindowState *pState, + uint32_t Width, + uint32_t Height) { + GLFWwindow *window = fromHwnd(hWindow); - glfwSetWindowSize(window, int32_t(Width), int32_t(Height)); - } + glfwSetWindowSize(window, int32_t(Width), int32_t(Height)); + } - bool setWindowMode( - HMONITOR hMonitor, - HWND hWindow, - const WsiMode& pMode) { - const int32_t displayId = fromHmonitor(hMonitor); - GLFWwindow *window = fromHwnd(hWindow); + bool setWindowMode( + HMONITOR hMonitor, + HWND hWindow, + const WsiMode& pMode) { + const int32_t displayId = fromHmonitor(hMonitor); + GLFWwindow *window = fromHwnd(hWindow); - if (!isDisplayValid(displayId)) - return false; + if (!isDisplayValid(displayId)) + return false; - int32_t displayCount = 0; - GLFWmonitor **monitors = glfwGetMonitors(&displayCount); - GLFWmonitor *monitor = monitors[displayId]; + int32_t displayCount = 0; + GLFWmonitor **monitors = glfwGetMonitors(&displayCount); + GLFWmonitor *monitor = monitors[displayId]; - GLFWvidmode wantedMode = {}; - wantedMode.width = pMode.width; - wantedMode.height = pMode.height; - wantedMode.refreshRate = pMode.refreshRate.numerator != 0 - ? pMode.refreshRate.numerator / pMode.refreshRate.denominator - : 0; - // TODO: Implement lookup format for bitsPerPixel here. + GLFWvidmode wantedMode = {}; + wantedMode.width = pMode.width; + wantedMode.height = pMode.height; + wantedMode.refreshRate = pMode.refreshRate.numerator != 0 + ? pMode.refreshRate.numerator / pMode.refreshRate.denominator + : 0; + // TODO: Implement lookup format for bitsPerPixel here. - glfwSetWindowMonitor(window, monitor, 0, 0, wantedMode.width, wantedMode.width, wantedMode.refreshRate); + glfwSetWindowMonitor(window, monitor, 0, 0, wantedMode.width, wantedMode.width, wantedMode.refreshRate); - return true; - } + return true; + } - bool enterFullscreenMode( - HMONITOR hMonitor, - HWND hWindow, - DxvkWindowState *pState, - bool ModeSwitch) { - const int32_t displayId = fromHmonitor(hMonitor); - GLFWwindow *window = fromHwnd(hWindow); + bool enterFullscreenMode( + HMONITOR hMonitor, + HWND hWindow, + DxvkWindowState *pState, + bool ModeSwitch) { + const int32_t displayId = fromHmonitor(hMonitor); + GLFWwindow *window = fromHwnd(hWindow); - if (!isDisplayValid(displayId)) - return false; + if (!isDisplayValid(displayId)) + return false; - GLFWmonitor *monitor = glfwGetPrimaryMonitor(); - auto videoMode = glfwGetVideoMode(monitor); + GLFWmonitor *monitor = glfwGetPrimaryMonitor(); + auto videoMode = glfwGetVideoMode(monitor); - // TODO: Set this on the correct monitor. - // Docs aren't clear on this... - glfwSetWindowMonitor(window, monitor, 0, 0, videoMode->width, videoMode->height, videoMode->refreshRate); + // TODO: Set this on the correct monitor. + // Docs aren't clear on this... + glfwSetWindowMonitor(window, monitor, 0, 0, videoMode->width, videoMode->height, videoMode->refreshRate); - return true; - } + return true; + } - bool leaveFullscreenMode( - HWND hWindow, - DxvkWindowState *pState, - bool restoreCoordinates) { - GLFWwindow *window = fromHwnd(hWindow); + bool leaveFullscreenMode( + HWND hWindow, + DxvkWindowState *pState, + bool restoreCoordinates) { + GLFWwindow *window = fromHwnd(hWindow); - GLFWmonitor *monitor = glfwGetPrimaryMonitor(); - auto videoMode = glfwGetVideoMode(monitor); - glfwSetWindowMonitor(window, nullptr, 0, 0, videoMode->width, videoMode->height, videoMode->refreshRate); + GLFWmonitor *monitor = glfwGetPrimaryMonitor(); + auto videoMode = glfwGetVideoMode(monitor); + glfwSetWindowMonitor(window, nullptr, 0, 0, videoMode->width, videoMode->height, videoMode->refreshRate); - return true; - } + return true; + } - bool restoreDisplayMode() { - // Don't need to do anything with GLFW here. - return true; - } + bool restoreDisplayMode() { + // Don't need to do anything with GLFW here. + return true; + } - HMONITOR getWindowMonitor(HWND hWindow) { - // TODO: implement this with glfwGetWindowMonitor - // (or maybe not? glfwGetWindowMonitor only seems to reference *fullscreen* windows) - // GLFWwindow *window = fromHwnd(hWindow); - const int32_t displayId = 0; + HMONITOR getWindowMonitor(HWND hWindow) { + // TODO: implement this with glfwGetWindowMonitor + // (or maybe not? glfwGetWindowMonitor only seems to reference *fullscreen* windows) + // GLFWwindow *window = fromHwnd(hWindow); + const int32_t displayId = 0; - return toHmonitor(displayId); - } + return toHmonitor(displayId); + } - bool isWindow(HWND hWindow) { - GLFWwindow *window = fromHwnd(hWindow); - return window != nullptr; - } + bool isWindow(HWND hWindow) { + GLFWwindow *window = fromHwnd(hWindow); + return window != nullptr; + } - void updateFullscreenWindow( - HMONITOR hMonitor, - HWND hWindow, - bool forceTopmost) { - // Don't need to do anything with GLFW here. - } + void updateFullscreenWindow( + HMONITOR hMonitor, + HWND hWindow, + bool forceTopmost) { + // Don't need to do anything with GLFW here. + } - VkResult createSurface( - HWND hWindow, - PFN_vkGetInstanceProcAddr pfnVkGetInstanceProcAddr, - VkInstance instance, - VkSurfaceKHR *pSurface) { - GLFWwindow *window = fromHwnd(hWindow); + VkResult createSurface( + HWND hWindow, + PFN_vkGetInstanceProcAddr pfnVkGetInstanceProcAddr, + VkInstance instance, + VkSurfaceKHR *pSurface) { + GLFWwindow *window = fromHwnd(hWindow); - return glfwCreateWindowSurface(instance, window, nullptr, pSurface); - } + return glfwCreateWindowSurface(instance, window, nullptr, pSurface); + } } \ No newline at end of file From 29ac10f5842b29208fcfd65c462e5cdca42172ba Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Mon, 5 Dec 2022 11:46:11 -0800 Subject: [PATCH 3/8] Fix build and remove hack in vulkan_loader --- src/dxvk/platform/dxvk_glfw_exts.cpp | 2 +- src/vulkan/vulkan_loader.h | 4 ++-- src/wsi/glfw/wsi_platform_glfw.h | 2 +- src/wsi/glfw/wsi_window_glfw.cpp | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/dxvk/platform/dxvk_glfw_exts.cpp b/src/dxvk/platform/dxvk_glfw_exts.cpp index e6e57f620a8..a9977d5d3b4 100644 --- a/src/dxvk/platform/dxvk_glfw_exts.cpp +++ b/src/dxvk/platform/dxvk_glfw_exts.cpp @@ -1,6 +1,6 @@ #include "../dxvk_platform_exts.h" -#define GLFW_INCLUDE_VULKAN +#include "../../vulkan/vulkan_loader.h" #include namespace dxvk { diff --git a/src/vulkan/vulkan_loader.h b/src/vulkan/vulkan_loader.h index 42f63ddd1e0..653cf3975ba 100644 --- a/src/vulkan/vulkan_loader.h +++ b/src/vulkan/vulkan_loader.h @@ -135,8 +135,8 @@ namespace dxvk::vk { #endif #ifdef VK_USE_PLATFORM_WIN32_KHR -// VULKAN_FN(vkCreateWin32SurfaceKHR); -// VULKAN_FN(vkGetPhysicalDeviceWin32PresentationSupportKHR); + VULKAN_FN(vkCreateWin32SurfaceKHR); + VULKAN_FN(vkGetPhysicalDeviceWin32PresentationSupportKHR); #endif VULKAN_FN(vkDestroySurfaceKHR); diff --git a/src/wsi/glfw/wsi_platform_glfw.h b/src/wsi/glfw/wsi_platform_glfw.h index 6f71deeaa35..978815761ae 100644 --- a/src/wsi/glfw/wsi_platform_glfw.h +++ b/src/wsi/glfw/wsi_platform_glfw.h @@ -1,6 +1,6 @@ #pragma once -#define GLFW_INCLUDE_VULKAN +#include "../../vulkan/vulkan_loader.h" #include #include "../wsi_monitor.h" diff --git a/src/wsi/glfw/wsi_window_glfw.cpp b/src/wsi/glfw/wsi_window_glfw.cpp index f2598275418..d6edfda5530 100644 --- a/src/wsi/glfw/wsi_window_glfw.cpp +++ b/src/wsi/glfw/wsi_window_glfw.cpp @@ -7,7 +7,7 @@ #include "../../util/log/log.h" #include -#define GLFW_INCLUDE_VULKAN +#include "../../vulkan/vulkan_loader.h" #include namespace dxvk::wsi { From c9e18cdb6039587f7aadf610c61d946a89ae8fdc Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Sun, 11 Dec 2022 12:07:07 -0800 Subject: [PATCH 4/8] Add option to meson_options to change the DXVK native WSI --- meson.build | 2 +- meson_options.txt | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 8e1f0ca0c09..39a28e0526d 100644 --- a/meson.build +++ b/meson.build @@ -112,7 +112,7 @@ else './include/native/directx' ] - dxvk_wsi = 'glfw' + dxvk_wsi = get_option('dxvk_native_wsi') if dxvk_wsi == 'sdl2' lib_sdl2 = cpp.find_library('SDL2') diff --git a/meson_options.txt b/meson_options.txt index 36fd01365ed..5ac9ea7b432 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -3,3 +3,5 @@ option('enable_d3d9', type : 'boolean', value : true, description: 'Build D3D9' option('enable_d3d10', type : 'boolean', value : true, description: 'Build D3D10') option('enable_d3d11', type : 'boolean', value : true, description: 'Build D3D11') option('build_id', type : 'boolean', value : false) + +option('dxvk_native_wsi', type : 'string', value : 'sdl2', description: 'WSI system to use if building natively.') \ No newline at end of file From 7d8594566854012d8beb6ddbebed27d1026fc93d Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Wed, 14 Dec 2022 10:27:30 -0800 Subject: [PATCH 5/8] Fix formatting of pointer types --- src/wsi/glfw/wsi_monitor_glfw.cpp | 28 ++++++++++++------------ src/wsi/glfw/wsi_window_glfw.cpp | 36 +++++++++++++++---------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/wsi/glfw/wsi_monitor_glfw.cpp b/src/wsi/glfw/wsi_monitor_glfw.cpp index 99bac84df3a..9537b9feb2e 100644 --- a/src/wsi/glfw/wsi_monitor_glfw.cpp +++ b/src/wsi/glfw/wsi_monitor_glfw.cpp @@ -44,15 +44,15 @@ namespace dxvk::wsi { bool getDesktopCoordinates( HMONITOR hMonitor, - RECT *pRect) { + RECT* pRect) { const int32_t displayId = fromHmonitor(hMonitor); if (!isDisplayValid(displayId)) return false; int32_t displayCount = 0; - GLFWmonitor **monitors = glfwGetMonitors(&displayCount); - GLFWmonitor *monitor = monitors[displayId]; + GLFWmonitor** monitors = glfwGetMonitors(&displayCount); + GLFWmonitor* monitor = monitors[displayId]; int32_t x; int32_t y; @@ -82,7 +82,7 @@ namespace dxvk::wsi { } - static inline void convertMode(const GLFWvidmode &mode, WsiMode *pMode) { + static inline void convertMode(const GLFWvidmode& mode, WsiMode* pMode) { pMode->width = uint32_t(mode.width); pMode->height = uint32_t(mode.height); pMode->refreshRate = WsiRational{uint32_t(mode.refreshRate) * 1000, 1000}; @@ -96,17 +96,17 @@ namespace dxvk::wsi { bool getDisplayMode( HMONITOR hMonitor, uint32_t ModeNumber, - WsiMode *pMode) { + WsiMode* pMode) { const int32_t displayId = fromHmonitor(hMonitor); int32_t displayCount = 0; - GLFWmonitor **monitors = glfwGetMonitors(&displayCount); - GLFWmonitor *monitor = monitors[displayId]; + GLFWmonitor** monitors = glfwGetMonitors(&displayCount); + GLFWmonitor* monitor = monitors[displayId]; if (!isDisplayValid(displayId)) return false; int32_t count = 0; - const GLFWvidmode *modes = glfwGetVideoModes(monitor, &count); + const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count); convertMode(modes[ModeNumber], pMode); @@ -116,15 +116,15 @@ namespace dxvk::wsi { bool getCurrentDisplayMode( HMONITOR hMonitor, - WsiMode *pMode) { + WsiMode* pMode) { const int32_t displayId = fromHmonitor(hMonitor); if (!isDisplayValid(displayId)) return false; int32_t displayCount = 0; - GLFWmonitor **monitors = glfwGetMonitors(&displayCount); - GLFWmonitor *monitor = monitors[displayId]; + GLFWmonitor** monitors = glfwGetMonitors(&displayCount); + GLFWmonitor* monitor = monitors[displayId]; auto mode = glfwGetVideoMode(monitor); @@ -136,15 +136,15 @@ namespace dxvk::wsi { bool getDesktopDisplayMode( HMONITOR hMonitor, - WsiMode *pMode) { + WsiMode* pMode) { const int32_t displayId = fromHmonitor(hMonitor); if (!isDisplayValid(displayId)) return false; int32_t displayCount = 0; - GLFWmonitor **monitors = glfwGetMonitors(&displayCount); - GLFWmonitor *monitor = monitors[displayId]; + GLFWmonitor** monitors = glfwGetMonitors(&displayCount); + GLFWmonitor* monitor = monitors[displayId]; //TODO: actually implement this properly, currently we just grab the current one convertMode(*glfwGetVideoMode(monitor), pMode); diff --git a/src/wsi/glfw/wsi_window_glfw.cpp b/src/wsi/glfw/wsi_window_glfw.cpp index d6edfda5530..27acaed2333 100644 --- a/src/wsi/glfw/wsi_window_glfw.cpp +++ b/src/wsi/glfw/wsi_window_glfw.cpp @@ -14,9 +14,9 @@ namespace dxvk::wsi { void getWindowSize( HWND hWindow, - uint32_t *pWidth, - uint32_t *pHeight) { - GLFWwindow *window = fromHwnd(hWindow); + uint32_t* pWidth, + uint32_t* pHeight) { + GLFWwindow* window = fromHwnd(hWindow); int32_t w, h; glfwGetWindowSize(window, &w, &h); @@ -31,10 +31,10 @@ namespace dxvk::wsi { void resizeWindow( HWND hWindow, - DxvkWindowState *pState, + DxvkWindowState* pState, uint32_t Width, uint32_t Height) { - GLFWwindow *window = fromHwnd(hWindow); + GLFWwindow* window = fromHwnd(hWindow); glfwSetWindowSize(window, int32_t(Width), int32_t(Height)); } @@ -45,14 +45,14 @@ namespace dxvk::wsi { HWND hWindow, const WsiMode& pMode) { const int32_t displayId = fromHmonitor(hMonitor); - GLFWwindow *window = fromHwnd(hWindow); + GLFWwindow* window = fromHwnd(hWindow); if (!isDisplayValid(displayId)) return false; int32_t displayCount = 0; - GLFWmonitor **monitors = glfwGetMonitors(&displayCount); - GLFWmonitor *monitor = monitors[displayId]; + GLFWmonitor** monitors = glfwGetMonitors(&displayCount); + GLFWmonitor* monitor = monitors[displayId]; GLFWvidmode wantedMode = {}; wantedMode.width = pMode.width; @@ -70,15 +70,15 @@ namespace dxvk::wsi { bool enterFullscreenMode( HMONITOR hMonitor, HWND hWindow, - DxvkWindowState *pState, + DxvkWindowState* pState, bool ModeSwitch) { const int32_t displayId = fromHmonitor(hMonitor); - GLFWwindow *window = fromHwnd(hWindow); + GLFWwindow* window = fromHwnd(hWindow); if (!isDisplayValid(displayId)) return false; - GLFWmonitor *monitor = glfwGetPrimaryMonitor(); + GLFWmonitor* monitor = glfwGetPrimaryMonitor(); auto videoMode = glfwGetVideoMode(monitor); // TODO: Set this on the correct monitor. @@ -91,11 +91,11 @@ namespace dxvk::wsi { bool leaveFullscreenMode( HWND hWindow, - DxvkWindowState *pState, + DxvkWindowState* pState, bool restoreCoordinates) { - GLFWwindow *window = fromHwnd(hWindow); + GLFWwindow* window = fromHwnd(hWindow); - GLFWmonitor *monitor = glfwGetPrimaryMonitor(); + GLFWmonitor* monitor = glfwGetPrimaryMonitor(); auto videoMode = glfwGetVideoMode(monitor); glfwSetWindowMonitor(window, nullptr, 0, 0, videoMode->width, videoMode->height, videoMode->refreshRate); @@ -112,7 +112,7 @@ namespace dxvk::wsi { HMONITOR getWindowMonitor(HWND hWindow) { // TODO: implement this with glfwGetWindowMonitor // (or maybe not? glfwGetWindowMonitor only seems to reference *fullscreen* windows) - // GLFWwindow *window = fromHwnd(hWindow); + // GLFWwindow* window = fromHwnd(hWindow); const int32_t displayId = 0; return toHmonitor(displayId); @@ -120,7 +120,7 @@ namespace dxvk::wsi { bool isWindow(HWND hWindow) { - GLFWwindow *window = fromHwnd(hWindow); + GLFWwindow* window = fromHwnd(hWindow); return window != nullptr; } @@ -135,8 +135,8 @@ namespace dxvk::wsi { HWND hWindow, PFN_vkGetInstanceProcAddr pfnVkGetInstanceProcAddr, VkInstance instance, - VkSurfaceKHR *pSurface) { - GLFWwindow *window = fromHwnd(hWindow); + VkSurfaceKHR* pSurface) { + GLFWwindow* window = fromHwnd(hWindow); return glfwCreateWindowSurface(instance, window, nullptr, pSurface); } From 893f24b95e45960da408310388a052a7b45fcb21 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Wed, 14 Dec 2022 10:32:41 -0800 Subject: [PATCH 6/8] Remove unneeded GLFW window --- src/dxvk/platform/dxvk_glfw_exts.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/dxvk/platform/dxvk_glfw_exts.cpp b/src/dxvk/platform/dxvk_glfw_exts.cpp index a9977d5d3b4..f5cfee88d78 100644 --- a/src/dxvk/platform/dxvk_glfw_exts.cpp +++ b/src/dxvk/platform/dxvk_glfw_exts.cpp @@ -15,12 +15,6 @@ namespace dxvk { if (!glfwVulkanSupported()) throw DxvkError(str::format("GLFW WSI: Vulkan is not supported in any capacity!")); - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - GLFWwindow* window = glfwCreateWindow(1, 1, "Dummy Window", nullptr, nullptr); - - if (window == nullptr) - throw DxvkError(str::format("GLFW WSI: Unable to create dummy window")); - uint32_t extensionCount = 0; const char** extensionArray = glfwGetRequiredInstanceExtensions(&extensionCount); @@ -32,8 +26,6 @@ namespace dxvk { names.add(extensionArray[i]); } - glfwDestroyWindow(window); - return names; } From db0f6e9ea535e63f2ed092e7b960c1522071bcc9 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Mon, 2 Jan 2023 13:36:29 -0800 Subject: [PATCH 7/8] Fix formatting more --- src/wsi/glfw/wsi_monitor_glfw.cpp | 210 +++++++++++++++--------------- src/wsi/glfw/wsi_platform_glfw.h | 22 ++-- src/wsi/glfw/wsi_window_glfw.cpp | 192 +++++++++++++-------------- 3 files changed, 212 insertions(+), 212 deletions(-) diff --git a/src/wsi/glfw/wsi_monitor_glfw.cpp b/src/wsi/glfw/wsi_monitor_glfw.cpp index 9537b9feb2e..782d058453b 100644 --- a/src/wsi/glfw/wsi_monitor_glfw.cpp +++ b/src/wsi/glfw/wsi_monitor_glfw.cpp @@ -11,150 +11,150 @@ namespace dxvk::wsi { - HMONITOR getDefaultMonitor() { - return enumMonitors(0); - } + HMONITOR getDefaultMonitor() { + return enumMonitors(0); + } - HMONITOR enumMonitors(uint32_t index) { - return isDisplayValid(int32_t(index)) - ? toHmonitor(index) - : nullptr; - } + HMONITOR enumMonitors(uint32_t index) { + return isDisplayValid(int32_t(index)) + ? toHmonitor(index) + : nullptr; + } - bool getDisplayName( - HMONITOR hMonitor, - WCHAR (&Name)[32]) { - const int32_t displayId = fromHmonitor(hMonitor); + bool getDisplayName( + HMONITOR hMonitor, + WCHAR (&Name)[32]) { + const int32_t displayId = fromHmonitor(hMonitor); - if (!isDisplayValid(displayId)) - return false; + if (!isDisplayValid(displayId)) + return false; - std::wstringstream nameStream; - nameStream << LR"(\\.\DISPLAY)" << (displayId + 1); + std::wstringstream nameStream; + nameStream << LR"(\\.\DISPLAY)" << (displayId + 1); - std::wstring name = nameStream.str(); + std::wstring name = nameStream.str(); - std::memset(Name, 0, sizeof(Name)); - name.copy(Name, name.length(), 0); + std::memset(Name, 0, sizeof(Name)); + name.copy(Name, name.length(), 0); - return true; - } + return true; + } - bool getDesktopCoordinates( - HMONITOR hMonitor, - RECT* pRect) { - const int32_t displayId = fromHmonitor(hMonitor); + bool getDesktopCoordinates( + HMONITOR hMonitor, + RECT* pRect) { + const int32_t displayId = fromHmonitor(hMonitor); - if (!isDisplayValid(displayId)) - return false; + if (!isDisplayValid(displayId)) + return false; - int32_t displayCount = 0; - GLFWmonitor** monitors = glfwGetMonitors(&displayCount); - GLFWmonitor* monitor = monitors[displayId]; + int32_t displayCount = 0; + GLFWmonitor** monitors = glfwGetMonitors(&displayCount); + GLFWmonitor* monitor = monitors[displayId]; - int32_t x; - int32_t y; - int32_t w; - int32_t h; - glfwGetMonitorWorkarea(monitor, &x, &y, &w, &h); + int32_t x; + int32_t y; + int32_t w; + int32_t h; + glfwGetMonitorWorkarea(monitor, &x, &y, &w, &h); - pRect->left = x; - pRect->top = y; - pRect->right = x + w; - pRect->bottom = y + h; + pRect->left = x; + pRect->top = y; + pRect->right = x + w; + pRect->bottom = y + h; - return true; - } + return true; + } - static inline uint32_t roundToNextPow2(uint32_t num) { - if (num-- == 0) - return 0; + static inline uint32_t roundToNextPow2(uint32_t num) { + if (num-- == 0) + return 0; - num |= num >> 1; - num |= num >> 2; - num |= num >> 4; - num |= num >> 8; - num |= num >> 16; + num |= num >> 1; + num |= num >> 2; + num |= num >> 4; + num |= num >> 8; + num |= num >> 16; - return ++num; - } + return ++num; + } - static inline void convertMode(const GLFWvidmode& mode, WsiMode* pMode) { - pMode->width = uint32_t(mode.width); - pMode->height = uint32_t(mode.height); - pMode->refreshRate = WsiRational{uint32_t(mode.refreshRate) * 1000, 1000}; - // BPP should always be a power of two - // to match Windows behaviour of including padding. - pMode->bitsPerPixel = roundToNextPow2(mode.blueBits + mode.redBits + mode.greenBits); - pMode->interlaced = false; - } + static inline void convertMode(const GLFWvidmode& mode, WsiMode* pMode) { + pMode->width = uint32_t(mode.width); + pMode->height = uint32_t(mode.height); + pMode->refreshRate = WsiRational{uint32_t(mode.refreshRate) * 1000, 1000}; + // BPP should always be a power of two + // to match Windows behaviour of including padding. + pMode->bitsPerPixel = roundToNextPow2(mode.blueBits + mode.redBits + mode.greenBits); + pMode->interlaced = false; + } - bool getDisplayMode( - HMONITOR hMonitor, - uint32_t ModeNumber, - WsiMode* pMode) { - const int32_t displayId = fromHmonitor(hMonitor); - int32_t displayCount = 0; - GLFWmonitor** monitors = glfwGetMonitors(&displayCount); - GLFWmonitor* monitor = monitors[displayId]; + bool getDisplayMode( + HMONITOR hMonitor, + uint32_t ModeNumber, + WsiMode* pMode) { + const int32_t displayId = fromHmonitor(hMonitor); + int32_t displayCount = 0; + GLFWmonitor** monitors = glfwGetMonitors(&displayCount); + GLFWmonitor* monitor = monitors[displayId]; - if (!isDisplayValid(displayId)) - return false; + if (!isDisplayValid(displayId)) + return false; - int32_t count = 0; - const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count); + int32_t count = 0; + const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count); - convertMode(modes[ModeNumber], pMode); + convertMode(modes[ModeNumber], pMode); - return true; - } + return true; + } - bool getCurrentDisplayMode( - HMONITOR hMonitor, - WsiMode* pMode) { - const int32_t displayId = fromHmonitor(hMonitor); + bool getCurrentDisplayMode( + HMONITOR hMonitor, + WsiMode* pMode) { + const int32_t displayId = fromHmonitor(hMonitor); - if (!isDisplayValid(displayId)) - return false; + if (!isDisplayValid(displayId)) + return false; - int32_t displayCount = 0; - GLFWmonitor** monitors = glfwGetMonitors(&displayCount); - GLFWmonitor* monitor = monitors[displayId]; + int32_t displayCount = 0; + GLFWmonitor** monitors = glfwGetMonitors(&displayCount); + GLFWmonitor* monitor = monitors[displayId]; - auto mode = glfwGetVideoMode(monitor); + auto mode = glfwGetVideoMode(monitor); - convertMode(*mode, pMode); + convertMode(*mode, pMode); - return true; - } + return true; + } - bool getDesktopDisplayMode( - HMONITOR hMonitor, - WsiMode* pMode) { - const int32_t displayId = fromHmonitor(hMonitor); + bool getDesktopDisplayMode( + HMONITOR hMonitor, + WsiMode* pMode) { + const int32_t displayId = fromHmonitor(hMonitor); - if (!isDisplayValid(displayId)) - return false; + if (!isDisplayValid(displayId)) + return false; - int32_t displayCount = 0; - GLFWmonitor** monitors = glfwGetMonitors(&displayCount); - GLFWmonitor* monitor = monitors[displayId]; + int32_t displayCount = 0; + GLFWmonitor** monitors = glfwGetMonitors(&displayCount); + GLFWmonitor* monitor = monitors[displayId]; - //TODO: actually implement this properly, currently we just grab the current one - convertMode(*glfwGetVideoMode(monitor), pMode); + //TODO: actually implement this properly, currently we just grab the current one + convertMode(*glfwGetVideoMode(monitor), pMode); - return true; - } + return true; + } - std::vector getMonitorEdid(HMONITOR hMonitor) { - Logger::err("getMonitorEdid not implemented on this platform."); - return {}; - } + std::vector getMonitorEdid(HMONITOR hMonitor) { + Logger::err("getMonitorEdid not implemented on this platform."); + return {}; + } } \ No newline at end of file diff --git a/src/wsi/glfw/wsi_platform_glfw.h b/src/wsi/glfw/wsi_platform_glfw.h index 978815761ae..25753494f04 100644 --- a/src/wsi/glfw/wsi_platform_glfw.h +++ b/src/wsi/glfw/wsi_platform_glfw.h @@ -7,17 +7,17 @@ namespace dxvk::wsi { - /** - * \brief Impl-dependent state - */ - struct DxvkWindowState { - }; - - inline bool isDisplayValid(int32_t displayId) { - int32_t displayCount = 0; - glfwGetMonitors(&displayCount); + /** + * \brief Impl-dependent state + */ + struct DxvkWindowState { + }; + + inline bool isDisplayValid(int32_t displayId) { + int32_t displayCount = 0; + glfwGetMonitors(&displayCount); - return displayId < displayCount && displayId >= 0; - } + return displayId < displayCount && displayId >= 0; + } } \ No newline at end of file diff --git a/src/wsi/glfw/wsi_window_glfw.cpp b/src/wsi/glfw/wsi_window_glfw.cpp index 27acaed2333..700312507bc 100644 --- a/src/wsi/glfw/wsi_window_glfw.cpp +++ b/src/wsi/glfw/wsi_window_glfw.cpp @@ -12,133 +12,133 @@ namespace dxvk::wsi { - void getWindowSize( - HWND hWindow, - uint32_t* pWidth, - uint32_t* pHeight) { - GLFWwindow* window = fromHwnd(hWindow); + void getWindowSize( + HWND hWindow, + uint32_t* pWidth, + uint32_t* pHeight) { + GLFWwindow* window = fromHwnd(hWindow); - int32_t w, h; - glfwGetWindowSize(window, &w, &h); + int32_t w, h; + glfwGetWindowSize(window, &w, &h); - if (pWidth) - *pWidth = uint32_t(w); + if (pWidth) + *pWidth = uint32_t(w); - if (pHeight) - *pHeight = uint32_t(h); - } + if (pHeight) + *pHeight = uint32_t(h); + } - void resizeWindow( - HWND hWindow, - DxvkWindowState* pState, - uint32_t Width, - uint32_t Height) { - GLFWwindow* window = fromHwnd(hWindow); + void resizeWindow( + HWND hWindow, + DxvkWindowState* pState, + uint32_t Width, + uint32_t Height) { + GLFWwindow* window = fromHwnd(hWindow); - glfwSetWindowSize(window, int32_t(Width), int32_t(Height)); - } + glfwSetWindowSize(window, int32_t(Width), int32_t(Height)); + } - bool setWindowMode( - HMONITOR hMonitor, - HWND hWindow, - const WsiMode& pMode) { - const int32_t displayId = fromHmonitor(hMonitor); - GLFWwindow* window = fromHwnd(hWindow); + bool setWindowMode( + HMONITOR hMonitor, + HWND hWindow, + const WsiMode& pMode) { + const int32_t displayId = fromHmonitor(hMonitor); + GLFWwindow* window = fromHwnd(hWindow); - if (!isDisplayValid(displayId)) - return false; + if (!isDisplayValid(displayId)) + return false; - int32_t displayCount = 0; - GLFWmonitor** monitors = glfwGetMonitors(&displayCount); - GLFWmonitor* monitor = monitors[displayId]; + int32_t displayCount = 0; + GLFWmonitor** monitors = glfwGetMonitors(&displayCount); + GLFWmonitor* monitor = monitors[displayId]; - GLFWvidmode wantedMode = {}; - wantedMode.width = pMode.width; - wantedMode.height = pMode.height; - wantedMode.refreshRate = pMode.refreshRate.numerator != 0 - ? pMode.refreshRate.numerator / pMode.refreshRate.denominator - : 0; - // TODO: Implement lookup format for bitsPerPixel here. + GLFWvidmode wantedMode = {}; + wantedMode.width = pMode.width; + wantedMode.height = pMode.height; + wantedMode.refreshRate = pMode.refreshRate.numerator != 0 + ? pMode.refreshRate.numerator / pMode.refreshRate.denominator + : 0; + // TODO: Implement lookup format for bitsPerPixel here. - glfwSetWindowMonitor(window, monitor, 0, 0, wantedMode.width, wantedMode.width, wantedMode.refreshRate); + glfwSetWindowMonitor(window, monitor, 0, 0, wantedMode.width, wantedMode.width, wantedMode.refreshRate); - return true; - } + return true; + } - bool enterFullscreenMode( - HMONITOR hMonitor, - HWND hWindow, - DxvkWindowState* pState, - bool ModeSwitch) { - const int32_t displayId = fromHmonitor(hMonitor); - GLFWwindow* window = fromHwnd(hWindow); + bool enterFullscreenMode( + HMONITOR hMonitor, + HWND hWindow, + DxvkWindowState* pState, + bool ModeSwitch) { + const int32_t displayId = fromHmonitor(hMonitor); + GLFWwindow* window = fromHwnd(hWindow); - if (!isDisplayValid(displayId)) - return false; + if (!isDisplayValid(displayId)) + return false; - GLFWmonitor* monitor = glfwGetPrimaryMonitor(); - auto videoMode = glfwGetVideoMode(monitor); + GLFWmonitor* monitor = glfwGetPrimaryMonitor(); + auto videoMode = glfwGetVideoMode(monitor); - // TODO: Set this on the correct monitor. - // Docs aren't clear on this... - glfwSetWindowMonitor(window, monitor, 0, 0, videoMode->width, videoMode->height, videoMode->refreshRate); + // TODO: Set this on the correct monitor. + // Docs aren't clear on this... + glfwSetWindowMonitor(window, monitor, 0, 0, videoMode->width, videoMode->height, videoMode->refreshRate); - return true; - } + return true; + } - bool leaveFullscreenMode( - HWND hWindow, - DxvkWindowState* pState, - bool restoreCoordinates) { - GLFWwindow* window = fromHwnd(hWindow); + bool leaveFullscreenMode( + HWND hWindow, + DxvkWindowState* pState, + bool restoreCoordinates) { + GLFWwindow* window = fromHwnd(hWindow); - GLFWmonitor* monitor = glfwGetPrimaryMonitor(); - auto videoMode = glfwGetVideoMode(monitor); - glfwSetWindowMonitor(window, nullptr, 0, 0, videoMode->width, videoMode->height, videoMode->refreshRate); + GLFWmonitor* monitor = glfwGetPrimaryMonitor(); + auto videoMode = glfwGetVideoMode(monitor); + glfwSetWindowMonitor(window, nullptr, 0, 0, videoMode->width, videoMode->height, videoMode->refreshRate); - return true; - } + return true; + } - bool restoreDisplayMode() { - // Don't need to do anything with GLFW here. - return true; - } + bool restoreDisplayMode() { + // Don't need to do anything with GLFW here. + return true; + } - HMONITOR getWindowMonitor(HWND hWindow) { - // TODO: implement this with glfwGetWindowMonitor - // (or maybe not? glfwGetWindowMonitor only seems to reference *fullscreen* windows) - // GLFWwindow* window = fromHwnd(hWindow); - const int32_t displayId = 0; + HMONITOR getWindowMonitor(HWND hWindow) { + // TODO: implement this with glfwGetWindowMonitor + // (or maybe not? glfwGetWindowMonitor only seems to reference *fullscreen* windows) + // GLFWwindow* window = fromHwnd(hWindow); + const int32_t displayId = 0; - return toHmonitor(displayId); - } + return toHmonitor(displayId); + } - bool isWindow(HWND hWindow) { - GLFWwindow* window = fromHwnd(hWindow); - return window != nullptr; - } + bool isWindow(HWND hWindow) { + GLFWwindow* window = fromHwnd(hWindow); + return window != nullptr; + } - void updateFullscreenWindow( - HMONITOR hMonitor, - HWND hWindow, - bool forceTopmost) { - // Don't need to do anything with GLFW here. - } + void updateFullscreenWindow( + HMONITOR hMonitor, + HWND hWindow, + bool forceTopmost) { + // Don't need to do anything with GLFW here. + } - VkResult createSurface( - HWND hWindow, - PFN_vkGetInstanceProcAddr pfnVkGetInstanceProcAddr, - VkInstance instance, - VkSurfaceKHR* pSurface) { - GLFWwindow* window = fromHwnd(hWindow); + VkResult createSurface( + HWND hWindow, + PFN_vkGetInstanceProcAddr pfnVkGetInstanceProcAddr, + VkInstance instance, + VkSurfaceKHR* pSurface) { + GLFWwindow* window = fromHwnd(hWindow); - return glfwCreateWindowSurface(instance, window, nullptr, pSurface); - } + return glfwCreateWindowSurface(instance, window, nullptr, pSurface); + } } \ No newline at end of file From 118b2ecc3e1bc5f57f60ed17a023fe6f7f1b5be4 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Sat, 7 Jan 2023 15:11:01 -0800 Subject: [PATCH 8/8] Monitor mode bound checking and remove use of auto --- src/wsi/glfw/wsi_monitor_glfw.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/wsi/glfw/wsi_monitor_glfw.cpp b/src/wsi/glfw/wsi_monitor_glfw.cpp index 782d058453b..5379f656f8f 100644 --- a/src/wsi/glfw/wsi_monitor_glfw.cpp +++ b/src/wsi/glfw/wsi_monitor_glfw.cpp @@ -108,6 +108,9 @@ namespace dxvk::wsi { int32_t count = 0; const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count); + if(ModeNumber >= uint32_t(count)) + return false; + convertMode(modes[ModeNumber], pMode); return true; @@ -126,7 +129,7 @@ namespace dxvk::wsi { GLFWmonitor** monitors = glfwGetMonitors(&displayCount); GLFWmonitor* monitor = monitors[displayId]; - auto mode = glfwGetVideoMode(monitor); + const GLFWvidmode *mode = glfwGetVideoMode(monitor); convertMode(*mode, pMode);