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"
20 VulkanCommands::VulkanCommands():
21 device(Device::get_current())
24 VulkanCommands::~VulkanCommands()
26 const VulkanFunctions &vk = device.get_functions();
31 void VulkanCommands::begin_buffer(VkRenderPass render_pass)
33 if(frame_index>=command_pools.size())
34 throw invalid_operation("VulkanCommands::begin_buffer");
36 const VulkanFunctions &vk = device.get_functions();
38 CommandPool ¤t_pool = command_pools[frame_index];
39 if(!current_pool.in_use)
41 current_pool.fence.reset();
42 current_pool.in_use = true;
45 CommandBuffers &buffers = (render_pass ? current_pool.secondary : current_pool.primary);
47 if(buffers.next_buffer>=buffers.buffers.size())
49 VkCommandBufferAllocateInfo alloc_info = { };
50 alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
51 alloc_info.commandPool = handle_cast<::VkCommandPool>(current_pool.pool);
52 alloc_info.level = (render_pass ? VK_COMMAND_BUFFER_LEVEL_SECONDARY : VK_COMMAND_BUFFER_LEVEL_PRIMARY);
53 alloc_info.commandBufferCount = 1;
55 VkCommandBuffer buffer;
56 vk.AllocateCommandBuffers(alloc_info, &buffer);
57 buffers.buffers.push_back(buffer);
60 VkCommandBuffer buffer = buffers.buffers[buffers.next_buffer++];
61 (render_pass ? pass_buffer : primary_buffer) = buffer;
63 VkCommandBufferBeginInfo begin_info = { };
64 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
65 begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
67 begin_info.flags |= VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
69 VkCommandBufferInheritanceInfo inherit_info = { };
72 inherit_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO;
73 inherit_info.renderPass = handle_cast<::VkRenderPass>(render_pass);
74 inherit_info.subpass = 0;
76 begin_info.pInheritanceInfo = &inherit_info;
79 vk.BeginCommandBuffer(buffer, begin_info);
83 void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_values)
85 framebuffer = pipeline_state->get_framebuffer();
87 throw invalid_operation("VulkanCommands::begin_render_pass");
89 viewport = pipeline_state->get_viewport();
94 fb_is_swapchain = framebuffer->is_presentable();
95 framebuffer->refresh();
97 RenderPass render_pass;
98 render_pass.framebuffer = framebuffer;
99 render_pass.render_area = viewport;
100 render_pass.clear = clear;
101 render_pass.clear_values = clear_values;
102 render_pass.to_present = fb_is_swapchain;
103 render_pass.update(device);
105 discard_fb_contents = render_pass.discard_fb_contents;
107 begin_buffer(render_pass.handle);
108 render_pass.fill_begin_info(pass_begin_info);
111 void VulkanCommands::end_render_pass()
113 const VulkanFunctions &vk = device.get_functions();
114 VulkanCommandRecorder vkCmd(vk, primary_buffer);
116 vk.EndCommandBuffer(pass_buffer);
118 device.get_transfer_queue().dispatch_transfers(vkCmd);
120 Synchronizer &sync = device.get_synchronizer();
123 framebuffer->prepare_image_layouts(discard_fb_contents);
126 const VkRenderPassBeginInfo &begin_info = *reinterpret_cast<const VkRenderPassBeginInfo *>(pass_begin_info.data());
127 vkCmd.BeginRenderPass(begin_info, VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS);
128 vkCmd.ExecuteCommands(1, &pass_buffer);
129 vkCmd.EndRenderPass();
132 viewport = Rect::max();
136 void VulkanCommands::begin_frame(unsigned index)
138 const VulkanFunctions &vk = device.get_functions();
140 frame_index = index%device.get_n_frames_in_flight();
141 if(frame_index>=command_pools.size())
143 command_pools.reserve(frame_index+1);
144 for(unsigned i=command_pools.size(); i<frame_index+1; ++i)
145 command_pools.emplace_back(device);
148 CommandPool ¤t_pool = command_pools[frame_index];
149 if(current_pool.in_use)
151 current_pool.fence.wait();
152 vk.ResetCommandPool(current_pool.pool, 0);
153 current_pool.in_use = false;
154 current_pool.primary.next_buffer = 0;
155 current_pool.secondary.next_buffer = 0;
158 device.get_descriptor_pool().begin_frame();
161 void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem)
166 const VulkanFunctions &vk = device.get_functions();
167 ::VkSemaphore vk_wait_sem = (wait_sem ? handle_cast<::VkSemaphore>(wait_sem->handle) : 0);
168 ::VkSemaphore vk_signal_sem = (signal_sem ? handle_cast<::VkSemaphore>(signal_sem->handle) : 0);
169 VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
174 vk.EndCommandBuffer(primary_buffer);
176 VkSubmitInfo submit_info = { };
177 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
178 submit_info.waitSemaphoreCount = (wait_sem ? 1 : 0);
179 submit_info.pWaitSemaphores = &vk_wait_sem;
180 submit_info.pWaitDstStageMask = &wait_stages;
181 submit_info.commandBufferCount = 1;
182 submit_info.pCommandBuffers = handle_cast<::VkCommandBuffer *>(&primary_buffer);
183 submit_info.signalSemaphoreCount = (signal_sem ? 1 : 0);
184 submit_info.pSignalSemaphores = &vk_signal_sem;
186 vk.QueueSubmit(1, &submit_info, command_pools[frame_index].fence.handle);
191 void VulkanCommands::use_pipeline(const PipelineState *ps)
193 if(!pipeline_state || !ps || ps->get_framebuffer()!=framebuffer || ps->get_viewport()!=viewport)
200 void VulkanCommands::clear(const ClearValue *values)
203 throw invalid_operation("VulkanCommands::clear");
205 begin_render_pass(true, values);
208 void VulkanCommands::draw(const Batch &batch)
210 draw_instanced(batch, 1);
213 void VulkanCommands::draw_instanced(const Batch &batch, unsigned count)
216 throw invalid_operation("VulkanCommands::draw_instanced");
219 begin_render_pass(false, 0);
221 VulkanCommandRecorder vkCmd(device.get_functions(), pass_buffer);
223 pipeline_state->refresh();
224 pipeline_state->apply(vkCmd, last_pipeline, frame_index, fb_is_swapchain);
225 last_pipeline = pipeline_state;
226 unsigned first_index = batch.get_offset()/batch.get_index_size();
227 vkCmd.DrawIndexed(batch.size(), count, first_index, 0, 0);
230 void VulkanCommands::dispatch(unsigned count_x, unsigned count_y, unsigned count_z)
233 throw invalid_operation("VulkanCommands::draw_instanced");
238 VulkanCommandRecorder vkCmd(device.get_functions(), primary_buffer);
240 pipeline_state->refresh();
241 pipeline_state->synchronize_resources();
242 device.get_synchronizer().barrier(vkCmd);
243 pipeline_state->apply(vkCmd, 0, frame_index, false);
244 vkCmd.Dispatch(count_x, count_y, count_z);
247 void VulkanCommands::resolve_multisample()
249 if(!framebuffer || !framebuffer->has_resolve_attachments())
250 throw invalid_operation("VulkanCommands::resolve_multisample");
255 void VulkanCommands::begin_query(const QueryPool &, unsigned)
257 throw logic_error("VulkanCommands::begin_query is unimplemented");
260 void VulkanCommands::end_query(const QueryPool &, unsigned)
262 throw logic_error("VulkanCommands::end_query is unimplemented");
266 VulkanCommands::CommandPool::CommandPool(Device &d):
270 const VulkanFunctions &vk = device.get_functions();
272 VkCommandPoolCreateInfo pool_info = { };
273 pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
274 pool_info.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
275 pool_info.queueFamilyIndex = device.get_context().get_private().graphics_queue_family;
277 vk.CreateCommandPool(pool_info, pool);
280 VulkanCommands::CommandPool::CommandPool(CommandPool &&other):
281 device(other.device),
283 fence(move(other.fence)),
289 VulkanCommands::CommandPool::~CommandPool()
291 const VulkanFunctions &vk = device.get_functions();
294 vk.DestroyCommandPool(pool);