diff --git a/crates/reims-vgpu-vulkan/src/gpu/desc.rs b/crates/reims-vgpu-vulkan/src/gpu/desc.rs index 4a51d422d..45446541f 100644 --- a/crates/reims-vgpu-vulkan/src/gpu/desc.rs +++ b/crates/reims-vgpu-vulkan/src/gpu/desc.rs @@ -543,19 +543,48 @@ pub struct TextureDescriptorObject { /// `MTLTextureDescriptor *`. pub type VulkanTextureDescriptor = Obj; -/// `MTLRenderPassDescriptor *`: the runtime's descriptor, which core's sets change. +/// `MTLRenderPassDescriptor *`: the runtime's descriptor, which core's sets change, and what +/// Metal's descriptor holds beside it, behind one lock: each set takes it once, and an encoder's +/// creation once for everything it reads ([`RenderPassDescriptorObject::held`]). A lock and its +/// release are each a locked instruction on x86-64, which costs about 0.49 µs between two +/// recorded Vulkan commands on RADV, whose command buffers are write-combined memory +/// (metal2vulkan's measurement on an RX 7900 XTX); one lock per part made a pass's attachment +/// set three and its encoder's creation four. #[derive(Debug)] pub struct RenderPassDescriptorObject { - /// The runtime's descriptor. - pub desc: Mutex, + /// The descriptor's parts. + held: Mutex, +} + +/// What a render pass descriptor holds ([`RenderPassDescriptorObject`]). +#[derive(Debug)] +pub struct PassHeld { + /// The runtime's descriptor, without its rate map. + pub desc: rt::RenderPassDescriptor, /// The rate map set, held by reference as Metal holds it: an encoder takes its class as it is /// at the encoder's creation ([`crate::gpu::rate_map`]). - rate_map: Mutex>, + pub rate_map: Option, /// What the attachments' textures are beyond the runtime's textures. - sources: Mutex, + pub sources: Sources, /// The attachments' properties the runtime has no value of, raw, as their setters took them /// ([`RawPass`]). - raw: Mutex, + raw: RawPass, +} + +impl PassHeld { + /// The first property the runtime holds raw ([`RawPass`]), by attachment then setter: the + /// attachment's name, the setter and the value. + pub fn raw_value(&self) -> Option<(String, &'static str, u64)> { + let first = (self.raw.0.first_key_value()).map(|(&at, &value)| (at, value)); + first.map(|((attachment, sel), value)| { + let name = match attachment { + DEPTH => "the depth attachment".to_owned(), + STENCIL => "the stencil attachment".to_owned(), + i => format!("colour attachment {i}"), + }; + (name, sel, value) + }) + } } /// The values of a pass's attachment properties the runtime has no value of, raw, as Metal's @@ -563,7 +592,7 @@ pub struct RenderPassDescriptorObject { /// [`STENCIL`]) and the property's setter: a load or store action, store action options, a /// resolve filter. The runtime's attachment keeps the value before; core reads the load and /// store actions back raw, and the encoder's creation asserts at one, as Metal's -/// ([`RenderPassDescriptorObject::raw_value`]). +/// ([`PassHeld::raw_value`]). #[derive(Debug, Default, Clone)] pub struct RawPass(std::collections::BTreeMap<(usize, &'static str), u64>); @@ -830,43 +859,35 @@ impl RenderPassDescriptorObject { /// A descriptor of Metal's defaults (`+new` and `+renderPassDescriptor` alike). pub fn new() -> RenderPassDescriptorObject { RenderPassDescriptorObject { - desc: Mutex::new(rt::RenderPassDescriptor::new()), - rate_map: Mutex::new(None), - sources: Mutex::new(Sources::default()), - raw: Mutex::new(RawPass::default()), + held: Mutex::new(PassHeld { + desc: rt::RenderPassDescriptor::new(), + rate_map: None, + sources: Sources::default(), + raw: RawPass::default(), + }), } } - /// The first property the runtime holds raw ([`RawPass`]), by attachment then setter: the - /// attachment's name, the setter and the value. - pub fn raw_value(&self) -> Option<(String, &'static str, u64)> { - let first = - plain(|| (self.raw.lock().0.first_key_value()).map(|(&at, &value)| (at, value))); - first.map(|((attachment, sel), value)| { - let name = match attachment { - DEPTH => "the depth attachment".to_owned(), - STENCIL => "the stencil attachment".to_owned(), - i => format!("colour attachment {i}"), - }; - (name, sel, value) - }) - } - - /// What the attachments' textures are as they stand ([`Source`]). - pub fn sources(&self) -> Sources { - plain(|| self.sources.lock().clone()) + /// Runs `read` with what the descriptor holds as it stands, under one hold of its lock: an + /// encoder's creation reads it there, borrowing it whole, with no copy of its textures' + /// references. Nothing `read` runs reaches this descriptor again. + pub fn held(&self, read: impl FnOnce(&PassHeld) -> T) -> T { + let held = plain(|| self.held.lock()); + read(&held) } /// The runtime's descriptor as it stands, without its rate map. pub fn descriptor(&self) -> rt::RenderPassDescriptor { - plain(|| self.desc.lock().clone()) + self.held(|held| plain(|| held.desc.clone())) } /// The runtime's descriptor as it stands, without its rate map, and the rate map set, whose /// class an encoder of it takes at the encoder's creation ([`crate::gpu::rate_map`]). pub fn pass(&self) -> (rt::RenderPassDescriptor, Option) { - let map = plain(|| self.rate_map.lock().as_ref().map(Handle::clone_ref)); - (self.descriptor(), map) + self.held(|held| { + let map = held.rate_map.as_ref().map(Handle::clone_ref); + (plain(|| held.desc.clone()), map) + }) } } @@ -997,7 +1018,7 @@ impl RenderPassDescriptor for VulkanRenderPassDescriptor { .iter() .map(|p| rt::SamplePosition::new(p.x, p.y)) .collect(); - plain(|| self.desc.lock().sample_positions = positions); + plain(|| self.held.lock().desc.sample_positions = positions); Ok(()) }); crate::recorder::rec_done!(recorded_call, recorded_answer); @@ -1007,10 +1028,12 @@ impl RenderPassDescriptor for VulkanRenderPassDescriptor { fn set_pass(&self, set: &PassSet<'_, VulkanPlatform>) { let recorded_call = crate::recorder::rec_call!("m2v.setPass", self, set); crate::recorder::run(|| { - let mut d = plain(|| self.desc.lock()); + let mut held = plain(|| self.held.lock()); + let held = &mut *held; + let d = &mut held.desc; if let Some(b) = set.visibility_result_buffer { d.visibility_result_buffer = b.map(|b| b.raw.clone()); - plain(|| self.sources.lock().visibility = b.map(Handle::clone_ref)); + held.sources.visibility = b.map(Handle::clone_ref); } // `setVisibilityResultType:` is kept nowhere: the runtime has no accumulation, so a pass // of `Accumulate` resets its results as one of `Reset` does, as the AMD Radeon Pro @@ -1044,7 +1067,7 @@ impl RenderPassDescriptor for VulkanRenderPassDescriptor { // pass shows, with attachments or without (`mutgap_dec_render_default_color_samples`). let _ = set.default_color_sample_count; if let Some(map) = set.rasterization_rate_map { - *plain(|| self.rate_map.lock()) = map.map(Handle::clone_ref); + held.rate_map = map.map(Handle::clone_ref); } if let Some(v) = set.imageblock_sample_length { d.imageblock_sample_length = v; @@ -1072,9 +1095,13 @@ impl RenderPassDescriptor for VulkanRenderPassDescriptor { let recorded_call = crate::recorder::rec_call!("m2v.setColorAttachment", self, index, set); let recorded_answer = crate::recorder::run(|| -> RaiseResult<()> { let i = color_index(Policy::On(on), index)?; - let mut d = plain(|| self.desc.lock()); - let mut sources = plain(|| self.sources.lock()); - let mut raw = plain(|| self.raw.lock()); + let mut held = plain(|| self.held.lock()); + let PassHeld { + desc: d, + sources, + raw, + .. + } = &mut *held; let a = &mut d.color_attachments[i]; set_attachment_common!(set, a, sources.color[i], raw, i); if let Some(c) = set.clear_color { @@ -1089,9 +1116,13 @@ impl RenderPassDescriptor for VulkanRenderPassDescriptor { fn set_depth_attachment(&self, set: &DepthAttachmentSet<'_, VulkanPlatform>) { let recorded_call = crate::recorder::rec_call!("m2v.setDepthAttachment", self, set); crate::recorder::run(|| { - let mut d = plain(|| self.desc.lock()); - let mut sources = plain(|| self.sources.lock()); - let mut raw = plain(|| self.raw.lock()); + let mut held = plain(|| self.held.lock()); + let PassHeld { + desc: d, + sources, + raw, + .. + } = &mut *held; let a = &mut d.depth_attachment; set_attachment_common!(set, a, sources.depth, raw, DEPTH); if let Some(v) = set.clear_depth { @@ -1110,9 +1141,13 @@ impl RenderPassDescriptor for VulkanRenderPassDescriptor { fn set_stencil_attachment(&self, set: &StencilAttachmentSet<'_, VulkanPlatform>) { let recorded_call = crate::recorder::rec_call!("m2v.setStencilAttachment", self, set); crate::recorder::run(|| { - let mut d = plain(|| self.desc.lock()); - let mut sources = plain(|| self.sources.lock()); - let mut raw = plain(|| self.raw.lock()); + let mut held = plain(|| self.held.lock()); + let PassHeld { + desc: d, + sources, + raw, + .. + } = &mut *held; let a = &mut d.stencil_attachment; set_attachment_common!(set, a, sources.stencil, raw, STENCIL); if let Some(v) = set.clear_stencil { @@ -1131,23 +1166,23 @@ impl RenderPassDescriptor for VulkanRenderPassDescriptor { /// An index past the eight attachments raises, the caller's to catch. fn color_attachment_state(&self, index: u64) -> RaiseResult { let i = color_index(Policy::Caught, index)?; - let d = plain(|| self.desc.lock()); - let raw = plain(|| self.raw.lock()); + let held = plain(|| self.held.lock()); + let (d, raw) = (&held.desc, &held.raw); let a = &d.color_attachments[i]; - Ok(attachment_state(&raw, i, a.load_action, a.store_action)) + Ok(attachment_state(raw, i, a.load_action, a.store_action)) } fn depth_attachment_state(&self) -> AttachmentState { - let d = plain(|| self.desc.lock()); - let raw = plain(|| self.raw.lock()); + let held = plain(|| self.held.lock()); + let (d, raw) = (&held.desc, &held.raw); let a = &d.depth_attachment; - attachment_state(&raw, DEPTH, a.load_action, a.store_action) + attachment_state(raw, DEPTH, a.load_action, a.store_action) } fn stencil_attachment_state(&self) -> AttachmentState { - let d = plain(|| self.desc.lock()); - let raw = plain(|| self.raw.lock()); + let held = plain(|| self.held.lock()); + let (d, raw) = (&held.desc, &held.raw); let a = &d.stencil_attachment; - attachment_state(&raw, STENCIL, a.load_action, a.store_action) + attachment_state(raw, STENCIL, a.load_action, a.store_action) } } diff --git a/crates/reims-vgpu-vulkan/src/gpu/encode_render.rs b/crates/reims-vgpu-vulkan/src/gpu/encode_render.rs index 8a185c78c..13d3dcb45 100644 --- a/crates/reims-vgpu-vulkan/src/gpu/encode_render.rs +++ b/crates/reims-vgpu-vulkan/src/gpu/encode_render.rs @@ -1269,55 +1269,76 @@ pub(crate) fn open( descriptor: &VulkanRenderPassDescriptor, ) -> RaiseResult> { command_buffer.protected_creation("renderCommandEncoderWithDescriptor:"); - if let Some((attachment, setter, raw)) = descriptor.raw_value() { - crate::answer::metal_aborts( - "renderCommandEncoderWithDescriptor:", - &format!( - "a pass whose {attachment} holds {raw:#x} from {setter}, which Metal's validation \ - refuses" - ), - ); - } - let (mut d, map) = descriptor.pass(); - let sources = descriptor.sources(); - if crate::os::COPIES_READ_ONLY - && let Some(what) = kept_attachment(&d, &sources) - { - crate::gpu::writes::kept_write("renderCommandEncoderWithDescriptor:", what); - } - // The colour resolves into a texture of another format, decided by the rate map the pass - // takes, as it takes it. - let settled = |d: &mut rt::RenderPassDescriptor, mapped: PassMap| { - if let Some(what) = refused_resolve_format(d, &sources, mapped) { - unrepresentable("renderCommandEncoderWithDescriptor:", &what); + // The pass is read under the descriptor's one hold, and borrowed where nothing of it is + // changed for the runtime: no rate map and no resolve texture. + descriptor.held(|held| { + if let Some((attachment, setter, raw)) = held.raw_value() { + crate::answer::metal_aborts( + "renderCommandEncoderWithDescriptor:", + &format!( + "a pass whose {attachment} holds {raw:#x} from {setter}, which Metal's \ + validation refuses" + ), + ); } - untouched_resolves(d, &sources, mapped) - }; - let made = match map { - None => { - let dropped = settled(&mut d, PassMap::Unmapped); - make(command_buffer, &mut d, dropped, Taken::UNMAPPED) + let sources = &held.sources; + if crate::os::COPIES_READ_ONLY + && let Some(what) = kept_attachment(&held.desc, sources) + { + crate::gpu::writes::kept_write("renderCommandEncoderWithDescriptor:", what); } - Some(map) => map.for_pass(command_buffer, |raw, class| { - let mapped = match class { - Class::Unset => PassMap::Unset, - Class::NoLayers | Class::Identity => PassMap::Identity, - Class::Varying => PassMap::Varying, - }; - let dropped = settled(&mut d, mapped); - d.rasterization_rate_map = Some(raw); - let taken = Taken { - class: Some(class), - mutable: map.is_mutable(), - }; - make(command_buffer, &mut d, dropped, taken) - }), - }; - if let Ok(Some(e)) = &made { - e.undecided_dropped(&sources); - e.read_only_stores(&sources); - } - made + // The colour resolves into a texture of another format, decided by the rate map the pass + // takes, as it takes it. + let settled = |d: &mut rt::RenderPassDescriptor, mapped: PassMap| { + if let Some(what) = refused_resolve_format(d, sources, mapped) { + unrepresentable("renderCommandEncoderWithDescriptor:", &what); + } + let dropped = untouched_resolves(d, sources, mapped); + without_unused_resolves(d); + dropped + }; + let made = match &held.rate_map { + None if !has_resolve_texture(&held.desc) => { + make(command_buffer, &held.desc, Vec::new(), Taken::UNMAPPED) + } + None => { + let mut d = plain(|| held.desc.clone()); + let dropped = settled(&mut d, PassMap::Unmapped); + make(command_buffer, &d, dropped, Taken::UNMAPPED) + } + Some(map) => { + let mut d = plain(|| held.desc.clone()); + map.for_pass(command_buffer, |raw, class| { + let mapped = match class { + Class::Unset => PassMap::Unset, + Class::NoLayers | Class::Identity => PassMap::Identity, + Class::Varying => PassMap::Varying, + }; + let dropped = settled(&mut d, mapped); + d.rasterization_rate_map = Some(raw); + let taken = Taken { + class: Some(class), + mutable: map.is_mutable(), + }; + make(command_buffer, &d, dropped, taken) + }) + } + }; + if let Ok(Some(e)) = &made { + e.undecided_dropped(sources); + e.read_only_stores(sources); + } + made + }) +} + +/// Whether an attachment of the pass `d` has a resolve texture: only such a pass has resolves +/// that [`open`] drops ([`untouched_resolves`], [`without_unused_resolves`]) or refuses +/// ([`refused_resolve_format`]). +fn has_resolve_texture(d: &rt::RenderPassDescriptor) -> bool { + d.color_attachments.iter().any(|a| a.resolve_texture.is_some()) + || d.depth_attachment.resolve_texture.is_some() + || d.stencil_attachment.resolve_texture.is_some() } /// The colour attachment of the pass `d` of a bfloat format (`R16Bfloat`, `RG16Bfloat`, @@ -1380,17 +1401,16 @@ impl Taken { } /// The encoder of the pass `d` on `command_buffer`, with its rate map as [`open`] took it, -/// `taken`, and without the colour resolves it `dropped` ([`untouched_resolves`]). One made is -/// work for Metal's tiler ([`crate::gpu::command::CommandBufferObject::tiler_work`]). +/// `taken`, and without the colour resolves it `dropped` ([`untouched_resolves`]) and its unused +/// depth and stencil resolves ([`without_unused_resolves`]), which [`open`] took from `d`. One +/// made is work for Metal's tiler ([`crate::gpu::command::CommandBufferObject::tiler_work`]). fn make( command_buffer: &VulkanCommandBuffer, - d: &mut rt::RenderPassDescriptor, + d: &rt::RenderPassDescriptor, dropped: Vec, taken: Taken, ) -> RaiseResult> { let sel = "renderCommandEncoderWithDescriptor:"; - without_unused_resolves(d); - let d = &*d; match plain_at(sel, || command_buffer.raw.render_command_encoder(d)) { Ok(raw) => { command_buffer.tiler_work(sel, crate::gpu::metal_space::FirstUse::RenderEncoder);