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