aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorladevieq <ladevie.quentin@gmail.com>2021-01-26 23:12:49 +0100
committerladevieq <ladevie.quentin@gmail.com>2021-01-26 23:12:49 +0100
commit1314f696a7861f3b1509c7f5c27d6b06bc22d5f1 (patch)
treedc438e1fe4f9c5503f984c00cac190a2cdb6268f
parentd3b08c7e8bf808281e32fdef5e5222fb6633a5d0 (diff)
Split vkinstance/vkdevice of renderer into context class
-rw-r--r--include/vk-context.hpp40
-rw-r--r--include/vk-renderer.hpp43
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/main.cpp3
-rw-r--r--src/vk-context.cpp178
-rw-r--r--src/vk-renderer.cpp288
6 files changed, 285 insertions, 268 deletions
diff --git a/include/vk-context.hpp b/include/vk-context.hpp
new file mode 100644
index 0000000..405cd83
--- /dev/null
+++ b/include/vk-context.hpp
@@ -0,0 +1,40 @@
+#ifndef __VK_CONTEXT_HPP_
+#define __VK_CONTEXT_HPP_
+
+#include "vulkan-loader.hpp"
+#include "thirdparty/vk_mem_alloc.h"
+
+class vkcontext {
+ public:
+ vkcontext();
+
+ ~vkcontext();
+
+ VkInstance instance;
+
+ VkPhysicalDevice physical_device;
+ VkDevice device;
+
+ VmaAllocator allocator;
+
+ uint32_t queue_index;
+ VkQueue queue;
+
+ private:
+ void create_instance();
+
+ void create_debugger();
+
+ void create_device();
+
+ void create_memory_allocator();
+
+ void select_physical_device();
+
+ void select_queue();
+
+
+ VkDebugUtilsMessengerEXT debugMessenger;
+};
+
+#endif // !__VK_CONTEXT_HPP_
diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp
index 5f70718..c13f029 100644
--- a/include/vk-renderer.hpp
+++ b/include/vk-renderer.hpp
@@ -3,12 +3,11 @@
#include <vector>
+#include "vk-context.hpp"
#include "vulkan-loader.hpp"
-#include "thirdparty/vk_mem_alloc.h"
-
-#include "window.hpp"
class input_data;
+class window;
class vkrenderer {
public:
@@ -16,27 +15,19 @@ class vkrenderer {
~vkrenderer();
- void compute();
+ void compute(uint32_t width, uint32_t height);
void recreate_swapchain();
struct input_data* mapped_data;
private:
- void create_instance();
-
- void create_debugger();
-
- void create_surface();
-
- void create_device();
+ void create_surface(window& wnd);
void create_swapchain();
void create_pipeline();
- void create_memory_allocator();
-
void create_command_buffers();
void create_descriptor_sets();
@@ -47,14 +38,8 @@ class vkrenderer {
void create_input_buffer(const input_data& inputs);
- void destroy_instance();
-
- void destroy_debugger();
-
void destroy_surface();
- void destroy_device();
-
void destroy_swapchain();
void destroy_swapchain_images();
@@ -63,8 +48,6 @@ class vkrenderer {
void destroy_command_buffers();
- void destroy_allocator();
-
void destroy_descriptor_sets();
void destroy_fences();
@@ -73,28 +56,16 @@ class vkrenderer {
void destroy_input_buffer();
- void select_physical_device();
-
- void select_compute_queue();
-
void update_descriptors_buffer();
void update_descriptors_image();
void handle_swapchain_result(VkResult function_result);
- uint32_t compute_queue_index;
- uint64_t frame_index = 0;
-
- VkInstance instance;
-
- VkDebugUtilsMessengerEXT debugMessenger;
+ vkcontext context;
- VkPhysicalDevice physical_device;
- VkDevice device;
- VkQueue compute_queue;
+ uint64_t frame_index = 0;
- VmaAllocator allocator;
VmaAllocation allocation;
VkBuffer compute_shader_buffer;
@@ -120,8 +91,6 @@ class vkrenderer {
std::vector<VkImage> swapchain_images;
std::vector<VkImageView> swapchain_images_views;
uint32_t current_image_index;
-
- window& wnd;
};
#endif // !__VK_RENDERER_HPP_
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a270ef0..1af1748 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -17,6 +17,7 @@ add_executable(
vulkan-loader.cpp
utils.cpp
vk-renderer.cpp
+ vk-context.cpp
window.cpp
)
diff --git a/src/main.cpp b/src/main.cpp
index a91e9e5..24d4c53 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -7,6 +7,7 @@
#include "thirdparty/renderdoc.h"
#include "vk-renderer.hpp"
+#include "window.hpp"
#include "utils.hpp"
#include "defines.hpp"
@@ -126,7 +127,7 @@ int main() {
if(rdoc_api) rdoc_api->StartFrameCapture(NULL, NULL);
if (canRender) {
- renderer.compute();
+ renderer.compute(width, height);
}
// stop the capture
diff --git a/src/vk-context.cpp b/src/vk-context.cpp
new file mode 100644
index 0000000..cbf2777
--- /dev/null
+++ b/src/vk-context.cpp
@@ -0,0 +1,178 @@
+#include <vector>
+#include <iostream>
+#include <cassert>
+
+#include "vk-context.hpp"
+
+#define VMA_IMPLEMENTATION
+#include "thirdparty/vk_mem_alloc.h"
+
+#define VKRESULT(result) assert(result == VK_SUCCESS);
+
+VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT severity,
+ VkDebugUtilsMessageTypeFlagsEXT type,
+ const VkDebugUtilsMessengerCallbackDataEXT *data,
+ void *userData)
+{
+ std::cerr << data->pMessage << std::endl;
+ return VK_FALSE;
+}
+
+
+vkcontext::vkcontext() {
+ load_vulkan();
+
+ create_instance();
+ create_device();
+}
+
+vkcontext::~vkcontext() {
+ vmaDestroyAllocator(allocator);
+ vkDestroyDevice(device, nullptr);
+ vkDestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
+ vkDestroyInstance(instance, nullptr);
+}
+
+
+void vkcontext::create_instance() {
+ VkApplicationInfo app_info = {};
+ app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
+ app_info.pApplicationName = "path-tracer";
+ app_info.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
+ app_info.pEngineName = "gpu-path-tracer";
+ app_info.engineVersion = VK_MAKE_VERSION(1, 0, 0);
+ app_info.apiVersion = VK_API_VERSION_1_2;
+
+
+ std::vector<const char*> layers = {
+ "VK_LAYER_KHRONOS_validation",
+ };
+ std::vector<const char*> extensions = {
+ VK_EXT_DEBUG_UTILS_EXTENSION_NAME,
+ "VK_KHR_surface",
+#if defined(LINUX)
+ "VK_KHR_xcb_surface"
+#elif defined(WINDOWS)
+ "VK_KHR_win32_surface"
+#endif
+ };
+
+ VkInstanceCreateInfo instance_create_info = {};
+ instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
+ instance_create_info.pApplicationInfo = &app_info;
+ instance_create_info.enabledLayerCount = layers.size();
+ instance_create_info.ppEnabledLayerNames = layers.data();
+ instance_create_info.enabledExtensionCount = extensions.size();
+ instance_create_info.ppEnabledExtensionNames = extensions.data();
+
+ VKRESULT(vkCreateInstance(&instance_create_info, nullptr, &instance))
+
+ load_instance_functions(instance);
+
+ create_debugger();
+
+ std::cerr << "Instance ready" << std::endl;
+}
+
+void vkcontext::create_debugger() {
+ VkDebugUtilsMessengerCreateInfoEXT debugUtilsMessengeCreateInfo {
+ VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
+ nullptr,
+ 0,
+ VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT,
+ VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT
+ | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT,
+ DebugCallback,
+ nullptr,
+ };
+
+ VKRESULT(vkCreateDebugUtilsMessengerEXT(
+ instance,
+ &debugUtilsMessengeCreateInfo,
+ nullptr,
+ &debugMessenger
+ ))
+}
+
+void vkcontext::create_device() {
+ select_physical_device();
+ select_queue();
+
+ VkDeviceQueueCreateInfo queue_create_info = {};
+ queue_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
+ queue_create_info.queueFamilyIndex = queue_index;
+ queue_create_info.queueCount = 1;
+
+ float queue_priority = 1.f;
+ queue_create_info.pQueuePriorities = &queue_priority;
+
+ VkPhysicalDeviceFeatures device_features = {};
+
+ std::vector<const char*> device_ext = {
+ "VK_KHR_swapchain"
+ };
+
+ VkDeviceCreateInfo device_create_info = {};
+ device_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
+ device_create_info.pQueueCreateInfos = &queue_create_info;
+ device_create_info.queueCreateInfoCount = 1;
+ device_create_info.pEnabledFeatures = &device_features;
+ device_create_info.ppEnabledExtensionNames = device_ext.data();
+ device_create_info.enabledExtensionCount = device_ext.size();
+ device_create_info.enabledLayerCount = 0; // Deprecated https://www.khronos.org/registry/vulkan/specs/1.2/html/chap31.html#extendingvulkan-layers-devicelayerdeprecation
+
+ VKRESULT(vkCreateDevice(physical_device, &device_create_info, nullptr, &device))
+
+ load_device_functions(device);
+
+ vkGetDeviceQueue(device, queue_index, 0, &queue);
+
+ create_memory_allocator();
+
+ std::cerr << "Device ready" << std::endl;
+}
+
+void vkcontext::create_memory_allocator() {
+ VmaAllocatorCreateInfo allocator_info = {};
+ allocator_info.vulkanApiVersion = VK_API_VERSION_1_2;
+ allocator_info.physicalDevice = physical_device;
+ allocator_info.device = device;
+ allocator_info.instance = instance;
+
+ vmaCreateAllocator(&allocator_info, &allocator);
+}
+
+
+void vkcontext::select_physical_device() {
+ uint32_t physical_devices_count = 0;
+ vkEnumeratePhysicalDevices(instance, &physical_devices_count, nullptr);
+
+ if (physical_devices_count < 1) {
+ std::cerr << "No physical device found !" << std::endl;
+ exit(1);
+ }
+
+ std::vector<VkPhysicalDevice> physical_devices { physical_devices_count };
+ vkEnumeratePhysicalDevices(instance, &physical_devices_count, physical_devices.data());
+
+ physical_device = physical_devices[0];
+}
+
+void vkcontext::select_queue() {
+ uint32_t queue_properties_count = 0;
+ vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &queue_properties_count, nullptr);
+
+ std::vector<VkQueueFamilyProperties> queue_properties { queue_properties_count };
+ vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &queue_properties_count, queue_properties.data());
+
+ for (uint32_t properties_index = 0; properties_index < queue_properties_count; properties_index++) {
+ if (queue_properties[properties_index].queueFlags & VK_QUEUE_COMPUTE_BIT) {
+ queue_index = properties_index;
+ return;
+ }
+ }
+
+ std::cerr << "No compute queue found !" << std::endl;
+ exit(1);
+}
+
diff --git a/src/vk-renderer.cpp b/src/vk-renderer.cpp
index e3eca75..5ac870f 100644
--- a/src/vk-renderer.cpp
+++ b/src/vk-renderer.cpp
@@ -1,34 +1,21 @@
#include <iostream>
#include <cstring>
+#include <cassert>
#include "vk-renderer.hpp"
-#define VMA_IMPLEMENTATION
#include "thirdparty/vk_mem_alloc.h"
#include "defines.hpp"
#include "utils.hpp"
+#include "window.hpp"
#define VKRESULT(result) assert(result == VK_SUCCESS);
-VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT severity,
- VkDebugUtilsMessageTypeFlagsEXT type,
- const VkDebugUtilsMessengerCallbackDataEXT *data,
- void *userData)
-{
- std::cerr << data->pMessage << std::endl;
- return VK_FALSE;
-}
-
-vkrenderer::vkrenderer(window& wnd, const input_data& inputs)
- : wnd(wnd) {
- load_vulkan();
+vkrenderer::vkrenderer(window& wnd, const input_data& inputs) {
- create_instance();
- create_surface();
- create_device();
+ create_surface(wnd);
create_pipeline();
create_swapchain();
- create_memory_allocator();
create_command_buffers();
create_descriptor_sets();
create_fences();
@@ -40,27 +27,23 @@ vkrenderer::vkrenderer(window& wnd, const input_data& inputs)
}
vkrenderer::~vkrenderer() {
- VKRESULT(vkWaitForFences(device, 1, &submission_fences[frame_index], VK_TRUE, UINT64_MAX))
+ VKRESULT(vkWaitForFences(context.device, 1, &submission_fences[frame_index], VK_TRUE, UINT64_MAX))
destroy_input_buffer();
destroy_fences();
destroy_semaphores();
destroy_descriptor_sets();
destroy_command_buffers();
- destroy_allocator();
destroy_pipeline();
destroy_swapchain();
- destroy_device();
destroy_surface();
- destroy_debugger();
- destroy_instance();
}
-void vkrenderer::compute() {
- VKRESULT(vkWaitForFences(device, 1, &submission_fences[frame_index], VK_TRUE, UINT64_MAX))
- VKRESULT(vkResetFences(device, 1, &submission_fences[frame_index]))
+void vkrenderer::compute(uint32_t width, uint32_t height) {
+ VKRESULT(vkWaitForFences(context.device, 1, &submission_fences[frame_index], VK_TRUE, UINT64_MAX))
+ VKRESULT(vkResetFences(context.device, 1, &submission_fences[frame_index]))
- auto acquire_result = vkAcquireNextImageKHR(device, swapchain, UINT64_MAX, acquire_semaphores[frame_index], VK_NULL_HANDLE, &current_image_index);
+ auto acquire_result = vkAcquireNextImageKHR(context.device, swapchain, UINT64_MAX, acquire_semaphores[frame_index], VK_NULL_HANDLE, &current_image_index);
handle_swapchain_result(acquire_result);
VkCommandBufferBeginInfo cmd_buf_begin_info = {};
@@ -107,7 +90,7 @@ void vkrenderer::compute() {
vkCmdBindPipeline(command_buffers[frame_index], VK_PIPELINE_BIND_POINT_COMPUTE, compute_pipeline);
- vkCmdDispatch(command_buffers[frame_index], wnd.width / 8, wnd.height / 8, 1);
+ vkCmdDispatch(command_buffers[frame_index], width / 8, height / 8, 1);
VkImageMemoryBarrier general_to_present_barrier = {};
general_to_present_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
@@ -149,7 +132,7 @@ void vkrenderer::compute() {
submit_info.commandBufferCount = 1;
submit_info.pCommandBuffers = &command_buffers[frame_index];
- VKRESULT(vkQueueSubmit(compute_queue, 1, &submit_info, submission_fences[frame_index]))
+ VKRESULT(vkQueueSubmit(context.queue, 1, &submit_info, submission_fences[frame_index]))
VkPresentInfoKHR present_info = {};
present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
@@ -161,16 +144,16 @@ void vkrenderer::compute() {
present_info.pImageIndices = &current_image_index;
present_info.pResults = nullptr;
- auto present_result = vkQueuePresentKHR(compute_queue, &present_info);
+ auto present_result = vkQueuePresentKHR(context.queue, &present_info);
handle_swapchain_result(present_result);
- VKRESULT(vkWaitForFences(device, 1, &submission_fences[frame_index], VK_TRUE, UINT64_MAX))
+ VKRESULT(vkWaitForFences(context.device, 1, &submission_fences[frame_index], VK_TRUE, UINT64_MAX))
frame_index = ++frame_index % swapchain_images_count;
}
void vkrenderer::recreate_swapchain() {
- VKRESULT(vkWaitForFences(device, 1, &submission_fences[frame_index], VK_TRUE, UINT64_MAX))
+ VKRESULT(vkWaitForFences(context.device, 1, &submission_fences[frame_index], VK_TRUE, UINT64_MAX))
destroy_swapchain_images();
@@ -180,67 +163,7 @@ void vkrenderer::recreate_swapchain() {
}
-void vkrenderer::create_instance() {
- VkApplicationInfo app_info = {};
- app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
- app_info.pApplicationName = "path-tracer";
- app_info.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
- app_info.pEngineName = "gpu-path-tracer";
- app_info.engineVersion = VK_MAKE_VERSION(1, 0, 0);
- app_info.apiVersion = VK_API_VERSION_1_2;
-
-
- std::vector<const char*> layers = {
- "VK_LAYER_KHRONOS_validation",
- };
- std::vector<const char*> extensions = {
- VK_EXT_DEBUG_UTILS_EXTENSION_NAME,
- "VK_KHR_surface",
-#if defined(LINUX)
- "VK_KHR_xcb_surface"
-#elif defined(WINDOWS)
- "VK_KHR_win32_surface"
-#endif
- };
-
- VkInstanceCreateInfo instance_create_info = {};
- instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
- instance_create_info.pApplicationInfo = &app_info;
- instance_create_info.enabledLayerCount = layers.size();
- instance_create_info.ppEnabledLayerNames = layers.data();
- instance_create_info.enabledExtensionCount = extensions.size();
- instance_create_info.ppEnabledExtensionNames = extensions.data();
-
- VKRESULT(vkCreateInstance(&instance_create_info, nullptr, &instance))
-
- load_instance_functions(instance);
-
- create_debugger();
-
- std::cerr << "Instance ready" << std::endl;
-}
-
-void vkrenderer::create_debugger() {
- VkDebugUtilsMessengerCreateInfoEXT debugUtilsMessengeCreateInfo {
- VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
- nullptr,
- 0,
- VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT,
- VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT
- | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT,
- DebugCallback,
- nullptr,
- };
-
- VKRESULT(vkCreateDebugUtilsMessengerEXT(
- instance,
- &debugUtilsMessengeCreateInfo,
- nullptr,
- &debugMessenger
- ))
-}
-
-void vkrenderer::create_surface() {
+void vkrenderer::create_surface(window& wnd) {
#if defined(LINUX)
VkXcbSurfaceCreateInfoKHR create_info = {};
create_info.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
@@ -249,7 +172,7 @@ void vkrenderer::create_surface() {
create_info.connection = wnd.connection;
create_info.window = wnd.win;
- VKRESULT(vkCreateXcbSurfaceKHR(instance, &create_info, nullptr, &platform_surface))
+ VKRESULT(vkCreateXcbSurfaceKHR(context.instance, &create_info, nullptr, &platform_surface))
#elif defined(WINDOWS)
VkWin32SurfaceCreateInfoKHR create_info = {};
create_info.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
@@ -262,48 +185,12 @@ void vkrenderer::create_surface() {
#endif
}
-void vkrenderer::create_device() {
- select_physical_device();
- select_compute_queue();
-
- VkDeviceQueueCreateInfo queue_create_info = {};
- queue_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
- queue_create_info.queueFamilyIndex = compute_queue_index;
- queue_create_info.queueCount = 1;
-
- float queue_priority = 1.f;
- queue_create_info.pQueuePriorities = &queue_priority;
-
- VkPhysicalDeviceFeatures device_features = {};
-
- std::vector<const char*> device_ext = {
- "VK_KHR_swapchain"
- };
-
- VkDeviceCreateInfo device_create_info = {};
- device_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
- device_create_info.pQueueCreateInfos = &queue_create_info;
- device_create_info.queueCreateInfoCount = 1;
- device_create_info.pEnabledFeatures = &device_features;
- device_create_info.ppEnabledExtensionNames = device_ext.data();
- device_create_info.enabledExtensionCount = device_ext.size();
- device_create_info.enabledLayerCount = 0; // Deprecated https://www.khronos.org/registry/vulkan/specs/1.2/html/chap31.html#extendingvulkan-layers-devicelayerdeprecation
-
- VKRESULT(vkCreateDevice(physical_device, &device_create_info, nullptr, &device))
-
- load_device_functions(device);
-
- vkGetDeviceQueue(device, compute_queue_index, 0, &compute_queue);
-
- std::cerr << "Device ready" << std::endl;
-}
-
void vkrenderer::create_swapchain() {
VkBool32 queue_support_presentation = VK_FALSE;
- VKRESULT(vkGetPhysicalDeviceSurfaceSupportKHR(physical_device, compute_queue_index, platform_surface, &queue_support_presentation))
+ VKRESULT(vkGetPhysicalDeviceSurfaceSupportKHR(context.physical_device, context.queue_index, platform_surface, &queue_support_presentation))
VkSurfaceCapabilitiesKHR caps;
- VKRESULT(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, platform_surface, &caps))
+ VKRESULT(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(context.physical_device, platform_surface, &caps))
VkExtent2D wantedExtent = caps.currentExtent;
@@ -320,16 +207,16 @@ void vkrenderer::create_swapchain() {
}
uint32_t supported_formats_count = 0;
- VKRESULT(vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, platform_surface, &supported_formats_count, VK_NULL_HANDLE))
+ VKRESULT(vkGetPhysicalDeviceSurfaceFormatsKHR(context.physical_device, platform_surface, &supported_formats_count, VK_NULL_HANDLE))
std::vector<VkSurfaceFormatKHR> supported_formats { supported_formats_count };
- VKRESULT(vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, platform_surface, &supported_formats_count, supported_formats.data()))
+ VKRESULT(vkGetPhysicalDeviceSurfaceFormatsKHR(context.physical_device, platform_surface, &supported_formats_count, supported_formats.data()))
uint32_t supported_present_modes_count = 0;
- VKRESULT(vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, platform_surface, &supported_present_modes_count, VK_NULL_HANDLE))
+ VKRESULT(vkGetPhysicalDeviceSurfacePresentModesKHR(context.physical_device, platform_surface, &supported_present_modes_count, VK_NULL_HANDLE))
std::vector<VkPresentModeKHR> supported_present_modes { supported_present_modes_count };
- VKRESULT(vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, platform_surface, &supported_present_modes_count, supported_present_modes.data()))
+ VKRESULT(vkGetPhysicalDeviceSurfacePresentModesKHR(context.physical_device, platform_surface, &supported_present_modes_count, supported_present_modes.data()))
surface_format = supported_formats[0];
@@ -353,18 +240,18 @@ void vkrenderer::create_swapchain() {
create_info.clipped = VK_TRUE;
create_info.oldSwapchain = old_swapchain;
- VKRESULT(vkCreateSwapchainKHR(device, &create_info, nullptr, &swapchain))
+ VKRESULT(vkCreateSwapchainKHR(context.device, &create_info, nullptr, &swapchain))
if (old_swapchain != VK_NULL_HANDLE) {
- vkDestroySwapchainKHR(device, old_swapchain, nullptr);
+ vkDestroySwapchainKHR(context.device, old_swapchain, nullptr);
}
swapchain_images_count = 0;
- VKRESULT(vkGetSwapchainImagesKHR(device, swapchain, (uint32_t*)&swapchain_images_count, VK_NULL_HANDLE))
+ VKRESULT(vkGetSwapchainImagesKHR(context.device, swapchain, (uint32_t*)&swapchain_images_count, VK_NULL_HANDLE))
swapchain_images = std::vector<VkImage>{ swapchain_images_count };
swapchain_images_views = std::vector<VkImageView>{ swapchain_images_count };
- VKRESULT(vkGetSwapchainImagesKHR(device, swapchain, (uint32_t*)&swapchain_images_count, swapchain_images.data()))
+ VKRESULT(vkGetSwapchainImagesKHR(context.device, swapchain, (uint32_t*)&swapchain_images_count, swapchain_images.data()))
for (size_t index = 0; index < swapchain_images_count; index++) {
VkImageSubresourceRange image_subresource_range = {};
@@ -389,7 +276,7 @@ void vkrenderer::create_swapchain() {
};
image_view_create_info.subresourceRange = image_subresource_range;
- VKRESULT(vkCreateImageView(device, &image_view_create_info, nullptr, &swapchain_images_views[index]))
+ VKRESULT(vkCreateImageView(context.device, &image_view_create_info, nullptr, &swapchain_images_views[index]))
}
}
@@ -406,7 +293,7 @@ void vkrenderer::create_pipeline() {
shader_create_info.codeSize = shader_code.size();
shader_create_info.pCode = (uint32_t*)shader_code.data();
- VKRESULT(vkCreateShaderModule(device, &shader_create_info, nullptr, &compute_shader_module))
+ VKRESULT(vkCreateShaderModule(context.device, &shader_create_info, nullptr, &compute_shader_module))
VkPipelineShaderStageCreateInfo stage_create_info = {};
stage_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
@@ -443,7 +330,7 @@ void vkrenderer::create_pipeline() {
descriptor_set_layout_create_info.bindingCount = bindings.size();
descriptor_set_layout_create_info.pBindings = bindings.data();
- VKRESULT(vkCreateDescriptorSetLayout(device, &descriptor_set_layout_create_info, nullptr, &compute_shader_layout))
+ VKRESULT(vkCreateDescriptorSetLayout(context.device, &descriptor_set_layout_create_info, nullptr, &compute_shader_layout))
VkPipelineLayoutCreateInfo layout_create_info = {};
layout_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
@@ -454,7 +341,7 @@ void vkrenderer::create_pipeline() {
layout_create_info.pushConstantRangeCount = 0;
layout_create_info.pPushConstantRanges = nullptr;
- VKRESULT(vkCreatePipelineLayout(device, &layout_create_info, nullptr, &compute_pipeline_layout))
+ VKRESULT(vkCreatePipelineLayout(context.device, &layout_create_info, nullptr, &compute_pipeline_layout))
VkComputePipelineCreateInfo create_info = {};
create_info.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
@@ -464,7 +351,7 @@ void vkrenderer::create_pipeline() {
create_info.layout = compute_pipeline_layout;
VKRESULT(vkCreateComputePipelines(
- device,
+ context.device,
VK_NULL_HANDLE,
1,
&create_info,
@@ -475,24 +362,14 @@ void vkrenderer::create_pipeline() {
std::cerr << "Compute pipeline ready" << std::endl;
}
-void vkrenderer::create_memory_allocator() {
- VmaAllocatorCreateInfo allocator_info = {};
- allocator_info.vulkanApiVersion = VK_API_VERSION_1_2;
- allocator_info.physicalDevice = physical_device;
- allocator_info.device = device;
- allocator_info.instance = instance;
-
- vmaCreateAllocator(&allocator_info, &allocator);
-}
-
void vkrenderer::create_command_buffers() {
VkCommandPoolCreateInfo cmd_pool_create_info = {};
cmd_pool_create_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
cmd_pool_create_info.pNext = nullptr;
cmd_pool_create_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
- cmd_pool_create_info.queueFamilyIndex = compute_queue_index;
+ cmd_pool_create_info.queueFamilyIndex = context.queue_index;
- VKRESULT(vkCreateCommandPool(device, &cmd_pool_create_info, nullptr, &command_pool))
+ VKRESULT(vkCreateCommandPool(context.device, &cmd_pool_create_info, nullptr, &command_pool))
VkCommandBufferAllocateInfo cmd_buf_allocate_info = {};
cmd_buf_allocate_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
@@ -502,7 +379,7 @@ void vkrenderer::create_command_buffers() {
cmd_buf_allocate_info.commandBufferCount = swapchain_images_count;
command_buffers = std::vector<VkCommandBuffer> { swapchain_images_count };
- vkAllocateCommandBuffers(device, &cmd_buf_allocate_info, command_buffers.data());
+ vkAllocateCommandBuffers(context.device, &cmd_buf_allocate_info, command_buffers.data());
}
void vkrenderer::create_descriptor_sets() {
std::vector<VkDescriptorPoolSize> descriptor_pools_sizes = {
@@ -524,7 +401,7 @@ void vkrenderer::create_descriptor_sets() {
descriptor_pool_create_info.poolSizeCount = descriptor_pools_sizes.size();
descriptor_pool_create_info.pPoolSizes = descriptor_pools_sizes.data();
- VKRESULT(vkCreateDescriptorPool(device, &descriptor_pool_create_info, nullptr, &descriptor_pool))
+ VKRESULT(vkCreateDescriptorPool(context.device, &descriptor_pool_create_info, nullptr, &descriptor_pool))
std::vector<VkDescriptorSetLayout> sets_layouts = { swapchain_images_count, compute_shader_layout };
@@ -536,7 +413,7 @@ void vkrenderer::create_descriptor_sets() {
descriptor_set_allocate_info.pSetLayouts = sets_layouts.data();
compute_shader_sets = std::vector<VkDescriptorSet> { swapchain_images_count };
- vkAllocateDescriptorSets(device, &descriptor_set_allocate_info, compute_shader_sets.data());
+ vkAllocateDescriptorSets(context.device, &descriptor_set_allocate_info, compute_shader_sets.data());
}
void vkrenderer::create_fences() {
@@ -547,7 +424,7 @@ void vkrenderer::create_fences() {
create_info.pNext = nullptr;
create_info.flags = VK_FENCE_CREATE_SIGNALED_BIT;
- VKRESULT(vkCreateFence(device, &create_info, nullptr, &submission_fence))
+ VKRESULT(vkCreateFence(context.device, &create_info, nullptr, &submission_fence))
submission_fences.push_back(submission_fence);
}
@@ -562,8 +439,8 @@ void vkrenderer::create_semaphores() {
create_info.pNext = nullptr;
create_info.flags = 0;
- VKRESULT(vkCreateSemaphore(device, &create_info, nullptr, &execution_semaphore))
- VKRESULT(vkCreateSemaphore(device, &create_info, nullptr, &acquire_semaphore))
+ VKRESULT(vkCreateSemaphore(context.device, &create_info, nullptr, &execution_semaphore))
+ VKRESULT(vkCreateSemaphore(context.device, &create_info, nullptr, &acquire_semaphore))
execution_semaphores.push_back(execution_semaphore);
acquire_semaphores.push_back(acquire_semaphore);
@@ -580,37 +457,25 @@ void vkrenderer::create_input_buffer(const input_data& inputs) {
VmaAllocationCreateInfo alloc_create_info = {};
alloc_create_info.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
- vmaCreateBuffer(allocator, &buffer_info, &alloc_create_info, &compute_shader_buffer, &allocation, nullptr);
+ vmaCreateBuffer(context.allocator, &buffer_info, &alloc_create_info, &compute_shader_buffer, &allocation, nullptr);
- vmaMapMemory(allocator, allocation, (void**) &mapped_data);
+ vmaMapMemory(context.allocator, allocation, (void**) &mapped_data);
std::memcpy(mapped_data, &inputs, sizeof(inputs));
}
-void vkrenderer::destroy_instance() {
- vkDestroyInstance(instance, nullptr);
-}
-
-void vkrenderer::destroy_debugger() {
- vkDestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
-}
-
void vkrenderer::destroy_surface() {
- vkDestroySurfaceKHR(instance, platform_surface, nullptr);
-}
-
-void vkrenderer::destroy_device() {
- vkDestroyDevice(device, nullptr);
+ vkDestroySurfaceKHR(context.instance, platform_surface, nullptr);
}
void vkrenderer::destroy_swapchain() {
- vkDestroySwapchainKHR(device, swapchain, nullptr);
+ vkDestroySwapchainKHR(context.device, swapchain, nullptr);
destroy_swapchain_images();
}
void vkrenderer::destroy_swapchain_images() {
for (auto view: swapchain_images_views) {
- vkDestroyImageView(device, view, nullptr);
+ vkDestroyImageView(context.device, view, nullptr);
}
swapchain_images_views.clear();
@@ -618,31 +483,27 @@ void vkrenderer::destroy_swapchain_images() {
}
void vkrenderer::destroy_pipeline() {
- vkDestroyShaderModule(device, compute_shader_module, nullptr);
- vkDestroyDescriptorSetLayout(device, compute_shader_layout, nullptr);
- vkDestroyPipelineLayout(device, compute_pipeline_layout, nullptr);
- vkDestroyPipeline(device, compute_pipeline, nullptr);
+ vkDestroyShaderModule(context.device, compute_shader_module, nullptr);
+ vkDestroyDescriptorSetLayout(context.device, compute_shader_layout, nullptr);
+ vkDestroyPipelineLayout(context.device, compute_pipeline_layout, nullptr);
+ vkDestroyPipeline(context.device, compute_pipeline, nullptr);
}
void vkrenderer::destroy_command_buffers() {
- vkFreeCommandBuffers(device, command_pool, swapchain_images_count, command_buffers.data());
-
- vkDestroyCommandPool(device, command_pool, nullptr);
-}
+ vkFreeCommandBuffers(context.device, command_pool, swapchain_images_count, command_buffers.data());
-void vkrenderer::destroy_allocator() {
- vmaDestroyAllocator(allocator);
+ vkDestroyCommandPool(context.device, command_pool, nullptr);
}
void vkrenderer::destroy_descriptor_sets() {
// vkFreeDescriptorSets(device, descriptor_pool, swapchain_images_count, compute_shader_sets.data());
- vkDestroyDescriptorPool(device, descriptor_pool, nullptr);
+ vkDestroyDescriptorPool(context.device, descriptor_pool, nullptr);
}
void vkrenderer::destroy_fences() {
for (auto fence: submission_fences) {
- vkDestroyFence(device, fence, nullptr);
+ vkDestroyFence(context.device, fence, nullptr);
}
submission_fences.clear();
@@ -650,54 +511,21 @@ void vkrenderer::destroy_fences() {
void vkrenderer::destroy_semaphores() {
for (auto semaphore: acquire_semaphores) {
- vkDestroySemaphore(device, semaphore, nullptr);
+ vkDestroySemaphore(context.device, semaphore, nullptr);
}
acquire_semaphores.clear();
for (auto semaphore: execution_semaphores) {
- vkDestroySemaphore(device, semaphore, nullptr);
+ vkDestroySemaphore(context.device, semaphore, nullptr);
}
execution_semaphores.clear();
}
void vkrenderer::destroy_input_buffer() {
- vmaUnmapMemory(allocator, allocation);
- vmaDestroyBuffer(allocator, compute_shader_buffer, allocation);
-}
-
-void vkrenderer::select_physical_device() {
- uint32_t physical_devices_count = 0;
- vkEnumeratePhysicalDevices(instance, &physical_devices_count, nullptr);
-
- if (physical_devices_count < 1) {
- std::cerr << "No physical device found !" << std::endl;
- exit(1);
- }
-
- std::vector<VkPhysicalDevice> physical_devices { physical_devices_count };
- vkEnumeratePhysicalDevices(instance, &physical_devices_count, physical_devices.data());
-
- physical_device = physical_devices[0];
-}
-
-void vkrenderer::select_compute_queue() {
- uint32_t queue_properties_count = 0;
- vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &queue_properties_count, nullptr);
-
- std::vector<VkQueueFamilyProperties> queue_properties { queue_properties_count };
- vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &queue_properties_count, queue_properties.data());
-
- for (uint32_t properties_index = 0; properties_index < queue_properties_count; properties_index++) {
- if (queue_properties[properties_index].queueFlags & VK_QUEUE_COMPUTE_BIT) {
- compute_queue_index = properties_index;
- return;
- }
- }
-
- std::cerr << "No compute queue found !" << std::endl;
- exit(1);
+ vmaUnmapMemory(context.allocator, allocation);
+ vmaDestroyBuffer(context.allocator, compute_shader_buffer, allocation);
}
void vkrenderer::update_descriptors_buffer() {
@@ -722,7 +550,7 @@ void vkrenderer::update_descriptors_buffer() {
write_descriptors[set_index].pTexelBufferView = VK_NULL_HANDLE;
}
- vkUpdateDescriptorSets(device, write_descriptors.size(), write_descriptors.data(), 0, nullptr);
+ vkUpdateDescriptorSets(context.device, write_descriptors.size(), write_descriptors.data(), 0, nullptr);
}
void vkrenderer::update_descriptors_image() {
@@ -747,7 +575,7 @@ void vkrenderer::update_descriptors_image() {
write_descriptors[set_index].pTexelBufferView = VK_NULL_HANDLE;
}
- vkUpdateDescriptorSets(device, write_descriptors.size(), write_descriptors.data(), 0, nullptr);
+ vkUpdateDescriptorSets(context.device, write_descriptors.size(), write_descriptors.data(), 0, nullptr);
}
void vkrenderer::handle_swapchain_result(VkResult function_result) {