1 #include <msp/core/hash.h>
2 #include <msp/graphics/vulkancontext_platform.h>
4 #include "commands_backend.h"
7 #include "framebuffer.h"
8 #include "frameformat.h"
9 #include "pipelinestate.h"
11 #include "renderpass.h"
12 #include "semaphore.h"
13 #include "swapchaintexture.h"
21 VulkanCommands::VulkanCommands():
22 device(Device::get_current())
25 VulkanCommands::~VulkanCommands()
27 const VulkanFunctions &vk = device.get_functions();
32 void VulkanCommands::begin_buffer(VkRenderPass render_pass)
34 if(frame_index>=command_pools.size())
35 throw invalid_operation("VulkanCommands::begin_buffer");
37 const VulkanFunctions &vk = device.get_functions();
39 CommandPool ¤t_pool = command_pools[frame_index];
40 if(!current_pool.in_use)
42 current_pool.fence.reset();
43 current_pool.in_use = true;
46 CommandBuffers &buffers = (render_pass ? current_pool.secondary : current_pool.primary);
48 if(buffers.next_buffer>=buffers.buffers.size())
50 VkCommandBufferAllocateInfo alloc_info = { };
51 alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
52 alloc_info.commandPool = handle_cast<::VkCommandPool>(current_pool.pool);
53 alloc_info.level = (render_pass ? VK_COMMAND_BUFFER_LEVEL_SECONDARY : VK_COMMAND_BUFFER_LEVEL_PRIMARY);
54 alloc_info.commandBufferCount = 1;
56 VkCommandBuffer buffer;
57 vk.AllocateCommandBuffers(alloc_info, &buffer);
58 buffers.buffers.push_back(buffer);
61 VkCommandBuffer buffer = buffers.buffers[buffers.next_buffer++];
62 (render_pass ? pass_buffer : primary_buffer) = buffer;
64 VkCommandBufferBeginInfo begin_info = { };
65 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
66 begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
68 begin_info.flags |= VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
70 VkCommandBufferInheritanceInfo inherit_info = { };
73 inherit_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO;
74 inherit_info.renderPass = handle_cast<::VkRenderPass>(render_pass);
75 inherit_info.subpass = 0;
77 begin_info.pInheritanceInfo = &inherit_info;
80 vk.BeginCommandBuffer(buffer, begin_info);
84 void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_values)
86 framebuffer = pipeline_state->get_framebuffer();
88 throw invalid_operation("VulkanCommands::begin_render_pass");
90 viewport = pipeline_state->get_viewport();
95 fb_is_swapchain = false;
96 unsigned n_attachments = framebuffer->get_format().size();
97 for(unsigned i=0; (!fb_is_swapchain && i<n_attachments); ++i)
98 if(dynamic_cast<const SwapChainTexture *>(framebuffer->get_attachment(i)))
99 fb_is_swapchain = true;
101 framebuffer->refresh();
103 RenderPass render_pass;
104 render_pass.framebuffer = framebuffer;
105 render_pass.render_area = viewport;
106 render_pass.clear = clear;
107 render_pass.clear_values = clear_values;
108 render_pass.to_present = fb_is_swapchain;
109 render_pass.update(device);
111 discard_fb_contents = render_pass.discard_fb_contents;
113 begin_buffer(render_pass.handle);
114 render_pass.fill_begin_info(pass_begin_info);
117 void VulkanCommands::end_render_pass()
119 const VulkanFunctions &vk = device.get_functions();
120 VulkanCommandRecorder vkCmd(vk, primary_buffer);
122 vk.EndCommandBuffer(pass_buffer);
124 device.get_transfer_queue().dispatch_transfers(vkCmd);
126 Synchronizer &sync = device.get_synchronizer();
129 framebuffer->prepare_image_layouts(discard_fb_contents);
132 const VkRenderPassBeginInfo &begin_info = *reinterpret_cast<const VkRenderPassBeginInfo *>(pass_begin_info.data());
133 vkCmd.BeginRenderPass(begin_info, VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS);
134 vkCmd.ExecuteCommands(1, &pass_buffer);
135 vkCmd.EndRenderPass();
138 viewport = Rect::max();
142 void VulkanCommands::begin_frame(unsigned index)
144 const VulkanFunctions &vk = device.get_functions();
146 frame_index = index%device.get_n_frames_in_flight();
147 if(frame_index>=command_pools.size())
149 command_pools.reserve(frame_index+1);
150 for(unsigned i=command_pools.size(); i<frame_index+1; ++i)
151 command_pools.emplace_back(device);
154 CommandPool ¤t_pool = command_pools[frame_index];
155 if(current_pool.in_use)
157 current_pool.fence.wait();
158 vk.ResetCommandPool(current_pool.pool, 0);
159 current_pool.in_use = false;
160 current_pool.primary.next_buffer = 0;
161 current_pool.secondary.next_buffer = 0;
164 device.get_descriptor_pool().begin_frame();
167 void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem)
172 const VulkanFunctions &vk = device.get_functions();
173 ::VkSemaphore vk_wait_sem = (wait_sem ? handle_cast<::VkSemaphore>(wait_sem->handle) : 0);
174 ::VkSemaphore vk_signal_sem = (signal_sem ? handle_cast<::VkSemaphore>(signal_sem->handle) : 0);
175 VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
180 vk.EndCommandBuffer(primary_buffer);
182 VkSubmitInfo submit_info = { };
183 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
184 submit_info.waitSemaphoreCount = (wait_sem ? 1 : 0);
185 submit_info.pWaitSemaphores = &vk_wait_sem;
186 submit_info.pWaitDstStageMask = &wait_stages;
187 submit_info.commandBufferCount = 1;
188 submit_info.pCommandBuffers = handle_cast<::VkCommandBuffer *>(&primary_buffer);
189 submit_info.signalSemaphoreCount = (signal_sem ? 1 : 0);
190 submit_info.pSignalSemaphores = &vk_signal_sem;
192 vk.QueueSubmit(1, &submit_info, command_pools[frame_index].fence.handle);
197 void VulkanCommands::use_pipeline(const PipelineState *ps)
199 if(!pipeline_state || !ps || ps->get_framebuffer()!=framebuffer || ps->get_viewport()!=viewport)
206 void VulkanCommands::clear(const ClearValue *values)
209 throw invalid_operation("VulkanCommands::clear");
211 begin_render_pass(true, values);
214 void VulkanCommands::draw(const Batch &batch)
216 draw_instanced(batch, 1);
219 void VulkanCommands::draw_instanced(const Batch &batch, unsigned count)
222 throw invalid_operation("VulkanCommands::draw_instanced");
225 begin_render_pass(false, 0);
227 VulkanCommandRecorder vkCmd(device.get_functions(), pass_buffer);
229 pipeline_state->refresh();
230 pipeline_state->apply(vkCmd, last_pipeline, frame_index, fb_is_swapchain);
231 last_pipeline = pipeline_state;
232 unsigned first_index = batch.get_offset()/batch.get_index_size();
233 vkCmd.DrawIndexed(batch.size(), count, first_index, 0, 0);
236 void VulkanCommands::resolve_multisample(Framebuffer &)
238 throw logic_error("VulkanCommands::resolve_multisample is unimplemented");
241 void VulkanCommands::begin_query(const QueryPool &, unsigned)
243 throw logic_error("VulkanCommands::begin_query is unimplemented");
246 void VulkanCommands::end_query(const QueryPool &, unsigned)
248 throw logic_error("VulkanCommands::end_query is unimplemented");
252 VulkanCommands::CommandPool::CommandPool(Device &d):
256 const VulkanFunctions &vk = device.get_functions();
258 VkCommandPoolCreateInfo pool_info = { };
259 pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
260 pool_info.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
261 pool_info.queueFamilyIndex = device.get_context().get_private().graphics_queue_family;
263 vk.CreateCommandPool(pool_info, pool);
266 VulkanCommands::CommandPool::CommandPool(CommandPool &&other):
267 device(other.device),
269 fence(move(other.fence)),
275 VulkanCommands::CommandPool::~CommandPool()
277 const VulkanFunctions &vk = device.get_functions();
280 vk.DestroyCommandPool(pool);