]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/commands_backend.cpp
b7a342defb5ab741eb16f3b9c3bc06af6f8d7555
[libs/gl.git] / source / backends / vulkan / commands_backend.cpp
1 #include <msp/core/hash.h>
2 #include <msp/graphics/vulkancontext_platform.h>
3 #include "batch.h"
4 #include "commands_backend.h"
5 #include "device.h"
6 #include "error.h"
7 #include "framebuffer.h"
8 #include "frameformat.h"
9 #include "pipelinestate.h"
10 #include "rect.h"
11 #include "semaphore.h"
12 #include "structurebuilder.h"
13 #include "swapchaintexture.h"
14 #include "vulkan.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GL {
20
21 VulkanCommands::VulkanCommands():
22         device(Device::get_current())
23 { }
24
25 VulkanCommands::~VulkanCommands()
26 {
27         const VulkanFunctions &vk = device.get_functions();
28
29         vk.QueueWaitIdle();
30 }
31
32 void VulkanCommands::begin_buffer(VkRenderPass render_pass)
33 {
34         if(!current_pool)
35                 throw invalid_operation("VulkanCommands::begin_buffer");
36
37         const VulkanFunctions &vk = device.get_functions();
38
39         if(!current_pool->in_use)
40         {
41                 current_pool->fence.reset();
42                 current_pool->in_use = true;
43         }
44
45         CommandBuffers &buffers = (render_pass ? current_pool->secondary : current_pool->primary);
46
47         if(buffers.next_buffer>=buffers.buffers.size())
48         {
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;
54
55                 VkCommandBuffer buffer;
56                 vk.AllocateCommandBuffers(alloc_info, &buffer);
57                 buffers.buffers.push_back(buffer);
58         }
59
60         VkCommandBuffer buffer = buffers.buffers[buffers.next_buffer++];
61         (render_pass ? pass_buffer : primary_buffer) = buffer;
62
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;
66         if(render_pass)
67                 begin_info.flags |= VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
68
69         VkCommandBufferInheritanceInfo inherit_info = { };
70         if(render_pass)
71         {
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;
75
76                 begin_info.pInheritanceInfo = &inherit_info;
77         }
78
79         vk.BeginCommandBuffer(buffer, begin_info);
80 }
81
82 void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_values)
83 {
84         framebuffer = pipeline_state->get_framebuffer();
85         if(!framebuffer)
86                 throw invalid_operation("VulkanCommands::begin_render_pass");
87
88         viewport = pipeline_state->get_viewport();
89
90         if(!primary_buffer)
91                 begin_buffer(0);
92
93         fb_is_swapchain = false;
94         unsigned n_attachments = framebuffer->get_format().size();
95         for(unsigned i=0; (!fb_is_swapchain && i<n_attachments); ++i)
96                 if(dynamic_cast<const SwapChainTexture *>(framebuffer->get_attachment(i)))
97                         fb_is_swapchain = true;
98
99         discard_fb_contents = (clear && !viewport);
100
101         framebuffer->refresh();
102
103         VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(framebuffer->get_format(), clear, (!clear_values && !viewport), fb_is_swapchain);
104         begin_buffer(render_pass);
105
106         StructureBuilder sb(pass_begin_info, 2);
107         VkRenderPassBeginInfo *&begin_info = sb.add<VkRenderPassBeginInfo>(1);
108         VkClearValue *&vk_clear_values = sb.add<VkClearValue>(FrameFormat::MAX_ATTACHMENTS);
109
110         begin_info->sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
111         begin_info->renderPass = handle_cast<::VkRenderPass>(render_pass);
112         begin_info->framebuffer = handle_cast<::VkFramebuffer>(framebuffer->handle);
113
114         if(viewport)
115         {
116                 begin_info->renderArea.offset.x = viewport->left;
117                 begin_info->renderArea.offset.y = viewport->bottom;
118                 begin_info->renderArea.extent.width = viewport->width;
119                 begin_info->renderArea.extent.height = viewport->height;
120         }
121         else
122         {
123                 begin_info->renderArea.extent.width = framebuffer->get_width();
124                 begin_info->renderArea.extent.height = framebuffer->get_height();
125         }
126
127         if(clear_values)
128         {
129                 unsigned i = 0;
130                 for(FrameAttachment a: framebuffer->get_format())
131                 {
132                         if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
133                                 vk_clear_values[i].depthStencil.depth = clear_values[i].depth_stencil.depth;
134                         else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
135                                 vk_clear_values[i].depthStencil.stencil = clear_values[i].depth_stencil.stencil;
136                         else
137                         {
138                                 vk_clear_values[i].color.float32[0] = clear_values[i].color.r;
139                                 vk_clear_values[i].color.float32[1] = clear_values[i].color.g;
140                                 vk_clear_values[i].color.float32[2] = clear_values[i].color.b;
141                                 vk_clear_values[i].color.float32[3] = clear_values[i].color.a;
142                         }
143                         ++i;
144                 }
145
146                 begin_info->clearValueCount = framebuffer->get_format().size();
147                 begin_info->pClearValues = vk_clear_values;
148         }
149 }
150
151 void VulkanCommands::end_render_pass()
152 {
153         const VulkanFunctions &vk = device.get_functions();
154
155         vk.EndCommandBuffer(pass_buffer);
156
157         device.get_transfer_queue().dispatch_transfers(primary_buffer);
158
159         Synchronizer &sync = device.get_synchronizer();
160         sync.reset();
161         if(!fb_is_swapchain)
162                 framebuffer->prepare_image_layouts(discard_fb_contents);
163         sync.barrier(primary_buffer);
164
165         const VkRenderPassBeginInfo &begin_info = *reinterpret_cast<const VkRenderPassBeginInfo *>(pass_begin_info.data());
166         vk.CmdBeginRenderPass(primary_buffer, begin_info, VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS);
167         vk.CmdExecuteCommands(primary_buffer, 1, &pass_buffer);
168         vk.CmdEndRenderPass(primary_buffer);
169
170         framebuffer = 0;
171         viewport = 0;
172         pass_buffer = 0;
173 }
174
175 void VulkanCommands::begin_frame(unsigned index)
176 {
177         const VulkanFunctions &vk = device.get_functions();
178
179         unsigned pool_index = index%device.get_n_frames_in_flight();
180         if(pool_index>=command_pools.size())
181         {
182                 command_pools.reserve(pool_index+1);
183                 for(unsigned i=command_pools.size(); i<pool_index+1; ++i)
184                         command_pools.emplace_back(device);
185         }
186
187         current_pool = &command_pools[pool_index];
188         if(current_pool->in_use)
189         {
190                 current_pool->fence.wait();
191                 vk.ResetCommandPool(current_pool->pool, 0);
192                 current_pool->in_use = false;
193                 current_pool->primary.next_buffer = 0;
194                 current_pool->secondary.next_buffer = 0;
195         }
196 }
197
198 void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem)
199 {
200         if(!primary_buffer)
201                 return;
202
203         const VulkanFunctions &vk = device.get_functions();
204         ::VkSemaphore vk_wait_sem = (wait_sem ? handle_cast<::VkSemaphore>(wait_sem->handle) : 0);
205         ::VkSemaphore vk_signal_sem = (signal_sem ? handle_cast<::VkSemaphore>(signal_sem->handle) : 0);
206         VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
207
208         if(framebuffer)
209                 end_render_pass();
210
211         vk.EndCommandBuffer(primary_buffer);
212
213         VkSubmitInfo submit_info = { };
214         submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
215         submit_info.waitSemaphoreCount = (wait_sem ? 1 : 0);
216         submit_info.pWaitSemaphores = &vk_wait_sem;
217         submit_info.pWaitDstStageMask = &wait_stages;
218         submit_info.commandBufferCount = 1;
219         submit_info.pCommandBuffers = handle_cast<::VkCommandBuffer *>(&primary_buffer);
220         submit_info.signalSemaphoreCount = (signal_sem ? 1 : 0);
221         submit_info.pSignalSemaphores = &vk_signal_sem;
222
223         vk.QueueSubmit(1, &submit_info, current_pool->fence.handle);
224
225         primary_buffer = 0;
226 }
227
228 void VulkanCommands::use_pipeline(const PipelineState *ps)
229 {
230         if(!pipeline_state || !ps || ps->get_framebuffer()!=framebuffer || ps->get_viewport()!=viewport)
231                 if(framebuffer)
232                         end_render_pass();
233
234         pipeline_state = ps;
235 }
236
237 void VulkanCommands::clear(const ClearValue *values)
238 {
239         if(framebuffer)
240                 throw invalid_operation("VulkanCommands::clear");
241
242         begin_render_pass(true, values);
243 }
244
245 void VulkanCommands::draw(const Batch &batch)
246 {
247         draw_instanced(batch, 1);
248 }
249
250 void VulkanCommands::draw_instanced(const Batch &batch, unsigned count)
251 {
252         if(!pipeline_state)
253                 throw invalid_operation("VulkanCommands::draw_instanced");
254
255         const VulkanFunctions &vk = device.get_functions();
256
257         if(!framebuffer)
258                  begin_render_pass(false, 0);
259
260         pipeline_state->refresh();
261         pipeline_state->apply(pass_buffer, fb_is_swapchain);
262         unsigned first_index = batch.get_offset()/batch.get_index_size();
263         vk.CmdDrawIndexed(pass_buffer, batch.size(), count, first_index, 0, 0);
264 }
265
266 void VulkanCommands::resolve_multisample(Framebuffer &)
267 {
268         throw logic_error("VulkanCommands::resolve_multisample is unimplemented");
269 }
270
271 void VulkanCommands::begin_query(const QueryPool &, unsigned)
272 {
273         throw logic_error("VulkanCommands::begin_query is unimplemented");
274 }
275
276 void VulkanCommands::end_query(const QueryPool &, unsigned)
277 {
278         throw logic_error("VulkanCommands::end_query is unimplemented");
279 }
280
281
282 VulkanCommands::CommandPool::CommandPool(Device &d):
283         device(d),
284         fence(true)
285 {
286         const VulkanFunctions &vk = device.get_functions();
287
288         VkCommandPoolCreateInfo pool_info = { };
289         pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
290         pool_info.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
291         pool_info.queueFamilyIndex = device.get_context().get_private().graphics_queue_family;
292
293         vk.CreateCommandPool(pool_info, pool);
294 }
295
296 VulkanCommands::CommandPool::CommandPool(CommandPool &&other):
297         device(other.device),
298         pool(other.pool),
299         fence(move(other.fence)),
300         in_use(other.in_use)
301 {
302         other.pool = 0;
303 }
304
305 VulkanCommands::CommandPool::~CommandPool()
306 {
307         const VulkanFunctions &vk = device.get_functions();
308
309         if(pool)
310                 vk.DestroyCommandPool(pool);
311 }
312
313 } // namespace GL
314 } // namespace Msp