Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added vsg::RegionOfInterst node to help hint for algorithms like shadow map to use tight bounds #1145

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/vsg/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/nodes/Node.h>
#include <vsg/nodes/PagedLOD.h>
#include <vsg/nodes/QuadGroup.h>
#include <vsg/nodes/RegionOfInterest.h>
#include <vsg/nodes/StateGroup.h>
#include <vsg/nodes/Switch.h>
#include <vsg/nodes/TileDatabase.h>
Expand Down
5 changes: 5 additions & 0 deletions include/vsg/app/RecordTraversal.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace vsg
class View;
class Bin;
class Switch;
class RegionOfInterest;
class ViewDependentState;
class Light;
class AmbientLight;
Expand Down Expand Up @@ -117,6 +118,7 @@ namespace vsg
void apply(const DepthSorted& depthSorted);
void apply(const Layer& layer);
void apply(const Switch& sw);
void apply(const RegionOfInterest& roi);

// leaf node
void apply(const VertexDraw& vid);
Expand Down Expand Up @@ -154,6 +156,9 @@ namespace vsg
// clear the bins to record a new frame.
void clearBins();

// list of pairs of modelview matrix & region of interest
std::vector<std::pair<dmat4, const RegionOfInterest*>> regionsOfInterest;

protected:
virtual ~RecordTraversal();

Expand Down
2 changes: 2 additions & 0 deletions include/vsg/core/ConstVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace vsg
class PointLight;
class SpotLight;
class InstrumentationNode;
class RegionOfInterest;

// forward declare text classes
class Text;
Expand Down Expand Up @@ -337,6 +338,7 @@ namespace vsg
virtual void apply(const PointLight&);
virtual void apply(const SpotLight&);
virtual void apply(const InstrumentationNode&);
virtual void apply(const RegionOfInterest&);

// text
virtual void apply(const Text&);
Expand Down
2 changes: 2 additions & 0 deletions include/vsg/core/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace vsg
class PointLight;
class SpotLight;
class InstrumentationNode;
class RegionOfInterest;

// forward declare text classes
class Text;
Expand Down Expand Up @@ -337,6 +338,7 @@ namespace vsg
virtual void apply(PointLight&);
virtual void apply(SpotLight&);
virtual void apply(InstrumentationNode&);
virtual void apply(RegionOfInterest&);

// text
virtual void apply(Text&);
Expand Down
57 changes: 57 additions & 0 deletions include/vsg/nodes/RegionOfInterest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

/* <editor-fold desc="MIT License">

Copyright(c) 2024 Robert Osfield

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.

</editor-fold> */

#include <vsg/maths/sphere.h>
#include <vsg/nodes/Node.h>

namespace vsg
{

/// RegionOfInterest node is inform applications/algorithms extents that should take account of.
class VSG_DECLSPEC RegionOfInterest : public Inherit<Node, RegionOfInterest>
{
public:
RegionOfInterest();
RegionOfInterest(const RegionOfInterest& rhs, const CopyOp& copyop = {});

Mask mask = MASK_ALL;
std::string name;
std::vector<dvec3> points;

public:
ref_ptr<Object> clone(const CopyOp& copyop = {}) const override { return RegionOfInterest::create(*this, copyop); }
int compare(const Object& rhs) const override;

void accept(Visitor& visitor) override
{
if ((visitor.traversalMask & (visitor.overrideMask | mask)) != MASK_OFF) visitor.apply(*this);
}
void accept(ConstVisitor& visitor) const override
{
if ((visitor.traversalMask & (visitor.overrideMask | mask)) != MASK_OFF) visitor.apply(*this);
}
void accept(RecordTraversal& visitor) const override
{
if ((visitor.traversalMask & (visitor.overrideMask | mask)) != MASK_OFF) visitor.apply(*this);
}

void read(Input& input) override;
void write(Output& output) const override;

protected:
virtual ~RegionOfInterest();
};
VSG_type_name(vsg::RegionOfInterest);

} // namespace vsg
1 change: 1 addition & 0 deletions src/vsg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ set(SOURCES
nodes/StateGroup.cpp
nodes/TileDatabase.cpp
nodes/InstrumentationNode.cpp
nodes/RegionOfInterest.cpp

lighting/Light.cpp
lighting/AmbientLight.cpp
Expand Down
1 change: 1 addition & 0 deletions src/vsg/app/CommandGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ void CommandGraph::record(ref_ptr<RecordedCommandBuffers> recordedCommandBuffers
recordTraversal->setFrameStamp(frameStamp);
recordTraversal->setDatabasePager(databasePager);
recordTraversal->clearBins();
recordTraversal->regionsOfInterest.clear();

ref_ptr<CommandBuffer> commandBuffer;
for (auto& cb : _commandBuffers)
Expand Down
11 changes: 11 additions & 0 deletions src/vsg/app/RecordTraversal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@ void RecordTraversal::apply(const Switch& sw)
}
}

