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"
12 #include "swapchaintexture.h"
20 VulkanCommands::VulkanCommands():
21 device(Device::get_current())
24 VulkanCommands::~VulkanCommands()
26 const VulkanFunctions &vk = device.get_functions();
31 void VulkanCommands::begin_buffer()
34 throw invalid_operation("VulkanCommands::begin_buffer");
36 const VulkanFunctions &vk = device.get_functions();
38 if(!current_pool->in_use)
40 current_pool->fence.reset();
41 current_pool->in_use = true;
44 VkCommandBufferAllocateInfo alloc_info = { };
45 alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
46 alloc_info.commandPool = handle_cast<::VkCommandPool>(current_pool->pool);
47 alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
48 alloc_info.commandBufferCount = 1;
50 vk.AllocateCommandBuffers(alloc_info, ¤t_buffer);
52 VkCommandBufferBeginInfo begin_info = { };
53 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
54 begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
56 vk.BeginCommandBuffer(current_buffer, begin_info);
59 void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_values)
61 framebuffer = pipeline_state->get_framebuffer();
63 throw invalid_operation("VulkanCommands::begin_render_pass");
65 const VulkanFunctions &vk = device.get_functions();
70 device.get_transfer_queue().dispatch_transfers(current_buffer);
72 Synchronizer &sync = device.get_synchronizer();
75 bool to_present = false;
76 unsigned n_attachments = framebuffer->get_format().size();
77 for(unsigned i=0; i<n_attachments; ++i)
78 if(dynamic_cast<const SwapChainTexture *>(framebuffer->get_attachment(i)))
81 framebuffer->synchronize(clear);
82 VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(framebuffer->get_format(), clear, !clear_values, to_present);
84 framebuffer->refresh();
86 sync.barrier(current_buffer);
88 VkRenderPassBeginInfo begin_info = { };
89 begin_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
90 begin_info.renderPass = handle_cast<::VkRenderPass>(render_pass);
91 begin_info.framebuffer = handle_cast<::VkFramebuffer>(framebuffer->handle);
93 viewport = pipeline_state->get_viewport();
96 begin_info.renderArea.offset.x = viewport->left;
97 begin_info.renderArea.offset.y = viewport->bottom;
98 begin_info.renderArea.extent.width = viewport->width;
99 begin_info.renderArea.extent.height = viewport->height;
103 begin_info.renderArea.extent.width = framebuffer->get_width();
104 begin_info.renderArea.extent.height = framebuffer->get_height();
107 VkClearValue vk_clear_values[7];
111 for(FrameAttachment a: framebuffer->get_format())
113 if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
114 vk_clear_values[i].depthStencil.depth = clear_values[i].depth_stencil.depth;
115 else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
116 vk_clear_values[i].depthStencil.stencil = clear_values[i].depth_stencil.stencil;
119 vk_clear_values[i].color.float32[0] = clear_values[i].color.r;
120 vk_clear_values[i].color.float32[1] = clear_values[i].color.g;
121 vk_clear_values[i].color.float32[2] = clear_values[i].color.b;
122 vk_clear_values[i].color.float32[3] = clear_values[i].color.a;
127 begin_info.clearValueCount = framebuffer->get_format().size();
128 begin_info.pClearValues = vk_clear_values;
131 vk.CmdBeginRenderPass(current_buffer, begin_info, VK_SUBPASS_CONTENTS_INLINE);
134 void VulkanCommands::end_render_pass()
136 const VulkanFunctions &vk = device.get_functions();
138 vk.CmdEndRenderPass(current_buffer);
143 void VulkanCommands::begin_frame(unsigned index)
145 const VulkanFunctions &vk = device.get_functions();
147 unsigned pool_index = index%device.get_n_frames_in_flight();
148 if(pool_index>=command_pools.size())
150 command_pools.reserve(pool_index+1);
151 for(unsigned i=command_pools.size(); i<pool_index+1; ++i)
152 command_pools.emplace_back(device);
155 current_pool = &command_pools[pool_index];
156 if(current_pool->in_use)
158 current_pool->fence.wait();
159 vk.ResetCommandPool(current_pool->pool, 0);
160 current_pool->in_use = false;
164 void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem)
169 const VulkanFunctions &vk = device.get_functions();
170 ::VkSemaphore vk_wait_sem = (wait_sem ? handle_cast<::VkSemaphore>(wait_sem->handle) : 0);
171 ::VkSemaphore vk_signal_sem = (signal_sem ? handle_cast<::VkSemaphore>(signal_sem->handle) : 0);
172 VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
177 vk.EndCommandBuffer(current_buffer);
179 VkSubmitInfo submit_info = { };
180 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
181 submit_info.waitSemaphoreCount = (wait_sem ? 1 : 0);
182 submit_info.pWaitSemaphores = &vk_wait_sem;
183 submit_info.pWaitDstStageMask = &wait_stages;
184 submit_info.commandBufferCount = 1;
185 submit_info.pCommandBuffers = handle_cast<::VkCommandBuffer *>(¤t_buffer);
186 submit_info.signalSemaphoreCount = (signal_sem ? 1 : 0);
187 submit_info.pSignalSemaphores = &vk_signal_sem;
189 vk.QueueSubmit(1, &submit_info, current_pool->fence.handle);
194 void VulkanCommands::use_pipeline(const PipelineState *ps)
196 if(!pipeline_state || !ps || ps->get_framebuffer()!=framebuffer || ps->get_viewport()!=viewport)
203 void VulkanCommands::clear(const ClearValue *values)
206 throw invalid_operation("VulkanCommands::clear");
208 begin_render_pass(true, values);
211 void VulkanCommands::draw(const Batch &batch)
213 draw_instanced(batch, 1);
216 void VulkanCommands::draw_instanced(const Batch &batch, unsigned count)
219 throw invalid_operation("VulkanCommands::draw_instanced");
221 const VulkanFunctions &vk = device.get_functions();
224 begin_render_pass(false, 0);
226 pipeline_state->refresh();
227 pipeline_state->apply(current_buffer);
228 unsigned first_index = batch.get_offset()/batch.get_index_size();
229 vk.CmdDrawIndexed(current_buffer, batch.size(), count, first_index, 0, 0);
232 void VulkanCommands::resolve_multisample(Framebuffer &)
234 throw logic_error("VulkanCommands::resolve_multisample is unimplemented");
237 void VulkanCommands::begin_query(const QueryPool &, unsigned)
239 throw logic_error("VulkanCommands::begin_query is unimplemented");
242 void VulkanCommands::end_query(const QueryPool &, unsigned)
244 throw logic_error("VulkanCommands::end_query is unimplemented");
248 VulkanCommands::CommandPool::CommandPool(Device &d):
252 const VulkanFunctions &vk = device.get_functions();
254 VkCommandPoolCreateInfo pool_info = { };
255 pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
256 pool_info.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
257 pool_info.queueFamilyIndex = device.get_context().get_private().graphics_queue_family;
259 vk.CreateCommandPool(pool_info, pool);
262 VulkanCommands::CommandPool::CommandPool(CommandPool &&other):
263 device(other.device),
265 fence(move(other.fence)),
271 VulkanCommands::CommandPool::~CommandPool()
273 const VulkanFunctions &vk = device.get_functions();
276 vk.DestroyCommandPool(pool);