Skip to content

Commit 6813c62

Browse files
committed
improvements
1 parent e624fe5 commit 6813c62

9 files changed

Lines changed: 139 additions & 6 deletions

File tree

sources/HAL/API/D3D12/HAL.D3D12.Device.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module HAL:Device;
44
import :Debug;
55
import :Utils;
6+
import :HeapAllocators;
67

78
import d3d12;
89
import Core;
@@ -213,6 +214,18 @@ namespace HAL
213214
return usedVRAM;
214215
}
215216

217+
size_t Device::get_upload_heap()
218+
{
219+
auto THIS = static_cast<HAL::Device*>(this);
220+
return THIS->get_heap_factory().get_upload_bytes() / 1024 / 1024;
221+
}
222+
223+
size_t Device::get_readback_heap()
224+
{
225+
auto THIS = static_cast<HAL::Device*>(this);
226+
return THIS->get_heap_factory().get_readback_bytes() / 1024 / 1024;
227+
}
228+
216229
void Device::init(DeviceDesc& desc)
217230
{
218231
auto THIS = static_cast<HAL::Device*>(this);

sources/HAL/API/D3D12/HAL.D3D12.Device.ixx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export namespace HAL {
3838
uint Subresources(const ResourceDesc& desc) const;
3939

4040
size_t get_vram();
41+
size_t get_upload_heap();
42+
size_t get_readback_heap();
4143

4244
RaytracingPrebuildInfo calculateBuffers(const RaytracingBuildDescBottomInputs& desc);
4345
RaytracingPrebuildInfo calculateBuffers(const RaytracingBuildDescTopInputs& desc);

sources/HAL/API/Vulkan/HAL.Vulkan.Device.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import :Debug;
88
import :Utils;
99
import :Impl; // get_vk_instance()
1010
import :Sampler;
11+
import :HeapAllocators;
1112

1213
import stl.core;
1314
import Core;
@@ -547,6 +548,18 @@ namespace HAL
547548
return total / 1024 / 1024;
548549
}
549550

551+
size_t Device::get_upload_heap()
552+
{
553+
auto THIS = static_cast<HAL::Device*>(this);
554+
return THIS->get_heap_factory().get_upload_bytes() / 1024 / 1024;
555+
}
556+
557+
size_t Device::get_readback_heap()
558+
{
559+
auto THIS = static_cast<HAL::Device*>(this);
560+
return THIS->get_heap_factory().get_readback_bytes() / 1024 / 1024;
561+
}
562+
550563
ResourceAllocationInfo Device::get_alloc_info(const ResourceDesc& desc)
551564
{
552565
auto it = alloc_info.find(desc);

sources/HAL/API/Vulkan/HAL.Vulkan.Device.ixx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ export namespace HAL
115115
ResourceAllocationInfo get_alloc_info(const ResourceDesc& desc);
116116
uint Subresources(const ResourceDesc& desc) const;
117117
size_t get_vram();
118+
size_t get_upload_heap();
119+
size_t get_readback_heap();
118120

119121
RaytracingPrebuildInfo calculateBuffers(const RaytracingBuildDescBottomInputs& desc);
120122
RaytracingPrebuildInfo calculateBuffers(const RaytracingBuildDescTopInputs& desc);

sources/HAL/HAL.HeapAllocators.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@ namespace HAL
1111
return std::make_shared<HAL::Heap>(device, desc);
1212
}
1313

14+
void HeapFactory::_add(HeapIndex index, size_t n) noexcept
15+
{
16+
if (index.type == HeapType::UPLOAD) _upload_bytes += n;
17+
else if (index.type == HeapType::READBACK) _readback_bytes += n;
18+
}
19+
20+
void HeapFactory::_sub(HeapIndex index, size_t n) noexcept
21+
{
22+
if (index.type == HeapType::UPLOAD) _upload_bytes -= n;
23+
else if (index.type == HeapType::READBACK) _readback_bytes -= n;
24+
}
25+
26+
HeapFactory::page_type HeapFactory::AllocateHeap(HeapIndex index, size_t size, Allocators::HeapAllocatorInterface<HAL::Heap>& owner)
27+
{
28+
auto page = Base::AllocateHeap(index, size, owner);
29+
_add(index, page->inner_heap_page_handle.get_size());
30+
return page;
31+
}
32+
33+
void HeapFactory::Free(HeapIndex index, page_type page)
34+
{
35+
_sub(index, page->inner_heap_page_handle.get_size());
36+
Base::Free(index, page);
37+
}
38+
1439
HeapFactory::HeapFactory(Device& device) : device(device) {}
1540

1641
}

sources/HAL/HAL.HeapAllocators.ixx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,25 @@ export namespace HAL
5555

