Skip to content
This repository has been archived by the owner on Nov 5, 2022. It is now read-only.

Add GLFW WSI #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions include/native/wsi/native_glfw.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <cstdint>
#include <windows.h>

#include <GLFW/glfw3.h>

namespace dxvk::wsi {

inline GLFWwindow* fromHwnd(HWND hWindow) {
return reinterpret_cast<GLFWwindow*>(hWindow);
}

inline HWND toHwnd(GLFWwindow* pWindow) {
return reinterpret_cast<HWND>(pWindow);
}

// Offset so null HMONITORs go to -1
inline int32_t fromHmonitor(HMONITOR hMonitor) {
return static_cast<int32_t>(reinterpret_cast<intptr_t>(hMonitor)) - 1;
}

// Offset so -1 display id goes to 0 == NULL
inline HMONITOR toHmonitor(int32_t displayId) {
return reinterpret_cast<HMONITOR>(static_cast<intptr_t>(displayId + 1));
}

}
2 changes: 2 additions & 0 deletions include/native/wsi/native_wsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 10 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,20 @@ else
if dxvk_platform == 'windows'
dxvk_include_path = include_directories('./include', './include/native/')
lib_vulkan = dxvk_compiler.find_library('vulkan-1', dirs : dxvk_library_path)
lib_sdl2 = dxvk_compiler.find_library('SDL2', dirs : dxvk_library_path)
if dxvk_wsi == 'sdl2'
lib_sdl2 = dxvk_compiler.find_library('SDL2', dirs : dxvk_library_path)
elif dxvk_wsi == 'glfw'
lib_glfw = dxvk_compiler.find_library('glfw', dirs : dxvk_library_path)
endif
wrc = find_program('rc')
else
dxvk_include_path = include_directories('./include', './include/native/', './include/native/windows', './include/native/directx')
lib_vulkan = dxvk_compiler.find_library('vulkan')
lib_sdl2 = dxvk_compiler.find_library('SDL2')
if dxvk_wsi == 'sdl2'
lib_sdl2 = dxvk_compiler.find_library('SDL2')
elif dxvk_wsi == 'glfw'
lib_glfw = dxvk_compiler.find_library('glfw')
endif
wrc = find_program('echo')
so_prefix = 'libdxvk_'
endif
Expand Down
6 changes: 6 additions & 0 deletions src/dxvk/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,16 @@ dxvk_src_sdl2 = [
'platform/dxvk_sdl2_exts.cpp'
]

dxvk_src_glfw = [
'platform/dxvk_glfw_exts.cpp'
]

if dxvk_wsi == 'win32'
dxvk_src += dxvk_src_win32
elif dxvk_wsi == 'sdl2'
dxvk_src += dxvk_src_sdl2
elif dxvk_wsi == 'glfw'
dxvk_src += dxvk_src_glfw
else
error('Unknown platform for dxvk')
endif
Expand Down
57 changes: 57 additions & 0 deletions src/dxvk/platform/dxvk_glfw_exts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "../dxvk_platform_exts.h"

#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>

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 (int 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) {

}

}
10 changes: 6 additions & 4 deletions src/vulkan/vulkan_presenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ namespace dxvk::vk {
}
#endif

if (createSurface() != VK_SUCCESS)
throw DxvkError("Failed to create surface");
auto result = createSurface();
if (result != VK_SUCCESS)
throw DxvkError(str::format("Failed to create surface. Result: ", result));

if (recreateSwapChain(desc) != VK_SUCCESS)
throw DxvkError("Failed to create swap chain");
result = recreateSwapChain(desc);
if (result != VK_SUCCESS)
throw DxvkError(str::format("Failed to create swap chain. Result: ", result));
}


Expand Down
16 changes: 16 additions & 0 deletions src/wsi/glfw/wsi_helpers_glfw.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <GLFW/glfw3.h>

#include "../wsi_monitor.h"

namespace dxvk {

inline bool isDisplayValid(int32_t displayId) {
int32_t displayCount = 0;
glfwGetMonitors(&displayCount);

return displayId < displayCount && displayId >= 0;
}

}
94 changes: 94 additions & 0 deletions src/wsi/glfw/wsi_mode_glfw.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "../wsi_mode.h"

#include "wsi_helpers_glfw.h"

#include <wsi/native_wsi.h>

#include "../../util/util_string.h"
#include "../../util/log/log.h"

namespace dxvk::wsi {

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;
}

}
70 changes: 70 additions & 0 deletions src/wsi/glfw/wsi_monitor_glfw.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "../wsi_monitor.h"

#include "wsi_helpers_glfw.h"

#include <windows.h>
#include <wsi/native_wsi.h>

#include <string>
#include <sstream>

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;
}

}
18 changes: 18 additions & 0 deletions src/wsi/glfw/wsi_presenter_glfw.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "../wsi_presenter.h"

#include <GLFW/glfw3.h>

#include <wsi/native_wsi.h>

namespace dxvk::wsi {

VkResult createSurface(
HWND hWindow,
const Rc<vk::InstanceFn>& vki,
VkSurfaceKHR* pSurface) {
GLFWwindow* window = fromHwnd(hWindow);

return glfwCreateWindowSurface(vki->instance(), window, nullptr, pSurface);
}

}
Loading