Added FrameContext struct and used VK_RESULT more

main
noah metz 2024-10-26 12:43:07 -06:00
parent d1cfff1ff7
commit 174a8808d7
4 changed files with 96 additions and 191 deletions

@ -24,6 +24,16 @@
#include <cglm/quat.h> #include <cglm/quat.h>
#include <cglm/cam.h> #include <cglm/cam.h>
#define MAX_FRAMES_IN_FLIGHT 2
#define WINDOW_MIN_WIDTH 800
#define WINDOW_MIN_HEIGHT 600
typedef struct GPUBufferStruct {
VkBuffer buffers[MAX_FRAMES_IN_FLIGHT];
VmaAllocation memory[MAX_FRAMES_IN_FLIGHT];
VkDeviceSize size;
} GPUBuffer;
typedef struct GPUQueueStruct { typedef struct GPUQueueStruct {
VkQueue handle; VkQueue handle;
uint32_t family; uint32_t family;
@ -46,10 +56,6 @@ VkResult command_transition_image_layout(VkDevice device, VkCommandPool transfer
}\ }\
} }
extern const uint32_t MAX_FRAMES_IN_FLIGHT;
extern const uint32_t WINDOW_MIN_WIDTH;
extern const uint32_t WINDOW_MIN_HEIGHT;
typedef struct SwapchainDetailsStruct { typedef struct SwapchainDetailsStruct {
VkSurfaceCapabilitiesKHR capabilities; VkSurfaceCapabilitiesKHR capabilities;
@ -60,11 +66,18 @@ typedef struct SwapchainDetailsStruct {
uint32_t present_modes_count; uint32_t present_modes_count;
} SwapchainDetails; } SwapchainDetails;
typedef struct FrameContextStruct {
VkFence ready;
VkSemaphore image;
VkSemaphore render;
VkSemaphore transfer;
VkSemaphore compute;
} FrameContext;
typedef struct RenderContextStruct { typedef struct RenderContextStruct {
VkInstance instance; VkInstance instance;
VkDebugUtilsMessengerEXT debug_messenger; VkDebugUtilsMessengerEXT debug_messenger;
VkPhysicalDevice physical_device; VkPhysicalDevice physical_device;
VkPhysicalDeviceMemoryProperties memories;
VkSurfaceKHR surface; VkSurfaceKHR surface;
GPUQueue graphics_queue; GPUQueue graphics_queue;
GPUQueue present_queue; GPUQueue present_queue;
@ -96,10 +109,7 @@ typedef struct RenderContextStruct {
VkCommandBuffer* swapchain_command_buffers; VkCommandBuffer* swapchain_command_buffers;
VkSemaphore* image_available_semaphores; FrameContext frame[MAX_FRAMES_IN_FLIGHT];
VkSemaphore* render_finished_semaphores;
VkFence* in_flight_fences;
uint32_t current_frame; uint32_t current_frame;
vec2 window_scale; vec2 window_scale;

@ -1,7 +1,7 @@
#include "draw.h" #include "draw.h"
#include "gpu.h" #include "gpu.h"
void record_ui_compute(VkCommandBuffer command_buffer, RenderContext* gpu, UIContext* ui_context, double time) { void record_ui_compute(VkCommandBuffer command_buffer, UIContext* ui_context, double time) {
UIPushConstant push = { UIPushConstant push = {
.time = (float)time, .time = (float)time,
.layer = 0, .layer = 0,
@ -85,18 +85,18 @@ VkResult draw_frame(
double time) { double time) {
VkResult result; VkResult result;
result = vkWaitForFences(context->device, 1, &context->in_flight_fences[context->current_frame], VK_TRUE, UINT64_MAX); result = vkWaitForFences(context->device, 1, &context->frame[context->current_frame].ready, VK_TRUE, UINT64_MAX);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return result; return result;
} }
result = vkResetFences(context->device, 1, &context->in_flight_fences[context->current_frame]); result = vkResetFences(context->device, 1, &context->frame[context->current_frame].ready);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return result; return result;
} }
uint32_t image_index; uint32_t image_index;
result = vkAcquireNextImageKHR(context->device, context->swapchain, UINT64_MAX, context->image_available_semaphores[context->current_frame], VK_NULL_HANDLE, &image_index); result = vkAcquireNextImageKHR(context->device, context->swapchain, UINT64_MAX, context->frame[context->current_frame].image, VK_NULL_HANDLE, &image_index);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return result; return result;
} }
@ -134,7 +134,7 @@ VkResult draw_frame(
.pClearValues = clear_values, .pClearValues = clear_values,
}; };
record_ui_compute(command_buffer, context, ui, time); record_ui_compute(command_buffer, ui, time);
vkCmdBeginRenderPass(command_buffer, &render_pass_begin, VK_SUBPASS_CONTENTS_INLINE); vkCmdBeginRenderPass(command_buffer, &render_pass_begin, VK_SUBPASS_CONTENTS_INLINE);
// Render World // Render World
@ -149,15 +149,15 @@ VkResult draw_frame(
VkSubmitInfo submit_info = { VkSubmitInfo submit_info = {
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
.waitSemaphoreCount = 1, .waitSemaphoreCount = 1,
.pWaitSemaphores = &context->image_available_semaphores[context->current_frame], .pWaitSemaphores = &context->frame[context->current_frame].image,
.pWaitDstStageMask = wait_stages, .pWaitDstStageMask = wait_stages,
.commandBufferCount = 1, .commandBufferCount = 1,
.pCommandBuffers = &context->swapchain_command_buffers[image_index], .pCommandBuffers = &context->swapchain_command_buffers[image_index],
.signalSemaphoreCount = 1, .signalSemaphoreCount = 1,
.pSignalSemaphores = &context->render_finished_semaphores[context->current_frame], .pSignalSemaphores = &context->frame[context->current_frame].render,
}; };
result = vkQueueSubmit(context->graphics_queue.handle, 1, &submit_info, context->in_flight_fences[context->current_frame]); result = vkQueueSubmit(context->graphics_queue.handle, 1, &submit_info, context->frame[context->current_frame].ready);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return result; return result;
} }
@ -165,7 +165,7 @@ VkResult draw_frame(
VkPresentInfoKHR present_info = { VkPresentInfoKHR present_info = {
.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
.waitSemaphoreCount = 1, .waitSemaphoreCount = 1,
.pWaitSemaphores = &context->render_finished_semaphores[context->current_frame], .pWaitSemaphores = &context->frame[context->current_frame].render,
.swapchainCount = 1, .swapchainCount = 1,
.pSwapchains = &context->swapchain, .pSwapchains = &context->swapchain,
.pImageIndices = &image_index, .pImageIndices = &image_index,

@ -5,10 +5,6 @@
#include "vk_mem_alloc.h" #include "vk_mem_alloc.h"
#include "vulkan/vulkan_core.h" #include "vulkan/vulkan_core.h"
const uint32_t MAX_FRAMES_IN_FLIGHT = 2;
const uint32_t WINDOW_MIN_WIDTH = 800;
const uint32_t WINDOW_MIN_HEIGHT = 600;
const char * validation_layers[] = { const char * validation_layers[] = {
"VK_LAYER_KHRONOS_validation", "VK_LAYER_KHRONOS_validation",
//"VK_LAYER_LUNARG_api_dump", //"VK_LAYER_LUNARG_api_dump",
@ -135,10 +131,8 @@ VkResult create_instance(VkInstance* instance) {
.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR, .flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR,
}; };
VkResult result = vkCreateInstance(&instance_info, 0, instance); VkResult result;
if(result != VK_SUCCESS) { VK_RESULT(vkCreateInstance(&instance_info, 0, instance));
return result;
}
free(requested_extensions); free(requested_extensions);
return VK_SUCCESS; return VK_SUCCESS;
@ -171,10 +165,7 @@ VkResult create_debug_messenger(VkInstance instance, VkDebugUtilsMessengerEXT* d
PFN_vkCreateDebugUtilsMessengerEXT func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT"); PFN_vkCreateDebugUtilsMessengerEXT func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
VkResult result; VkResult result;
result = func(instance, &messenger_info, 0, debug_messenger); VK_RESULT(func(instance, &messenger_info, 0, debug_messenger));
if(result != VK_SUCCESS) {
return result;
}
return VK_SUCCESS; return VK_SUCCESS;
} }
@ -182,10 +173,7 @@ VkResult create_debug_messenger(VkInstance instance, VkDebugUtilsMessengerEXT* d
VkResult get_best_physical_device(VkInstance instance, VkPhysicalDevice* device) { VkResult get_best_physical_device(VkInstance instance, VkPhysicalDevice* device) {
uint32_t device_count = 0; uint32_t device_count = 0;
VkResult result; VkResult result;
result = vkEnumeratePhysicalDevices(instance, &device_count, 0); VK_RESULT(vkEnumeratePhysicalDevices(instance, &device_count, 0));
if(result != VK_SUCCESS) {
return result;
}
VkPhysicalDevice* devices = malloc(sizeof(VkPhysicalDevice)*device_count); VkPhysicalDevice* devices = malloc(sizeof(VkPhysicalDevice)*device_count);
result = vkEnumeratePhysicalDevices(instance, &device_count, devices); result = vkEnumeratePhysicalDevices(instance, &device_count, devices);
@ -375,10 +363,8 @@ VkResult create_logical_device(VkPhysicalDevice physical_device, VkSurfaceKHR su
.pNext = &features_12, .pNext = &features_12,
}; };
VkResult result = vkCreateDevice(physical_device, &device_create_info, 0, device); VkResult result;
if(result != VK_SUCCESS) { VK_RESULT(vkCreateDevice(physical_device, &device_create_info, 0, device));
return result;
}
vkGetDeviceQueue(*device, graphics_queue->family, graphics_queue->index, &graphics_queue->handle); vkGetDeviceQueue(*device, graphics_queue->family, graphics_queue->index, &graphics_queue->handle);
vkGetDeviceQueue(*device, present_queue->family, present_queue->index, &present_queue->handle); vkGetDeviceQueue(*device, present_queue->family, present_queue->index, &present_queue->handle);
@ -396,10 +382,8 @@ VkResult create_memory_allocator(VkInstance instance, VkPhysicalDevice physical_
.flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT, .flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT,
}; };
VkResult result = vmaCreateAllocator(&allocator_create_info, allocator); VkResult result;
if(result != VK_SUCCESS) { VK_RESULT(vmaCreateAllocator(&allocator_create_info, allocator));
return result;
}
return VK_SUCCESS; return VK_SUCCESS;
} }
@ -410,16 +394,12 @@ VkResult get_swapchain_details(VkPhysicalDevice physical_device, VkSurfaceKHR su
VkResult result; VkResult result;
result = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, surface, &details->capabilities); VK_RESULT(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, surface, &details->capabilities));
if(result != VK_SUCCESS) {
return result; VK_RESULT(vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &details->formats_count, 0));
}
result = vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &details->formats_count, 0);
if(result != VK_SUCCESS) {
return result;
}
details->formats = malloc(sizeof(VkSurfaceFormatKHR)*details->formats_count); details->formats = malloc(sizeof(VkSurfaceFormatKHR)*details->formats_count);
result = vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &details->formats_count, details->formats); result = vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &details->formats_count, details->formats);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
free(details->formats); free(details->formats);
@ -502,20 +482,14 @@ VkResult create_swapchain(VkDevice device, VkSurfaceFormatKHR format, VkPresentM
VkResult result; VkResult result;
result = vkCreateSwapchainKHR(device, &swapchain_info, 0, swapchain); VK_RESULT(vkCreateSwapchainKHR(device, &swapchain_info, 0, swapchain));
if(result != VK_SUCCESS) {
return result;
}
return VK_SUCCESS; return VK_SUCCESS;
} }
VkResult get_swapchain_images(VkDevice device, VkSwapchainKHR swapchain, VkImage** images, uint32_t* image_count) { VkResult get_swapchain_images(VkDevice device, VkSwapchainKHR swapchain, VkImage** images, uint32_t* image_count) {
VkResult result; VkResult result;
result = vkGetSwapchainImagesKHR(device, swapchain, image_count, 0); VK_RESULT(vkGetSwapchainImagesKHR(device, swapchain, image_count, 0));
if(result != VK_SUCCESS) {
return result;
}
*images = malloc(sizeof(VkImage)*(*image_count)); *images = malloc(sizeof(VkImage)*(*image_count));
if(*images == 0) { if(*images == 0) {
@ -581,7 +555,7 @@ VkResult find_depth_format(VkPhysicalDevice physical_device, VkImageTiling tilin
return VK_SUCCESS; return VK_SUCCESS;
} }
} }
return VK_ERROR_UNKNOWN; return VK_ERROR_VALIDATION_FAILED_EXT;
} }
VkResult create_render_pass(VkDevice device, VkSurfaceFormatKHR format, VkFormat depth_format, VkRenderPass* render_pass) { VkResult create_render_pass(VkDevice device, VkSurfaceFormatKHR format, VkFormat depth_format, VkRenderPass* render_pass) {
@ -673,10 +647,8 @@ VkResult create_render_pass(VkDevice device, VkSurfaceFormatKHR format, VkFormat
.pDependencies = dependencies, .pDependencies = dependencies,
}; };
VkResult result = vkCreateRenderPass(device, &render_info, 0, render_pass); VkResult result;
if(result != VK_SUCCESS) { VK_RESULT(vkCreateRenderPass(device, &render_info, 0, render_pass));
return result;
}
return VK_SUCCESS; return VK_SUCCESS;
} }
@ -715,46 +687,44 @@ VkResult create_swapchain_framebuffers(VkDevice device, uint32_t image_count, Vk
return VK_SUCCESS; return VK_SUCCESS;
} }
VkSemaphore* create_semaphores(VkDevice device, VkSemaphoreCreateFlags flags, uint32_t count) {
VkSemaphore* semaphores = malloc(sizeof(VkSemaphore)*count);
if(semaphores == 0) {
return 0;
}
VkSemaphore create_semaphore(VkDevice device, VkSemaphoreCreateFlags flags) {
VkSemaphoreCreateInfo semaphore_info = { VkSemaphoreCreateInfo semaphore_info = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.flags = flags, .flags = flags,
}; };
for(uint32_t i = 0; i < count; i++) { VkSemaphore semaphore;
VkResult result = vkCreateSemaphore(device, &semaphore_info, 0, &semaphores[i]); VkResult result = vkCreateSemaphore(device, &semaphore_info, 0, &semaphore);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
free(semaphores);
return 0; return 0;
} }
}
return semaphores;
}
VkFence* create_fences(VkDevice device, VkFenceCreateFlags flags, uint32_t count) { return semaphore;
VkFence* fences = malloc(sizeof(VkFence)*count);
if(fences == 0) {
return 0;
} }
VkFence create_fence(VkDevice device, VkFenceCreateFlags flags) {
VkFenceCreateInfo fence_info = { VkFenceCreateInfo fence_info = {
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
.flags = flags, .flags = flags,
}; };
for(uint32_t i = 0; i < count; i++) { VkFence fence;
VkResult result = vkCreateFence(device, &fence_info, 0, &fences[i]); VkResult result = vkCreateFence(device, &fence_info, 0, &fence);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
free(fences); return VK_NULL_HANDLE;
return 0;
} }
return fence;
} }
return fences;
VkResult create_frame_context(VkDevice device, FrameContext* frame) {
frame->ready = create_fence(device, VK_FENCE_CREATE_SIGNALED_BIT);
frame->image = create_semaphore(device, 0);
frame->render = create_semaphore(device, 0);
frame->transfer = create_semaphore(device, 0);
frame->compute = create_semaphore(device, 0);
return VK_SUCCESS;
} }
VkCommandBuffer* create_command_buffers(VkDevice device, VkCommandPool command_pool, uint32_t image_count) { VkCommandBuffer* create_command_buffers(VkDevice device, VkCommandPool command_pool, uint32_t image_count) {
@ -805,10 +775,8 @@ VkResult create_depth_image(VkDevice device, VkFormat depth_format, VkExtent2D s
.usage = VMA_MEMORY_USAGE_AUTO, .usage = VMA_MEMORY_USAGE_AUTO,
}; };
VkResult result = vmaCreateImage(allocator, &depth_image_info, &allocation_info, depth_image, depth_image_memory, NULL); VkResult result;
if(result != VK_SUCCESS) { VK_RESULT(vmaCreateImage(allocator, &depth_image_info, &allocation_info, depth_image, depth_image_memory, NULL));
return result;
}
VkImageViewCreateInfo depth_view_info = { VkImageViewCreateInfo depth_view_info = {
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
@ -830,15 +798,9 @@ VkResult create_depth_image(VkDevice device, VkFormat depth_format, VkExtent2D s
}, },
}; };
result = vkCreateImageView(device, &depth_view_info, 0, depth_image_view); VK_RESULT(vkCreateImageView(device, &depth_view_info, 0, depth_image_view));
if(result != VK_SUCCESS) {
return result;
}
result = command_transition_image_layout(device, extra_graphics_pool, graphics_queue, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL, *depth_image, 0, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT, VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED, VK_IMAGE_ASPECT_DEPTH_BIT); VK_RESULT(command_transition_image_layout(device, extra_graphics_pool, graphics_queue, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL, *depth_image, 0, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT, VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED, VK_IMAGE_ASPECT_DEPTH_BIT));
if(result != VK_SUCCESS) {
return result;
}
return VK_SUCCESS; return VK_SUCCESS;
} }
@ -853,131 +815,67 @@ VkResult init_vulkan(GLFWwindow* window, RenderContext* context) {
GLFWmonitor** monitors = glfwGetMonitors(&monitor_count); GLFWmonitor** monitors = glfwGetMonitors(&monitor_count);
glfwGetMonitorContentScale(monitors[0], &context->window_scale[0], &context->window_scale[1]); glfwGetMonitorContentScale(monitors[0], &context->window_scale[0], &context->window_scale[1]);
result = create_instance(&context->instance); VK_RESULT(create_instance(&context->instance));
if(result != VK_SUCCESS) {
return result;
}
result = create_debug_messenger(context->instance, &context->debug_messenger); VK_RESULT(create_debug_messenger(context->instance, &context->debug_messenger));
if(result != VK_SUCCESS) {
return result;
}
result = get_best_physical_device(context->instance, &context->physical_device); VK_RESULT(get_best_physical_device(context->instance, &context->physical_device));
if(result != VK_SUCCESS) {
return result;
}
vkGetPhysicalDeviceMemoryProperties(context->physical_device, &context->memories); VK_RESULT(glfwCreateWindowSurface(context->instance, window, 0, &context->surface));
result = glfwCreateWindowSurface(context->instance, window, 0, &context->surface); VK_RESULT(create_logical_device(context->physical_device, context->surface, &context->graphics_queue, &context->present_queue, &context->transfer_queue, &context->device));
if(result != VK_SUCCESS) {
return result;
}
result = create_logical_device(context->physical_device, context->surface, &context->graphics_queue, &context->present_queue, &context->transfer_queue, &context->device); VK_RESULT(create_memory_allocator(context->instance, context->physical_device, context->device, &context->allocator));
if(result != VK_SUCCESS) {
return result;
}
result = create_memory_allocator(context->instance, context->physical_device, context->device, &context->allocator);
if(result != VK_SUCCESS) {
return result;
}
VkCommandPoolCreateInfo extra_pool_info = { VkCommandPoolCreateInfo extra_pool_info = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.queueFamilyIndex = context->graphics_queue.family, .queueFamilyIndex = context->graphics_queue.family,
.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, .flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT,
}; };
result = vkCreateCommandPool(context->device, &extra_pool_info, 0, &context->extra_graphics_pool); VK_RESULT(vkCreateCommandPool(context->device, &extra_pool_info, 0, &context->extra_graphics_pool));
if(result != VK_SUCCESS) {
return result;
}
VkCommandPoolCreateInfo graphics_pool_info = { VkCommandPoolCreateInfo graphics_pool_info = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
.queueFamilyIndex = context->graphics_queue.family, .queueFamilyIndex = context->graphics_queue.family,
}; };
result = vkCreateCommandPool(context->device, &graphics_pool_info, 0, &context->graphics_pool); VK_RESULT(vkCreateCommandPool(context->device, &graphics_pool_info, 0, &context->graphics_pool));
if(result != VK_SUCCESS) {
return result;
}
VkCommandPoolCreateInfo transfer_pool_info = { VkCommandPoolCreateInfo transfer_pool_info = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, .flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT,
.queueFamilyIndex = context->transfer_queue.family, .queueFamilyIndex = context->transfer_queue.family,
}; };
result = vkCreateCommandPool(context->device, &transfer_pool_info, 0, &context->transfer_pool); VK_RESULT(vkCreateCommandPool(context->device, &transfer_pool_info, 0, &context->transfer_pool));
if(result != VK_SUCCESS) {
return result;
}
context->image_available_semaphores = create_semaphores(context->device, 0, MAX_FRAMES_IN_FLIGHT); for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
if(context->image_available_semaphores == NULL) { VK_RESULT(create_frame_context(context->device, &context->frame[i]));
return VK_ERROR_UNKNOWN;
} }
context->render_finished_semaphores = create_semaphores(context->device, 0, MAX_FRAMES_IN_FLIGHT); VK_RESULT(get_swapchain_details(context->physical_device, context->surface, &context->swapchain_details));
if(context->render_finished_semaphores == NULL) {
return VK_ERROR_UNKNOWN;
}
context->in_flight_fences = create_fences(context->device, VK_FENCE_CREATE_SIGNALED_BIT, MAX_FRAMES_IN_FLIGHT);
if(context->in_flight_fences == NULL) {
return VK_ERROR_UNKNOWN;
}
result = get_swapchain_details(context->physical_device, context->surface, &context->swapchain_details);
if(result != VK_SUCCESS) {
return result;
}
context->swapchain_format = choose_swapchain_format(context->swapchain_details); context->swapchain_format = choose_swapchain_format(context->swapchain_details);
context->swapchain_present_mode = choose_present_mode(context->swapchain_details); context->swapchain_present_mode = choose_present_mode(context->swapchain_details);
context->swapchain_extent = choose_swapchain_extent(context->swapchain_details); context->swapchain_extent = choose_swapchain_extent(context->swapchain_details);
context->swapchain = VK_NULL_HANDLE; context->swapchain = VK_NULL_HANDLE;
result = create_swapchain(context->device, context->swapchain_format, context->swapchain_present_mode, context->swapchain_extent, context->surface, context->swapchain_details.capabilities, context->graphics_queue.family, context->present_queue.family, &context->swapchain); VK_RESULT(create_swapchain(context->device, context->swapchain_format, context->swapchain_present_mode, context->swapchain_extent, context->surface, context->swapchain_details.capabilities, context->graphics_queue.family, context->present_queue.family, &context->swapchain));
if(result != VK_SUCCESS) {
return result;
}
result = get_swapchain_images(context->device, context->swapchain, &context->swapchain_images, &context->swapchain_image_count); VK_RESULT(get_swapchain_images(context->device, context->swapchain, &context->swapchain_images, &context->swapchain_image_count));
if(result != VK_SUCCESS) {
return result;
}
context->swapchain_command_buffers = create_command_buffers(context->device, context->graphics_pool, context->swapchain_image_count); context->swapchain_command_buffers = create_command_buffers(context->device, context->graphics_pool, context->swapchain_image_count);
if(context->swapchain_command_buffers == NULL) { if(context->swapchain_command_buffers == NULL) {
return VK_ERROR_UNKNOWN; return VK_ERROR_VALIDATION_FAILED_EXT;
} }
result = create_image_views(context->device, context->swapchain_image_count, context->swapchain_images, context->swapchain_format, &context->swapchain_image_views); VK_RESULT(create_image_views(context->device, context->swapchain_image_count, context->swapchain_images, context->swapchain_format, &context->swapchain_image_views));
if(result != VK_SUCCESS) {
return result;
}
result = find_depth_format(context->physical_device, VK_IMAGE_TILING_OPTIMAL, VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT, &context->depth_format); VK_RESULT(find_depth_format(context->physical_device, VK_IMAGE_TILING_OPTIMAL, VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT, &context->depth_format));
if(result != VK_SUCCESS) {
return result;
}
result = create_render_pass(context->device, context->swapchain_format, context->depth_format, &context->render_pass); VK_RESULT(create_render_pass(context->device, context->swapchain_format, context->depth_format, &context->render_pass));
if(result != VK_SUCCESS) {
return result;
}
result = create_depth_image(context->device, context->depth_format, context->swapchain_extent, context->allocator, context->extra_graphics_pool, context->graphics_queue, &context->depth_image, &context->depth_image_memory, &context->depth_image_view); VK_RESULT(create_depth_image(context->device, context->depth_format, context->swapchain_extent, context->allocator, context->extra_graphics_pool, context->graphics_queue, &context->depth_image, &context->depth_image_memory, &context->depth_image_view));
if(result != VK_SUCCESS) {
return result;
}
result = create_swapchain_framebuffers(context->device, context->swapchain_image_count, context->swapchain_image_views, context->depth_image_view, context->render_pass, context->swapchain_extent, &context->swapchain_framebuffers); VK_RESULT(create_swapchain_framebuffers(context->device, context->swapchain_image_count, context->swapchain_image_views, context->depth_image_view, context->render_pass, context->swapchain_extent, &context->swapchain_framebuffers));
if(result != VK_SUCCESS) {
return result;
}
context->current_frame = 0; context->current_frame = 0;
return VK_SUCCESS; return VK_SUCCESS;
@ -999,10 +897,8 @@ VkResult create_transfer_buffer(
.usage = VMA_MEMORY_USAGE_CPU_TO_GPU, .usage = VMA_MEMORY_USAGE_CPU_TO_GPU,
}; };
VkResult result = vmaCreateBuffer(allocator, &buffer_info, &memory_info, buffer, memory, NULL); VkResult result;
if(result != VK_SUCCESS) { VK_RESULT(vmaCreateBuffer(allocator, &buffer_info, &memory_info, buffer, memory, NULL));
return result;
}
result = vmaMapMemory(allocator, *memory, mapped); result = vmaMapMemory(allocator, *memory, mapped);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {

@ -200,10 +200,9 @@ int main_thread(void* data) {
int x = 0; int x = 0;
while(glfwWindowShouldClose(context->window) == 0) { while(glfwWindowShouldClose(context->window) == 0) {
glfwPollEvents(); glfwPollEvents();
if(x == 1000) { if(x == 0 && glfwGetTime() > 2.0) {
x = 1;
test_ui(&context->render, &context->ui); test_ui(&context->render, &context->ui);
} else {
x += 1;
} }
} }