upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorNico <nicolas.tassin23@gmail.com>2026-11-24 19:45:45 +0100
committerNico <nicolas.tassin23@gmail.com>2026-11-24 19:45:45 +0100
commit370e52b8cbc7dadfaecd3bf00e6fdc919d4244dc (patch)
treecda32619c527f49e6198a7f6327dd28aa784c685 /runtime/src
parent9c6cbbcf12757aa012da8064a6750d482826668a (diff)
parent6293c1c4f88c7ea2088ec595fea51362f1759996 (diff)
Merge branch 'main' of https://github.com/ubiquitarianism/leonida into xapu-connect-layouts
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/frame_pacer.h100
-rw-r--r--runtime/src/gfx10_descriptors.h6
-rw-r--r--runtime/src/retire_tickets.h16
-rw-r--r--runtime/src/vulkan_backend.h5
-rw-r--r--runtime/src/vulkan_replay.h17
5 files changed, 133 insertions, 11 deletions
diff --git a/runtime/src/frame_pacer.h b/runtime/src/frame_pacer.h
index 38f7860c1..afeaf191e 100644
--- a/runtime/src/frame_pacer.h
+++ b/runtime/src/frame_pacer.h
@@ -30,14 +30,17 @@
30// suspended) is unchanged. 30// suspended) is unchanged.
31 31
32#include <algorithm> 32#include <algorithm>
33#include <array>
33#include <atomic> 34#include <atomic>
34#include <chrono> 35#include <chrono>
35#include <condition_variable> 36#include <condition_variable>
37#include <cstddef>
36#include <cstdint> 38#include <cstdint>
37#include <deque> 39#include <deque>
38#include <functional> 40#include <functional>
39#include <limits> 41#include <limits>
40#include <mutex> 42#include <mutex>
43#include <vector>
41 44
42namespace leonida { 45namespace leonida {
43 46
@@ -149,6 +152,25 @@ struct FrameStatistic {
149 float percent_scanned = 0.0f; // display entries: where the scan was when the frame came up (0 at a vblank) 152 float percent_scanned = 0.0f; // display entries: where the scan was when the frame came up (0 at a vblank)
150}; 153};
151 154
155// Fixed-capacity ring buffer size for recent frame-time and origin-to-present latency samples (BL-6).
156inline constexpr std::size_t frame_pacing_window = 1024;
157
158// Summary over the most recent `frame_pacing_window` presented frames (p50/p95/p99 and mean drift against target).
159struct FramePacingSummary {
160 std::uint64_t frames = 0; // total presented frames recorded
161 std::uint64_t samples = 0; // present-to-present intervals in the ring buffer (<= frame_pacing_window)
162 std::uint32_t target_interval_us = 0; // active target interval in us (0 when unpaced or uncapped)
163 double mean_us = 0.0; // mean present-to-present interval over `samples`
164 double drift_us = 0.0; // mean_us - target_interval_us (0 when unpaced/uncapped or samples == 0)
165 std::uint64_t p50_us = 0; // nearest-rank 50th percentile frame interval in us
166 std::uint64_t p95_us = 0; // nearest-rank 95th percentile frame interval in us
167 std::uint64_t p99_us = 0; // nearest-rank 99th percentile frame interval in us
168 std::uint64_t latency_samples = 0; // origin-to-present latency samples in the ring buffer
169 std::uint64_t latency_p50_us = 0; // nearest-rank 50th percentile origin-to-present latency in us
170 std::uint64_t latency_p95_us = 0; // nearest-rank 95th percentile origin-to-present latency in us
171 std::uint64_t latency_p99_us = 0; // nearest-rank 99th percentile origin-to-present latency in us
172};
173
152class FramePacer { 174class FramePacer {
153public: 175public:
154 explicit FramePacer(FrameClock& clock) : clock_(clock) { 176 explicit FramePacer(FrameClock& clock) : clock_(clock) {
@@ -311,6 +333,7 @@ public:
311 std::lock_guard guard(mutex_); 333 std::lock_guard guard(mutex_);
312 const std::uint64_t now = clock_.now_us(); 334 const std::uint64_t now = clock_.now_us();
313 retire_locked(now); 335 retire_locked(now);
336 record_present_timing_locked(now, token);
314 last_presented_ = std::max(last_presented_, token); 337 last_presented_ = std::max(last_presented_, token);
315 if (!interval_set_ || !any_armed_locked() || uncapped_) { // not paced or uncapped: shows at once 338 if (!interval_set_ || !any_armed_locked() || uncapped_) { // not paced or uncapped: shows at once
316 for (Record& r : records_) 339 for (Record& r : records_)
@@ -340,8 +363,9 @@ public:
340 // the origin event not armed or the cap off it is not paced: it shows at once and is not counted. 363 // the origin event not armed or the cap off it is not paced: it shows at once and is not counted.
341 void note_untokened_present() { 364 void note_untokened_present() {
342 std::lock_guard guard(mutex_); 365 std::lock_guard guard(mutex_);
343 if (!interval_set_ || !any_armed_locked() || uncapped_) return;
344 const std::uint64_t now = clock_.now_us(); 366 const std::uint64_t now = clock_.now_us();
367 record_present_timing_locked(now, 0);
368 if (!interval_set_ || !any_armed_locked() || uncapped_) return;
345 retire_locked(now); 369 retire_locked(now);
346 const std::uint64_t since = now > epoch_us_ ? now - epoch_us_ : 0; 370 const std::uint64_t since = now > epoch_us_ ? now - epoch_us_ : 0;
347 std::uint64_t flip = epoch_us_ + (since / period_us_ + 1) * period_us_; 371 std::uint64_t flip = epoch_us_ + (since / period_us_ + 1) * period_us_;
@@ -355,6 +379,7 @@ public:
355 std::lock_guard guard(mutex_); 379 std::lock_guard guard(mutex_);
356 suspended_ = true; 380 suspended_ = true;
357 interval_set_ = false; 381 interval_set_ = false;
382 has_last_present_ = false; // do not count the suspended pause as a frame interval after resume
358 for (bool& a : event_armed_) a = false; 383 for (bool& a : event_armed_) a = false;
359 pending_.clear(); 384 pending_.clear();
360 cv_.notify_all(); 385 cv_.notify_all();
@@ -385,6 +410,47 @@ public:
385 void set_uncapped(bool on) { std::lock_guard guard(mutex_); uncapped_ = on; } 410 void set_uncapped(bool on) { std::lock_guard guard(mutex_); uncapped_ = on; }
386 bool uncapped() const { std::lock_guard guard(mutex_); return uncapped_; } 411 bool uncapped() const { std::lock_guard guard(mutex_); return uncapped_; }
387 412
413 // Frame-time percentiles, mean drift against the active target interval, and origin-to-present latency over
414 // the last `frame_pacing_window` presented frames (BL-6).
415 FramePacingSummary pacing_summary() const {
416 FramePacingSummary out;
417 std::vector<std::uint64_t> intervals;
418 std::vector<std::uint64_t> latencies;
419 {
420 std::lock_guard guard(mutex_);
421 out.frames = presents_recorded_;
422 out.target_interval_us = (interval_set_ && !uncapped_) ? period_us_ : 0u;
423 intervals.reserve(interval_count_);
424 for (std::size_t i = 0; i < interval_count_; ++i) intervals.push_back(interval_ring_[i]);
425 latencies.reserve(latency_count_);
426 for (std::size_t i = 0; i < latency_count_; ++i) latencies.push_back(latency_ring_[i]);
427 }
428 auto percentile = [](const std::vector<std::uint64_t>& sorted, std::size_t pct) -> std::uint64_t {
429 if (sorted.empty()) return 0;
430 const std::size_t idx = (pct * sorted.size() + 99) / 100 - 1;
431 return sorted[std::min(idx, sorted.size() - 1)];
432 };
433 if (!intervals.empty()) {
434 out.samples = intervals.size();
435 double sum = 0.0;
436 for (std::uint64_t v : intervals) sum += double(v);
437 out.mean_us = sum / double(intervals.size());
438 if (out.target_interval_us != 0) out.drift_us = out.mean_us - double(out.target_interval_us);
439 std::sort(intervals.begin(), intervals.end());
440 out.p50_us = percentile(intervals, 50);
441 out.p95_us = percentile(intervals, 95);
442 out.p99_us = percentile(intervals, 99);
443 }
444 if (!latencies.empty()) {
445 out.latency_samples = latencies.size();
446 std::sort(latencies.begin(), latencies.end());
447 out.latency_p50_us = percentile(latencies, 50);
448 out.latency_p95_us = percentile(latencies, 95);
449 out.latency_p99_us = percentile(latencies, 99);
450 }
451 return out;
452 }
453
388private: 454private:
389 struct Pending { 455 struct Pending {
390 std::uint64_t token; 456 std::uint64_t token;
@@ -398,6 +464,29 @@ private:
398 bool presented = false, displayed = false; 464 bool presented = false, displayed = false;
399 }; 465 };
400 466
467 void record_present_timing_locked(std::uint64_t now, std::uint64_t token) {
468 ++presents_recorded_;
469 if (has_last_present_ && now >= last_present_us_) {
470 interval_ring_[interval_head_] = now - last_present_us_;
471 interval_head_ = (interval_head_ + 1) % frame_pacing_window;
472 if (interval_count_ < frame_pacing_window) ++interval_count_;
473 }
474 has_last_present_ = true;
475 last_present_us_ = now;
476 if (token != 0) {
477 for (const Record& r : records_) {
478 if (r.token == token) {
479 if (now >= r.origin_us) {
480 latency_ring_[latency_head_] = now - r.origin_us;
481 latency_head_ = (latency_head_ + 1) % frame_pacing_window;
482 if (latency_count_ < frame_pacing_window) ++latency_count_;
483 }
484 break;
485 }
486 }
487 }
488 }
489
401 // Frames whose vblank has come are shown. 490 // Frames whose vblank has come are shown.
402 void retire_locked(std::uint64_t now) { 491 void retire_locked(std::uint64_t now) {
403 while (!pending_.empty() && pending_.front().flip_us <= now) { 492 while (!pending_.empty() && pending_.front().flip_us <= now) {
@@ -494,6 +583,15 @@ private:
494 std::uint64_t issued_ = 0; 583 std::uint64_t issued_ = 0;
495 std::uint64_t last_presented_ = 0; 584 std::uint64_t last_presented_ = 0;
496 std::uint64_t immediate_presents_ = 0; 585 std::uint64_t immediate_presents_ = 0;
586 std::uint64_t presents_recorded_ = 0;
587 bool has_last_present_ = false;
588 std::uint64_t last_present_us_ = 0;
589 std::array<std::uint64_t, frame_pacing_window> interval_ring_{};
590 std::size_t interval_head_ = 0;
591 std::size_t interval_count_ = 0;
592 std::array<std::uint64_t, frame_pacing_window> latency_ring_{};
593 std::size_t latency_head_ = 0;
594 std::size_t latency_count_ = 0;
497 std::deque<Pending> pending_; 595 std::deque<Pending> pending_;
498 std::deque<Record> records_; 596 std::deque<Record> records_;
499}; 597};
diff --git a/runtime/src/gfx10_descriptors.h b/runtime/src/gfx10_descriptors.h
index b225cddef..a4a9b4448 100644
--- a/runtime/src/gfx10_descriptors.h
+++ b/runtime/src/gfx10_descriptors.h
@@ -631,8 +631,9 @@ inline VkSamplerCreateInfo vulkan_sampler(const Sampler& sampler, float max_lod_
631 if (sampler.unnormalized) { 631 if (sampler.unnormalized) {
632 info.unnormalizedCoordinates = VK_TRUE; 632 info.unnormalizedCoordinates = VK_TRUE;
633 // VUID-VkSamplerCreateInfo-unnormalizedCoordinates-01075: U and V clamp to the edge or the border (a 633 // VUID-VkSamplerCreateInfo-unnormalizedCoordinates-01075: U and V clamp to the edge or the border (a
634 // repeating mode would wrap at a texel count the sampler cannot know); -01072: one filter for min and mag. 634 // repeating mode would wrap at a texel count the sampler cannot know); -01072: one filter for min and mag;
635 // The S# allows any address mode with FORCE_UNNORMALIZED, and WRAP is its default (word 0 = 0). 635 // -01074: mipLodBias must be zero. The S# allows any address mode or LOD bias with FORCE_UNNORMALIZED, and
636 // WRAP is its default (word 0 = 0).
636 const auto clamp_or_border = [](VkSamplerAddressMode m) { 637 const auto clamp_or_border = [](VkSamplerAddressMode m) {
637 return m == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ? m : VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; 638 return m == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ? m : VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
638 }; 639 };
@@ -640,6 +641,7 @@ inline VkSamplerCreateInfo vulkan_sampler(const Sampler& sampler, float max_lod_
640 info.addressModeV = clamp_or_border(info.addressModeV); 641 info.addressModeV = clamp_or_border(info.addressModeV);
641 info.minFilter = info.magFilter; 642 info.minFilter = info.magFilter;
642 info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST; 643 info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
644 info.mipLodBias = 0.0f;
643 info.minLod = info.maxLod = 0.0f; 645 info.minLod = info.maxLod = 0.0f;
644 info.anisotropyEnable = VK_FALSE; 646 info.anisotropyEnable = VK_FALSE;
645 info.maxAnisotropy = 1.0f; 647 info.maxAnisotropy = 1.0f;
diff --git a/runtime/src/retire_tickets.h b/runtime/src/retire_tickets.h
index a48302859..675460cac 100644
--- a/runtime/src/retire_tickets.h
+++ b/runtime/src/retire_tickets.h
@@ -24,7 +24,7 @@ public:
24 while (!pending_.empty()) { 24 while (!pending_.empty()) {
25 const Entry& front = pending_.front(); 25 const Entry& front = pending_.front();
26 if (!front.idle && !query(front.semaphore, front.value)) break; 26 if (!front.idle && !query(front.semaphore, front.value)) break;
27 completed_ = front.ticket; 27 if (front.ticket > completed_) completed_ = front.ticket;
28 pending_.pop_front(); 28 pending_.pop_front();
29 } 29 }
30 return completed_; 30 return completed_;
@@ -34,8 +34,18 @@ public:
34 // that must have them done (and may not hold the lock that guards this object while it waits) waits for. 34 // that must have them done (and may not hold the lock that guards this object while it waits) waits for.
35 std::vector<std::pair<Semaphore, std::uint64_t>> pending_through(std::uint64_t ticket) const { 35 std::vector<std::pair<Semaphore, std::uint64_t>> pending_through(std::uint64_t ticket) const {
36 std::vector<std::pair<Semaphore, std::uint64_t>> out; 36 std::vector<std::pair<Semaphore, std::uint64_t>> out;
37 for (const Entry& entry : pending_) 37 for (const Entry& entry : pending_) {
38 if (entry.ticket <= ticket && !entry.idle) out.emplace_back(entry.semaphore, entry.value); 38 if (entry.ticket > ticket || entry.idle) continue;
39 bool merged = false;
40 for (auto& [sem, val] : out) {
41 if (sem == entry.semaphore) {
42 if (entry.value > val) val = entry.value;
43 merged = true;
44 break;
45 }
46 }
47 if (!merged) out.emplace_back(entry.semaphore, entry.value);
48 }
39 return out; 49 return out;
40 } 50 }
41 51
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index a95994256..f4373787b 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -1285,6 +1285,11 @@ public:
1285 replay_.zero_size = size; 1285 replay_.zero_size = size;
1286 } 1286 }
1287 1287
1288 void set_depth_range_unrestricted(bool enabled) {
1289 std::unique_lock lock(resource_mutex_);
1290 replay_.depth_range_unrestricted = enabled;
1291 }
1292
1288 // Physical-device COLOR_ATTACHMENT format check for reinterpreted UAV clears. 1293 // Physical-device COLOR_ATTACHMENT format check for reinterpreted UAV clears.
1289 void set_color_attachment_format(std::function<bool(VkFormat)> check) { 1294 void set_color_attachment_format(std::function<bool(VkFormat)> check) {
1290 std::unique_lock lock(resource_mutex_); 1295 std::unique_lock lock(resource_mutex_);
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h
index d5a73b909..bcaea02f8 100644
--- a/runtime/src/vulkan_replay.h
+++ b/runtime/src/vulkan_replay.h
@@ -216,7 +216,10 @@ inline VkPipelineStageFlags2 vulkan_sync(std::uint32_t sync) {
216 VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT}, 216 VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT},
217 {y::render_target, VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT}, 217 {y::render_target, VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT},
218 {y::compute_shading, VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT}, 218 {y::compute_shading, VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT},
219 {y::copy, VK_PIPELINE_STAGE_2_COPY_BIT}, 219 // CLEAR as well: two D3D12 copy-scope commands are recorded as Vulkan clear commands, WriteBufferImmediate
220 // as vkCmdUpdateBuffer and ResolveQueryData's never-written queries as vkCmdFillBuffer, and synchronization2
221 // puts both in the CLEAR stage, not COPY.
222 {y::copy, VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_CLEAR_BIT},
220 {y::resolve, VK_PIPELINE_STAGE_2_RESOLVE_BIT}, 223 {y::resolve, VK_PIPELINE_STAGE_2_RESOLVE_BIT},
221 {y::execute_indirect, VK_PIPELINE_STAGE_2_DRAW_INDIRECT_BIT}, 224 {y::execute_indirect, VK_PIPELINE_STAGE_2_DRAW_INDIRECT_BIT},
222 {y::all_shading, pre_raster | VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT}, 225 {y::all_shading, pre_raster | VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT},
@@ -814,6 +817,7 @@ struct ReplayFunctions {
814 PFN_vkCmdPipelineBarrier pipeline_barrier = nullptr; 817 PFN_vkCmdPipelineBarrier pipeline_barrier = nullptr;
815 PFN_vkCmdClearColorImage clear_color_image = nullptr; 818 PFN_vkCmdClearColorImage clear_color_image = nullptr;
816 PFN_vkCmdClearDepthStencilImage clear_depth_stencil_image = nullptr; 819 PFN_vkCmdClearDepthStencilImage clear_depth_stencil_image = nullptr;
820 bool depth_range_unrestricted = false; // Allows out-of-range float depth clear values.
817 // Optional; a list that needs a missing one is rejected with ReplayResult::unsupported. 821 // Optional; a list that needs a missing one is rejected with ReplayResult::unsupported.
818 PFN_vkCmdPipelineBarrier2 pipeline_barrier2 = nullptr; // enhanced barriers 822 PFN_vkCmdPipelineBarrier2 pipeline_barrier2 = nullptr; // enhanced barriers
819 VkPipelineStageFlags2 queue_stages = 0; 823 VkPipelineStageFlags2 queue_stages = 0;
@@ -3682,10 +3686,13 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
3682 const VulkanAccess state = vulkan_access(d3d12_state::depth_write, false); 3686 const VulkanAccess state = vulkan_access(d3d12_state::depth_write, false);
3683 auto pieces = detail::current(tracker, id, range, image_layout(p.binding, state.layout)); 3687 auto pieces = detail::current(tracker, id, range, image_layout(p.binding, state.layout));
3684 detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker); 3688 detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker);
3685 const float depth = p.binding.depth24 ? depth24_clear_value(depth_clear->depth) // see depth24.h 3689 float depth = p.binding.depth24 ? depth24_clear_value(depth_clear->depth) // see depth24.h
3686 : p.binding.format == VK_FORMAT_D16_UNORM 3690 : p.binding.format == VK_FORMAT_D16_UNORM
3687 ? detail::quantize_unorm(depth_clear->depth, 65535.0) // see quantize_unorm 3691 ? detail::quantize_unorm(depth_clear->depth, 65535.0) // see quantize_unorm
3688 : depth_clear->depth; 3692 : depth_clear->depth;
3693 // Without the extension both clear commands require a depth in [0, 1], including for float images.
3694 if (!vk.depth_range_unrestricted)
3695 depth = std::isnan(depth) ? 0.0f : std::clamp(depth, 0.0f, 1.0f);
3689 const VkClearDepthStencilValue value{depth, depth_clear->stencil}; 3696 const VkClearDepthStencilValue value{depth, depth_clear->stencil};
3690 if (p.rects.empty()) { 3697 if (p.rects.empty()) {
3691 detail::round_trip(vk, command_buffer, p.binding, pieces, image_layout(p.binding, transfer_dst.layout), 3698 detail::round_trip(vk, command_buffer, p.binding, pieces, image_layout(p.binding, transfer_dst.layout),