vsg  1.1.0
VulkanSceneGraph library
SubmitCommands.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 Robert Osfield
6 
7 Permission 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 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE 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 
22 namespace vsg
23 {
25  template<typename F>
26  void submitCommandsToQueue(CommandPool* commandPool, Fence* fence, uint64_t timeout, Queue* queue, F function)
27  {
28  auto commandBuffer = commandPool->allocate();
29 
30  VkCommandBufferBeginInfo beginInfo = {};
31  beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
32  beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
33 
34  vkBeginCommandBuffer(*commandBuffer, &beginInfo);
35 
36  function(*commandBuffer);
37 
38  vkEndCommandBuffer(*commandBuffer);
39 
40  VkSubmitInfo submitInfo = {};
41  submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
42  submitInfo.commandBufferCount = 1;
43  submitInfo.pCommandBuffers = commandBuffer->data();
44 
45  // we must wait for the queue to empty before we can safely clean up the commandBuffer
46  if (fence)
47  {
48  queue->submit(submitInfo, fence);
49  if (timeout > 0) fence->wait(timeout);
50  }
51  else
52  {
53  queue->submit(submitInfo, VK_NULL_HANDLE);
54  queue->waitIdle();
55  }
56  }
57 
59  template<typename F>
60  void submitCommandsToQueue(CommandPool* commandPool, Queue* queue, F function)
61  {
62  submitCommandsToQueue(commandPool, nullptr, 0, queue, function);
63  }
64 
65 } // namespace vsg