From 7c9918994d1087ac856eb1c346b618b33755fba6 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Tue, 17 Nov 2026 21:19:45 -0800 Subject: [PATCH] rt render: a state keys the device's map of contents on its descriptor's key, as a compute state does A seen render content cost 220.9 us a creation (harness 44254, create_render_state, p50 at 80b8705d4f) against compute's 0.8 us: the map's key was Device::render_variant_key, which decodes and specializes both functions on every creation. Compute keys the same map on Device::compute_pipeline_key, which reads no function. Render now keys it on Device::render_descriptor_key (every field, each function by its compile key under the default inputs; the old runtime's memo key too), and decodes, keys for the compile and lowers only on a miss. The one map stays bounded at 1,024 entries; a device test fills it past a cap of 2 with render contents and checks the gauges and the frees. The cost: two descriptors that differ only in a field their compile does not read compile apart (README, module doc). Verified: cargo fmt; lint, device tests and the harness rows follow as potluck jobs. --- crates/metal2vulkan-rt/README.md | 16 +++--- crates/metal2vulkan-rt/src/kernel_map.rs | 8 +-- crates/metal2vulkan-rt/src/render.rs | 38 ++++++++------ crates/metal2vulkan-rt/src/tests/render.rs | 61 ++++++++++++++++++++++ 4 files changed, 95 insertions(+), 28 deletions(-) diff --git a/crates/metal2vulkan-rt/README.md b/crates/metal2vulkan-rt/README.md index c03166401..247f6649b 100644 --- a/crates/metal2vulkan-rt/README.md +++ b/crates/metal2vulkan-rt/README.md @@ -876,13 +876,15 @@ a miss. With no directory, nothing is kept on disk, and each `VkPipeline` is mad vertex and fragment functions on the calling thread, which waits for them, within its 20-second budget per lowering, for what device creation enabled and under its identity domains, as for a compute state. A refusal of the translator is `Error::Translation`. -- What a state compiles is its content, never the state: its descriptor's functions with their - constant values and linked functions, and the descriptor's fields that change the compile (the - translator's `Device::render_pipeline_key`). Every state of one content shares one compiled - content: its translation, its shader modules and their `VkPipeline`s, and its precision notes. - Render contents and compute kernels share the device's one map of the last 1,024 contents used, - so a state of a content it holds costs the key, which decodes the descriptor's functions, and a - lookup. `Gauges::render_contents` counts the compiled render contents alive. +- What a state compiles is its content, never the state: its descriptor, with its functions' + constant values and linked functions, and every field (the translator's + `Device::render_descriptor_key`). Every state of one content shares one compiled content: its + translation, its shader modules and their `VkPipeline`s, and its precision notes. Render + contents and compute kernels share the device's one map of the last 1,024 contents used, so a + state of a content it holds costs the key, which reads no function, and a lookup. + `Gauges::render_contents` counts the compiled render contents alive, and + `Gauges::kernel_entries` the map's entries, compute and render together. Two descriptors that + differ only in a field their compile does not read are two contents, each compiled. - A `VkPipeline` bakes what Vulkan 1.3 cannot leave dynamic: the content and the topology class (point, line or triangle) of the draws it serves. It takes as dynamic state the viewport, scissor, cull mode, front face, primitive topology within its class, primitive restart, depth diff --git a/crates/metal2vulkan-rt/src/kernel_map.rs b/crates/metal2vulkan-rt/src/kernel_map.rs index 55ed90332..410b25a6c 100644 --- a/crates/metal2vulkan-rt/src/kernel_map.rs +++ b/crates/metal2vulkan-rt/src/kernel_map.rs @@ -22,10 +22,10 @@ pub(crate) const KERNEL_ENTRIES: usize = 1024; /// (`Device::compute_pipeline_key`: the kernel's AIR and entry, its constant values, linked /// functions and dynamic libraries, its lowering inputs, the descriptor's fields that change the /// compile, the device's profile and the translator's semantics version; or -/// `Device::render_pipeline_key`, the same of a render pipeline's functions and fields), and a -/// compute descriptor's `requiredThreadsPerThreadgroup`, which the state answers as its -/// `maxTotalThreadsPerThreadgroup` and the translator's key leaves out (`None` for a render -/// pipeline). +/// `Device::render_descriptor_key`, every field of a render pipeline descriptor with its +/// functions keyed as a kernel is; neither key decodes a function), and a compute descriptor's +/// `requiredThreadsPerThreadgroup`, which the state answers as its `maxTotalThreadsPerThreadgroup` +/// and the translator's key leaves out (`None` for a render pipeline). #[derive(Clone, PartialEq, Eq, Hash)] pub(crate) struct KernelKey { pub(crate) pipeline: PipelineKey, diff --git a/crates/metal2vulkan-rt/src/render.rs b/crates/metal2vulkan-rt/src/render.rs index 60347cd7b..7679c4aaf 100644 --- a/crates/metal2vulkan-rt/src/render.rs +++ b/crates/metal2vulkan-rt/src/render.rs @@ -2,12 +2,16 @@ //! modules, and its `VkPipeline`s, one for each topology class drawn. //! //! What a state compiles is its content, never the state: the translator's key of its descriptor -//! (`Device::render_variant_key`: its functions with their constant values and linked functions, -//! the lowering inputs each function's use in the pipeline gives, the descriptor's fields that -//! change the compile, the device's profile and the translator's semantics version). Every state -//! of one content shares one [`RenderCompiled`]: the translation, its shader modules and their -//! `VkPipeline`s. The device's one map of contents (`kernel_map`), which compute states use too, -//! holds each content's, so a state of a content the map holds costs the key and a lookup. +//! (`Device::render_descriptor_key`: every field of the descriptor, each function by its AIR, +//! entry, constant values, linked functions and dynamic libraries, with the device's profile and +//! the translator's semantics version), which reads no function, as a compute state's key reads +//! none. Every state of one content shares one [`RenderCompiled`]: the translation, its shader +//! modules and their `VkPipeline`s. The device's one map of contents (`kernel_map`), which compute +//! states use too, holds each content's, so a state of a content the map holds costs the key and a +//! lookup. A content the map does not hold is decoded, keyed for its compile +//! (`Device::render_variant_key`) and lowered. The cost of keying on the descriptor: two +//! descriptors that differ only in a field their compile does not read are two contents, each +//! compiled. //! //! What a `VkPipeline` bakes is what Vulkan 1.3 cannot leave dynamic: the content, and the //! topology class (point, line or triangle) of the draws it serves. Everything Metal sets on the @@ -121,11 +125,11 @@ struct Fixed { impl crate::Device { /// `newRenderPipelineStateWithDescriptor:error:`, without reflection. /// - /// A state's content is its descriptor's functions with their constant values and linked - /// functions, and the descriptor's fields that change the compile. Every state of one content - /// shares its translation, its shader modules and their `VkPipeline`s: a state of a content - /// the device holds (the last 1,024 contents used, compute and render together) costs the - /// translator's key of the descriptor, which decodes its functions, and a lookup. For a + /// A state's content is its descriptor: its functions with their constant values and linked + /// functions, and every field. Every state of one content shares its translation, its shader + /// modules and their `VkPipeline`s: a state of a content the device holds (the last 1,024 + /// contents used, compute and render together) costs the translator's key of the descriptor, + /// which reads no function, and a lookup. For a /// content the device does not hold, the translator lowers each function on the calling /// thread, which waits for it, within its 20-second budget per lowering, and the content's /// triangle-class `VkPipeline` is made here. Where the device has a cache directory @@ -150,18 +154,18 @@ impl crate::Device { ) -> Result { refuse(descriptor)?; let core = self.core(); - let budget = Budget::standard(); - let entries = StageEntries::new(); - let keys = (core.translator) - .render_variant_key(descriptor, &VariantKey::new(), &budget, &entries) - .map_err(Error::Translation)?; let key = KernelKey { - pipeline: keys.pipeline, + pipeline: core.translator.render_descriptor_key(descriptor), required: None, }; let compiled = match core.kernels.find_render(&key) { Some(compiled) => compiled, None => { + let budget = Budget::standard(); + let entries = StageEntries::new(); + let keys = (core.translator) + .render_variant_key(descriptor, &VariantKey::new(), &budget, &entries) + .map_err(Error::Translation)?; let compiled = RenderCompiled::of(core, descriptor, &keys, &budget, &entries)?; (core.kernels).keep_render(key, Arc::new(compiled), &core.counters) } diff --git a/crates/metal2vulkan-rt/src/tests/render.rs b/crates/metal2vulkan-rt/src/tests/render.rs index 41905883d..6277eac89 100644 --- a/crates/metal2vulkan-rt/src/tests/render.rs +++ b/crates/metal2vulkan-rt/src/tests/render.rs @@ -6,6 +6,9 @@ //! shares the first's compiled content, which the device's map of contents holds beside its //! compute kernels. Another colour format, a blended attachment, or a stencil attachment with a //! second colour attachment is another content. +//! - Filled past a cap of 2, the map holds 2 render contents and forgets the least recently used, +//! which lives while a state holds it and goes once that state does; a forgotten content +//! compiles again, and one the map holds does not. //! - A depth attachment, more than one sample, or a fragment function with a constexpr sampler is //! refused by kind, making no `VkPipeline`. //! - A device on a cache directory a device before it used lowers nothing at creation: both @@ -152,6 +155,64 @@ mod on_device { }); } + /// Filled past a cap of 2, the map holds 2 render contents and forgets the least recently + /// used, which lives while a state holds it and goes once that state does; a content + /// forgotten compiles again, and one the map holds does not. + #[test] + fn every_device_bounds_its_render_contents_and_frees_a_forgotten_one_with_its_last_state() { + let Some(library) = library() else { + return; + }; + let [one, two, three] = [ + PixelFormat::BGRA8Unorm, + PixelFormat::RGBA8Unorm, + PixelFormat::RGBA16Float, + ] + .map(|color| descriptor(&library, "colored_fragment", color)); + each_device("render_past_cap", |name, _, device| { + device.core().kernels.set_cap(2); + let counters = device.counters(); + let gauges = || { + let gauges = counters.gauges(); + (gauges.render_contents, gauges.kernel_entries) + }; + let lowered = || counters.metrics().translations; + let start = lowered(); + let a = state(name, &device, &one); + let each = lowered() - start; + assert!(each > 0, "{name}: a lowering"); + let b = state(name, &device, &two); + assert_eq!(gauges(), (2, 2), "{name}"); + // `one` is the least recently used, and goes from the map; its state keeps it. + let c = state(name, &device, &three); + assert_eq!( + gauges(), + (3, 2), + "{name}: a forgotten content its state holds" + ); + let b_again = state(name, &device, &two); + assert!(shared(&b, &b_again), "{name}: `two` is held"); + assert_eq!(lowered() - start, 3 * each, "{name}: `two` compiled once"); + drop(a); + assert_eq!( + gauges(), + (2, 2), + "{name}: the forgotten content went with its last state" + ); + // `one` compiles again, and `three`, now the least recently used, goes from the map. + let a = state(name, &device, &one); + assert_eq!(lowered() - start, 4 * each, "{name}: `one` compiled again"); + assert_eq!(gauges(), (3, 2), "{name}"); + drop(c); + assert_eq!(gauges(), (2, 2), "{name}"); + drop((a, b, b_again)); + assert_eq!(gauges(), (2, 2), "{name}: the map holds 2"); + println!( + "{name}: the map held 2 of 3 render contents, and freed a forgotten one with its state" + ); + }); + } + #[test] fn every_device_refuses_what_it_does_not_build_by_kind() { let Some(library) = library() else {