void RecordTraversal::apply(const RegionOfInterest& roi)
{
CPU_INSTRUMENTATION_L2_NCO(instrumentation, "RegionOfInterest", COLOR_RECORD_L2, &roi);

regionsOfInterest.emplace_back(_state->modelviewMatrixStack.top(), &roi);
}

void RecordTraversal::apply(const DepthSorted& depthSorted)
{
CPU_INSTRUMENTATION_L2_NCO(instrumentation, "DepthSorted", COLOR_RECORD_L2, &depthSorted);
Expand Down Expand Up @@ -497,6 +504,9 @@ void RecordTraversal::apply(const View& view)
cached_bins.swap(_bins);
auto cached_viewDependentState = _viewDependentState;

decltype(regionsOfInterest) cached_regionsOfInterest;
cached_regionsOfInterest.swap(regionsOfInterest);

// assign and clear the View's bins
int32_t min_binNumber = 0;
int32_t max_binNumber = 0;
Expand Down Expand Up @@ -566,6 +576,7 @@ void RecordTraversal::apply(const View& view)
// swap back previous bin setup.
_minimumBinNumber = cached_minimumBinNumber;
cached_bins.swap(_bins);
cached_regionsOfInterest.swap(regionsOfInterest);
_state->_commandBuffer->traversalMask = cached_traversalMask;
_viewDependentState = cached_viewDependentState;
}
Expand Down
4 changes: 4 additions & 0 deletions src/vsg/core/ConstVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,10 @@ void ConstVisitor::apply(const InstrumentationNode& value)
{
apply(static_cast<const Node&>(value));
}
void ConstVisitor::apply(const RegionOfInterest& value)
{
apply(static_cast<const Node&>(value));
}

////////////////////////////////////////////////////////////////////////////////
//
Expand Down
4 changes: 4 additions & 0 deletions src/vsg/core/Visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,10 @@ void Visitor::apply(InstrumentationNode& value)
{
apply(static_cast<Node&>(value));
}
void Visitor::apply(RegionOfInterest& value)
{
apply(static_cast<Node&>(value));
}

////////////////////////////////////////////////////////////////////////////////
//
Expand Down
62 changes: 62 additions & 0 deletions src/vsg/nodes/RegionOfInterest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* <editor-fold desc="MIT License">

Copyright(c) 2024 Robert Osfield

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.

</editor-fold> */

#include <vsg/io/Options.h>
#include <vsg/io/stream.h>
#include <vsg/nodes/RegionOfInterest.h>

using namespace vsg;

RegionOfInterest::RegionOfInterest()
{
}

RegionOfInterest::RegionOfInterest(const RegionOfInterest& rhs, const CopyOp& copyop) :
Inherit(rhs, copyop),
mask(rhs.mask),
name(rhs.name),
points(rhs.points)
{
}

RegionOfInterest::~RegionOfInterest()
{
}

