From 7100b72e9dabf9ac08ca940022cbd389439d2ea5 Mon Sep 17 00:00:00 2001 From: Greg Humphreys Date: Tue, 22 Sep 2026 16:25:06 -0400 Subject: [PATCH 1/8] Add reusable GPU memory statistics --- include/donut/engine/MemoryStats.h | 74 ++++++++++++ include/donut/engine/Scene.h | 2 + src/engine/MemoryStats.cpp | 175 +++++++++++++++++++++++++++++ src/engine/Scene.cpp | 5 + 4 files changed, 256 insertions(+) create mode 100644 include/donut/engine/MemoryStats.h create mode 100644 src/engine/MemoryStats.cpp diff --git a/include/donut/engine/MemoryStats.h b/include/donut/engine/MemoryStats.h new file mode 100644 index 00000000..42c24141 --- /dev/null +++ b/include/donut/engine/MemoryStats.h @@ -0,0 +1,74 @@ +/* +* 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 + +#include + +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; + }; + + struct TopLevelAccelStructPrebuildStats + { + uint64_t ResultBytes = 0; + uint64_t ScratchBytes = 0; + uint64_t UpdateScratchBytes = 0; + bool Available = false; + }; + + // Returns the NVRHI allocation size for buffers when the backend exposes it. Capacity remains + // available from IBuffer::getDesc().byteSize even when this returns false. + bool TryGetBufferAllocationBytes(nvrhi::IDevice* device, nvrhi::IBuffer* buffer, uint64_t& outBytes); + bool TryGetAccelStructAllocationBytes(nvrhi::IDevice* device, nvrhi::rt::IAccelStruct* accelStruct, uint64_t& outBytes); + + // Returns the allocation size for resources that expose a native buffer-like object but do not have + // a dedicated NVRHI memory-requirements query, such as opacity micromap arrays on D3D12. + bool TryGetNativeResourceAllocationBytes(nvrhi::IDevice* device, nvrhi::IResource* resource, uint64_t& outBytes); + + SceneGeometryMemoryStats GetSceneGeometryMemoryStats(nvrhi::IDevice* device, const Scene& scene); + + // Queries the build requirements for a TLAS with the supplied instance count. Scratch is the exact + // build request, not a resident allocation: NVRHI suballocates it from an internal shared pool. + TopLevelAccelStructPrebuildStats QueryTopLevelAccelStructPrebuildStats( + nvrhi::IDevice* device, + const nvrhi::rt::AccelStructDesc& tlasDesc, + uint32_t instanceCount); +} diff --git a/include/donut/engine/Scene.h b/include/donut/engine/Scene.h index 31886a16..f16421de 100644 --- a/include/donut/engine/Scene.h +++ b/include/donut/engine/Scene.h @@ -22,6 +22,7 @@ #pragma once +#include #include #include #include @@ -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; diff --git a/src/engine/MemoryStats.cpp b/src/engine/MemoryStats.cpp new file mode 100644 index 00000000..19926348 --- /dev/null +++ b/src/engine/MemoryStats.cpp @@ -0,0 +1,175 @@ +/* +* 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 +#include +#include + +#include + +#if DONUT_WITH_DX12 +#include +#include +#endif + +namespace donut::engine +{ +namespace +{ + void AddBuffer(nvrhi::IDevice* device, nvrhi::IBuffer* buffer, GpuMemoryUsage& stats, + std::unordered_set& visited) + { + if (buffer == nullptr || !visited.insert(buffer).second) + return; + + stats.ResourceCount++; + stats.CapacityBytes += buffer->getDesc().byteSize; + + uint64_t allocationBytes = 0; + if (TryGetBufferAllocationBytes(device, buffer, allocationBytes)) + stats.AllocationBytes += allocationBytes; + else + stats.UnknownAllocationCount++; + } +} + +bool TryGetBufferAllocationBytes(nvrhi::IDevice* device, nvrhi::IBuffer* buffer, uint64_t& outBytes) +{ + if (device == nullptr || buffer == nullptr) + return false; + + const nvrhi::MemoryRequirements requirements = device->getBufferMemoryRequirements(buffer); + if (requirements.size == 0) + return false; + + outBytes = requirements.size; + return true; +} + +bool TryGetAccelStructAllocationBytes(nvrhi::IDevice* device, nvrhi::rt::IAccelStruct* accelStruct, uint64_t& outBytes) +{ + if (device == nullptr || accelStruct == nullptr) + return false; + + const nvrhi::MemoryRequirements requirements = device->getAccelStructMemoryRequirements(accelStruct); + if (requirements.size == 0) + return false; + + outBytes = requirements.size; + return true; +} + +bool TryGetNativeResourceAllocationBytes(nvrhi::IDevice* device, nvrhi::IResource* resource, uint64_t& outBytes) +{ + if (device == nullptr || resource == nullptr) + return false; + +#if DONUT_WITH_DX12 + if (device->getGraphicsAPI() != nvrhi::GraphicsAPI::D3D12) + return false; + + ID3D12Resource* d3dResource = resource->getNativeObject(nvrhi::ObjectTypes::D3D12_Resource); + if (d3dResource == nullptr) + return false; + + ID3D12Device* d3dDevice = device->getNativeObject(nvrhi::ObjectTypes::D3D12_Device); + if (d3dDevice == nullptr) + return false; + + const D3D12_RESOURCE_DESC desc = d3dResource->GetDesc(); + const D3D12_RESOURCE_ALLOCATION_INFO info = d3dDevice->GetResourceAllocationInfo(0, 1, &desc); + if (info.SizeInBytes == UINT64_MAX) + return false; + + outBytes = info.SizeInBytes; + return true; +#else + (void)outBytes; + return false; +#endif +} + +SceneGeometryMemoryStats GetSceneGeometryMemoryStats(nvrhi::IDevice* device, const Scene& scene) +{ + SceneGeometryMemoryStats stats; + + const std::shared_ptr sceneGraph = scene.GetSceneGraph(); + if (sceneGraph == nullptr) + return stats; + + std::unordered_set vertexBuffers; + std::unordered_set indexBuffers; + + for (const std::shared_ptr& 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; +} + +TopLevelAccelStructPrebuildStats QueryTopLevelAccelStructPrebuildStats( + nvrhi::IDevice* device, + const nvrhi::rt::AccelStructDesc& tlasDesc, + uint32_t instanceCount) +{ + TopLevelAccelStructPrebuildStats stats; + +#if DONUT_WITH_DX12 + if (device == nullptr || device->getGraphicsAPI() != nvrhi::GraphicsAPI::D3D12 || !tlasDesc.isTopLevel) + return stats; + + ID3D12Device* d3dDevice = device->getNativeObject(nvrhi::ObjectTypes::D3D12_Device); + if (d3dDevice == nullptr) + return stats; + + Microsoft::WRL::ComPtr d3dDevice5; + if (FAILED(d3dDevice->QueryInterface(IID_PPV_ARGS(&d3dDevice5)))) + return stats; + + nvrhi::rt::AccelStructBuildFlags buildFlags = tlasDesc.buildFlags & ~nvrhi::rt::AccelStructBuildFlags::AllowEmptyInstances; + + D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS inputs = {}; + inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL; + inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY; + inputs.InstanceDescs = 0; + inputs.NumDescs = instanceCount; + inputs.Flags = (D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS)buildFlags; + + D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO info = {}; + d3dDevice5->GetRaytracingAccelerationStructurePrebuildInfo(&inputs, &info); + + stats.ResultBytes = info.ResultDataMaxSizeInBytes; + stats.ScratchBytes = info.ScratchDataSizeInBytes; + stats.UpdateScratchBytes = info.UpdateScratchDataSizeInBytes; + stats.Available = true; +#else + (void)device; (void)tlasDesc; (void)instanceCount; +#endif + + return stats; +} +} diff --git a/src/engine/Scene.cpp b/src/engine/Scene.cpp index 1a2fffff..51be57ea 100644 --- a/src/engine/Scene.cpp +++ b/src/engine/Scene.cpp @@ -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& instance) { From d83b3eef42a53ffb8f59708975a201b149b9dd1b Mon Sep 17 00:00:00 2001 From: Greg Humphreys Date: Wed, 23 Sep 2026 10:18:29 -0400 Subject: [PATCH 2/8] Use NVRHI optional memory queries and camelCase statistics --- .gitmodules | 2 +- doc/MemoryStats.md | 18 +++++ include/donut/engine/MemoryStats.h | 44 ++++------- include/donut/engine/Scene.h | 2 +- nvrhi | 2 +- src/engine/MemoryStats.cpp | 117 +++-------------------------- src/engine/Scene.cpp | 4 +- 7 files changed, 48 insertions(+), 141 deletions(-) create mode 100644 doc/MemoryStats.md diff --git a/.gitmodules b/.gitmodules index 244a4906..5ecc1e09 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "nvrhi"] path = nvrhi - url = ../NVRHI.git + url = https://github.com/nvhumper/NVRHI.git [submodule "thirdparty/imgui"] path = thirdparty/imgui url = https://github.com/ocornut/imgui diff --git a/doc/MemoryStats.md b/doc/MemoryStats.md new file mode 100644 index 00000000..7ac9dd93 --- /dev/null +++ b/doc/MemoryStats.md @@ -0,0 +1,18 @@ +# Scene geometry memory statistics + +`Scene::getGeometryMemoryStats()` returns current vertex/index buffer totals, deduplicating +shared buffer handles within each category. It keeps descriptor `capacityBytes` separate +from backing-buffer `allocationBytes`. Check `hasKnownAllocationBytes()` before displaying +an allocation total: unavailable resources increment `unknownAllocationCount`, not a known zero. +Re-query after scene resource replacement or removal; the statistics do not retain resources. + +`tryGetResourceAllocationBytes` uses NVRHI's optional `queryResourceMemoryRequirements`. +D3D11 reports unavailable safely; D3D12 and Vulkan report requirements for exposed backing +buffers, including acceleration structures and opacity micromaps. These numbers are not +residency, driver overhead, or unique shared-heap allocation. Pool reservations and aliases +between categories need attribution by their owner in the consuming application. + +TLAS prebuild queries are exposed directly by +`IDevice::queryTopLevelAccelStructPrebuildInfo` in NVRHI, rather than Donut. Scratch build +requests are not resident scratch-pool capacity. Unsupported backends return `false` and +leave the output unchanged; see NVRHI's `doc/memory-queries.md` for backend support and tests. diff --git a/include/donut/engine/MemoryStats.h b/include/donut/engine/MemoryStats.h index 42c24141..76e0a653 100644 --- a/include/donut/engine/MemoryStats.h +++ b/include/donut/engine/MemoryStats.h @@ -32,43 +32,25 @@ namespace donut::engine struct GpuMemoryUsage { - uint64_t CapacityBytes = 0; - uint64_t AllocationBytes = 0; - uint32_t ResourceCount = 0; - uint32_t UnknownAllocationCount = 0; + uint64_t capacityBytes = 0; + uint64_t allocationBytes = 0; + uint32_t resourceCount = 0; + uint32_t unknownAllocationCount = 0; - [[nodiscard]] bool HasKnownAllocationBytes() const { return UnknownAllocationCount == 0; } + [[nodiscard]] bool hasKnownAllocationBytes() const { return unknownAllocationCount == 0; } }; struct SceneGeometryMemoryStats { - GpuMemoryUsage VertexBuffers; - GpuMemoryUsage IndexBuffers; + GpuMemoryUsage vertexBuffers; + GpuMemoryUsage indexBuffers; }; - struct TopLevelAccelStructPrebuildStats - { - uint64_t ResultBytes = 0; - uint64_t ScratchBytes = 0; - uint64_t UpdateScratchBytes = 0; - bool Available = false; - }; - - // Returns the NVRHI allocation size for buffers when the backend exposes it. Capacity remains - // available from IBuffer::getDesc().byteSize even when this returns false. - bool TryGetBufferAllocationBytes(nvrhi::IDevice* device, nvrhi::IBuffer* buffer, uint64_t& outBytes); - bool TryGetAccelStructAllocationBytes(nvrhi::IDevice* device, nvrhi::rt::IAccelStruct* accelStruct, uint64_t& outBytes); - - // Returns the allocation size for resources that expose a native buffer-like object but do not have - // a dedicated NVRHI memory-requirements query, such as opacity micromap arrays on D3D12. - bool TryGetNativeResourceAllocationBytes(nvrhi::IDevice* device, nvrhi::IResource* resource, uint64_t& outBytes); - - SceneGeometryMemoryStats GetSceneGeometryMemoryStats(nvrhi::IDevice* device, const Scene& scene); + // Returns backing-buffer memory requirements, not residency or unique heap allocation bytes. + // Unsupported queries leave outBytes unchanged; descriptor capacity remains separately available. + bool tryGetResourceAllocationBytes(nvrhi::IDevice* device, nvrhi::IResource* resource, uint64_t& outBytes); - // Queries the build requirements for a TLAS with the supplied instance count. Scratch is the exact - // build request, not a resident allocation: NVRHI suballocates it from an internal shared pool. - TopLevelAccelStructPrebuildStats QueryTopLevelAccelStructPrebuildStats( - nvrhi::IDevice* device, - const nvrhi::rt::AccelStructDesc& tlasDesc, - uint32_t instanceCount); + // 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); } diff --git a/include/donut/engine/Scene.h b/include/donut/engine/Scene.h index f16421de..7290897b 100644 --- a/include/donut/engine/Scene.h +++ b/include/donut/engine/Scene.h @@ -146,7 +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; + [[nodiscard]] SceneGeometryMemoryStats getGeometryMemoryStats() const; // Can return nullptr if not yet created (by Scene::RefreshBuffers) GeometryData* GetGeometryData(const MeshGeometry& geometry) const; diff --git a/nvrhi b/nvrhi index c8d34b4f..0ced597b 160000 --- a/nvrhi +++ b/nvrhi @@ -1 +1 @@ -Subproject commit c8d34b4f75956c94bd9c3ce33b87ec88fead3f08 +Subproject commit 0ced597b84bdaa7e7d1fc57bc107777da6e37961 diff --git a/src/engine/MemoryStats.cpp b/src/engine/MemoryStats.cpp index 19926348..c78db32b 100644 --- a/src/engine/MemoryStats.cpp +++ b/src/engine/MemoryStats.cpp @@ -26,89 +26,38 @@ #include -#if DONUT_WITH_DX12 -#include -#include -#endif - namespace donut::engine { namespace { - void AddBuffer(nvrhi::IDevice* device, nvrhi::IBuffer* buffer, GpuMemoryUsage& stats, + void addBuffer(nvrhi::IDevice* device, nvrhi::IBuffer* buffer, GpuMemoryUsage& stats, std::unordered_set& visited) { if (buffer == nullptr || !visited.insert(buffer).second) return; - stats.ResourceCount++; - stats.CapacityBytes += buffer->getDesc().byteSize; + stats.resourceCount++; + stats.capacityBytes += buffer->getDesc().byteSize; uint64_t allocationBytes = 0; - if (TryGetBufferAllocationBytes(device, buffer, allocationBytes)) - stats.AllocationBytes += allocationBytes; + if (tryGetResourceAllocationBytes(device, buffer, allocationBytes)) + stats.allocationBytes += allocationBytes; else - stats.UnknownAllocationCount++; + stats.unknownAllocationCount++; } } -bool TryGetBufferAllocationBytes(nvrhi::IDevice* device, nvrhi::IBuffer* buffer, uint64_t& outBytes) +bool tryGetResourceAllocationBytes(nvrhi::IDevice* device, nvrhi::IResource* resource, uint64_t& outBytes) { - if (device == nullptr || buffer == nullptr) - return false; - - const nvrhi::MemoryRequirements requirements = device->getBufferMemoryRequirements(buffer); - if (requirements.size == 0) + nvrhi::MemoryRequirements requirements; + if (device == nullptr || !device->queryResourceMemoryRequirements(resource, requirements)) return false; outBytes = requirements.size; return true; } -bool TryGetAccelStructAllocationBytes(nvrhi::IDevice* device, nvrhi::rt::IAccelStruct* accelStruct, uint64_t& outBytes) -{ - if (device == nullptr || accelStruct == nullptr) - return false; - - const nvrhi::MemoryRequirements requirements = device->getAccelStructMemoryRequirements(accelStruct); - if (requirements.size == 0) - return false; - - outBytes = requirements.size; - return true; -} - -bool TryGetNativeResourceAllocationBytes(nvrhi::IDevice* device, nvrhi::IResource* resource, uint64_t& outBytes) -{ - if (device == nullptr || resource == nullptr) - return false; - -#if DONUT_WITH_DX12 - if (device->getGraphicsAPI() != nvrhi::GraphicsAPI::D3D12) - return false; - - ID3D12Resource* d3dResource = resource->getNativeObject(nvrhi::ObjectTypes::D3D12_Resource); - if (d3dResource == nullptr) - return false; - - ID3D12Device* d3dDevice = device->getNativeObject(nvrhi::ObjectTypes::D3D12_Device); - if (d3dDevice == nullptr) - return false; - - const D3D12_RESOURCE_DESC desc = d3dResource->GetDesc(); - const D3D12_RESOURCE_ALLOCATION_INFO info = d3dDevice->GetResourceAllocationInfo(0, 1, &desc); - if (info.SizeInBytes == UINT64_MAX) - return false; - - outBytes = info.SizeInBytes; - return true; -#else - (void)outBytes; - return false; -#endif -} - -SceneGeometryMemoryStats GetSceneGeometryMemoryStats(nvrhi::IDevice* device, const Scene& scene) +SceneGeometryMemoryStats getSceneGeometryMemoryStats(nvrhi::IDevice* device, const Scene& scene) { SceneGeometryMemoryStats stats; @@ -124,52 +73,10 @@ SceneGeometryMemoryStats GetSceneGeometryMemoryStats(nvrhi::IDevice* device, con if (mesh == nullptr || mesh->buffers == nullptr) continue; - AddBuffer(device, mesh->buffers->vertexBuffer, stats.VertexBuffers, vertexBuffers); - AddBuffer(device, mesh->buffers->indexBuffer, stats.IndexBuffers, indexBuffers); + addBuffer(device, mesh->buffers->vertexBuffer, stats.vertexBuffers, vertexBuffers); + addBuffer(device, mesh->buffers->indexBuffer, stats.indexBuffers, indexBuffers); } return stats; } - -TopLevelAccelStructPrebuildStats QueryTopLevelAccelStructPrebuildStats( - nvrhi::IDevice* device, - const nvrhi::rt::AccelStructDesc& tlasDesc, - uint32_t instanceCount) -{ - TopLevelAccelStructPrebuildStats stats; - -#if DONUT_WITH_DX12 - if (device == nullptr || device->getGraphicsAPI() != nvrhi::GraphicsAPI::D3D12 || !tlasDesc.isTopLevel) - return stats; - - ID3D12Device* d3dDevice = device->getNativeObject(nvrhi::ObjectTypes::D3D12_Device); - if (d3dDevice == nullptr) - return stats; - - Microsoft::WRL::ComPtr d3dDevice5; - if (FAILED(d3dDevice->QueryInterface(IID_PPV_ARGS(&d3dDevice5)))) - return stats; - - nvrhi::rt::AccelStructBuildFlags buildFlags = tlasDesc.buildFlags & ~nvrhi::rt::AccelStructBuildFlags::AllowEmptyInstances; - - D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS inputs = {}; - inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL; - inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY; - inputs.InstanceDescs = 0; - inputs.NumDescs = instanceCount; - inputs.Flags = (D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS)buildFlags; - - D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO info = {}; - d3dDevice5->GetRaytracingAccelerationStructurePrebuildInfo(&inputs, &info); - - stats.ResultBytes = info.ResultDataMaxSizeInBytes; - stats.ScratchBytes = info.ScratchDataSizeInBytes; - stats.UpdateScratchBytes = info.UpdateScratchDataSizeInBytes; - stats.Available = true; -#else - (void)device; (void)tlasDesc; (void)instanceCount; -#endif - - return stats; -} } diff --git a/src/engine/Scene.cpp b/src/engine/Scene.cpp index 51be57ea..87f132ad 100644 --- a/src/engine/Scene.cpp +++ b/src/engine/Scene.cpp @@ -1223,9 +1223,9 @@ GeometryData* Scene::GetGeometryData(const MeshGeometry& geometry) const return &m_Resources->geometryData[geometry.globalGeometryIndex]; } -SceneGeometryMemoryStats Scene::GetGeometryMemoryStats() const +SceneGeometryMemoryStats Scene::getGeometryMemoryStats() const { - return GetSceneGeometryMemoryStats(m_Device, *this); + return getSceneGeometryMemoryStats(m_Device, *this); } From c33306d2d28b8fbafe0870589b2c91042cdc122f Mon Sep 17 00:00:00 2001 From: Greg Humphreys Date: Wed, 23 Sep 2026 10:32:38 -0400 Subject: [PATCH 3/8] no-mistakes(test): Add verified headless memory statistics regression tests --- tests/src/memory_stats.cpp | 234 +++++++++++++++++++++++++++++++++++++ tests/test-engine.cmake | 20 ++++ 2 files changed, 254 insertions(+) create mode 100644 tests/src/memory_stats.cpp diff --git a/tests/src/memory_stats.cpp b/tests/src/memory_stats.cpp new file mode 100644 index 00000000..eba16d9e --- /dev/null +++ b/tests/src/memory_stats.cpp @@ -0,0 +1,234 @@ +// Headless native-device regression for the public Donut memory statistics APIs. +// No command lists are submitted and no windows or rendering workloads are created. +#include +#include +#include +#include +#include +#include +#if TEST_VALIDATION +#include +#endif +#include +#include +#include +#include +#include +#include +#ifdef _MSC_VER +#include +#endif + +using namespace donut::engine; +using Microsoft::WRL::ComPtr; + +static void check(bool value, const char* message) +{ + if (!value) + throw std::runtime_error(message); +} + +struct Messages : nvrhi::IMessageCallback +{ + unsigned errors = 0; + void message(nvrhi::MessageSeverity severity, const char* text) override + { + if (severity == nvrhi::MessageSeverity::Error || severity == nvrhi::MessageSeverity::Fatal) + ++errors; + std::printf("NVRHI: %s\n", text); + } +}; + +static void expectUsage(const GpuMemoryUsage& usage, uint64_t capacity, uint64_t allocation, + uint32_t count, uint32_t unknown) +{ + check(usage.capacityBytes == capacity, "descriptor capacity total"); + check(usage.allocationBytes == allocation, "backing allocation total"); + check(usage.resourceCount == count, "unique resource count"); + check(usage.unknownAllocationCount == unknown, "unknown allocation count"); + check(usage.hasKnownAllocationBytes() == (unknown == 0), "unknown versus known zero"); +} + +static void expectEmpty(const SceneGeometryMemoryStats& stats) +{ + expectUsage(stats.vertexBuffers, 0, 0, 0, 0); + expectUsage(stats.indexBuffers, 0, 0, 0, 0); +} + +static uint64_t checkAllocation(nvrhi::IDevice* device, nvrhi::IBuffer* buffer, bool supported) +{ + uint64_t bytes = 123; + check(tryGetResourceAllocationBytes(device, buffer, bytes) == supported, "allocation availability"); + if (!supported) + { + check(bytes == 123, "D3D11 optional query preserves output without legacy assertion"); + return 0; + } + + ID3D12Device* nativeDevice = device->getNativeObject(nvrhi::ObjectTypes::D3D12_Device); + ID3D12Resource* nativeBuffer = buffer->getNativeObject(nvrhi::ObjectTypes::D3D12_Resource); + check(nativeDevice && nativeBuffer, "native D3D12 objects"); + const auto desc = nativeBuffer->GetDesc(); + const auto expected = nativeDevice->GetResourceAllocationInfo(0, 1, &desc); + check(bytes == expected.SizeInBytes && bytes > buffer->getDesc().byteSize, + "allocation requirements match native query, not descriptor capacity"); + std::printf("Buffer capacity=%" PRIu64 " allocation=%" PRIu64 "\n", buffer->getDesc().byteSize, bytes); + return bytes; +} + +static void runScenarios(nvrhi::IDevice* device, const char* shaderPath) +{ + const bool supported = device->getGraphicsAPI() == nvrhi::GraphicsAPI::D3D12; + auto a = device->createBuffer(nvrhi::BufferDesc().setByteSize(1024)); + auto b = device->createBuffer(nvrhi::BufferDesc().setByteSize(2048)); + auto c = device->createBuffer(nvrhi::BufferDesc().setByteSize(4096)); + check(a && b && c, "real native buffers"); + const uint64_t aBytes = checkAllocation(device, a, supported); + const uint64_t bBytes = checkAllocation(device, b, supported); + const uint64_t cBytes = checkAllocation(device, c, supported); + std::puts("PASS: real allocation versus descriptor capacity / D3D11 unavailable"); + + uint64_t sentinel = 987; + check(!tryGetResourceAllocationBytes(nullptr, a, sentinel) && sentinel == 987, "null device preserves output"); + check(!tryGetResourceAllocationBytes(device, nullptr, sentinel) && sentinel == 987, "null resource preserves output"); + check(!tryGetResourceAllocationBytes(device, device, sentinel) && sentinel == 987, "unsupported resource preserves output"); + std::puts("PASS: null and unsupported output preservation"); + + auto fs = std::make_shared(); + fs->mount("/donut", std::filesystem::path(shaderPath)); + ShaderFactory shaders(device, fs, "/"); + Scene scene(device, shaders, fs, nullptr, nullptr, nullptr); + expectEmpty(scene.getGeometryMemoryStats()); // No graph yet. + auto graph = scene.CreateSceneGraph(); + expectEmpty(scene.getGeometryMemoryStats()); // A new graph has no root. + auto root = graph->SetRootNode(std::make_shared()); + expectEmpty(scene.getGeometryMemoryStats()); + + auto partial = std::make_shared(); + partial->buffers = nullptr; + auto partialNode = graph->AttachLeafNode(root, std::make_shared(partial)); + expectEmpty(scene.getGeometryMemoryStats()); + partial->buffers = std::make_shared(); + expectEmpty(scene.getGeometryMemoryStats()); + partial->buffers->vertexBuffer = a; + auto stats = scene.getGeometryMemoryStats(); + expectUsage(stats.vertexBuffers, 1024, aBytes, 1, supported ? 0 : 1); + expectUsage(stats.indexBuffers, 0, 0, 0, 0); + graph->Detach(partialNode); + expectEmpty(scene.getGeometryMemoryStats()); + std::puts("PASS: absent graph, empty and partially populated scenes"); + + auto mesh = std::make_shared(); + mesh->buffers = std::make_shared(); + mesh->buffers->vertexBuffer = a; + mesh->buffers->indexBuffer = b; + auto first = graph->AttachLeafNode(root, std::make_shared(mesh)); + auto repeated = graph->AttachLeafNode(root, std::make_shared(mesh)); + auto sharedGroup = std::make_shared(); + sharedGroup->buffers = mesh->buffers; + auto shared = graph->AttachLeafNode(root, std::make_shared(sharedGroup)); + auto aliases = std::make_shared(); + aliases->buffers = std::make_shared(); + aliases->buffers->vertexBuffer = a; // Same handle, different BufferGroup. + aliases->buffers->indexBuffer = a; // Same handle also counts in the index category. + auto crossCategory = graph->AttachLeafNode(root, std::make_shared(aliases)); + stats = scene.getGeometryMemoryStats(); + expectUsage(stats.vertexBuffers, 1024, aBytes, 1, supported ? 0 : 1); + expectUsage(stats.indexBuffers, 3072, aBytes + bBytes, 2, supported ? 0 : 2); + auto unknown = getSceneGeometryMemoryStats(nullptr, scene); + expectUsage(unknown.vertexBuffers, 1024, 0, 1, 1); + expectUsage(unknown.indexBuffers, 3072, 0, 2, 2); + std::puts("PASS: shared geometry deduplication, cross-category attribution, unknown versus zero"); + + aliases->buffers->vertexBuffer = c; + stats = scene.getGeometryMemoryStats(); + expectUsage(stats.vertexBuffers, 5120, aBytes + cBytes, 2, supported ? 0 : 2); + expectUsage(stats.indexBuffers, 3072, aBytes + bBytes, 2, supported ? 0 : 2); + mesh->buffers->indexBuffer = nullptr; + stats = scene.getGeometryMemoryStats(); + expectUsage(stats.vertexBuffers, 5120, aBytes + cBytes, 2, supported ? 0 : 2); + expectUsage(stats.indexBuffers, 1024, aBytes, 1, supported ? 0 : 1); + graph->Detach(crossCategory); + stats = scene.getGeometryMemoryStats(); + expectUsage(stats.vertexBuffers, 1024, aBytes, 1, supported ? 0 : 1); + expectUsage(stats.indexBuffers, 0, 0, 0, 0); + graph->Detach(first); + graph->Detach(repeated); + stats = scene.getGeometryMemoryStats(); // Shared mesh still retains the buffer. + expectUsage(stats.vertexBuffers, 1024, aBytes, 1, supported ? 0 : 1); + graph->Detach(shared); + expectEmpty(scene.getGeometryMemoryStats()); + expectEmpty(getSceneGeometryMemoryStats(nullptr, scene)); + std::puts("PASS: replacement and removal recompute totals without stale state"); +} + +static void runDevice(nvrhi::IDevice* device, Messages& messages, const char* shaderPath) +{ + check(device != nullptr, "NVRHI device"); + std::puts("Device path: raw"); + runScenarios(device, shaderPath); +#if TEST_VALIDATION + auto validation = nvrhi::validation::createValidationLayer(device); + std::puts("Device path: validation"); + runScenarios(validation, shaderPath); +#endif + device->waitForIdle(); + device->runGarbageCollection(); + check(messages.errors == 0, "no backend or validation errors"); +} + +int main(int argc, char** argv) +{ + // Keep failures headless too, including a regression into D3D11's legacy assert. + SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); +#ifdef _MSC_VER + _set_error_mode(_OUT_TO_STDERR); + _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE); + _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); +#endif + try + { + check(argc == 3, "usage: test_memory_stats d3d11|d3d12 shader-directory"); + Messages messages; + const std::string backend = argv[1]; + if (backend == "d3d11") + { + ComPtr native; + ComPtr context; + check(SUCCEEDED(D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_WARP, nullptr, 0, nullptr, 0, + D3D11_SDK_VERSION, &native, nullptr, &context)), "D3D11 WARP device"); + nvrhi::d3d11::DeviceDesc desc; + desc.context = context.Get(); + desc.messageCallback = &messages; + auto device = nvrhi::d3d11::createDevice(desc); + runDevice(device, messages, argv[2]); + } + else if (backend == "d3d12") + { + ComPtr factory; + ComPtr adapter; + check(SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory))), "DXGI factory"); + check(SUCCEEDED(factory->EnumWarpAdapter(IID_PPV_ARGS(&adapter))), "WARP adapter"); + ComPtr native; + check(SUCCEEDED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&native))), "D3D12 WARP device"); + ComPtr queue; + D3D12_COMMAND_QUEUE_DESC queueDesc = {}; + check(SUCCEEDED(native->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&queue))), "D3D12 queue"); + nvrhi::d3d12::DeviceDesc desc; + desc.pDevice = native.Get(); + desc.pGraphicsCommandQueue = queue.Get(); + desc.errorCB = &messages; + auto device = nvrhi::d3d12::createDevice(desc); + runDevice(device, messages, argv[2]); + } + else + throw std::runtime_error("unknown backend"); + std::puts("PASS: all memory statistics scenarios"); + return 0; + } + catch (const std::exception& e) + { + std::fprintf(stderr, "FAIL: %s\n", e.what()); + return 1; + } +} diff --git a/tests/test-engine.cmake b/tests/test-engine.cmake index 75525eb9..eb1b064e 100755 --- a/tests/test-engine.cmake +++ b/tests/test-engine.cmake @@ -38,3 +38,23 @@ foreach(test_src ${donut_engine_tests}) endforeach() +# Native headless coverage, using WARP so no physical GPU or desktop is required. +if (WIN32 AND DONUT_WITH_DX11 AND DONUT_WITH_DX12) + add_executable(test_memory_stats src/memory_stats.cpp) + target_link_libraries(test_memory_stats PRIVATE donut_engine dxgi) + if (NOT NVRHI_BUILD_SHARED) + target_link_libraries(test_memory_stats PRIVATE nvrhi_d3d11 nvrhi_d3d12) + endif() + target_compile_definitions(test_memory_stats PRIVATE TEST_VALIDATION=$) + add_dependencies(donut_all_tests test_memory_stats) + set_property(TARGET test_memory_stats PROPERTY FOLDER "Donut/donut_tests/donut_engine_tests") + + set(memory_stats_shader_dir "${donut_BINARY_DIR}/shaders/compiled_shaders") + if (DONUT_SHADERS_OUTPUT_DIR AND NOT DONUT_WITH_STATIC_SHADERS) + set(memory_stats_shader_dir "${DONUT_SHADERS_OUTPUT_DIR}") + endif() + add_test(NAME test_memory_stats_d3d11 COMMAND test_memory_stats d3d11 "${memory_stats_shader_dir}/dxbc") + add_test(NAME test_memory_stats_d3d12 COMMAND test_memory_stats d3d12 "${memory_stats_shader_dir}/dxil") + set_tests_properties(test_memory_stats_d3d11 test_memory_stats_d3d12 PROPERTIES TIMEOUT 60) +endif() + From bfbfe92cdd8bbc0b8450f7482db9b6460eda8c5c Mon Sep 17 00:00:00 2001 From: Greg Humphreys Date: Wed, 23 Sep 2026 10:37:18 -0400 Subject: [PATCH 4/8] no-mistakes(document): Clarify memory statistics and link authoritative backend documentation --- README.md | 4 ++++ doc/MemoryStats.md | 31 +++++++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 6e099615..6a16f41b 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/doc/MemoryStats.md b/doc/MemoryStats.md index 7ac9dd93..7d153c5c 100644 --- a/doc/MemoryStats.md +++ b/doc/MemoryStats.md @@ -1,18 +1,25 @@ # Scene geometry memory statistics `Scene::getGeometryMemoryStats()` returns current vertex/index buffer totals, deduplicating -shared buffer handles within each category. It keeps descriptor `capacityBytes` separate -from backing-buffer `allocationBytes`. Check `hasKnownAllocationBytes()` before displaying -an allocation total: unavailable resources increment `unknownAllocationCount`, not a known zero. +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 `queryResourceMemoryRequirements`. -D3D11 reports unavailable safely; D3D12 and Vulkan report requirements for exposed backing -buffers, including acceleration structures and opacity micromaps. These numbers are not -residency, driver overhead, or unique shared-heap allocation. Pool reservations and aliases -between categories need attribution by their owner in the consuming application. +`tryGetResourceAllocationBytes` uses NVRHI's optional `queryResourceMemoryRequirements` +and leaves `outBytes` unchanged on failure, including a null device or resource. +See [NVRHI's optional memory queries](../nvrhi/doc/memory-queries.md) for resource/device +requirements, 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. -TLAS prebuild queries are exposed directly by -`IDevice::queryTopLevelAccelStructPrebuildInfo` in NVRHI, rather than Donut. Scratch build -requests are not resident scratch-pool capacity. Unsupported backends return `false` and -leave the output unchanged; see NVRHI's `doc/memory-queries.md` for backend support and tests. +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). From 00ee41b13d1bb6a89a852fb0a4cc799b95a9b04b Mon Sep 17 00:00:00 2001 From: Greg Humphreys Date: Wed, 23 Sep 2026 13:45:42 -0400 Subject: [PATCH 5/8] Use the optional NVRHI resource memory query --- doc/MemoryStats.md | 10 ++++++---- nvrhi | 2 +- src/engine/MemoryStats.cpp | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/doc/MemoryStats.md b/doc/MemoryStats.md index 7d153c5c..f69af023 100644 --- a/doc/MemoryStats.md +++ b/doc/MemoryStats.md @@ -13,10 +13,12 @@ The free function `getSceneGeometryMemoryStats(device, scene)` provides the same 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 `queryResourceMemoryRequirements` -and leaves `outBytes` unchanged on failure, including a null device or resource. -See [NVRHI's optional memory queries](../nvrhi/doc/memory-queries.md) for resource/device -requirements, backend support and exceptions, allocation interpretation, and the native +`tryGetResourceAllocationBytes` uses NVRHI's optional `IResource::queryMemoryRequirements` +and leaves `outBytes` unchanged on failure, including a null device or resource. The helper's +existing device parameter and null-device behavior are retained for source compatibility; +the resource itself dispatches the query to its 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. diff --git a/nvrhi b/nvrhi index 0ced597b..37eb75a5 160000 --- a/nvrhi +++ b/nvrhi @@ -1 +1 @@ -Subproject commit 0ced597b84bdaa7e7d1fc57bc107777da6e37961 +Subproject commit 37eb75a5ec42e4fb78025180b44af7471ee0969b diff --git a/src/engine/MemoryStats.cpp b/src/engine/MemoryStats.cpp index c78db32b..35e2e8cb 100644 --- a/src/engine/MemoryStats.cpp +++ b/src/engine/MemoryStats.cpp @@ -50,7 +50,7 @@ namespace bool tryGetResourceAllocationBytes(nvrhi::IDevice* device, nvrhi::IResource* resource, uint64_t& outBytes) { nvrhi::MemoryRequirements requirements; - if (device == nullptr || !device->queryResourceMemoryRequirements(resource, requirements)) + if (device == nullptr || resource == nullptr || !resource->queryMemoryRequirements(requirements)) return false; outBytes = requirements.size; From 78fe88c381589e57dd1ed0b602e6be2abfdf3196 Mon Sep 17 00:00:00 2001 From: Greg Humphreys Date: Wed, 23 Sep 2026 16:44:37 -0400 Subject: [PATCH 6/8] Guard unavailable memory statistics with updated NVRHI --- doc/MemoryStats.md | 5 +++-- include/donut/engine/MemoryStats.h | 3 ++- nvrhi | 2 +- src/engine/MemoryStats.cpp | 9 ++++++++- tests/src/memory_stats.cpp | 6 +++++- 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/doc/MemoryStats.md b/doc/MemoryStats.md index f69af023..b44321cc 100644 --- a/doc/MemoryStats.md +++ b/doc/MemoryStats.md @@ -15,8 +15,9 @@ 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 -existing device parameter and null-device behavior are retained for source compatibility; -the resource itself dispatches the query to its backend. +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 diff --git a/include/donut/engine/MemoryStats.h b/include/donut/engine/MemoryStats.h index 76e0a653..c4f9a817 100644 --- a/include/donut/engine/MemoryStats.h +++ b/include/donut/engine/MemoryStats.h @@ -47,7 +47,8 @@ namespace donut::engine }; // Returns backing-buffer memory requirements, not residency or unique heap allocation bytes. - // Unsupported queries leave outBytes unchanged; descriptor capacity remains separately available. + // 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 diff --git a/nvrhi b/nvrhi index 37eb75a5..10fbb572 160000 --- a/nvrhi +++ b/nvrhi @@ -1 +1 @@ -Subproject commit 37eb75a5ec42e4fb78025180b44af7471ee0969b +Subproject commit 10fbb572f3071f29ca1754dd6fbc72e9993fb23d diff --git a/src/engine/MemoryStats.cpp b/src/engine/MemoryStats.cpp index 35e2e8cb..7edc2bc5 100644 --- a/src/engine/MemoryStats.cpp +++ b/src/engine/MemoryStats.cpp @@ -49,8 +49,15 @@ namespace 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(resource)) + return false; + nvrhi::MemoryRequirements requirements; - if (device == nullptr || resource == nullptr || !resource->queryMemoryRequirements(requirements)) + if (!resource->queryMemoryRequirements(requirements)) return false; outBytes = requirements.size; diff --git a/tests/src/memory_stats.cpp b/tests/src/memory_stats.cpp index eba16d9e..8b678609 100644 --- a/tests/src/memory_stats.cpp +++ b/tests/src/memory_stats.cpp @@ -92,7 +92,11 @@ static void runScenarios(nvrhi::IDevice* device, const char* shaderPath) check(!tryGetResourceAllocationBytes(nullptr, a, sentinel) && sentinel == 987, "null device preserves output"); check(!tryGetResourceAllocationBytes(device, nullptr, sentinel) && sentinel == 987, "null resource preserves output"); check(!tryGetResourceAllocationBytes(device, device, sentinel) && sentinel == 987, "unsupported resource preserves output"); - std::puts("PASS: null and unsupported output preservation"); + auto texture = device->createTexture(nvrhi::TextureDesc().setWidth(1).setHeight(1).setFormat(nvrhi::Format::RGBA8_UNORM)); + check(texture != nullptr, "create unsupported texture"); + check(!tryGetResourceAllocationBytes(device, texture, sentinel) && sentinel == 987, + "unsupported texture preserves output without assertion"); + std::puts("PASS: null, non-memory resource and texture output preservation"); auto fs = std::make_shared(); fs->mount("/donut", std::filesystem::path(shaderPath)); From 972b157a1d62cc59eaf2cdc0b3988d89ce9aaeea Mon Sep 17 00:00:00 2001 From: Greg Humphreys Date: Wed, 23 Sep 2026 17:07:31 -0400 Subject: [PATCH 7/8] Use merged upstream NVRHI and keep assertion test failures noninteractive --- .gitmodules | 2 +- nvrhi | 2 +- tests/src/memory_stats.cpp | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index 5ecc1e09..0c9b0eee 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "nvrhi"] path = nvrhi - url = https://github.com/nvhumper/NVRHI.git + url = https://github.com/NVIDIA-RTX/NVRHI.git [submodule "thirdparty/imgui"] path = thirdparty/imgui url = https://github.com/ocornut/imgui diff --git a/nvrhi b/nvrhi index 10fbb572..d0c8e30d 160000 --- a/nvrhi +++ b/nvrhi @@ -1 +1 @@ -Subproject commit 10fbb572f3071f29ca1754dd6fbc72e9993fb23d +Subproject commit d0c8e30d5f8d58c3b838b06aa1d8d0a912bea076 diff --git a/tests/src/memory_stats.cpp b/tests/src/memory_stats.cpp index 8b678609..0dcbd881 100644 --- a/tests/src/memory_stats.cpp +++ b/tests/src/memory_stats.cpp @@ -187,6 +187,9 @@ int main(int argc, char** argv) SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); #ifdef _MSC_VER _set_error_mode(_OUT_TO_STDERR); + _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); + _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE); + _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR); _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE); _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); #endif From 0864e7027a0a8aa4405f49e0fd41a5c5d73d7a28 Mon Sep 17 00:00:00 2001 From: Greg Humphreys Date: Wed, 23 Sep 2026 17:17:47 -0400 Subject: [PATCH 8/8] no-mistakes(review): Retain scene root in memory statistics test fixture --- tests/src/memory_stats.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/src/memory_stats.cpp b/tests/src/memory_stats.cpp index 0dcbd881..42765e7a 100644 --- a/tests/src/memory_stats.cpp +++ b/tests/src/memory_stats.cpp @@ -105,7 +105,8 @@ static void runScenarios(nvrhi::IDevice* device, const char* shaderPath) expectEmpty(scene.getGeometryMemoryStats()); // No graph yet. auto graph = scene.CreateSceneGraph(); expectEmpty(scene.getGeometryMemoryStats()); // A new graph has no root. - auto root = graph->SetRootNode(std::make_shared()); + auto root = std::make_shared(); + graph->SetRootNode(root); expectEmpty(scene.getGeometryMemoryStats()); auto partial = std::make_shared();