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 if(current_pool->next_buffer>=current_pool->buffers.size())
46 VkCommandBufferAllocateInfo alloc_info = { };
47 alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
48 alloc_info.commandPool = handle_cast<::VkCommandPool>(current_pool->pool);
49 alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
50 alloc_info.commandBufferCount = 1;
52 VkCommandBuffer buffer;
53 vk.AllocateCommandBuffers(alloc_info, &buffer);
54 current_pool->buffers.push_back(buffer);
57 current_buffer = current_pool->buffers[current_pool->next_buffer++];
59 VkCommandBufferBeginInfo begin_info = { };
60 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
61 begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
63 vk.BeginCommandBuffer(current_buffer, begin_info);
66 void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_values)
68 framebuffer = pipeline_state->get_framebuffer();
70 throw invalid_operation("VulkanCommands::begin_render_pass");
72 const VulkanFunctions &vk = device.get_functions();
77 device.get_transfer_queue().dispatch_transfers(current_buffer);
79 Synchronizer &sync = device.get_synchronizer();
82 bool to_present = false;
83 unsigned n_attachments = framebuffer->get_format().size();
84 for(unsigned i=0; i<n_attachments; ++i)
85 if(dynamic_cast<const SwapChainTexture *>(framebuffer->get_attachment(i)))
88 framebuffer->prepare_image_layouts(clear);
89 VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(framebuffer->get_format(), clear, !clear_values, to_present);
91 framebuffer->refresh();
93 sync.barrier(current_buffer);
95 VkRenderPassBeginInfo begin_info = { };
96 begin_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
97 begin_info.renderPass = handle_cast<::VkRenderPass>(render_pass);
98 begin_info.framebuffer = handle_cast<::VkFramebuffer>(framebuffer->handle);
100 viewport = pipeline_state->get_viewport();
103 begin_info.renderArea.offset.x = viewport->left;
104 begin_info.renderArea.offset.y = viewport->bottom;
105 begin_info.renderArea.extent.width = viewport->width;
106 begin_info.renderArea.extent.height = viewport->height;
110 begin_info.renderArea.extent.width = framebuffer->get_width();
111 begin_info.renderArea.extent.height = framebuffer->get_height();
114 VkClearValue vk_clear_values[7];
118 for(FrameAttachment a: framebuffer->get_format())
120 if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
121 vk_clear_values[i].depthStencil.depth = clear_values[i].depth_stencil.depth;
122 else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
123 vk_clear_values[i].depthStencil.stencil = clear_values[i].depth_stencil.stencil;
126 vk_clear_values[i].color.float32[0] = clear_values[i].color.r;
127 vk_clear_values[i].color.float32[1] = clear_values[i].color.g;
128 vk_clear_values[i].color.float32[2] = clear_values[i].color.b;
129 vk_clear_values[i].color.float32[3] = clear_values[i].color.a;
134 begin_info.clearValueCount = framebuffer->get_format().size();
135 begin_info.pClearValues = vk_clear_values;
138 vk.CmdBeginRenderPass(current_buffer, begin_info, VK_SUBPASS_CONTENTS_INLINE);
141 void VulkanCommands::end_render_pass()
143 const VulkanFunctions &vk = device.get_functions();
145 vk.CmdEndRenderPass(current_buffer);
150 void VulkanCommands::begin_frame(unsigned index)
152 const VulkanFunctions &vk = device.get_functions();
154 unsigned pool_index = index%device.get_n_frames_in_flight();
155 if(pool_index>=command_pools.size())
157 command_pools.reserve(pool_index+1);
158 for(unsigned i=command_pools.size(); i<pool_index+1; ++i)
159 command_pools.emplace_back(device);
162 current_pool = &command_pools[pool_index];
163 if(current_pool->in_use)
165 current_pool->fence.wait();
166 vk.ResetCommandPool(current_pool->pool, 0);
167 current_pool->in_use = false;
168 current_pool->next_buffer = 0;
172 void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem)
177 const VulkanFunctions &vk = device.get_functions();
178 ::VkSemaphore vk_wait_sem = (wait_sem ? handle_cast<::VkSemaphore>(wait_sem->handle) : 0);
179 ::VkSemaphore vk_signal_sem = (signal_sem ? handle_cast<::VkSemaphore>(signal_sem->handle) : 0);
180 VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
185 vk.EndCommandBuffer(current_buffer);
187 VkSubmitInfo submit_info = { };
188 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
189 submit_info.waitSemaphoreCount = (wait_sem ? 1 : 0);
190 submit_info.pWaitSemaphores = &vk_wait_sem;
191 submit_info.pWaitDstStageMask = &wait_stages;
192 submit_info.commandBufferCount = 1;
193 submit_info.pCommandBuffers = handle_cast<::VkCommandBuffer *>(¤t_buffer);
194 submit_info.signalSemaphoreCount = (signal_sem ? 1 : 0);
195 submit_info.pSignalSemaphores = &vk_signal_sem;
197 vk.QueueSubmit(1, &submit_info, current_pool->fence.handle);
202 void VulkanCommands::use_pipeline(const PipelineState *ps)
204 if(!pipeline_state || !ps || ps->get_framebuffer()!=framebuffer || ps->get_viewport()!=viewport)
211 void VulkanCommands::clear(const ClearValue *values)
214 throw invalid_operation("VulkanCommands::clear");
216 begin_render_pass(true, values);
219 void VulkanCommands::draw(const Batch &batch)
221 draw_instanced(batch, 1);
224 void VulkanCommands::draw_instanced(const Batch &batch, unsigned count)
227 throw invalid_operation("VulkanCommands::draw_instanced");
229 const VulkanFunctions &vk = device.get_functions();
232 begin_render_pass(false, 0);
234 pipeline_state->refresh();
235 pipeline_state->apply(current_buffer);
236 unsigned first_index = batch.get_offset()/batch.get_index_size();
237 vk.CmdDrawIndexed(current_buffer, batch.size(), count, first_index, 0, 0);
240 void VulkanCommands::resolve_multisample(Framebuffer &)
242 throw logic_error("VulkanCommands::resolve_multisample is unimplemented");
245 void VulkanCommands::begin_query(const QueryPool &, unsigned)
247 throw logic_error("VulkanCommands::begin_query is unimplemented");
250 void VulkanCommands::end_query(const QueryPool &, unsigned)
252 throw logic_error("VulkanCommands::end_query is unimplemented");
256 VulkanCommands::CommandPool::CommandPool(Device &d):
260 const VulkanFunctions &vk = device.get_functions();
262 VkCommandPoolCreateInfo pool_info = { };
263 pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
264 pool_info.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
265 pool_info.queueFamilyIndex = device.get_context().get_private().graphics_queue_family;
267 vk.CreateCommandPool(pool_info, pool);
270 VulkanCommands::CommandPool::CommandPool(CommandPool &&other):
271 device(other.device),
273 fence(move(other.fence)),
279 VulkanCommands::CommandPool::~CommandPool()
281 const VulkanFunctions &vk = device.get_functions();
284 vk.DestroyCommandPool(pool);