]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/commands_backend.cpp
Check if render target is swapchain and set to_present accordingly
[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         const Framebuffer *target = pipeline_state->get_framebuffer();
62         if(!target)
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         bool to_present = false;
73         unsigned n_attachments = target->get_format().size();
74         for(unsigned i=0; i<n_attachments; ++i)
75                 if(dynamic_cast<const SwapChainTexture *>(target->VulkanFramebuffer::get_attachment(i)))
76                         to_present = true;
77         VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(target->get_format(), clear, !clear_values, to_present);
78
79         target->refresh();
80
81         VkRenderPassBeginInfo begin_info = { };
82         begin_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
83         begin_info.renderPass = handle_cast<::VkRenderPass>(render_pass);
84         begin_info.framebuffer = handle_cast<::VkFramebuffer>(target->handle);
85
86         const Rect *viewport = pipeline_state->get_viewport();
87         if(viewport)
88         {
89                 begin_info.renderArea.offset.x = viewport->left;
90                 begin_info.renderArea.offset.y = viewport->bottom;
91                 begin_info.renderArea.extent.width = viewport->width;
92                 begin_info.renderArea.extent.height = viewport->height;
93         }
94         else
95         {
96                 begin_info.renderArea.extent.width = target->get_width();
97                 begin_info.renderArea.extent.height = target->get_height();
98         }
99
100         VkClearValue vk_clear_values[7];
101         if(clear_values)
102         {
103                 unsigned i = 0;
104                 for(FrameAttachment a: target->get_format())
105                 {
106                         if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
107                                 vk_clear_values[i].depthStencil.depth = clear_values[i].depth_stencil.depth;
108                         else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
109                                 vk_clear_values[i].depthStencil.stencil = clear_values[i].depth_stencil.stencil;
110                         else
111                         {
112                                 vk_clear_values[i].color.float32[0] = clear_values[i].color.r;
113                                 vk_clear_values[i].color.float32[1] = clear_values[i].color.g;
114                                 vk_clear_values[i].color.float32[2] = clear_values[i].color.b;
115                                 vk_clear_values[i].color.float32[3] = clear_values[i].color.a;
116                         }
117                         ++i;
118                 }
119
120                 begin_info.clearValueCount = target->get_format().size();
121                 begin_info.pClearValues = vk_clear_values;
122         }
123
124         vk.CmdBeginRenderPass(current_buffer, begin_info, VK_SUBPASS_CONTENTS_INLINE);
125 }
126
127 void VulkanCommands::end_render_pass()
128 {
129         const VulkanFunctions &vk = device.get_functions();
130
131         vk.CmdEndRenderPass(current_buffer);
132         render_pass = 0;
133 }
134
135 void VulkanCommands::begin_frame(unsigned index)
136 {
137         const VulkanFunctions &vk = device.get_functions();
138
139         unsigned pool_index = index%device.get_n_frames_in_flight();
140         if(pool_index>=command_pools.size())
141         {
142                 command_pools.reserve(pool_index+1);
143                 for(unsigned i=command_pools.size(); i<pool_index+1; ++i)
144                         command_pools.emplace_back(device);
145         }
146
147         current_pool = &command_pools[pool_index];
148         if(current_pool->in_use)
149         {
150                 current_pool->fence.wait();
151                 vk.ResetCommandPool(current_pool->pool, 0);
152                 current_pool->in_use = false;
153         }
154 }
155
156 void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem)
157 {
158         if(!current_buffer)
159                 return;
160
161         const VulkanFunctions &vk = device.get_functions();
162         ::VkSemaphore vk_wait_sem = (wait_sem ? handle_cast<::VkSemaphore>(wait_sem->handle) : 0);
163         ::VkSemaphore vk_signal_sem = (signal_sem ? handle_cast<::VkSemaphore>(signal_sem->handle) : 0);
164         VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
165
166         if(render_pass)
167                 end_render_pass();
168
169         vk.EndCommandBuffer(current_buffer);
170
171         VkSubmitInfo submit_info = { };
172         submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
173         submit_info.waitSemaphoreCount = (wait_sem ? 1 : 0);
174         submit_info.pWaitSemaphores = &vk_wait_sem;
175         submit_info.pWaitDstStageMask = &wait_stages;
176         submit_info.commandBufferCount = 1;
177         submit_info.pCommandBuffers = handle_cast<::VkCommandBuffer *>(&current_buffer);
178         submit_info.signalSemaphoreCount = (signal_sem ? 1 : 0);
179         submit_info.pSignalSemaphores = &vk_signal_sem;
180
181         vk.QueueSubmit(1, &submit_info, current_pool->fence.handle);
182
183         current_buffer = 0;
184 }
185
186 void VulkanCommands::use_pipeline(const PipelineState *ps)
187 {
188         if(!pipeline_state || !ps || ps->get_framebuffer()!=pipeline_state->get_framebuffer() || ps->get_viewport()!=pipeline_state->get_viewport())
189                 if(render_pass)
190                         end_render_pass();
191
192         pipeline_state = ps;
193         if(pipeline_state)
194                 pipeline_state->refresh();
195 }
196
197 void VulkanCommands::clear(const ClearValue *values)
198 {
199         if(render_pass)
200                 throw invalid_operation("VulkanCommands::clear");
201
202         begin_render_pass(true, values);
203 }
204
205 void VulkanCommands::draw(const Batch &batch)
206 {
207         draw_instanced(batch, 1);
208 }
209
210 void VulkanCommands::draw_instanced(const Batch &batch, unsigned count)
211 {
212         if(!pipeline_state)
213                 throw invalid_operation("VulkanCommands::draw_instanced");
214
215         const VulkanFunctions &vk = device.get_functions();
216
217         if(!render_pass)
218                  begin_render_pass(false, 0);
219
220         pipeline_state->apply(current_buffer);
221         unsigned first_index = batch.get_offset()/batch.get_index_size();
222         vk.CmdDrawIndexed(current_buffer, batch.size(), count, first_index, 0, 0);
223 }
224
225 void VulkanCommands::resolve_multisample(Framebuffer &)
226 {
227         throw logic_error("VulkanCommands::resolve_multisample is unimplemented");
228 }
229
230 void VulkanCommands::begin_query(const QueryPool &, unsigned)
231 {
232         throw logic_error("VulkanCommands::begin_query is unimplemented");
233 }
234
235 void VulkanCommands::end_query(const QueryPool &, unsigned)
236 {
237         throw logic_error("VulkanCommands::end_query is unimplemented");
238 }
239
240
241 VulkanCommands::CommandPool::CommandPool(Device &d):
242         device(d),
243         fence(true)
244 {
245         const VulkanFunctions &vk = device.get_functions();
246
247         VkCommandPoolCreateInfo pool_info = { };
248         pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
249         pool_info.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
250         pool_info.queueFamilyIndex = device.get_context().get_private().graphics_queue_family;
251
252         vk.CreateCommandPool(pool_info, pool);
253 }
254
255 VulkanCommands::CommandPool::CommandPool(CommandPool &&other):
256         device(other.device),
257         pool(other.pool),
258         fence(move(other.fence)),
259         in_use(other.in_use)
260 {
261         other.pool = 0;
262 }
263
264 VulkanCommands::CommandPool::~CommandPool()
265 {
266         const VulkanFunctions &vk = device.get_functions();
267
268         if(pool)
269                 vk.DestroyCommandPool(pool);
270 }
271
272 } // namespace GL
273 } // namespace Msp