From 444db417a55322a9db363860042657758d169d7e Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 27 Oct 2025 02:32:23 -0500 Subject: [PATCH] more gpu stats --- src/gpu/gpu_core.h | 22 ++++++++------ src/gpu/gpu_dx12/gpu_dx12.c | 43 ++++++++++++++------------- src/gpu/gpu_dx12/gpu_dx12.h | 8 +++-- src/pp/pp.c | 59 +++++++++++++++++++------------------ 4 files changed, 71 insertions(+), 61 deletions(-) diff --git a/src/gpu/gpu_core.h b/src/gpu/gpu_core.h index 18e18a5c..dc82b651 100644 --- a/src/gpu/gpu_core.h +++ b/src/gpu/gpu_core.h @@ -349,14 +349,19 @@ Struct(GPU_Scissor) }; //////////////////////////////////////////////////////////// -//~ Memory info types +//~ Statistic types -Struct(GPU_MemoryStats) +Struct(GPU_Stats) { - u64 budget; - u64 committed; - u64 reserved; - u64 available_to_reserve; + /* Memory usage */ + u64 local_committed; + u64 local_budget; + u64 non_local_committed; + u64 non_local_budget; + + /* Resources */ + u64 driver_resources_allocated; + u64 driver_descriptors_allocated; }; //////////////////////////////////////////////////////////// @@ -468,10 +473,9 @@ void GPU_Unmap(GPU_Mapped mapped); void GPU_CopyBytesToFootprint(void *dst, void *src, GPU_Resource *footprint_reference); //////////////////////////////////////////////////////////// -//~ @hookdecl Memory info operations +//~ @hookdecl Statistics -GPU_MemoryStats GPU_QueryDedicatedMemoryStats(void); -GPU_MemoryStats GPU_QuerySharedMemoryStats(void); +GPU_Stats GPU_QueryStats(void); //////////////////////////////////////////////////////////// //~ @hookdecl Swapchain available_to_reserve diff --git a/src/gpu/gpu_dx12/gpu_dx12.c b/src/gpu/gpu_dx12/gpu_dx12.c index dec530d8..1f1de412 100644 --- a/src/gpu/gpu_dx12/gpu_dx12.c +++ b/src/gpu/gpu_dx12/gpu_dx12.c @@ -565,6 +565,7 @@ GPU_D12_Descriptor *GPU_D12_AcquireDescriptor(GPU_D12_DescriptorHeap *heap) d = PushStructNoZero(heap->arena, GPU_D12_Descriptor); index = heap->allocated_count++; handle.ptr = heap->start_handle.ptr + (index * heap->descriptor_size); + Atomic64FetchAdd(&GPU_D12_shared_state.driver_descriptors_allocated, 1); } Unlock(&lock); } @@ -886,6 +887,7 @@ GPU_Resource *GPU_AcquireResource(GPU_ResourceDesc desc) d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS * !!(desc.flags & GPU_ResourceFlag_Writable); r->state = desc.buffer.heap_kind == GPU_HeapKind_Upload ? D3D12_RESOURCE_STATE_GENERIC_READ : D3D12_RESOURCE_STATE_COPY_DEST; HRESULT hr = ID3D12Device_CreateCommittedResource(g->device, &heap_props, heap_flags, &d3d_desc, r->state, 0, &IID_ID3D12Resource, (void **)&r->d3d_resource); + Atomic64FetchAdd(&g->driver_resources_allocated, 1); if (FAILED(hr)) { /* TODO: Don't panic */ @@ -924,6 +926,7 @@ GPU_Resource *GPU_AcquireResource(GPU_ResourceDesc desc) clear_value.Color[3] = desc.clear_color.w; D3D12_CLEAR_VALUE *clear_value_ptr = d3d_desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET ? &clear_value : 0; HRESULT hr = ID3D12Device_CreateCommittedResource(g->device, &heap_props, heap_flags, &d3d_desc, r->state, clear_value_ptr, &IID_ID3D12Resource, (void **)&r->d3d_resource); + Atomic64FetchAdd(&g->driver_resources_allocated, 1); if (FAILED(hr)) { /* TODO: Don't panic */ @@ -1092,13 +1095,15 @@ void GPU_ReleaseResource(GPU_Resource *gpu_resource, GPU_ReleaseFlag flags) { switch (r->desc.kind) { + default: break; case GPU_ResourceKind_Buffer: case GPU_ResourceKind_Texture1D: case GPU_ResourceKind_Texture2D: case GPU_ResourceKind_Texture3D: { ID3D12Resource_Release(r->d3d_resource); - } + Atomic64FetchAdd(&g->driver_resources_allocated, -1); + } break; } Lock lock = LockE(&g->free_resources_mutex); r->next_free = g->first_free_resource; @@ -1808,37 +1813,33 @@ void GPU_CopyBytesToFootprint(void *dst, void *src, GPU_Resource *footprint_refe } //////////////////////////////////////////////////////////// -//~ @hookdef Memory info hooks +//~ @hookdef Statistics -GPU_MemoryStats GPU_QueryDedicatedMemoryStats(void) +GPU_Stats GPU_QueryStats(void) { GPU_D12_SharedState *g = &GPU_D12_shared_state; - GPU_MemoryStats result = ZI; + GPU_Stats result = ZI; { DXGI_QUERY_VIDEO_MEMORY_INFO info = ZI; IDXGIAdapter3_QueryVideoMemoryInfo(g->adapter, 0, DXGI_MEMORY_SEGMENT_GROUP_LOCAL, &info); - result.budget = info.Budget; - result.committed = info.CurrentUsage; - result.reserved = info.CurrentReservation; - result.available_to_reserve = info.AvailableForReservation; - + result.local_committed = info.CurrentUsage; + result.local_budget = info.Budget; } - return result; -} - -GPU_MemoryStats GPU_QuerySharedMemoryStats(void) -{ - GPU_D12_SharedState *g = &GPU_D12_shared_state; - GPU_MemoryStats result = ZI; { DXGI_QUERY_VIDEO_MEMORY_INFO info = ZI; IDXGIAdapter3_QueryVideoMemoryInfo(g->adapter, 0, DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL, &info); - result.budget = info.Budget; - result.committed = info.CurrentUsage; - result.reserved = info.CurrentReservation; - result.available_to_reserve = info.AvailableForReservation; - + result.non_local_budget = info.Budget; + result.non_local_committed = info.CurrentUsage; } + result.driver_resources_allocated = Atomic64Fetch(&g->driver_resources_allocated); + result.driver_descriptors_allocated = Atomic64Fetch(&g->driver_descriptors_allocated); + return result; +} + +GPU_Stats GPU_QuerySharedMemoryStats(void) +{ + GPU_D12_SharedState *g = &GPU_D12_shared_state; + GPU_Stats result = ZI; return result; } diff --git a/src/gpu/gpu_dx12/gpu_dx12.h b/src/gpu/gpu_dx12/gpu_dx12.h index e9ce2e23..94da7941 100644 --- a/src/gpu/gpu_dx12/gpu_dx12.h +++ b/src/gpu/gpu_dx12/gpu_dx12.h @@ -12,8 +12,8 @@ //~ Tweakable defines #define GPU_D12_TearingIsAllowed 1 -#define GPU_D12_FrameLatency 2 -#define GPU_D12_SwapchainBufferCount 2 +#define GPU_D12_FrameLatency 1 +#define GPU_D12_SwapchainBufferCount 3 #define GPU_D12_SwapchainFlags (((GPU_D12_TearingIsAllowed != 0) * DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING) \ | ((GPU_D12_FrameLatency != 0) * DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT)) @@ -288,6 +288,10 @@ Struct(GPU_D12_SharedState) Atomic64Padded resource_barrier_gen; + /* Stats */ + Atomic64 driver_resources_allocated; + Atomic64 driver_descriptors_allocated; + /* Queues */ GPU_D12_Queue *queues[GPU_NumQueues]; diff --git a/src/pp/pp.c b/src/pp/pp.c index 9abaae89..d9242ed8 100644 --- a/src/pp/pp.c +++ b/src/pp/pp.c @@ -2055,14 +2055,11 @@ void UpdateUser(void) } #endif - //- Query vram - - GPU_MemoryStats vram_dedi = GPU_QueryDedicatedMemoryStats(); - GPU_MemoryStats vram_shared = GPU_QuerySharedMemoryStats(); - ////////////////////////////// //- Debug draw + GPU_Stats gpu_stats = GPU_QueryStats(); + /* Draw debug info */ if (g->debug_draw) { @@ -2079,60 +2076,64 @@ void UpdateUser(void) UI_Push(BorderColor, 0); UI_Push(TextPadding, 0); - UI_BuildLabelF("blended world entities: %F/%F", FmtUint(g->ss_blended->num_ents_allocated), FmtUint(g->ss_blended->num_ents_reserved)); - UI_BuildLabelF("blended world tick: %F", FmtUint(g->ss_blended->tick)); + UI_BuildLabelF("blended world entities: %F/%F", FmtUint(g->ss_blended->num_ents_allocated), FmtUint(g->ss_blended->num_ents_reserved)); + UI_BuildLabelF("blended world tick: %F", FmtUint(g->ss_blended->tick)); - UI_BuildLabelF("blended world time: %F", FmtFloat(SecondsFromNs(g->ss_blended->sim_time_ns))); + UI_BuildLabelF("blended world time: %F", FmtFloat(SecondsFromNs(g->ss_blended->sim_time_ns))); UI_BuildSpacer(UI_PixelSize(20, 0)); - UI_BuildLabelF("average local sim publish dt: %F", FmtFloat(SecondsFromNs(g->average_local_to_user_snapshot_publish_dt_ns))); - UI_BuildLabelF("local sim last known tick: %F", FmtUint(g->local_sim_last_known_tick)); - UI_BuildLabelF("local sim last known time: %F", FmtFloat(SecondsFromNs(g->local_sim_last_known_time_ns))); - UI_BuildLabelF("local sim predicted time: %F", FmtFloat(SecondsFromNs(g->local_sim_predicted_time_ns))); - UI_BuildLabelF("render time target: %F", FmtFloat(SecondsFromNs(g->render_time_target_ns))); + UI_BuildLabelF("average local sim publish dt: %F", FmtFloat(SecondsFromNs(g->average_local_to_user_snapshot_publish_dt_ns))); + UI_BuildLabelF("local sim last known tick: %F", FmtUint(g->local_sim_last_known_tick)); + UI_BuildLabelF("local sim last known time: %F", FmtFloat(SecondsFromNs(g->local_sim_last_known_time_ns))); + UI_BuildLabelF("local sim predicted time: %F", FmtFloat(SecondsFromNs(g->local_sim_predicted_time_ns))); + UI_BuildLabelF("render time target: %F", FmtFloat(SecondsFromNs(g->render_time_target_ns))); - UI_BuildLabelF("render time: %F", FmtFloat(SecondsFromNs(g->render_time_ns))); + UI_BuildLabelF("render time: %F", FmtFloat(SecondsFromNs(g->render_time_ns))); - UI_BuildLabelF("local player: [%F]", FmtUid(local_player->id.uid)); + UI_BuildLabelF("local player: [%F]", FmtUid(local_player->id.uid)); UI_BuildSpacer(UI_PixelSize(20, 0)); Vec2 world_cursor = g->world_cursor; - UI_BuildLabelF("cursor world: %F, %F", FmtFloat(world_cursor.x), FmtFloat(world_cursor.y)); + UI_BuildLabelF("cursor world: %F, %F", FmtFloat(world_cursor.x), FmtFloat(world_cursor.y)); Vec2I32 world_tile_cursor = WorldTileIndexFromPos(world_cursor); - UI_BuildLabelF("cursor world tile: %F, %F", FmtSint(world_tile_cursor.x), FmtSint(world_tile_cursor.y)); + UI_BuildLabelF("cursor world tile: %F, %F", FmtSint(world_tile_cursor.x), FmtSint(world_tile_cursor.y)); Vec2I32 local_tile_cursor = LocalTileIndexFromWorldTileIndex(world_tile_cursor); - UI_BuildLabelF("cursor local tile: %F, %F", FmtSint(local_tile_cursor.x), FmtSint(local_tile_cursor.y)); + UI_BuildLabelF("cursor local tile: %F, %F", FmtSint(local_tile_cursor.x), FmtSint(local_tile_cursor.y)); Vec2I32 tile_chunk_cursor = TileChunkIndexFromWorldTileIndex(world_tile_cursor); - UI_BuildLabelF("cursor tile chunk: %F, %F", FmtSint(tile_chunk_cursor.x), FmtSint(tile_chunk_cursor.y)); + UI_BuildLabelF("cursor tile chunk: %F, %F", FmtSint(tile_chunk_cursor.x), FmtSint(tile_chunk_cursor.y)); UI_BuildSpacer(UI_PixelSize(20, 0)); - UI_BuildLabelF("Network read: %F mbit/s", FmtFloat((f64)g->net_bytes_read.last_second * 8 / 1000 / 1000)); + UI_BuildLabelF("Network read: %F mbit/s", FmtFloat((f64)g->net_bytes_read.last_second * 8 / 1000 / 1000)); - UI_BuildLabelF("Network write: %F mbit/s", FmtFloat((f64)g->net_bytes_sent.last_second * 8 / 1000 / 1000)); + UI_BuildLabelF("Network write: %F mbit/s", FmtFloat((f64)g->net_bytes_sent.last_second * 8 / 1000 / 1000)); - UI_BuildLabelF("Ping (real): %F ms", FmtFloat(SecondsFromNs(local_player->player_last_rtt_ns) * 1000)); + UI_BuildLabelF("Ping (real): %F ms", FmtFloat(SecondsFromNs(local_player->player_last_rtt_ns) * 1000)); - UI_BuildLabelF("Ping (average): %F ms", FmtFloat(local_player->player_average_rtt_seconds * 1000)); + UI_BuildLabelF("Ping (average): %F ms", FmtFloat(local_player->player_average_rtt_seconds * 1000)); UI_BuildSpacer(UI_PixelSize(20, 0)); - UI_BuildLabelF("Memory committed: %F MiB", FmtFloat((f64)GetGstat(GSTAT_MEMORY_COMMITTED) / 1024 / 1024)); + UI_BuildLabelF("Memory committed: %F MiB", FmtFloat((f64)GetGstat(GSTAT_MEMORY_COMMITTED) / 1024 / 1024)); - UI_BuildLabelF("Virtual memory reserved: %F TiB", FmtFloat((f64)GetGstat(GSTAT_MEMORY_RESERVED) / 1024 / 1024 / 1024 / 1024)); + UI_BuildLabelF("Virtual memory reserved: %F TiB", FmtFloat((f64)GetGstat(GSTAT_MEMORY_RESERVED) / 1024 / 1024 / 1024 / 1024)); - UI_BuildLabelF("Arenas allocated: %F", FmtUint(GetGstat(GSTAT_NUM_ARENAS))); + UI_BuildLabelF("Arenas allocated: %F", FmtUint(GetGstat(GSTAT_NUM_ARENAS))); UI_BuildSpacer(UI_PixelSize(20, 0)); - UI_BuildLabelF("Video memory (GPU): %F MiB", FmtFloat((f64)vram_dedi.committed / 1024 / 1024)); - UI_BuildLabelF("Video memory (shared): %F MiB", FmtFloat((f64)vram_shared.committed / 1024 / 1024)); + UI_BuildLabelF("GPU dedicated memory usage: %F MiB", FmtFloat((f64)gpu_stats.local_committed / 1024 / 1024)); + UI_BuildLabelF("GPU shared memory usage: %F MiB", FmtFloat((f64)gpu_stats.non_local_committed / 1024 / 1024)); + UI_BuildSpacer(UI_PixelSize(20, 0)); + + UI_BuildLabelF("GPU resources: %F", FmtUint(gpu_stats.driver_resources_allocated)); + UI_BuildLabelF("GPU descriptors: %F", FmtUint(gpu_stats.driver_descriptors_allocated)); //UI_BuildLabelF(\n")); //UI_BuildLabelF(\n")); #if RtcIsEnabled UI_BuildSpacer(UI_PixelSize(20, 0)); - UI_BuildLabelF("Debug steps: %F", FmtUint(GetGstat(GSTAT_DEBUG_STEPS))); + UI_BuildLabelF("Debug steps: %F", FmtUint(GetGstat(GSTAT_DEBUG_STEPS))); //UI_BuildLabelF(\n")); #endif }