|
|
@ -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 fences;
|
|
|
|
|
|
|
|
|
|
|
|
return fence;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|