]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/commands_backend.cpp
Initial implementation of Vulkan backend
[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 "vulkan.h"
13
14 using namespace std;
15
16 namespace Msp {
17 namespace GL {
18
19 VulkanCommands::VulkanCommands():
20         device(Device::get_current())
21 { }
22
23 VulkanCommands::~VulkanCommands()
24 {
25         const VulkanFunctions &vk = device.get_functions();
26
27         vk.QueueWaitIdle();
28 }
29
30 void VulkanCommands::begin_buffer()
31 {
32         if(!current_pool)
33                 throw invalid_operation("VulkanCommands::begin_buffer");
34
35         const VulkanFunctions &vk = device.get_functions();
36
37         if(!current_pool->in_use)
38         {
39                 current_pool->fence.reset();
40                 current_pool->in_use = true;
41         }
42
43         VkCommandBufferAllocateInfo alloc_info = { };
44         alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
45         alloc_info.commandPool = handle_cast<::VkCommandPool>(current_pool->pool);
46         alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
47         alloc_info.commandBufferCount = 1;
48
49         vk.AllocateCommandBuffers(alloc_info, &current_buffer);
50
51         VkCommandBufferBeginInfo begin_info = { };
52         begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
53         begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
54
55         vk.BeginCommandBuffer(current_buffer, begin_info);
56 }
57
58 void VulkanCommands::begin_render_pass(const ClearValue *clear_values)
59 {
60         const Framebuffer *target = pipeline_state->get_framebuffer();
61         if(!target)
62                 throw invalid_operation("VulkanCommands::begin_render_pass");
63
64         const VulkanFunctions &vk = device.get_functions();
65
66         if(!current_buffer)
67                 begin_buffer();
68
69         device.get_transfer_queue().dispatch_transfers(current_buffer);
70
71         // TODO Use proper value for to_present
72         render_pass = device.get_pipeline_cache().get_render_pass(target->get_format(), clear_values, true);
73
74         target->refresh();
75
76         VkRenderPassBeginInfo begin_info = { };
77         begin_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
78         begin_info.renderPass = handle_cast<::VkRenderPass>(render_pass);
79         begin_info.framebuffer = handle_cast<::VkFramebuffer>(target->handle);
80
81         const Rect *viewport = pipeline_state->get_viewport();
82         if(viewport)
83         {
84                 begin_info.renderArea.offset.x = viewport->left;
85                 begin_info.renderArea.offset.y = viewport->bottom;
86                 begin_info.renderArea.extent.width = viewport->width;
87                 begin_info.renderArea.extent.height = viewport->height;
88         }
89         else
90         {
91                 begin_info.renderArea.extent.width = target->get_width();
92                 begin_info.renderArea.extent.height = target->get_height();
93         }
94
95         VkClearValue vk_clear_values[7];
96         if(clear_values)
97         {
98                 unsigned i = 0;
99                 for(FrameAttachment a: target->get_format())
100                 {
101                         if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
102                                 vk_clear_values[i].depthStencil.depth = clear_values[i].depth_stencil.depth;
103                         else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
104                                 vk_clear_values[i].depthStencil.stencil = clear_values[i].depth_stencil.stencil;
105                         else
106                         {
107                                 vk_clear_values[i].color.float32[0] = clear_values[i].color.r;
108                                 vk_clear_values[i].color.float32[1] = clear_values[i].color.g;
109                                 vk_clear_values[i].color.float32[2] = clear_values[i].color.b;
110                                 vk_clear_values[i].color.float32[3] = clear_values[i].color.a;
111                         }
112                         ++i;
113                 }
114
115                 begin_info.clearValueCount = target->get_format().size();
116                 begin_info.pClearValues = vk_clear_values;
117         }
118
119         vk.CmdBeginRenderPass(current_buffer, begin_info, VK_SUBPASS_CONTENTS_INLINE);
120 }
121
122 void VulkanCommands::end_render_pass()
123 {
124         const VulkanFunctions &vk = device.get_functions();
125
126         vk.CmdEndRenderPass(current_buffer);
127         render_pass = 0;
128 }
129
130 void VulkanCommands::begin_frame(unsigned index)
131 {
132         const VulkanFunctions &vk = device.get_functions();
133
134         unsigned pool_index = index%device.get_n_frames_in_flight();
135         if(pool_index>=command_pools.size())
136         {
137                 command_pools.reserve(pool_index+1);
138                 for(unsigned i=command_pools.size(); i<pool_index+1; ++i)
139                         command_pools.emplace_back(device);
140         }
141
142         current_pool = &command_pools[pool_index];
143         if(current_pool->in_use)
144         {
145                 current_pool->fence.wait();
146                 vk.ResetCommandPool(current_pool->pool, 0);
147                 current_pool->in_use = false;
148         }
149 }
150
151 void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem)
152 {
153         if(!current_buffer)
154                 return;
155
156         const VulkanFunctions &vk = device.get_functions();
157         ::VkSemaphore vk_wait_sem = (wait_sem ? handle_cast<::VkSemaphore>(wait_sem->handle) : 0);
158         ::VkSemaphore vk_signal_sem = (signal_sem ? handle_cast<::VkSemaphore>(signal_sem->handle) : 0);
159         VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
160
161         if(render_pass)
162                 end_render_pass();
163
164         vk.EndCommandBuffer(current_buffer);
165
166         VkSubmitInfo submit_info = { };
167         submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
168         submit_info.waitSemaphoreCount = (wait_sem ? 1 : 0);
169         submit_info.pWaitSemaphores = &vk_wait_sem;
170         submit_info.pWaitDstStageMask = &wait_stages;
171         submit_info.commandBufferCount = 1;
172         submit_info.pCommandBuffers = handle_cast<::VkCommandBuffer *>(&current_buffer);
173         submit_info.signalSemaphoreCount = (signal_sem ? 1 : 0);
174         submit_info.pSignalSemaphores = &vk_signal_sem;
175
176         vk.QueueSubmit(1, &submit_info, current_pool->fence.handle);
177
178         current_buffer = 0;
179 }
180
181 void VulkanCommands::use_pipeline(const PipelineState *ps)
182 {
183         if(!pipeline_state || !ps || ps->get_framebuffer()!=pipeline_state->get_framebuffer() || ps->get_viewport()!=pipeline_state->get_viewport())
184                 if(render_pass)
185                         end_render_pass();
186
187         pipeline_state = ps;
188         if(pipeline_state)
189                 pipeline_state->refresh();
190 }
191
192 void VulkanCommands::clear(const ClearValue *values)
193 {
194         if(render_pass)
195                 throw invalid_operation("VulkanCommands::clear");
196
197         begin_render_pass(values);
198 }
199
200 void VulkanCommands::draw(const Batch &batch)
201 {
202         draw_instanced(batch, 1);
203 }
204
205 void VulkanCommands::draw_instanced(const Batch &batch, unsigned count)
206 {
207         if(!pipeline_state)
208                 throw invalid_operation("VulkanCommands::draw_instanced");
209
210         const VulkanFunctions &vk = device.get_functions();
211
212         if(!render_pass)
213                  begin_render_pass(0);
214
215         pipeline_state->apply(current_buffer);
216         unsigned first_index = batch.get_offset()/batch.get_index_size();
217         vk.CmdDrawIndexed(current_buffer, batch.size(), count, first_index, 0, 0);
218 }
219
220 void VulkanCommands::resolve_multisample(Framebuffer &)
221 {
222         throw logic_error("VulkanCommands::resolve_multisample is unimplemented");
223 }
224
225 void VulkanCommands::begin_query(const QueryPool &, unsigned)
226 {
227         throw logic_error("VulkanCommands::begin_query is unimplemented");
228 }
229
230 void VulkanCommands::end_query(const QueryPool &, unsigned)
231 {
232         throw logic_error("VulkanCommands::end_query is unimplemented");
233 }
234
235
236 VulkanCommands::CommandPool::CommandPool(Device &d):
237         device(d),
238         fence(true)
239 {
240         const VulkanFunctions &vk = device.get_functions();
241
242         VkCommandPoolCreateInfo pool_info = { };
243         pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
244         pool_info.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
245         pool_info.queueFamilyIndex = device.get_context().get_private().graphics_queue_family;
246
247         vk.CreateCommandPool(pool_info, pool);
248 }
249
250 VulkanCommands::CommandPool::CommandPool(CommandPool &&other):
251         device(other.device),
252         pool(other.pool),
253         fence(move(other.fence)),
254         in_use(other.in_use)
255 {
256         other.pool = 0;
257 }
258
259 VulkanCommands::CommandPool::~CommandPool()
260 {
261         const VulkanFunctions &vk = device.get_functions();
262
263         if(pool)
264                 vk.DestroyCommandPool(pool);
265 }
266
267 } // namespace GL
268 } // namespace Msp