main
noah metz 2024-01-11 21:13:30 -07:00
parent 4b6405934b
commit 69ad926976
1 changed files with 324 additions and 282 deletions

@ -322,9 +322,10 @@ VkSemaphore* create_semaphores(VkDevice device, VkSemaphoreCreateFlags flags, ui
return 0; return 0;
} }
VkSemaphoreCreateInfo semaphore_info = {}; VkSemaphoreCreateInfo semaphore_info = {
semaphore_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
semaphore_info.flags = flags; .flags = flags,
};
for(uint32_t i = 0; i < count; i++) { for(uint32_t i = 0; i < count; i++) {
VkResult result = vkCreateSemaphore(device, &semaphore_info, 0, &semaphores[i]); VkResult result = vkCreateSemaphore(device, &semaphore_info, 0, &semaphores[i]);
@ -411,11 +412,12 @@ VkDescriptorSet* create_descriptor_sets(VkDevice device, VkDescriptorSetLayout l
layouts[i] = layout; layouts[i] = layout;
} }
VkDescriptorSetAllocateInfo alloc_info = {}; VkDescriptorSetAllocateInfo alloc_info = {
alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
alloc_info.descriptorPool = pool; .descriptorPool = pool,
alloc_info.descriptorSetCount = count; .descriptorSetCount = count,
alloc_info.pSetLayouts = layouts; .pSetLayouts = layouts,
};
VkResult result = vkAllocateDescriptorSets(device, &alloc_info, sets); VkResult result = vkAllocateDescriptorSets(device, &alloc_info, sets);
free(layouts); free(layouts);
@ -490,13 +492,14 @@ bool check_queue_indices(QueueIndices indices) {
} }
QueueIndices get_queue_indices(VkPhysicalDevice physical_device, VkSurfaceKHR surface) { QueueIndices get_queue_indices(VkPhysicalDevice physical_device, VkSurfaceKHR surface) {
QueueIndices indices = {}; QueueIndices indices = {
indices.graphics_family = 0xFFFFFFFF; .graphics_family = 0xFFFFFFFF,
indices.graphics_index = 0xFFFFFFFF; .graphics_index = 0xFFFFFFFF,
indices.present_family = 0xFFFFFFFF; .present_family = 0xFFFFFFFF,
indices.present_index = 0xFFFFFFFF; .present_index = 0xFFFFFFFF,
indices.transfer_family = 0xFFFFFFFF; .transfer_family = 0xFFFFFFFF,
indices.transfer_index = 0xFFFFFFFF; .transfer_index = 0xFFFFFFFF,
};
uint32_t queue_family_count; uint32_t queue_family_count;
vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &queue_family_count, 0); vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &queue_family_count, 0);
@ -547,12 +550,13 @@ QueueIndices get_queue_indices(VkPhysicalDevice physical_device, VkSurfaceKHR su
VkDebugUtilsMessengerEXT create_debug_messenger(VkInstance instance) { VkDebugUtilsMessengerEXT create_debug_messenger(VkInstance instance) {
VkDebugUtilsMessengerEXT debug_messenger; VkDebugUtilsMessengerEXT debug_messenger;
VkDebugUtilsMessengerCreateInfoEXT messenger_info = {}; VkDebugUtilsMessengerCreateInfoEXT messenger_info = {
messenger_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
messenger_info.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT; .messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT,
messenger_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_DEVICE_ADDRESS_BINDING_BIT_EXT; .messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_DEVICE_ADDRESS_BINDING_BIT_EXT,
messenger_info.pfnUserCallback = debug_callback; .pfnUserCallback = debug_callback,
messenger_info.pUserData = 0; .pUserData = 0,
};
PFN_vkCreateDebugUtilsMessengerEXT func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT"); PFN_vkCreateDebugUtilsMessengerEXT func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
@ -574,20 +578,14 @@ VkInstance create_instance() {
return VK_NULL_HANDLE; return VK_NULL_HANDLE;
} }
VkApplicationInfo app_info = {}; VkApplicationInfo app_info = {
app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
app_info.pApplicationName = "spacegame"; .pApplicationName = "spacegame",
app_info.applicationVersion = VK_MAKE_VERSION(0, 0, 1); .applicationVersion = VK_MAKE_VERSION(0, 0, 1),
app_info.pEngineName = "spacegame"; .pEngineName = "spacegame",
app_info.engineVersion = VK_MAKE_VERSION(0, 0, 1); .engineVersion = VK_MAKE_VERSION(0, 0, 1),
app_info.apiVersion = VK_API_VERSION_1_2; .apiVersion = VK_API_VERSION_1_2,
};
VkInstanceCreateInfo instance_info = {};
instance_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instance_info.pApplicationInfo = &app_info;
instance_info.enabledLayerCount = validation_layer_count;
instance_info.ppEnabledLayerNames = validation_layers;
uint32_t glfwExtensionCount = 0; uint32_t glfwExtensionCount = 0;
const char** glfwExtensions; const char** glfwExtensions;
@ -603,10 +601,17 @@ VkInstance create_instance() {
requested_extensions[glfwExtensionCount + i] = instance_extensions[i]; requested_extensions[glfwExtensionCount + i] = instance_extensions[i];
} }
instance_info.enabledExtensionCount = glfwExtensionCount + instance_extension_count; VkInstanceCreateInfo instance_info = {
instance_info.ppEnabledExtensionNames = requested_extensions; .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pApplicationInfo = &app_info,
.enabledLayerCount = validation_layer_count,
.ppEnabledLayerNames = validation_layers,
.enabledExtensionCount = glfwExtensionCount + instance_extension_count,
.ppEnabledExtensionNames = requested_extensions,
.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR,
};
instance_info.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
VkResult result = vkCreateInstance(&instance_info, 0, &instance); VkResult result = vkCreateInstance(&instance_info, 0, &instance);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
@ -664,17 +669,16 @@ VkDevice create_logical_device(VkPhysicalDevice physical_device, QueueIndices qu
.samplerAnisotropy = VK_TRUE, .samplerAnisotropy = VK_TRUE,
}; };
VkDeviceCreateInfo device_create_info = {}; VkDeviceCreateInfo device_create_info = {
device_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
device_create_info.pQueueCreateInfos = queue_create_info; .pQueueCreateInfos = queue_create_info,
device_create_info.queueCreateInfoCount = unique_family_count; .queueCreateInfoCount = unique_family_count,
device_create_info.pEnabledFeatures = &device_features; .pEnabledFeatures = &device_features,
.enabledExtensionCount = device_extension_count,
device_create_info.enabledExtensionCount = device_extension_count; .ppEnabledExtensionNames = device_extensions,
device_create_info.ppEnabledExtensionNames = device_extensions; .enabledLayerCount = validation_layer_count,
.ppEnabledLayerNames = validation_layers,
device_create_info.enabledLayerCount = validation_layer_count; };
device_create_info.ppEnabledLayerNames = validation_layers;
VkResult result = vkCreateDevice(physical_device, &device_create_info, 0, &device); VkResult result = vkCreateDevice(physical_device, &device_create_info, 0, &device);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
@ -758,15 +762,21 @@ VkSwapchainKHR create_swapchain(VkDevice device, VkSurfaceFormatKHR format, VkPr
image_count = max_images; image_count = max_images;
} }
VkSwapchainCreateInfoKHR swapchain_info = {}; VkSwapchainCreateInfoKHR swapchain_info = {
swapchain_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
swapchain_info.surface = surface; .surface = surface,
swapchain_info.minImageCount = image_count; .minImageCount = image_count,
swapchain_info.imageFormat = format.format; .imageFormat = format.format,
swapchain_info.imageColorSpace = format.colorSpace; .imageColorSpace = format.colorSpace,
swapchain_info.imageExtent = extent; .imageExtent = extent,
swapchain_info.imageArrayLayers = 1; .imageArrayLayers = 1,
swapchain_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
.preTransform = capabilities.currentTransform,
.compositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
.presentMode = present_mode,
.clipped = VK_TRUE,
.oldSwapchain = old_swapchain,
};
uint32_t queue_families[2] = {indices.graphics_family, indices.present_index}; uint32_t queue_families[2] = {indices.graphics_family, indices.present_index};
if(indices.graphics_family != indices.present_family) { if(indices.graphics_family != indices.present_family) {
@ -779,11 +789,6 @@ VkSwapchainKHR create_swapchain(VkDevice device, VkSurfaceFormatKHR format, VkPr
swapchain_info.pQueueFamilyIndices = 0; swapchain_info.pQueueFamilyIndices = 0;
} }
swapchain_info.preTransform = capabilities.currentTransform;
swapchain_info.compositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
swapchain_info.presentMode = present_mode;
swapchain_info.clipped = VK_TRUE;
swapchain_info.oldSwapchain = old_swapchain;
VkSwapchainKHR swapchain; VkSwapchainKHR swapchain;
VkResult result; VkResult result;
@ -796,9 +801,10 @@ VkSwapchainKHR create_swapchain(VkDevice device, VkSurfaceFormatKHR format, VkPr
} }
SwapchainImages get_swapchain_images(VkDevice device, VkSwapchainKHR swapchain) { SwapchainImages get_swapchain_images(VkDevice device, VkSwapchainKHR swapchain) {
SwapchainImages images; SwapchainImages images = {
images.images = 0; .images = NULL,
images.count = 0; .count = 0,
};
VkResult result; VkResult result;
result = vkGetSwapchainImagesKHR(device, swapchain, &images.count, 0); result = vkGetSwapchainImagesKHR(device, swapchain, &images.count, 0);
@ -829,22 +835,25 @@ VkImageView* create_image_views(VkDevice device, uint32_t image_count, VkImage*
} }
for(uint32_t i = 0; i < image_count; i++) { for(uint32_t i = 0; i < image_count; i++) {
VkImageViewCreateInfo view_info = {}; VkImageViewCreateInfo view_info = {
view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
view_info.image = images[i]; .image = images[i],
view_info.viewType = VK_IMAGE_VIEW_TYPE_2D; .viewType = VK_IMAGE_VIEW_TYPE_2D,
view_info.format = format.format; .format = format.format,
.components = {
view_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY; .r = VK_COMPONENT_SWIZZLE_IDENTITY,
view_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; .g = VK_COMPONENT_SWIZZLE_IDENTITY,
view_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; .b = VK_COMPONENT_SWIZZLE_IDENTITY,
view_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY; .a = VK_COMPONENT_SWIZZLE_IDENTITY,
},
view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; .subresourceRange = {
view_info.subresourceRange.baseMipLevel = 0; .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
view_info.subresourceRange.levelCount = 1; .baseMipLevel = 0,
view_info.subresourceRange.baseArrayLayer = 0; .levelCount = 1,
view_info.subresourceRange.layerCount = 1; .baseArrayLayer = 0,
.layerCount = 1,
},
};
VkResult result = vkCreateImageView(device, &view_info, 0, &image_views[i]); VkResult result = vkCreateImageView(device, &view_info, 0, &image_views[i]);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
@ -868,14 +877,15 @@ VkFramebuffer* create_swapchain_framebuffers(VkDevice device, uint32_t image_cou
depth_image_view, depth_image_view,
}; };
VkFramebufferCreateInfo framebuffer_info = {}; VkFramebufferCreateInfo framebuffer_info = {
framebuffer_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
framebuffer_info.renderPass = render_pass; .renderPass = render_pass,
framebuffer_info.attachmentCount = 2; .attachmentCount = 2,
framebuffer_info.pAttachments = attachments; .pAttachments = attachments,
framebuffer_info.width = extent.width; .width = extent.width,
framebuffer_info.height = extent.height; .height = extent.height,
framebuffer_info.layers = 1; .layers = 1,
};
VkResult result = vkCreateFramebuffer(device, &framebuffer_info, 0, &framebuffers[i]); VkResult result = vkCreateFramebuffer(device, &framebuffer_info, 0, &framebuffers[i]);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
@ -888,10 +898,11 @@ VkFramebuffer* create_swapchain_framebuffers(VkDevice device, uint32_t image_cou
} }
VkShaderModule create_shader_module(VkDevice device, const char * code, uint32_t code_size) { VkShaderModule create_shader_module(VkDevice device, const char * code, uint32_t code_size) {
VkShaderModuleCreateInfo shader_info = {}; VkShaderModuleCreateInfo shader_info = {
shader_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
shader_info.codeSize = code_size; .codeSize = code_size,
shader_info.pCode = (uint32_t*)code; .pCode = (uint32_t*)code,
};
VkShaderModule shader; VkShaderModule shader;
VkResult result; VkResult result;
@ -1048,16 +1059,17 @@ AllocatedImage allocate_image(VkPhysicalDeviceMemoryProperties memories, VkDevic
} }
AllocatedBuffer allocate_buffer(VkPhysicalDeviceMemoryProperties memories, VkDevice device, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags include, VkMemoryPropertyFlags exclude) { AllocatedBuffer allocate_buffer(VkPhysicalDeviceMemoryProperties memories, VkDevice device, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags include, VkMemoryPropertyFlags exclude) {
AllocatedBuffer ret = {}; AllocatedBuffer ret = {
ret.memory = VK_NULL_HANDLE; .memory = VK_NULL_HANDLE,
ret.buffer = VK_NULL_HANDLE; .buffer = VK_NULL_HANDLE,
};
VkBufferCreateInfo buffer_info = {}; VkBufferCreateInfo buffer_info = {
buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
buffer_info.size = size; .size = size,
buffer_info.usage = usage; .usage = usage,
buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
VkResult result = vkCreateBuffer(device, &buffer_info, 0, &ret.buffer); VkResult result = vkCreateBuffer(device, &buffer_info, 0, &ret.buffer);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
@ -1069,10 +1081,11 @@ AllocatedBuffer allocate_buffer(VkPhysicalDeviceMemoryProperties memories, VkDev
VkMemoryRequirements memory_requirements; VkMemoryRequirements memory_requirements;
vkGetBufferMemoryRequirements(device, ret.buffer, &memory_requirements); vkGetBufferMemoryRequirements(device, ret.buffer, &memory_requirements);
VkMemoryAllocateInfo alloc_info = {}; VkMemoryAllocateInfo alloc_info = {
alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
alloc_info.allocationSize = memory_requirements.size; .allocationSize = memory_requirements.size,
alloc_info.memoryTypeIndex = pick_memory(memories, memory_requirements.memoryTypeBits, include, exclude); .memoryTypeIndex = pick_memory(memories, memory_requirements.memoryTypeBits, include, exclude),
};
result = vkAllocateMemory(device, &alloc_info, 0, &ret.memory); result = vkAllocateMemory(device, &alloc_info, 0, &ret.memory);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
@ -1125,11 +1138,12 @@ AllocatedBuffer* allocate_buffers(VkPhysicalDeviceMemoryProperties memories, VkD
} }
VkCommandBuffer command_begin_single(VkDevice device, VkCommandPool transfer_pool) { VkCommandBuffer command_begin_single(VkDevice device, VkCommandPool transfer_pool) {
VkCommandBufferAllocateInfo command_info = {}; VkCommandBufferAllocateInfo command_info = {
command_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
command_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
command_info.commandPool = transfer_pool; .commandPool = transfer_pool,
command_info.commandBufferCount = 1; .commandBufferCount = 1,
};
VkCommandBuffer command_buffer; VkCommandBuffer command_buffer;
VkResult result = vkAllocateCommandBuffers(device, &command_info, &command_buffer); VkResult result = vkAllocateCommandBuffers(device, &command_info, &command_buffer);
@ -1137,9 +1151,10 @@ VkCommandBuffer command_begin_single(VkDevice device, VkCommandPool transfer_poo
return VK_NULL_HANDLE; return VK_NULL_HANDLE;
} }
VkCommandBufferBeginInfo begin_info = {}; VkCommandBufferBeginInfo begin_info = {
begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
};
result = vkBeginCommandBuffer(command_buffer, &begin_info); result = vkBeginCommandBuffer(command_buffer, &begin_info);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
@ -1157,10 +1172,11 @@ VkResult command_end_single(VkDevice device, VkCommandBuffer command_buffer, VkC
return result; return result;
} }
VkSubmitInfo submit_info = {}; VkSubmitInfo submit_info = {
submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
submit_info.commandBufferCount = 1; .commandBufferCount = 1,
submit_info.pCommandBuffers = &command_buffer; .pCommandBuffers = &command_buffer,
};
result = vkQueueSubmit(transfer_queue, 1, &submit_info, 0); result = vkQueueSubmit(transfer_queue, 1, &submit_info, 0);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
@ -1176,10 +1192,11 @@ VkResult command_end_single(VkDevice device, VkCommandBuffer command_buffer, VkC
VkResult command_copy_buffers(VkDevice device, VkCommandPool transfer_pool, VkQueue transfer_queue, VkBuffer source, VkBuffer dest, VkDeviceSize size) { VkResult command_copy_buffers(VkDevice device, VkCommandPool transfer_pool, VkQueue transfer_queue, VkBuffer source, VkBuffer dest, VkDeviceSize size) {
VkCommandBuffer command_buffer = command_begin_single(device, transfer_pool); VkCommandBuffer command_buffer = command_begin_single(device, transfer_pool);
VkBufferCopy copy_region = {}; VkBufferCopy copy_region = {
copy_region.srcOffset = 0; .srcOffset = 0,
copy_region.dstOffset = 0; .dstOffset = 0,
copy_region.size = size; .size = size,
};
vkCmdCopyBuffer(command_buffer, source, dest, 1, &copy_region); vkCmdCopyBuffer(command_buffer, source, dest, 1, &copy_region);
@ -1415,64 +1432,74 @@ VkPipeline create_graphics_pipeline(
}; };
uint32_t dynamic_state_count = sizeof(dynamic_states)/sizeof(VkDynamicState); uint32_t dynamic_state_count = sizeof(dynamic_states)/sizeof(VkDynamicState);
VkPipelineDynamicStateCreateInfo dynamic_info = {}; VkPipelineDynamicStateCreateInfo dynamic_info = {
dynamic_info.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
dynamic_info.dynamicStateCount = dynamic_state_count; .dynamicStateCount = dynamic_state_count,
dynamic_info.pDynamicStates = dynamic_states; .pDynamicStates = dynamic_states,
};
VkPipelineVertexInputStateCreateInfo vertex_input_info = {};
vertex_input_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; VkPipelineVertexInputStateCreateInfo vertex_input_info = {
vertex_input_info.vertexBindingDescriptionCount = mesh_type.bindings_count; .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
vertex_input_info.pVertexBindingDescriptions = mesh_type.bindings; .vertexBindingDescriptionCount = mesh_type.bindings_count,
vertex_input_info.vertexAttributeDescriptionCount = mesh_type.attributes_count; .pVertexBindingDescriptions = mesh_type.bindings,
vertex_input_info.pVertexAttributeDescriptions = mesh_type.attributes; .vertexAttributeDescriptionCount = mesh_type.attributes_count,
.pVertexAttributeDescriptions = mesh_type.attributes,
VkPipelineInputAssemblyStateCreateInfo input_assembly_info = {}; };
input_assembly_info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
input_assembly_info.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; VkPipelineInputAssemblyStateCreateInfo input_assembly_info = {
input_assembly_info.primitiveRestartEnable = VK_FALSE; .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
VkViewport viewport = {}; .primitiveRestartEnable = VK_FALSE,
viewport.x = 0.0f; };
viewport.y = 0.0f;
viewport.width = (float)(extent.width); VkViewport viewport = {
viewport.height = (float)(extent.height); .x = 0.0f,
viewport.minDepth = 0.0f; .y = 0.0f,
viewport.maxDepth = 1.0f; .width = (float)(extent.width),
.height = (float)(extent.height),
VkRect2D scissor = {}; .minDepth = 0.0f,
VkOffset2D scissor_offset = {.x = 0, .y = 0}; .maxDepth = 1.0f,
scissor.offset = scissor_offset; };
scissor.extent = extent;
VkRect2D scissor = {
VkPipelineViewportStateCreateInfo viewport_state = {}; .offset = {
viewport_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; .x = 0,
viewport_state.viewportCount = 1; .y = 0,
viewport_state.pViewports = &viewport; },
viewport_state.scissorCount = 1; .extent = extent,
viewport_state.pScissors = &scissor; };
VkPipelineRasterizationStateCreateInfo raster_info = {}; VkPipelineViewportStateCreateInfo viewport_state = {
raster_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
raster_info.depthClampEnable = VK_FALSE; .viewportCount = 1,
raster_info.rasterizerDiscardEnable = VK_FALSE; .pViewports = &viewport,
raster_info.polygonMode = VK_POLYGON_MODE_FILL; .scissorCount = 1,
raster_info.lineWidth = 1.0f; .pScissors = &scissor,
raster_info.cullMode = VK_CULL_MODE_NONE; };
raster_info.frontFace = VK_FRONT_FACE_CLOCKWISE;
raster_info.depthBiasEnable = VK_FALSE; VkPipelineRasterizationStateCreateInfo raster_info = {
raster_info.depthBiasConstantFactor = 0.0f; .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
raster_info.depthBiasClamp = 0.0f; .depthClampEnable = VK_FALSE,
raster_info.depthBiasSlopeFactor = 0.0f; .rasterizerDiscardEnable = VK_FALSE,
.polygonMode = VK_POLYGON_MODE_FILL,
VkPipelineMultisampleStateCreateInfo multisample_info = {}; .lineWidth = 1.0f,
multisample_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; .cullMode = VK_CULL_MODE_NONE,
multisample_info.sampleShadingEnable = VK_FALSE; .frontFace = VK_FRONT_FACE_CLOCKWISE,
multisample_info.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; .depthBiasEnable = VK_FALSE,
multisample_info.minSampleShading = 1.0f; .depthBiasConstantFactor = 0.0f,
multisample_info.pSampleMask = 0; .depthBiasClamp = 0.0f,
multisample_info.alphaToCoverageEnable = VK_FALSE; .depthBiasSlopeFactor = 0.0f,
multisample_info.alphaToOneEnable = VK_FALSE; };
VkPipelineMultisampleStateCreateInfo multisample_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
.sampleShadingEnable = VK_FALSE,
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
.minSampleShading = 1.0f,
.pSampleMask = 0,
.alphaToCoverageEnable = VK_FALSE,
.alphaToOneEnable = VK_FALSE,
};
VkPipelineDepthStencilStateCreateInfo depth_info = { VkPipelineDepthStencilStateCreateInfo depth_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
@ -1487,46 +1514,47 @@ VkPipeline create_graphics_pipeline(
.back = {}, .back = {},
}; };
VkPipelineColorBlendAttachmentState color_blend_attachment = {}; VkPipelineColorBlendAttachmentState color_blend_attachment = {
color_blend_attachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; .colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT,
color_blend_attachment.blendEnable = VK_TRUE; .blendEnable = VK_TRUE,
color_blend_attachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA; .srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA,
color_blend_attachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; .dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
color_blend_attachment.colorBlendOp = VK_BLEND_OP_ADD; .colorBlendOp = VK_BLEND_OP_ADD,
color_blend_attachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; .srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE,
color_blend_attachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; .dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO,
color_blend_attachment.alphaBlendOp = VK_BLEND_OP_ADD; .alphaBlendOp = VK_BLEND_OP_ADD,
};
VkPipelineColorBlendStateCreateInfo color_blend_info = {};
color_blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; VkPipelineColorBlendStateCreateInfo color_blend_info = {
color_blend_info.logicOpEnable = VK_FALSE; .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
color_blend_info.logicOp = VK_LOGIC_OP_COPY; .logicOpEnable = VK_FALSE,
color_blend_info.attachmentCount = 1; .logicOp = VK_LOGIC_OP_COPY,
color_blend_info.pAttachments = &color_blend_attachment; .attachmentCount = 1,
color_blend_info.blendConstants[0] = 0.0f; .pAttachments = &color_blend_attachment,
color_blend_info.blendConstants[1] = 0.0f; .blendConstants[0] = 0.0f,
color_blend_info.blendConstants[2] = 0.0f; .blendConstants[1] = 0.0f,
color_blend_info.blendConstants[3] = 0.0f; .blendConstants[2] = 0.0f,
.blendConstants[3] = 0.0f,
VkGraphicsPipelineCreateInfo pipeline_info = {}; };
pipeline_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipeline_info.stageCount = shader_stage_count; VkGraphicsPipelineCreateInfo pipeline_info = {
pipeline_info.pStages = shader_stages; .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
pipeline_info.pVertexInputState = &vertex_input_info; .stageCount = shader_stage_count,
pipeline_info.pInputAssemblyState = &input_assembly_info; .pStages = shader_stages,
pipeline_info.pViewportState = &viewport_state; .pVertexInputState = &vertex_input_info,
pipeline_info.pRasterizationState = &raster_info; .pInputAssemblyState = &input_assembly_info,
pipeline_info.pDepthStencilState = 0; .pViewportState = &viewport_state,
pipeline_info.pColorBlendState = &color_blend_info; .pRasterizationState = &raster_info,
pipeline_info.pDynamicState = &dynamic_info; .pColorBlendState = &color_blend_info,
pipeline_info.pDepthStencilState = &depth_info; .pDynamicState = &dynamic_info,
pipeline_info.pMultisampleState = &multisample_info; .pDepthStencilState = &depth_info,
pipeline_info.layout = layout; .pMultisampleState = &multisample_info,
pipeline_info.renderPass = render_pass; .layout = layout,
pipeline_info.subpass = 0; .renderPass = render_pass,
.subpass = 0,
pipeline_info.basePipelineHandle = VK_NULL_HANDLE; .basePipelineHandle = VK_NULL_HANDLE,
pipeline_info.basePipelineIndex = -1; .basePipelineIndex = -1,
};
VkPipeline pipeline; VkPipeline pipeline;
VkResult result = vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipeline_info, 0, &pipeline); VkResult result = vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipeline_info, 0, &pipeline);
@ -1640,8 +1668,6 @@ VkResult recreate_swapchain(VulkanContext* context) {
vkDestroySemaphore(context->device, context->image_available_semaphores[i], 0); vkDestroySemaphore(context->device, context->image_available_semaphores[i], 0);
} }
//vkDestroySwapchainKHR(context->device, context->swapchain, 0);
//free(context->swapchain_images);
free(context->swapchain_image_views); free(context->swapchain_image_views);
free(context->swapchain_framebuffers); free(context->swapchain_framebuffers);
free(context->swapchain_details.formats); free(context->swapchain_details.formats);
@ -1748,23 +1774,17 @@ void command_draw_material(Material material, uint32_t object_count, Object* obj
} }
VkResult command_draw_scene(uint32_t materials_count, Material* materials, uint32_t* object_counts, Object** objects, uint32_t frame_num, VkDescriptorSet* scene_descriptors, VkCommandBuffer command_buffer, VkRenderPass render_pass, VkFramebuffer framebuffer, VkExtent2D extent) { VkResult command_draw_scene(uint32_t materials_count, Material* materials, uint32_t* object_counts, Object** objects, uint32_t frame_num, VkDescriptorSet* scene_descriptors, VkCommandBuffer command_buffer, VkRenderPass render_pass, VkFramebuffer framebuffer, VkExtent2D extent) {
VkCommandBufferBeginInfo begin_info = {}; VkCommandBufferBeginInfo begin_info = {
begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
begin_info.flags = 0; .flags = 0,
begin_info.pInheritanceInfo = 0; .pInheritanceInfo = 0,
};
VkResult result = vkBeginCommandBuffer(command_buffer, &begin_info); VkResult result = vkBeginCommandBuffer(command_buffer, &begin_info);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return result; return result;
} }
VkRenderPassBeginInfo render_pass_info = {};
render_pass_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
render_pass_info.renderPass = render_pass;
render_pass_info.framebuffer = framebuffer;
VkOffset2D render_offset = {.x = 0, .y = 0};
render_pass_info.renderArea.offset = render_offset;
render_pass_info.renderArea.extent = extent;
VkClearValue clear_colors[] = { VkClearValue clear_colors[] = {
{ {
.color = { .color = {
@ -1775,24 +1795,41 @@ VkResult command_draw_scene(uint32_t materials_count, Material* materials, uint3
.depthStencil = {1.0f, 0.0f}, .depthStencil = {1.0f, 0.0f},
}, },
}; };
render_pass_info.clearValueCount = sizeof(clear_colors)/sizeof(VkClearValue);
render_pass_info.pClearValues = clear_colors; VkRenderPassBeginInfo render_pass_info = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
.renderPass = render_pass,
.framebuffer = framebuffer,
.renderArea = {
.offset = {
.x = 0,
.y = 0,
},
.extent = extent,
},
.clearValueCount = sizeof(clear_colors)/sizeof(VkClearValue),
.pClearValues = clear_colors,
};
vkCmdBeginRenderPass(command_buffer, &render_pass_info, VK_SUBPASS_CONTENTS_INLINE); vkCmdBeginRenderPass(command_buffer, &render_pass_info, VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport = {}; VkViewport viewport = {
viewport.x = 0.0f; .x = 0.0f,
viewport.y = 0.0f; .y = 0.0f,
viewport.width = (float)(extent.width); .width = (float)(extent.width),
viewport.height = (float)(extent.height); .height = (float)(extent.height),
viewport.minDepth = 0.0f; .minDepth = 0.0f,
viewport.maxDepth = 1.0f; .maxDepth = 1.0f,
};
vkCmdSetViewport(command_buffer, 0, 1, &viewport); vkCmdSetViewport(command_buffer, 0, 1, &viewport);
VkRect2D scissor = {}; VkRect2D scissor = {
VkOffset2D scissor_offset = {.x = 0.0f, .y = 0.0f}; .offset = {
scissor.offset = scissor_offset; .x = 0.0f,
scissor.extent = extent; .y = 0.0f,
},
.extent = extent,
};
vkCmdSetScissor(command_buffer, 0, 1, &scissor); vkCmdSetScissor(command_buffer, 0, 1, &scissor);
for(uint i = 0; i < materials_count; i++) { for(uint i = 0; i < materials_count; i++) {
@ -1805,11 +1842,12 @@ VkResult command_draw_scene(uint32_t materials_count, Material* materials, uint3
} }
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) {
VkCommandBufferAllocateInfo alloc_info = {}; VkCommandBufferAllocateInfo alloc_info = {
alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
alloc_info.commandPool = command_pool; .commandPool = command_pool,
alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
alloc_info.commandBufferCount = image_count; .commandBufferCount = image_count,
};
VkCommandBuffer* command_buffers = malloc(sizeof(VkCommandBuffer)*image_count); VkCommandBuffer* command_buffers = malloc(sizeof(VkCommandBuffer)*image_count);
if(command_buffers == 0) { if(command_buffers == 0) {
@ -1830,9 +1868,10 @@ VkFence* create_fences(VkDevice device, VkFenceCreateFlags flags, uint32_t count
return 0; return 0;
} }
VkFenceCreateInfo fence_info = {}; VkFenceCreateInfo fence_info = {
fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
fence_info.flags = flags; .flags = flags,
};
for(uint32_t i = 0; i < count; i++) { for(uint32_t i = 0; i < count; i++) {
VkResult result = vkCreateFence(device, &fence_info, 0, &fences[i]); VkResult result = vkCreateFence(device, &fence_info, 0, &fences[i]);
@ -2926,30 +2965,32 @@ VkResult draw_frame(VulkanContext* context, SceneContext* scene, uint32_t materi
return result; return result;
} }
VkSubmitInfo submit_info = {};
VkPipelineStageFlags wait_stages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT}; VkPipelineStageFlags wait_stages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; VkSubmitInfo submit_info = {
submit_info.waitSemaphoreCount = 1; .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
submit_info.pWaitSemaphores = &context->image_available_semaphores[context->current_frame]; .waitSemaphoreCount = 1,
submit_info.pWaitDstStageMask = wait_stages; .pWaitSemaphores = &context->image_available_semaphores[context->current_frame],
submit_info.commandBufferCount = 1; .pWaitDstStageMask = wait_stages,
submit_info.pCommandBuffers = &context->swapchain_command_buffers[context->current_frame]; .commandBufferCount = 1,
submit_info.signalSemaphoreCount = 1; .pCommandBuffers = &context->swapchain_command_buffers[context->current_frame],
submit_info.pSignalSemaphores = &context->render_finished_semaphores[context->current_frame]; .signalSemaphoreCount = 1,
.pSignalSemaphores = &context->render_finished_semaphores[context->current_frame],
};
result = vkQueueSubmit(context->queues.graphics, 1, &submit_info, context->in_flight_fences[context->current_frame]); result = vkQueueSubmit(context->queues.graphics, 1, &submit_info, context->in_flight_fences[context->current_frame]);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return result; return result;
} }
VkPresentInfoKHR present_info = {}; VkPresentInfoKHR present_info = {
present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
present_info.waitSemaphoreCount = 1; .waitSemaphoreCount = 1,
present_info.pWaitSemaphores = &context->render_finished_semaphores[context->current_frame]; .pWaitSemaphores = &context->render_finished_semaphores[context->current_frame],
present_info.swapchainCount = 1; .swapchainCount = 1,
present_info.pSwapchains = &context->swapchain; .pSwapchains = &context->swapchain,
present_info.pImageIndices = &image_index; .pImageIndices = &image_index,
present_info.pResults = 0; .pResults = 0,
};
return vkQueuePresentKHR(context->queues.present, &present_info); return vkQueuePresentKHR(context->queues.present, &present_info);
} }
@ -3179,16 +3220,17 @@ Object create_texture_mesh_object(Material* texture_mesh_material, VkPhysicalDev
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
}; };
VkWriteDescriptorSet descriptor_write = {}; VkWriteDescriptorSet descriptor_write = {
descriptor_write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
descriptor_write.dstSet = sets[i]; .dstSet = sets[i],
descriptor_write.dstBinding = 0; .dstBinding = 0,
descriptor_write.dstArrayElement = 0; .dstArrayElement = 0,
descriptor_write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
descriptor_write.descriptorCount = 1; .descriptorCount = 1,
descriptor_write.pBufferInfo = 0; .pBufferInfo = 0,
descriptor_write.pImageInfo = &image_info; .pImageInfo = &image_info,
descriptor_write.pTexelBufferView = 0; .pTexelBufferView = 0,
};
vkUpdateDescriptorSets(device, 1, &descriptor_write, 0, 0); vkUpdateDescriptorSets(device, 1, &descriptor_write, 0, 0);
} }