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