Remove VulkanState dependency

Passing VulkanState everywhere (including windowLoop) made no sense.
I know that "globals are evil", but this is the best solution for now.
This commit is contained in:
Piotr Krygier committed 2023-04-04 15:27:42 +02:00
1 parent e06288320d
commit 6781c1cff1
16 files changed
+284 -271

No files matched your search

+1
View File
@@ -52,6 +52,7 @@ target_sources(rse_graphics PRIVATE
src/vulkan_descriptors.cpp src/vulkan_descriptors.cpp
src/rse_graphics.cpp src/rse_graphics.cpp
src/mesh_controller.cpp src/mesh_controller.cpp
src/vulkan_commons.cpp
) )
target_link_libraries(rse_graphics PRIVATE target_link_libraries(rse_graphics PRIVATE
+4 -6
View File
@@ -14,8 +14,7 @@ std::queue<uint16_t> free_ids;
std::map<uint16_t, std::queue<uint16_t>> instance_free_ids; std::map<uint16_t, std::queue<uint16_t>> instance_free_ids;
uint16_t create_mesh(rse::graphics::vulkanbase::VulkanState& vulkan_state, uint16_t create_mesh(std::vector<rse::graphics::vulkanbase::Vertex> vertices,
std::vector<rse::graphics::vulkanbase::Vertex> vertices,
std::vector<uint16_t> indices) std::vector<uint16_t> indices)
{ {
static uint16_t last_id = 0U; static uint16_t last_id = 0U;
@@ -34,13 +33,12 @@ uint16_t create_mesh(rse::graphics::vulkanbase::VulkanState& vulkan_state,
indices indices
)); ));
rse::graphics::vulkanbase::update_mesh_buffers(vulkan_state, vertices, indices); rse::graphics::vulkanbase::update_mesh_buffers(vertices, indices);
return assignable_id; return assignable_id;
} }
void create_mesh_instance(rse::graphics::vulkanbase::VulkanState& vulkan_state, void create_mesh_instance(uint16_t mesh_id,
uint16_t mesh_id,
InstanceData instance_data) InstanceData instance_data)
{ {
uint16_t instance_id = 0U; uint16_t instance_id = 0U;
@@ -59,7 +57,7 @@ void create_mesh_instance(rse::graphics::vulkanbase::VulkanState& vulkan_state,
instance_data instance_data
); );
rse::graphics::vulkanbase::update_mesh_instances(vulkan_state, instance_data); rse::graphics::vulkanbase::update_mesh_instances(instance_data);
} }
//TODO: Replace with something else. It will take to much time to do this each time we draw frame for all the objects //TODO: Replace with something else. It will take to much time to do this each time we draw frame for all the objects
+2 -4
View File
@@ -47,8 +47,7 @@ struct Mesh
* @param indices indices for mesh * @param indices indices for mesh
* @return uint16_t Mesh identifier. Can be useful for getting vertices, indices and instances * @return uint16_t Mesh identifier. Can be useful for getting vertices, indices and instances
*/ */
uint16_t create_mesh(rse::graphics::vulkanbase::VulkanState& vulkan_state, uint16_t create_mesh(std::vector<rse::graphics::vulkanbase::Vertex> vertices,
std::vector<rse::graphics::vulkanbase::Vertex> vertices,
std::vector<uint16_t> indices); std::vector<uint16_t> indices);
/** /**
@@ -57,8 +56,7 @@ uint16_t create_mesh(rse::graphics::vulkanbase::VulkanState& vulkan_state,
* @param mesh_id Mesh id * @param mesh_id Mesh id
* @param instance_data Transformation info * @param instance_data Transformation info
*/ */
void create_mesh_instance(rse::graphics::vulkanbase::VulkanState& vulkan_state, void create_mesh_instance(uint16_t mesh_id,
uint16_t mesh_id,
InstanceData instance_data); InstanceData instance_data);
std::vector<rse::graphics::vulkanbase::Vertex> get_mesh_vertices(uint16_t mesh_id); std::vector<rse::graphics::vulkanbase::Vertex> get_mesh_vertices(uint16_t mesh_id);
+5 -5
View File
@@ -36,7 +36,7 @@ uint8_t RseGraphics::graphics_run(void)
if (0 != vulkanbase::initVulkan(vulkan_state)) if (0 != vulkanbase::initVulkan())
return -1; return -1;
// FIXME: Temporary array of vertices, for testing purposes // FIXME: Temporary array of vertices, for testing purposes
@@ -48,8 +48,8 @@ uint8_t RseGraphics::graphics_run(void)
// FIXME: Temporary array of vertices, for testing purposes // FIXME: Temporary array of vertices, for testing purposes
std::vector<uint16_t> indices = {0, 1, 2, 2, 3, 0}; std::vector<uint16_t> indices = {0, 1, 2, 2, 3, 0};
uint16_t mesh_id = rse::graphics::mesh::create_mesh(vulkan_state, vertices, indices); uint16_t mesh_id = rse::graphics::mesh::create_mesh(vertices, indices);
rse::graphics::mesh::create_mesh_instance(vulkan_state, mesh_id, rse::graphics::mesh::InstanceData( rse::graphics::mesh::create_mesh_instance(mesh_id, rse::graphics::mesh::InstanceData(
glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, glm::radians(45.0f)), glm::vec3(0.0f, 0.0f, glm::radians(45.0f)),
1.0f 1.0f
@@ -61,9 +61,9 @@ uint8_t RseGraphics::graphics_run(void)
// 1.0f // 1.0f
// )); // ));
window::windowLoop(vulkan_state); window::windowLoop();
vulkanbase::deinitVulkan(vulkan_state); vulkanbase::deinitVulkan();
window::windowTerminate(); window::windowTerminate();
+60 -60
View File
@@ -90,12 +90,12 @@ static void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMesse
static void setEnabledExtension(); static void setEnabledExtension();
static uint8_t createInstance(); static uint8_t createInstance();
static uint8_t createSurface(VulkanState& vulkan_state); static uint8_t createSurface();
static uint8_t pickPhysicalDevice(VulkanState& vulkan_state); static uint8_t pickPhysicalDevice();
static uint8_t createDevice(VulkanState& vulkan_state); static uint8_t createDevice();
static uint8_t createMemoryAllocator(VulkanState& vulkan_state); static uint8_t createMemoryAllocator();
static uint8_t createSyncObjects(VulkanState& vulkan_state); static uint8_t createSyncObjects();
@@ -306,9 +306,9 @@ uint8_t createInstance()
* *
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
*/ */
uint8_t createSurface(VulkanState& vulkan_state) uint8_t createSurface()
{ {
if (VK_SUCCESS != glfwCreateWindowSurface(gVulkanInstance, window::getWindowHandle(), nullptr, &vulkan_state.surface)) { if (VK_SUCCESS != glfwCreateWindowSurface(gVulkanInstance, window::getWindowHandle(), nullptr, &g_vulkan_state.surface)) {
LOGF << vulkanErrorMesssages[VULKAN_SURFACE_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_SURFACE_CREATION_FAILED];
return VULKAN_ERROR_SURFACE_CREATION_FAILED; return VULKAN_ERROR_SURFACE_CREATION_FAILED;
} }
@@ -323,7 +323,7 @@ uint8_t createSurface(VulkanState& vulkan_state)
* VULKAN_ERROR_NO_PHYSICAL_DEVICE_FOUND * VULKAN_ERROR_NO_PHYSICAL_DEVICE_FOUND
* VULKAN_ERROR_NO_SUITABLE_PHYSICAL_DEVICE_FOUND * VULKAN_ERROR_NO_SUITABLE_PHYSICAL_DEVICE_FOUND
*/ */
uint8_t pickPhysicalDevice(VulkanState& vulkan_state) uint8_t pickPhysicalDevice()
{ {
uint32_t physicalDeviceCount = 0U; uint32_t physicalDeviceCount = 0U;
std::vector<VkPhysicalDevice> pPhysicalDevices; std::vector<VkPhysicalDevice> pPhysicalDevices;
@@ -351,12 +351,12 @@ uint8_t pickPhysicalDevice(VulkanState& vulkan_state)
deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU) { deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU) {
/* Application MUST have geometry shader */ /* Application MUST have geometry shader */
if (deviceFeatures.geometryShader) { if (deviceFeatures.geometryShader) {
vulkan_state.physicalDevice = pPhysicalDevices[physicalDeviceIdx]; g_vulkan_state.physicalDevice = pPhysicalDevices[physicalDeviceIdx];
} }
} }
} }
if (VK_NULL_HANDLE == vulkan_state.physicalDevice) { if (VK_NULL_HANDLE == g_vulkan_state.physicalDevice) {
/* No suitable device found */ /* No suitable device found */
LOGF << vulkanErrorMesssages[VULKAN_NO_SUITABLE_PHYSICAL_DEVICES]; LOGF << vulkanErrorMesssages[VULKAN_NO_SUITABLE_PHYSICAL_DEVICES];
return VULKAN_ERROR_NO_SUITABLE_PHYSICAL_DEVICE_FOUND; return VULKAN_ERROR_NO_SUITABLE_PHYSICAL_DEVICE_FOUND;
@@ -372,7 +372,7 @@ uint8_t pickPhysicalDevice(VulkanState& vulkan_state)
* VULKAN_ERROR_QUEUE_NOT_SUPPORTED * VULKAN_ERROR_QUEUE_NOT_SUPPORTED
* VULKAN_ERROR_DEVICE_CREATION_FAILED * VULKAN_ERROR_DEVICE_CREATION_FAILED
*/ */
uint8_t createDevice(VulkanState& vulkan_state) uint8_t createDevice()
{ {
uint32_t queueFamiliesPropertyCount = 0U; uint32_t queueFamiliesPropertyCount = 0U;
ssize_t graphicsFamilyIdx = -1; ssize_t graphicsFamilyIdx = -1;
@@ -384,9 +384,9 @@ uint8_t createDevice(VulkanState& vulkan_state)
std::vector<float> pPresentationFamiliyQueuePriorities; std::vector<float> pPresentationFamiliyQueuePriorities;
/* Get information about supported queue families */ /* Get information about supported queue families */
vkGetPhysicalDeviceQueueFamilyProperties(vulkan_state.physicalDevice, &queueFamiliesPropertyCount, nullptr); vkGetPhysicalDeviceQueueFamilyProperties(g_vulkan_state.physicalDevice, &queueFamiliesPropertyCount, nullptr);
pQueueFamilyProperties.resize(queueFamiliesPropertyCount); pQueueFamilyProperties.resize(queueFamiliesPropertyCount);
vkGetPhysicalDeviceQueueFamilyProperties(vulkan_state.physicalDevice, &queueFamiliesPropertyCount, vkGetPhysicalDeviceQueueFamilyProperties(g_vulkan_state.physicalDevice, &queueFamiliesPropertyCount,
pQueueFamilyProperties.data()); pQueueFamilyProperties.data());
/* Get queue families that support graphics operations AND have most /* Get queue families that support graphics operations AND have most
@@ -402,7 +402,7 @@ uint8_t createDevice(VulkanState& vulkan_state)
} }
/* Check for family with surface support */ /* Check for family with surface support */
VkBool32 presentSupport = false; VkBool32 presentSupport = false;
vkGetPhysicalDeviceSurfaceSupportKHR(vulkan_state.physicalDevice, familyIdx, vulkan_state.surface, &presentSupport); vkGetPhysicalDeviceSurfaceSupportKHR(g_vulkan_state.physicalDevice, familyIdx, g_vulkan_state.surface, &presentSupport);
if (presentSupport) { if (presentSupport) {
presentationFamiliyIdx = familyIdx; presentationFamiliyIdx = familyIdx;
} }
@@ -419,7 +419,7 @@ uint8_t createDevice(VulkanState& vulkan_state)
return VULKAN_ERROR_QUEUE_NOT_SUPPORTED; return VULKAN_ERROR_QUEUE_NOT_SUPPORTED;
} }
vulkan_state.queueFamilyIndices[0] = graphicsFamilyIdx; /* TODO: Change when more families are needed */ g_vulkan_state.queueFamilyIndices[0] = graphicsFamilyIdx; /* TODO: Change when more families are needed */
pGraphicsFamiliyQueuePriorities.resize(pQueueFamilyProperties[graphicsFamilyIdx].queueCount); pGraphicsFamiliyQueuePriorities.resize(pQueueFamilyProperties[graphicsFamilyIdx].queueCount);
for (size_t i = 0; i < pQueueFamilyProperties[graphicsFamilyIdx].queueCount; i++) { for (size_t i = 0; i < pQueueFamilyProperties[graphicsFamilyIdx].queueCount; i++) {
@@ -457,13 +457,13 @@ uint8_t createDevice(VulkanState& vulkan_state)
deviceCreateInfo.ppEnabledExtensionNames = gEnabledDeviceExtensionsNames.data(); deviceCreateInfo.ppEnabledExtensionNames = gEnabledDeviceExtensionsNames.data();
deviceCreateInfo.pEnabledFeatures = nullptr; /* TODO: Enable features, when needed */ deviceCreateInfo.pEnabledFeatures = nullptr; /* TODO: Enable features, when needed */
if (VK_SUCCESS != vkCreateDevice(vulkan_state.physicalDevice, &deviceCreateInfo, nullptr, &vulkan_state.device)) { if (VK_SUCCESS != vkCreateDevice(g_vulkan_state.physicalDevice, &deviceCreateInfo, nullptr, &g_vulkan_state.device)) {
LOGF << vulkanErrorMesssages[VULKAN_FAILED_TO_CREATE_DEVICE]; LOGF << vulkanErrorMesssages[VULKAN_FAILED_TO_CREATE_DEVICE];
return VULKAN_ERROR_DEVICE_CREATION_FAILED; return VULKAN_ERROR_DEVICE_CREATION_FAILED;
} }
vkGetDeviceQueue(vulkan_state.device, graphicsFamilyIdx, 0, &vulkan_state.graphicsQueue); vkGetDeviceQueue(g_vulkan_state.device, graphicsFamilyIdx, 0, &g_vulkan_state.graphicsQueue);
vkGetDeviceQueue(vulkan_state.device, presentationFamiliyIdx, 0, &gPresentQueue); vkGetDeviceQueue(g_vulkan_state.device, presentationFamiliyIdx, 0, &gPresentQueue);
return VULKAN_ERROR_NO_ERROR; return VULKAN_ERROR_NO_ERROR;
} }
@@ -474,7 +474,7 @@ uint8_t createDevice(VulkanState& vulkan_state)
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_ALLOCATOR_CREATION_FAILED * VULKAN_ERROR_ALLOCATOR_CREATION_FAILED
*/ */
uint8_t createMemoryAllocator(VulkanState& vulkan_state) uint8_t createMemoryAllocator()
{ {
VmaVulkanFunctions vulkanFunctions = {0}; VmaVulkanFunctions vulkanFunctions = {0};
vulkanFunctions.vkGetInstanceProcAddr = &vkGetInstanceProcAddr; vulkanFunctions.vkGetInstanceProcAddr = &vkGetInstanceProcAddr;
@@ -482,12 +482,12 @@ uint8_t createMemoryAllocator(VulkanState& vulkan_state)
VmaAllocatorCreateInfo allocatorCreateInfo = {0}; VmaAllocatorCreateInfo allocatorCreateInfo = {0};
allocatorCreateInfo.vulkanApiVersion = VK_API_VERSION_1_3; allocatorCreateInfo.vulkanApiVersion = VK_API_VERSION_1_3;
allocatorCreateInfo.physicalDevice = vulkan_state.physicalDevice; allocatorCreateInfo.physicalDevice = g_vulkan_state.physicalDevice;
allocatorCreateInfo.device = vulkan_state.device; allocatorCreateInfo.device = g_vulkan_state.device;
allocatorCreateInfo.instance = gVulkanInstance; allocatorCreateInfo.instance = gVulkanInstance;
allocatorCreateInfo.pVulkanFunctions = &vulkanFunctions; allocatorCreateInfo.pVulkanFunctions = &vulkanFunctions;
if (VK_SUCCESS != vmaCreateAllocator(&allocatorCreateInfo, &vulkan_state.allocator)) { if (VK_SUCCESS != vmaCreateAllocator(&allocatorCreateInfo, &g_vulkan_state.allocator)) {
LOGF << vulkanErrorMesssages[VULKAN_ERROR_ALLOCATOR_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_ERROR_ALLOCATOR_CREATION_FAILED];
return VULKAN_ERROR_ALLOCATOR_CREATION_FAILED; return VULKAN_ERROR_ALLOCATOR_CREATION_FAILED;
} }
@@ -502,7 +502,7 @@ uint8_t createMemoryAllocator(VulkanState& vulkan_state)
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_SYNCOBJCTS_CREATION_FAILED * VULKAN_ERROR_SYNCOBJCTS_CREATION_FAILED
*/ */
uint8_t createSyncObjects(VulkanState& vulkan_state) uint8_t createSyncObjects()
{ {
gImageAvailableSemaphores.resize(SWAP_BUFFER_COUNT); gImageAvailableSemaphores.resize(SWAP_BUFFER_COUNT);
gRenderFinishedSemaphores.resize(SWAP_BUFFER_COUNT); gRenderFinishedSemaphores.resize(SWAP_BUFFER_COUNT);
@@ -516,9 +516,9 @@ uint8_t createSyncObjects(VulkanState& vulkan_state)
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
for (size_t i = 0; i < SWAP_BUFFER_COUNT; ++i) { for (size_t i = 0; i < SWAP_BUFFER_COUNT; ++i) {
if (VK_SUCCESS != vkCreateSemaphore(vulkan_state.device, &semaphoreInfo, nullptr, &gImageAvailableSemaphores[i]) || if (VK_SUCCESS != vkCreateSemaphore(g_vulkan_state.device, &semaphoreInfo, nullptr, &gImageAvailableSemaphores[i]) ||
VK_SUCCESS != vkCreateSemaphore(vulkan_state.device, &semaphoreInfo, nullptr, &gRenderFinishedSemaphores[i]) || VK_SUCCESS != vkCreateSemaphore(g_vulkan_state.device, &semaphoreInfo, nullptr, &gRenderFinishedSemaphores[i]) ||
VK_SUCCESS != vkCreateFence(vulkan_state.device, &fenceInfo, nullptr, &gInFlightFences[i])) { VK_SUCCESS != vkCreateFence(g_vulkan_state.device, &fenceInfo, nullptr, &gInFlightFences[i])) {
LOGF << vulkanErrorMesssages[VULKAN_SYNCOBJCTS_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_SYNCOBJCTS_CREATION_FAILED];
return VULKAN_ERROR_SYNCOBJCTS_CREATION_FAILED; return VULKAN_ERROR_SYNCOBJCTS_CREATION_FAILED;
} }
@@ -528,7 +528,7 @@ uint8_t createSyncObjects(VulkanState& vulkan_state)
} }
uint8_t initVulkan(VulkanState& vulkan_state) uint8_t initVulkan()
{ {
uint8_t status = VULKAN_ERROR_NO_ERROR; uint8_t status = VULKAN_ERROR_NO_ERROR;
@@ -537,62 +537,62 @@ uint8_t initVulkan(VulkanState& vulkan_state)
#ifndef NDEBUG #ifndef NDEBUG
setupDebugMessenger(); setupDebugMessenger();
#endif #endif
STATUS_CHECK(createSurface(vulkan_state)); STATUS_CHECK(createSurface());
STATUS_CHECK(pickPhysicalDevice(vulkan_state)); STATUS_CHECK(pickPhysicalDevice());
STATUS_CHECK(createDevice(vulkan_state)); STATUS_CHECK(createDevice());
STATUS_CHECK(createMemoryAllocator(vulkan_state)); STATUS_CHECK(createMemoryAllocator());
STATUS_CHECK(create_pipeline(vulkan_state)); STATUS_CHECK(create_pipeline());
STATUS_CHECK(create_buffers(vulkan_state)); STATUS_CHECK(create_buffers());
STATUS_CHECK(create_descriptors(vulkan_state)); STATUS_CHECK(create_descriptors());
STATUS_CHECK(createSyncObjects(vulkan_state)); STATUS_CHECK(createSyncObjects());
return status; return status;
} }
void drawFrame(VulkanState& vulkan_state) void drawFrame()
{ {
vkWaitForFences(vulkan_state.device, 1, &gInFlightFences[vulkan_state.currentFrame], VK_TRUE, UINT64_MAX); vkWaitForFences(g_vulkan_state.device, 1, &gInFlightFences[g_vulkan_state.currentFrame], VK_TRUE, UINT64_MAX);
uint32_t imageIndex; uint32_t imageIndex;
VkResult result = vkAcquireNextImageKHR(vulkan_state.device, vulkan_state.swapchain, UINT64_MAX, gImageAvailableSemaphores[vulkan_state.currentFrame], VkResult result = vkAcquireNextImageKHR(g_vulkan_state.device, g_vulkan_state.swapchain, UINT64_MAX, gImageAvailableSemaphores[g_vulkan_state.currentFrame],
VK_NULL_HANDLE, &imageIndex); VK_NULL_HANDLE, &imageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) { if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) {
recreateSwapchain(vulkan_state); recreateSwapchain();
return; return;
} else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) { } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
// throw std::runtime_error("failed to acquire swap chain image!"); // throw std::runtime_error("failed to acquire swap chain image!");
return; return;
} }
updateUniformBuffer(vulkan_state); updateUniformBuffer();
/* Only reset the fence if we are submitting work */ /* Only reset the fence if we are submitting work */
vkResetFences(vulkan_state.device, 1, &gInFlightFences[vulkan_state.currentFrame]); vkResetFences(g_vulkan_state.device, 1, &gInFlightFences[g_vulkan_state.currentFrame]);
reset_command_buffer(vulkan_state); reset_command_buffer();
recordCommandBuffer(vulkan_state, imageIndex); recordCommandBuffer(imageIndex);
VkSubmitInfo submitInfo{}; VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
VkSemaphore waitSemaphores[] = {gImageAvailableSemaphores[vulkan_state.currentFrame]}; VkSemaphore waitSemaphores[] = {gImageAvailableSemaphores[g_vulkan_state.currentFrame]};
VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT}; VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
submitInfo.waitSemaphoreCount = 1; submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores; submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages; submitInfo.pWaitDstStageMask = waitStages;
submitInfo.commandBufferCount = 1; submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &vulkan_state.pCommandBuffers[vulkan_state.currentFrame]; submitInfo.pCommandBuffers = &g_vulkan_state.pCommandBuffers[g_vulkan_state.currentFrame];
VkSemaphore signalSemaphores[] = {gRenderFinishedSemaphores[vulkan_state.currentFrame]}; VkSemaphore signalSemaphores[] = {gRenderFinishedSemaphores[g_vulkan_state.currentFrame]};
submitInfo.signalSemaphoreCount = 1; submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores; submitInfo.pSignalSemaphores = signalSemaphores;
if (vkQueueSubmit(vulkan_state.graphicsQueue, 1, &submitInfo, gInFlightFences[vulkan_state.currentFrame]) != VK_SUCCESS) { if (vkQueueSubmit(g_vulkan_state.graphicsQueue, 1, &submitInfo, gInFlightFences[g_vulkan_state.currentFrame]) != VK_SUCCESS) {
throw std::runtime_error("failed to submit draw command buffer!"); throw std::runtime_error("failed to submit draw command buffer!");
} }
@@ -602,7 +602,7 @@ void drawFrame(VulkanState& vulkan_state)
presentInfo.waitSemaphoreCount = 1; presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores; presentInfo.pWaitSemaphores = signalSemaphores;
VkSwapchainKHR swapChains[] = {vulkan_state.swapchain}; VkSwapchainKHR swapChains[] = {g_vulkan_state.swapchain};
presentInfo.swapchainCount = 1; presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains; presentInfo.pSwapchains = swapChains;
@@ -610,28 +610,28 @@ void drawFrame(VulkanState& vulkan_state)
vkQueuePresentKHR(gPresentQueue, &presentInfo); vkQueuePresentKHR(gPresentQueue, &presentInfo);
vulkan_state.currentFrame = (vulkan_state.currentFrame + 1) % SWAP_BUFFER_COUNT; g_vulkan_state.currentFrame = (g_vulkan_state.currentFrame + 1) % SWAP_BUFFER_COUNT;
} }
void deinitVulkan(VulkanState& vulkan_state) void deinitVulkan()
{ {
cleanup_swapchain(vulkan_state); cleanup_swapchain();
destroy_descriptors(vulkan_state); destroy_descriptors();
destroy_buffers(vulkan_state); destroy_buffers();
for (size_t i = 0; i < SWAP_BUFFER_COUNT; ++i) { for (size_t i = 0; i < SWAP_BUFFER_COUNT; ++i) {
vkDestroySemaphore(vulkan_state.device, gImageAvailableSemaphores[i], nullptr); vkDestroySemaphore(g_vulkan_state.device, gImageAvailableSemaphores[i], nullptr);
vkDestroySemaphore(vulkan_state.device, gRenderFinishedSemaphores[i], nullptr); vkDestroySemaphore(g_vulkan_state.device, gRenderFinishedSemaphores[i], nullptr);
vkDestroyFence(vulkan_state.device, gInFlightFences[i], nullptr); vkDestroyFence(g_vulkan_state.device, gInFlightFences[i], nullptr);
} }
destroy_pipeline(vulkan_state); destroy_pipeline();
vmaDestroyAllocator(vulkan_state.allocator); vmaDestroyAllocator(g_vulkan_state.allocator);
vkDestroyDevice(vulkan_state.device, nullptr); vkDestroyDevice(g_vulkan_state.device, nullptr);
#ifndef NDEBUG #ifndef NDEBUG
DestroyDebugUtilsMessengerEXT(gVulkanInstance, gDebugMessenger, nullptr); DestroyDebugUtilsMessengerEXT(gVulkanInstance, gDebugMessenger, nullptr);
#endif #endif
vkDestroySurfaceKHR(gVulkanInstance, vulkan_state.surface, nullptr); vkDestroySurfaceKHR(gVulkanInstance, g_vulkan_state.surface, nullptr);
vkDestroyInstance(gVulkanInstance, nullptr); vkDestroyInstance(gVulkanInstance, nullptr);
} }
+3 -3
View File
@@ -29,21 +29,21 @@ namespace rse::graphics::vulkanbase
* @param state * @param state
* @return uint8_t 0 on success. Status code on failure * @return uint8_t 0 on success. Status code on failure
*/ */
uint8_t initVulkan(VulkanState& state); uint8_t initVulkan();
/** /**
* @brief Deinitialize Vulkan backend * @brief Deinitialize Vulkan backend
* *
* @param state * @param state
*/ */
void deinitVulkan(VulkanState& state); void deinitVulkan();
/** /**
* @brief Draw frame on the screen. * @brief Draw frame on the screen.
* *
* @param state * @param state
*/ */
void drawFrame(VulkanState& state); void drawFrame();
} /* namespace rse::graphics::vulkanbase */ } /* namespace rse::graphics::vulkanbase */
+88 -79
View File
@@ -36,15 +36,21 @@ std::vector<Buffer> gInstanceBuffers = {};
VkCommandPool gCommandPool = VK_NULL_HANDLE; VkCommandPool gCommandPool = VK_NULL_HANDLE;
static uint8_t createBuffer(VulkanState& vulkan_state, const VkDeviceSize size, VkBufferUsageFlags bufferUsage, VmaMemoryUsage memoryUsage, static uint8_t createBuffer(const VkDeviceSize size,
const VmaAllocationCreateFlags allocationFlags, Buffer& buffer); VkBufferUsageFlags bufferUsage,
static uint8_t copyBuffer(VulkanState& vulkan_state, VkBuffer src, VkBuffer dst, VkDeviceSize size, VkDeviceSize dest_offset = 0); VmaMemoryUsage memoryUsage,
const VmaAllocationCreateFlags allocationFlags,
Buffer& buffer);
static uint8_t copyBuffer(VkBuffer src,
VkBuffer dst,
VkDeviceSize size,
VkDeviceSize dest_offset = 0);
static uint8_t createCommandPools(VulkanState& vulkan_state); static uint8_t createCommandPools();
static uint8_t createVertexBuffer(VulkanState& vulkan_state); static uint8_t createVertexBuffer();
static uint8_t createIndexBuffer(VulkanState& vulkan_state); static uint8_t createIndexBuffer();
static uint8_t createUniformBuffers(VulkanState& vulkan_state); static uint8_t createUniformBuffers();
static uint8_t allocateCommandBuffers(VulkanState& vulkan_state); static uint8_t allocateCommandBuffers();
/** /**
* @brief Helper function. Creates a buffer object * @brief Helper function. Creates a buffer object
@@ -57,7 +63,7 @@ static uint8_t allocateCommandBuffers(VulkanState& vulkan_state);
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_COMMAND_BUFFER_ALLOCATION_FAILED * VULKAN_ERROR_COMMAND_BUFFER_ALLOCATION_FAILED
*/ */
uint8_t createBuffer(VulkanState& vulkan_state, const VkDeviceSize size, VkBufferUsageFlags bufferUsage, uint8_t createBuffer(const VkDeviceSize size, VkBufferUsageFlags bufferUsage,
VmaMemoryUsage memoryUsage, const VmaAllocationCreateFlags allocationFlags, Buffer& buffer) VmaMemoryUsage memoryUsage, const VmaAllocationCreateFlags allocationFlags, Buffer& buffer)
{ {
VkBufferCreateInfo vertexBufferInfo; VkBufferCreateInfo vertexBufferInfo;
@@ -82,7 +88,11 @@ uint8_t createBuffer(VulkanState& vulkan_state, const VkDeviceSize size, VkBuffe
createInfo.pUserData = VK_NULL_HANDLE; createInfo.pUserData = VK_NULL_HANDLE;
createInfo.priority = 0.0f; createInfo.priority = 0.0f;
if (VK_SUCCESS != vmaCreateBuffer(vulkan_state.allocator, &vertexBufferInfo, &createInfo, &buffer.buffer, &buffer.allocation, if (VK_SUCCESS != vmaCreateBuffer(g_vulkan_state.allocator,
&vertexBufferInfo,
&createInfo,
&buffer.buffer,
&buffer.allocation,
&buffer.allocationInfo)) { &buffer.allocationInfo)) {
LOGF << vulkanErrorMesssages[VULKAN_BUFFER_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_BUFFER_CREATION_FAILED];
return VULKAN_ERROR_BUFFER_CREATION_FAILED; return VULKAN_ERROR_BUFFER_CREATION_FAILED;
@@ -99,7 +109,7 @@ uint8_t createBuffer(VulkanState& vulkan_state, const VkDeviceSize size, VkBuffe
* @param size Size of buffer to copy * @param size Size of buffer to copy
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
*/ */
uint8_t copyBuffer(VulkanState& vulkan_state, VkBuffer src, VkBuffer dst, VkDeviceSize size, VkDeviceSize dest_offset) uint8_t copyBuffer(VkBuffer src, VkBuffer dst, VkDeviceSize size, VkDeviceSize dest_offset)
{ {
/* Vulkan buffers can only be copied using command buffers */ /* Vulkan buffers can only be copied using command buffers */
VkBufferCopy copyRegion; VkBufferCopy copyRegion;
@@ -135,14 +145,14 @@ uint8_t copyBuffer(VulkanState& vulkan_state, VkBuffer src, VkBuffer dst, VkDevi
// FIXME: At result checks // FIXME: At result checks
VkResult status; VkResult status;
status = vkAllocateCommandBuffers(vulkan_state.device, &allocInfo, &copyCommandBuffer); status = vkAllocateCommandBuffers(g_vulkan_state.device, &allocInfo, &copyCommandBuffer);
status = vkBeginCommandBuffer(copyCommandBuffer, &beginInfo); status = vkBeginCommandBuffer(copyCommandBuffer, &beginInfo);
vkCmdCopyBuffer(copyCommandBuffer, src, dst, 1, &copyRegion); vkCmdCopyBuffer(copyCommandBuffer, src, dst, 1, &copyRegion);
status = vkEndCommandBuffer(copyCommandBuffer); status = vkEndCommandBuffer(copyCommandBuffer);
status = vkQueueSubmit(vulkan_state.graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); status = vkQueueSubmit(g_vulkan_state.graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
/* TODO: Use fences to wait instead of idle */ /* TODO: Use fences to wait instead of idle */
status = vkQueueWaitIdle(vulkan_state.graphicsQueue); status = vkQueueWaitIdle(g_vulkan_state.graphicsQueue);
vkFreeCommandBuffers(vulkan_state.device, gCommandPool, 1, &copyCommandBuffer); vkFreeCommandBuffers(g_vulkan_state.device, gCommandPool, 1, &copyCommandBuffer);
if (status != VK_SUCCESS) { if (status != VK_SUCCESS) {
return VULKAN_ERROR_NO_ERROR; return VULKAN_ERROR_NO_ERROR;
@@ -158,16 +168,16 @@ uint8_t copyBuffer(VulkanState& vulkan_state, VkBuffer src, VkBuffer dst, VkDevi
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_COMMAND_POOL_CREATION_FAILED * VULKAN_ERROR_COMMAND_POOL_CREATION_FAILED
*/ */
uint8_t createCommandPools(VulkanState& vulkan_state) uint8_t createCommandPools()
{ {
VkCommandPoolCreateInfo createInfo; VkCommandPoolCreateInfo createInfo;
createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
createInfo.pNext = nullptr; createInfo.pNext = nullptr;
createInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; createInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
createInfo.queueFamilyIndex = vulkan_state.queueFamilyIndices[0]; /* TODO: Change when more families are needed */ createInfo.queueFamilyIndex = g_vulkan_state.queueFamilyIndices[0]; /* TODO: Change when more families are needed */
if (VK_SUCCESS != vkCreateCommandPool(vulkan_state.device, &createInfo, nullptr, &gCommandPool)) { if (VK_SUCCESS != vkCreateCommandPool(g_vulkan_state.device, &createInfo, nullptr, &gCommandPool)) {
LOGF << vulkanErrorMesssages[VULKAN_COMMAND_POOL_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_COMMAND_POOL_CREATION_FAILED];
return VULKAN_ERROR_COMMAND_POOL_CREATION_FAILED; return VULKAN_ERROR_COMMAND_POOL_CREATION_FAILED;
} }
@@ -183,7 +193,7 @@ uint8_t createCommandPools(VulkanState& vulkan_state)
* VULKAN_ERROR_BUFFER_CREATION_FAILED * VULKAN_ERROR_BUFFER_CREATION_FAILED
* VULKAN_ERROR_VERTEX_BUFFER_MAPPING_FAILED * VULKAN_ERROR_VERTEX_BUFFER_MAPPING_FAILED
*/ */
uint8_t createVertexBuffer(VulkanState& vulkan_state) uint8_t createVertexBuffer()
{ {
VkDeviceSize bufferSize; VkDeviceSize bufferSize;
Buffer vertex_buffer; Buffer vertex_buffer;
@@ -191,8 +201,10 @@ uint8_t createVertexBuffer(VulkanState& vulkan_state)
bufferSize = MAX_VERTEX_BUFFER_SIZE; bufferSize = MAX_VERTEX_BUFFER_SIZE;
/* Create Vertex Buffer*/ /* Create Vertex Buffer*/
if (VK_SUCCESS != createBuffer(vulkan_state, bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, if (VK_SUCCESS != createBuffer(bufferSize,
VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0, /* Will not be mapped with vmaMapMemory */ VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE,
0, /* Will not be mapped with vmaMapMemory */
vertex_buffer)) { vertex_buffer)) {
LOGF << vulkanErrorMesssages[VULKAN_BUFFER_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_BUFFER_CREATION_FAILED];
return VULKAN_ERROR_BUFFER_CREATION_FAILED; return VULKAN_ERROR_BUFFER_CREATION_FAILED;
@@ -209,7 +221,7 @@ uint8_t createVertexBuffer(VulkanState& vulkan_state)
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_BUFFER_CREATION_FAILED * VULKAN_ERROR_BUFFER_CREATION_FAILED
*/ */
uint8_t createIndexBuffer(VulkanState& vulkan_state) uint8_t createIndexBuffer()
{ {
VkDeviceSize bufferSize; VkDeviceSize bufferSize;
Buffer index_buffer; Buffer index_buffer;
@@ -217,8 +229,9 @@ uint8_t createIndexBuffer(VulkanState& vulkan_state)
bufferSize = MAX_VERTEX_BUFFER_SIZE; bufferSize = MAX_VERTEX_BUFFER_SIZE;
/* Create Index Buffer*/ /* Create Index Buffer*/
if (VK_SUCCESS != createBuffer(vulkan_state, bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, if (VK_SUCCESS != createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0, /* Will not be mapped with vmaMapMemory */ VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE,
0, /* Will not be mapped with vmaMapMemory */
index_buffer)) { index_buffer)) {
LOGF << vulkanErrorMesssages[VULKAN_BUFFER_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_BUFFER_CREATION_FAILED];
return VULKAN_ERROR_BUFFER_CREATION_FAILED; return VULKAN_ERROR_BUFFER_CREATION_FAILED;
@@ -229,7 +242,7 @@ uint8_t createIndexBuffer(VulkanState& vulkan_state)
return VULKAN_ERROR_NO_ERROR; return VULKAN_ERROR_NO_ERROR;
} }
uint8_t createInstanceBuffer(VulkanState& vulkan_state) uint8_t createInstanceBuffer()
{ {
VkDeviceSize bufferSize; VkDeviceSize bufferSize;
Buffer instance_buffer; Buffer instance_buffer;
@@ -238,7 +251,8 @@ uint8_t createInstanceBuffer(VulkanState& vulkan_state)
bufferSize = MAX_VERTEX_BUFFER_SIZE; bufferSize = MAX_VERTEX_BUFFER_SIZE;
/* Create Instance Buffer*/ /* Create Instance Buffer*/
if (VK_SUCCESS != createBuffer(vulkan_state, bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, if (VK_SUCCESS != createBuffer(bufferSize,
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0, /* Will not be mapped with vmaMapMemory */ VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0, /* Will not be mapped with vmaMapMemory */
instance_buffer)) { instance_buffer)) {
LOGF << vulkanErrorMesssages[VULKAN_BUFFER_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_BUFFER_CREATION_FAILED];
@@ -257,16 +271,16 @@ uint8_t createInstanceBuffer(VulkanState& vulkan_state)
* *
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
*/ */
uint8_t createUniformBuffers(VulkanState& vulkan_state) uint8_t createUniformBuffers()
{ {
VkDeviceSize bufferSize = sizeof(UniformBufferObject); VkDeviceSize bufferSize = sizeof(UniformBufferObject);
vulkan_state.uniformBuffers.resize(SWAP_BUFFER_COUNT); g_vulkan_state.uniformBuffers.resize(SWAP_BUFFER_COUNT);
for (size_t i = 0; i < SWAP_BUFFER_COUNT; i++) { for (size_t i = 0; i < SWAP_BUFFER_COUNT; i++) {
createBuffer(vulkan_state, bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_HOST, createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_HOST,
VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT, VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
vulkan_state.uniformBuffers[i]); g_vulkan_state.uniformBuffers[i]);
} }
return VULKAN_ERROR_NO_ERROR; return VULKAN_ERROR_NO_ERROR;
@@ -278,21 +292,21 @@ uint8_t createUniformBuffers(VulkanState& vulkan_state)
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_COMMAND_BUFFER_ALLOCATION_FAILED * VULKAN_ERROR_COMMAND_BUFFER_ALLOCATION_FAILED
*/ */
uint8_t allocateCommandBuffers(VulkanState& vulkan_state) uint8_t allocateCommandBuffers()
{ {
VkCommandBufferAllocateInfo allocateInfo; VkCommandBufferAllocateInfo allocateInfo;
/* Allocate memory for command buffers */ /* Allocate memory for command buffers */
vulkan_state.pCommandBuffers.resize(SWAP_BUFFER_COUNT); g_vulkan_state.pCommandBuffers.resize(SWAP_BUFFER_COUNT);
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocateInfo.pNext = nullptr; allocateInfo.pNext = nullptr;
allocateInfo.commandPool = gCommandPool; allocateInfo.commandPool = gCommandPool;
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; /* TODO: When needed, add secondary command buffer */ allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; /* TODO: When needed, add secondary command buffer */
allocateInfo.commandBufferCount = SWAP_BUFFER_COUNT; allocateInfo.commandBufferCount = SWAP_BUFFER_COUNT;
if (VK_SUCCESS != vkAllocateCommandBuffers(vulkan_state.device, if (VK_SUCCESS != vkAllocateCommandBuffers(g_vulkan_state.device,
&allocateInfo, &allocateInfo,
vulkan_state.pCommandBuffers.data())) { g_vulkan_state.pCommandBuffers.data())) {
LOGF << vulkanErrorMesssages[VULKAN_COMMAND_BUFFER_ALLOCATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_COMMAND_BUFFER_ALLOCATION_FAILED];
return VULKAN_ERROR_COMMAND_BUFFER_ALLOCATION_FAILED; return VULKAN_ERROR_COMMAND_BUFFER_ALLOCATION_FAILED;
} }
@@ -304,22 +318,21 @@ uint8_t allocateCommandBuffers(VulkanState& vulkan_state)
* *
* @param currentImage Current image index * @param currentImage Current image index
*/ */
void updateUniformBuffer(VulkanState& vulkan_state) void updateUniformBuffer()
{ {
UniformBufferObject ubo{}; UniformBufferObject ubo{};
// ubo.model = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f, 0.0f, 0.0f)) * glm::rotate(glm::mat4(1.0f), glm::radians(45.0f), glm::vec3(0.0f, 0.0f, 1.0f)) * glm::scale(glm::mat4(1.0f), glm::vec3(2, 1, 1)); // ubo.model = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f, 0.0f, 0.0f)) * glm::rotate(glm::mat4(1.0f), glm::radians(45.0f), glm::vec3(0.0f, 0.0f, 1.0f)) * glm::scale(glm::mat4(1.0f), glm::vec3(2, 1, 1));
ubo.model = glm::mat4(1.0f); ubo.model = glm::mat4(1.0f);
ubo.view = glm::lookAt(glm::vec3(0.0f, 0.0f, 5.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); ubo.view = glm::lookAt(glm::vec3(0.0f, 0.0f, 5.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
ubo.proj = glm::perspective(glm::radians(45.0f), ubo.proj = glm::perspective(glm::radians(45.0f),
vulkan_state.swapchainExtent.width / (float)vulkan_state.swapchainExtent.height, g_vulkan_state.swapchainExtent.width / (float)g_vulkan_state.swapchainExtent.height,
0.1f, 0.1f,
20.0f); 20.0f);
ubo.proj[1][1] *= -1; /* GLM was developed for OpenGL, where Y coordinate of the clip is inverted */ ubo.proj[1][1] *= -1; /* GLM was developed for OpenGL, where Y coordinate of the clip is inverted */
memcpy(vulkan_state.uniformBuffers[vulkan_state.currentFrame].allocationInfo.pMappedData, &ubo, sizeof(ubo)); memcpy(g_vulkan_state.uniformBuffers[g_vulkan_state.currentFrame].allocationInfo.pMappedData, &ubo, sizeof(ubo));
} }
uint8_t update_mesh_buffers(VulkanState& vulkan_state, uint8_t update_mesh_buffers( const std::vector<rse::graphics::vulkanbase::Vertex>& vertices,
const std::vector<rse::graphics::vulkanbase::Vertex>& vertices,
const std::vector<uint16_t>& indices) const std::vector<uint16_t>& indices)
{ {
// void* mapped_data; // void* mapped_data;
@@ -331,8 +344,7 @@ uint8_t update_mesh_buffers(VulkanState& vulkan_state,
auto last_index_buffer = &gIndexBuffers[gIndexBuffers.size() - 1]; auto last_index_buffer = &gIndexBuffers[gIndexBuffers.size() - 1];
/* Creating staging buffer*/ /* Creating staging buffer*/
if (VK_SUCCESS != createBuffer(vulkan_state, if (VK_SUCCESS != createBuffer(MAX_VERTEX_BUFFER_SIZE,
MAX_VERTEX_BUFFER_SIZE,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VMA_MEMORY_USAGE_AUTO_PREFER_HOST, VMA_MEMORY_USAGE_AUTO_PREFER_HOST,
VMA_ALLOCATION_CREATE_MAPPED_BIT VMA_ALLOCATION_CREATE_MAPPED_BIT
@@ -345,8 +357,7 @@ uint8_t update_mesh_buffers(VulkanState& vulkan_state,
/* Fill staging buffer with vertex data */ /* Fill staging buffer with vertex data */
memset(staging_buffer.allocationInfo.pMappedData, 0, MAX_VERTEX_BUFFER_SIZE); memset(staging_buffer.allocationInfo.pMappedData, 0, MAX_VERTEX_BUFFER_SIZE);
memcpy(staging_buffer.allocationInfo.pMappedData, vertices.data(), vertices_size); memcpy(staging_buffer.allocationInfo.pMappedData, vertices.data(), vertices_size);
copyBuffer(vulkan_state, copyBuffer(staging_buffer.buffer,
staging_buffer.buffer,
last_vertex_buffer->buffer, last_vertex_buffer->buffer,
vertices_size, vertices_size,
last_vertex_buffer->allocated_size); last_vertex_buffer->allocated_size);
@@ -354,19 +365,17 @@ uint8_t update_mesh_buffers(VulkanState& vulkan_state,
/* Fill staging buffer with index data */ /* Fill staging buffer with index data */
memset(staging_buffer.allocationInfo.pMappedData, 0, MAX_VERTEX_BUFFER_SIZE); memset(staging_buffer.allocationInfo.pMappedData, 0, MAX_VERTEX_BUFFER_SIZE);
memcpy(staging_buffer.allocationInfo.pMappedData, indices.data(), indices_size); memcpy(staging_buffer.allocationInfo.pMappedData, indices.data(), indices_size);
copyBuffer(vulkan_state, copyBuffer(staging_buffer.buffer,
staging_buffer.buffer,
last_index_buffer->buffer, last_index_buffer->buffer,
indices_size, indices_size,
last_index_buffer->allocated_size); last_index_buffer->allocated_size);
vmaDestroyBuffer(vulkan_state.allocator, staging_buffer.buffer, staging_buffer.allocation); vmaDestroyBuffer(g_vulkan_state.allocator, staging_buffer.buffer, staging_buffer.allocation);
return VULKAN_ERROR_NO_ERROR; return VULKAN_ERROR_NO_ERROR;
} }
uint8_t update_mesh_instances(VulkanState& vulkan_state, uint8_t update_mesh_instances( rse::graphics::mesh::InstanceData& instance_data)
rse::graphics::mesh::InstanceData& instance_data)
{ {
Buffer staging_buffer; Buffer staging_buffer;
@@ -374,8 +383,7 @@ uint8_t update_mesh_instances(VulkanState& vulkan_state,
auto instance_size = sizeof(instance_data); auto instance_size = sizeof(instance_data);
/* Creating staging buffer*/ /* Creating staging buffer*/
if (VK_SUCCESS != createBuffer(vulkan_state, if (VK_SUCCESS != createBuffer(MAX_VERTEX_BUFFER_SIZE,
MAX_VERTEX_BUFFER_SIZE,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VMA_MEMORY_USAGE_AUTO_PREFER_HOST, VMA_MEMORY_USAGE_AUTO_PREFER_HOST,
VMA_ALLOCATION_CREATE_MAPPED_BIT VMA_ALLOCATION_CREATE_MAPPED_BIT
@@ -386,13 +394,12 @@ uint8_t update_mesh_instances(VulkanState& vulkan_state,
} }
memcpy(staging_buffer.allocationInfo.pMappedData, &instance_data, instance_size); memcpy(staging_buffer.allocationInfo.pMappedData, &instance_data, instance_size);
copyBuffer(vulkan_state, copyBuffer(staging_buffer.buffer,
staging_buffer.buffer,
last_instance_buffer->buffer, last_instance_buffer->buffer,
instance_size, instance_size,
last_instance_buffer->allocated_size); last_instance_buffer->allocated_size);
vmaDestroyBuffer(vulkan_state.allocator, staging_buffer.buffer, staging_buffer.allocation); vmaDestroyBuffer(g_vulkan_state.allocator, staging_buffer.buffer, staging_buffer.allocation);
last_instance_buffer->allocated_size += instance_size; last_instance_buffer->allocated_size += instance_size;
@@ -407,9 +414,9 @@ uint8_t update_mesh_instances(VulkanState& vulkan_state,
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_RECORD_COMMAND_BUFFER_FAILED * VULKAN_ERROR_RECORD_COMMAND_BUFFER_FAILED
*/ */
uint8_t recordCommandBuffer(VulkanState& vulkan_state, uint32_t imageIndex) uint8_t recordCommandBuffer(uint32_t imageIndex)
{ {
VkCommandBuffer commandBuffer = vulkan_state.pCommandBuffers[vulkan_state.currentFrame]; VkCommandBuffer commandBuffer = g_vulkan_state.pCommandBuffers[g_vulkan_state.currentFrame];
VkCommandBufferBeginInfo beginInfo{}; VkCommandBufferBeginInfo beginInfo{};
VkClearValue clearColor = {{{0.0f, 0.0f, 0.0f, 1.0f}}}; VkClearValue clearColor = {{{0.0f, 0.0f, 0.0f, 1.0f}}};
VkRenderPassBeginInfo renderPassInfo{}; VkRenderPassBeginInfo renderPassInfo{};
@@ -430,26 +437,26 @@ uint8_t recordCommandBuffer(VulkanState& vulkan_state, uint32_t imageIndex)
} }
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = vulkan_state.renderPass; renderPassInfo.renderPass = g_vulkan_state.renderPass;
renderPassInfo.framebuffer = vulkan_state.swapchainFramebuffers[imageIndex]; renderPassInfo.framebuffer = g_vulkan_state.swapchainFramebuffers[imageIndex];
renderPassInfo.renderArea.offset = {0, 0}; renderPassInfo.renderArea.offset = {0, 0};
renderPassInfo.renderArea.extent = vulkan_state.swapchainExtent; renderPassInfo.renderArea.extent = g_vulkan_state.swapchainExtent;
renderPassInfo.clearValueCount = 1; renderPassInfo.clearValueCount = 1;
renderPassInfo.pClearValues = &clearColor; renderPassInfo.pClearValues = &clearColor;
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, vulkan_state.graphicsPipeline); vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_vulkan_state.graphicsPipeline);
viewport.x = 0.0f; viewport.x = 0.0f;
viewport.y = 0.0f; viewport.y = 0.0f;
viewport.width = static_cast<float>(vulkan_state.swapchainExtent.width); viewport.width = static_cast<float>(g_vulkan_state.swapchainExtent.width);
viewport.height = static_cast<float>(vulkan_state.swapchainExtent.height); viewport.height = static_cast<float>(g_vulkan_state.swapchainExtent.height);
viewport.minDepth = 0.0f; viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f; viewport.maxDepth = 1.0f;
vkCmdSetViewport(commandBuffer, 0, 1, &viewport); vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
scissor.offset = {0, 0}; scissor.offset = {0, 0};
scissor.extent = vulkan_state.swapchainExtent; scissor.extent = g_vulkan_state.swapchainExtent;
vkCmdSetScissor(commandBuffer, 0, 1, &scissor); vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
for (size_t i = 0; i < gVertexBuffers.size(); ++i) { for (size_t i = 0; i < gVertexBuffers.size(); ++i) {
@@ -461,8 +468,8 @@ uint8_t recordCommandBuffer(VulkanState& vulkan_state, uint32_t imageIndex)
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets); vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
vkCmdBindVertexBuffers(commandBuffer, 1, 1, instanceBuffers, offsets); vkCmdBindVertexBuffers(commandBuffer, 1, 1, instanceBuffers, offsets);
vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT16); vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT16);
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, vulkan_state.pipelineLayout, 0, 1, vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_vulkan_state.pipelineLayout, 0, 1,
&vulkan_state.descriptorSets[vulkan_state.currentFrame], 0, nullptr); &g_vulkan_state.descriptorSets[g_vulkan_state.currentFrame], 0, nullptr);
vkCmdDrawIndexed(commandBuffer, static_cast<uint32_t>(indices.size()), instances.size(), 0, 0, 0); vkCmdDrawIndexed(commandBuffer, static_cast<uint32_t>(indices.size()), instances.size(), 0, 0, 0);
} }
@@ -477,37 +484,39 @@ uint8_t recordCommandBuffer(VulkanState& vulkan_state, uint32_t imageIndex)
return VULKAN_ERROR_NO_ERROR; return VULKAN_ERROR_NO_ERROR;
} }
uint8_t create_buffers(VulkanState& vulkan_state) uint8_t create_buffers()
{ {
uint8_t status = VULKAN_ERROR_NO_ERROR;\ uint8_t status = VULKAN_ERROR_NO_ERROR;\
STATUS_CHECK(createCommandPools(vulkan_state)); STATUS_CHECK(createCommandPools());
STATUS_CHECK(createVertexBuffer(vulkan_state)); STATUS_CHECK(createVertexBuffer());
STATUS_CHECK(createIndexBuffer(vulkan_state)); STATUS_CHECK(createIndexBuffer());
STATUS_CHECK(createInstanceBuffer(vulkan_state)); STATUS_CHECK(createInstanceBuffer());
STATUS_CHECK(createUniformBuffers(vulkan_state)); STATUS_CHECK(createUniformBuffers());
STATUS_CHECK(allocateCommandBuffers(vulkan_state)); STATUS_CHECK(allocateCommandBuffers());
return status; return status;
} }
void reset_command_buffer(VulkanState& vulkan_state) void reset_command_buffer()
{ {
vkResetCommandBuffer(vulkan_state.pCommandBuffers[vulkan_state.currentFrame], /*VkCommandBufferResetFlagBits*/ 0); vkResetCommandBuffer(g_vulkan_state.pCommandBuffers[g_vulkan_state.currentFrame], /*VkCommandBufferResetFlagBits*/ 0);
} }
void destroy_buffers(VulkanState& vulkan_state) void destroy_buffers()
{ {
for (size_t i = 0; i < SWAP_BUFFER_COUNT; ++i) { for (size_t i = 0; i < SWAP_BUFFER_COUNT; ++i) {
vmaDestroyBuffer(vulkan_state.allocator, vulkan_state.uniformBuffers[i].buffer, vulkan_state.uniformBuffers[i].allocation); vmaDestroyBuffer(g_vulkan_state.allocator,
g_vulkan_state.uniformBuffers[i].buffer,
g_vulkan_state.uniformBuffers[i].allocation);
} }
for (size_t i = 0; i < gVertexBuffers.size(); ++i) { for (size_t i = 0; i < gVertexBuffers.size(); ++i) {
vmaDestroyBuffer(vulkan_state.allocator, gVertexBuffers[i].buffer, gVertexBuffers[i].allocation); vmaDestroyBuffer(g_vulkan_state.allocator, gVertexBuffers[i].buffer, gVertexBuffers[i].allocation);
vmaDestroyBuffer(vulkan_state.allocator, gIndexBuffers[i].buffer, gIndexBuffers[i].allocation); vmaDestroyBuffer(g_vulkan_state.allocator, gIndexBuffers[i].buffer, gIndexBuffers[i].allocation);
vmaDestroyBuffer(vulkan_state.allocator, gInstanceBuffers[i].buffer, gInstanceBuffers[i].allocation); vmaDestroyBuffer(g_vulkan_state.allocator, gInstanceBuffers[i].buffer, gInstanceBuffers[i].allocation);
} }
vkDestroyCommandPool(vulkan_state.device, gCommandPool, nullptr); vkDestroyCommandPool(g_vulkan_state.device, gCommandPool, nullptr);
} }
} }
+7 -9
View File
@@ -18,21 +18,19 @@ namespace rse::graphics::vulkanbase
* @param vulkan_state * @param vulkan_state
* @return uint8_t * @return uint8_t
*/ */
uint8_t create_buffers(VulkanState& vulkan_state); uint8_t create_buffers();
/** /**
* @brief Update uniform buffers * @brief Update uniform buffers
* *
* @param vulkan_state * @param vulkan_state
*/ */
void updateUniformBuffer(VulkanState& vulkan_state); void updateUniformBuffer();
uint8_t update_mesh_buffers(VulkanState& vulkan_state, uint8_t update_mesh_buffers(const std::vector<rse::graphics::vulkanbase::Vertex>& vertices,
const std::vector<rse::graphics::vulkanbase::Vertex>& vertices,
const std::vector<uint16_t>& indices); const std::vector<uint16_t>& indices);
uint8_t update_mesh_instances(VulkanState& vulkan_state, uint8_t update_mesh_instances(rse::graphics::mesh::InstanceData& instance_data);
rse::graphics::mesh::InstanceData& instance_data);
/** /**
* @brief Record commands for given image index * @brief Record commands for given image index
* *
@@ -40,21 +38,21 @@ uint8_t update_mesh_instances(VulkanState& vulkan_state,
* @param imageIndex * @param imageIndex
* @return uint8_t * @return uint8_t
*/ */
uint8_t recordCommandBuffer(VulkanState& vulkan_state, uint32_t imageIndex); uint8_t recordCommandBuffer(uint32_t imageIndex);
/** /**
* @brief Reset command buffer for current frame. * @brief Reset command buffer for current frame.
* *
* @param vulkan_state * @param vulkan_state
*/ */
void reset_command_buffer(VulkanState& vulkan_state); void reset_command_buffer();
/** /**
* @brief Destroy ALL previously allocated buffers * @brief Destroy ALL previously allocated buffers
* *
* @param vulkan_state * @param vulkan_state
*/ */
void destroy_buffers(VulkanState& vulkan_state); void destroy_buffers();
} // namespace rse::graphics::vulkanbase } // namespace rse::graphics::vulkanbase
+7
View File
@@ -0,0 +1,7 @@
#include "vulkan_commons.hpp"
namespace rse::graphics::vulkanbase {
VulkanState g_vulkan_state;
}
+3
View File
@@ -100,6 +100,9 @@ struct VulkanState
VkSwapchainKHR swapchain; VkSwapchainKHR swapchain;
}; };
extern VulkanState g_vulkan_state;
} // namespace rse::graphics::vulkanbase } // namespace rse::graphics::vulkanbase
namespace rse::graphics::mesh namespace rse::graphics::mesh
+17 -17
View File
@@ -11,8 +11,8 @@ using namespace locale::vulkan;
VkDescriptorPool gDescriptorPool = {}; VkDescriptorPool gDescriptorPool = {};
static uint8_t createDescriptorPool(VulkanState& vulkan_state); static uint8_t createDescriptorPool();
static uint8_t createDescriptorSets(VulkanState& vulkan_state); static uint8_t createDescriptorSets();
/** /**
* @brief Create a Descriptor Pool * @brief Create a Descriptor Pool
@@ -20,7 +20,7 @@ static uint8_t createDescriptorSets(VulkanState& vulkan_state);
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_DESCRIPTOR_POOL_CREATION_FAILED * VULKAN_ERROR_DESCRIPTOR_POOL_CREATION_FAILED
*/ */
uint8_t createDescriptorPool(VulkanState& vulkan_state) uint8_t createDescriptorPool()
{ {
VkDescriptorPoolSize poolSize{}; VkDescriptorPoolSize poolSize{};
poolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; poolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
@@ -32,7 +32,7 @@ uint8_t createDescriptorPool(VulkanState& vulkan_state)
poolInfo.pPoolSizes = &poolSize; poolInfo.pPoolSizes = &poolSize;
poolInfo.maxSets = static_cast<uint32_t>(SWAP_BUFFER_COUNT); poolInfo.maxSets = static_cast<uint32_t>(SWAP_BUFFER_COUNT);
if (vkCreateDescriptorPool(vulkan_state.device, &poolInfo, nullptr, &gDescriptorPool) != VK_SUCCESS) { if (vkCreateDescriptorPool(g_vulkan_state.device, &poolInfo, nullptr, &gDescriptorPool) != VK_SUCCESS) {
LOGF << vulkanErrorMesssages[VULKAN_DESCRIPTOR_POOL_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_DESCRIPTOR_POOL_CREATION_FAILED];
return VULKAN_ERROR_DESCRIPTOR_POOL_CREATION_FAILED; return VULKAN_ERROR_DESCRIPTOR_POOL_CREATION_FAILED;
} }
@@ -45,56 +45,56 @@ uint8_t createDescriptorPool(VulkanState& vulkan_state)
* *
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_DESCRIPTOR_SETS_ALLOCATION_FAILED */ * VULKAN_ERROR_DESCRIPTOR_SETS_ALLOCATION_FAILED */
uint8_t createDescriptorSets(VulkanState& vulkan_state) uint8_t createDescriptorSets()
{ {
std::vector<VkDescriptorSetLayout> layouts(SWAP_BUFFER_COUNT, vulkan_state.descriptorSetLayout); std::vector<VkDescriptorSetLayout> layouts(SWAP_BUFFER_COUNT, g_vulkan_state.descriptorSetLayout);
VkDescriptorSetAllocateInfo allocInfo{}; VkDescriptorSetAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = gDescriptorPool; allocInfo.descriptorPool = gDescriptorPool;
allocInfo.descriptorSetCount = static_cast<uint32_t>(SWAP_BUFFER_COUNT); allocInfo.descriptorSetCount = static_cast<uint32_t>(SWAP_BUFFER_COUNT);
allocInfo.pSetLayouts = layouts.data(); allocInfo.pSetLayouts = layouts.data();
vulkan_state.descriptorSets.resize(SWAP_BUFFER_COUNT); g_vulkan_state.descriptorSets.resize(SWAP_BUFFER_COUNT);
if (vkAllocateDescriptorSets(vulkan_state.device, &allocInfo, vulkan_state.descriptorSets.data()) != VK_SUCCESS) { if (vkAllocateDescriptorSets(g_vulkan_state.device, &allocInfo, g_vulkan_state.descriptorSets.data()) != VK_SUCCESS) {
LOGF << vulkanErrorMesssages[VULKAN_DESCRIPTOR_SETS_ALLOCATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_DESCRIPTOR_SETS_ALLOCATION_FAILED];
return VULKAN_ERROR_DESCRIPTOR_SETS_ALLOCATION_FAILED; return VULKAN_ERROR_DESCRIPTOR_SETS_ALLOCATION_FAILED;
} }
for (size_t i = 0; i < SWAP_BUFFER_COUNT; i++) { for (size_t i = 0; i < SWAP_BUFFER_COUNT; i++) {
VkDescriptorBufferInfo bufferInfo{}; VkDescriptorBufferInfo bufferInfo{};
bufferInfo.buffer = vulkan_state.uniformBuffers[i].buffer; bufferInfo.buffer = g_vulkan_state.uniformBuffers[i].buffer;
bufferInfo.offset = 0; bufferInfo.offset = 0;
bufferInfo.range = sizeof(UniformBufferObject); bufferInfo.range = sizeof(UniformBufferObject);
VkWriteDescriptorSet descriptorWrite{}; VkWriteDescriptorSet descriptorWrite{};
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrite.dstSet = vulkan_state.descriptorSets[i]; descriptorWrite.dstSet = g_vulkan_state.descriptorSets[i];
descriptorWrite.dstBinding = 0; descriptorWrite.dstBinding = 0;
descriptorWrite.dstArrayElement = 0; descriptorWrite.dstArrayElement = 0;
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptorWrite.descriptorCount = 1; descriptorWrite.descriptorCount = 1;
descriptorWrite.pBufferInfo = &bufferInfo; descriptorWrite.pBufferInfo = &bufferInfo;
vkUpdateDescriptorSets(vulkan_state.device, 1, &descriptorWrite, 0, nullptr); vkUpdateDescriptorSets(g_vulkan_state.device, 1, &descriptorWrite, 0, nullptr);
} }
return VULKAN_ERROR_NO_ERROR; return VULKAN_ERROR_NO_ERROR;
} }
uint8_t create_descriptors(VulkanState& vulkan_state) uint8_t create_descriptors()
{ {
uint8_t status = VULKAN_ERROR_NO_ERROR; uint8_t status = VULKAN_ERROR_NO_ERROR;
STATUS_CHECK(createDescriptorPool(vulkan_state)); STATUS_CHECK(createDescriptorPool());
STATUS_CHECK(createDescriptorSets(vulkan_state)); STATUS_CHECK(createDescriptorSets());
return status; return status;
} }
void destroy_descriptors(VulkanState& vulkan_state) void destroy_descriptors()
{ {
vkDestroyDescriptorPool(vulkan_state.device, gDescriptorPool, nullptr); vkDestroyDescriptorPool(g_vulkan_state.device, gDescriptorPool, nullptr);
vkDestroyDescriptorSetLayout(vulkan_state.device, vulkan_state.descriptorSetLayout, nullptr); vkDestroyDescriptorSetLayout(g_vulkan_state.device, g_vulkan_state.descriptorSetLayout, nullptr);
} }
} // namespace rse::graphics::vulkanbase } // namespace rse::graphics::vulkanbase
+2 -2
View File
@@ -16,14 +16,14 @@ namespace rse::graphics::vulkanbase
* @param vulkan_state * @param vulkan_state
* @return uint8_t * @return uint8_t
*/ */
uint8_t create_descriptors(VulkanState& vulkan_state); uint8_t create_descriptors();
/** /**
* @brief Destroy all descriptors * @brief Destroy all descriptors
* *
* @param vulkan_state * @param vulkan_state
*/ */
void destroy_descriptors(VulkanState& vulkan_state); void destroy_descriptors();
} // namespace rse::graphics::vulkanbase } // namespace rse::graphics::vulkanbase
+78 -78
View File
@@ -41,23 +41,23 @@ const std::map<ShaderStage, std::string> gShaderPaths =
"../graphics/shaders/shader.frag.num", "../graphics/shaders/shader.frag.num",
}}; }};
static std::optional<VkShaderModule> createShaderModule(VulkanState& state, const std::vector<char>& binaryData); static std::optional<VkShaderModule> createShaderModule(const std::vector<char>& binaryData);
static uint8_t createSwapchain(VulkanState& vulkan_state); static uint8_t createSwapchain();
static uint8_t createSwapchainImageViews(VulkanState& vulkan_state); static uint8_t createSwapchainImageViews();
static uint8_t createRenderPass(VulkanState& vulkan_state); static uint8_t createRenderPass();
static uint8_t createDescriptorSetLayout(VulkanState& vulkan_state); static uint8_t createDescriptorSetLayout();
static uint8_t createPipelineLayout(VulkanState& vulkan_state); static uint8_t createPipelineLayout();
static uint8_t createGraphicsPipeline(VulkanState& vulkan_state); static uint8_t createGraphicsPipeline();
static uint8_t createFrambuffers(VulkanState& vulkan_state); static uint8_t createFrambuffers();
static uint8_t fillShaderStages(VulkanState& vulkan_state, std::vector<VkPipelineShaderStageCreateInfo>* shaderStages); static uint8_t fillShaderStages(std::vector<VkPipelineShaderStageCreateInfo>* shaderStages);
static void fillDynamicPipelineStateInfo(VkPipelineDynamicStateCreateInfo* dynamicState); static void fillDynamicPipelineStateInfo(VkPipelineDynamicStateCreateInfo* dynamicState);
static void freeDynamicPipelineStateInfo(VkPipelineDynamicStateCreateInfo* dynamicState); static void freeDynamicPipelineStateInfo(VkPipelineDynamicStateCreateInfo* dynamicState);
static void fillVertexInputInfo(VkPipelineVertexInputStateCreateInfo* vertexInputInfo); static void fillVertexInputInfo(VkPipelineVertexInputStateCreateInfo* vertexInputInfo);
static void freeVertexInputInfo(VkPipelineVertexInputStateCreateInfo* vertexInputInfo); static void freeVertexInputInfo(VkPipelineVertexInputStateCreateInfo* vertexInputInfo);
static void fillInputAssemblyInfo(VkPipelineInputAssemblyStateCreateInfo* inputAssembly); static void fillInputAssemblyInfo(VkPipelineInputAssemblyStateCreateInfo* inputAssembly);
static void fillViewportStateInfo(VulkanState& vulkan_state, VkPipelineViewportStateCreateInfo* viewportState); static void fillViewportStateInfo(VkPipelineViewportStateCreateInfo* viewportState);
static void freeViewportStateInfo(VkPipelineViewportStateCreateInfo* viewportState); static void freeViewportStateInfo(VkPipelineViewportStateCreateInfo* viewportState);
static void fillRasterizationInfo(VkPipelineRasterizationStateCreateInfo* rasterizer); static void fillRasterizationInfo(VkPipelineRasterizationStateCreateInfo* rasterizer);
static void fillMultisamplingStateInfo(VkPipelineMultisampleStateCreateInfo* multisampling); static void fillMultisamplingStateInfo(VkPipelineMultisampleStateCreateInfo* multisampling);
@@ -70,7 +70,7 @@ static void freeColorBlendInfo(VkPipelineColorBlendStateCreateInfo* colorBlendin
* @param binaryData Shader compiled code * @param binaryData Shader compiled code
* @return std::optional<VkShaderModule> Handle to compiled shader module or nullopt * @return std::optional<VkShaderModule> Handle to compiled shader module or nullopt
*/ */
std::optional<VkShaderModule> createShaderModule(VulkanState& vulkan_state, const std::vector<char>& binaryData) std::optional<VkShaderModule> createShaderModule(const std::vector<char>& binaryData)
{ {
VkShaderModule shaderModule; VkShaderModule shaderModule;
VkShaderModuleCreateInfo createInfo{}; VkShaderModuleCreateInfo createInfo{};
@@ -80,7 +80,7 @@ std::optional<VkShaderModule> createShaderModule(VulkanState& vulkan_state, cons
createInfo.codeSize = binaryData.size(); createInfo.codeSize = binaryData.size();
createInfo.pCode = reinterpret_cast<const uint32_t*>(binaryData.data()); createInfo.pCode = reinterpret_cast<const uint32_t*>(binaryData.data());
if (VK_SUCCESS != vkCreateShaderModule(vulkan_state.device, &createInfo, nullptr, &shaderModule)) { if (VK_SUCCESS != vkCreateShaderModule(g_vulkan_state.device, &createInfo, nullptr, &shaderModule)) {
LOGF << vulkanErrorMesssages[VULKAN_SHADER_UNABLE_TO_CREATE_SHADER]; LOGF << vulkanErrorMesssages[VULKAN_SHADER_UNABLE_TO_CREATE_SHADER];
return std::nullopt; return std::nullopt;
} }
@@ -94,48 +94,48 @@ std::optional<VkShaderModule> createShaderModule(VulkanState& vulkan_state, cons
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_SWAPCHAIN_CREATION_FAILED * VULKAN_ERROR_SWAPCHAIN_CREATION_FAILED
*/ */
uint8_t createSwapchain(VulkanState& vulkan_state) uint8_t createSwapchain()
{ {
VkSwapchainCreateInfoKHR createInfo; VkSwapchainCreateInfoKHR createInfo;
VkSurfaceCapabilitiesKHR physicalDeviceSurfaceCapabilities; VkSurfaceCapabilitiesKHR physicalDeviceSurfaceCapabilities;
VkBool32 surfaceSupported; VkBool32 surfaceSupported;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vulkan_state.physicalDevice, vulkan_state.surface, &physicalDeviceSurfaceCapabilities); vkGetPhysicalDeviceSurfaceCapabilitiesKHR(g_vulkan_state.physicalDevice, g_vulkan_state.surface, &physicalDeviceSurfaceCapabilities);
/* Store extent in global variable, for later use */ /* Store extent in global variable, for later use */
vulkan_state.swapchainExtent = physicalDeviceSurfaceCapabilities.currentExtent; g_vulkan_state.swapchainExtent = physicalDeviceSurfaceCapabilities.currentExtent;
/* Check if device supports surface for presentation */ /* Check if device supports surface for presentation */
vkGetPhysicalDeviceSurfaceSupportKHR(vulkan_state.physicalDevice, vulkan_state.queueFamilyIndices[0], vulkan_state.surface, &surfaceSupported); vkGetPhysicalDeviceSurfaceSupportKHR(g_vulkan_state.physicalDevice, g_vulkan_state.queueFamilyIndices[0], g_vulkan_state.surface, &surfaceSupported);
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
createInfo.pNext = nullptr; createInfo.pNext = nullptr;
createInfo.flags = 0U; createInfo.flags = 0U;
createInfo.surface = vulkan_state.surface; createInfo.surface = g_vulkan_state.surface;
createInfo.minImageCount = SWAP_BUFFER_COUNT + 1; createInfo.minImageCount = SWAP_BUFFER_COUNT + 1;
createInfo.imageFormat = IMAGE_FORMAT; /* TODO: Check for supported formats */ createInfo.imageFormat = IMAGE_FORMAT; /* TODO: Check for supported formats */
createInfo.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; /* TODO: Check for supported color space */ createInfo.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; /* TODO: Check for supported color space */
createInfo.imageExtent = vulkan_state.swapchainExtent; createInfo.imageExtent = g_vulkan_state.swapchainExtent;
createInfo.imageArrayLayers = 1U; /* For non-stereoscopic-3D applications, this value is 1. */ createInfo.imageArrayLayers = 1U; /* For non-stereoscopic-3D applications, this value is 1. */
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; /* TODO: Probably change later? Dunno */ createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; /* TODO: Probably change later? Dunno */
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; /* Using only one queue family, so this is ok */ createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; /* Using only one queue family, so this is ok */
createInfo.queueFamilyIndexCount = 1U; createInfo.queueFamilyIndexCount = 1U;
createInfo.pQueueFamilyIndices = vulkan_state.queueFamilyIndices; createInfo.pQueueFamilyIndices = g_vulkan_state.queueFamilyIndices;
createInfo.preTransform = physicalDeviceSurfaceCapabilities.currentTransform; createInfo.preTransform = physicalDeviceSurfaceCapabilities.currentTransform;
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
createInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR; /* TODO: Choose present mode from VkPresentModeKHR */ createInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR; /* TODO: Choose present mode from VkPresentModeKHR */
createInfo.clipped = VK_TRUE; createInfo.clipped = VK_TRUE;
createInfo.oldSwapchain = VK_NULL_HANDLE; createInfo.oldSwapchain = VK_NULL_HANDLE;
if (VK_SUCCESS != vkCreateSwapchainKHR(vulkan_state.device, &createInfo, nullptr, &vulkan_state.swapchain)) { if (VK_SUCCESS != vkCreateSwapchainKHR(g_vulkan_state.device, &createInfo, nullptr, &g_vulkan_state.swapchain)) {
LOGF << vulkanErrorMesssages[VULKAN_SWAPCHAIN_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_SWAPCHAIN_CREATION_FAILED];
return VULKAN_ERROR_SWAPCHAIN_CREATION_FAILED; return VULKAN_ERROR_SWAPCHAIN_CREATION_FAILED;
} }
/* Obtain swapchain images */ /* Obtain swapchain images */
vkGetSwapchainImagesKHR(vulkan_state.device, vulkan_state.swapchain, &gSwapchainImagesCount, nullptr); vkGetSwapchainImagesKHR(g_vulkan_state.device, g_vulkan_state.swapchain, &gSwapchainImagesCount, nullptr);
gSwapChainImages.resize(gSwapchainImagesCount); gSwapChainImages.resize(gSwapchainImagesCount);
vkGetSwapchainImagesKHR(vulkan_state.device, vulkan_state.swapchain, &gSwapchainImagesCount, gSwapChainImages.data()); vkGetSwapchainImagesKHR(g_vulkan_state.device, g_vulkan_state.swapchain, &gSwapchainImagesCount, gSwapChainImages.data());
return VULKAN_ERROR_NO_ERROR; return VULKAN_ERROR_NO_ERROR;
} }
@@ -145,9 +145,9 @@ uint8_t createSwapchain(VulkanState& vulkan_state)
* *
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
*/ */
uint8_t createSwapchainImageViews(VulkanState& vulkan_state) uint8_t createSwapchainImageViews()
{ {
vulkan_state.swapChainImageViews.resize(gSwapchainImagesCount); g_vulkan_state.swapChainImageViews.resize(gSwapchainImagesCount);
for (size_t i = 0; i < gSwapchainImagesCount; i++) { for (size_t i = 0; i < gSwapchainImagesCount; i++) {
VkImageViewCreateInfo createInfo; VkImageViewCreateInfo createInfo;
@@ -167,7 +167,7 @@ uint8_t createSwapchainImageViews(VulkanState& vulkan_state)
createInfo.subresourceRange.baseArrayLayer = 0U; createInfo.subresourceRange.baseArrayLayer = 0U;
createInfo.subresourceRange.layerCount = 1U; createInfo.subresourceRange.layerCount = 1U;
if (VK_SUCCESS != vkCreateImageView(vulkan_state.device, &createInfo, nullptr, &vulkan_state.swapChainImageViews[i])) { if (VK_SUCCESS != vkCreateImageView(g_vulkan_state.device, &createInfo, nullptr, &g_vulkan_state.swapChainImageViews[i])) {
LOGF << vulkanErrorMesssages[VULKAN_SWAPCHAIN_IMVIEW_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_SWAPCHAIN_IMVIEW_CREATION_FAILED];
return VULKAN_ERROR_SWAPCHAIN_IMVIEW_CREATION_FAILED; return VULKAN_ERROR_SWAPCHAIN_IMVIEW_CREATION_FAILED;
} }
@@ -182,7 +182,7 @@ uint8_t createSwapchainImageViews(VulkanState& vulkan_state)
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_RENDERPASS_CREATION_FAILED * VULKAN_ERROR_RENDERPASS_CREATION_FAILED
*/ */
uint8_t createRenderPass(VulkanState& vulkan_state) uint8_t createRenderPass()
{ {
VkAttachmentDescription colorAttachment{}; VkAttachmentDescription colorAttachment{};
colorAttachment.flags = 0U; colorAttachment.flags = 0U;
@@ -211,7 +211,7 @@ uint8_t createRenderPass(VulkanState& vulkan_state)
renderPassInfo.subpassCount = 1; renderPassInfo.subpassCount = 1;
renderPassInfo.pSubpasses = &subpass; renderPassInfo.pSubpasses = &subpass;
if (VK_SUCCESS != vkCreateRenderPass(vulkan_state.device, &renderPassInfo, nullptr, &vulkan_state.renderPass)) { if (VK_SUCCESS != vkCreateRenderPass(g_vulkan_state.device, &renderPassInfo, nullptr, &g_vulkan_state.renderPass)) {
LOGF << vulkanErrorMesssages[VULKAN_RENDERPASS_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_RENDERPASS_CREATION_FAILED];
return VULKAN_ERROR_RENDERPASS_CREATION_FAILED; return VULKAN_ERROR_RENDERPASS_CREATION_FAILED;
} }
@@ -224,7 +224,7 @@ uint8_t createRenderPass(VulkanState& vulkan_state)
* *
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
*/ */
uint8_t createDescriptorSetLayout(VulkanState& vulkan_state) uint8_t createDescriptorSetLayout()
{ {
VkDescriptorSetLayoutCreateInfo layoutInfo; VkDescriptorSetLayoutCreateInfo layoutInfo;
VkDescriptorSetLayoutBinding uboLayoutBinding; VkDescriptorSetLayoutBinding uboLayoutBinding;
@@ -241,7 +241,7 @@ uint8_t createDescriptorSetLayout(VulkanState& vulkan_state)
layoutInfo.bindingCount = 1U; layoutInfo.bindingCount = 1U;
layoutInfo.pBindings = &uboLayoutBinding; layoutInfo.pBindings = &uboLayoutBinding;
if (vkCreateDescriptorSetLayout(vulkan_state.device, &layoutInfo, nullptr, &vulkan_state.descriptorSetLayout) != VK_SUCCESS) { if (vkCreateDescriptorSetLayout(g_vulkan_state.device, &layoutInfo, nullptr, &g_vulkan_state.descriptorSetLayout) != VK_SUCCESS) {
LOGF << vulkanErrorMesssages[VULKAN_DESCRIPTORSETLAYOUT_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_DESCRIPTORSETLAYOUT_CREATION_FAILED];
return VULKAN_ERROR_DESCRIPTORSETLAYOUT_CREATION_FAILED; return VULKAN_ERROR_DESCRIPTORSETLAYOUT_CREATION_FAILED;
} }
@@ -254,18 +254,18 @@ uint8_t createDescriptorSetLayout(VulkanState& vulkan_state)
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_PIPELINE_LAYOUT_CREATION_FAILED * VULKAN_ERROR_PIPELINE_LAYOUT_CREATION_FAILED
*/ */
uint8_t createPipelineLayout(VulkanState& vulkan_state) uint8_t createPipelineLayout()
{ {
VkPipelineLayoutCreateInfo createInfo{}; VkPipelineLayoutCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
createInfo.pNext = nullptr; createInfo.pNext = nullptr;
createInfo.flags = 0U; createInfo.flags = 0U;
createInfo.setLayoutCount = 1U; createInfo.setLayoutCount = 1U;
createInfo.pSetLayouts = &vulkan_state.descriptorSetLayout; createInfo.pSetLayouts = &g_vulkan_state.descriptorSetLayout;
createInfo.pushConstantRangeCount = 0U; createInfo.pushConstantRangeCount = 0U;
createInfo.pPushConstantRanges = nullptr; createInfo.pPushConstantRanges = nullptr;
if (VK_SUCCESS != vkCreatePipelineLayout(vulkan_state.device, &createInfo, nullptr, &vulkan_state.pipelineLayout)) { if (VK_SUCCESS != vkCreatePipelineLayout(g_vulkan_state.device, &createInfo, nullptr, &g_vulkan_state.pipelineLayout)) {
LOGF << vulkanErrorMesssages[VULKAN_PIPELAYOUT_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_PIPELAYOUT_CREATION_FAILED];
return VULKAN_ERROR_PIPELINE_LAYOUT_CREATION_FAILED; return VULKAN_ERROR_PIPELINE_LAYOUT_CREATION_FAILED;
} }
@@ -279,7 +279,7 @@ uint8_t createPipelineLayout(VulkanState& vulkan_state)
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_GRAPHICSPIPELINE_CREATION_FAILED * VULKAN_ERROR_GRAPHICSPIPELINE_CREATION_FAILED
*/ */
uint8_t createGraphicsPipeline(VulkanState& vulkan_state) uint8_t createGraphicsPipeline()
{ {
uint8_t status = VULKAN_ERROR_NO_ERROR; uint8_t status = VULKAN_ERROR_NO_ERROR;
@@ -292,10 +292,10 @@ uint8_t createGraphicsPipeline(VulkanState& vulkan_state)
VkPipelineColorBlendStateCreateInfo colorBlending{}; VkPipelineColorBlendStateCreateInfo colorBlending{};
VkPipelineDynamicStateCreateInfo dynamicState{}; VkPipelineDynamicStateCreateInfo dynamicState{};
STATUS_CHECK(fillShaderStages(vulkan_state, &shaderStages)); STATUS_CHECK(fillShaderStages(&shaderStages));
fillVertexInputInfo(&vertexInputInfo); fillVertexInputInfo(&vertexInputInfo);
fillInputAssemblyInfo(&inputAssembly); fillInputAssemblyInfo(&inputAssembly);
fillViewportStateInfo(vulkan_state, &viewportState); fillViewportStateInfo(&viewportState);
fillRasterizationInfo(&rasterizer); fillRasterizationInfo(&rasterizer);
fillMultisamplingStateInfo(&multisampling); fillMultisamplingStateInfo(&multisampling);
fillColorBlendStateInfo(&colorBlending); fillColorBlendStateInfo(&colorBlending);
@@ -316,14 +316,14 @@ uint8_t createGraphicsPipeline(VulkanState& vulkan_state)
pipelineInfo.pDepthStencilState = nullptr; pipelineInfo.pDepthStencilState = nullptr;
pipelineInfo.pColorBlendState = &colorBlending; pipelineInfo.pColorBlendState = &colorBlending;
pipelineInfo.pDynamicState = &dynamicState; pipelineInfo.pDynamicState = &dynamicState;
pipelineInfo.layout = vulkan_state.pipelineLayout; pipelineInfo.layout = g_vulkan_state.pipelineLayout;
pipelineInfo.renderPass = vulkan_state.renderPass; pipelineInfo.renderPass = g_vulkan_state.renderPass;
pipelineInfo.subpass = 0; pipelineInfo.subpass = 0;
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
pipelineInfo.basePipelineIndex = -1; pipelineInfo.basePipelineIndex = -1;
if (VK_SUCCESS != if (VK_SUCCESS !=
vkCreateGraphicsPipelines(vulkan_state.device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &vulkan_state.graphicsPipeline)) { vkCreateGraphicsPipelines(g_vulkan_state.device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &g_vulkan_state.graphicsPipeline)) {
LOGF << vulkanErrorMesssages[VULKAN_GRAPHICSPIPELINE_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_GRAPHICSPIPELINE_CREATION_FAILED];
status = VULKAN_ERROR_GRAPHICSPIPELINE_CREATION_FAILED; status = VULKAN_ERROR_GRAPHICSPIPELINE_CREATION_FAILED;
} }
@@ -341,23 +341,23 @@ uint8_t createGraphicsPipeline(VulkanState& vulkan_state)
* *
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
*/ */
uint8_t createFrambuffers(VulkanState& vulkan_state) uint8_t createFrambuffers()
{ {
vulkan_state.swapchainFramebuffers.resize(vulkan_state.swapChainImageViews.size()); g_vulkan_state.swapchainFramebuffers.resize(g_vulkan_state.swapChainImageViews.size());
for (size_t i = 0; i < vulkan_state.swapChainImageViews.size(); i++) { for (size_t i = 0; i < g_vulkan_state.swapChainImageViews.size(); i++) {
VkImageView attachments[] = {vulkan_state.swapChainImageViews[i]}; VkImageView attachments[] = {g_vulkan_state.swapChainImageViews[i]};
VkFramebufferCreateInfo framebufferInfo{}; VkFramebufferCreateInfo framebufferInfo{};
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
framebufferInfo.renderPass = vulkan_state.renderPass; framebufferInfo.renderPass = g_vulkan_state.renderPass;
framebufferInfo.attachmentCount = 1; framebufferInfo.attachmentCount = 1;
framebufferInfo.pAttachments = attachments; framebufferInfo.pAttachments = attachments;
framebufferInfo.width = vulkan_state.swapchainExtent.width; framebufferInfo.width = g_vulkan_state.swapchainExtent.width;
framebufferInfo.height = vulkan_state.swapchainExtent.height; framebufferInfo.height = g_vulkan_state.swapchainExtent.height;
framebufferInfo.layers = 1; framebufferInfo.layers = 1;
if (vkCreateFramebuffer(vulkan_state.device, &framebufferInfo, nullptr, &vulkan_state.swapchainFramebuffers[i]) != VK_SUCCESS) { if (vkCreateFramebuffer(g_vulkan_state.device, &framebufferInfo, nullptr, &g_vulkan_state.swapchainFramebuffers[i]) != VK_SUCCESS) {
LOGF << vulkanErrorMesssages[VULKAN_FRAMEBUFFERS_CREATION_FAILED]; LOGF << vulkanErrorMesssages[VULKAN_FRAMEBUFFERS_CREATION_FAILED];
return VULKAN_ERROR_FRAMEBUFFERS_CREATION_FAILED; return VULKAN_ERROR_FRAMEBUFFERS_CREATION_FAILED;
} }
@@ -371,30 +371,30 @@ uint8_t createFrambuffers(VulkanState& vulkan_state)
* @brief Recreate swapchain * @brief Recreate swapchain
* *
*/ */
void recreateSwapchain(VulkanState& vulkan_state) void recreateSwapchain()
{ {
cleanup_swapchain(vulkan_state); cleanup_swapchain();
createSwapchain(vulkan_state); createSwapchain();
createSwapchainImageViews(vulkan_state); createSwapchainImageViews();
createFrambuffers(vulkan_state); createFrambuffers();
} }
/** /**
* @brief Cleanup swapchain * @brief Cleanup swapchain
* *
*/ */
void cleanup_swapchain(VulkanState& vulkan_state) void cleanup_swapchain()
{ {
vkDeviceWaitIdle(vulkan_state.device); vkDeviceWaitIdle(g_vulkan_state.device);
for (size_t i = 0; i < vulkan_state.swapchainFramebuffers.size(); i++) { for (size_t i = 0; i < g_vulkan_state.swapchainFramebuffers.size(); i++) {
vkDestroyFramebuffer(vulkan_state.device, vulkan_state.swapchainFramebuffers[i], nullptr); vkDestroyFramebuffer(g_vulkan_state.device, g_vulkan_state.swapchainFramebuffers[i], nullptr);
} }
for (size_t i = 0; i < vulkan_state.swapChainImageViews.size(); i++) { for (size_t i = 0; i < g_vulkan_state.swapChainImageViews.size(); i++) {
vkDestroyImageView(vulkan_state.device, vulkan_state.swapChainImageViews[i], nullptr); vkDestroyImageView(g_vulkan_state.device, g_vulkan_state.swapChainImageViews[i], nullptr);
} }
vkDestroySwapchainKHR(vulkan_state.device, vulkan_state.swapchain, nullptr); vkDestroySwapchainKHR(g_vulkan_state.device, g_vulkan_state.swapchain, nullptr);
} }
/** /**
@@ -405,7 +405,7 @@ void cleanup_swapchain(VulkanState& vulkan_state)
* VULKAN_ERROR_SHADER_FILE_OPEN_FAILED * VULKAN_ERROR_SHADER_FILE_OPEN_FAILED
* VULKAN_ERROR_SHADER_CREATION_FAILED * VULKAN_ERROR_SHADER_CREATION_FAILED
*/ */
uint8_t fillShaderStages(VulkanState& vulkan_state, std::vector<VkPipelineShaderStageCreateInfo>* shaderStages) uint8_t fillShaderStages(std::vector<VkPipelineShaderStageCreateInfo>* shaderStages)
{ {
auto vertexShaderCode = rse::utils::file::read_file(gShaderPaths.at(ShaderStage::VERTEX)); auto vertexShaderCode = rse::utils::file::read_file(gShaderPaths.at(ShaderStage::VERTEX));
auto fragmentsShaderCode = rse::utils::file::read_file(gShaderPaths.at(ShaderStage::FRAGMENTS)); auto fragmentsShaderCode = rse::utils::file::read_file(gShaderPaths.at(ShaderStage::FRAGMENTS));
@@ -414,8 +414,8 @@ uint8_t fillShaderStages(VulkanState& vulkan_state, std::vector<VkPipelineShader
return VULKAN_ERROR_SHADER_FILE_OPEN_FAILED; return VULKAN_ERROR_SHADER_FILE_OPEN_FAILED;
} }
std::optional<VkShaderModule> vertexShaderModule = createShaderModule(vulkan_state, vertexShaderCode); std::optional<VkShaderModule> vertexShaderModule = createShaderModule(vertexShaderCode);
std::optional<VkShaderModule> fragmentsShaderModule = createShaderModule(vulkan_state, fragmentsShaderCode); std::optional<VkShaderModule> fragmentsShaderModule = createShaderModule(fragmentsShaderCode);
if (!vertexShaderModule.has_value() || !fragmentsShaderModule.has_value()) { if (!vertexShaderModule.has_value() || !fragmentsShaderModule.has_value()) {
return VULKAN_ERROR_SHADER_CREATION_FAILED; return VULKAN_ERROR_SHADER_CREATION_FAILED;
@@ -570,20 +570,20 @@ void fillInputAssemblyInfo(VkPipelineInputAssemblyStateCreateInfo* inputAssembly
* *
* @param viewportState structure to be filled * @param viewportState structure to be filled
*/ */
void fillViewportStateInfo(VulkanState& vulkan_state, VkPipelineViewportStateCreateInfo* viewportState) void fillViewportStateInfo(VkPipelineViewportStateCreateInfo* viewportState)
{ {
VkViewport viewport{}; VkViewport viewport{};
viewport.x = 0.0f; viewport.x = 0.0f;
viewport.y = 0.0f; viewport.y = 0.0f;
viewport.width = static_cast<float>(vulkan_state.swapchainExtent.width); viewport.width = static_cast<float>(g_vulkan_state.swapchainExtent.width);
viewport.height = static_cast<float>(vulkan_state.swapchainExtent.height); viewport.height = static_cast<float>(g_vulkan_state.swapchainExtent.height);
viewport.minDepth = 0.0f; viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f; viewport.maxDepth = 1.0f;
/* Setting Scissors to fill entire window*/ /* Setting Scissors to fill entire window*/
VkRect2D scissor{}; VkRect2D scissor{};
scissor.offset = {0, 0}; scissor.offset = {0, 0};
scissor.extent = vulkan_state.swapchainExtent; scissor.extent = g_vulkan_state.swapchainExtent;
viewportState->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; viewportState->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState->viewportCount = 1; viewportState->viewportCount = 1;
@@ -684,29 +684,29 @@ void freeColorBlendInfo(VkPipelineColorBlendStateCreateInfo* colorBlending)
delete colorBlending->pAttachments; delete colorBlending->pAttachments;
} }
uint8_t create_pipeline(VulkanState& vulkan_state) uint8_t create_pipeline()
{ {
uint8_t status = VULKAN_ERROR_NO_ERROR; uint8_t status = VULKAN_ERROR_NO_ERROR;
STATUS_CHECK(createSwapchain(vulkan_state)); STATUS_CHECK(createSwapchain());
STATUS_CHECK(createSwapchainImageViews(vulkan_state)); STATUS_CHECK(createSwapchainImageViews());
STATUS_CHECK(createRenderPass(vulkan_state)); STATUS_CHECK(createRenderPass());
STATUS_CHECK(createDescriptorSetLayout(vulkan_state)); STATUS_CHECK(createDescriptorSetLayout());
STATUS_CHECK(createPipelineLayout(vulkan_state)); STATUS_CHECK(createPipelineLayout());
STATUS_CHECK(createGraphicsPipeline(vulkan_state)); STATUS_CHECK(createGraphicsPipeline());
STATUS_CHECK(createFrambuffers(vulkan_state)); STATUS_CHECK(createFrambuffers());
return status; return status;
} }
void destroy_pipeline(VulkanState& vulkan_state) void destroy_pipeline()
{ {
vkDestroyPipeline(vulkan_state.device, vulkan_state.graphicsPipeline, nullptr); vkDestroyPipeline(g_vulkan_state.device, g_vulkan_state.graphicsPipeline, nullptr);
vkDestroyPipelineLayout(vulkan_state.device, vulkan_state.pipelineLayout, nullptr); vkDestroyPipelineLayout(g_vulkan_state.device, g_vulkan_state.pipelineLayout, nullptr);
vkDestroyRenderPass(vulkan_state.device, vulkan_state.renderPass, nullptr); vkDestroyRenderPass(g_vulkan_state.device, g_vulkan_state.renderPass, nullptr);
for (size_t i = 0; i < gShaderModules.size(); i++) { for (size_t i = 0; i < gShaderModules.size(); i++) {
vkDestroyShaderModule(vulkan_state.device, gShaderModules[i], nullptr); vkDestroyShaderModule(g_vulkan_state.device, gShaderModules[i], nullptr);
} }
} }
+4 -4
View File
@@ -15,28 +15,28 @@ namespace rse::graphics::vulkanbase
* @param vulkan_state * @param vulkan_state
* @return uint8_t * @return uint8_t
*/ */
uint8_t create_pipeline(VulkanState& vulkan_state); uint8_t create_pipeline();
/** /**
* @brief Cleanup swapchain * @brief Cleanup swapchain
* *
* @param vulkan_state * @param vulkan_state
*/ */
void cleanup_swapchain(VulkanState& vulkan_state); void cleanup_swapchain();
/** /**
* @brief Recreates swapchain * @brief Recreates swapchain
* *
* @param vulkan_state * @param vulkan_state
*/ */
void recreateSwapchain(VulkanState& vulkan_state); void recreateSwapchain();
/** /**
* @brief Destroy all vulkan pipeline objects * @brief Destroy all vulkan pipeline objects
* *
* @param vulkan_state * @param vulkan_state
*/ */
void destroy_pipeline(VulkanState& vulkan_state); void destroy_pipeline();
} }
+2 -2
View File
@@ -96,11 +96,11 @@ uint16_t windowInit()
return WINDOW_ERROR_NO_ERROR; return WINDOW_ERROR_NO_ERROR;
} }
uint16_t windowLoop(rse::graphics::vulkanbase::VulkanState& vulkan_state) uint16_t windowLoop()
{ {
while (!glfwWindowShouldClose(gpWindowHandle)) { while (!glfwWindowShouldClose(gpWindowHandle)) {
glfwPollEvents(); glfwPollEvents();
rse::graphics::vulkanbase::drawFrame(vulkan_state); rse::graphics::vulkanbase::drawFrame();
} }
return WINDOW_ERROR_NO_ERROR; return WINDOW_ERROR_NO_ERROR;
+1 -2
View File
@@ -16,7 +16,6 @@
/* Vulkan header MUST be included before glfw */ /* Vulkan header MUST be included before glfw */
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "vulkan_commons.hpp"
namespace rse::graphics::window namespace rse::graphics::window
{ {
@@ -37,7 +36,7 @@ uint16_t windowInit();
* @param vulkan_state * @param vulkan_state
* @return uint16_t WINDOW_SUCCESS or error code * @return uint16_t WINDOW_SUCCESS or error code
*/ */
uint16_t windowLoop(rse::graphics::vulkanbase::VulkanState& vulkan_state); uint16_t windowLoop();
/** /**
* @brief Closes window and terminates GLFW * @brief Closes window and terminates GLFW