From c982fadaf53376e60e57c284ede2e1f8555ecd2a Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Tue, 13 Oct 2026 15:04:23 +0200 Subject: [PATCH 1/9] Graphics benchmark: scripted flights over four scenes at render distances 8/16/32, with CPU and GPU frame times horizon-ctl gains 'fly' (a constant-speed camera path that replies with the frame summary); frame summaries add CPU time per frame (waits for the GPU, display and limiter excluded), per-phase averages and GPU time from timestamp queries. --offscreen renders into a texture on the discrete GPU so the 4080 can be measured under the headless compositor. Baselines: lavapipe on vscreen and the RTX 4080 offscreen, seed 1234. Co-Authored-By: Claude Opus 5.5 --- benchmarks/graphics/baseline-4080.json | 392 +++++++++++++++++++++ benchmarks/graphics/baseline-lavapipe.json | 392 +++++++++++++++++++++ crates/horizon-client/src/control.rs | 59 +++- crates/horizon-client/src/game.rs | 28 ++ crates/horizon-client/src/game/control.rs | 54 +++ crates/horizon-client/src/gfx.rs | 206 ++++++++--- crates/horizon-client/src/gfx/timing.rs | 122 +++++++ tools/graphics_bench.py | 163 +++++++++ 8 files changed, 1369 insertions(+), 47 deletions(-) create mode 100644 benchmarks/graphics/baseline-4080.json create mode 100644 benchmarks/graphics/baseline-lavapipe.json create mode 100644 crates/horizon-client/src/gfx/timing.rs create mode 100644 tools/graphics_bench.py diff --git a/benchmarks/graphics/baseline-4080.json b/benchmarks/graphics/baseline-4080.json new file mode 100644 index 000000000..491908424 --- /dev/null +++ b/benchmarks/graphics/baseline-4080.json @@ -0,0 +1,392 @@ +{ + "label": "baseline-4080", + "date": "2026-10-13", + "commit": "27a32cc3", + "host": "ludodesktop", + "offscreen": true, + "seed": "1234", + "flight_seconds": 12.0, + "scenes": { + "village": { + "from": [ + -960.0, + 125.0, + 80.0 + ], + "to": [ + -960.0, + 125.0, + 272.0 + ], + "pitch": 35.0 + }, + "forest": { + "from": [ + -1100.0, + 120.0, + -560.0 + ], + "to": [ + -1100.0, + 120.0, + -368.0 + ], + "pitch": 25.0 + }, + "ocean": { + "from": [ + -300.0, + 85.0, + -420.0 + ], + "to": [ + -300.0, + 85.0, + -228.0 + ], + "pitch": 10.0 + }, + "mountain": { + "from": [ + 560.0, + 260.0, + 860.0 + ], + "to": [ + 752.0, + 260.0, + 860.0 + ], + "pitch": 30.0 + } + }, + "distances": { + "8": { + "renderer": { + "backend": "Vulkan", + "drawn_sections": 158, + "gpu": "NVIDIA GeForce RTX 4080", + "present_mode": "Immediate", + "size": [ + 1280, + 720 + ], + "uploaded_bytes": 1137747200 + }, + "village": { + "avg_fps": 362.94072531330323, + "cpu": { + "avg_ms": 2.734311161203731, + "max_ms": 68.06377410888672, + "p50_ms": 1.4820960760116577, + "p95_ms": 7.922675609588623, + "p99_ms": 35.78989028930664 + }, + "frames": 4373, + "gpu": { + "avg_ms": 0.4188921034758625, + "max_ms": 2.5876479148864746, + "p50_ms": 0.4106239974498749, + "p95_ms": 0.5283839702606201, + "p99_ms": 0.5427200198173523 + }, + "low_1pct_fps": 21.64332698060583, + "max_ms": 68.08219146728516, + "p50_ms": 1.4894599914550781, + "p95_ms": 7.936410427093506, + "p99_ms": 36.54151153564453, + "seconds": 12.048799418210983 + }, + "forest": { + "avg_fps": 613.1553113846195, + "cpu": { + "avg_ms": 1.6186122214214547, + "max_ms": 216.98960876464844, + "p50_ms": 1.10076105594635, + "p95_ms": 3.0016868114471436, + "p99_ms": 10.144570350646973 + }, + "frames": 6746, + "gpu": { + "avg_ms": 0.395751640401594, + "max_ms": 1.99782395362854, + "p50_ms": 0.3983359932899475, + "p95_ms": 0.4833280146121979, + "p99_ms": 0.5375999808311462 + }, + "low_1pct_fps": 49.639170314777054, + "max_ms": 217.0211181640625, + "p50_ms": 1.109616994857788, + "p95_ms": 3.0161218643188477, + "p99_ms": 10.163111686706543, + "seconds": 11.002106439828873 + }, + "ocean": { + "avg_fps": 323.23791637473414, + "cpu": { + "avg_ms": 2.7230400277200597, + "max_ms": 4748.7744140625, + "p50_ms": 0.9233829975128174, + "p95_ms": 1.677724003791809, + "p99_ms": 7.7397589683532715 + }, + "frames": 3870, + "gpu": { + "avg_ms": 0.24116999250656182, + "max_ms": 0.954367995262146, + "p50_ms": 0.2754560112953186, + "p95_ms": 0.3389439880847931, + "p99_ms": 0.3532800078392029 + }, + "low_1pct_fps": 5.421154869716093, + "max_ms": 4748.80615234375, + "p50_ms": 0.9285120368003845, + "p95_ms": 1.7079449892044067, + "p99_ms": 33.33257293701172, + "seconds": 11.97260532861948 + }, + "mountain": { + "avg_fps": 28.19080143244723, + "cpu": { + "avg_ms": 12.358911560308298, + "max_ms": 187.3409881591797, + "p50_ms": 9.513923645019531, + "p95_ms": 25.783824920654297, + "p99_ms": 110.71585845947266 + }, + "frames": 338, + "gpu": { + "avg_ms": 0.46002139662497143, + "max_ms": 1.5626239776611328, + "p50_ms": 0.4546560049057007, + "p95_ms": 0.5140479803085327, + "p99_ms": 0.5959680080413818 + }, + "low_1pct_fps": 7.428141780247131, + "max_ms": 187.36033630371094, + "p50_ms": 33.334835052490234, + "p95_ms": 35.65903854370117, + "p99_ms": 110.73509216308594, + "seconds": 11.98972653579712 + } + }, + "16": { + "renderer": { + "backend": "Vulkan", + "drawn_sections": 1, + "gpu": "NVIDIA GeForce RTX 4080", + "present_mode": "Immediate", + "size": [ + 1280, + 720 + ], + "uploaded_bytes": 54284928 + }, + "village": { + "avg_fps": 7.569165639843409, + "cpu": { + "avg_ms": 138.25648041658624, + "max_ms": 877.6473999023438, + "p50_ms": 103.97663879394531, + "p95_ms": 318.5262145996094, + "p99_ms": 877.6473999023438 + }, + "frames": 86, + "gpu": { + "avg_ms": 3.585262137790059, + "max_ms": 8.415231704711914, + "p50_ms": 3.299328088760376, + "p95_ms": 7.171072006225586, + "p99_ms": 8.415231704711914 + }, + "low_1pct_fps": 2.44461512120682, + "max_ms": 409.0623474121094, + "p50_ms": 104.01687622070312, + "p95_ms": 318.5569763183594, + "p99_ms": 409.0623474121094, + "seconds": 11.361886381149292 + }, + "forest": { + "avg_fps": 27.65529078792617, + "cpu": { + "avg_ms": 37.926895780927815, + "max_ms": 3484.08349609375, + "p50_ms": 3.398620128631592, + "p95_ms": 44.56385040283203, + "p99_ms": 684.4393920898438 + }, + "frames": 314, + "gpu": { + "avg_ms": 2.683724634467986, + "max_ms": 9.881600379943848, + "p50_ms": 1.8923519849777222, + "p95_ms": 7.221248149871826, + "p99_ms": 9.04089641571045 + }, + "low_1pct_fps": 0.4753349245465373, + "max_ms": 3484.115234375, + "p50_ms": 3.5420219898223877, + "p95_ms": 44.591251373291016, + "p99_ms": 159.3817901611328, + "seconds": 11.354066113710404 + }, + "ocean": { + "avg_fps": 59.16125632179682, + "cpu": { + "avg_ms": 16.807428855239674, + "max_ms": 106.0348129272461, + "p50_ms": 9.335607528686523, + "p95_ms": 44.57380294799805, + "p99_ms": 84.97154998779297 + }, + "frames": 712, + "gpu": { + "avg_ms": 1.958949391112736, + "max_ms": 8.635392189025879, + "p50_ms": 1.1182080507278442, + "p95_ms": 5.493760108947754, + "p99_ms": 7.108607769012451 + }, + "low_1pct_fps": 10.542641239411056, + "max_ms": 106.06544494628906, + "p50_ms": 9.364562034606934, + "p95_ms": 45.14366912841797, + "p99_ms": 84.9927749633789, + "seconds": 12.034903317928315 + }, + "mountain": { + "avg_fps": 26.78175419456072, + "cpu": { + "avg_ms": 20.230309660251326, + "max_ms": 128.05508422851562, + "p50_ms": 15.369550704956055, + "p95_ms": 67.15973663330078, + "p99_ms": 104.6961898803711 + }, + "frames": 325, + "gpu": { + "avg_ms": 7.143625645270714, + "max_ms": 21.25823974609375, + "p50_ms": 7.187456130981445, + "p95_ms": 12.228608131408691, + "p99_ms": 19.310592651367188 + }, + "low_1pct_fps": 7.777799947825389, + "max_ms": 155.30685424804688, + "p50_ms": 33.335182189941406, + "p95_ms": 68.94293975830078, + "p99_ms": 109.09304809570312, + "seconds": 12.135127431869506 + } + }, + "32": { + "renderer": { + "backend": "Vulkan", + "drawn_sections": 0, + "gpu": "NVIDIA GeForce RTX 4080", + "present_mode": "Immediate", + "size": [ + 1280, + 720 + ], + "uploaded_bytes": 4829184 + }, + "village": { + "avg_fps": 4.1477100728464, + "cpu": { + "avg_ms": 244.17284844846142, + "max_ms": 386.7664489746094, + "p50_ms": 244.8783416748047, + "p95_ms": 363.0765380859375, + "p99_ms": 386.7664489746094 + }, + "frames": 49, + "gpu": { + "avg_ms": 10.357822744213806, + "max_ms": 23.010303497314453, + "p50_ms": 9.227264404296875, + "p95_ms": 19.309568405151367, + "p99_ms": 23.010303497314453 + }, + "low_1pct_fps": 2.5853636290905797, + "max_ms": 386.79278564453125, + "p50_ms": 230.8652801513672, + "p95_ms": 363.0941162109375, + "p99_ms": 386.79278564453125, + "seconds": 11.813747619628906 + }, + "forest": { + "avg_fps": 8.3624969613463, + "cpu": { + "avg_ms": 118.74922356747165, + "max_ms": 278.8187255859375, + "p50_ms": 123.03407287597656, + "p95_ms": 231.6608123779297, + "p99_ms": 260.8450622558594 + }, + "frames": 101, + "gpu": { + "avg_ms": 15.898816661669477, + "max_ms": 72.880126953125, + "p50_ms": 20.789247512817383, + "p95_ms": 28.441600799560547, + "p99_ms": 60.816383361816406 + }, + "low_1pct_fps": 3.7057290596141406, + "max_ms": 278.83917236328125, + "p50_ms": 123.19290161132812, + "p95_ms": 231.68026733398438, + "p99_ms": 260.8656921386719, + "seconds": 12.077732340812684 + }, + "ocean": { + "avg_fps": 2.3954947141714045, + "cpu": { + "avg_ms": 424.7621285574777, + "max_ms": 625.8532104492188, + "p50_ms": 435.64044189453125, + "p95_ms": 577.7593994140625, + "p99_ms": 625.8532104492188 + }, + "frames": 28, + "gpu": { + "avg_ms": 9.483117733682905, + "max_ms": 12.973055839538574, + "p50_ms": 9.90719985961914, + "p95_ms": 12.610560417175293, + "p99_ms": 12.973055839538574 + }, + "low_1pct_fps": 1.5977632874291703, + "max_ms": 625.8749389648438, + "p50_ms": 433.9859619140625, + "p95_ms": 577.7803955078125, + "p99_ms": 625.8749389648438, + "seconds": 11.688608551025391 + }, + "mountain": { + "avg_fps": 3.3756775332390268, + "cpu": { + "avg_ms": 300.512197249975, + "max_ms": 407.4363708496094, + "p50_ms": 292.483642578125, + "p95_ms": 394.8424377441406, + "p99_ms": 407.4363708496094 + }, + "frames": 39, + "gpu": { + "avg_ms": 9.728866460995796, + "max_ms": 18.930688858032227, + "p50_ms": 8.820735931396484, + "p95_ms": 17.399808883666992, + "p99_ms": 18.930688858032227 + }, + "low_1pct_fps": 2.4541792422631716, + "max_ms": 407.46820068359375, + "p50_ms": 292.4177551269531, + "p95_ms": 394.8649597167969, + "p99_ms": 407.46820068359375, + "seconds": 11.553236236572266 + } + } + } +} diff --git a/benchmarks/graphics/baseline-lavapipe.json b/benchmarks/graphics/baseline-lavapipe.json new file mode 100644 index 000000000..72ea8253c --- /dev/null +++ b/benchmarks/graphics/baseline-lavapipe.json @@ -0,0 +1,392 @@ +{ + "label": "baseline-lavapipe", + "date": "2026-10-13", + "commit": "27a32cc3", + "host": "ludodesktop", + "offscreen": false, + "seed": "1234", + "flight_seconds": 12.0, + "scenes": { + "village": { + "from": [ + -960.0, + 125.0, + 80.0 + ], + "to": [ + -960.0, + 125.0, + 272.0 + ], + "pitch": 35.0 + }, + "forest": { + "from": [ + -1100.0, + 120.0, + -560.0 + ], + "to": [ + -1100.0, + 120.0, + -368.0 + ], + "pitch": 25.0 + }, + "ocean": { + "from": [ + -300.0, + 85.0, + -420.0 + ], + "to": [ + -300.0, + 85.0, + -228.0 + ], + "pitch": 10.0 + }, + "mountain": { + "from": [ + 560.0, + 260.0, + 860.0 + ], + "to": [ + 752.0, + 260.0, + 860.0 + ], + "pitch": 30.0 + } + }, + "distances": { + "8": { + "renderer": { + "backend": "Vulkan", + "drawn_sections": 146, + "gpu": "llvmpipe (LLVM 21.1.8, 256 bits)", + "present_mode": "Mailbox", + "size": [ + 1600, + 1000 + ], + "uploaded_bytes": 1288851072 + }, + "village": { + "avg_fps": 16.38225374384963, + "cpu": { + "avg_ms": 48.17248754105975, + "max_ms": 134.31028747558594, + "p50_ms": 44.57072067260742, + "p95_ms": 97.15985870361328, + "p99_ms": 127.31073760986328 + }, + "frames": 199, + "gpu": { + "avg_ms": 26.732369270324707, + "max_ms": 47.3107795715332, + "p50_ms": 26.284116744995117, + "p95_ms": 35.006874084472656, + "p99_ms": 39.38005828857422 + }, + "low_1pct_fps": 6.569673692138871, + "max_ms": 169.97352600097656, + "p50_ms": 56.791988372802734, + "p95_ms": 109.15220642089844, + "p99_ms": 134.45559692382812, + "seconds": 12.147290788650512 + }, + "forest": { + "avg_fps": 19.917172468484612, + "cpu": { + "avg_ms": 31.039839970568817, + "max_ms": 106.39506530761719, + "p50_ms": 33.26064682006836, + "p95_ms": 70.09876251220703, + "p99_ms": 86.21255493164062 + }, + "frames": 240, + "gpu": { + "avg_ms": 37.272586759195285, + "max_ms": 77.586669921875, + "p50_ms": 34.38500213623047, + "p95_ms": 59.649635314941406, + "p99_ms": 66.52396392822266 + }, + "low_1pct_fps": 9.909291086862112, + "max_ms": 106.57542419433594, + "p50_ms": 46.05862808227539, + "p95_ms": 80.38935852050781, + "p99_ms": 94.94140625, + "seconds": 12.049903186798096 + }, + "ocean": { + "avg_fps": 27.969763129786234, + "cpu": { + "avg_ms": 25.69895526429727, + "max_ms": 930.7354125976562, + "p50_ms": 18.981081008911133, + "p95_ms": 39.42117691040039, + "p99_ms": 168.07225036621094 + }, + "frames": 336, + "gpu": { + "avg_ms": 18.856842384451912, + "max_ms": 57.835872650146484, + "p50_ms": 18.172868728637695, + "p95_ms": 28.62451171875, + "p99_ms": 44.414398193359375 + }, + "low_1pct_fps": 1.527565223414601, + "max_ms": 930.8734741210938, + "p50_ms": 26.45800018310547, + "p95_ms": 50.053672790527344, + "p99_ms": 180.92478942871094, + "seconds": 12.012972667694092 + }, + "mountain": { + "avg_fps": 26.5942562164059, + "cpu": { + "avg_ms": 22.482411369495093, + "max_ms": 104.89199829101562, + "p50_ms": 22.033512115478516, + "p95_ms": 46.385555267333984, + "p99_ms": 61.15697479248047 + }, + "frames": 320, + "gpu": { + "avg_ms": 23.762589550018312, + "max_ms": 48.10239028930664, + "p50_ms": 22.580347061157227, + "p95_ms": 35.14606475830078, + "p99_ms": 42.47938919067383 + }, + "low_1pct_fps": 11.197907022053611, + "max_ms": 105.07585906982422, + "p50_ms": 33.362815856933594, + "p95_ms": 53.36695098876953, + "p99_ms": 74.80219268798828, + "seconds": 12.03267342376709 + } + }, + "16": { + "renderer": { + "backend": "Vulkan", + "drawn_sections": 1, + "gpu": "llvmpipe (LLVM 21.1.8, 256 bits)", + "present_mode": "Mailbox", + "size": [ + 1600, + 1000 + ], + "uploaded_bytes": 16106880 + }, + "village": { + "avg_fps": 10.754658718707336, + "cpu": { + "avg_ms": 69.22208800682655, + "max_ms": 193.21267700195312, + "p50_ms": 66.95130157470703, + "p95_ms": 154.44834899902344, + "p99_ms": 191.29208374023438 + }, + "frames": 130, + "gpu": { + "avg_ms": 49.041961078495945, + "max_ms": 73.83775329589844, + "p50_ms": 48.90928268432617, + "p95_ms": 60.60416793823242, + "p99_ms": 66.1044921875 + }, + "low_1pct_fps": 4.171866301284614, + "max_ms": 252.21458435058594, + "p50_ms": 79.8940200805664, + "p95_ms": 173.05947875976562, + "p99_ms": 227.18716430664062, + "seconds": 12.087784782409669 + }, + "forest": { + "avg_fps": 12.167278778001634, + "cpu": { + "avg_ms": 58.366991678873696, + "max_ms": 156.683349609375, + "p50_ms": 69.24014282226562, + "p95_ms": 110.83757019042969, + "p99_ms": 134.16554260253906 + }, + "frames": 147, + "gpu": { + "avg_ms": 56.45229888606716, + "max_ms": 78.26596069335938, + "p50_ms": 55.92947006225586, + "p95_ms": 63.54412841796875, + "p99_ms": 66.5202407836914 + }, + "low_1pct_fps": 5.722095670390665, + "max_ms": 212.99729919433594, + "p50_ms": 77.36986541748047, + "p95_ms": 122.84784698486328, + "p99_ms": 136.52499389648438, + "seconds": 12.081583950042724 + }, + "ocean": { + "avg_fps": 7.681804845753147, + "cpu": { + "avg_ms": 98.35419226740743, + "max_ms": 385.2493591308594, + "p50_ms": 78.85044860839844, + "p95_ms": 225.36231994628906, + "p99_ms": 385.2493591308594 + }, + "frames": 91, + "gpu": { + "avg_ms": 71.51263585297957, + "max_ms": 128.29212951660156, + "p50_ms": 67.92412567138672, + "p95_ms": 101.93219757080078, + "p99_ms": 128.29212951660156 + }, + "low_1pct_fps": 2.5948566279945817, + "max_ms": 385.37774658203125, + "p50_ms": 123.91693878173828, + "p95_ms": 225.49420166015625, + "p99_ms": 385.37774658203125, + "seconds": 11.846174411773681 + }, + "mountain": { + "avg_fps": 5.886844433101332, + "cpu": { + "avg_ms": 124.87309415575484, + "max_ms": 321.2157897949219, + "p50_ms": 117.2811050415039, + "p95_ms": 237.34304809570312, + "p99_ms": 321.2157897949219 + }, + "frames": 71, + "gpu": { + "avg_ms": 95.46965434816148, + "max_ms": 166.9488067626953, + "p50_ms": 83.54202270507812, + "p95_ms": 160.2403564453125, + "p99_ms": 166.9488067626953 + }, + "low_1pct_fps": 3.111272818975726, + "max_ms": 321.411865234375, + "p50_ms": 153.0963134765625, + "p95_ms": 275.37579345703125, + "p99_ms": 321.411865234375, + "seconds": 12.060790939331055 + } + }, + "32": { + "renderer": { + "backend": "Vulkan", + "drawn_sections": 1, + "gpu": "llvmpipe (LLVM 21.1.8, 256 bits)", + "present_mode": "Mailbox", + "size": [ + 1600, + 1000 + ], + "uploaded_bytes": 19154560 + }, + "village": { + "avg_fps": 2.5956714101148957, + "cpu": { + "avg_ms": 345.63314671670236, + "max_ms": 474.2013854980469, + "p50_ms": 347.07415771484375, + "p95_ms": 468.0090026855469, + "p99_ms": 474.2013854980469 + }, + "frames": 31, + "gpu": { + "avg_ms": 85.50063645839691, + "max_ms": 134.11460876464844, + "p50_ms": 82.40946960449219, + "p95_ms": 133.71929931640625, + "p99_ms": 134.11460876464844 + }, + "low_1pct_fps": 2.01406006678458, + "max_ms": 496.509521484375, + "p50_ms": 390.230224609375, + "p95_ms": 493.8070373535156, + "p99_ms": 496.509521484375, + "seconds": 11.942960067749024 + }, + "forest": { + "avg_fps": 2.2283930626963917, + "cpu": { + "avg_ms": 354.7813931978666, + "max_ms": 611.8099975585938, + "p50_ms": 293.61376953125, + "p95_ms": 601.3502197265625, + "p99_ms": 611.8099975585938 + }, + "frames": 26, + "gpu": { + "avg_ms": 201.54420654296874, + "max_ms": 357.0310363769531, + "p50_ms": 172.65980529785156, + "p95_ms": 341.8655700683594, + "p99_ms": 357.0310363769531 + }, + "low_1pct_fps": 1.52339962842959, + "max_ms": 656.4265747070312, + "p50_ms": 407.1051330566406, + "p95_ms": 611.97119140625, + "p99_ms": 656.4265747070312, + "seconds": 11.667600494384766 + }, + "ocean": { + "avg_fps": 2.6137193379326398, + "cpu": { + "avg_ms": 330.783615620931, + "max_ms": 730.7304077148438, + "p50_ms": 296.65692138671875, + "p95_ms": 680.8717041015625, + "p99_ms": 730.7304077148438 + }, + "frames": 30, + "gpu": { + "avg_ms": 113.80178985595703, + "max_ms": 216.57342529296875, + "p50_ms": 104.23703002929688, + "p95_ms": 193.67672729492188, + "p99_ms": 216.57342529296875 + }, + "low_1pct_fps": 1.1784160273210693, + "max_ms": 848.5967407226562, + "p50_ms": 346.120849609375, + "p95_ms": 681.0716552734375, + "p99_ms": 848.5967407226562, + "seconds": 11.477896484375 + }, + "mountain": { + "avg_fps": 2.72441042065798, + "cpu": { + "avg_ms": 325.24547207740045, + "max_ms": 578.7177124023438, + "p50_ms": 295.25457763671875, + "p95_ms": 569.8099975585938, + "p99_ms": 578.7177124023438 + }, + "frames": 31, + "gpu": { + "avg_ms": 104.99177145957947, + "max_ms": 163.95635986328125, + "p50_ms": 99.96784210205078, + "p95_ms": 160.04824829101562, + "p99_ms": 163.95635986328125 + }, + "low_1pct_fps": 1.7544071931551564, + "max_ms": 569.9931030273438, + "p50_ms": 378.246337890625, + "p95_ms": 555.5369873046875, + "p99_ms": 569.9931030273438, + "seconds": 11.378608657836914 + } + } + } +} diff --git a/crates/horizon-client/src/control.rs b/crates/horizon-client/src/control.rs index 880b88f5b..0608bc72f 100644 --- a/crates/horizon-client/src/control.rs +++ b/crates/horizon-client/src/control.rs @@ -140,10 +140,38 @@ fn serve(stream: UnixStream, sender: &mpsc::Sender) { } } -/// Frame durations since the last reset, summarized like the Java recorder. +/// Frame durations since the last reset, summarized like the Java recorder, +/// with the CPU part of each frame and GPU times from timestamp queries. #[derive(Default)] pub struct FrameStats { millis: Vec, + cpu: Vec, + gpu: Vec, + /// Summed milliseconds per frame phase (`PHASES`) and the frames counted. + phases: [f64; PHASES.len()], + phase_frames: u64, +} + +/// The parts of a frame's CPU time: world streaming and uploads, game ticks, +/// building the frame's geometry and HUD, and the renderer's own work. +pub const PHASES: [&str; 4] = ["update", "tick", "prepare", "render"]; + +fn percentiles(values: &[f32]) -> serde_json::Value { + let mut sorted = values.to_vec(); + sorted.sort_by(f32::total_cmp); + let n = sorted.len(); + if n == 0 { + return serde_json::Value::Null; + } + let total: f64 = sorted.iter().map(|&m| f64::from(m)).sum(); + let percentile = |p: f64| sorted[((n as f64 * p).ceil() as usize).clamp(1, n) - 1]; + serde_json::json!({ + "avg_ms": total / n as f64, + "p50_ms": percentile(0.5), + "p95_ms": percentile(0.95), + "p99_ms": percentile(0.99), + "max_ms": sorted[n - 1], + }) } impl FrameStats { @@ -153,8 +181,30 @@ impl FrameStats { self.millis.push(millis); } } + /// The frame's own work: its duration minus waits for the GPU, the + /// display and the frame limiter. + pub fn record_cpu(&mut self, millis: f32) { + if self.cpu.len() < Self::LIMIT { + self.cpu.push(millis); + } + } + pub fn record_phases(&mut self, millis: [f32; PHASES.len()]) { + for (sum, m) in self.phases.iter_mut().zip(millis) { + *sum += f64::from(m); + } + self.phase_frames += 1; + } + pub fn record_gpu(&mut self, millis: &[f32]) { + if self.gpu.len() < Self::LIMIT { + self.gpu.extend_from_slice(millis); + } + } pub fn reset(&mut self) { self.millis.clear(); + self.cpu.clear(); + self.gpu.clear(); + self.phases = [0.0; PHASES.len()]; + self.phase_frames = 0; } pub fn summary(&self) -> serde_json::Value { let mut sorted = self.millis.clone(); @@ -181,6 +231,13 @@ impl FrameStats { "p95_ms": percentile(0.95), "p99_ms": percentile(0.99), "max_ms": sorted[n - 1], + "cpu": percentiles(&self.cpu), + "gpu": percentiles(&self.gpu), + "phases_avg_ms": PHASES + .iter() + .zip(self.phases) + .map(|(name, sum)| (name.to_string(), (sum / self.phase_frames.max(1) as f64).into())) + .collect::>(), }) } } diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index a9c440959..45719da03 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -183,6 +183,9 @@ struct Options { no_pause: bool, /// Control socket path (`--control`), for `horizon-ctl`. control: Option, + /// Render into a texture instead of the window (`--offscreen`), so a + /// GPU the display cannot present from can still be benchmarked. + offscreen: bool, /// Multiplayer: `--server HOST[:PORT]` joins at start; `--name` is the offline player name. server: Option, player_name: String, @@ -218,6 +221,7 @@ impl Options { dimension: None, no_pause: false, control: None, + offscreen: false, server: None, player_name: std::env::var("HORIZON_PLAYER_NAME").unwrap_or_else(|_| "Player".into()), dedicated: None, @@ -238,6 +242,7 @@ impl Options { "--creative" => o.creative = true, "--test-terrain" => o.test_terrain = true, "--no-pause" => o.no_pause = true, + "--offscreen" => o.offscreen = true, "--control" => o.control = Some(horizon_client::control::default_path()), _ => { let value = args @@ -776,6 +781,8 @@ struct Game<'a> { /// Control replies waiting for a rendered frame or a benchmark deadline. pending_screenshot: Option>, pending_bench: Option<(Instant, std::sync::mpsc::Sender)>, + /// A scripted flight from the control socket (`fly`). + flight: Option, persist: persist::EntityMemory, /// Connected to a server: the world comes from the network (game/remote.rs). remote: Option, @@ -7593,6 +7600,7 @@ impl ApplicationHandler for Game<'_> { let assets = &self.res.assets; match pollster::block_on(Renderer::new( window.clone(), + self.options.offscreen, &assets.pixels, assets.sprite_size, assets.texture_layers, @@ -8005,6 +8013,7 @@ impl ApplicationHandler for Game<'_> { self.update_world(); self.spawn_player()?; } + let updated = Instant::now(); self.track_toggle_screens(); let steps = self.clock.advance(elapsed); for _ in 0..steps { @@ -8022,7 +8031,25 @@ impl ApplicationHandler for Game<'_> { } } self.apply_mouse_look(); + self.advance_flight(); + let ticked = Instant::now(); let rendered = self.render()?; + if let Some(r) = self.renderer.as_mut() { + let cpu = now.elapsed().saturating_sub(r.last_wait); + if rendered { + let ms = |d: Duration| d.as_secs_f32() * 1000.0; + self.frame_stats.record_cpu(ms(cpu)); + let render = r.last_encode; + self.frame_stats.record_phases([ + ms(updated - now), + ms(ticked - updated), + ms(ticked.elapsed().saturating_sub(r.last_wait + render)), + ms(render), + ]); + } + self.frame_stats.record_gpu(&r.gpu_times); + r.gpu_times.clear(); + } self.limit_frame(); self.pause = self.pauses_game(); self.clock.end_frame(self.pause); @@ -8428,6 +8455,7 @@ pub fn run() -> Result<()> { frame_stats: Default::default(), pending_screenshot: None, pending_bench: None, + flight: None, persist: Default::default(), remote: None, lan: None, diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index e11776548..64960ac23 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -32,6 +32,7 @@ const HELP: &[&str] = &[ "options [PAGE] open the options (video sound controls keybinds mouse chat accessibility language font packs)", "stats [reset] frame-time summary since the last reset", "bench SECONDS reset stats, wait, then reply with the summary", + "fly X1 Y1 Z1 X2 Y2 Z2 SECONDS [PITCH] fly the camera along a line at constant speed, then reply with the frame summary", "open WORLD [SEED] [creative] open or create a world from the title screen", "save save the world", "save-all save the world, rewriting every resident chunk", @@ -42,7 +43,42 @@ const HELP: &[&str] = &[ "quit save and exit", ]; +/// A straight camera path at constant speed, for repeatable benchmarks: the +/// player is placed on it every frame, facing along it. +pub struct Flight { + from: Vec3, + to: Vec3, + pitch: f32, + started: Instant, + seconds: f64, + reply: Sender, +} + impl Game<'_> { + /// Move the camera along the running flight; reply when it ends. + pub(super) fn advance_flight(&mut self) { + let Some(flight) = &self.flight else { return }; + let t = (flight.started.elapsed().as_secs_f64() / flight.seconds).min(1.0); + let (a, b) = (flight.from, flight.to); + let d = [b.x - a.x, b.y - a.y, b.z - a.z]; + let at = Vec3::new(a.x + d[0] * t, a.y + d[1] * t, a.z + d[2] * t); + // Minecraft yaw: 0 faces +Z, 90 faces -X. + self.yaw = (-d[0]).atan2(d[2]).to_degrees() as f32; + self.pitch = flight.pitch; + if let Some(player) = self.player.as_mut() { + player.position = at; + player.velocity = Vec3::new(0.0, 0.0, 0.0); + player.fall_distance = 0.0; + // Stay where the flight ends (creative flight). + player.flying = true; + self.previous_position = at; + } + if t >= 1.0 + && let Some(flight) = self.flight.take() + { + let _ = flight.reply.send(self.frame_stats.summary().to_string()); + } + } pub(super) fn poll_control(&mut self, event_loop: &ActiveEventLoop) { if let Some((deadline, reply)) = self.pending_bench.take() { if Instant::now() >= deadline { @@ -357,6 +393,24 @@ impl Game<'_> { )); Ok(None) } + "fly" => { + self.require_world()?; + if self.flight.is_some() || self.pending_bench.is_some() { + return Err("a benchmark is already running".into()); + } + let n = |i: usize| -> Result { Ok(arg(i)?.parse()?) }; + let pitch = words.get(8).map_or(Ok(10.0), |p| p.parse::())?; + self.frame_stats.reset(); + self.flight = Some(Flight { + from: Vec3::new(n(1)?, n(2)?, n(3)?), + to: Vec3::new(n(4)?, n(5)?, n(6)?), + pitch, + started: Instant::now(), + seconds: n(7)?.max(0.1), + reply: reply.clone(), + }); + Ok(None) + } "open" => { if self.title.is_none() { return Err("a world is already open".into()); diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 0a8896b6f..6d1b3b530 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -13,6 +13,8 @@ use std::{collections::HashMap, path::PathBuf, sync::Arc, time::Instant}; use wgpu::util::DeviceExt; use winit::window::Window; +mod timing; + #[repr(C)] #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] struct Globals { @@ -115,7 +117,20 @@ struct BorderUniform { } pub struct Renderer { instance: wgpu::Instance, - surface: wgpu::Surface<'static>, + /// The window's swapchain; `None` when rendering offscreen (`--offscreen`). + surface: Option>, + /// The offscreen colour target, sized like `config`. + offscreen: Option, + /// Offscreen submissions still on the GPU: at most two frames run ahead. + in_flight: std::collections::VecDeque, + gpu_timer: Option, + /// GPU frame times (ms) read back since the caller last drained them. + pub gpu_times: Vec, + /// Time the last `render` spent blocked on the GPU or the display + /// (acquiring, presenting, offscreen pacing), excluded from CPU time. + pub last_wait: std::time::Duration, + /// CPU time of the last `render`, waits excluded. + pub last_encode: std::time::Duration, window: Arc, pub device: wgpu::Device, pub queue: wgpu::Queue, @@ -217,6 +232,7 @@ fn mip_chain(pixels: &[u8], size: u32, layers: u32) -> (Vec, u32) { impl Renderer { pub async fn new( window: Arc, + offscreen: bool, sprite_pixels: &[u8], sprite_size: u32, layers: u32, @@ -226,11 +242,17 @@ impl Renderer { ) -> Result { let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env()); - let surface = instance.create_surface(window.clone())?; + // Offscreen rendering needs no surface, so it can use a GPU the + // window's display cannot present from (a headless compositor). + let surface = if offscreen { + None + } else { + Some(instance.create_surface(window.clone())?) + }; let adapter = instance .request_adapter(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, - compatible_surface: Some(&surface), + compatible_surface: surface.as_ref(), force_fallback_adapter: false, ..Default::default() }) @@ -251,34 +273,56 @@ impl Renderer { let (device, queue) = adapter .request_device(&wgpu::DeviceDescriptor { label: Some("Horizon device"), + required_features: timing::GpuTimer::features(&adapter) + | (adapter.features() & wgpu::Features::INDIRECT_FIRST_INSTANCE), required_limits: limits.clone(), ..Default::default() }) .await?; let size = window.inner_size(); - let mut config = surface - .get_default_config(&adapter, size.width.max(1), size.height.max(1)) - .ok_or("surface has no supported configuration")?; - let caps = surface.get_capabilities(&adapter); - // Vanilla blends and tints in gamma space: use a non-sRGB target. - config.format = caps - .formats - .iter() - .copied() - .find(|f| !f.is_srgb()) - .unwrap_or(config.format); - config.view_formats = vec![config.format.remove_srgb_suffix()]; - // Screenshots copy the presented frame back from the swapchain. - if caps.usages.contains(wgpu::TextureUsages::COPY_SRC) { - config.usage |= wgpu::TextureUsages::COPY_SRC; - } - let present_modes = caps.present_modes.clone(); - config.present_mode = if caps.present_modes.contains(&wgpu::PresentMode::Mailbox) { - wgpu::PresentMode::Mailbox + let (config, present_modes) = if let Some(surface) = &surface { + let mut config = surface + .get_default_config(&adapter, size.width.max(1), size.height.max(1)) + .ok_or("surface has no supported configuration")?; + let caps = surface.get_capabilities(&adapter); + // Vanilla blends and tints in gamma space: use a non-sRGB target. + config.format = caps + .formats + .iter() + .copied() + .find(|f| !f.is_srgb()) + .unwrap_or(config.format); + config.view_formats = vec![config.format.remove_srgb_suffix()]; + // Screenshots copy the presented frame back from the swapchain. + if caps.usages.contains(wgpu::TextureUsages::COPY_SRC) { + config.usage |= wgpu::TextureUsages::COPY_SRC; + } + config.present_mode = if caps.present_modes.contains(&wgpu::PresentMode::Mailbox) { + wgpu::PresentMode::Mailbox + } else { + wgpu::PresentMode::Fifo + }; + surface.configure(&device, &config); + (config, caps.present_modes.clone()) } else { - wgpu::PresentMode::Fifo + let format = wgpu::TextureFormat::Bgra8Unorm; + let config = wgpu::SurfaceConfiguration { + usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC, + format, + width: size.width.max(1), + height: size.height.max(1), + present_mode: wgpu::PresentMode::Immediate, + desired_maximum_frame_latency: 2, + alpha_mode: wgpu::CompositeAlphaMode::Opaque, + view_formats: vec![format], + color_space: wgpu::SurfaceColorSpace::default(), + }; + (config, Vec::new()) }; - surface.configure(&device, &config); + let offscreen = surface + .is_none() + .then(|| Self::offscreen_target(&device, &config)); + let gpu_timer = timing::GpuTimer::new(&device, &queue); let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("Horizon terrain"), source: wgpu::ShaderSource::Wgsl(include_str!("terrain.wgsl").into()), @@ -1044,6 +1088,12 @@ impl Renderer { Ok(Self { instance, surface, + offscreen, + in_flight: std::collections::VecDeque::new(), + gpu_timer, + gpu_times: Vec::new(), + last_wait: std::time::Duration::ZERO, + last_encode: std::time::Duration::ZERO, window, device, queue, @@ -1110,6 +1160,29 @@ impl Renderer { usage: wgpu::BufferUsages::INDEX, }) } + fn offscreen_target( + device: &wgpu::Device, + config: &wgpu::SurfaceConfiguration, + ) -> wgpu::Texture { + device.create_texture(&wgpu::TextureDescriptor { + label: Some("Offscreen frame"), + size: wgpu::Extent3d { + width: config.width, + height: config.height, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: config.format, + usage: config.usage, + view_formats: &config.view_formats, + }) + } + /// Rendering to a texture instead of the window (`--offscreen`). + pub fn is_offscreen(&self) -> bool { + self.surface.is_none() + } fn depth(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) -> wgpu::TextureView { device .create_texture(&wgpu::TextureDescriptor { @@ -1147,9 +1220,11 @@ impl Renderer { Some(PresentMode::Immediate) => wgpu::PresentMode::Immediate, _ => wgpu::PresentMode::Fifo, }; - if self.config.present_mode != mode { + if self.config.present_mode != mode + && let Some(surface) = &self.surface + { self.config.present_mode = mode; - self.surface.configure(&self.device, &self.config); + surface.configure(&self.device, &self.config); } } pub fn resize(&mut self, width: u32, height: u32) { @@ -1159,7 +1234,11 @@ impl Renderer { } self.config.width = width; self.config.height = height; - self.surface.configure(&self.device, &self.config); + if let Some(surface) = &self.surface { + surface.configure(&self.device, &self.config); + } else { + self.offscreen = Some(Self::offscreen_target(&self.device, &self.config)); + } self.depth = Self::depth(&self.device, &self.config); } pub fn section_count(&self) -> usize { @@ -1387,25 +1466,37 @@ impl Renderer { if self.suspended { return Ok(false); } - let (surface_frame, reconfigure) = match self.surface.get_current_texture() { - wgpu::CurrentSurfaceTexture::Success(f) => (f, false), - wgpu::CurrentSurfaceTexture::Suboptimal(f) => (f, true), - wgpu::CurrentSurfaceTexture::Timeout | wgpu::CurrentSurfaceTexture::Occluded => { + let started = Instant::now(); + let acquire = Instant::now(); + let acquired = self.surface.as_ref().map(|s| s.get_current_texture()); + self.last_wait = acquire.elapsed(); + let (surface_frame, reconfigure) = match acquired { + None => (None, false), + Some(wgpu::CurrentSurfaceTexture::Success(f)) => (Some(f), false), + Some(wgpu::CurrentSurfaceTexture::Suboptimal(f)) => (Some(f), true), + Some(wgpu::CurrentSurfaceTexture::Timeout | wgpu::CurrentSurfaceTexture::Occluded) => { return Ok(false); } - wgpu::CurrentSurfaceTexture::Outdated => { - self.surface.configure(&self.device, &self.config); + Some(wgpu::CurrentSurfaceTexture::Outdated) => { + if let Some(surface) = &self.surface { + surface.configure(&self.device, &self.config); + } return Ok(false); } - wgpu::CurrentSurfaceTexture::Lost => { - self.surface = self.instance.create_surface(self.window.clone())?; - self.surface.configure(&self.device, &self.config); + Some(wgpu::CurrentSurfaceTexture::Lost) => { + let surface = self.instance.create_surface(self.window.clone())?; + surface.configure(&self.device, &self.config); + self.surface = Some(surface); return Ok(false); } - wgpu::CurrentSurfaceTexture::Validation => { + Some(wgpu::CurrentSurfaceTexture::Validation) => { return Err("surface acquisition failed validation".into()); } }; + let target_texture = match &surface_frame { + Some(f) => f.texture.clone(), + None => self.offscreen.clone().ok_or("no offscreen target")?, + }; self.animate(frame.ticks); self.update_lightmap( &frame.sky, @@ -1650,9 +1741,7 @@ impl Renderer { self.queue .write_buffer(&self.border_uniform, 0, bytemuck::bytes_of(&uniform)); } - let view_target = surface_frame - .texture - .create_view(&wgpu::TextureViewDescriptor { + let view_target = target_texture.create_view(&wgpu::TextureViewDescriptor { format: Some(self.config.format.remove_srgb_suffix()), ..Default::default() }); @@ -1661,6 +1750,9 @@ impl Renderer { .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("Horizon frame"), }); + if let Some(timer) = &mut self.gpu_timer { + timer.begin(&mut encoder); + } { let attachments = [Some(wgpu::RenderPassColorAttachment { view: &view_target, @@ -1861,22 +1953,44 @@ impl Renderer { frame.hud, [self.config.width, self.config.height], ); + if let Some(timer) = &mut self.gpu_timer { + timer.end(&mut encoder); + } let readback = self .screenshot .take() - .map(|path| (path, self.copy_frame(&mut encoder, &surface_frame.texture))); - self.queue.submit([encoder.finish()]); + .map(|path| (path, self.copy_frame(&mut encoder, &target_texture))); + let submission = self.queue.submit([encoder.finish()]); + if let Some(timer) = &mut self.gpu_timer { + timer.submitted(); + timer.collect(&self.device, &mut self.gpu_times); + } if let Some((path, buffer)) = readback { self.screenshot_result = Some( self.write_screenshot(&path, &buffer) .map_err(|e| e.to_string()), ); } - self.window.pre_present_notify(); - self.queue.present(surface_frame); - if reconfigure { - self.surface.configure(&self.device, &self.config); + let present = Instant::now(); + if let Some(surface_frame) = surface_frame { + self.window.pre_present_notify(); + self.queue.present(surface_frame); + if reconfigure && let Some(surface) = &self.surface { + surface.configure(&self.device, &self.config); + } + } else { + // No swapchain paces offscreen frames: keep two on the GPU at most. + self.in_flight.push_back(submission); + while self.in_flight.len() > 2 { + let index = self.in_flight.pop_front(); + let _ = self.device.poll(wgpu::PollType::Wait { + submission_index: index, + timeout: Some(std::time::Duration::from_secs(5)), + }); + } } + self.last_wait += present.elapsed(); + self.last_encode = started.elapsed().saturating_sub(self.last_wait); let _ = self.started; Ok(true) } diff --git a/crates/horizon-client/src/gfx/timing.rs b/crates/horizon-client/src/gfx/timing.rs new file mode 100644 index 000000000..90e69d08c --- /dev/null +++ b/crates/horizon-client/src/gfx/timing.rs @@ -0,0 +1,122 @@ +//! GPU frame time from timestamp queries, read back a few frames late so the +//! CPU never waits for it. Frames whose readback slot is still busy go untimed. +use std::sync::{ + Arc, + atomic::{AtomicU8, Ordering}, +}; + +const SLOTS: usize = 4; +const FREE: u8 = 0; +const PENDING: u8 = 1; +const MAPPED: u8 = 2; + +struct Slot { + buffer: wgpu::Buffer, + state: Arc, +} + +pub struct GpuTimer { + queries: wgpu::QuerySet, + resolve: wgpu::Buffer, + slots: Vec, + /// The slot this frame writes into, when one was free at `begin`. + current: Option, + next: usize, + period_ns: f32, +} + +impl GpuTimer { + /// The features timing needs; `None` when the adapter lacks them. + pub fn features(adapter: &wgpu::Adapter) -> wgpu::Features { + let wanted = + wgpu::Features::TIMESTAMP_QUERY | wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS; + if adapter.features().contains(wanted) { + wanted + } else { + wgpu::Features::empty() + } + } + pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Option { + if !device + .features() + .contains(wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS) + { + return None; + } + let queries = device.create_query_set(&wgpu::QuerySetDescriptor { + label: Some("Frame timestamps"), + ty: wgpu::QueryType::Timestamp, + count: 2, + }); + let resolve = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Frame timestamp resolve"), + size: 16, + usage: wgpu::BufferUsages::QUERY_RESOLVE | wgpu::BufferUsages::COPY_SRC, + mapped_at_creation: false, + }); + let slots = (0..SLOTS) + .map(|_| Slot { + buffer: device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Frame timestamp readback"), + size: 16, + usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }), + state: Arc::new(AtomicU8::new(FREE)), + }) + .collect(); + Some(Self { + queries, + resolve, + slots, + current: None, + next: 0, + period_ns: queue.get_timestamp_period(), + }) + } + pub fn begin(&mut self, encoder: &mut wgpu::CommandEncoder) { + let slot = self.next; + self.current = (self.slots[slot].state.load(Ordering::Acquire) == FREE).then_some(slot); + if self.current.is_some() { + encoder.write_timestamp(&self.queries, 0); + } + } + pub fn end(&mut self, encoder: &mut wgpu::CommandEncoder) { + let Some(slot) = self.current else { return }; + encoder.write_timestamp(&self.queries, 1); + encoder.resolve_query_set(&self.queries, 0..2, &self.resolve, 0); + encoder.copy_buffer_to_buffer(&self.resolve, 0, &self.slots[slot].buffer, 0, 16); + } + /// After the frame's submission: start reading its timestamps back. + pub fn submitted(&mut self) { + let Some(slot) = self.current.take() else { + return; + }; + let state = self.slots[slot].state.clone(); + state.store(PENDING, Ordering::Release); + self.slots[slot] + .buffer + .slice(..) + .map_async(wgpu::MapMode::Read, move |result| { + state.store(if result.is_ok() { MAPPED } else { FREE }, Ordering::Release); + }); + self.next = (slot + 1) % SLOTS; + } + /// GPU milliseconds of frames whose timestamps have arrived, oldest first. + pub fn collect(&mut self, device: &wgpu::Device, out: &mut Vec) { + let _ = device.poll(wgpu::PollType::Poll); + for i in 0..SLOTS { + let slot = &self.slots[(self.next + i) % SLOTS]; + if slot.state.load(Ordering::Acquire) != MAPPED { + continue; + } + if let Ok(data) = slot.buffer.slice(..).get_mapped_range() { + let stamps: &[u64] = bytemuck::cast_slice(&data); + let ticks = stamps[1].saturating_sub(stamps[0]); + out.push((ticks as f64 * f64::from(self.period_ns) / 1e6) as f32); + } + slot.buffer.unmap(); + slot.state.store(FREE, Ordering::Release); + } + } +} diff --git a/tools/graphics_bench.py b/tools/graphics_bench.py new file mode 100644 index 000000000..d7bf6379a --- /dev/null +++ b/tools/graphics_bench.py @@ -0,0 +1,163 @@ +#!/usr/bin/env python3 +"""Repeatable graphics benchmark for the native client. + +Opens a fresh seeded creative world on a virtual screen (vscreen) and flies the +camera along fixed straight paths over a village, a forest, the ocean and a +mountain, at render distances 8, 16 and 32. Each flight replies with frame +times (average FPS, 1% lows, percentiles), the CPU part of each frame and the +GPU time from timestamp queries. + + python3 tools/graphics_bench.py --label baseline + python3 tools/graphics_bench.py --label baseline --offscreen # the discrete GPU + +`--offscreen` renders into a texture instead of the window, so the desktop's +GPU can be measured even though the headless compositor (vscreen) only +presents software-rendered (lavapipe) frames. Without it, frame times on +vscreen are lavapipe's: use them for CPU-side numbers. +""" +import argparse +import datetime +import json +import os +import shutil +import subprocess +import sys +import time +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +SEED = "1234" +WORLD = "GraphicsBench" +# (name, from xyz, to xyz, pitch): straight flights at a constant speed over +# fixed scenery of seed 1234. Each path is 192 blocks long. +SCENES = [ + ("village", (-960.0, 125.0, 80.0), (-960.0, 125.0, 272.0), 35.0), + ("forest", (-1100.0, 120.0, -560.0), (-1100.0, 120.0, -368.0), 25.0), + ("ocean", (-300.0, 85.0, -420.0), (-300.0, 85.0, -228.0), 10.0), + ("mountain", (560.0, 260.0, 860.0), (752.0, 260.0, 860.0), 30.0), +] +OPTIONS = """version:4790 +enableVsync:false +maxFps:260 +renderDistance:{distance} +simulationDistance:12 +fov:0.0 +guiScale:0 +""" + + +def vscreen_env(): + out = subprocess.run(["vscreen", "env"], cwd=ROOT, capture_output=True, text=True, check=True) + env = dict(os.environ) + for part in out.stdout.replace(";", " ").split(): + if "=" in part and not part.startswith("export"): + k, v = part.split("=", 1) + env[k] = v + env.pop("DISPLAY", None) + return env + + +class Game: + def __init__(self, ctl, socket, env, cwd): + self.ctl, self.socket, self.env, self.cwd = ctl, socket, env, cwd + + def run(self, *words, check=True): + out = subprocess.run( + [str(self.ctl), "--socket", str(self.socket), *map(str, words)], + cwd=self.cwd, env=self.env, capture_output=True, text=True, + ) + line = out.stdout.strip().splitlines()[-1] if out.stdout.strip() else "{}" + reply = json.loads(line) if line.startswith("{") else {"error": out.stderr.strip()} + if check and "error" in reply: + raise RuntimeError(f"{' '.join(map(str, words))}: {reply} {out.stderr.strip()}") + return reply + + +def run_distance(args, ctl, env, cwd, distance): + (cwd / "options.txt").write_text(OPTIONS.format(distance=distance)) + socket = cwd / "control.sock" + game = Game(ctl, socket, env, cwd) + launch = [ + "launch", "--world", WORLD, "--seed", SEED, "--creative", + "--render-distance", distance, "--resources", args.resources, + "--no-pause", "--weather", "clear", "--time", "6000", + ] + if args.offscreen: + launch.append("--offscreen") + game.run(*launch) + results = {} + try: + game.run("wait-ready", 300) + status = game.run("status") + results["renderer"] = status.get("renderer") + for name, start, end, pitch in SCENES: + if args.scenes and name not in args.scenes.split(","): + continue + game.run("tp", *start) + game.run("look", 0, pitch) + game.run("time", 6000) + game.run("wait-ready", 300) + time.sleep(args.settle) + summary = game.run("fly", *start, *end, args.seconds, pitch) + results[name] = summary + print( + f" rd {distance:>2} {name:<9} {summary.get('avg_fps', 0):7.1f} fps" + f" 1% low {summary.get('low_1pct_fps', 0):7.1f}" + f" p99 {summary.get('p99_ms', 0):6.2f} ms" + f" cpu {((summary.get('cpu') or {}).get('avg_ms') or 0):6.2f} ms" + f" gpu {((summary.get('gpu') or {}).get('avg_ms') or 0):6.2f} ms" + + " [" + " ".join(f"{k} {v:.2f}" for k, v in (summary.get("phases_avg_ms") or {}).items()) + "]", + flush=True, + ) + finally: + game.run("quit", check=False) + for _ in range(100): + if not socket.exists(): + break + time.sleep(0.2) + return results + + +def main(): + p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + p.add_argument("--label", default="run") + p.add_argument("--distances", default="8,16,32") + p.add_argument("--scenes", help="comma-separated subset of " + ",".join(s[0] for s in SCENES)) + p.add_argument("--seconds", type=float, default=12.0, help="flight duration per scene") + p.add_argument("--settle", type=float, default=2.0, help="seconds to wait before each flight") + p.add_argument("--offscreen", action="store_true", help="render offscreen on the discrete GPU") + p.add_argument("--resources", default=str(ROOT / ".local/horizon-client/26.3")) + p.add_argument("--build", default=str(ROOT / "target/release")) + p.add_argument("--out", help="JSON file (default benchmarks/graphics/