Skip to content
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
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "nvrhi"]
path = nvrhi
url = ../NVRHI.git
url = https://github.com/NVIDIA-RTX/NVRHI.git
[submodule "thirdparty/imgui"]
path = thirdparty/imgui
url = https://github.com/ocornut/imgui
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ Supported glTF extensions:

In addition to glTF, Donut supports its own [JSON-based scene layout files](doc/SceneFile.md). Those files can load multiple glTF models and combine them into a larger scene graph, also add lights, cameras, animations, and apply animations to scene nodes imported from the models using their paths.

### Scene memory statistics

For geometry buffer accounting, see [scene geometry memory statistics](doc/MemoryStats.md).

### Render passes

For a full list of render passes, refer to the headers in the [include/donut/render](include/donut/render) folder.
Expand Down
28 changes: 28 additions & 0 deletions doc/MemoryStats.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Scene geometry memory statistics

`Scene::getGeometryMemoryStats()` returns current vertex/index buffer totals, deduplicating
shared buffer handles within each category. A handle used in both categories counts once
in each, so adding the categories does not give unique physical memory usage. It keeps
descriptor `capacityBytes` separate from backing-buffer `allocationBytes`, which sums only
successful queries. Check `hasKnownAllocationBytes()` before displaying a complete allocation
total: unavailable resources increment `unknownAllocationCount`, not a known zero.
`resourceCount` includes both known and unknown non-null buffers. Missing graphs, meshes,
buffer groups, and buffer handles contribute nothing; an empty category is known zero.
Re-query after scene resource replacement or removal; the statistics do not retain resources.
The free function `getSceneGeometryMemoryStats(device, scene)` provides the same aggregation
with an explicit device; a null device preserves capacity and counts but makes allocation
sizes unavailable for every counted buffer.

