Skip to content
Closed
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
22 changes: 22 additions & 0 deletions include/glfwutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <cstdlib>
#include <stdexcept>
#include <string>

Expand All @@ -18,6 +19,27 @@ namespace glfwutils {
* Must be called once before creating windows.
*/
static void initGLFW() {
// Default to X11 on Linux to avoid Wayland/libdecor ASAN leak noise.
// See issue #68: https://github.com/jamylak/vsdf/issues/68
// Summary: ASAN reports leaks from the Wayland decoration stack
// (libdecor/GTK/Pango/Fontconfig via GLFW) that persist until process exit.
// This is not related to render/present stalls; override with GLFW_PLATFORM
// if you explicitly want Wayland.
#if defined(__linux__)
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
#endif
const char *platform = std::getenv("GLFW_PLATFORM");
if (platform) {
if (std::string(platform) == "x11" || std::string(platform) == "X11") {
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
} else if (std::string(platform) == "wayland" || std::string(platform) == "Wayland") {
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND);
} else if (std::string(platform) == "any" || std::string(platform) == "ANY") {
glfwInitHint(GLFW_PLATFORM, GLFW_ANY_PLATFORM);
} else if (std::string(platform) == "null" || std::string(platform) == "NULL") {
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_NULL);
}
}
if (!glfwInit()) {
throw std::runtime_error("Failed to initialize GLFW");
}
Expand Down
110 changes: 110 additions & 0 deletions include/vkutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,53 @@ getDeviceProperties(VkPhysicalDevice physicalDevice) {
return deviceProperties;
}

[[nodiscard]] static bool deviceSupportsSwapchain(
VkPhysicalDevice physicalDevice) {
uint32_t extensionCount = 0;
VK_CHECK(vkEnumerateDeviceExtensionProperties(
physicalDevice, nullptr, &extensionCount, nullptr));
if (extensionCount == 0)
return false;

std::vector<VkExtensionProperties> extensions(extensionCount);
VK_CHECK(vkEnumerateDeviceExtensionProperties(
physicalDevice, nullptr, &extensionCount, extensions.data()));

for (const auto &ext : extensions) {
if (std::strcmp(ext.extensionName, "VK_KHR_swapchain") == 0)
return true;
}
return false;
}

[[nodiscard]] static bool deviceHasPresentQueue(
VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
uint32_t *outQueueIndex) {
uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount,
nullptr);
if (queueFamilyCount == 0)
return false;

std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount,
queueFamilies.data());

for (uint32_t i = 0; i < queueFamilyCount; ++i) {
if (!(queueFamilies[i].queueFlags & VK_QUEUE_GRAPHICS_BIT))
continue;
VkBool32 supportsPresent = VK_FALSE;
vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, i, surface,
&supportsPresent);
if (supportsPresent) {
if (outQueueIndex)
*outQueueIndex = i;
return true;
}
}
return false;
}

[[nodiscard]] static VkPhysicalDevice findGPU(VkInstance instance) {
uint32_t deviceCount = 0;
spdlog::debug("Enumerating devices...");
Expand Down Expand Up @@ -292,6 +339,69 @@ getDeviceProperties(VkPhysicalDevice physicalDevice) {
return devices[0];
}

[[nodiscard]] static VkPhysicalDevice
findGPUForSurface(VkInstance instance, VkSurfaceKHR surface,
uint32_t *outQueueIndex) {
uint32_t deviceCount = 0;
spdlog::debug("Enumerating devices for present-capable GPU...");
VK_CHECK(vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr));

if (deviceCount == 0) {
spdlog::error("No devices found!");
throw std::runtime_error("No devices found!");
}

std::vector<VkPhysicalDevice> devices(deviceCount);
VK_CHECK(
vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data()));

VkPhysicalDevice fallback = VK_NULL_HANDLE;
uint32_t fallbackQueueIndex = 0;
std::string fallbackName;

for (uint32_t i = 0; i < deviceCount; ++i) {
VkPhysicalDeviceProperties deviceProperties{};
vkGetPhysicalDeviceProperties(devices[i], &deviceProperties);
spdlog::debug("Device {} has device name {}", i,
deviceProperties.deviceName);

if (!deviceSupportsSwapchain(devices[i])) {
spdlog::debug("Device {} lacks VK_KHR_swapchain", i);
continue;
}

uint32_t presentQueueIndex = 0;
if (!deviceHasPresentQueue(devices[i], surface, &presentQueueIndex)) {
spdlog::debug("Device {} has no present-capable graphics queue", i);
continue;
}

if (deviceProperties.deviceType ==
VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) {
if (outQueueIndex)
*outQueueIndex = presentQueueIndex;
spdlog::info("Selecting discrete present-capable GPU: {}",
deviceProperties.deviceName);
return devices[i];
}

if (fallback == VK_NULL_HANDLE) {
fallback = devices[i];
fallbackQueueIndex = presentQueueIndex;
fallbackName = deviceProperties.deviceName;
}
}

if (fallback != VK_NULL_HANDLE) {
if (outQueueIndex)
*outQueueIndex = fallbackQueueIndex;
spdlog::info("Selecting present-capable GPU: {}", fallbackName);
return fallback;
}

throw std::runtime_error("No present-capable Vulkan device found");
}

[[nodiscard]] static VkSurfaceKHR createVulkanSurface(VkInstance instance,
GLFWwindow *window) {
spdlog::debug("Creating Vulkan surface...");
Expand Down
7 changes: 3 additions & 4 deletions src/online_sdf_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ void OnlineSDFRenderer::glfwSetup() {

void OnlineSDFRenderer::vulkanSetup() {
instance = vkutils::setupVulkanInstance();
physicalDevice = vkutils::findGPU(instance);
surface = vkutils::createVulkanSurface(instance, window);
physicalDevice =
vkutils::findGPUForSurface(instance, surface, &graphicsQueueIndex);
deviceProperties = vkutils::getDeviceProperties(physicalDevice);
logDeviceLimits();
surface = vkutils::createVulkanSurface(instance, window);
graphicsQueueIndex =
vkutils::getVulkanGraphicsQueueIndex(physicalDevice, surface);
logicalDevice =
vkutils::createVulkanLogicalDevice(physicalDevice, graphicsQueueIndex);
queue = VK_NULL_HANDLE;
Expand Down
Loading