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

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