vsg: include/vsg/vk/Context.h Source File

vsg 1.1.3 VulkanSceneGraph library

Loading...
Searching...
No Matches
Context.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission 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
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE 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/commands/Command.h>
19#include <vsg/commands/CopyAndReleaseBuffer.h>
20#include <vsg/commands/CopyAndReleaseImage.h>
21#include <vsg/core/ScratchMemory.h>
22#include <vsg/nodes/Group.h>
23#include <vsg/state/BufferInfo.h>
24#include <vsg/state/GraphicsPipeline.h>
25#include <vsg/state/ImageInfo.h>
26#include <vsg/utils/Instrumentation.h>
27#include <vsg/utils/ShaderCompiler.h>
28#include <vsg/vk/CommandPool.h>
29#include <vsg/vk/DescriptorPool.h>
30#include <vsg/vk/Fence.h>
31#include <vsg/vk/MemoryBufferPools.h>
32#include <vsg/vk/ResourceRequirements.h>
33
34namespace 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 };
62 VSG_type_name(vsg::BuildAccelerationStructureCommand);
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
85 ShaderCompiler* getOrCreateShaderCompiler();
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
96 ref_ptr<DescriptorSet::Implementation> allocateDescriptorSet(DescriptorSetLayout* descriptorSetLayout);
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
123 ref_ptr<Instrumentation> instrumentation;
124
125 // transfer data settings
126 ref_ptr<Queue> graphicsQueue;
127 ref_ptr<CommandPool> commandPool;
128 ref_ptr<CommandBuffer> commandBuffer;
129 ref_ptr<Fence> fence;
130 ref_ptr<Semaphore> semaphore;
131 ref_ptr<ScratchMemory> scratchMemory;
132
133 std::vector<ref_ptr<Command>> commands;
134
135 ref_ptr<CopyAndReleaseImage> copyImageCmd;
136 void copy(ref_ptr<Data> data, ref_ptr<ImageInfo> dest);
137 void copy(ref_ptr<Data> data, ref_ptr<ImageInfo> dest, uint32_t numMipMapLevels);
138
139 ref_ptr<CopyAndReleaseBuffer> copyBufferCmd;
140 void copy(ref_ptr<BufferInfo> src, ref_ptr<BufferInfo> dest);
141
143 bool record();
144
145 void waitForCompletion();
146
147 ref_ptr<MemoryBufferPools> deviceMemoryBufferPools;
148 ref_ptr<MemoryBufferPools> stagingMemoryBufferPools;
149
150 // RTX ray tracing
151 VkDeviceSize scratchBufferSize;
152 std::vector<ref_ptr<BuildAccelerationStructureCommand>> buildAccelerationStructureCommands;
153 };
154 VSG_type_name(vsg::Context);
155
156} // namespace vsg
vsg::BuildAccelerationStructureCommand
Helper command for setting up RayTracing structures.
Definition Context.h:42
vsg::CommandBuffer
CommandBuffer encapsulates VkCommandBuffer.
Definition CommandBuffer.h:27
vsg::Context
Definition Context.h:67
vsg::Context::reserve
void reserve(const ResourceRequirements &requirements)
reserve resources that may be needed during compile traversal.
vsg::Context::getDescriptorPoolSizesToUse
void getDescriptorPoolSizesToUse(uint32_t &maxSets, DescriptorPoolSizes &descriptorPoolSizes)
get the maxSets and descriptorPoolSizes to use
vsg::Context::getOrCreateShaderCompiler
ShaderCompiler * getOrCreateShaderCompiler()
get existing ShaderCompiler or create a new one when GLSLang is supported
vsg::Context::instrumentation
ref_ptr< Instrumentation > instrumentation
Hook for assigning Instrumentation to enable profiling.
Definition Context.h:123
vsg::Context::record
bool record()
return true if there are commands that have been submitted
vsg::Context::allocateDescriptorSet
ref_ptr< DescriptorSet::Implementation > allocateDescriptorSet(DescriptorSetLayout *descriptorSetLayout)
allocate or reuse a DescriptorSet::Implementation from the available DescriptorPool
vsg::DescriptorSetLayout
DescriptorSetLayout encapsulates VkDescriptorSetLayout and VkDescriptorSetLayoutCreateInfo settings u...
Definition DescriptorSetLayout.h:28
vsg::Device
Device encapsulates VkDevice, a logical handle to the PhysicalDevice with capabilities specified duri...
Definition Device.h:37
vsg::Inherit
Definition Inherit.h:28
vsg::ResourceRequirements
ResourceRequirements provides a container for various Vulkan resource requirements that can be used t...
Definition ResourceRequirements.h:30
vsg::ShaderCompiler
Definition ShaderCompiler.h:15
vsg::ViewDependentState
Definition ViewDependentState.h:105
VkAccelerationStructureBuildGeometryInfoKHR
Definition vulkan.h:699
vsg::allocator_affinity_nodes
std container adapter for allocating with MEMORY_AFFINITY_NODES
Definition Allocator.h:138