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 "structurebuilder.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)
35 throw invalid_operation("VulkanCommands::begin_buffer");
37 const VulkanFunctions &vk = device.get_functions();
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);
82 void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_values)
84 framebuffer = pipeline_state->get_framebuffer();
86 throw invalid_operation("VulkanCommands::begin_render_pass");
88 viewport = pipeline_state->get_viewport();
93 Synchronizer &sync = device.get_synchronizer();
96 fb_is_swapchain = false;
97 unsigned n_attachments = framebuffer->get_format().size();
98 for(unsigned i=0; (!fb_is_swapchain && i<n_attachments); ++i)
99 if(dynamic_cast<const SwapChainTexture *>(framebuffer->get_attachment(i)))
100 fb_is_swapchain = true;
102 framebuffer->prepare_image_layouts(clear && !viewport);
103 VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(framebuffer->get_format(), clear, (!clear_values && !viewport), fb_is_swapchain);
105 framebuffer->refresh();
107 sync.barrier(primary_buffer);
109 begin_buffer(render_pass);
111 StructureBuilder sb(pass_begin_info, 2);
112 VkRenderPassBeginInfo *&begin_info = sb.add<VkRenderPassBeginInfo>(1);
113 VkClearValue *&vk_clear_values = sb.add<VkClearValue>(FrameFormat::MAX_ATTACHMENTS);
115 begin_info->sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
116 begin_info->renderPass = handle_cast<::VkRenderPass>(render_pass);
117 begin_info->framebuffer = handle_cast<::VkFramebuffer>(framebuffer->handle);
121 begin_info->renderArea.offset.x = viewport->left;
122 begin_info->renderArea.offset.y = viewport->bottom;
123 begin_info->renderArea.extent.width = viewport->width;
124 begin_info->renderArea.extent.height = viewport->height;
128 begin_info->renderArea.extent.width = framebuffer->get_width();
129 begin_info->renderArea.extent.height = framebuffer->get_height();
135 for(FrameAttachment a: framebuffer->get_format())
137 if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
138 vk_clear_values[i].depthStencil.depth = clear_values[i].depth_stencil.depth;
139 else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
140 vk_clear_values[i].depthStencil.stencil = clear_values[i].depth_stencil.stencil;
143 vk_clear_values[i].color.float32[0] = clear_values[i].color.r;
144 vk_clear_values[i].color.float32[1] = clear_values[i].color.g;
145 vk_clear_values[i].color.float32[2] = clear_values[i].color.b;
146 vk_clear_values[i].color.float32[3] = clear_values[i].color.a;
151 begin_info->clearValueCount = framebuffer->get_format().size();
152 begin_info->pClearValues = vk_clear_values;
156 void VulkanCommands::end_render_pass()
158 const VulkanFunctions &vk = device.get_functions();
160 vk.EndCommandBuffer(pass_buffer);
162 const VkRenderPassBeginInfo &begin_info = *reinterpret_cast<const VkRenderPassBeginInfo *>(pass_begin_info.data());
163 vk.CmdBeginRenderPass(primary_buffer, begin_info, VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS);
164 vk.CmdExecuteCommands(primary_buffer, 1, &pass_buffer);
165 vk.CmdEndRenderPass(primary_buffer);
172 void VulkanCommands::begin_frame(unsigned index)
174 const VulkanFunctions &vk = device.get_functions();
176 unsigned pool_index = index%device.get_n_frames_in_flight();
177 if(pool_index>=command_pools.size())
179 command_pools.reserve(pool_index+1);
180 for(unsigned i=command_pools.size(); i<pool_index+1; ++i)
181 command_pools.emplace_back(device);
184 current_pool = &command_pools[pool_index];
185 if(current_pool->in_use)
187 current_pool->fence.wait();
188 vk.ResetCommandPool(current_pool->pool, 0);
189 current_pool->in_use = false;
190 current_pool->primary.next_buffer = 0;
191 current_pool->secondary.next_buffer = 0;
195 void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem)
200 const VulkanFunctions &vk = device.get_functions();
201 ::VkSemaphore vk_wait_sem = (wait_sem ? handle_cast<::VkSemaphore>(wait_sem->handle) : 0);
202 ::VkSemaphore vk_signal_sem = (signal_sem ? handle_cast<::VkSemaphore>(signal_sem->handle) : 0);
203 VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
208 vk.EndCommandBuffer(primary_buffer);
210 VkSubmitInfo submit_info = { };
211 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
212 submit_info.waitSemaphoreCount = (wait_sem ? 1 : 0);
213 submit_info.pWaitSemaphores = &vk_wait_sem;
214 submit_info.pWaitDstStageMask = &wait_stages;
215 submit_info.commandBufferCount = 1;
216 submit_info.pCommandBuffers = handle_cast<::VkCommandBuffer *>(&primary_buffer);
217 submit_info.signalSemaphoreCount = (signal_sem ? 1 : 0);
218 submit_info.pSignalSemaphores = &vk_signal_sem;
220 vk.QueueSubmit(1, &submit_info, current_pool->fence.handle);
225 void VulkanCommands::use_pipeline(const PipelineState *ps)
227 if(!pipeline_state || !ps || ps->get_framebuffer()!=framebuffer || ps->get_viewport()!=viewport)
234 void VulkanCommands::clear(const ClearValue *values)
237 throw invalid_operation("VulkanCommands::clear");
239 begin_render_pass(true, values);
242 void VulkanCommands::draw(const Batch &batch)
244 draw_instanced(batch, 1);
247 void VulkanCommands::draw_instanced(const Batch &batch, unsigned count)
250 throw invalid_operation("VulkanCommands::draw_instanced");
252 const VulkanFunctions &vk = device.get_functions();
255 begin_render_pass(false, 0);
257 pipeline_state->refresh();
258 pipeline_state->apply(pass_buffer, fb_is_swapchain);
259 unsigned first_index = batch.get_offset()/batch.get_index_size();
260 vk.CmdDrawIndexed(pass_buffer, batch.size(), count, first_index, 0, 0);
263 void VulkanCommands::resolve_multisample(Framebuffer &)
265 throw logic_error("VulkanCommands::resolve_multisample is unimplemented");
268 void VulkanCommands::begin_query(const QueryPool &, unsigned)
270 throw logic_error("VulkanCommands::begin_query is unimplemented");
273 void VulkanCommands::end_query(const QueryPool &, unsigned)
275 throw logic_error("VulkanCommands::end_query is unimplemented");
279 VulkanCommands::CommandPool::CommandPool(Device &d):
283 const VulkanFunctions &vk = device.get_functions();
285 VkCommandPoolCreateInfo pool_info = { };
286 pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
287 pool_info.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
288 pool_info.queueFamilyIndex = device.get_context().get_private().graphics_queue_family;
290 vk.CreateCommandPool(pool_info, pool);
293 VulkanCommands::CommandPool::CommandPool(CommandPool &&other):
294 device(other.device),
296 fence(move(other.fence)),
302 VulkanCommands::CommandPool::~CommandPool()
304 const VulkanFunctions &vk = device.get_functions();
307 vk.DestroyCommandPool(pool);