vsg  1.0.4
VulkanSceneGraph library
ResourceRequirements.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/nodes/Bin.h>
16 #include <vsg/state/BufferInfo.h>
17 #include <vsg/state/Descriptor.h>
18 #include <vsg/state/ImageInfo.h>
19 #include <vsg/state/ResourceHints.h>
20 #include <vsg/vk/DescriptorPool.h>
21 
22 #include <map>
23 #include <set>
24 #include <stack>
25 
26 namespace vsg
27 {
29  class VSG_DECLSPEC ResourceRequirements
30  {
31  public:
33  ResourceRequirements(const ResourceRequirements& rhs) = default;
34 
35  ResourceRequirements& operator=(const ResourceRequirements& rhs) = default;
36 
37  void apply(const ResourceHints& resourceHints);
38 
39  uint32_t computeNumDescriptorSets() const;
40  DescriptorPoolSizes computeDescriptorPoolSizes() const;
41 
42  struct BinDetails
43  {
44  std::set<int32_t> indices;
45  std::set<const Bin*> bins;
46  };
47 
48  using Descriptors = std::set<const Descriptor*>;
49  using DescriptorSets = std::set<const DescriptorSet*>;
50  using DescriptorTypeMap = std::map<VkDescriptorType, uint32_t>;
51  using Views = std::map<const View*, BinDetails>;
52  using BinStack = std::stack<BinDetails>;
53 
54  struct DynamicData
55  {
56  BufferInfoList bufferInfos;
57  ImageInfoList imageInfos;
58 
59  explicit operator bool() const noexcept { return !bufferInfos.empty() || !imageInfos.empty(); }
60 
61  void clear()
62  {
63  bufferInfos.clear();
64  imageInfos.clear();
65  }
66 
67  void add(const DynamicData& dd)
68  {
69  bufferInfos.insert(bufferInfos.end(), dd.bufferInfos.begin(), dd.bufferInfos.end());
70  imageInfos.insert(imageInfos.end(), dd.imageInfos.begin(), dd.imageInfos.end());
71  }
72  };
73 
74  DynamicData earlyDynamicData;
75  DynamicData lateDynamicData;
76 
77  Descriptors descriptors;
78  DescriptorSets descriptorSets;
79  DescriptorTypeMap descriptorTypeMap;
80  Views views;
81  BinStack binStack;
82 
83  uint32_t maxSlot = 0;
84  uint32_t externalNumDescriptorSets = 0;
85  bool containsPagedLOD = false;
86 
87  VkDeviceSize minimumBufferSize = 16 * 1024 * 1024;
88  VkDeviceSize minimumDeviceMemorySize = 16 * 1024 * 1024;
89  };
90  VSG_type_name(vsg::ResourceRequirements);
91 
93  class VSG_DECLSPEC CollectResourceRequirements : public Inherit<ConstVisitor, CollectResourceRequirements>
94  {
95  public:
96  CollectResourceRequirements() { overrideMask = vsg::MASK_ALL; }
97 
98  ResourceRequirements requirements;
99 
101  ref_ptr<ResourceHints> createResourceHints(uint32_t tileMultiplier = 1) const;
102 
103  using ConstVisitor::apply;
104 
105  bool checkForResourceHints(const Object& object);
106 
107  void apply(const Object& object) override;
108  void apply(const ResourceHints& resourceHints) override;
109  void apply(const Node& node) override;
110  void apply(const StateGroup& stategroup) override;
111  void apply(const StateCommand& stateCommand) override;
112  void apply(const DescriptorSet& descriptorSet) override;
113  void apply(const Descriptor& descriptor) override;
114  void apply(const DescriptorBuffer& descriptorBuffer) override;
115  void apply(const DescriptorImage& descriptorImage) override;
116  void apply(const PagedLOD& plod) override;
117  void apply(const View& view) override;
118  void apply(const DepthSorted& depthSorted) override;
119  void apply(const Bin& bin) override;
120  void apply(const Geometry& geometry) override;
121  void apply(const VertexDraw& vid) override;
122  void apply(const VertexIndexDraw& vid) override;
123  void apply(const BindVertexBuffers& bvb) override;
124  void apply(const BindIndexBuffer& bib) override;
125 
126  inline void apply(ref_ptr<BufferInfo> bufferInfo)
127  {
128  if (bufferInfo && bufferInfo->data && bufferInfo->data->dynamic())
129  {
130  if (bufferInfo->data->properties.dataVariance == DYNAMIC_DATA)
131  {
132  requirements.earlyDynamicData.bufferInfos.push_back(bufferInfo);
133  }
134  else // DYNAMIC_DATA_TRANSFER_AFTER_RECORD)
135  {
136  requirements.lateDynamicData.bufferInfos.push_back(bufferInfo);
137  }
138  }
139  }
140 
141  inline void apply(ref_ptr<ImageInfo> imageInfo)
142  {
143  if (imageInfo && imageInfo->imageView && imageInfo->imageView->image)
144  {
145  auto& data = imageInfo->imageView->image->data;
146  if (data && data->dynamic())
147  {
148  if (data->properties.dataVariance == DYNAMIC_DATA)
149  {
150  requirements.earlyDynamicData.imageInfos.push_back(imageInfo);
151  }
152  else // DYNAMIC_DATA_TRANSFER_AFTER_RECORD)
153  {
154  requirements.lateDynamicData.imageInfos.push_back(imageInfo);
155  }
156  }
157  }
158  }
159 
160  protected:
161  uint32_t _numResourceHintsAbove = 0;
162 
163  bool registerDescriptor(const Descriptor& descriptor);
164  };
165  VSG_type_name(vsg::CollectResourceRequirements);
166 
167 } // namespace vsg
Definition: Bin.h:24
BindIndexBuffer command encapsulates vkBindIndexBuffer call and associated settings.
Definition: BindIndexBuffer.h:28
BindVertexBuffers command encapsulates vkBindVertexBuffers call and associated settings.
Definition: BindVertexBuffers.h:25
CollectResourceRequirements is a visitor class that collects the ResourceRequirements of a scene grap...
Definition: ResourceRequirements.h:94
ref_ptr< ResourceHints > createResourceHints(uint32_t tileMultiplier=1) const
create ResouceHints that capture the collected ResourceRequirements. Note, call after the CollectReso...
Definition: DepthSorted.h:27
Definition: DescriptorBuffer.h:24
Definition: DescriptorImage.h:24
DescriptorSet encapsulates VkDescriptorSet and VkDescriptorSetAllocateInfo settings used to describe ...
Definition: DescriptorSet.h:26
Definition: Descriptor.h:28
Definition: Geometry.h:30
Definition: Inherit.h:28
Definition: Node.h:24
Definition: Object.h:42
Definition: PagedLOD.h:36
ResourceHints provides settings that help preallocation of Vulkan resources and memory.
Definition: ResourceHints.h:22
ResourceRequirements provides a container for various Vulkan resource requirements that be used to he...
Definition: ResourceRequirements.h:30
Definition: StateCommand.h:23
Definition: StateGroup.h:32
Definition: VertexDraw.h:25
Definition: VertexIndexDraw.h:25
View class is Group class that pairs a Camera that defines the view with a subgraph that defines the ...
Definition: View.h:25
Definition: ref_ptr.h:22
Definition: ResourceRequirements.h:43
Definition: ResourceRequirements.h:55