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

vsg 1.1.8 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/app/TransferTask.h>
19#include <vsg/commands/Command.h>
20#include <vsg/commands/CopyAndReleaseBuffer.h>
21#include <vsg/commands/CopyAndReleaseImage.h>
22#include <vsg/core/ScratchMemory.h>
23#include <vsg/nodes/Group.h>
24#include <vsg/state/BufferInfo.h>
25#include <vsg/state/GraphicsPipeline.h>
26#include <vsg/state/ImageInfo.h>
27#include <vsg/utils/Instrumentation.h>
28#include <vsg/utils/ShaderCompiler.h>
29#include <vsg/vk/CommandPool.h>
30#include <vsg/vk/DescriptorPool.h>
31#include <vsg/vk/Fence.h>
32#include <vsg/vk/MemoryBufferPools.h>
33#include <vsg/vk/ResourceRequirements.h>
34
35namespace vsg
36{
37 // forward declare
38 class View;
39 class ViewDependentState;
40
42 class VSG_DECLSPEC BuildAccelerationStructureCommand : public Inherit<Command, BuildAccelerationStructureCommand>
43 {
44 public:
45 // 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
46 // and C) the number of acceleration structures for type VK_GEOMETRY_TYPE_INSTANCES_KHR
47 BuildAccelerationStructureCommand(Device* device, const VkAccelerationStructureBuildGeometryInfoKHR& info, const VkAccelerationStructureKHR& structure, const std::vector<uint32_t>& primitiveCounts);
48
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
90 void reserve(const ResourceRequirements& requirements);
91
92 ref_ptr<DescriptorSet::Implementation> allocateDescriptorSet(DescriptorSetLayout* descriptorSetLayout);
93
94 // used by GraphicsPipeline.cpp
95 ref_ptr<RenderPass> renderPass;
96
97 // pipeline states that are usually not set in a scene, e.g.,
98 // the viewport state, but might be set for some uses
99 GraphicsPipelineStates defaultPipelineStates;
100
101 // pipeline states that must be set to avoid Vulkan errors
102 // e.g., MultisampleState.
103 // XXX MultisampleState is complicated because the sample
104 // number needs to agree with the renderpass attachment, but
105 // other parts of the state, like alpha to coverage, belong to
106 // the scene graph .
107 GraphicsPipelineStates overridePipelineStates;
108
109 // DescriptorPools
110 ref_ptr<DescriptorPools> descriptorPools;
111
112 // ShaderCompiler
113 ref_ptr<ShaderCompiler> shaderCompiler;
114
116 ref_ptr<Instrumentation> instrumentation;
117
118 // transfer data settings
119 ref_ptr<Queue> graphicsQueue;
120 ref_ptr<CommandPool> commandPool;
121 ref_ptr<CommandBuffer> commandBuffer;
122 ref_ptr<Fence> fence;
123 ref_ptr<Semaphore> semaphore;
124 ref_ptr<ScratchMemory> scratchMemory;
125 bool requiresWaitForCompletion = false;
126
127 std::vector<ref_ptr<Command>> commands;
128
129 ref_ptr<CopyAndReleaseImage> copyImageCmd;
130 void copy(ref_ptr<Data> data, ref_ptr<ImageInfo> dest);
131 void copy(ref_ptr<Data> data, ref_ptr<ImageInfo> dest, uint32_t numMipMapLevels);
132
133 ref_ptr<CopyAndReleaseBuffer> copyBufferCmd;
134 void copy(ref_ptr<BufferInfo> src, ref_ptr<BufferInfo> dest);
135
137 bool record();
138
139 void waitForCompletion();
140
141 ref_ptr<MemoryBufferPools> deviceMemoryBufferPools;
142 ref_ptr<MemoryBufferPools> stagingMemoryBufferPools;
143
144 // RTX ray tracing
145 VkDeviceSize scratchBufferSize;
146 std::vector<ref_ptr<BuildAccelerationStructureCommand>> buildAccelerationStructureCommands;
147
148 ref_ptr<TransferTask> transferTask;
149 };
150 VSG_type_name(vsg::Context);
151
152} // namespace vsg
vsg::BuildAccelerationStructureCommand
Helper command for setting up RayTracing structures.
Definition Context.h:43
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::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:116
vsg::Context::record
bool record()
return true if there are commands that have been submitted
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:39
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:106
vsg::observer_ptr
Definition observer_ptr.h:24
vsg::ref_ptr
Definition ref_ptr.h:22
VkAccelerationStructureBuildGeometryInfoKHR
Definition vulkan.h:699