vsg  1.1.0
VulkanSceneGraph library
Context.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 <deque>
16 #include <memory>
17 
18 #include <vsg/core/ScratchMemory.h>
19 #include <vsg/nodes/Group.h>
20 #include <vsg/state/BufferInfo.h>
21 #include <vsg/state/GraphicsPipeline.h>
22 #include <vsg/state/ImageInfo.h>
23 #include <vsg/utils/ShaderCompiler.h>
24 #include <vsg/vk/CommandPool.h>
25 #include <vsg/vk/DescriptorPool.h>
26 #include <vsg/vk/Fence.h>
27 #include <vsg/vk/MemoryBufferPools.h>
28 #include <vsg/vk/ResourceRequirements.h>
29 
30 #include <vsg/commands/Command.h>
31 #include <vsg/commands/CopyAndReleaseBuffer.h>
32 #include <vsg/commands/CopyAndReleaseImage.h>
33 
34 namespace vsg
35 {
36  // forward declare
37  class View;
38  class ViewDependentState;
39 
41  class VSG_DECLSPEC BuildAccelerationStructureCommand : public Inherit<Command, BuildAccelerationStructureCommand>
42  {
43  public:
44  // the primitive Count is A) the amount of triangles to be built for type VK_GEOMETRY_TYPE_TRIANGLES_KHR (blas) B) the amount of AABBs for type VK_GEOMETRY_TYPE_AABBS_KHR
45  // and C) the number of acceleration structures for type VK_GEOMETRY_TYPE_INSTANCES_KHR
46  BuildAccelerationStructureCommand(Device* device, const VkAccelerationStructureBuildGeometryInfoKHR& info, const VkAccelerationStructureKHR& structure, const std::vector<uint32_t>& primitiveCounts);
47 
48  void compile(Context&) override {}
49  void record(CommandBuffer& commandBuffer) const override;
50  void setScratchBuffer(ref_ptr<Buffer> scratchBuffer);
51 
52  ref_ptr<Device> _device;
53  VkAccelerationStructureBuildGeometryInfoKHR _accelerationStructureInfo;
54  std::vector<VkAccelerationStructureGeometryKHR> _accelerationStructureGeometries;
55  std::vector<VkAccelerationStructureBuildRangeInfoKHR> _accelerationStructureBuildRangeInfos;
56  VkAccelerationStructureKHR _accelerationStructure;
57 
58  protected:
59  // scratch buffer set after compile traversal before record of build commands
60  ref_ptr<Buffer> _scratchBuffer;
61  };
63 
66  class VSG_DECLSPEC Context : public Inherit<Object, Context>
67  {
68  public:
69  explicit Context(Device* in_device, const ResourceRequirements& in_resourceRequirements = {});
70 
71  Context(const Context& context);
72 
73  virtual ~Context();
74 
75  const uint32_t deviceID = 0;
76  ref_ptr<Device> device;
77  ResourceRequirements resourceRequirements;
78 
79  observer_ptr<View> view;
80  uint32_t viewID = 0;
81  Mask mask = MASK_ALL;
82  ViewDependentState* viewDependentState = nullptr;
83 
86 
87  ref_ptr<CommandBuffer> getOrCreateCommandBuffer();
88 
89  uint32_t minimum_maxSets = 0;
90  DescriptorPoolSizes minimum_descriptorPoolSizes;
91 
93  void getDescriptorPoolSizesToUse(uint32_t& maxSets, DescriptorPoolSizes& descriptorPoolSizes);
94 
97 
99  void reserve(const ResourceRequirements& requirements);
100 
101  // used by GraphicsPipeline.cpp
102  ref_ptr<RenderPass> renderPass;
103 
104  // pipeline states that are usually not set in a scene, e.g.,
105  // the viewport state, but might be set for some uses
106  GraphicsPipelineStates defaultPipelineStates;
107 
108  // pipeline states that must be set to avoid Vulkan errors
109  // e.g., MultisampleState.
110  // XXX MultisampleState is complicated because the sample
111  // number needs to agree with the renderpass attachment, but
112  // other parts of the state, like alpha to coverage, belong to
113  // the scene graph .
114  GraphicsPipelineStates overridePipelineStates;
115 
116  // DescriptorPool
117  std::list<ref_ptr<DescriptorPool>> descriptorPools;
118 
119  // ShaderCompiler
120  ref_ptr<ShaderCompiler> shaderCompiler;
121 
122  // transfer data settings
123  ref_ptr<Queue> graphicsQueue;
124  ref_ptr<CommandPool> commandPool;
125  ref_ptr<CommandBuffer> commandBuffer;
126  ref_ptr<Fence> fence;
127  ref_ptr<Semaphore> semaphore;
128  ref_ptr<ScratchMemory> scratchMemory;
129 
130  std::vector<ref_ptr<Command>> commands;
131 
132  ref_ptr<CopyAndReleaseImage> copyImageCmd;
133  void copy(ref_ptr<Data> data, ref_ptr<ImageInfo> dest);
134  void copy(ref_ptr<Data> data, ref_ptr<ImageInfo> dest, uint32_t numMipMapLevels);
135 
136  ref_ptr<CopyAndReleaseBuffer> copyBufferCmd;
137  void copy(ref_ptr<BufferInfo> src, ref_ptr<BufferInfo> dest);
138 
140  bool record();
141 
142  void waitForCompletion();
143 
144  ref_ptr<MemoryBufferPools> deviceMemoryBufferPools;
145  ref_ptr<MemoryBufferPools> stagingMemoryBufferPools;
146 
147  // RTX ray tracing
148  VkDeviceSize scratchBufferSize;
149  std::vector<ref_ptr<BuildAccelerationStructureCommand>> buildAccelerationStructureCommands;
150  };
151  VSG_type_name(vsg::Context);
152 
153 } // namespace vsg
Helper command for setting up RayTracing structures.
Definition: Context.h:42
CommandBuffer encapsulates VkCommandBuffer.
Definition: CommandBuffer.h:27
Definition: Context.h:67
ShaderCompiler * getOrCreateShaderCompiler()
get existing ShaderCompiler or create a new one when GLSLang is supported
void reserve(const ResourceRequirements &requirements)
reserve resources that may be needed during compile traversal.
void getDescriptorPoolSizesToUse(uint32_t &maxSets, DescriptorPoolSizes &descriptorPoolSizes)
get the maxSets and descriptorPoolSizes to use
ref_ptr< DescriptorSet::Implementation > allocateDescriptorSet(DescriptorSetLayout *descriptorSetLayout)
allocate or reuse a DescriptorSet::Implementation from the available DescriptorPool
bool record()
return true if there are commands that have been submitted
DescriptorSetLayout encapsulates VkDescriptorSetLayout and VkDescriptorSetLayoutCreateInfo settings u...
Definition: DescriptorSetLayout.h:28
Device encapsulates VkDevice, a logical handle to the PhysicalDevice with capabilities specified duri...
Definition: Device.h:37
Definition: Inherit.h:28
ResourceRequirements provides a container for various Vulkan resource requirements that can be used t...
Definition: ResourceRequirements.h:30
Definition: ShaderCompiler.h:15
Definition: ViewDependentState.h:105
Definition: observer_ptr.h:24
Definition: ref_ptr.h:22