diff --git a/include/vsg/app/LoadBalancing.h b/include/vsg/app/LoadBalancing.h
new file mode 100644
index 0000000000..17c6e0afac
--- /dev/null
+++ b/include/vsg/app/LoadBalancing.h
@@ -0,0 +1,55 @@
+#pragma once
+
+/*
+
+Copyright(c) 2026 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
+#include
+
+namespace vsg
+{
+
+ // forward declare
+ class Viewer;
+
+
+ /// LoadBalancing is a base class for specifying the Camera view matrix and its inverse.
+ class VSG_DECLSPEC LoadBalancing : public Inherit
+ {
+ public:
+ LoadBalancing();
+ explicit LoadBalancing(const LoadBalancing& rhs, const CopyOp& copyop = {});
+
+ virtual void update(Viewer& viewer);
+
+ time_point previousFrameTime = std::numeric_limits::max();
+
+ double targetFrameTime = 0.016; // seconds
+ double targetCPUTime = 0.008; // seconds
+ double targetGPUTime = 0.008; // seconds
+ double targetGPUMemoryUtilization = 0.9; // ratio
+
+ void control(ref_ptr view) { views.emplace_back(view); }
+ void control(ref_ptr pager) { pagers.emplace_back(pager); }
+
+ std::vector> views;
+ std::vector> pagers;
+
+ protected:
+ ~LoadBalancing();
+ };
+ VSG_type_name(vsg::LoadBalancing);
+
+
+} // namespace vsg
diff --git a/include/vsg/app/Viewer.h b/include/vsg/app/Viewer.h
index a2603e0daa..d34b4a694c 100644
--- a/include/vsg/app/Viewer.h
+++ b/include/vsg/app/Viewer.h
@@ -18,6 +18,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include
#include
#include
+#include
#include
#include
#include
@@ -129,6 +130,8 @@ namespace vsg
ref_ptr status;
std::list threads;
+ ref_ptr loadBalancing;
+
void setupThreading();
void stopThreading();
diff --git a/include/vsg/utils/Profiler.h b/include/vsg/utils/Profiler.h
index 27753ef513..6c2f29b73e 100644
--- a/include/vsg/utils/Profiler.h
+++ b/include/vsg/utils/Profiler.h
@@ -42,6 +42,7 @@ namespace vsg
const SourceLocation* sourceLocation = nullptr;
const Object* object = nullptr;
uint64_t reference = 0;
+ uint64_t frameCount = 0;
std::thread::id thread_id = {};
};
@@ -134,6 +135,7 @@ namespace vsg
};
mutable size_t frameIndex = 0;
+ mutable std::atomic_uint64_t frameCount = 0;
mutable std::vector perFrameGPUStats;
VkResult getGpuResults(FrameStatsCollection& frameStats) const;
diff --git a/src/vsg/CMakeLists.txt b/src/vsg/CMakeLists.txt
index 89c4c71b64..819deeb201 100644
--- a/src/vsg/CMakeLists.txt
+++ b/src/vsg/CMakeLists.txt
@@ -187,6 +187,7 @@ set(SOURCES
app/UpdateOperations.cpp
app/RecordTraversal.cpp
app/CompileTraversal.cpp
+ app/LoadBalancing.cpp
raytracing/AccelerationGeometry.cpp
raytracing/AccelerationStructure.cpp
diff --git a/src/vsg/app/LoadBalancing.cpp b/src/vsg/app/LoadBalancing.cpp
new file mode 100644
index 0000000000..0cdece7a4d
--- /dev/null
+++ b/src/vsg/app/LoadBalancing.cpp
@@ -0,0 +1,159 @@
+/*
+
+Copyright(c) 2026 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
+#include
+
+using namespace vsg;
+
+LoadBalancing::LoadBalancing()
+{
+}
+
+LoadBalancing::LoadBalancing(const LoadBalancing& rhs, const CopyOp& copyop) :
+ Inherit(rhs, copyop)
+{
+}
+
+LoadBalancing::~LoadBalancing()
+{
+}
+
+void LoadBalancing::update(Viewer& viewer)
+{
+ if (previousFrameTime == std::numeric_limits::max())
+ {
+ previousFrameTime = viewer.getFrameStamp()->time;
+ return;
+ }
+
+ double frameDelta = (previousFrameTime == std::numeric_limits::max()) ? 0.0 : std::chrono::duration(viewer.getFrameStamp()->time - previousFrameTime).count();
+ uint64_t previousFrameCount = viewer.getFrameStamp()->frameCount - 1;
+
+ // vsg::info("LoadBalancing::update( ", &viewer, " ) ", viewer.getFrameStamp()->frameCount, ", ", frameDelta);
+
+ double frameTargetRatio = frameDelta/targetFrameTime;
+ double cpuTargetRatio = 0.0;
+ double gpuTargetRatio = 0.0;
+
+#if 0
+ for(auto& task : viewer.recordAndSubmitTasks)
+ {
+ if (task->databasePager)
+ {
+
+ auto& pagedLODContainer = task->databasePager->pagedLODContainer;
+ if (task->databasePager->frameCountFailedToCompile != std::numeric_limits::max())
+ {
+ uint64_t delta = task->databasePager->frameCount - task->databasePager->frameCountFailedToCompile;
+
+ vsg::info("LoadBalancing::update(Viewer& viewer) ", task->databasePager, " ", task->databasePager->numActiveRequests, ", frames since last compile failure delta = ", delta);
+ vsg::info(" availableList.count = ", pagedLODContainer->availableList.count,
+ ", activeList.count = ", pagedLODContainer->activeList.count,
+ ", inactiveList.count = ", pagedLODContainer->inactiveList.count);
+ }
+ else
+ {
+ vsg::info("LoadBalancing::update(Viewer& viewer) ", task->databasePager, " ", task->databasePager->numActiveRequests, " All loads compiled ");
+ vsg::info(" availableList.count = ", pagedLODContainer->availableList.count,
+ ", activeList.count = ", pagedLODContainer->activeList.count,
+ ", inactiveList.count = ", pagedLODContainer->inactiveList.count);
+ }
+ }
+ }
+#endif
+
+ if (auto profiler = viewer.instrumentation.cast())
+ {
+ auto& log = profiler->log;
+
+ size_t targetFrameCount = 16;
+
+ double max_cpu_durection = 0.0;
+ double max_gpu_durection = 0.0;
+
+ auto& frameIndices = log->frameIndices;
+ auto last_frame = frameIndices.end();
+ auto first_frame = (frameIndices.size() > targetFrameCount) ? (frameIndices.end() - targetFrameCount) : frameIndices.begin();
+ for(auto itr = first_frame; itr != last_frame; ++itr)
+ {
+ auto first_index = *itr;
+ const auto& first = log->entry(first_index);
+ auto last_index = first.reference;
+
+ auto cpu_duration = 0.0;
+ auto gpu_duration = 0.0;
+
+ for(auto i = first_index; i <= last_index;)
+ {
+ if (log->entry(i).type == ProfileLog::COMMAND_BUFFER)
+ {
+ const auto& start = log->entry(i);
+ const auto& end = log->entry(start.reference);
+ if (start.gpuTime != 0 && end.gpuTime != 0)
+ {
+ gpu_duration = static_cast(end.gpuTime - start.gpuTime) * log->timestampScaleToMilliseconds * 0.001;
+ if (gpu_duration > max_gpu_durection)
+ {
+ max_gpu_durection = gpu_duration;
+ }
+ }
+
+ cpu_duration = std::abs(std::chrono::duration(end.cpuTime - start.cpuTime).count());
+
+ if (cpu_duration > max_cpu_durection)
+ {
+ max_cpu_durection = cpu_duration;
+ }
+
+ i = start.reference+1;
+ }
+ else
+ {
+ ++i;
+ }
+ }
+
+
+
+#if 0
+ if (first.sourceLocation) vsg::info(" entry(", first_index,") { ", int(first.type), ", ", first.reference, ", func=", first.sourceLocation->function, ", cpu_duration = ", cpu_duration, ", gpu_duration = ", gpu_duration, "} ");
+ else vsg::info(" entry(", first_index,") { ", int(first.type), ", ", first.reference, " }");
+
+ if (max_gpu_durection > targetGPUTime)
+ {
+ vsg::info(" GPU time too high ", max_gpu_durection, "ms is greated than target ", targetGPUTime, "ms");
+ }
+#endif
+ }
+
+ cpuTargetRatio = max_cpu_durection/targetCPUTime;
+ gpuTargetRatio = max_gpu_durection/targetGPUTime;
+ }
+
+ double maxTargetRatio = std::max({frameTargetRatio, cpuTargetRatio, gpuTargetRatio});
+
+ if (maxTargetRatio > 2.0)
+ {
+ vsg::info("Frame break on frameCount = ", previousFrameCount, " : maxTargetRatio = ", maxTargetRatio, " { frame = ", frameTargetRatio, ", cpu = ", cpuTargetRatio, ", gpu = ", gpuTargetRatio, " }");
+ }
+ else if (maxTargetRatio > 1.2)
+ {
+ vsg::info("Frame targets exceeded on frameCount = ", previousFrameCount, " : maxTargetRatio = ", maxTargetRatio, " { frame = ", frameTargetRatio, ", cpu = ", cpuTargetRatio, ", gpu = ", gpuTargetRatio, " }");
+ }
+
+ previousFrameTime = viewer.getFrameStamp()->time;
+}
+
+
diff --git a/src/vsg/app/Viewer.cpp b/src/vsg/app/Viewer.cpp
index 07a599860f..10f62c3f8b 100644
--- a/src/vsg/app/Viewer.cpp
+++ b/src/vsg/app/Viewer.cpp
@@ -31,6 +31,7 @@ Viewer::Viewer() :
updateOperations(UpdateOperations::create()),
animationManager(AnimationManager::create()),
status(vsg::ActivityStatus::create()),
+ loadBalancing(vsg::LoadBalancing::create()),
_firstFrame(true),
_start_point(clock::now()),
_frameStamp(FrameStamp::create(_start_point, 0, 0.0))
@@ -793,6 +794,8 @@ void Viewer::update()
{
CPU_INSTRUMENTATION_L1_NC(instrumentation, "Viewer update", COLOR_UPDATE);
+ if (loadBalancing) loadBalancing->update(*this);
+
// merge any updates from the DatabasePager
for (const auto& task : recordAndSubmitTasks)
{
diff --git a/src/vsg/utils/Profiler.cpp b/src/vsg/utils/Profiler.cpp
index 282774e123..7403b6ae65 100644
--- a/src/vsg/utils/Profiler.cpp
+++ b/src/vsg/utils/Profiler.cpp
@@ -87,7 +87,9 @@ uint64_t ProfileLog::report(std::ostream& out, uint64_t reference)
{
++i;
- out << indent << "{ " << typeNames[first.type] << ", cpu_duration = " << cpu_duration << "ms, ";
+ out << indent << "{ " << typeNames[first.type];
+ if (first.type==ProfileLog::FRAME) out << ", frameCount = "<queryIndex = 0;
}
+ else if (result == VK_NOT_READY)
+ {
+ debug("Profiler::getGpuResults() ", gpuStats, ", query failed with result = ", result);
+ }
else
{
info("Profiler::getGpuResults() ", gpuStats, ", query failed with result = ", result);
@@ -190,9 +198,12 @@ void Profiler::setThreadName(const std::string& name) const
void Profiler::enterFrame(const SourceLocation* sl, uint64_t& reference, FrameStamp& frameStamp) const
{
+ frameCount = frameStamp.frameCount;
+
auto& entry = log->enter(reference, ProfileLog::FRAME);
entry.sourceLocation = sl;
entry.object = &frameStamp;
+ entry.frameCount = frameCount;
getGpuResults(perFrameGPUStats[frameIndex]);
}
@@ -203,6 +214,8 @@ void Profiler::leaveFrame(const SourceLocation* sl, uint64_t& reference, FrameSt
auto& entry = log->leave(reference, ProfileLog::FRAME);
entry.sourceLocation = sl;
entry.object = &frameStamp;
+ entry.frameCount = frameCount;
+
uint64_t endReference = reference;
if (endReference >= static_cast(log->entries.size()))
@@ -236,6 +249,7 @@ void Profiler::enter(const SourceLocation* sl, uint64_t& reference, const Object
auto& entry = log->enter(reference, ProfileLog::CPU);
entry.sourceLocation = sl;
entry.object = object;
+ entry.frameCount = frameCount;
}
}
@@ -246,6 +260,7 @@ void Profiler::leave(const SourceLocation* sl, uint64_t& reference, const Object
auto& entry = log->leave(reference, ProfileLog::CPU);
entry.sourceLocation = sl;
entry.object = object;
+ entry.frameCount = frameCount;
}
}
@@ -285,6 +300,7 @@ void Profiler::enterCommandBuffer(const SourceLocation* sl, uint64_t& reference,
auto& entry = log->enter(reference, ProfileLog::COMMAND_BUFFER);
entry.sourceLocation = sl;
entry.object = &commandBuffer;
+ entry.frameCount = frameCount;
if (gpuStats)
{
@@ -303,6 +319,7 @@ void Profiler::leaveCommandBuffer(const SourceLocation* sl, uint64_t& reference,
auto& entry = log->leave(reference, ProfileLog::COMMAND_BUFFER);
entry.sourceLocation = sl;
entry.object = &commandBuffer;
+ entry.frameCount = frameCount;
if (commandBuffer.gpuStats) commandBuffer.gpuStats->writeGpuTimestamp(commandBuffer, reference, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
}
@@ -315,6 +332,7 @@ void Profiler::enter(const SourceLocation* sl, uint64_t& reference, CommandBuffe
auto& entry = log->enter(reference, ProfileLog::GPU);
entry.sourceLocation = sl;
entry.object = object;
+ entry.frameCount = frameCount;
if (commandBuffer.gpuStats) commandBuffer.gpuStats->writeGpuTimestamp(commandBuffer, reference, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
}
@@ -327,6 +345,7 @@ void Profiler::leave(const SourceLocation* sl, uint64_t& reference, CommandBuffe
auto& entry = log->leave(reference, ProfileLog::GPU);
entry.sourceLocation = sl;
entry.object = object;
+ entry.frameCount = frameCount;
if (commandBuffer.gpuStats) commandBuffer.gpuStats->writeGpuTimestamp(commandBuffer, reference, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
}