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 "semaphore.h"
19 VulkanCommands::VulkanCommands():
20 device(Device::get_current())
23 VulkanCommands::~VulkanCommands()
25 const VulkanFunctions &vk = device.get_functions();
30 void VulkanCommands::begin_buffer()
33 throw invalid_operation("VulkanCommands::begin_buffer");
35 const VulkanFunctions &vk = device.get_functions();
37 if(!current_pool->in_use)
39 current_pool->fence.reset();
40 current_pool->in_use = true;
43 VkCommandBufferAllocateInfo alloc_info = { };
44 alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
45 alloc_info.commandPool = handle_cast<::VkCommandPool>(current_pool->pool);
46 alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
47 alloc_info.commandBufferCount = 1;
49 vk.AllocateCommandBuffers(alloc_info, ¤t_buffer);
51 VkCommandBufferBeginInfo begin_info = { };
52 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
53 begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
55 vk.BeginCommandBuffer(current_buffer, begin_info);
58 void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_values)
60 const Framebuffer *target = pipeline_state->get_framebuffer();
62 throw invalid_operation("VulkanCommands::begin_render_pass");
64 const VulkanFunctions &vk = device.get_functions();
69 device.get_transfer_queue().dispatch_transfers(current_buffer);
71 // TODO Use proper value for to_present
72 VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(target->get_format(), clear, !clear_values, true);
76 VkRenderPassBeginInfo begin_info = { };
77 begin_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
78 begin_info.renderPass = handle_cast<::VkRenderPass>(render_pass);
79 begin_info.framebuffer = handle_cast<::VkFramebuffer>(target->handle);
81 const Rect *viewport = pipeline_state->get_viewport();
84 begin_info.renderArea.offset.x = viewport->left;
85 begin_info.renderArea.offset.y = viewport->bottom;
86 begin_info.renderArea.extent.width = viewport->width;
87 begin_info.renderArea.extent.height = viewport->height;
91 begin_info.renderArea.extent.width = target->get_width();
92 begin_info.renderArea.extent.height = target->get_height();
95 VkClearValue vk_clear_values[7];
99 for(FrameAttachment a: target->get_format())
101 if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
102 vk_clear_values[i].depthStencil.depth = clear_values[i].depth_stencil.depth;
103 else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
104 vk_clear_values[i].depthStencil.stencil = clear_values[i].depth_stencil.stencil;
107 vk_clear_values[i].color.float32[0] = clear_values[i].color.r;
108 vk_clear_values[i].color.float32[1] = clear_values[i].color.g;
109 vk_clear_values[i].color.float32[2] = clear_values[i].color.b;
110 vk_clear_values[i].color.float32[3] = clear_values[i].color.a;
115 begin_info.clearValueCount = target->get_format().size();
116 begin_info.pClearValues = vk_clear_values;
119 vk.CmdBeginRenderPass(current_buffer, begin_info, VK_SUBPASS_CONTENTS_INLINE);
122 void VulkanCommands::end_render_pass()
124 const VulkanFunctions &vk = device.get_functions();
126 vk.CmdEndRenderPass(current_buffer);
130 void VulkanCommands::begin_frame(unsigned index)
132 const VulkanFunctions &vk = device.get_functions();
134 unsigned pool_index = index%device.get_n_frames_in_flight();
135 if(pool_index>=command_pools.size())
137 command_pools.reserve(pool_index+1);
138 for(unsigned i=command_pools.size(); i<pool_index+1; ++i)
139 command_pools.emplace_back(device);
142 current_pool = &command_pools[pool_index];
143 if(current_pool->in_use)
145 current_pool->fence.wait();
146 vk.ResetCommandPool(current_pool->pool, 0);
147 current_pool->in_use = false;
151 void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem)
156 const VulkanFunctions &vk = device.get_functions();
157 ::VkSemaphore vk_wait_sem = (wait_sem ? handle_cast<::VkSemaphore>(wait_sem->handle) : 0);
158 ::VkSemaphore vk_signal_sem = (signal_sem ? handle_cast<::VkSemaphore>(signal_sem->handle) : 0);
159 VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
164 vk.EndCommandBuffer(current_buffer);
166 VkSubmitInfo submit_info = { };
167 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
168 submit_info.waitSemaphoreCount = (wait_sem ? 1 : 0);
169 submit_info.pWaitSemaphores = &vk_wait_sem;
170 submit_info.pWaitDstStageMask = &wait_stages;
171 submit_info.commandBufferCount = 1;
172 submit_info.pCommandBuffers = handle_cast<::VkCommandBuffer *>(¤t_buffer);
173 submit_info.signalSemaphoreCount = (signal_sem ? 1 : 0);
174 submit_info.pSignalSemaphores = &vk_signal_sem;
176 vk.QueueSubmit(1, &submit_info, current_pool->fence.handle);
181 void VulkanCommands::use_pipeline(const PipelineState *ps)
183 if(!pipeline_state || !ps || ps->get_framebuffer()!=pipeline_state->get_framebuffer() || ps->get_viewport()!=pipeline_state->get_viewport())
189 pipeline_state->refresh();
192 void VulkanCommands::clear(const ClearValue *values)
195 throw invalid_operation("VulkanCommands::clear");
197 begin_render_pass(true, values);
200 void VulkanCommands::draw(const Batch &batch)
202 draw_instanced(batch, 1);
205 void VulkanCommands::draw_instanced(const Batch &batch, unsigned count)
208 throw invalid_operation("VulkanCommands::draw_instanced");
210 const VulkanFunctions &vk = device.get_functions();
213 begin_render_pass(false, 0);
215 pipeline_state->apply(current_buffer);
216 unsigned first_index = batch.get_offset()/batch.get_index_size();
217 vk.CmdDrawIndexed(current_buffer, batch.size(), count, first_index, 0, 0);
220 void VulkanCommands::resolve_multisample(Framebuffer &)
222 throw logic_error("VulkanCommands::resolve_multisample is unimplemented");
225 void VulkanCommands::begin_query(const QueryPool &, unsigned)
227 throw logic_error("VulkanCommands::begin_query is unimplemented");
230 void VulkanCommands::end_query(const QueryPool &, unsigned)
232 throw logic_error("VulkanCommands::end_query is unimplemented");
236 VulkanCommands::CommandPool::CommandPool(Device &d):
240 const VulkanFunctions &vk = device.get_functions();
242 VkCommandPoolCreateInfo pool_info = { };
243 pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
244 pool_info.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
245 pool_info.queueFamilyIndex = device.get_context().get_private().graphics_queue_family;
247 vk.CreateCommandPool(pool_info, pool);
250 VulkanCommands::CommandPool::CommandPool(CommandPool &&other):
251 device(other.device),
253 fence(move(other.fence)),
259 VulkanCommands::CommandPool::~CommandPool()
261 const VulkanFunctions &vk = device.get_functions();
264 vk.DestroyCommandPool(pool);