]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/commands_backend.cpp
5de0f090a9d774aa7e6c8d76d6dc4ebcf7bc5dd8
[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 "renderpass.h"
12 #include "semaphore.h"
13 #include "vulkan.h"
14
15 using namespace std;
16
17 namespace Msp {
18 namespace GL {
19
20 VulkanCommands::VulkanCommands():
21         device(Device::get_current())
22 { }
23
24 VulkanCommands::~VulkanCommands()
25 {
26         const VulkanFunctions &vk = device.get_functions();
27
28         vk.QueueWaitIdle();
29 }
30
31 void VulkanCommands::begin_buffer(VkRenderPass render_pass)
32 {
33         if(frame_index>=command_pools.size())
34                 throw invalid_operation("VulkanCommands::begin_buffer");
35
36         const VulkanFunctions &vk = device.get_functions();
37
38         CommandPool &current_pool = command_pools[frame_index];
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         last_pipeline = 0;
81 }
82
83 void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_values)
84 {
85         framebuffer = pipeline_state->get_framebuffer();
86         if(!framebuffer)
87                 throw invalid_operation("VulkanCommands::begin_render_pass");
88
89         viewport = pipeline_state->get_viewport();
90
91         if(!primary_buffer)
92                 begin_buffer(0);
93
94         fb_is_swapchain = framebuffer->is_presentable();
95         framebuffer->refresh();
96
97         RenderPass render_pass;
98         render_pass.framebuffer = framebuffer;
99         render_pass.render_area = viewport;
100         render_pass.clear = clear;
101         render_pass.clear_values = clear_values;
102         render_pass.to_present = fb_is_swapchain;
103         render_pass.update(device);
104
105         discard_fb_contents = render_pass.discard_fb_contents;
106
107         begin_buffer(render_pass.handle);
108         render_pass.fill_begin_info(pass_begin_info);
109 }
110
111 void VulkanCommands::end_render_pass()
112 {
113         const VulkanFunctions &vk = device.get_functions();
114         VulkanCommandRecorder vkCmd(vk, primary_buffer);
115
116         vk.EndCommandBuffer(pass_buffer);
117
118         device.get_transfer_queue().dispatch_transfers(vkCmd);
119
120         Synchronizer &sync = device.get_synchronizer();
121         sync.reset();
122         if(!fb_is_swapchain)
123                 framebuffer->prepare_image_layouts(discard_fb_contents);
124         sync.barrier(vkCmd);
125
126         const VkRenderPassBeginInfo &begin_info = *reinterpret_cast<const VkRenderPassBeginInfo *>(pass_begin_info.data());
127         vkCmd.BeginRenderPass(begin_info, VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS);
128         vkCmd.ExecuteCommands(1, &pass_buffer);
129         vkCmd.EndRenderPass();
130
131         framebuffer = 0;
132         viewport = Rect::max();
133         pass_buffer = 0;
134 }
135
136 void VulkanCommands::begin_frame(unsigned index)
137 {
138         const VulkanFunctions &vk = device.get_functions();
139
140         frame_index = index%device.get_n_frames_in_flight();
141         if(frame_index>=command_pools.size())
142         {
143                 command_pools.reserve(frame_index+1);
144                 for(unsigned i=command_pools.size(); i<frame_index+1; ++i)
145                         command_pools.emplace_back(device);
146         }
147
148         CommandPool &current_pool = command_pools[frame_index];
149         if(current_pool.in_use)
150         {
151                 current_pool.fence.wait();
152                 vk.ResetCommandPool(current_pool.pool, 0);
153                 current_pool.in_use = false;
154                 current_pool.primary.next_buffer = 0;
155                 current_pool.secondary.next_buffer = 0;
156         }
157
158         device.get_descriptor_pool().begin_frame();
159 }
160
161 void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem)
162 {
163         if(!primary_buffer)
164                 return;
165
166         const VulkanFunctions &vk = device.get_functions();
167         ::VkSemaphore vk_wait_sem = (wait_sem ? handle_cast<::VkSemaphore>(wait_sem->handle) : 0);
168         ::VkSemaphore vk_signal_sem = (signal_sem ? handle_cast<::VkSemaphore>(signal_sem->handle) : 0);
169         VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
170
171         if(framebuffer)
172                 end_render_pass();
173
174         vk.EndCommandBuffer(primary_buffer);
175
176         VkSubmitInfo submit_info = { };
177         submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
178         submit_info.waitSemaphoreCount = (wait_sem ? 1 : 0);
179         submit_info.pWaitSemaphores = &vk_wait_sem;
180         submit_info.pWaitDstStageMask = &wait_stages;
181         submit_info.commandBufferCount = 1;
182         submit_info.pCommandBuffers = handle_cast<::VkCommandBuffer *>(&primary_buffer);
183         submit_info.signalSemaphoreCount = (signal_sem ? 1 : 0);
184         submit_info.pSignalSemaphores = &vk_signal_sem;
185
186         vk.QueueSubmit(1, &submit_info, command_pools[frame_index].fence.handle);
187
188         primary_buffer = 0;
189 }
190
191 void VulkanCommands::use_pipeline(const PipelineState *ps)
192 {
193         if(!pipeline_state || !ps || ps->get_framebuffer()!=framebuffer || ps->get_viewport()!=viewport)
194                 if(framebuffer)
195                         end_render_pass();
196
197         pipeline_state = ps;
198 }
199
200 void VulkanCommands::clear(const ClearValue *values)
201 {
202         if(framebuffer)
203                 throw invalid_operation("VulkanCommands::clear");
204
205         begin_render_pass(true, values);
206 }
207
208 void VulkanCommands::draw(const Batch &batch)
209 {
210         draw_instanced(batch, 1);
211 }
212
213 void VulkanCommands::draw_instanced(const Batch &batch, unsigned count)
214 {
215         if(!pipeline_state)
216                 throw invalid_operation("VulkanCommands::draw_instanced");
217
218         if(!framebuffer)
219                  begin_render_pass(false, 0);
220
221         VulkanCommandRecorder vkCmd(device.get_functions(), pass_buffer);
222
223         pipeline_state->refresh();
224         pipeline_state->apply(vkCmd, last_pipeline, frame_index, fb_is_swapchain);
225         last_pipeline = pipeline_state;
226         unsigned first_index = batch.get_offset()/batch.get_index_size();
227         vkCmd.DrawIndexed(batch.size(), count, first_index, 0, 0);
228 }
229
230 void VulkanCommands::dispatch(unsigned count_x, unsigned count_y, unsigned count_z)
231 {
232         if(!pipeline_state)
233                 throw invalid_operation("VulkanCommands::draw_instanced");
234
235         if(framebuffer)
236                 end_render_pass();
237
238         VulkanCommandRecorder vkCmd(device.get_functions(), primary_buffer);
239
240         pipeline_state->refresh();
241         pipeline_state->synchronize_resources();
242         device.get_synchronizer().barrier(vkCmd);
243         pipeline_state->apply(vkCmd, 0, frame_index, false);
244         vkCmd.Dispatch(count_x, count_y, count_z);
245 }
246
247 void VulkanCommands::resolve_multisample()
248 {
249         if(!framebuffer || !framebuffer->has_resolve_attachments())
250                 throw invalid_operation("VulkanCommands::resolve_multisample");
251
252         end_render_pass();
253 }
254
255 void VulkanCommands::begin_query(const QueryPool &, unsigned)
256 {
257         throw logic_error("VulkanCommands::begin_query is unimplemented");
258 }
259
260 void VulkanCommands::end_query(const QueryPool &, unsigned)
261 {
262         throw logic_error("VulkanCommands::end_query is unimplemented");
263 }
264
265
266 VulkanCommands::CommandPool::CommandPool(Device &d):
267         device(d),
268         fence(true)
269 {
270         const VulkanFunctions &vk = device.get_functions();
271
272         VkCommandPoolCreateInfo pool_info = { };
273         pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
274         pool_info.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
275         pool_info.queueFamilyIndex = device.get_context().get_private().graphics_queue_family;
276
277         vk.CreateCommandPool(pool_info, pool);
278 }
279
280 VulkanCommands::CommandPool::CommandPool(CommandPool &&other):
281         device(other.device),
282         pool(other.pool),
283         fence(move(other.fence)),
284         in_use(other.in_use)
285 {
286         other.pool = 0;
287 }
288
289 VulkanCommands::CommandPool::~CommandPool()
290 {
291         const VulkanFunctions &vk = device.get_functions();
292
293         if(pool)
294                 vk.DestroyCommandPool(pool);
295 }
296
297 } // namespace GL
298 } // namespace Msp