vsg  1.1.0
VulkanSceneGraph library
ViewDependentState.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2022 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 <vsg/app/CommandGraph.h>
16 #include <vsg/app/RenderGraph.h>
17 #include <vsg/io/Logger.h>
18 #include <vsg/nodes/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 
24 namespace vsg
25 {
26 
30  class VSG_DECLSPEC ViewDescriptorSetLayout : public Inherit<DescriptorSetLayout, ViewDescriptorSetLayout>
31  {
32  public:
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:
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;
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  ref_ptr<vec4Array> lightData;
137  ref_ptr<BufferInfo> lightDataBufferInfo;
138 
139  ref_ptr<vec4Array> viewportData;
140  ref_ptr<BufferInfo> viewportDataBufferInfo;
141 
142  ref_ptr<Image> shadowDepthImage;
143  ref_ptr<DescriptorImage> shadowMapImages;
144 
145  ref_ptr<DescriptorSetLayout> descriptorSetLayout;
146  ref_ptr<DescriptorBuffer> descriptor;
147  ref_ptr<DescriptorSet> descriptorSet;
148 
149  // shadow map hints
150  double maxShadowDistance = 1e8;
151  double shadowMapBias = 0.005;
152  double lambda = 0.5;
153 
154  // Shadow backend.
155  ref_ptr<CommandGraph> preRenderCommandGraph;
156  ref_ptr<Switch> preRenderSwitch;
157 
158  struct ShadowMap
159  {
160  ref_ptr<RenderGraph> renderGraph;
161  ref_ptr<View> view;
162  };
163 
164  mutable std::vector<ShadowMap> shadowMaps;
165 
166  protected:
168  };
169  VSG_type_name(vsg::ViewDependentState);
170 
171 } // namespace vsg
Definition: ViewDependentState.h:53
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,...
CommandBuffer encapsulates VkCommandBuffer.
Definition: CommandBuffer.h:27
Definition: ConstVisitor.h:147
Definition: Context.h:67
Definition: Inherit.h:28
Definition: Input.h:44
Definition: Object.h:42
Definition: Output.h:41
PipelineLayout encapsulates VkPipelineLayout and the VkPipelineLayoutCreateInfo settings used to set ...
Definition: PipelineLayout.h:27
RecordTraversal traverses a scene graph doing view frustum culling and invoking state/commands to rec...
Definition: RecordTraversal.h:61
ResourceRequirements provides a container for various Vulkan resource requirements that can be used t...
Definition: ResourceRequirements.h:30
Definition: ViewDependentState.h:105
Definition: ViewDependentState.h:31
VkDescriptorSetLayout vk(uint32_t deviceID) const override
Vulkan VkDescriptorSetLayout handle.
Definition: ViewDependentState.h:35
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,...
View is a Group class that pairs a Camera that defines the view with a subgraph that defines the scen...
Definition: View.h:36
Definition: Visitor.h:147
Definition: ref_ptr.h:22
Definition: ViewDependentState.h:159