vsg: include/vsg/vk/SubmitCommands.h Source File

vsg 1.1.10 VulkanSceneGraph library |

Loading...
Searching...
No Matches
SubmitCommands.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <vsg/core/Object.h>
16#include <vsg/vk/Fence.h>
17#include <vsg/vk/Queue.h>
18
19#include <mutex>
20#include <vector>
21
22namespace vsg
23{
25 template<typename F>
26 VkResult submitCommandsToQueue(CommandPool* commandPool, Fence* fence, uint64_t timeout, Queue* queue, F function)
27 {
28 VkResult result = VK_SUCCESS;
29
30 auto commandBuffer = commandPool->allocate();
31
32 VkCommandBufferBeginInfo beginInfo = {};
33 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
34 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
35
36 vkBeginCommandBuffer(*commandBuffer, &beginInfo);
37
38 function(*commandBuffer);
39
40 vkEndCommandBuffer(*commandBuffer);
41
42 VkSubmitInfo submitInfo = {};
43 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
44 submitInfo.commandBufferCount = 1;
45 submitInfo.pCommandBuffers = commandBuffer->data();
46
47 // we must wait for the queue to empty before we can safely clean up the commandBuffer
48 if (fence)
49 {
50 result = queue->submit(submitInfo, fence);
51 if (result == VK_SUCCESS && timeout > 0)
52 {
53 result = fence->wait(timeout);
54 }
55 }
56 else
57 {
58 result = queue->submit(submitInfo, VK_NULL_HANDLE);
59 if (result == VK_SUCCESS)
60 {
61 result = queue->waitIdle();
62 }
63 }
64 return result;
65 }
66
68 template<typename F>
69 VkResult submitCommandsToQueue(CommandPool* commandPool, Queue* queue, F function)
70 {
71 return submitCommandsToQueue(commandPool, nullptr, 0, queue, function);
72 }
73
74} // namespace vsg
vsg::CommandPool
Definition CommandPool.h:25
vsg::Fence
Definition Fence.h:23
vsg::Queue
Queue encapsulates a single vkQueue, used to submit vulkan commands for processing.
Definition Queue.h:28