diff --git a/crates/metal2vulkan-runtime/examples/host_ab/gpu.rs b/crates/metal2vulkan-runtime/examples/host_ab/gpu.rs index 84a80ceca..db32c3254 100644 --- a/crates/metal2vulkan-runtime/examples/host_ab/gpu.rs +++ b/crates/metal2vulkan-runtime/examples/host_ab/gpu.rs @@ -96,6 +96,8 @@ pub trait Gpu: Sync { fn commands_id(&self, commands: &Self::IndirectCommands) -> u64; /// The `gpuResourceID` of `state`, made with `supportIndirectCommandBuffers`. fn pipeline_id(&self, state: &Self::ComputePipeline) -> u64; + /// The `gpuResourceID` of `texture`, as a kernel reads it from an argument buffer. + fn texture_id(&self, texture: &Self::Texture) -> u64; /// `newIndirectCommandBufferWithDescriptor:maxCommandCount:options:` of `count` concurrent /// dispatches of `Shared` storage that inherit nothing and bind one kernel buffer, for the /// CPU to encode. diff --git a/crates/metal2vulkan-runtime/examples/host_ab/host_ab.metal b/crates/metal2vulkan-runtime/examples/host_ab/host_ab.metal index ad1e86d38..cf16afdcd 100644 --- a/crates/metal2vulkan-runtime/examples/host_ab/host_ab.metal +++ b/crates/metal2vulkan-runtime/examples/host_ab/host_ab.metal @@ -219,6 +219,18 @@ kernel void tiny_kernel_b(device uint *out [[buffer(0)]], out[tid] = value * 3u + tid + uint(NONCE); } +// The handle dispatches' kernel: it reads its texture through a handle in an argument buffer, so +// each of its dispatches binds the device's texture table (`dispatch_handles`). +struct TinyHandles { + texture2d texture; +}; + +kernel void tiny_handle_kernel(device uint *out [[buffer(0)]], + constant TinyHandles &handles [[buffer(1)]], + uint tid [[thread_position_in_grid]]) { + out[tid] = uint(handles.texture.read(uint2(0, 0)).x * 255.0) + tid + uint(NONCE); +} + // The many-draws workload's functions: a small rectangle per draw, of its own uniforms, and four // fragment functions over two textures and one sampler. struct DrawUniforms { diff --git a/crates/metal2vulkan-runtime/examples/host_ab/main.rs b/crates/metal2vulkan-runtime/examples/host_ab/main.rs index 1f1ca9304..32f86120f 100644 --- a/crates/metal2vulkan-runtime/examples/host_ab/main.rs +++ b/crates/metal2vulkan-runtime/examples/host_ab/main.rs @@ -30,7 +30,8 @@ //! wall time after it and the process's CPU time; `dispatch_resident_moving_0`, `_16`, `_256` //! and `_4096`: as many after none, 16, 256 or 4096 buffers made resident, the output buffer's //! offset moved before each dispatch; `dispatch_resident_textures_256` and `_4096`: as many after -//! 256 or 4096 textures made resident. +//! 256 or 4096 textures made resident; `dispatch_handles`: as many of a kernel reading a +//! resident texture through a handle in an argument buffer. //! - `fixed`: a command buffer's creation, an encoder of one command each (render, compute, //! blit), and the commit call of a command buffer of 30 such encoders. //! - `calls`: the frames of a guest as reims-vgpu's `frame_cost` measures them, call by call: one @@ -208,7 +209,7 @@ //! `draw_static`, `draw_offset`, `draw_bind`, `draw_pipeline`, `draw_passes`, `dispatch_static`, //! `dispatch_offsets`, `dispatch_churn`, `dispatch_resident_0`, `_1`, `_256`, `_4096`, //! `_same_4096`, `dispatch_resident_moving_0`, `_16`, `_256`, `_4096`, -//! `dispatch_resident_textures_256`, `_4096`; for a profile of one; and the memory workload's parts alone: `create`, +//! `dispatch_resident_textures_256`, `_4096`, `dispatch_handles`; for a profile of one; and the memory workload's parts alone: `create`, //! `no_copy`, `held_1m`, `held_64m`, `texture`), `--announce-each a,b,...` (the runtime's device //! announces these degradation kinds, by code with or without its `metal2vulkan-degrade-` prefix, //! or `all` of them, at every occurrence, `DeviceOptions::announce_each`, as a consumer that hears @@ -511,7 +512,7 @@ fn run(g: &G, opened: Instant, metallib: &Path, options: &Options) { ); for workload in rest { match workload { - "encode" => workloads::encode(g, &scene, sizes, &options.variants), + "encode" => workloads::encode(g, &scene, &library, sizes, &options.variants), "fixed" => workloads::fixed(g, &scene, sizes), "calls" => workloads::calls(g, &scene, &library, sizes, &options.variants), "latency" => workloads::latency(g, &scene, sizes), diff --git a/crates/metal2vulkan-runtime/examples/host_ab/native.rs b/crates/metal2vulkan-runtime/examples/host_ab/native.rs index 9d076346a..c705e945a 100644 --- a/crates/metal2vulkan-runtime/examples/host_ab/native.rs +++ b/crates/metal2vulkan-runtime/examples/host_ab/native.rs @@ -253,6 +253,10 @@ impl Gpu for Native { state.gpuResourceID().to_raw() } + fn texture_id(&self, texture: &Self::Texture) -> u64 { + texture.gpuResourceID().to_raw() + } + fn cpu_indirect_commands(&self, count: usize) -> Self::IndirectCommands { let descriptor = MTLIndirectCommandBufferDescriptor::new(); descriptor.setCommandTypes(MTLIndirectCommandType::ConcurrentDispatch); diff --git a/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs b/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs index 9c1fe39b2..4acb079fb 100644 --- a/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs +++ b/crates/metal2vulkan-runtime/examples/host_ab/runtime.rs @@ -273,6 +273,10 @@ impl Gpu for Runtime { state.gpu_resource_id().0 } + fn texture_id(&self, texture: &rt::Texture) -> u64 { + texture.gpu_resource_id().0 + } + fn cpu_indirect_commands(&self, count: usize) -> rt::IndirectCommandBuffer { let mut descriptor = rt::IndirectCommandBufferDescriptor::new(); descriptor.command_types = IndirectCommandType::CONCURRENT_DISPATCH; diff --git a/crates/metal2vulkan-runtime/examples/host_ab/workloads.rs b/crates/metal2vulkan-runtime/examples/host_ab/workloads.rs index fe9c85401..9313958eb 100644 --- a/crates/metal2vulkan-runtime/examples/host_ab/workloads.rs +++ b/crates/metal2vulkan-runtime/examples/host_ab/workloads.rs @@ -502,7 +502,13 @@ pub fn threads(g: &G, library: &G::Library, shared: &G::RenderPipeline, /// The CPU cost of encoding: per draw and per dispatch, on the encoding thread (wall) and in the /// whole process (CPU time of every thread, from the command buffer's creation until its wait /// returned, which takes in work moved off the encoding thread to commit or to other threads). -pub fn encode(g: &G, s: &Scene, sizes: &Sizes, variants: &[String]) { +pub fn encode( + g: &G, + s: &Scene, + library: &G::Library, + sizes: &Sizes, + variants: &[String], +) { let n = sizes.draws; let runs = |name: String| variants.is_empty() || variants.contains(&name); for churn in [ @@ -611,6 +617,64 @@ pub fn encode(g: &G, s: &Scene, sizes: &Sizes, variants: &[String]) { emit_memo_shares(g, counts, dispatches, &format!("dispatch_{name}")); } encode_resident(g, s, sizes, &runs); + encode_handles(g, s, library, sizes, &runs); +} + +/// The encode workload's `dispatch_handles` variant: a compute encoder of `draws` +/// single-threadgroup dispatches of `tiny_handle_kernel`, which reads a texture made resident +/// through its handle in an argument buffer, so each dispatch binds the device's texture table. +/// Per dispatch: the encoding thread's wall time and the process's CPU time. +fn encode_handles( + g: &G, + s: &Scene, + library: &G::Library, + sizes: &Sizes, + runs: &dyn Fn(String) -> bool, +) { + if !runs("dispatch_handles".to_owned()) { + return; + } + let n = sizes.draws; + let pipeline = g.compute_pipeline(library, "tiny_handle_kernel"); + let texture = &s.textures[0]; + let handles = g.buffer_with(&g.texture_id(texture).to_le_bytes()); + let (mut wall, mut cpu) = (vec![], vec![]); + for run in 0..sizes.timed + 3 { + g.pool(|| { + let cpu0 = process_cpu(); + let command_buffer = g.command_buffer(); + let mut encoder = g.compute_encoder(&command_buffer); + g.set_compute_pipeline(&mut encoder, &pipeline); + g.set_buffer(&mut encoder, &s.tiny_out, 0, 0); + g.set_buffer(&mut encoder, &handles, 0, 1); + g.use_textures(&mut encoder, &[texture]); + let t0 = Instant::now(); + for _ in 0..n { + g.dispatch(&mut encoder, 1, 64); + } + let t1 = Instant::now(); + g.end_compute(encoder); + g.commit(&command_buffer); + g.wait(&command_buffer); + let cpu1 = process_cpu(); + if run >= 3 { + wall.push(us(t1 - t0) / n as f64); + cpu.push(us(cpu1 - cpu0) / n as f64); + } + }); + } + emit( + "encode", + "dispatch_handles_encode", + "us/dispatch", + &mut wall, + ); + emit( + "encode", + "dispatch_handles_process_cpu", + "us/dispatch", + &mut cpu, + ); } /// The resident buffers of the encode workload's `dispatch_resident` variants, of 256 bytes each.