From 1de0d46505ca73a997132173f969f7103850585f Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 3 Apr 2024 10:04:32 +0100 Subject: [PATCH 1/3] Added RegionOfInterest node --- include/vsg/all.h | 1 + include/vsg/nodes/RegionOfInterest.h | 57 +++++++++++++++++++++++++ src/vsg/CMakeLists.txt | 1 + src/vsg/nodes/RegionOfInterest.cpp | 62 ++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 include/vsg/nodes/RegionOfInterest.h create mode 100644 src/vsg/nodes/RegionOfInterest.cpp diff --git a/include/vsg/all.h b/include/vsg/all.h index 7022b6ee5..cae892c94 100644 --- a/include/vsg/all.h +++ b/include/vsg/all.h @@ -71,6 +71,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #include #include #include +#include #include #include #include diff --git a/include/vsg/nodes/RegionOfInterest.h b/include/vsg/nodes/RegionOfInterest.h new file mode 100644 index 000000000..93d767d03 --- /dev/null +++ b/include/vsg/nodes/RegionOfInterest.h @@ -0,0 +1,57 @@ +#pragma once + +/* + +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. + + */ + +#include +#include + +namespace vsg +{ + + /// RegionOfInterest node is inform applications/algorithms extents that should take account of. + class VSG_DECLSPEC RegionOfInterest : public Inherit + { + public: + RegionOfInterest(); + RegionOfInterest(const RegionOfInterest& rhs, const CopyOp& copyop = {}); + + Mask mask = MASK_ALL; + std::string name; + std::vector points; + + public: + ref_ptr 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 diff --git a/src/vsg/CMakeLists.txt b/src/vsg/CMakeLists.txt index 057b993d1..36597e0fe 100644 --- a/src/vsg/CMakeLists.txt +++ b/src/vsg/CMakeLists.txt @@ -43,6 +43,7 @@ set(SOURCES nodes/StateGroup.cpp nodes/TileDatabase.cpp nodes/InstrumentationNode.cpp + nodes/RegionOfInterest.cpp lighting/Light.cpp lighting/AmbientLight.cpp diff --git a/src/vsg/nodes/RegionOfInterest.cpp b/src/vsg/nodes/RegionOfInterest.cpp new file mode 100644 index 000000000..d4365f07e --- /dev/null +++ b/src/vsg/nodes/RegionOfInterest.cpp @@ -0,0 +1,62 @@ +/* + +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. + + */ + +#include +#include +#include + +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(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); +} From 383218bb471a094b8c21dcc0c4fe03ae44876bda Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 3 Apr 2024 12:48:28 +0100 Subject: [PATCH 2/3] Implemented RegionOfInterest support in ViewDependentState --- include/vsg/app/RecordTraversal.h | 5 +++ include/vsg/core/ConstVisitor.h | 2 ++ include/vsg/core/Visitor.h | 2 ++ src/vsg/app/CommandGraph.cpp | 1 + src/vsg/app/RecordTraversal.cpp | 12 ++++++++ src/vsg/core/ConstVisitor.cpp | 4 +++ src/vsg/core/Visitor.cpp | 4 +++ src/vsg/state/ViewDependentState.cpp | 46 ++++++++++++++++++++-------- 8 files changed, 63 insertions(+), 13 deletions(-) diff --git a/include/vsg/app/RecordTraversal.h b/include/vsg/app/RecordTraversal.h index d85b538f6..a3f31c874 100644 --- a/include/vsg/app/RecordTraversal.h +++ b/include/vsg/app/RecordTraversal.h @@ -51,6 +51,7 @@ namespace vsg class View; class Bin; class Switch; + class RegionOfInterest; class ViewDependentState; class Light; class AmbientLight; @@ -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); @@ -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> regionsOfInterest; + protected: virtual ~RecordTraversal(); diff --git a/include/vsg/core/ConstVisitor.h b/include/vsg/core/ConstVisitor.h index 2a1d8f19e..841d15ebb 100644 --- a/include/vsg/core/ConstVisitor.h +++ b/include/vsg/core/ConstVisitor.h @@ -49,6 +49,7 @@ namespace vsg class PointLight; class SpotLight; class InstrumentationNode; + class RegionOfInterest; // forward declare text classes class Text; @@ -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&); diff --git a/include/vsg/core/Visitor.h b/include/vsg/core/Visitor.h index 331bac495..93a9ae31c 100644 --- a/include/vsg/core/Visitor.h +++ b/include/vsg/core/Visitor.h @@ -49,6 +49,7 @@ namespace vsg class PointLight; class SpotLight; class InstrumentationNode; + class RegionOfInterest; // forward declare text classes class Text; @@ -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&); diff --git a/src/vsg/app/CommandGraph.cpp b/src/vsg/app/CommandGraph.cpp index 319306ce4..13968680b 100644 --- a/src/vsg/app/CommandGraph.cpp +++ b/src/vsg/app/CommandGraph.cpp @@ -92,6 +92,7 @@ void CommandGraph::record(ref_ptr recordedCommandBuffers recordTraversal->setFrameStamp(frameStamp); recordTraversal->setDatabasePager(databasePager); recordTraversal->clearBins(); + recordTraversal->regionsOfInterest.clear(); ref_ptr commandBuffer; for (auto& cb : _commandBuffers) diff --git a/src/vsg/app/RecordTraversal.cpp b/src/vsg/app/RecordTraversal.cpp index 382c53c93..a131f5fc5 100644 --- a/src/vsg/app/RecordTraversal.cpp +++ b/src/vsg/app/RecordTraversal.cpp @@ -289,6 +289,14 @@ 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); @@ -497,6 +505,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; @@ -566,6 +577,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; } diff --git a/src/vsg/core/ConstVisitor.cpp b/src/vsg/core/ConstVisitor.cpp index b8c9e0745..cc2d578f0 100644 --- a/src/vsg/core/ConstVisitor.cpp +++ b/src/vsg/core/ConstVisitor.cpp @@ -625,6 +625,10 @@ void ConstVisitor::apply(const InstrumentationNode& value) { apply(static_cast(value)); } +void ConstVisitor::apply(const RegionOfInterest& value) +{ + apply(static_cast(value)); +} //////////////////////////////////////////////////////////////////////////////// // diff --git a/src/vsg/core/Visitor.cpp b/src/vsg/core/Visitor.cpp index a83ea7e69..6d5497f9d 100644 --- a/src/vsg/core/Visitor.cpp +++ b/src/vsg/core/Visitor.cpp @@ -625,6 +625,10 @@ void Visitor::apply(InstrumentationNode& value) { apply(static_cast(value)); } +void Visitor::apply(RegionOfInterest& value) +{ + apply(static_cast(value)); +} //////////////////////////////////////////////////////////////////////////////// // diff --git a/src/vsg/state/ViewDependentState.cpp b/src/vsg/state/ViewDependentState.cpp index 6cb5fd0be..d67cf9665 100644 --- a/src/vsg/state/ViewDependentState.cpp +++ b/src/vsg/state/ViewDependentState.cpp @@ -25,6 +25,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #include #include #include +#include #include using namespace vsg; @@ -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(); @@ -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 @@ -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) { From 5b431b6aa97d1c2afc352b828200cebc50bb98ea Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 3 Apr 2024 12:48:53 +0100 Subject: [PATCH 3/3] Ran clang-format --- src/vsg/app/RecordTraversal.cpp | 1 - src/vsg/state/ViewDependentState.cpp | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/vsg/app/RecordTraversal.cpp b/src/vsg/app/RecordTraversal.cpp index a131f5fc5..c0ca27fd7 100644 --- a/src/vsg/app/RecordTraversal.cpp +++ b/src/vsg/app/RecordTraversal.cpp @@ -296,7 +296,6 @@ void RecordTraversal::apply(const RegionOfInterest& roi) regionsOfInterest.emplace_back(_state->modelviewMatrixStack.top(), &roi); } - void RecordTraversal::apply(const DepthSorted& depthSorted) { CPU_INSTRUMENTATION_L2_NCO(instrumentation, "DepthSorted", COLOR_RECORD_L2, &depthSorted); diff --git a/src/vsg/state/ViewDependentState.cpp b/src/vsg/state/ViewDependentState.cpp index d67cf9665..47a5d2691 100644 --- a/src/vsg/state/ViewDependentState.cpp +++ b/src/vsg/state/ViewDependentState.cpp @@ -23,9 +23,9 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #include #include #include +#include #include #include -#include #include using namespace vsg; @@ -592,9 +592,9 @@ void ViewDependentState::traverse(RecordTraversal& rt) const if (!rt.regionsOfInterest.empty()) { dbox eyeSpaceRegionBounds; - for(auto& [mv, regionOfInterest] : rt.regionsOfInterest) + for (auto& [mv, regionOfInterest] : rt.regionsOfInterest) { - for(auto& v : regionOfInterest->points) + for (auto& v : regionOfInterest->points) { eyeSpaceRegionBounds.add(mv * v); }