From bb905521df5fcaa76e02cc91efce1a8b2adb925c Mon Sep 17 00:00:00 2001 From: BearOso Date: Fri, 19 Apr 2024 16:24:54 -0500 Subject: [PATCH] Vulkan: Simplify some code. --- gtk/src/gtk_display_driver_vulkan.h | 1 - qt/src/EmuApplication.cpp | 13 ++--- qt/src/EmuCanvasVulkan.cpp | 2 - qt/src/EmuCanvasVulkan.hpp | 1 - vulkan/vulkan_context.cpp | 86 +++++++++++++++-------------- vulkan/vulkan_context.hpp | 4 +- 6 files changed, 50 insertions(+), 57 deletions(-) diff --git a/gtk/src/gtk_display_driver_vulkan.h b/gtk/src/gtk_display_driver_vulkan.h index fa0e72a6..dcc6567a 100644 --- a/gtk/src/gtk_display_driver_vulkan.h +++ b/gtk/src/gtk_display_driver_vulkan.h @@ -40,7 +40,6 @@ class S9xVulkanDisplayDriver : public S9xDisplayDriver std::unique_ptr context; vk::Device device; vk::UniqueDescriptorPool imgui_descriptor_pool; - vk::UniqueRenderPass imgui_render_pass; GdkDisplay *gdk_display; GdkWindow *gdk_window; diff --git a/qt/src/EmuApplication.cpp b/qt/src/EmuApplication.cpp index 915bfd0c..6f90debf 100644 --- a/qt/src/EmuApplication.cpp +++ b/qt/src/EmuApplication.cpp @@ -13,8 +13,6 @@ #include #include #include -#include -using namespace std::chrono_literals; #undef SOUND_BUFFER_WINDOW @@ -65,7 +63,11 @@ void EmuApplication::restartAudio() } #ifdef SOUND_BUFFER_WINDOW + #include +#include +using namespace std::chrono_literals; + static void trackBufferLevel(int percent, QWidget *parent) { static uint64_t total = 0; @@ -139,13 +141,6 @@ void EmuApplication::startGame() if (window->canvas) { window->output((uint8_t *)data, width, height, QImage::Format_RGB16, stride_bytes, frame_rate); - // QMetaObject::invokeMethod(window.get(), "output", Qt::ConnectionType::DirectConnection, - // Q_ARG(uint8_t *, (uint8_t *)data), - // Q_ARG(int, width), - // Q_ARG(int, height), - // Q_ARG(QImage::Format, QImage::Format_RGB16), - // Q_ARG(int, stride_bytes), - // Q_ARG(double, frame_rate)); } }; diff --git a/qt/src/EmuCanvasVulkan.cpp b/qt/src/EmuCanvasVulkan.cpp index f4e2d848..4da80e0f 100644 --- a/qt/src/EmuCanvasVulkan.cpp +++ b/qt/src/EmuCanvasVulkan.cpp @@ -277,7 +277,6 @@ void EmuCanvasVulkan::deinit() if (context) context->wait_idle(); imgui_descriptor_pool.reset(); - imgui_render_pass.reset(); ImGui_ImplVulkan_Shutdown(); ImGui::DestroyContext(); } @@ -339,7 +338,6 @@ void EmuCanvasVulkan::recreateUIAssets() { context->wait_idle(); imgui_descriptor_pool.reset(); - imgui_render_pass.reset(); ImGui_ImplVulkan_Shutdown(); ImGui::DestroyContext(); } diff --git a/qt/src/EmuCanvasVulkan.hpp b/qt/src/EmuCanvasVulkan.hpp index 20afb375..704daf86 100644 --- a/qt/src/EmuCanvasVulkan.hpp +++ b/qt/src/EmuCanvasVulkan.hpp @@ -31,7 +31,6 @@ class EmuCanvasVulkan : public EmuCanvas bool initImGui(); void recreateUIAssets() override; - vk::UniqueRenderPass imgui_render_pass; vk::UniqueDescriptorPool imgui_descriptor_pool; std::unique_ptr context; diff --git a/vulkan/vulkan_context.cpp b/vulkan/vulkan_context.cpp index 68b2ec5f..e17cd530 100644 --- a/vulkan/vulkan_context.cpp +++ b/vulkan/vulkan_context.cpp @@ -1,9 +1,7 @@ #include -#include #include #include #include "vulkan_context.hpp" -#include "slang_shader.hpp" namespace Vulkan { @@ -18,18 +16,14 @@ Context::~Context() { if (!device) return; + wait_idle(); - - swapchain = nullptr; - + swapchain.reset(); command_pool.reset(); descriptor_pool.reset(); - allocator.destroy(); - surface.reset(); wait_idle(); - device.destroy(); } @@ -58,8 +52,11 @@ static vk::UniqueInstance create_instance_preamble(const char *wsi_extension) if (!dl || !dl->success()) return {}; - std::vector extensions = { wsi_extension, VK_KHR_SURFACE_EXTENSION_NAME }; - vk::ApplicationInfo application_info({}, {}, {}, {}, VK_API_VERSION_1_0); + std::vector extensions = { + wsi_extension, + VK_KHR_SURFACE_EXTENSION_NAME + }; + vk::ApplicationInfo application_info({}, {}, {}, {}, VK_API_VERSION_1_1); vk::InstanceCreateInfo instance_create_info({}, &application_info, {}, extensions); auto [result, instance] = vk::createInstanceUnique(instance_create_info); @@ -118,7 +115,7 @@ bool Context::init_Xlib(Display *dpy, Window xid, int preferred_device) instance = create_instance_preamble(VK_KHR_XLIB_SURFACE_EXTENSION_NAME); if (!instance) return false; - + auto retval = instance->createXlibSurfaceKHRUnique({ {}, dpy, xid }); if (retval.result != vk::Result::eSuccess) return false; @@ -144,24 +141,18 @@ bool Context::init_wayland(wl_display *dpy, wl_surface *parent, int initial_widt return false; surface = std::move(new_surface); - init_device(preferred_device); - init_vma(); - init_command_pool(); - init_descriptor_pool(); - create_swapchain(initial_width, initial_height); - wait_idle(); - return true; + return init(preferred_device, initial_width, initial_height) } #endif -bool Context::init(int preferred_device) +bool Context::init(int preferred_device, int initial_width, int initial_height) { init_device(preferred_device); init_vma(); init_command_pool(); init_descriptor_pool(); - create_swapchain(); + create_swapchain(initial_width, initial_height); wait_idle(); return true; } @@ -175,7 +166,7 @@ bool Context::init_descriptor_pool() .setPoolSizes(descriptor_pool_size) .setMaxSets(20) .setFlags(vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet); - + auto retval = device.createDescriptorPoolUnique(descriptor_pool_create_info); descriptor_pool = std::move(retval.value); @@ -194,49 +185,62 @@ bool Context::init_command_pool() bool Context::init_device(int preferred_device) { - auto device_list = instance->enumeratePhysicalDevices().value; - - auto find_device = [&]() -> vk::PhysicalDevice { - for (auto &d : device_list) - { - auto [retval, ep] = d.enumerateDeviceExtensionProperties(); - auto exists = std::find_if(ep.begin(), ep.end(), [](vk::ExtensionProperties &ext) { - return (std::string(ext.extensionName.data()) == VK_KHR_SWAPCHAIN_EXTENSION_NAME); + const char *required_extensions[] = { + VK_KHR_SWAPCHAIN_EXTENSION_NAME, + // VK_KHR_DYNAMIC_RENDERING_EXTENSION_NAME + }; + auto check_extensions = [&](vk::PhysicalDevice &device) -> bool { + auto [retval, props] = device.enumerateDeviceExtensionProperties(); + for (const auto &extension : required_extensions) { + auto found = std::find_if( + props.begin(), props.end(), [&](vk::ExtensionProperties &ext) { + return (std::string(ext.extensionName.data()) == extension); }); - - if (exists != ep.end()) - return d; + return found != props.end(); } - return device_list[0]; + return true; }; - if (preferred_device > -1 && (size_t)preferred_device < device_list.size()) + auto device_list = instance->enumeratePhysicalDevices().value; + + if (preferred_device > -1 && + preferred_device < device_list.size() && + check_extensions(device_list[preferred_device])) + { physical_device = device_list[preferred_device]; + } else - physical_device = find_device(); + { + for (auto &device : device_list) + if (check_extensions(device)) + { + physical_device = device; + break; + } + } physical_device.getProperties(&physical_device_props); - printf("Vulkan: Using device \"%s\"\n", (const char *)physical_device_props.deviceName); graphics_queue_family_index = UINT32_MAX; auto queue_props = physical_device.getQueueFamilyProperties(); for (size_t i = 0; i < queue_props.size(); i++) { if (queue_props[i].queueFlags & vk::QueueFlagBits::eGraphics) + { graphics_queue_family_index = i; + break; + } } if (graphics_queue_family_index == UINT32_MAX) return false; - std::vector extension_names = { VK_KHR_SWAPCHAIN_EXTENSION_NAME }; - std::vector priorities { 1.0f }; - vk::DeviceQueueCreateInfo dqci({}, graphics_queue_family_index, priorities); - vk::DeviceCreateInfo dci({}, dqci, {}, extension_names, {}); + vk::DeviceQueueCreateInfo dqci({}, graphics_queue_family_index, 1); + vk::DeviceCreateInfo dci({}, dqci, {}, required_extensions, {}); device = physical_device.createDevice(dci).value; queue = device.getQueue(graphics_queue_family_index, 0); - auto [retval, surface_formats] = physical_device.getSurfaceFormatsKHR(surface.get()); + auto surface_formats = physical_device.getSurfaceFormatsKHR(surface.get()).value; auto format = std::find_if(surface_formats.begin(), surface_formats.end(), [](vk::SurfaceFormatKHR &f) { return (f.format == vk::Format::eB8G8R8A8Unorm); }); diff --git a/vulkan/vulkan_context.hpp b/vulkan/vulkan_context.hpp index e9ace37c..8ebe4b83 100644 --- a/vulkan/vulkan_context.hpp +++ b/vulkan/vulkan_context.hpp @@ -7,13 +7,11 @@ #undef WINVER #define WINVER 0x599 #endif -#include #include #include "vulkan/vulkan_hpp_wrapper.hpp" #include "../external/VulkanMemoryAllocator-Hpp/include/vk_mem_alloc.hpp" #include "vulkan_swapchain.hpp" #include -#include namespace Vulkan { @@ -32,7 +30,7 @@ class Context #ifdef VK_USE_PLATFORM_WIN32_KHR bool init_win32(HINSTANCE hinstance, HWND hwnd, int preferred_device = -1); #endif - bool init(int preferred_device = -1); + bool init(int preferred_device = -1, int initial_width = -1, int initial_height = -1); bool create_swapchain(int width = -1, int height = -1); bool recreate_swapchain(int width = -1, int height = -1); void wait_idle();