From 158e7be308ec052d7f3ea6379b03dc63edda2b4c Mon Sep 17 00:00:00 2001 From: rponeawa Date: Wed, 11 Nov 2026 23:35:22 +0800 Subject: [PATCH] perf(bridge): lower on the lowering thread, submit at commit, warm pipelines The three work placements are on by default. metal-flow= selects them: 1 the lowering thread, 2 submission at commit, 4 the warm pass over the pipeline records of earlier processes; 0 turns all off. --- NativeAdrenoMetalPlugin.mm | 15 ++++++++++++--- bridge/NativeMetalObjects.rs | 21 +++++++++++++-------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 660198e..be6c45d 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -359,9 +359,18 @@ static id deviceForPort(unsigned port) { // for a comparison on the same image. Read by the bridge at the first such texture. if (bootArgSet("gpu-ycbcr-planes=0")) setenv("NATIVE_METAL_YCBCR_PLANES", "0", 1); else unsetenv("NATIVE_METAL_YCBCR_PLANES"); - if (bootArgSet("metal-flow=0")) setenv("NATIVE_METAL_FLOW", "0", 1); - else if (bootArgSet("metal-flow=1")) setenv("NATIVE_METAL_FLOW", "1", 1); - else unsetenv("NATIVE_METAL_FLOW"); + { + // metal-flow=: 1 the lowering thread, 2 commit submission, 4 the warm pass; all three without the argument. + char args[1100] = {}, flow[16] = ""; + size_t size = sizeof args - 1; + if (sysctlbyname("kern.bootargs", args, &size, nullptr, 0) == 0) + if (const char *at = bootArgToken(args, "metal-flow=")) { + strlcpy(flow, at + strlen("metal-flow="), sizeof flow); + flow[strcspn(flow, " ")] = 0; + } + if (flow[0]) setenv("NATIVE_METAL_FLOW", flow, 1); + else unsetenv("NATIVE_METAL_FLOW"); + } if (bootArgSet("wallpaper-freeze=1")) setenv("NATIVE_METAL_FREEZE_WALLPAPER", "1", 1); // log-gpu=1: the GPU stack's log lines (NSLog, the pipeline journal); off without it. if (bootArgSet("log-gpu=1")) setenv("SHENG_LOG_GPU", "1", 1); diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index c3f85df..fc426e4 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -239,10 +239,14 @@ impl PipelineRecords { } } -/// True where `NATIVE_METAL_FLOW=1` (the boot argument `metal-flow=1`) enables the lowering thread, commit submission and the warm pass. -fn flow_on() -> bool { - static ON: std::sync::OnceLock = std::sync::OnceLock::new(); - *ON.get_or_init(|| std::env::var_os("NATIVE_METAL_FLOW").is_some_and(|value| value == "1")) +/// The work placement the bridge asks of the runtime, as bits: 1 render encoder calls lowered on the device's lowering thread, +/// 2 a command buffer submitted at its commit (`commit_and_submit`), 4 the warm pass over the pipeline records of earlier +/// processes. All three by default; `NATIVE_METAL_FLOW=` (the boot argument `metal-flow=`) states the bits, 0 none. +fn flow_bits() -> u32 { + static BITS: std::sync::OnceLock = std::sync::OnceLock::new(); + *BITS.get_or_init(|| { + std::env::var("NATIVE_METAL_FLOW").ok().and_then(|value| value.trim().parse::().ok()).unwrap_or(7) + }) } /// # Safety @@ -268,11 +272,12 @@ pub unsafe extern "C" fn nmb_device_open(loader: *const c_char, cache: *const c_ // render server showed as missing pieces of the screen while it compiled (int 590aaee2). options.policy.pipeline_waits = metal2vulkan::device::PipelineWaitPolicy::Exact; // Render encoder calls are checked at the call and lowered on the device's lowering thread, so the program's thread does not - // pay for them. A refusal only the lowering finds fails the command buffer, naming the call. - if flow_on() { + // pay for them (the render server's main thread spent most of the plugin's CPU time lowering). A refusal only the lowering + // finds fails the command buffer, naming the call. `metal-flow=` without bit 1 keeps the lowering at the call. + if flow_bits() & 1 != 0 { options.policy.render_lowering = metal2vulkan::device::RenderLoweringPolicy::Worker; } - let records = flow_on().then(|| PipelineRecords::open(cache.to_string_lossy().as_ref())).flatten(); + let records = (flow_bits() & 4 != 0).then(|| PipelineRecords::open(cache.to_string_lossy().as_ref())).flatten(); if let Some(records) = records.clone() { options.pipeline_records = Some(std::sync::Arc::new(move |record| records.keep(&record))); } @@ -2589,7 +2594,7 @@ pub extern "C" fn nmb_command_buffer_commit(command_buffer: *mut NmbCommandBuffe guard(false, || { // The committing thread submits where no other pass is under way, with no wake of the scheduler thread between commit and GPU. let command_buffer = &get(command_buffer)?.0; - if flow_on() { + if flow_bits() & 2 != 0 { command_buffer.commit_and_submit().map_err(e("commit"))?; } else { command_buffer.commit().map_err(e("commit"))?;