@ -27,6 +27,10 @@ const char * instance_extensions[] = {
uint32_t instance_extension_count = sizeof ( instance_extensions ) / sizeof ( const char * ) ;
const char * device_extensions [ ] = {
# ifdef __APPLE__
" VK_KHR_portability_subset " ,
# endif
VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME ,
VK_KHR_SWAPCHAIN_EXTENSION_NAME ,
VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME ,
} ;
@ -125,8 +129,6 @@ VkResult create_instance(VkInstance* instance) {
. flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR ,
} ;
VkResult result = vkCreateInstance ( & instance_info , 0 , instance ) ;
if ( result ! = VK_SUCCESS ) {
return result ;
@ -575,17 +577,17 @@ VkResult find_depth_format(VkPhysicalDevice physical_device, VkImageTiling tilin
return VK_ERROR_UNKNOWN ;
}
VkResult create_render_pass ( VkDevice device , VkSurfaceFormatKHR format , VkFormat depth_format , VkRenderPass * render_pass , VkImageLayout initial_layout , VkImageLayout final_layout , VkAttachmentLoadOp color_load_op ) {
VkResult create_render_pass ( VkDevice device , VkSurfaceFormatKHR format , VkFormat depth_format , VkRenderPass * render_pass ) {
VkAttachmentDescription attachments [ ] = {
{
. format = format . format ,
. samples = VK_SAMPLE_COUNT_1_BIT ,
. loadOp = color_load_op ,
. loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR ,
. storeOp = VK_ATTACHMENT_STORE_OP_STORE ,
. stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR ,
. stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE ,
. initialLayout = initial_layout ,
. finalLayout = final_layout ,
. initialLayout = VK_IMAGE_LAYOUT_UNDEFINED ,
. finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR ,
} ,
{
. format = depth_format ,
@ -619,6 +621,12 @@ VkResult create_render_pass(VkDevice device, VkSurfaceFormatKHR format, VkFormat
. pColorAttachments = color_attachment_refs ,
. pDepthStencilAttachment = & depth_attachment_ref ,
} ,
{
. pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS ,
. colorAttachmentCount = sizeof ( color_attachment_refs ) / sizeof ( VkAttachmentReference ) ,
. pColorAttachments = color_attachment_refs ,
. pDepthStencilAttachment = & depth_attachment_ref ,
} ,
} ;
// This basically says "make sure nothing else is writing to the depth_stencil or the color attachment during the pipeline
@ -631,6 +639,15 @@ VkResult create_render_pass(VkDevice device, VkSurfaceFormatKHR format, VkFormat
. dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT ,
. dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT ,
. dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT ,
} ,
{
. srcSubpass = 0 ,
. dstSubpass = 1 ,
. srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT ,
. srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT ,
. dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT ,
. dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT ,
. dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT ,
}
} ;
@ -931,12 +948,7 @@ VkResult init_vulkan(GLFWwindow* window, RenderContext* context) {
return result ;
}
result = create_render_pass ( context - > device , context - > swapchain_format , context - > depth_format , & context - > world_render_pass , VK_IMAGE_LAYOUT_UNDEFINED , VK_IMAGE_LAYOUT_PRESENT_SRC_KHR , VK_ATTACHMENT_LOAD_OP_CLEAR ) ;
if ( result ! = VK_SUCCESS ) {
return result ;
}
result = create_render_pass ( context - > device , context - > swapchain_format , context - > depth_format , & context - > ui_render_pass , VK_IMAGE_LAYOUT_PRESENT_SRC_KHR , VK_IMAGE_LAYOUT_PRESENT_SRC_KHR , VK_ATTACHMENT_LOAD_OP_LOAD ) ;
result = create_render_pass ( context - > device , context - > swapchain_format , context - > depth_format , & context - > render_pass ) ;
if ( result ! = VK_SUCCESS ) {
return result ;
}
@ -946,7 +958,7 @@ VkResult init_vulkan(GLFWwindow* window, RenderContext* context) {
return result ;
}
result = create_swapchain_framebuffers ( context - > device , context - > swapchain_image_count , context - > swapchain_image_views , context - > depth_image_view , context - > world_ render_pass, context - > swapchain_extent , & context - > swapchain_framebuffers ) ;
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 ;
}
@ -1005,30 +1017,20 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
VkClearValue clear_values [ 2 ] = { { . color = { { 0.0f , 0.0f , 0.0f , 1.0f } } } , { . depthStencil = { 1.0f , 0.0f } } } ;
VkDeviceSize offset = 0 ;
// World Render Pass
VkRenderPassBeginInfo world_render_pass_begin = {
VkRenderPassBeginInfo render_pass_begin = {
. sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO ,
. renderPass = context - > world_ render_pass,
. renderPass = context - > render_pass,
. framebuffer = context - > swapchain_framebuffers [ image_index ] ,
. renderArea . offset = { 0 , 0 } ,
. renderArea . extent = context - > swapchain_extent ,
. clearValueCount = 2 ,
. pClearValues = clear_values ,
} ;
vkCmdBeginRenderPass ( command_buffer , & world_ render_pass_begin, VK_SUBPASS_CONTENTS_INLINE ) ;
vkCmdEndRenderPass ( command_buffer ) ;
vkCmdBeginRenderPass ( command_buffer , & render_pass_begin, VK_SUBPASS_CONTENTS_INLINE ) ;
// World subpass
// UI Render Pass
VkRenderPassBeginInfo ui_render_pass_begin = {
. sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO ,
. renderPass = context - > ui_render_pass ,
. framebuffer = context - > swapchain_framebuffers [ image_index ] ,
. renderArea . offset = { 0 , 0 } ,
. renderArea . extent = context - > swapchain_extent ,
. clearValueCount = 2 ,
. pClearValues = clear_values ,
} ;
vkCmdBeginRenderPass ( command_buffer , & ui_render_pass_begin , VK_SUBPASS_CONTENTS_INLINE ) ;
vkCmdNextSubpass ( command_buffer , VK_SUBPASS_CONTENTS_INLINE ) ;
// UI subpass
// Draw UI colored rects ////////////////////////////////
vkCmdBindPipeline ( command_buffer , VK_PIPELINE_BIND_POINT_GRAPHICS , ui_context - > ui_pipeline_rect . pipeline ) ;
@ -1044,12 +1046,15 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
}
/////////////////////////////////////////////////////////
// Draw UI text /////////////////////////////////////////
vkCmdBindPipeline ( command_buffer , VK_PIPELINE_BIND_POINT_COMPUTE , ui_context - > ui_compute_text . pipeline ) ;
vkCmdBindPipeline ( command_buffer , VK_PIPELINE_BIND_POINT_GRAPHICS , ui_context - > ui_pipeline_text . pipeline ) ;
vkCmdBindDescriptorSets ( command_buffer , VK_PIPELINE_BIND_POINT_GRAPHICS , ui_context - > ui_pipeline_text . layout , 0 , 1 , & ui_context - > ui_descriptor_set , 0 , NULL ) ;
for ( uint32_t i = 0 ; i < ui_layer_count ; i + + ) {
if ( ui_layers [ i ] . text_count > 0 ) {
// Bind Font Descriptor
vkCmdBindDescriptorSets ( command_buffer , VK_PIPELINE_BIND_POINT_GRAPHICS , ui_context - > ui_pipeline_text . layout , 1 , 1 , & ui_layers [ i ] . font . set , 0 , NULL ) ;
vkCmdPushConstants ( command_buffer , ui_context - > ui_pipeline_text . layout , VK_SHADER_STAGE_VERTEX_BIT , 0 , 8 , & ui_layers [ i ] . texts_address ) ;
// Push pointers
vkCmdPushConstants ( command_buffer , ui_context - > ui_pipeline_text . layout , VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_COMPUTE_BIT , 0 , 8 , & ui_layers [ i ] . texts_address ) ;
vkCmdDrawIndexed ( command_buffer , 6 , ui_layers [ i ] . text_count , 0 , 0 , 0 ) ;
}
}