]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/commands_backend.cpp
Move PipelineState refresh to just before draw call
[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 "swapchaintexture.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()
32 {
33         if(!current_pool)
34                 throw invalid_operation("VulkanCommands::begin_buffer");
35
36         const VulkanFunctions &vk = device.get_functions();
37
38         if(!current_pool->in_use)
39         {
40                 current_pool->fence.reset();
41                 current_pool->in_use = true;
42         }
43
44         VkCommandBufferAllocateInfo alloc_info = { };
45         alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
46         alloc_info.commandPool = handle_cast<::VkCommandPool>(current_pool->pool);
47         alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
48         alloc_info.commandBufferCount = 1;
49
50         vk.AllocateCommandBuffers(alloc_info, &current_buffer);
51
52         VkCommandBufferBeginInfo begin_info = { };
53         begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
54         begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
55
56         vk.BeginCommandBuffer(current_buffer, begin_info);
57 }
58
59 void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_values)
60 {
61         framebuffer = pipeline_state->get_framebuffer();
62         if(!framebuffer)
63                 throw invalid_operation("VulkanCommands::begin_render_pass");
64
65         const VulkanFunctions &vk = device.get_functions();
66
67         if(!current_buffer)
68                 begin_buffer();
69
70         device.get_transfer_queue().dispatch_transfers(current_buffer);
71
72         Synchronizer &sync = device.get_synchronizer();
73         sync.reset();
74         sync.barrier(current_buffer);
75
76         bool to_present = false;
77         unsigned n_attachments = framebuffer->get_format().size();
78         for(unsigned i=0; i<n_attachments; ++i)
79                 if(dynamic_cast<const SwapChainTexture *>(framebuffer->get_attachment(i)))
80                         to_present = true;
81         VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(framebuffer->get_format(), clear, !clear_values, to_present);
82
83         framebuffer->refresh();
84
85         VkRenderPassBeginInfo begin_info = { };
86         begin_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
87         begin_info.renderPass = handle_cast<::VkRenderPass>(render_pass);
88         begin_info.framebuffer = handle_cast<::VkFramebuffer>(framebuffer->handle);
89
90         viewport = pipeline_state->get_viewport();
91         if(viewport)
92         {
93                 begin_info.renderArea.offset.x = viewport->left;
94                 begin_info.renderArea.offset.y = viewport->bottom;
95                 begin_info.renderArea.extent.width = viewport->width;
96                 begin_info.renderArea.extent.height = viewport->height;
97         }
98         else
99         {
100                 begin_info.renderArea.extent.width = framebuffer->get_width();
101                 begin_info.renderArea.extent.height = framebuffer->get_height();
102         }
103
104         VkClearValue vk_clear_values[7];
105         if(clear_values)
106         {
107                 unsigned i = 0;
108                 for(FrameAttachment a: framebuffer->get_format())
109                 {
110                         if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
111                                 vk_clear_values[i].depthStencil.depth = clear_values[i].depth_stencil.depth;
112                         else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
113                                 vk_clear_values[i].depthStencil.stencil = clear_values[i].depth_stencil.stencil;
114                         else
115                         {
116                                 vk_clear_values[i].color.float32[0] = clear_values[i].color.r;
117                                 vk_clear_values[i].color.float32[1] = clear_values[i].color.g;
118                                 vk_clear_values[i].color.float32[2] = clear_values[i].color.b;
119                                 vk_clear_values[i].color.float32[3] = clear_values[i].color.a;
120                         }
121                         ++i;
122                 }
123
124                 begin_info.clearValueCount = framebuffer->get_format().size();
125                 begin_info.pClearValues = vk_clear_values;
126         }
127
128         vk.CmdBeginRenderPass(current_buffer, begin_info, VK_SUBPASS_CONTENTS_INLINE);
129 }
130
131 void VulkanCommands::end_render_pass()
132 {
133         const VulkanFunctions &vk = device.get_functions();
134
135         vk.CmdEndRenderPass(current_buffer);
136         framebuffer = 0;
137         viewport = 0;
138 }
139
140 void VulkanCommands::begin_frame(unsigned index)
141 {
142         const VulkanFunctions &vk = device.get_functions();
143
144         unsigned pool_index = index%device.get_n_frames_in_flight();
145         if(pool_index>=command_pools.size())
146         {
147                 command_pools.reserve(pool_index+1);
148                 for(unsigned i=command_pools.size(); i<pool_index+1; ++i)
149                         command_pools.emplace_back(device);
150         }
151
152         current_pool = &command_pools[pool_index];
153         if(current_pool->in_use)
154         {
155                 current_pool->fence.wait();
156                 vk.ResetCommandPool(current_pool->pool, 0);
157                 current_pool->in_use = false;
158         }
159 }
160
161 void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem)
162 {
163         if(!current_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(current_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 *>(&current_buffer);
183         submit_info.signalSemaphoreCount = (signal_sem ? 1 : 0);
184         submit_info.pSignalSemaphores = &vk_signal_sem;
185
186         vk.QueueSubmit(1, &submit_info, current_pool->fence.handle);
187
188         current_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         const VulkanFunctions &vk = device.get_functions();
219
220         if(!framebuffer)
221                  begin_render_pass(false, 0);
222
223         pipeline_state->refresh();
224         pipeline_state->apply(current_buffer);
225         unsigned first_index = batch.get_offset()/batch.get_index_size();
226         vk.CmdDrawIndexed(current_buffer, batch.size(), count, first_index, 0, 0);
227 }
228
229 void VulkanCommands::resolve_multisample(Framebuffer &)
230 {
231         throw logic_error("VulkanCommands::resolve_multisample is unimplemented");
232 }
233
234 void VulkanCommands::begin_query(const QueryPool &, unsigned)
235 {
236         throw logic_error("VulkanCommands::begin_query is unimplemented");
237 }
238
239 void VulkanCommands::end_query(const QueryPool &, unsigned)
240 {
241         throw logic_error("VulkanCommands::end_query is unimplemented");
242 }
243
244
245 VulkanCommands::CommandPool::CommandPool(Device &d):
246         device(d),
247         fence(true)
248 {
249         const VulkanFunctions &vk = device.get_functions();
250
251         VkCommandPoolCreateInfo pool_info = { };
252         pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
253         pool_info.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
254         pool_info.queueFamilyIndex = device.get_context().get_private().graphics_queue_family;
255
256         vk.CreateCommandPool(pool_info, pool);
257 }
258
259 VulkanCommands::CommandPool::CommandPool(CommandPool &&other):
260         device(other.device),
261         pool(other.pool),
262         fence(move(other.fence)),
263         in_use(other.in_use)
264 {
265         other.pool = 0;
266 }
267
268 VulkanCommands::CommandPool::~CommandPool()
269 {
270         const VulkanFunctions &vk = device.get_functions();
271
272         if(pool)
273                 vk.DestroyCommandPool(pool);
274 }
275
276 } // namespace GL
277 } // namespace Msp