From e10f4eed8dc62586b02975c3daf1582936d25248 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Thu, 9 Jul 2020 03:48:42 +0200 Subject: [PATCH] (Vulkan) Cleanups --- gfx/common/vulkan_common.c | 98 +++++++++++++--------------- gfx/common/vulkan_common.h | 26 ++++++-- gfx/drivers_shader/shader_vulkan.cpp | 8 ++- 3 files changed, 70 insertions(+), 62 deletions(-) diff --git a/gfx/common/vulkan_common.c b/gfx/common/vulkan_common.c index 92a2c813c5..383fad8fa8 100644 --- a/gfx/common/vulkan_common.c +++ b/gfx/common/vulkan_common.c @@ -2814,6 +2814,8 @@ static void vulkan_acquire_clear_fences(gfx_ctx_vulkan_data_t *vk) static VkSemaphore vulkan_get_wsi_acquire_semaphore(struct vulkan_context *ctx) { + VkSemaphore sem; + if (ctx->num_recycled_acquire_semaphores == 0) { VkSemaphoreCreateInfo sem_info; @@ -2825,7 +2827,7 @@ static VkSemaphore vulkan_get_wsi_acquire_semaphore(struct vulkan_context *ctx) &ctx->swapchain_recycled_semaphores[ctx->num_recycled_acquire_semaphores++]); } - VkSemaphore sem = + sem = ctx->swapchain_recycled_semaphores[--ctx->num_recycled_acquire_semaphores]; ctx->swapchain_recycled_semaphores[ctx->num_recycled_acquire_semaphores] = VK_NULL_HANDLE; @@ -2834,16 +2836,21 @@ static VkSemaphore vulkan_get_wsi_acquire_semaphore(struct vulkan_context *ctx) static void vulkan_acquire_wait_fences(gfx_ctx_vulkan_data_t *vk) { - VkFenceCreateInfo fence_info = - { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO }; + unsigned index; + VkFenceCreateInfo fence_info; + VkFence *next_fence = NULL; + + fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; + fence_info.pNext = NULL; + fence_info.flags = 0; /* Decouples the frame fence index from swapchain index. */ vk->context.current_frame_index = (vk->context.current_frame_index + 1) % vk->context.num_swapchain_images; - unsigned index = vk->context.current_frame_index; - VkFence *next_fence = &vk->context.swapchain_fences[index]; + index = vk->context.current_frame_index; + next_fence = &vk->context.swapchain_fences[index]; if (*next_fence != VK_NULL_HANDLE) { @@ -2880,14 +2887,20 @@ void vulkan_acquire_next_image(gfx_ctx_vulkan_data_t *vk) { unsigned index; VkResult err; - VkFence fence = VK_NULL_HANDLE; - VkSemaphore semaphore = VK_NULL_HANDLE; - VkFenceCreateInfo fence_info = - { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO }; - VkSemaphoreCreateInfo sem_info = - { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO }; + VkFenceCreateInfo fence_info; + VkSemaphoreCreateInfo sem_info; + VkFence fence = VK_NULL_HANDLE; + VkSemaphore semaphore = VK_NULL_HANDLE; bool is_retrying = false; + fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; + fence_info.pNext = NULL; + fence_info.flags = 0; + + sem_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; + sem_info.pNext = NULL; + sem_info.flags = 0; + retry: if (vk->swapchain == VK_NULL_HANDLE) { @@ -2903,10 +2916,10 @@ retry: { /* We still don't have a swapchain, so just fake it ... */ vk->context.current_swapchain_index = 0; - vk->context.current_frame_index = 0; + vk->context.current_frame_index = 0; vulkan_acquire_clear_fences(vk); vulkan_acquire_wait_fences(vk); - vk->context.invalid_swapchain = true; + vk->context.invalid_swapchain = true; return; } } @@ -3012,10 +3025,8 @@ retry: index = vk->context.current_swapchain_index; if (vk->context.swapchain_semaphores[index] == VK_NULL_HANDLE) - { vkCreateSemaphore(vk->context.device, &sem_info, NULL, &vk->context.swapchain_semaphores[index]); - } vulkan_acquire_wait_fences(vk); } @@ -3027,6 +3038,7 @@ bool vulkan_create_swapchain(gfx_ctx_vulkan_data_t *vk, uint32_t format_count; uint32_t present_mode_count; uint32_t desired_swapchain_images; + VkResult res; VkSurfaceCapabilitiesKHR surface_properties; VkSurfaceFormatKHR formats[256]; VkPresentModeKHR present_modes[16]; @@ -3039,7 +3051,6 @@ bool vulkan_create_swapchain(gfx_ctx_vulkan_data_t *vk, VkPresentModeKHR swapchain_present_mode = VK_PRESENT_MODE_FIFO_KHR; settings_t *settings = config_get_ptr(); VkCompositeAlphaFlagBitsKHR composite = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; - VkResult res; vkDeviceWaitIdle(vk->context.device); vulkan_acquire_clear_fences(vk); @@ -3346,22 +3357,33 @@ bool vulkan_create_swapchain(gfx_ctx_vulkan_data_t *vk, void vulkan_initialize_render_pass(VkDevice device, VkFormat format, VkRenderPass *render_pass) { - VkRenderPassCreateInfo rp_info = { - VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO }; - VkAttachmentReference color_ref = { 0, - VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL }; - VkAttachmentDescription attachment = {0}; + VkAttachmentReference color_ref; + VkRenderPassCreateInfo rp_info; + VkAttachmentDescription attachment; VkSubpassDescription subpass = {0}; + rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; + rp_info.pNext = NULL; + rp_info.flags = 0; + rp_info.attachmentCount = 1; + rp_info.pAttachments = &attachment; + rp_info.subpassCount = 1; + rp_info.pSubpasses = &subpass; + rp_info.dependencyCount = 0; + rp_info.pDependencies = NULL; + + color_ref.attachment = 0; + color_ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + /* We will always write to the entire framebuffer, * so we don't really need to clear. */ + attachment.flags = 0; attachment.format = format; attachment.samples = VK_SAMPLE_COUNT_1_BIT; attachment.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; - attachment.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; attachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; @@ -3369,11 +3391,6 @@ void vulkan_initialize_render_pass(VkDevice device, VkFormat format, subpass.colorAttachmentCount = 1; subpass.pColorAttachments = &color_ref; - rp_info.attachmentCount = 1; - rp_info.pAttachments = &attachment; - rp_info.subpassCount = 1; - rp_info.pSubpasses = &subpass; - vkCreateRenderPass(device, &rp_info, NULL, render_pass); } @@ -3659,30 +3676,3 @@ void vulkan_framebuffer_clear(VkImage image, VkCommandBuffer cmd) VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED); } - -void vulkan_pass_set_texture( - VkDevice device, - VkDescriptorSet set, VkSampler sampler, - unsigned binding, - VkImageView imageView, VkImageLayout imageLayout) -{ - VkDescriptorImageInfo image_info; - VkWriteDescriptorSet write; - - image_info.sampler = sampler; - image_info.imageView = imageView; - image_info.imageLayout = imageLayout; - - write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - write.pNext = NULL; - write.dstSet = set; - write.dstBinding = binding; - write.dstArrayElement = 0; - write.descriptorCount = 1; - write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - write.pImageInfo = &image_info; - write.pBufferInfo = NULL; - write.pTexelBufferView = NULL; - - vkUpdateDescriptorSets(device, 1, &write, 0, NULL); -} diff --git a/gfx/common/vulkan_common.h b/gfx/common/vulkan_common.h index 092c10f7a6..70362a1dcc 100644 --- a/gfx/common/vulkan_common.h +++ b/gfx/common/vulkan_common.h @@ -507,6 +507,26 @@ typedef struct vk vkMapMemory(device, texture->memory, texture->offset, texture->size, 0, &texture->mapped); \ } +#define VULKAN_PASS_SET_TEXTURE(device, set, _sampler, binding, image_view, image_layout) \ +{ \ + VkDescriptorImageInfo image_info; \ + VkWriteDescriptorSet write; \ + image_info.sampler = _sampler; \ + image_info.imageView = image_view; \ + image_info.imageLayout = image_layout; \ + write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; \ + write.pNext = NULL; \ + write.dstSet = set; \ + write.dstBinding = binding; \ + write.dstArrayElement = 0; \ + write.descriptorCount = 1; \ + write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; \ + write.pImageInfo = &image_info; \ + write.pBufferInfo = NULL; \ + write.pTexelBufferView = NULL; \ + vkUpdateDescriptorSets(device, 1, &write, 0, NULL); \ +} + uint32_t vulkan_find_memory_type( const VkPhysicalDeviceMemoryProperties *mem_props, uint32_t device_reqs, uint32_t host_reqs); @@ -654,12 +674,6 @@ void vulkan_framebuffer_clear(VkImage image, VkCommandBuffer cmd); void vulkan_initialize_render_pass(VkDevice device, VkFormat format, VkRenderPass *render_pass); -void vulkan_pass_set_texture( - VkDevice device, - VkDescriptorSet set, VkSampler sampler, - unsigned binding, - VkImageView imageView, VkImageLayout imageLayout); - RETRO_END_DECLS #endif diff --git a/gfx/drivers_shader/shader_vulkan.cpp b/gfx/drivers_shader/shader_vulkan.cpp index a1d6f4cd04..9dd573b4d7 100644 --- a/gfx/drivers_shader/shader_vulkan.cpp +++ b/gfx/drivers_shader/shader_vulkan.cpp @@ -2028,7 +2028,9 @@ void Pass::set_semantic_texture(VkDescriptorSet set, slang_texture_semantic semantic, const Texture &texture) { if (reflection.semantic_textures[semantic][0].texture) - vulkan_pass_set_texture(device, set, common->samplers[texture.filter][texture.mip_filter][texture.address], reflection.semantic_textures[semantic][0].binding, texture.texture.view, texture.texture.layout); + { + VULKAN_PASS_SET_TEXTURE(device, set, common->samplers[texture.filter][texture.mip_filter][texture.address], reflection.semantic_textures[semantic][0].binding, texture.texture.view, texture.texture.layout); + } } void Pass::set_semantic_texture_array(VkDescriptorSet set, @@ -2037,7 +2039,9 @@ void Pass::set_semantic_texture_array(VkDescriptorSet set, { if (index < reflection.semantic_textures[semantic].size() && reflection.semantic_textures[semantic][index].texture) - vulkan_pass_set_texture(device, set, common->samplers[texture.filter][texture.mip_filter][texture.address], reflection.semantic_textures[semantic][index].binding, texture.texture.view, texture.texture.layout); + { + VULKAN_PASS_SET_TEXTURE(device, set, common->samplers[texture.filter][texture.mip_filter][texture.address], reflection.semantic_textures[semantic][index].binding, texture.texture.view, texture.texture.layout); + } } void Pass::build_semantic_texture_array_vec4(uint8_t *data, slang_texture_semantic semantic,