Skip to content

Commit 2e21b39

Browse files
committed
Merge branch 'calhsu/cmdlist-memory-usage' into 'main'
Add ICommandList::getInternalMemoryUsage See merge request nvrhi/NVRHI!97
2 parents 805a37e + 0c77748 commit 2e21b39

10 files changed

Lines changed: 53 additions & 3 deletions

File tree

‎include/nvrhi/nvrhi.h‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace nvrhi
6565
{
6666
// Version of the public API provided by NVRHI.
6767
// Increment this when any changes to the API are made.
68-
static constexpr uint32_t c_HeaderVersion = 27;
68+
static constexpr uint32_t c_HeaderVersion = 28;
6969

7070
// Verifies that the version of the implementation matches the version of the header.
7171
// Returns true if they match. Use this when initializing apps using NVRHI as a shared library.
@@ -3318,6 +3318,16 @@ namespace nvrhi
33183318

33193319
typedef RefCountPtr<ICommandListLifetimeTracker> CommandListLifetimeTrackerHandle;
33203320

3321+
// Memory held by a command list's internal chunk pools, see ICommandList::getInternalMemoryUsage.
3322+
struct CommandListMemoryUsage
3323+
{
3324+
// AS build scratch chunks, in device-local memory.
3325+
uint64_t scratchBytes = 0;
3326+
3327+
// Upload chunks, in CPU-visible memory.
3328+
uint64_t uploadBytes = 0;
3329+
};
3330+
33213331
//////////////////////////////////////////////////////////////////////////
33223332
// ICommandList
33233333
//////////////////////////////////////////////////////////////////////////
@@ -3813,6 +3823,12 @@ namespace nvrhi
38133823

38143824
// Returns the CommandListParameters structure that was used to create the command list.
38153825
virtual const CommandListParameters& getDesc() = 0;
3826+
3827+
// Returns the total size of the chunks created by the upload and scratch managers. The chunks are only
3828+
// freed when the command list is destroyed, so this never decreases. Not safe to call while another thread
3829+
// is recording into this command list.
3830+
// - DX11: Always returns zeros.
3831+
[[nodiscard]] virtual CommandListMemoryUsage getInternalMemoryUsage() const = 0;
38163832
};
38173833

38183834
typedef RefCountPtr<ICommandList> CommandListHandle;

‎src/d3d11/d3d11-backend.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ namespace nvrhi::d3d11
381381

382382
IDevice* getDevice() override { return m_Device; }
383383
const CommandListParameters& getDesc() override { return m_Desc; }
384+
[[nodiscard]] CommandListMemoryUsage getInternalMemoryUsage() const override { return CommandListMemoryUsage(); }
384385

385386
private:
386387
const Context& m_Context;

‎src/d3d12/d3d12-backend.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,12 +782,15 @@ namespace nvrhi::d3d12
782782

783783
void submitChunks(uint64_t currentVersion, uint64_t submittedVersion);
784784

785+
uint64_t getChunkBytes() const { return m_ChunkBytes; }
786+
785787
private:
786788
const Context& m_Context;
787789
Queue* m_Queue;
788790
size_t m_DefaultChunkSize = 0;
789791
uint64_t m_MemoryLimit = 0;
790-
uint64_t m_AllocatedMemory = 0;
792+
uint64_t m_AllocatedMemory = 0; // TODO: This value currently has no effect.
793+
uint64_t m_ChunkBytes = 0;
791794
bool m_IsScratchBuffer = false;
792795

793796
std::list<std::shared_ptr<BufferChunk>> m_ChunkPool;
@@ -1124,6 +1127,7 @@ namespace nvrhi::d3d12
11241127

11251128
nvrhi::IDevice* getDevice() override;
11261129
const CommandListParameters& getDesc() override { return m_Desc; }
1130+
[[nodiscard]] CommandListMemoryUsage getInternalMemoryUsage() const override;
11271131

11281132
// D3D12 specific methods
11291133

‎src/d3d12/d3d12-commandlist.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ namespace nvrhi::d3d12
175175
return m_Device;
176176
}
177177