int RegionOfInterest::compare(const Object& rhs_object) const
{
int result = Node::compare(rhs_object);
if (result != 0) return result;

auto& rhs = static_cast<decltype(*this)>(rhs_object);
if ((result = compare_value(mask, rhs.mask)) != 0) return result;
if ((result = compare_value(name, rhs.name)) != 0) return result;
return compare_value_container(points, rhs.points);
}

void RegionOfInterest::read(Input& input)
{
Node::read(input);

input.read("mask", mask);
input.read("name", name);
input.read("points", points);
}

void RegionOfInterest::write(Output& output) const
{
Node::write(output);

output.write("mask", mask);
output.write("name", name);
output.write("points", points);
}
46 changes: 33 additions & 13 deletions src/vsg/state/ViewDependentState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/lighting/PointLight.h>
#include <vsg/lighting/SoftShadows.h>
#include <vsg/lighting/SpotLight.h>
#include <vsg/nodes/RegionOfInterest.h>
#include <vsg/state/DescriptorImage.h>
#include <vsg/state/ViewDependentState.h>
#include <vsg/vk/Context.h>
Expand Down Expand Up @@ -576,6 +577,38 @@ void ViewDependentState::traverse(RecordTraversal& rt) const

// info("\n\nViewDependentState::traverse(", &rt, ", ", &view, ") numShadowMaps = ", numShadowMaps);

// cache general view parameters
auto projectionMatrix = view->camera->projectionMatrix->transform();
auto viewMatrix = view->camera->viewMatrix->transform();
auto inverse_viewMatrix = inverse(viewMatrix);
auto view_direction = normalize(dvec3(0.0, 0.0, -1.0) * (projectionMatrix * viewMatrix));
auto view_up = normalize(dvec3(0.0, -1.0, 0.0) * (projectionMatrix * viewMatrix));

auto clipToEye = inverse(projectionMatrix);
auto n = -(clipToEye * dvec3(0.0, 0.0, 1.0)).z;
auto f = -(clipToEye * dvec3(0.0, 0.0, 0.0)).z;

// if regions of interest have been found in the scene graph use them to clamp the near/far values.
if (!rt.regionsOfInterest.empty())
{
dbox eyeSpaceRegionBounds;
for (auto& [mv, regionOfInterest] : rt.regionsOfInterest)
{
for (auto& v : regionOfInterest->points)
{
eyeSpaceRegionBounds.add(mv * v);
}
}

if (eyeSpaceRegionBounds)
{
double regionNear = -eyeSpaceRegionBounds.max.z;
double regionFar = -eyeSpaceRegionBounds.min.z;
if (regionNear > n) { n = regionNear; }
if (regionFar < f) { f = regionFar; }
}
}

// set up the light data
auto light_itr = lightData->begin();
lightData->dirty();
Expand Down Expand Up @@ -629,14 +662,6 @@ void ViewDependentState::traverse(RecordTraversal& rt) const
requiresPerRenderShadowMaps = true;

// compute directional light space
auto projectionMatrix = view->camera->projectionMatrix->transform();
auto viewMatrix = view->camera->viewMatrix->transform();
auto inverse_viewMatrix = inverse(viewMatrix);

// view direction in world coords
auto view_direction = normalize(dvec3(0.0, 0.0, -1.0) * (projectionMatrix * viewMatrix));
auto view_up = normalize(dvec3(0.0, -1.0, 0.0) * (projectionMatrix * viewMatrix));

// light direction in world coords
auto light_direction = normalize(light->direction * (inverse_3x3(mv * inverse_viewMatrix)));
#if 0
Expand All @@ -652,11 +677,6 @@ void ViewDependentState::traverse(RecordTraversal& rt) const
auto light_y = cross(light_x, light_direction);
auto light_z = light_direction;

auto clipToEye = inverse(projectionMatrix);

auto n = -(clipToEye * dvec3(0.0, 0.0, 1.0)).z;
auto f = -(clipToEye * dvec3(0.0, 0.0, 0.0)).z;

// clamp the near and far values
if (n > maxShadowDistance)
{
Expand Down
Loading