vsg  1.0.4
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& resourceRequirements = {});
70 
71  Context(const Context& context);
72 
73  virtual ~Context();
74 
75  const uint32_t deviceID = 0;
76  ref_ptr<Device> device;
77 
78  observer_ptr<View> view;
79  uint32_t viewID = 0;
80  ViewDependentState* viewDependentState = nullptr;
81 
84 
85  ref_ptr<CommandBuffer> getOrCreateCommandBuffer();
86 
87  uint32_t minimum_maxSets = 0;
88  DescriptorPoolSizes minimum_descriptorPoolSizes;
89 
91  void getDescriptorPoolSizesToUse(uint32_t& maxSets, DescriptorPoolSizes& descriptorPoolSizes);
92 
95 
97  void reserve(const ResourceRequirements& requirements);
98 
99  // used by GraphicsPipeline.cpp
100  ref_ptr<RenderPass> renderPass;
101 
102  // pipeline states that are usually not set in a scene, e.g.,
103  // the viewport state, but might be set for some uses
104  GraphicsPipelineStates defaultPipelineStates;
105 
106  // pipeline states that must be set to avoid Vulkan errors
107  // e.g., MultisampleState.
108  // XXX MultisampleState is complicated because the sample
109  // number needs to agree with the renderpass attachment, but
110  // other parts of the state, like alpha to coverage, belong to
111  // the scene graph .
112  GraphicsPipelineStates overridePipelineStates;
113 
114  // DescriptorPool
115  std::list<ref_ptr<DescriptorPool>> descriptorPools;
116 
117  // ShaderCompiler
118  ref_ptr<ShaderCompiler> shaderCompiler;
119 
120  // transfer data settings
121  ref_ptr<Queue> graphicsQueue;
122  ref_ptr<CommandPool> commandPool;
123  ref_ptr<CommandBuffer> commandBuffer;
124  ref_ptr<Fence> fence;
125  ref_ptr<Semaphore> semaphore;
126  ref_ptr<ScratchMemory> scratchMemory;
127 
128  std::vector<ref_ptr<Command>> commands;
129 
130  ref_ptr<CopyAndReleaseImage> copyImageCmd;
131  void copy(ref_ptr<Data> data, ref_ptr<ImageInfo> dest);
132  void copy(ref_ptr<Data> data, ref_ptr<ImageInfo> dest, uint32_t numMipMapLevels);
133 
134  ref_ptr<CopyAndReleaseBuffer> copyBufferCmd;
135  void copy(ref_ptr<BufferInfo> src, ref_ptr<BufferInfo> dest);
136 
138  bool record();
139 
140  void waitForCompletion();
141 
142  ref_ptr<MemoryBufferPools> deviceMemoryBufferPools;
143  ref_ptr<MemoryBufferPools> stagingMemoryBufferPools;
144 
145  // RTX ray tracing
146  VkDeviceSize scratchBufferSize;
147  std::vector<ref_ptr<BuildAccelerationStructureCommand>> buildAccelerationStructureCommands;
148  };
149  VSG_type_name(vsg::Context);
150 
151 } // namespace vsg
Helper command for settings up RayTracing structures.
Definition: Context.h:42
CommandBuffer encapsulates VkCommandBuffer.
Definition: CommandBuffer.h:27
Definition: Context.h:67
ShaderCompiler * getOrCreateShaderCompiler()
get existing ShaderCompile 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 encapsulate vkDeivce, a logical handle to the PhysicalDevice with capabilities specified durin...
Definition: Device.h:37
Definition: Inherit.h:28
ResourceRequirements provides a container for various Vulkan resource requirements that be used to he...
Definition: ResourceRequirements.h:30
Definition: ShaderCompiler.h:15
Definition: ViewDependentState.h:97
Definition: observer_ptr.h:24
Definition: ref_ptr.h:22