From 681554787b32dbb3534bfb0d7e25ece4cbc9e9ed Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Wed, 7 Dec 2016 14:50:52 +0100 Subject: [PATCH] vulkan: Add GskVulkanRender object This is refactoring work. GskVulkanRender is supposed to be the global object for a render operation, ie GskVulkanRenderer.render() will create this object for what it does. The object will be split into stages that perform the operations necessary to create a drawing. --- gsk/Makefile.am | 2 ++ gsk/gskvulkanrender.c | 69 ++++++++++++++++++++++++++++++++++++ gsk/gskvulkanrenderer.c | 55 +++++----------------------- gsk/gskvulkanrenderprivate.h | 29 +++++++++++++++ 4 files changed, 108 insertions(+), 47 deletions(-) create mode 100644 gsk/gskvulkanrender.c create mode 100644 gsk/gskvulkanrenderprivate.h diff --git a/gsk/Makefile.am b/gsk/Makefile.am index a29ebc55f8..bde8dbee5a 100644 --- a/gsk/Makefile.am +++ b/gsk/Makefile.am @@ -28,6 +28,7 @@ gsk_private_vulan_source_h = \ gskvulkanimageprivate.h \ gskvulkanmemoryprivate.h \ gskvulkanpipelineprivate.h \ + gskvulkanrenderprivate.h \ gskvulkanrendererprivate.h \ gskvulkanshaderprivate.h gsk_private_vulkan_source_c = \ @@ -35,6 +36,7 @@ gsk_private_vulkan_source_c = \ gskvulkanimage.c \ gskvulkanmemory.c \ gskvulkanpipeline.c \ + gskvulkanrender.c \ gskvulkanrenderer.c \ gskvulkanshader.c endif diff --git a/gsk/gskvulkanrender.c b/gsk/gskvulkanrender.c new file mode 100644 index 0000000000..3dff0bfaeb --- /dev/null +++ b/gsk/gskvulkanrender.c @@ -0,0 +1,69 @@ +#include "config.h" + +#include "gskvulkanrenderprivate.h" + +#include "gskvulkanpipelineprivate.h" + +void +gsk_vulkan_render_init (GskVulkanRender *self, + GdkVulkanContext *context, + VkCommandPool command_pool) +{ + self->vulkan = context; + self->command_pool = command_pool; + + GSK_VK_CHECK (vkAllocateCommandBuffers, gdk_vulkan_context_get_device (self->vulkan), + &(VkCommandBufferAllocateInfo) { + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, + .commandPool = self->command_pool, + .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, + .commandBufferCount = 1, + }, + &self->command_buffer); + + GSK_VK_CHECK (vkBeginCommandBuffer, self->command_buffer, + &(VkCommandBufferBeginInfo) { + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, + .flags = 0 + }); +} + +void +gsk_vulkan_render_submit (GskVulkanRender *self, + VkFence fence) +{ + GSK_VK_CHECK (vkEndCommandBuffer, self->command_buffer); + + GSK_VK_CHECK (vkQueueSubmit, gdk_vulkan_context_get_queue (self->vulkan), + 1, + &(VkSubmitInfo) { + .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, + .waitSemaphoreCount = 1, + .pWaitSemaphores = (VkSemaphore[1]) { + gdk_vulkan_context_get_draw_semaphore (self->vulkan), + }, + .pWaitDstStageMask = (VkPipelineStageFlags []) { + VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, + }, + .commandBufferCount = 1, + .pCommandBuffers = &self->command_buffer, + }, + fence); + + GSK_VK_CHECK (vkWaitForFences, gdk_vulkan_context_get_device (self->vulkan), + 1, + &fence, + VK_TRUE, + INT64_MAX); + GSK_VK_CHECK (vkResetFences, gdk_vulkan_context_get_device (self->vulkan), + 1, + &fence); +} + +void +gsk_vulkan_render_finish (GskVulkanRender *self) +{ + GSK_VK_CHECK (vkResetCommandPool, gdk_vulkan_context_get_device (self->vulkan), + self->command_pool, + 0); +} diff --git a/gsk/gskvulkanrenderer.c b/gsk/gskvulkanrenderer.c index e2f9591461..9856f3fd41 100644 --- a/gsk/gskvulkanrenderer.c +++ b/gsk/gskvulkanrenderer.c @@ -11,6 +11,7 @@ #include "gskvulkanbufferprivate.h" #include "gskvulkanimageprivate.h" #include "gskvulkanpipelineprivate.h" +#include "gskvulkanrenderprivate.h" #include @@ -524,7 +525,7 @@ gsk_vulkan_renderer_render (GskRenderer *renderer, GskRenderNode *root) { GskVulkanRenderer *self = GSK_VULKAN_RENDERER (renderer); - VkCommandBuffer command_buffer; + GskVulkanRender render; GskVulkanImage *image; #ifdef G_ENABLE_DEBUG GskProfiler *profiler; @@ -536,58 +537,18 @@ gsk_vulkan_renderer_render (GskRenderer *renderer, gsk_profiler_timer_begin (profiler, self->profile_timers.cpu_time); #endif - GSK_VK_CHECK (vkAllocateCommandBuffers, gdk_vulkan_context_get_device (self->vulkan), - &(VkCommandBufferAllocateInfo) { - .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, - .commandPool = self->command_pool, - .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, - .commandBufferCount = 1, - }, - &command_buffer); + gsk_vulkan_render_init (&render, self->vulkan, self->command_pool); - GSK_VK_CHECK (vkBeginCommandBuffer, command_buffer, - &(VkCommandBufferBeginInfo) { - .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, - .flags = 0 - }); + image = gsk_vulkan_renderer_prepare_render (self, render.command_buffer, root); - image = gsk_vulkan_renderer_prepare_render (self, command_buffer, root); + gsk_vulkan_renderer_do_render_pass (self, render.command_buffer, image); - gsk_vulkan_renderer_do_render_pass (self, command_buffer, image); + gsk_vulkan_render_submit (&render, self->command_pool_fence); - GSK_VK_CHECK (vkEndCommandBuffer, command_buffer); - - GSK_VK_CHECK (vkQueueSubmit, gdk_vulkan_context_get_queue (self->vulkan), - 1, - &(VkSubmitInfo) { - .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, - .waitSemaphoreCount = 1, - .pWaitSemaphores = (VkSemaphore[1]) { - gdk_vulkan_context_get_draw_semaphore (self->vulkan), - }, - .pWaitDstStageMask = (VkPipelineStageFlags []) { - VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, - }, - .commandBufferCount = 1, - .pCommandBuffers = &command_buffer, - }, - self->command_pool_fence); - - GSK_VK_CHECK (vkWaitForFences, gdk_vulkan_context_get_device (self->vulkan), - 1, - &self->command_pool_fence, - true, - INT64_MAX); - GSK_VK_CHECK (vkResetFences, gdk_vulkan_context_get_device (self->vulkan), - 1, - &self->command_pool_fence); - - GSK_VK_CHECK (vkResetCommandPool, gdk_vulkan_context_get_device (self->vulkan), - self->command_pool, - 0); - gsk_vulkan_image_free (image); + gsk_vulkan_render_finish (&render); + #ifdef G_ENABLE_DEBUG cpu_time = gsk_profiler_timer_end (profiler, self->profile_timers.cpu_time); gsk_profiler_timer_set (profiler, self->profile_timers.cpu_time, cpu_time); diff --git a/gsk/gskvulkanrenderprivate.h b/gsk/gskvulkanrenderprivate.h new file mode 100644 index 0000000000..c1f3202b31 --- /dev/null +++ b/gsk/gskvulkanrenderprivate.h @@ -0,0 +1,29 @@ +#ifndef __GSK_VULKAN_RENDER_PRIVATE_H__ +#define __GSK_VULKAN_RENDER_PRIVATE_H__ + +#include + +G_BEGIN_DECLS + +typedef struct _GskVulkanRender GskVulkanRender; + +struct _GskVulkanRender +{ + GdkVulkanContext *vulkan; + + VkCommandPool command_pool; + VkCommandBuffer command_buffer; +}; + +void gsk_vulkan_render_init (GskVulkanRender *self, + GdkVulkanContext *context, + VkCommandPool command_pool); + +void gsk_vulkan_render_submit (GskVulkanRender *self, + VkFence fence); + +void gsk_vulkan_render_finish (GskVulkanRender *self); + +G_END_DECLS + +#endif /* __GSK_VULKAN_RENDER_PRIVATE_H__ */