5656
class HeapFactory :public Allocators::HeapFactory<ResourceContext, GlobalAllocationPolicy>
5757
{
58+
using Base = Allocators::HeapFactory<ResourceContext, GlobalAllocationPolicy>;
59+
5860
Device& device;
5961
virtual ptr_type make_heap(HeapIndex index, size_t size) override;
6062

63+
std::atomic<size_t> _upload_bytes = 0;
64+
std::atomic<size_t> _readback_bytes = 0;
65+
66+
void _add(HeapIndex index, size_t n) noexcept;
67+
void _sub(HeapIndex index, size_t n) noexcept;
68+
6169
public:
6270
HeapFactory(Device& device);
71+
72+
page_type AllocateHeap(HeapIndex index, size_t size, Allocators::HeapAllocatorInterface<HAL::Heap>& owner) override;
73+
void Free(HeapIndex index, page_type page) override;
74+
75+
size_t get_upload_bytes() const { return _upload_bytes.load(std::memory_order_relaxed); }
76+
size_t get_readback_bytes() const { return _readback_bytes.load(std::memory_order_relaxed); }
6377
};
6478

6579

sources/RenderSystem/GUI/Elements/StatGraph.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ namespace GUI
4141
header_label->color = {0.9f, 0.9f, 0.9f, 0.85f};
4242
add_child(header_label);
4343

44+
auto make_legend_label = [this]() -> label::ptr {
45+
auto l = label::ptr(new label());
46+
l->docking = dock::NONE;
47+
l->x_type = pos_x_type::LEFT;
48+
l->y_type = pos_y_type::TOP;
49+
l->pos = {0, 2};
50+
l->width_size = size_type::MATCH_PARENT;
51+
l->height_size = size_type::FIXED;
52+
l->size = {0, 12};
53+
l->magnet_text = FW1_RIGHT | FW1_VCENTER | FW1_NOWORDWRAP;
54+
l->color = {0.75f, 0.75f, 0.75f, 0.65f};
55+
return l;
56+
};
57+
58+
_max_label = make_legend_label();
59+
add_child(_max_label);
60+
61+
_min_label = make_legend_label();
62+
add_child(_min_label);
63+
4464
draw_tex.mul_color = {1, 1, 1, 1};
4565
draw_tex.add_color = {0, 0, 0, 0};
4666
}
@@ -67,7 +87,7 @@ namespace GUI
6787
float half_data = (vmax - vmin) * 0.5f;
6888
float half_min = std::max(std::abs(mid), std::abs(vmax)) * 0.05f;
6989
if (half_min < 1.0f) half_min = 1.0f;
70-
float half = std::max(half_data, half_min);
90+
float half = std::max({half_data, half_min, min_range * 0.5f});
7191
float pad = half * 0.15f;
7292
vmin = mid - half - pad;
7393
vmax = mid + half + pad;
@@ -189,6 +209,8 @@ namespace GUI
189209
void stat_graph::update_label()
190210
{
191211
header_label->visible = show_stats;
212+
_max_label->visible = show_stats;
213+
_min_label->visible = show_stats;
192214
if (!show_stats) return;
193215

194216
char buf[128];
@@ -205,14 +227,25 @@ namespace GUI
205227
title.c_str(), cur, usp, u, avg, usp, u);
206228

207229
header_label->text = buf;
230+
231+
// Legend: actual data min/max (not the padded display range)
232+
snprintf(buf, sizeof(buf), "%.1f%s%s ", max_stored(), usp, u);
233+
_max_label->text = buf;
234+
235+
snprintf(buf, sizeof(buf), "%.1f%s%s ", min_stored(), usp, u);
236+
_min_label->text = buf;
208237
}
209238

210239
// ── GPU rendering hooks ────────────────────────────────────────────────
211240

212241
void stat_graph::on_pre_render(Context& c)
213242
{
214-
if (count == 0) return;
243+
// Keep min label pinned to the bottom edge
215244
auto bounds = get_render_bounds();
245+
if (bounds.h > 14.0f)
246+
_min_label->pos = {0, bounds.h - 14.0f};
247+
248+
if (count == 0) return;
216249
if (bounds.w < 4.0f || bounds.h < 4.0f) return;
217250
user_ui->pre_draw_infos.emplace_back(this);
218251
}

sources/RenderSystem/GUI/Elements/StatGraph.ixx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export namespace GUI
2626
bool _range_valid = false;
2727

2828
label::ptr header_label;
29+
label::ptr _max_label;
30+
label::ptr _min_label;
2931
void update_label();
3032

3133
float get_sample(size_t i) const;
@@ -57,6 +59,7 @@ export namespace GUI
5759

5860
float sample_interval = 0.0f;
5961
float range_smooth = 3.0f; // exp-decay speed toward target range; 0 = instant
62+
float min_range = 0.0f; // minimum span of the auto-scale display range
6063
draw_mode mode = draw_mode::cs;
6164