`tryGetResourceAllocationBytes` uses NVRHI's optional `IResource::queryMemoryRequirements`
and leaves `outBytes` unchanged on failure, including a null device or resource. The helper's
device must be the resource's owning device. It filters D3D11 resources and textures before
calling NVRHI, whose unsupported queries assert in debug builds; these remain unavailable
without modifying the output. Supported resources dispatch the query to their backend.
See [NVRHI's optional memory queries](../nvrhi/doc/memory-queries.md) for
backend support and exceptions, allocation interpretation, and the native
`IDevice::queryTopLevelAccelStructPrebuildInfo` API. Application-specific pooled BLAS/OMM
attribution and statistics UI remain the consuming application's responsibility.

The headless [memory statistics regression](../tests/src/memory_stats.cpp) covers the public
helper and scene aggregation. Its build conditions and CTest registration are defined in
[tests/test-engine.cmake](../tests/test-engine.cmake).
57 changes: 57 additions & 0 deletions include/donut/engine/MemoryStats.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
*
* 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.
*/

#pragma once

#include <nvrhi/nvrhi.h>

#include <cstdint>

namespace donut::engine
{
class Scene;

struct GpuMemoryUsage
{
uint64_t capacityBytes = 0;
uint64_t allocationBytes = 0;
uint32_t resourceCount = 0;
uint32_t unknownAllocationCount = 0;

[[nodiscard]] bool hasKnownAllocationBytes() const { return unknownAllocationCount == 0; }
};

struct SceneGeometryMemoryStats
{
GpuMemoryUsage vertexBuffers;
GpuMemoryUsage indexBuffers;
};

// Returns backing-buffer memory requirements, not residency or unique heap allocation bytes.
// Pass the resource's owning device. Unsupported queries leave outBytes unchanged;
// descriptor capacity remains separately available.
bool tryGetResourceAllocationBytes(nvrhi::IDevice* device, nvrhi::IResource* resource, uint64_t& outBytes);

// Deduplicates buffers within each category. Shared heaps and cross-category aliases need
// application-specific attribution rather than summing these requirements as physical memory.
SceneGeometryMemoryStats getSceneGeometryMemoryStats(nvrhi::IDevice* device, const Scene& scene);
}
2 changes: 2 additions & 0 deletions include/donut/engine/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#pragma once

#include <donut/engine/MemoryStats.h>
#include <donut/engine/SceneGraph.h>
#include <nvrhi/nvrhi.h>
#include <vector>
Expand Down Expand Up @@ -145,6 +146,7 @@ namespace donut::engine
[[nodiscard]] nvrhi::IBuffer* GetMaterialBuffer() const { return m_MaterialBuffer; }
[[nodiscard]] nvrhi::IBuffer* GetGeometryBuffer() const { return m_GeometryBuffer; }
[[nodiscard]] nvrhi::IBuffer* GetInstanceBuffer() const { return m_InstanceBuffer; }
[[nodiscard]] SceneGeometryMemoryStats getGeometryMemoryStats() const;

// Can return nullptr if not yet created (by Scene::RefreshBuffers)
GeometryData* GetGeometryData(const MeshGeometry& geometry) const;
Expand Down
89 changes: 89 additions & 0 deletions src/engine/MemoryStats.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
*
* 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 <donut/engine/MemoryStats.h>
#include <donut/engine/Scene.h>
#include <donut/engine/SceneTypes.h>

#include <unordered_set>

namespace donut::engine
{
namespace
{
void addBuffer(nvrhi::IDevice* device, nvrhi::IBuffer* buffer, GpuMemoryUsage& stats,
std::unordered_set<nvrhi::IBuffer*>& visited)
{
if (buffer == nullptr || !visited.insert(buffer).second)
return;

stats.resourceCount++;
stats.capacityBytes += buffer->getDesc().byteSize;

uint64_t allocationBytes = 0;
if (tryGetResourceAllocationBytes(device, buffer, allocationBytes))
stats.allocationBytes += allocationBytes;
else
stats.unknownAllocationCount++;
}
}

bool tryGetResourceAllocationBytes(nvrhi::IDevice* device, nvrhi::IResource* resource, uint64_t& outBytes)
{
if (device == nullptr || resource == nullptr)
return false;

// NVRHI asserts for these unsupported queries in debug builds.
if (device->getGraphicsAPI() == nvrhi::GraphicsAPI::D3D11 || dynamic_cast<nvrhi::ITexture*>(resource))
return false;

nvrhi::MemoryRequirements requirements;
if (!resource->queryMemoryRequirements(requirements))
return false;

outBytes = requirements.size;
return true;
}

SceneGeometryMemoryStats getSceneGeometryMemoryStats(nvrhi::IDevice* device, const Scene& scene)
{
SceneGeometryMemoryStats stats;

const std::shared_ptr<SceneGraph> sceneGraph = scene.GetSceneGraph();
if (sceneGraph == nullptr)
return stats;

std::unordered_set<nvrhi::IBuffer*> vertexBuffers;
std::unordered_set<nvrhi::IBuffer*> indexBuffers;

for (const std::shared_ptr<MeshInfo>& mesh : sceneGraph->GetMeshes())
{
if (mesh == nullptr || mesh->buffers == nullptr)
continue;

addBuffer(device, mesh->buffers->vertexBuffer, stats.vertexBuffers, vertexBuffers);
addBuffer(device, mesh->buffers->indexBuffer, stats.indexBuffers, indexBuffers);
}

return stats;
}
}
5 changes: 5 additions & 0 deletions src/engine/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,11 @@ GeometryData* Scene::GetGeometryData(const MeshGeometry& geometry) const
return &m_Resources->geometryData[geometry.globalGeometryIndex];
}

SceneGeometryMemoryStats Scene::getGeometryMemoryStats() const
{
return getSceneGeometryMemoryStats(m_Device, *this);
}


void Scene::UpdateInstance(const std::shared_ptr<MeshInstance>& instance)
{
Expand Down
Loading
Loading