178+
CommandListMemoryUsage CommandList::getInternalMemoryUsage() const
179+
{
180+
CommandListMemoryUsage usage;
181+
usage.scratchBytes = m_DxrScratchManager.getChunkBytes();
182+
usage.uploadBytes = m_UploadManager.getChunkBytes();
183+
return usage;
184+
}
185+
178186
void CommandList::beginMarker(const char* name)
179187
{
180188
PIXBeginEvent(m_ActiveCommandList->commandList, 0, name);

‎src/d3d12/d3d12-upload.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ namespace nvrhi::d3d12
224224
else
225225
{
226226
m_CurrentChunk = createChunk(sizeToAllocate);
227+
if (m_CurrentChunk)
228+
m_ChunkBytes += m_CurrentChunk->bufferSize;
227229
}
228230
}
229231

‎src/validation/validation-backend.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ namespace nvrhi::validation
281281

282282
IDevice* getDevice() override;
283283
const CommandListParameters& getDesc() override;
284+
[[nodiscard]] CommandListMemoryUsage getInternalMemoryUsage() const override;
284285
};
285286

286287
class DeviceWrapper : public RefCounter<IDevice>

‎src/validation/validation-commandlist.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,11 @@ namespace nvrhi::validation
11801180
return m_Device;
11811181
}
11821182

1183+
CommandListMemoryUsage CommandListWrapper::getInternalMemoryUsage() const
1184+
{
1185+
return m_CommandList->getInternalMemoryUsage();
1186+
}
1187+
11831188
const CommandListParameters& CommandListWrapper::getDesc()
11841189
{
11851190
return m_CommandList->getDesc();

‎src/vulkan/vulkan-backend.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,11 +1024,14 @@ namespace nvrhi::vulkan
10241024
bool suballocateBuffer(uint64_t size, Buffer** pBuffer, uint64_t* pOffset, void** pCpuVA, uint64_t currentVersion, uint32_t alignment = 256);
10251025
void submitChunks(uint64_t currentVersion, uint64_t submittedVersion);
10261026

1027+
uint64_t getChunkBytes() const { return m_ChunkBytes; }
1028+
10271029
private:
10281030
Device* m_Device;
10291031
uint64_t m_DefaultChunkSize = 0;
10301032
uint64_t m_MemoryLimit = 0;
1031-
uint64_t m_AllocatedMemory = 0;
1033+
uint64_t m_AllocatedMemory = 0; // TODO: This value currently has no effect.
1034+
uint64_t m_ChunkBytes = 0;
10321035
bool m_IsScratchBuffer = false;
10331036

10341037
std::list<std::shared_ptr<BufferChunk>> m_ChunkPool;
@@ -1330,6 +1333,7 @@ namespace nvrhi::vulkan
13301333

13311334
IDevice* getDevice() override { return m_Device; }
13321335
const CommandListParameters& getDesc() override { return m_CommandListParameters; }
1336+
[[nodiscard]] CommandListMemoryUsage getInternalMemoryUsage() const override;
13331337

13341338
TrackedCommandBufferPtr getCurrentCmdBuf() const { return m_CurrentCmdBuf; }
13351339

‎src/vulkan/vulkan-commandlist.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ namespace nvrhi::vulkan
5858
}
5959
}
6060

61+
CommandListMemoryUsage CommandList::getInternalMemoryUsage() const
62+
{
63+
CommandListMemoryUsage usage;
64+
usage.scratchBytes = m_ScratchManager->getChunkBytes();
65+
usage.uploadBytes = m_UploadManager->getChunkBytes();
66+
return usage;
67+
}
68+
6169
void CommandList::open()
6270
{
6371
m_CurrentCmdBuf = m_Device->getQueue(m_CommandListParameters.queueType)->getOrCreateCommandBuffer();

‎src/vulkan/vulkan-upload.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ namespace nvrhi::vulkan
121121
return false;
122122

123123
m_CurrentChunk = CreateChunk(sizeToAllocate);
124+
m_ChunkBytes += m_CurrentChunk->bufferSize;
124125
}
125126

126127
m_CurrentChunk->version = currentVersion;

0 commit comments

Comments
 (0)