6265
stat_graph();

sources/Spectrum/main.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ class GraphRender : public Window, public GUI::user_interface
467467
GUI::Elements::stat_graph::ptr graph_fps;
468468
GUI::Elements::stat_graph::ptr graph_frametime;
469469
GUI::Elements::stat_graph::ptr graph_vram;
470+
GUI::Elements::stat_graph::ptr graph_upload;
471+
GUI::Elements::stat_graph::ptr graph_readback;
470472

471473
std::shared_ptr<triangle_drawer> drawer;
472474
std::shared_ptr<triangle_drawer> drawer2;
@@ -546,9 +548,12 @@ class GraphRender : public Window, public GUI::user_interface
546548

547549
if (frame_dt > 0.0f)
548550
{
551+
auto& dev = RenderSystem::get().device();
549552
if (graph_fps) graph_fps->push(1.0f / frame_dt, frame_dt);
550553
if (graph_frametime) graph_frametime->push(frame_dt * 1000.0f, frame_dt);
551-
if (graph_vram) graph_vram->push((float)RenderSystem::get().device().get_vram(), frame_dt);
554+
if (graph_vram) graph_vram->push((float)dev.get_vram(), frame_dt);
555+
if (graph_upload) graph_upload->push((float)dev.get_upload_heap(), frame_dt);
556+
if (graph_readback) graph_readback->push((float)dev.get_readback_heap(), frame_dt);
552557
}
553558

554559

@@ -861,15 +866,17 @@ class GraphRender : public Window, public GUI::user_interface
861866
stats_panel->height_size = GUI::size_type::MATCH_PARENT;
862867

863868
graph_fps.reset(new GUI::Elements::stat_graph());
864-
graph_fps->title = "FPS";
865-
graph_fps->size = {0, 70};
866-
graph_fps->mode = GUI::Elements::stat_graph::draw_mode::lines;
869+
graph_fps->title = "FPS";
870+
graph_fps->size = {0, 70};
871+
graph_fps->min_range = 150.0f;
872+
graph_fps->mode = GUI::Elements::stat_graph::draw_mode::lines;
867873
stats_panel->add_child(graph_fps);
868874

869875
graph_frametime.reset(new GUI::Elements::stat_graph());
870876
graph_frametime->title = "Frame Time";
871877
graph_frametime->unit = "ms";
872878
graph_frametime->size = {0, 70};
879+
graph_frametime->min_range = 20.0f;
873880
graph_frametime->line_color = {0.95f, 0.70f, 0.20f, 1.00f};
874881
graph_frametime->fill_color = {0.95f, 0.70f, 0.20f, 0.15f};
875882
graph_frametime->mode = GUI::Elements::stat_graph::draw_mode::lines;
@@ -878,11 +885,32 @@ class GraphRender : public Window, public GUI::user_interface
878885
graph_vram.reset(new GUI::Elements::stat_graph());
879886
graph_vram->title = "VRAM";
880887
graph_vram->size = {0, 70};
888+
graph_vram->min_range = 100.0f;
881889
graph_vram->line_color = {0.90f, 0.30f, 0.30f, 1.00f};
882890
graph_vram->fill_color = {0.90f, 0.30f, 0.30f, 0.15f};
883891
graph_vram->mode = GUI::Elements::stat_graph::draw_mode::lines;
884892
stats_panel->add_child(graph_vram);
885893

894+
graph_upload.reset(new GUI::Elements::stat_graph());
895+
graph_upload->title = "Upload Heap";
896+
graph_upload->unit = "MB";
897+
graph_upload->size = {0, 70};
898+
graph_upload->min_range = 50.0f;
899+
graph_upload->line_color = {0.40f, 0.70f, 1.00f, 1.00f};
900+
graph_upload->fill_color = {0.40f, 0.70f, 1.00f, 0.15f};
901+
graph_upload->mode = GUI::Elements::stat_graph::draw_mode::lines;
902+
stats_panel->add_child(graph_upload);
903+
904+
graph_readback.reset(new GUI::Elements::stat_graph());
905+
graph_readback->title = "Readback Heap";
906+
graph_readback->unit = "MB";
907+
graph_readback->size = {0, 70};
908+
graph_readback->min_range = 50.0f;
909+
graph_readback->line_color = {1.00f, 0.60f, 0.20f, 1.00f};
910+
graph_readback->fill_color = {1.00f, 0.60f, 0.20f, 0.15f};
911+
graph_readback->mode = GUI::Elements::stat_graph::draw_mode::lines;
912+
stats_panel->add_child(graph_readback);
913+
886914
dock->get_tabs()->add_page("Stats", stats_panel);
887915
dock->size = {100, 230};
888916
}

0 commit comments

Comments
 (0)