vsg: include/vsg/state/ViewDependentState.h Source File

vsg 1.1.10 VulkanSceneGraph library |

Loading...
Searching...
No Matches
ViewDependentState.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2022 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 <vsg/app/CommandGraph.h>
16#include <vsg/app/RenderGraph.h>
17#include <vsg/io/Logger.h>
18#include <vsg/lighting/Light.h>
19#include <vsg/nodes/Switch.h>
20#include <vsg/state/BindDescriptorSet.h>
21#include <vsg/state/DescriptorBuffer.h>
22#include <vsg/state/DescriptorImage.h>
23#include <vsg/utils/ShaderSet.h>
24
25namespace vsg
26{
27
31 class VSG_DECLSPEC ViewDescriptorSetLayout : public Inherit<DescriptorSetLayout, ViewDescriptorSetLayout>
32 {
33 public:
34 ViewDescriptorSetLayout();
35
36 VkDescriptorSetLayout vk(uint32_t deviceID) const override { return _viewDescriptorSetLayout ? _viewDescriptorSetLayout->vk(deviceID) : 0; }
37
38 int compare(const Object& rhs_object) const override;
39
40 void read(Input& input) override;
41 void write(Output& output) const override;
42
43 void compile(Context& context) override;
44
45 protected:
46 ref_ptr<DescriptorSetLayout> _viewDescriptorSetLayout;
47 };
48 VSG_type_name(vsg::ViewDescriptorSetLayout);
49
53 class VSG_DECLSPEC BindViewDescriptorSets : public Inherit<StateCommand, BindViewDescriptorSets>
54 {
55 public:
56 BindViewDescriptorSets();
57
58 BindViewDescriptorSets(VkPipelineBindPoint in_bindPoint, PipelineLayout* in_pipelineLayout, uint32_t in_firstSet) :
59 Inherit(1 + in_firstSet),
60 pipelineBindPoint(in_bindPoint),
61 layout(in_pipelineLayout),
62 firstSet(in_firstSet)
63 {
64 }
65
66 // vkCmdBindDescriptorSets settings
67 VkPipelineBindPoint pipelineBindPoint;
68 ref_ptr<PipelineLayout> layout;
69 uint32_t firstSet;
70
71 int compare(const Object& rhs_object) const override;
72
73 template<class N, class V>
74 static void t_traverse(N& bds, V& visitor)
75 {
76 if (bds.layout) bds.layout->accept(visitor);
77 }
78
79 void traverse(Visitor& visitor) override { t_traverse(*this, visitor); }
80 void traverse(ConstVisitor& visitor) const override { t_traverse(*this, visitor); }
81
82 void read(Input& input) override;
83 void write(Output& output) const override;
84
85 // compile the Vulkan object, context parameter used for Device
86 void compile(Context& context) override;
87
88 void record(CommandBuffer& commandBuffer) const override;
89
90 protected:
91 virtual ~BindViewDescriptorSets() {}
92 };
93 VSG_type_name(vsg::BindViewDescriptorSets);
94
95 // forward declare
96 class ResourceRequirements;
97
105 class VSG_DECLSPEC ViewDependentState : public Inherit<Object, ViewDependentState>
106 {
107 public:
108 explicit ViewDependentState(View* in_view);
109
110 template<class N, class V>
111 static void t_traverse(N& node, V& visitor)
112 {
113 node.descriptorSet->accept(visitor);
114 if (node.preRenderCommandGraph) node.preRenderCommandGraph->accept(visitor);
115 }
116
117 void traverse(Visitor& visitor) override { t_traverse(*this, visitor); }
118 void traverse(ConstVisitor& visitor) const override { t_traverse(*this, visitor); }
119 void traverse(RecordTraversal& rt) const override;
120
121 // containers filled in by RecordTraversal
122 std::vector<std::pair<dmat4, const AmbientLight*>> ambientLights;
123 std::vector<std::pair<dmat4, const DirectionalLight*>> directionalLights;
124 std::vector<std::pair<dmat4, const PointLight*>> pointLights;
125 std::vector<std::pair<dmat4, const SpotLight*>> spotLights;
126
127 virtual void init(ResourceRequirements& requirements);
128 virtual void update(ResourceRequirements& requirements);
129
130 virtual void clear();
131 virtual void bindDescriptorSets(CommandBuffer& commandBuffer, VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout, uint32_t firstSet);
132
133 virtual void compile(Context& context);
134
135 View* view = nullptr;
136
137 // ShaderSet to guide the set up of lightData, viewportData and shadowMap Descriptors & DescriptorSet(s).
138 ref_ptr<ShaderSet> shaderSet;
139
140 ref_ptr<vec4Array> lightData;
141 ref_ptr<BufferInfo> lightDataBufferInfo;
142
143 ref_ptr<vec4Array> viewportData;
144 ref_ptr<BufferInfo> viewportDataBufferInfo;
145
146 ref_ptr<Image> shadowDepthImage;
147
148 ref_ptr<DescriptorSetLayout> descriptorSetLayout;
149 ref_ptr<DescriptorSet> descriptorSet;
150
151 // shadow map hints
152 double maxShadowDistance = 1e8;
153 double shadowMapBias = 0.005;
154 double lambda = 0.5;
155
156 // map of Light's that we wish to override their ShadowSettings,
157 // assigning shadowSettingsOverride[{}] = shadowSettings will override all Light not otherwise explicitly matched.
158 std::map<ref_ptr<const Light>, ref_ptr<ShadowSettings>> shadowSettingsOverride;
159
160 virtual ref_ptr<ShadowSettings> getActiveShadowSettings(const Light* light) const;
161
162 // Shadow backend.
163 bool compiled = false;
164 ref_ptr<CommandGraph> preRenderCommandGraph;
165 ref_ptr<Switch> preRenderSwitch;
166
167 struct ShadowMap
168 {
169 ref_ptr<RenderGraph> renderGraph;
170 ref_ptr<View> view;
171 };
172
173 mutable std::vector<ShadowMap> shadowMaps;
174
175 protected:
176 ~ViewDependentState();
177 };
178 VSG_type_name(vsg::ViewDependentState);
179
180} // namespace vsg
vsg::BindViewDescriptorSets::compare
int compare(const Object &rhs_object) const override
compare two objects, return -1 if this object is less than rhs, return 0 if it's equal,...
vsg::CommandBuffer
CommandBuffer encapsulates VkCommandBuffer.
Definition CommandBuffer.h:27
vsg::ConstVisitor
Definition ConstVisitor.h:175
vsg::Context
Definition Context.h:67
vsg::Input
Definition Input.h:44
vsg::Light
Definition Light.h:26
vsg::Object
Definition Object.h:60
vsg::Output
Definition Output.h:41
vsg::PipelineLayout
PipelineLayout encapsulates VkPipelineLayout and the VkPipelineLayoutCreateInfo settings used to set ...
Definition PipelineLayout.h:27
vsg::RecordTraversal
RecordTraversal traverses a scene graph doing view frustum culling and invoking state/commands to rec...
Definition RecordTraversal.h:70
vsg::ResourceRequirements
ResourceRequirements provides a container for various Vulkan resource requirements that can be used t...
Definition ResourceRequirements.h:30
vsg::ViewDependentState
Definition ViewDependentState.h:106
vsg::ViewDescriptorSetLayout
Definition ViewDependentState.h:32
vsg::ViewDescriptorSetLayout::vk
VkDescriptorSetLayout vk(uint32_t deviceID) const override
Vulkan VkDescriptorSetLayout handle.
Definition ViewDependentState.h:36
vsg::ViewDescriptorSetLayout::compare
int compare(const Object &rhs_object) const override
compare two objects, return -1 if this object is less than rhs, return 0 if it's equal,...
vsg::View
View is a Group class that pairs a Camera that defines the view with a subgraph that defines the scen...
Definition View.h:36
vsg::Visitor
Definition Visitor.h:175
vsg::ref_ptr
Definition ref_ptr.h:22
vsg::ViewDependentState::ShadowMap
Definition ViewDependentState.h:168