vsg  1.1.0
VulkanSceneGraph library
CommandBuffer.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/ScratchMemory.h>
16 #include <vsg/state/PipelineLayout.h>
17 #include <vsg/vk/CommandPool.h>
18 
19 namespace vsg
20 {
21 
22  // forward declare
23  class ViewDependentState;
24 
26  class VSG_DECLSPEC CommandBuffer : public Inherit<Object, CommandBuffer>
27  {
28  public:
29  const VkCommandBuffer* data() const { return &_commandBuffer; }
30  operator VkCommandBuffer() const { return _commandBuffer; }
31  VkCommandBuffer vk() const { return _commandBuffer; }
32 
33  std::atomic_uint& numDependentSubmissions() { return _numDependentSubmissions; }
34 
35  const uint32_t deviceID;
36  uint32_t viewID = 0;
37  Mask traversalMask = MASK_ALL;
38  Mask overrideMask = MASK_OFF;
39  ViewDependentState* viewDependentState = nullptr;
40 
41  VkCommandBufferLevel level() const { return _level; }
42 
44  void reset();
45 
46  Device* getDevice() { return _device; }
47  const Device* getDevice() const { return _device; }
48 
49  CommandPool* getCommandPool() { return _commandPool; }
50  const CommandPool* getCommandPool() const { return _commandPool; }
51 
52  void setCurrentPipelineLayout(const PipelineLayout* pipelineLayout)
53  {
54  _currentPipelineLayout = pipelineLayout->vk(deviceID);
55  if (pipelineLayout->pushConstantRanges.empty())
56  _currentPushConstantStageFlags = 0;
57  else
58  _currentPushConstantStageFlags = pipelineLayout->pushConstantRanges.front().stageFlags;
59  }
60 
61  VkPipelineLayout getCurrentPipelineLayout() const { return _currentPipelineLayout; }
62  VkShaderStageFlags getCurrentPushConstantStageFlags() const { return _currentPushConstantStageFlags; }
63 
64  ref_ptr<ScratchMemory> scratchMemory;
65 
66  protected:
67  friend CommandPool;
68  CommandBuffer(CommandPool* commandPool, VkCommandBuffer commandBuffer, VkCommandBufferLevel level);
69 
70  virtual ~CommandBuffer();
71 
72  VkCommandBuffer _commandBuffer;
73  VkCommandBufferLevel _level;
74 
75  std::atomic_uint _numDependentSubmissions{0};
76  ref_ptr<Device> _device;
77  ref_ptr<CommandPool> _commandPool;
78  VkPipelineLayout _currentPipelineLayout;
79  VkShaderStageFlags _currentPushConstantStageFlags;
80  };
81  VSG_type_name(vsg::CommandBuffer);
82 
83  using CommandBuffers = std::vector<ref_ptr<CommandBuffer>>;
84 
86  class RecordedCommandBuffers : public Inherit<Object, RecordedCommandBuffers>
87  {
88  public:
89  ref_ptr<RecordedCommandBuffers> getOrCreateRecordedCommandBuffers(int submitOrder);
90 
91  void clear();
92  void add(int order, ref_ptr<CommandBuffer> commandGraph);
93  bool empty() const;
94 
95  CommandBuffers buffers() const;
96 
97  protected:
98  virtual ~RecordedCommandBuffers();
99  mutable std::mutex _mutex;
100  std::map<int, ref_ptr<RecordedCommandBuffers>> _orderedCommandBuffers;
101  CommandBuffers _commandBuffers;
102  };
103  VSG_type_name(vsg::RecordedCommandBuffers);
104 
105 } // namespace vsg
CommandBuffer encapsulates VkCommandBuffer.
Definition: CommandBuffer.h:27
void reset()
reset the CommandBuffer for the new frame.
Device encapsulates VkDevice, a logical handle to the PhysicalDevice with capabilities specified duri...
Definition: Device.h:37
Definition: Inherit.h:28
Thread safe container class.
Definition: CommandBuffer.h:87
Definition: ViewDependentState.h:105
Definition: ref_ptr.h:22