Render draws reach their bind state with no Arc clone and no lock

Every render draw cloned the encoder's Arc<Mutex<LiveBind>> and locked it:
two atomic counts and a lock and unlock per draw, there only so that a draw
could hold the bind state while it called the lowering's other methods. On
draws.10k the clone, lock, unlock and drop were about 28% of draw_rebound's
own samples (a line-level profile on gem10), and a probe that reached the
state with neither cut about 104 instructions a draw on the M3 (-2.0%).

The bind state now lies beside the lowering instead of inside it: Lowering
holds the boxed RenderLowering and the boxed LiveBind, derefs to the lowering
for every other call, and hands a draw both as two borrows (Lowering::parts,
RenderCommandEncoder::draw_at_call). The draw path takes the state as a
parameter: the lowering's draw entry points, draw, draw_again, draw_rebound,
draw_resident, the extended shadows' re-recording, the execution replays and
their settles. Ending the encoder gives the state back to the device
(Lowering::ending); SpareLiveBinds keeps the boxes, so an encoder's open and
end still move and allocate none. An execution's encoders, and its chunks
recorded apart, carry their bind state beside the encoder as before.

Verified: pending (build, clippy, A/B on the M3 and lavapipe, suites).
(cherry picked from commit 7692b71ca46440c42ead3f3b1061428559ba85d5)
This commit is contained in:
Anees Iqbal committed 2026-11-08 03:55:35 -08:00
1 parent 8a974ab605
commit 75e5e5def1
13 files changed
+368 -204

No files matched your search

+8 -15
View File
@@ -683,30 +683,23 @@ impl<T> Drop for Emptied<'_, T> {
/// A device's bind states that ended render encoders left, which the render encoders it opens
/// next start from (the module docs): at most [`SPARE_LIVE_BINDS`], the latest given taken first.
/// Each is kept in the shared cell its encoder held it in, so an encoder's open and end move and
/// allocate none.
/// Each is kept in the box its encoder held it in, so an encoder's open and end move and allocate
/// none.
#[derive(Default)]
pub(super) struct SpareLiveBinds(Mutex<Vec<Arc<Mutex<LiveBind>>>>);
pub(super) struct SpareLiveBinds(Mutex<Vec<Box<LiveBind>>>);
impl SpareLiveBinds {
/// The state a render encoder opening starts from: the latest an encoder gave back, else an
/// empty one.
pub(super) fn take(&self) -> Arc<Mutex<LiveBind>> {
pub(super) fn take(&self) -> Box<LiveBind> {
lock(&self.0).pop().unwrap_or_default()
}
/// Keep `live`, an ended render encoder's state, for the next encoder to start from, where
/// nothing else holds it (no draw does once its encoder ended) and fewer than
/// [`SPARE_LIVE_BINDS`] are kept; else it goes. A draw that panicked holding it left it as
/// far as it got, which the next draw brings in place as any.
pub(super) fn give(&self, mut live: Arc<Mutex<LiveBind>>) {
let Some(cell) = Arc::get_mut(&mut live) else {
return;
};
cell.clear_poison();
if let Ok(state) = cell.get_mut() {
state.end_encoder();
}
/// fewer than [`SPARE_LIVE_BINDS`] are kept; else it goes. A draw that panicked left it as far
/// as it got, which the next draw brings in place as any.
pub(super) fn give(&self, mut live: Box<LiveBind>) {
live.end_encoder();
let mut spare = lock(&self.0);
if spare.len() < SPARE_LIVE_BINDS {
spare.push(live);
+114 -39
View File
@@ -287,7 +287,7 @@ use super::depth_stand_in::DepthStandIn;
use super::depth_stencil::{DepthStencilState, FailingDraw};
use super::depth_stencil_resolve::DrawnResolve;
use super::depth_stencil_scratch::{ScratchCopy, Separate};
use super::device::{lock, Device};
use super::device::Device;
use super::draw_source::{CopySteps, DrawSource, IndexBind};
use super::dynamic_state::ExtendedDynamicState;
use super::encoder::{EncoderCore, Replayed};
@@ -1659,10 +1659,6 @@ pub(crate) struct RenderLowering {
/// strips restart (`render/restart.rs`); `None` before any, where they restart at the index
/// type's all-ones index.
restart: Option<u64>,
/// The bind state the encoder's last draw bound, with its shape, which each draw brings to
/// its own in place (`live_bind.rs`). Shared only so that a draw holds it while it calls the
/// encoder's other methods.
live: Arc<Mutex<LiveBind>>,
/// Its pass's descriptor and its calls so far, where the device had a buffer over host pages
/// as it opened (`render/replay.rs`): `None` where it logs nothing, as when it is encoded
/// again. Boxed where it is taken (`SpareLogs::render`), the box its end hands the command
@@ -1908,6 +1904,66 @@ impl std::fmt::Debug for RenderLowering {
}
}
/// A render encoder's lowering beside the bind state its last draw bound, with its shape, which
/// each draw brings to its own in place (`live_bind.rs`): held apart from the lowering, so a draw
/// holds both, the bind state while it calls the lowering's other methods, as two borrows
/// ([`parts`](Self::parts)), with no lock and no count. Each in its box, so the encoder's return
/// and moves copy neither one's state. Every other call reaches the lowering through it.
pub(crate) struct Lowering {
lowering: Box<RenderLowering>,
live: Box<LiveBind>,
}
impl std::ops::Deref for Lowering {
type Target = RenderLowering;
fn deref(&self) -> &RenderLowering {
&self.lowering
}
}
impl std::ops::DerefMut for Lowering {
fn deref_mut(&mut self) -> &mut RenderLowering {
&mut self.lowering
}
}
impl std::fmt::Debug for Lowering {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.lowering, f)
}
}
impl Lowering {
/// The lowering and the bind state, which a draw takes together.
pub(super) fn parts(&mut self) -> (&mut RenderLowering, &mut LiveBind) {
(&mut self.lowering, &mut self.live)
}
/// The lowering, its bind state given to the device for its next render encoder to start
/// from (`live_bind.rs`): no draw holds it once the encoder ends.
fn ending(self) -> Box<RenderLowering> {
let Lowering { lowering, live } = self;
lowering.core.device().shared().live_binds.give(live);
lowering
}
/// The lowering of [`RenderCommandEncoder::end_encoding`] (`RenderLowering::end_encoding`).
pub(crate) fn end_encoding(self) -> Result<(), Error> {
self.ending().end_encoding()
}
/// `RenderLowering::finish_replay`, its bind state given back first.
pub(super) fn finish_replay(self) -> Result<(Replayed, Option<Residency>), Error> {
self.ending().finish_replay()
}
/// `RenderLowering::end_pass`, its bind state given back first.
fn end_pass(self) -> Result<color_groups::Ended, Error> {
self.ending().end_pass()
}
}
impl CommandBuffer {
/// `renderCommandEncoderWithDescriptor:`: an encoder of the pass `descriptor` describes.
/// Refused as Metal's validation refuses the descriptor (`render_pass.rs`), and as
@@ -1938,7 +1994,7 @@ impl CommandBuffer {
given: &RenderPassDescriptor,
again: Option<Residency>,
screen: Option<[u32; 2]>,
) -> Result<Box<RenderLowering>, Error> {
) -> Result<Lowering, Error> {
const CALL: &str = "render_command_encoder";
let device = self.device();
let screen = screen.or_else(|| {
@@ -2117,7 +2173,7 @@ impl RenderLowering {
/// recording into `draws` (null where it records inline), made in its box: the box is
/// allocated first and the lowering written into it, where `Box::new` of a lowering makes it
/// on the stack and copies its thousands of bytes in.
fn assemble(core: Box<EncoderCore>, draws: vk::CommandBuffer, opening: Opening) -> Box<Self> {
fn assemble(core: Box<EncoderCore>, draws: vk::CommandBuffer, opening: Opening) -> Lowering {
let Opening {
layout,
render_target_array_length,
@@ -2143,7 +2199,7 @@ impl RenderLowering {
let area = rate_map
.as_ref()
.map_or(layout.extent, RateMappedPass::area);
Box::write(
let lowering = Box::write(
Box::new_uninit(),
RenderLowering {
core,
@@ -2246,7 +2302,6 @@ impl RenderLowering {
extended,
pass_start: room.pass_start,
restart: None,
live,
log,
quiet_bound_values: QuietKept::default(),
attached: render_target::AttachedMemo::default(),
@@ -2266,7 +2321,8 @@ impl RenderLowering {
pinned_through: 0,
pins_frozen: false,
},
)
);
Lowering { lowering, live }
}
}
@@ -3415,6 +3471,7 @@ impl RenderLowering {
/// The lowering of [`RenderCommandEncoder::draw_primitives`].
pub(crate) fn draw_primitives(
&mut self,
live: &mut LiveBind,
primitive_type: DrawPrimitiveType,
vertex_start: u32,
vertex_count: u32,
@@ -3444,12 +3501,13 @@ impl RenderLowering {
let hangs = vertex_count >= 3 && !empty;
return self.draw_nine(CALL, empty, hangs.then_some(NineFailure::Hang));
};
self.draw(CALL, assembly, draw, buffers, empty)
self.draw(live, CALL, assembly, draw, buffers, empty)
}
/// The lowering of [`RenderCommandEncoder::draw_indexed_primitives`].
pub(crate) fn draw_indexed_primitives(
&mut self,
live: &mut LiveBind,
primitive_type: DrawPrimitiveType,
index_count: u32,
index_type: IndexType,
@@ -3483,7 +3541,7 @@ impl RenderLowering {
base_vertex,
base_instance,
};
self.draw_indexed(CALL, draw, index_buffer, index_buffer_offset)
self.draw_indexed(live, CALL, draw, index_buffer, index_buffer_offset)
}
/// Draw `draw`'s indices from `index_buffer_offset` of `index_buffer`, a multiple of the index
@@ -3492,6 +3550,7 @@ impl RenderLowering {
/// indirect render commands takes too (`render/executions.rs`).
fn draw_indexed(
&mut self,
live: &mut LiveBind,
call: &'static str,
draw: IndexedDraw,
index_buffer: &Buffer,
@@ -3534,12 +3593,13 @@ impl RenderLowering {
let fault = NineFailure::Fault(CommandBufferError::PageFault);
return self.draw_nine(call, empty, Some(fault));
};
self.draw(call, assembly, draw, buffers, empty)
self.draw(live, call, assembly, draw, buffers, empty)
}
/// The lowering of [`RenderCommandEncoder::draw_primitives_indirect`].
pub(crate) fn draw_primitives_indirect(
&mut self,
live: &mut LiveBind,
primitive_type: DrawPrimitiveType,
indirect_buffer: &Buffer,
indirect_buffer_offset: u64,
@@ -3570,12 +3630,13 @@ impl RenderLowering {
let victim = NineFailure::Fault(CommandBufferError::GpuRecoveryVictim);
return self.draw_nine(CALL, false, Some(victim));
};
self.draw(CALL, assembly, draw, buffers, false)
self.draw(live, CALL, assembly, draw, buffers, false)
}
/// The lowering of [`RenderCommandEncoder::draw_indexed_primitives_indirect`].
pub(crate) fn draw_indexed_primitives_indirect(
&mut self,
live: &mut LiveBind,
primitive_type: DrawPrimitiveType,
index_type: IndexType,
index_buffer: &Buffer,
@@ -3667,12 +3728,13 @@ impl RenderLowering {
let victim = NineFailure::Fault(CommandBufferError::GpuRecoveryVictim);
return self.draw_nine(CALL, false, Some(victim));
};
self.draw(CALL, assembly, draw, buffers, false)
self.draw(live, CALL, assembly, draw, buffers, false)
}
/// The lowering of [`RenderCommandEncoder::draw_patches`].
pub(crate) fn draw_patches(
&mut self,
live: &mut LiveBind,
number_of_patch_control_points: u32,
patch_start: u32,
patch_count: u32,
@@ -3715,7 +3777,7 @@ impl RenderLowering {
None => None,
};
let points = zeros.as_ref().map(|zeros| (zeros, 0));
self.draw_patch_range(CALL, draw, patch_indices, points)
self.draw_patch_range(live, CALL, draw, patch_indices, points)
}
/// Draw `draw`'s patches, each patch's index from `patch_indices` (a buffer and an offset
@@ -3727,6 +3789,7 @@ impl RenderLowering {
/// commands takes too (`render/executions.rs`).
fn draw_patch_range(
&mut self,
live: &mut LiveBind,
call: &'static str,
draw: PatchDraw,
patch_indices: Option<(&Buffer, u64)>,
@@ -3754,12 +3817,13 @@ impl RenderLowering {
..DrawBuffers::default()
};
let empty = patch_count == 0 || instance_count == 0;
self.draw(call, Assembly::Patches, draw, buffers, empty)
self.draw(live, call, Assembly::Patches, draw, buffers, empty)
}
/// The lowering of [`RenderCommandEncoder::draw_patches_indirect`].
pub(crate) fn draw_patches_indirect(
&mut self,
live: &mut LiveBind,
number_of_patch_control_points: u32,
patch_index_buffer: Option<&Buffer>,
patch_index_buffer_offset: u64,
@@ -3799,12 +3863,13 @@ impl RenderLowering {
control_point_indices: zeros.as_ref().map(|zeros| (zeros, 0)),
..DrawBuffers::default()
};
self.draw(CALL, Assembly::Patches, draw, buffers, false)
self.draw(live, CALL, Assembly::Patches, draw, buffers, false)
}
/// The lowering of [`RenderCommandEncoder::draw_indexed_patches`].
pub(crate) fn draw_indexed_patches(
&mut self,
live: &mut LiveBind,
number_of_patch_control_points: u32,
patch_start: u32,
patch_count: u32,
@@ -3861,6 +3926,7 @@ impl RenderLowering {
)
});
self.draw_patch_range(
live,
CALL,
draw,
patch_index_buffer.map(|b| (b, patch_index_buffer_offset)),
@@ -3871,6 +3937,7 @@ impl RenderLowering {
/// The lowering of [`RenderCommandEncoder::draw_indexed_patches_indirect`].
pub(crate) fn draw_indexed_patches_indirect(
&mut self,
live: &mut LiveBind,
number_of_patch_control_points: u32,
patch_index_buffer: Option<&Buffer>,
patch_index_buffer_offset: u64,
@@ -3922,7 +3989,7 @@ impl RenderLowering {
control_point_indices: points,
..DrawBuffers::default()
};
self.draw(CALL, Assembly::Patches, draw, buffers, false)
self.draw(live, CALL, Assembly::Patches, draw, buffers, false)
}
/// A patch draw's patch index buffer (`None` for `nil`) at `offset`: the offset the draw reads
@@ -4175,7 +4242,7 @@ impl RenderLowering {
descriptor: &RenderPassDescriptor,
residency: Residency,
screen: Option<[u32; 2]>,
) -> Result<Box<RenderLowering>, Error> {
) -> Result<Lowering, Error> {
command_buffer.open_render(descriptor, Some(residency), screen)
}
@@ -4265,7 +4332,6 @@ impl RenderLowering {
drew_depth_writes,
inline,
grouped,
live,
bind_memo,
pass_start,
pinned_through,
@@ -4274,9 +4340,6 @@ impl RenderLowering {
// The views and sampler descriptions its binds made or found, which the device's next
// render encoder takes (`binding::BindMemo`).
core.device().shared().bind_memos.give(bind_memo);
// The bind state its last draw left, which the device's next render encoder starts from
// (`live_bind.rs`): no draw holds it once the encoder ends.
core.device().shared().live_binds.give(live);
// The pass's measured readers, whose counts their textures take as the command buffer
// completes (`render/coverage.rs`): held with its pool until then.
if let Some(readback) = readers.readback() {
@@ -7781,6 +7844,7 @@ impl RenderLowering {
/// draw passed (the module docs). Every texture the encoder's heaps hold is resident.
fn draw(
&mut self,
live: &mut LiveBind,
call: &'static str,
assembly: Assembly,
draw: DrawCall,
@@ -7791,7 +7855,7 @@ impl RenderLowering {
// The encoder's kept draw again, which no call came between: its Vulkan draw alone
// (`render/draw_again.rs`). Any other draw forgets it, and keeps itself where it can.
let mut checked = None;
match self.draw_again(call, assembly, &draw, &buffers, &mut checked) {
match self.draw_again(live, call, assembly, &draw, &buffers, &mut checked) {
draw_again::Again::Drawn => return Ok(()),
#[cfg(debug_assertions)]
draw_again::Again::Check => {}
@@ -7822,7 +7886,7 @@ impl RenderLowering {
color_groups::Turn::Later(true) if self.group_skips() => self.group_skipped(),
_ => {
self.plain_drawn = false;
let drawn = self.draw_resident(call, assembly, draw, buffers, empty);
let drawn = self.draw_resident(live, call, assembly, draw, buffers, empty);
self.replay_plain &= self.plain_drawn;
self.group_outcome(turn, drawn.is_ok());
if drawn.is_err() {
@@ -7889,7 +7953,12 @@ impl RenderLowering {
}
// A replayed draw that met a notice announced at every occurrence must meet it again.
self.replay_plain &= !watch.met();
self.kept_again(&watch, (reads, !empty && rasterizes, depth_writes), checked);
self.kept_again(
live,
&watch,
(reads, !empty && rasterizes, depth_writes),
checked,
);
Ok(())
}
@@ -7918,6 +7987,7 @@ impl RenderLowering {
/// [`draw`](Self::draw) with the heaps' textures resident.
fn draw_resident(
&mut self,
live: &mut LiveBind,
call: &'static str,
assembly: Assembly,
mut draw: DrawCall,
@@ -7994,7 +8064,7 @@ impl RenderLowering {
// A draw that blends into or reads an extended-range color an earlier draw of the pass
// wrote, or whose clear its codes cannot hold, starts the color's `half` shadow where it
// can (`render/extended_shadow.rs`).
self.start_extended_shadows(&pipeline)?;
self.start_extended_shadows(live, &pipeline)?;
// What the draw settles from the encoder's state, as an execution's precompile at commit
// settles it (`draw_settings`).
// An indexed strip the encoder's call restarts at another index than Vulkan's, and a draw
@@ -8037,9 +8107,6 @@ impl RenderLowering {
}
// The encoder's kept bind state, brought to the draw's (`live_bind.rs`), held for the
// draw: no draw of the encoder runs inside another past here.
let live = Arc::clone(&self.live);
let mut held = lock(&live);
let live = &mut *held;
// A draw that reads its pass's texture after a barrier inside its instance reads it
// through a snapshot where it can (`render/snapshot.rs`): asked of the plan, kept where
// the plan reads so, else `None` and the split.
@@ -11774,8 +11841,9 @@ impl RenderCommandEncoder {
) -> Result<(), Error> {
let primitive_type = primitive_type.into();
match self.lowers_at_call() {
true => self.at_call(|lowering| {
true => self.draw_at_call(|lowering, live| {
lowering.draw_primitives(
live,
primitive_type,
vertex_start,
vertex_count,
@@ -11817,8 +11885,9 @@ impl RenderCommandEncoder {
) -> Result<(), Error> {
let primitive_type = primitive_type.into();
match self.lowers_at_call() {
true => self.at_call(|lowering| {
true => self.draw_at_call(|lowering, live| {
lowering.draw_indexed_primitives(
live,
primitive_type,
index_count,
index_type,
@@ -11855,8 +11924,9 @@ impl RenderCommandEncoder {
) -> Result<(), Error> {
let primitive_type = primitive_type.into();
match self.lowers_at_call() {
true => self.at_call(|lowering| {
true => self.draw_at_call(|lowering, live| {
lowering.draw_primitives_indirect(
live,
primitive_type,
indirect_buffer,
indirect_buffer_offset,
@@ -11891,8 +11961,9 @@ impl RenderCommandEncoder {
) -> Result<(), Error> {
let primitive_type = primitive_type.into();
match self.lowers_at_call() {
true => self.at_call(|lowering| {
true => self.draw_at_call(|lowering, live| {
lowering.draw_indexed_primitives_indirect(
live,
primitive_type,
index_type,
index_buffer,
@@ -11936,8 +12007,9 @@ impl RenderCommandEncoder {
base_instance: u32,
) -> Result<(), Error> {
match self.lowers_at_call() {
true => self.at_call(|lowering| {
true => self.draw_at_call(|lowering, live| {
lowering.draw_patches(
live,
number_of_patch_control_points,
patch_start,
patch_count,
@@ -11979,8 +12051,9 @@ impl RenderCommandEncoder {
indirect_buffer_offset: u64,
) -> Result<(), Error> {
match self.lowers_at_call() {
true => self.at_call(|lowering| {
true => self.draw_at_call(|lowering, live| {
lowering.draw_patches_indirect(
live,
number_of_patch_control_points,
patch_index_buffer,
patch_index_buffer_offset,
@@ -12026,8 +12099,9 @@ impl RenderCommandEncoder {
base_instance: u32,
) -> Result<(), Error> {
match self.lowers_at_call() {
true => self.at_call(|lowering| {
true => self.draw_at_call(|lowering, live| {
lowering.draw_indexed_patches(
live,
number_of_patch_control_points,
patch_start,
patch_count,
@@ -12076,8 +12150,9 @@ impl RenderCommandEncoder {
indirect_buffer_offset: u64,
) -> Result<(), Error> {
match self.lowers_at_call() {
true => self.at_call(|lowering| {
true => self.draw_at_call(|lowering, live| {
lowering.draw_indexed_patches_indirect(
live,
number_of_patch_control_points,
patch_index_buffer,
patch_index_buffer_offset,
@@ -77,7 +77,7 @@
use super::extended_shadow::ExtendedShadows;
use super::{
reads_handles, DrawingState, Opening, RenderLog, RenderLowering, RenderPipelineState,
reads_handles, DrawingState, Lowering, Opening, RenderLog, RenderLowering, RenderPipelineState,
RenderStateKind,
};
use crate::attachment_stand_in::{self, StandInCopies};
@@ -455,7 +455,7 @@ impl RenderLowering {
later: &Later,
group: u32,
log: &RenderLog,
) -> Result<Box<RenderLowering>, Error> {
) -> Result<Lowering, Error> {
const CALL: &str = "end_encoding";
let grouping = ColorGroup::of(later.group.present.clone(), later.group.width, group)
.ok_or_else(|| internal("a later color group of a pass in one"))?;
@@ -71,11 +71,12 @@ use super::call_checks::{CallChecks, Checked};
use super::lowering_worker::{self, LoweringWorker};
use super::queued_commits::QueuedCommits;
use super::replay::{render, DrawIndexed};
use super::{RenderCall, RenderCommandEncoder, RenderLowering};
use super::{Lowering, RenderCall, RenderCommandEncoder, RenderLowering};
use crate::command_buffer::{CommandBuffer, WeakCommandBuffer};
use crate::device::{lock, Device};
use crate::error::{internal, EncoderCall, Error};
use crate::exit_guard;
use crate::live_bind::LiveBind;
use crate::replay::CallLog;
use metal2vulkan::device::RenderLoweringPolicy;
use std::collections::VecDeque;
@@ -199,12 +200,12 @@ const _: () = {
sent::<Lowerer>();
};
/// An encoder's own lowering (`RenderLoweringPolicy::AtCall`), boxed where it is made
/// ([`RenderLowering::assemble`]).
pub(super) struct OwnLowering(Box<RenderLowering>);
/// An encoder's own lowering (`RenderLoweringPolicy::AtCall`), with its bind state, each boxed
/// where it is made ([`RenderLowering::assemble`]).
pub(super) struct OwnLowering(Lowering);
// SAFETY: an `OwnLowering` is `Send` exactly when `RenderLowering` is, its one field being a box of
// one; the constant below makes the compiler prove that. Written out, as `LoweringSlot`'s are, the
// SAFETY: an `OwnLowering` is `Send` exactly when `Lowering` is, its one field being one; the
// constant below makes the compiler prove that. Written out, as `LoweringSlot`'s are, the
// impl ends a program's crate's walk of an encoder's fields there: the walk from a
// `RenderCommandEncoder` through its own lowering ran past rustc's default recursion limit
// (`tests/consumer_auto_traits.rs`).
@@ -213,27 +214,28 @@ unsafe impl Send for OwnLowering {}
// The compiler's proof of what `OwnLowering`'s `Send` impl rests on.
const _: () = {
const fn sent<T: Send>() {}
sent::<RenderLowering>();
sent::<Lowering>();
};
impl std::ops::Deref for OwnLowering {
type Target = RenderLowering;
type Target = Lowering;
fn deref(&self) -> &RenderLowering {
fn deref(&self) -> &Lowering {
&self.0
}
}
impl std::ops::DerefMut for OwnLowering {
fn deref_mut(&mut self) -> &mut RenderLowering {
fn deref_mut(&mut self) -> &mut Lowering {
&mut self.0
}
}
/// A slot's lowering, and whether it refused a call.
struct Lowerer {
/// Its encoder's lowering: `None` once the encoder ended, and in a slot not taken.
lowering: Option<Box<RenderLowering>>,
/// Its encoder's lowering, with its bind state: `None` once the encoder ended, and in a slot
/// not taken.
lowering: Option<Lowering>,
/// A call was refused as it was lowered: no later call is lowered.
refused: bool,
}
@@ -535,7 +537,7 @@ pub(crate) struct DeferredEncoders {
impl DeferredEncoders {
/// A slot for `lowering`'s encoder, which logs its calls, with its checks at the call: one
/// kept, its checks' room reused, else a new one.
fn take(&self, lowering: Box<RenderLowering>) -> Arc<LoweringSlot> {
fn take(&self, lowering: Lowering) -> Arc<LoweringSlot> {
let mut slots = lock(&self.slots);
let kept = slots.iter().find(|slot| !lock(&slot.state).taken).cloned();
let slot = match kept {
@@ -710,7 +712,7 @@ impl EndInFlight {
impl RenderCommandEncoder {
/// The program's encoder over `lowering`, which lowers its calls as the device's policy says
/// (the module docs).
pub(super) fn new(lowering: Box<RenderLowering>) -> RenderCommandEncoder {
pub(super) fn new(lowering: Lowering) -> RenderCommandEncoder {
let device = lowering.core.device();
let logs = device.compile_device().policy().render_lowering == RenderLoweringPolicy::Worker
&& lowering.grouped.is_none()
@@ -748,7 +750,7 @@ impl RenderCommandEncoder {
/// as it began lowering its calls at the call.
pub(crate) fn at_call<T>(
&mut self,
f: impl FnOnce(&mut RenderLowering) -> Result<T, Error>,
f: impl FnOnce(&mut Lowering) -> Result<T, Error>,
) -> Result<T, Error> {
self.next += 1;
match (&mut self.own, &self.logged) {
@@ -763,9 +765,21 @@ impl RenderCommandEncoder {
}
}
/// Run `f`, a draw, on the lowering now, as [`at_call`](Self::at_call) does, with the bind
/// state its draws bring in place beside it (`Lowering::parts`).
pub(crate) fn draw_at_call(
&mut self,
f: impl FnOnce(&mut RenderLowering, &mut LiveBind) -> Result<(), Error>,
) -> Result<(), Error> {
self.at_call(|lowering| {
let (lowering, live) = lowering.parts();
f(lowering, live)
})
}
/// Run `f`, a call that returns nothing, on the lowering now, as [`at_call`](Self::at_call)
/// does.
pub(crate) fn at_call_unit(&mut self, f: impl FnOnce(&mut RenderLowering)) {
pub(crate) fn at_call_unit(&mut self, f: impl FnOnce(&mut Lowering)) {
self.next += 1;
match (&mut self.own, &self.logged) {
(Some(lowering), _) => f(lowering),
@@ -72,6 +72,7 @@ use crate::degradation::EachWatch;
use crate::draw_source::DrawSource;
use crate::error::Error;
use crate::execution::{ExecutedPipeline, Execution};
use crate::live_bind::LiveBind;
use crate::noted_apart::NotedInto;
use crate::resource::ResourceKey;
use crate::tracker::{Use, UseRange};
@@ -250,9 +251,11 @@ impl RenderLowering {
/// Whether the draw of `call` assembling `assembly`, `draw` with `buffers`, is the kept draw
/// again (the module docs): then, but where it is checked (its check left in `checked`), it
/// is recorded. Call it once the draw made the heaps' new textures resident.
/// is recorded, with the encoder's bind state `live`. Call it once the draw made the heaps'
/// new textures resident.
pub(super) fn draw_again(
&mut self,
live: &mut LiveBind,
call: &'static str,
assembly: Assembly,
draw: &DrawCall,
@@ -279,7 +282,7 @@ impl RenderLowering {
let stamps = self.table_stamps();
if kept.stamps != stamps {
return match kept.rebinds {
true => self.draw_rebound(call, (assembly, draw), buffers, stamps, checked),
true => self.draw_rebound(live, call, (assembly, draw), buffers, stamps, checked),
false => Again::No,
};
}
@@ -288,7 +291,7 @@ impl RenderLowering {
let hits = self.again.hits;
self.again.hits += 1;
if hits.is_multiple_of(2) {
let held = Held::of(self);
let held = Held::of(self, live);
*checked = Some(Checked::of(self, Expected::Plain(held)));
return Again::Check;
}
@@ -299,7 +302,7 @@ impl RenderLowering {
// Its noting: its reads of fresh textures, where any texture is fresh, and what the
// encoder made resident since (nothing: no call came).
match kept.reads_images && self.core.observes_fresh() {
true => self.core.note_again(super::lock(&self.live).noted.uses()),
true => self.core.note_again(live.noted.uses()),
false => self.core.note_again(&[]),
}
// SAFETY: the kept draw recorded this command into this command buffer, which records
@@ -334,6 +337,7 @@ impl RenderLowering {
/// `checked`, a kept draw again that took every step, check it (the module docs).
pub(super) fn kept_again(
&mut self,
live: &LiveBind,
watch: &EachWatch,
(reads, rasterizes, depth_writes): (RenderStages, bool, bool),
checked: Option<Checked>,
@@ -345,10 +349,10 @@ impl RenderLowering {
}
#[cfg(debug_assertions)]
if let Some(checked) = checked {
checked.check(self, watch.met());
checked.check(self, live, watch.met());
}
#[cfg(not(debug_assertions))]
let _: Option<Checked> = checked;
let _: (&LiveBind, Option<Checked>) = (live, checked);
if watch.met() {
self.forget_again();
}
@@ -484,7 +488,7 @@ impl Held {
"pass",
];
fn of(encoder: &RenderLowering) -> Held {
fn of(encoder: &RenderLowering, live: &LiveBind) -> Held {
let current = &encoder.current;
let pass = (
&encoder.inputs_read,
@@ -498,7 +502,7 @@ impl Held {
digest(&encoder.coverage),
digest(encoder.pass_start.held()),
digest(current.shadows.gathered()),
digest(&super::lock(&encoder.live).noted),
digest(&live.noted),
digest(pass),
],
instance: (
@@ -552,7 +556,7 @@ impl Checked {
/// Check the draw that took every step, which `met` a notice met at each occurrence or not,
/// against the kept one (the module docs).
fn check(self, encoder: &mut RenderLowering, met: bool) {
fn check(self, encoder: &mut RenderLowering, live: &LiveBind, met: bool) {
let Checked {
kept,
hits,
@@ -562,7 +566,7 @@ impl Checked {
let rebound = matches!(expected, Expected::Rebound(_));
match &expected {
// Its uses differ from the kept draw's: what noting them adds is every step's own.
Expected::Rebound(rebound) => rebound.assert_as(&crate::device::lock(&encoder.live)),
Expected::Rebound(rebound) => rebound.assert_as(live),
Expected::Plain(held) => {
assert_eq!(
crate::degradation::announced_here(),
@@ -571,7 +575,7 @@ impl Checked {
);
assert!(!met, "a draw again met a notice met at each occurrence");
let executed = kept.execution.is_some();
Held::of(encoder).assert_as(held, (kept.index.is_some(), executed));
Held::of(encoder, live).assert_as(held, (kept.index.is_some(), executed));
}
}
let again = encoder.again.kept.as_mut();
@@ -259,10 +259,12 @@ struct Rebinding {
impl RenderLowering {
/// The kept draw again after calls that bound other buffers, offsets, textures or samplers in
/// its tables, whose stamps are now `stamps` (the module docs): recorded, refused, or
/// [`Again::No`] where it takes every step (its check left in `checked` where it is checked).
/// its tables, whose stamps are now `stamps` (the module docs), with the encoder's bind state
/// `live`: recorded, refused, or [`Again::No`] where it takes every step (its check left in
/// `checked` where it is checked).
pub(super) fn draw_rebound(
&mut self,
live: &mut LiveBind,
call: &'static str,
(assembly, draw): (Assembly, &DrawCall),
buffers: &DrawBuffers<'_>,
@@ -271,9 +273,6 @@ impl RenderLowering {
) -> Again {
// A notice met at each occurrence forgets the draw once recorded, as its every step does.
let watch = crate::degradation::EachWatch::start();
let live = Arc::clone(&self.live);
let mut held = crate::device::lock(&live);
let live = &mut *held;
let Some(changed) = self.bring_in(live, stamps) else {
return Again::No;
};
@@ -286,7 +285,6 @@ impl RenderLowering {
self.again.hits += 1;
if hits.is_multiple_of(2) {
let rebound = super::Expected::Rebound(super::Rebound::of(live));
drop(held);
*checked = Some(super::Checked::of(self, rebound));
return Again::Check;
}
@@ -305,6 +305,7 @@ use crate::indirect_command_buffer::{
use crate::indirect_dispatch;
use crate::indirect_execute::{self, unstrided_stride, ExecutionRange};
use crate::kept_commands;
use crate::live_bind::LiveBind;
use crate::pipeline::budget::Urgency;
use crate::pipeline::RenderPipelineState;
use crate::pipeline::Settled;
@@ -1820,8 +1821,14 @@ impl RenderLowering {
/// class), the variant in the large-buffer form and the `VkPipeline` of the draw in the
/// execution's instance, an indexed draw's alike ([`settle_draw_of`](Self::settle_draw_of)).
/// A tessellation or mesh pipeline's draws, and a state the pass refuses, are left to the
/// step, which meets them as they are then. Announces nothing: it draws nothing.
fn settle_draws(&mut self, pipeline: &RenderPipelineState, made: &mut Vec<SettledDraw>) {
/// step, which meets them as they are then. Announces nothing: it draws nothing. The draws
/// settle with the bind state `live`.
fn settle_draws(
&mut self,
live: &mut LiveBind,
pipeline: &RenderPipelineState,
made: &mut Vec<SettledDraw>,
) {
let RenderStateKind::Graphics(state) = pipeline.kind() else {
return;
};
@@ -1851,7 +1858,7 @@ impl RenderLowering {
// The encoder's pipeline state, which its bind state reads, as a command's draw sets it.
self.pipeline = Some(pipeline.clone());
for &primitive_type in types {
if let Ok(settled) = self.settle_draw_of(state, primitive_type) {
if let Ok(settled) = self.settle_draw_of(live, state, primitive_type) {
made.push(settled);
}
}
@@ -1871,6 +1878,7 @@ impl RenderLowering {
/// (a layout of per-bind strides that are not dynamic state).
fn settle_draw_of(
&self,
live: &mut LiveBind,
state: &DrawingState,
primitive_type: PrimitiveType,
) -> Result<SettledDraw, Error> {
@@ -1880,11 +1888,10 @@ impl RenderLowering {
let settings = self.draw_settings(CALL, state, assembly, false)?;
let mut argument_samplers = ArgumentSamplers::taken(self.core.device());
let function_tables = state.function_tables();
let live = Arc::clone(&self.live);
let pass_start = PassStartTables::default();
let sampler_maps = self.sampler_maps(state, &pass_start)?;
let settled = self.settle_draw(
&mut lock(&live),
live,
(
state,
&settings,
@@ -2093,6 +2100,8 @@ impl RenderExecution {
(storage, layout, residency.clone()),
forms,
);
// The commands' draws start from an empty bind state, as an encoder's first draw may.
let mut live = LiveBind::default();
if !step {
// Counted before the records are read: a write meanwhile records it again.
self.read_at.store(self.icb.cpu_writes(), Ordering::Release);
@@ -2104,7 +2113,8 @@ impl RenderExecution {
let (written, copies) = reads.read(&self.icb)?;
let (commands, written, _) = self.named_commands(&device, range.start, written);
found.faults = ReplayFaults(found.faults.0 | written.0);
self.replay(&mut encoder, commands.iter().flatten(), residency, found)?;
let replaying = (&mut encoder, &mut live);
self.replay(replaying, commands.iter().flatten(), residency, found)?;
copies.keep(&self.icb);
return self.finish(encoder, found, rendered, &device);
}
@@ -2119,7 +2129,7 @@ impl RenderExecution {
self.report(command_buffer, read, start, executed);
}
let commands = commands.iter().flatten();
self.replay(&mut encoder, commands, residency, found)?;
self.replay((&mut encoder, &mut live), commands, residency, found)?;
continue;
}
let (commands, released) = self.icb.render_records(piece, Reader::Commit)?;
@@ -2135,7 +2145,7 @@ impl RenderExecution {
let read = (RecordsRead::Commit, residency);
self.report(command_buffer, read, start, executed);
}
self.replay(&mut encoder, &commands, residency, found)?;
self.replay((&mut encoder, &mut live), &commands, residency, found)?;
}
self.finish(encoder, found, rendered, &device)
}
@@ -2174,7 +2184,7 @@ impl RenderExecution {
/// plain draw (`RenderLowering::replay_plain`).
fn replay<'c>(
&self,
encoder: &mut RenderLowering,
(encoder, live): (&mut RenderLowering, &mut LiveBind),
commands: impl IntoIterator<Item = &'c Command>,
residency: &Residency,
found: &mut Found,
@@ -2273,14 +2283,14 @@ impl RenderExecution {
}
match draw {
Draw::Primitives(assembly, draw) => {
encoder.draw(CALL, assembly, draw, DrawBuffers::default(), false)?
encoder.draw(live, CALL, assembly, draw, DrawBuffers::default(), false)?
}
Draw::Indexed(draw, (buffer, offset)) => {
encoder.draw_indexed(CALL, draw, &buffer, offset)?;
encoder.draw_indexed(live, CALL, draw, &buffer, offset)?;
}
Draw::Mesh(grid, object, mesh) => {
encoder.replay_plain = false;
encoder.draw_mesh(CALL, grid, object, mesh)?
encoder.draw_mesh(live, CALL, grid, object, mesh)?
}
Draw::Patches {
mut draw,
@@ -2311,7 +2321,7 @@ impl RenderExecution {
PointIndices::Zero => zeros.as_ref().map(|zero| (zero, 0)),
PointIndices::Buffer((b, offset)) => Some((b, *offset)),
};
encoder.draw_patch_range(CALL, draw, patch_indices, points)?;
encoder.draw_patch_range(live, CALL, draw, patch_indices, points)?;
}
}
}
@@ -2559,7 +2569,6 @@ impl RenderExecution {
pass_start: snapshot.pass_start.notes(),
// An execution's draws restart at the all-ones index whatever the encoder's call.
restart: None,
live: Arc::default(),
log: None,
quiet_bound_values: Default::default(),
attached: Default::default(),
@@ -2626,8 +2635,9 @@ impl RenderExecution {
(storage, snapshot.layout.clone(), residency.clone()),
BufferForms::Large,
);
let mut live = LiveBind::default();
for pipeline in named {
encoder.settle_draws(pipeline, made);
encoder.settle_draws(&mut live, pipeline, made);
}
let RenderLowering { core, .. } = encoder;
EncoderCore::unboxed(core)
@@ -70,7 +70,6 @@
//! states why (`bind::ReplayFaults`), as the host step's; the others draw.
use std::ops::Range;
use std::sync::Arc;
use super::super::bound::{Bound, Recorded};
use super::super::line_width::LineRoute;
@@ -81,12 +80,12 @@ use super::{HostStepCause, PassStorage, RenderExecution, Rendered, CALL};
use crate::binding::PushWrite;
use crate::blit::{reads_by, writes_by};
use crate::command_buffer::{CommandBuffer, ReplayTable};
use crate::device::lock;
use crate::draw_source::DrawSource;
use crate::encoder::EncoderCore;
use crate::error::{internal, Error};
use crate::executed_commands::RecordsRead;
use crate::indirect_execute::ExecutionRange;
use crate::live_bind::LiveBind;
use crate::owned_kernel::OwnedModule;
use crate::pipeline::{BufferForms, GraphicsState, RenderStateKind, Settled};
use crate::residency::Residency;
@@ -350,7 +349,7 @@ impl RenderExecution {
.check_pipeline(CALL, pipeline, &encoder.layout)
.and_then(|()| {
encoder.pipeline = Some(pipeline.clone());
encoder.replay_draw_of(state, primitive_type)
encoder.replay_draw_of(&mut LiveBind::default(), state, primitive_type)
});
let RenderLowering { core, .. } = encoder;
let form = match slots {
@@ -620,7 +619,15 @@ impl RenderExecution {
..DrawBuffers::default()
};
let assembly = Assembly::Primitives(form.primitive_type);
encoder.draw(CALL, assembly, draw, buffers, false)?;
// Its draw starts from an empty bind state, as an encoder's first draw may.
encoder.draw(
&mut LiveBind::default(),
CALL,
assembly,
draw,
buffers,
false,
)?;
encoder.replayed = None;
let counts = &device.shared().counts;
counts.gpu_form_commands(u64::from(pre.layout.commands));
@@ -673,6 +680,7 @@ impl RenderLowering {
/// nothing: it draws nothing.
fn replay_draw_of(
&self,
live: &mut LiveBind,
state: &crate::pipeline::GraphicsState,
primitive_type: PrimitiveType,
) -> Result<Result<(u32, u32), HostStepCause>, Error> {
@@ -725,12 +733,10 @@ impl RenderLowering {
}
let mut argument_samplers = ArgumentSamplers::taken(self.core.device());
let function_tables = state.function_tables();
let live = Arc::clone(&self.live);
let pass_start = PassStartTables::default();
let sampler_maps = self.sampler_maps(state, &pass_start)?;
let mut live = lock(&live);
let (Settled { variant, plan, .. }, _) = self.settle_draw(
&mut live,
live,
(
state,
&settings,
@@ -135,7 +135,7 @@ use metal2vulkan::metal::IndirectCommandType;
use std::collections::BTreeSet;
use std::ops::Range;
use std::sync::atomic::Ordering;
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use std::time::Instant;
/// What a chunk of an execution's draws is recorded from beside its records (the module docs).
@@ -231,7 +231,7 @@ pub(super) struct Keeping {
/// the note of them its pass's core takes for the chunks (`EncoderCore::capture_apart`).
struct Carried {
/// The bind state (`live_bind.rs`), its heap parts kept (`LiveBind::end_chunk`).
live: Arc<Mutex<LiveBind>>,
live: Box<LiveBind>,
/// The bind memo (`binding::BindMemo`).
memo: BindMemo,
residency: Residency,
@@ -252,7 +252,7 @@ struct Carried {
impl Carried {
/// What the first chunk starts from: `live` and `memo`, a copy of `residency`.
fn first((live, memo): (Arc<Mutex<LiveBind>>, BindMemo), residency: &Residency) -> Carried {
fn first((live, memo): (Box<LiveBind>, BindMemo), residency: &Residency) -> Carried {
Carried {
live,
memo,
@@ -265,11 +265,13 @@ impl Carried {
}
}
/// A chunk recorded apart: its encoder after its commands, the addresses they named and what
/// they resolved to, the no-copy buffers' moves before it read its records, whether a command
/// of it named a released state, and for a host step the bytes of its records.
/// A chunk recorded apart: its encoder after its commands, with the bind state its draws brought
/// in place, the addresses they named and what they resolved to, the no-copy buffers' moves
/// before it read its records, whether a command of it named a released state, and for a host
/// step the bytes of its records.
struct Apart {
encoder: RenderLowering,
live: Box<LiveBind>,
resolved: Vec<Resolution>,
moved: u64,
released: bool,
@@ -697,6 +699,7 @@ impl RenderExecution {
}
let Apart {
encoder,
live,
resolved,
moved,
released,
@@ -709,12 +712,12 @@ impl RenderExecution {
if !plain {
self.keep_chunk(chunk, None, read_at);
reads_back.push((chunk, records));
rest = Some((encoder, covered.end, chunk_found));
rest = Some((encoder, live, covered.end, chunk_found));
break;
}
let raw = encoder.current.draws;
let heaps = encoder.replayed_heaps.clone();
let (capture, back) = self.end_apart(encoder, &device)?;
let (capture, back) = self.end_apart((encoder, live), &device)?;
carried = Some(back);
let captured = capture.finish_capture()?;
let held = captured.retained.all_shared();
@@ -756,7 +759,7 @@ impl RenderExecution {
shared.live_binds.give(live);
shared.bind_memos.give(memo);
}
let Some((mut encoder, from, chunk_found)) = rest else {
let Some((mut encoder, mut live, from, chunk_found)) = rest else {
self.read_back(reads_back);
let run = Run {
draws,
@@ -794,14 +797,15 @@ impl RenderExecution {
let (commands, faults, _) =
self.named_commands(&device, covered.start, written);
found.faults = ReplayFaults(found.faults.0 | faults.0);
self.replay(&mut encoder, commands.iter().flatten(), residency, found)?;
let replaying = (&mut encoder, &mut *live);
self.replay(replaying, commands.iter().flatten(), residency, found)?;
}
}
false => {
for piece in kept_commands::pieces(from..range.end, false) {
let (commands, released) = self.icb.render_records(piece, Reader::Commit)?;
self.icb.degrade_released(&released);
self.replay(&mut encoder, &commands, residency, found)?;
self.replay((&mut encoder, &mut *live), &commands, residency, found)?;
}
}
}
@@ -872,7 +876,7 @@ impl RenderExecution {
let raw = command_buffer.device().shared().vk();
let visibility = Visibility::borrowing(None, None, raw, draws);
let Carried {
live,
mut live,
memo,
residency: carried,
gathered,
@@ -886,14 +890,15 @@ impl RenderExecution {
(storage.clone(), layout.clone(), carried),
forms,
);
(encoder.live, encoder.bind_memo) = (live, memo);
encoder.bind_memo = memo;
(encoder.unreadable, encoder.resident_again) = (unreadable, resident_again);
if let Some(gathered) = gathered {
encoder.current.shadows = gathered;
}
self.replay(&mut encoder, commands(), residency, found)?;
self.replay((&mut encoder, &mut *live), commands(), residency, found)?;
Ok(Apart {
encoder,
live,
resolved,
moved,
released,
@@ -926,7 +931,7 @@ impl RenderExecution {
/// starts from ([`Carried`]), its encoder ended (`LiveBind::end_chunk`).
fn end_apart(
&self,
encoder: RenderLowering,
(encoder, mut live): (RenderLowering, Box<LiveBind>),
device: &crate::Device,
) -> Result<(EncoderCore, Carried), Error> {
let raw = device.shared().vk();
@@ -934,7 +939,6 @@ impl RenderExecution {
core,
current,
mut visibility,
mut live,
bind_memo,
residency,
unreadable,
@@ -945,12 +949,8 @@ impl RenderExecution {
visibility.close_spares(raw, current.draws);
// SAFETY: only this recording records the secondary command buffer.
unsafe { core.command_buffer().end_raw(current.draws) }.map_err(vulkan)?;
// No draw holds it once its encoder ended; one that did leaves the next chunk a new one.
// Its heap parts stay for the next chunk's draws (`LiveBind::end_chunk`).
match Arc::get_mut(&mut live).map(Mutex::get_mut) {
Some(Ok(state)) => state.end_chunk(),
Some(Err(_)) | None => live = Arc::default(),
}
live.end_chunk();
let resident = core.resident_part();
let carried = Carried {
live,
@@ -61,6 +61,7 @@ use crate::device::functions::Vk;
use crate::error::{internal, Error};
use crate::framebuffer_fetch;
use crate::image_plan::ImagePlan;
use crate::live_bind::LiveBind;
use crate::owned_kernel::{OwnedKernel, OwnedModule};
use crate::pipeline::GraphicsState as DrawingState;
use crate::render_pass::{PassLayout, RenderPassDescriptor, Target};
@@ -484,8 +485,12 @@ impl RenderLowering {
/// an earlier draw of the pass wrote or whose clear their codes cannot hold (the module
/// docs), in a multisampled pass every color it blends into or reads: those no earlier
/// instance wrote, the current instance's draws recorded again first into one that leaves
/// them out where they wrote them.
pub(super) fn start_extended_shadows(&mut self, pipeline: &DrawingState) -> Result<(), Error> {
/// them out where they wrote them, with the encoder's bind state `live`.
pub(super) fn start_extended_shadows(
&mut self,
live: &mut LiveBind,
pipeline: &DrawingState,
) -> Result<(), Error> {
if !self.extended.holds
|| self.replaying
|| self.rerouting
@@ -550,7 +555,7 @@ impl RenderLowering {
colors.extend(starting.iter().copied());
colors.sort_unstable();
colors.dedup();
match self.record_leaving_out(colors, pipeline)? {
match self.record_leaving_out(live, colors, pipeline)? {
true => self
.extended
.fills
+22 -7
View File
@@ -110,6 +110,7 @@ use crate::device::functions::Vk;
use crate::draw_source::DrawSource;
use crate::encoder::EncoderCore;
use crate::error::{internal, invalid, Error, Problem};
use crate::live_bind::LiveBind;
use crate::owned_kernel::{OwnedKernel, OwnedModule, OwnedSets};
use crate::pipeline::RawPipeline;
use crate::resource::ResourceKey;
@@ -377,6 +378,7 @@ impl RenderLowering {
/// The lowering of [`RenderCommandEncoder::draw_mesh_threadgroups`].
pub(crate) fn draw_mesh_threadgroups(
&mut self,
live: &mut LiveBind,
groups: Size,
threads_per_object_group: Size,
threads_per_mesh_group: Size,
@@ -392,12 +394,13 @@ impl RenderLowering {
let grid = MeshGrid::Threadgroups(axes(CALL, groups)?);
let object = axes(CALL, threads_per_object_group)?;
let mesh = axes(CALL, threads_per_mesh_group)?;
self.draw_mesh(CALL, grid, object, mesh)
self.draw_mesh(live, CALL, grid, object, mesh)
}
/// The lowering of [`RenderCommandEncoder::draw_mesh_threads`].
pub(crate) fn draw_mesh_threads(
&mut self,
live: &mut LiveBind,
threads: Size,
threads_per_object_group: Size,
threads_per_mesh_group: Size,
@@ -413,7 +416,7 @@ impl RenderLowering {
let grid = MeshGrid::Threads(axes(CALL, threads)?);
let object = axes(CALL, threads_per_object_group)?;
let mesh = axes(CALL, threads_per_mesh_group)?;
self.draw_mesh(CALL, grid, object, mesh)
self.draw_mesh(live, CALL, grid, object, mesh)
}
/// The direct mesh draw of `grid` for `call`, its object and mesh threadgroups of `object`
@@ -421,6 +424,7 @@ impl RenderLowering {
/// an indirect command buffer's mesh command (`render/executions.rs`).
pub(super) fn draw_mesh(
&mut self,
live: &mut LiveBind,
call: &'static str,
grid: MeshGrid,
object: [u32; 3],
@@ -446,12 +450,20 @@ impl RenderLowering {
threads_per_mesh_group: mesh,
},
};
self.draw(call, Assembly::Meshes, draw, DrawBuffers::default(), empty)
self.draw(
live,
call,
Assembly::Meshes,
draw,
DrawBuffers::default(),
empty,
)
}
/// The lowering of [`RenderCommandEncoder::draw_mesh_threadgroups_indirect`].
pub(crate) fn draw_mesh_threadgroups_indirect(
&mut self,
live: &mut LiveBind,
indirect_buffer: &Buffer,
indirect_buffer_offset: u64,
threads_per_object_group: Size,
@@ -499,7 +511,7 @@ impl RenderLowering {
indirect: Some(indirect),
..DrawBuffers::default()
};
self.draw(CALL, Assembly::Meshes, draw, buffers, false)
self.draw(live, CALL, Assembly::Meshes, draw, buffers, false)
}
/// Whether a mesh draw of `object` and `mesh` threads in its object and mesh threadgroups
@@ -821,8 +833,9 @@ impl RenderCommandEncoder {
threads_per_mesh_group: Size,
) -> Result<(), Error> {
match self.lowers_at_call() {
true => self.at_call(|lowering| {
true => self.draw_at_call(|lowering, live| {
lowering.draw_mesh_threadgroups(
live,
groups,
threads_per_object_group,
threads_per_mesh_group,
@@ -848,8 +861,9 @@ impl RenderCommandEncoder {
threads_per_mesh_group: Size,
) -> Result<(), Error> {
match self.lowers_at_call() {
true => self.at_call(|lowering| {
true => self.draw_at_call(|lowering, live| {
lowering.draw_mesh_threads(
live,
threads,
threads_per_object_group,
threads_per_mesh_group,
@@ -885,8 +899,9 @@ impl RenderCommandEncoder {
threads_per_mesh_group: Size,
) -> Result<(), Error> {
match self.lowers_at_call() {
true => self.at_call(|lowering| {
true => self.draw_at_call(|lowering, live| {
lowering.draw_mesh_threadgroups_indirect(
live,
indirect_buffer,
indirect_buffer_offset,
threads_per_object_group,
@@ -15,7 +15,7 @@
//! commit did (no draw whose pipeline was still being made).
//! - **Per-stage calls** share a variant, with the [`Table`] they set.
use super::RenderLowering;
use super::{Lowering, RenderLowering};
use crate::amplification::VertexAmplificationViewMapping;
use crate::buffer::Buffer;
use crate::command_buffer::CommandBuffer;
@@ -125,7 +125,7 @@ impl RenderLog {
/// (`render/color_groups.rs`). A call refused there was refused in the first group's
/// encoder, whose answer the program had, or is a divergence the later group's encoder notes
/// (`RenderCommandEncoder::group_outcome`): either way the calls after it run.
pub(crate) fn replay_into(&self, encoder: &mut RenderLowering) {
pub(crate) fn replay_into(&self, encoder: &mut Lowering) {
for call in &self.calls.calls {
let _ = render(encoder, (&self.calls, &self.indexed), call);
}
@@ -669,15 +669,17 @@ impl RenderLowering {
log.calls.push(call, extra);
}
}
}
impl Lowering {
/// Run `run`, a public call made at once (`RenderCommandEncoder::at_call`, a call its checks
/// do not cover, `render/deferral.rs`), which logs itself as it starts ([`log`](Self::log)):
/// where it is refused, its log notes it so (`CallLog::refuse`), and an encoding again
/// refuses it again and goes on (`replay.rs`). A call refused before it logged itself left
/// nothing in the log.
/// do not cover, `render/deferral.rs`), which logs itself as it starts
/// (`RenderLowering::log`): where it is refused, its log notes it so (`CallLog::refuse`), and
/// an encoding again refuses it again and goes on (`replay.rs`). A call refused before it
/// logged itself left nothing in the log.
pub(crate) fn logged<T>(
&mut self,
run: impl FnOnce(&mut RenderLowering) -> Result<T, Error>,
run: impl FnOnce(&mut Lowering) -> Result<T, Error>,
) -> Result<T, Error> {
let at = self.log.as_ref().map(|log| log.calls.calls.len());
let ran = run(self);
@@ -688,7 +690,9 @@ impl RenderLowering {
}
ran
}
}
impl RenderLowering {
/// Log a `draw_indexed_primitives` call, whose arguments `draw` makes of the log, before it
/// runs, where the encoder logs: its arguments go into the log's list of them. It keeps the
/// encoder's kept draw, as [`log_keeping`](Self::log_keeping) does.
@@ -712,7 +716,7 @@ impl RenderLowering {
/// `indexed`: a rebind's encoding again, a later color group's, or a deferred call's lowering
/// (`render/deferral.rs`).
pub(super) fn render(
encoder: &mut RenderLowering,
encoder: &mut Lowering,
(log, indexed): (&CallLog<RenderCall>, &[DrawIndexed]),
call: &RenderCall,
) -> Result<(), Error> {
@@ -889,13 +893,16 @@ pub(super) fn render(
encoder.set_vertex_amplification_count(*count, mappings.as_deref())
}
C::DrawPrimitives(primitive, start, count, instances, base) => {
encoder.draw_primitives(*primitive, *start, *count, *instances, *base)
let (encoder, live) = encoder.parts();
encoder.draw_primitives(live, *primitive, *start, *count, *instances, *base)
}
C::DrawIndexedPrimitives(at) => {
let call = indexed
.get(*at as usize)
.ok_or_else(|| crate::error::internal("a logged indexed draw past its list"))?;
let (encoder, live) = encoder.parts();
encoder.draw_indexed_primitives(
live,
call.primitive_type,
call.index_count,
call.index_type,
@@ -907,52 +914,73 @@ pub(super) fn render(
)
}
C::DrawPrimitivesIndirect(primitive, buffer, offset) => {
encoder.draw_primitives_indirect(*primitive, buffer, *offset)
let (encoder, live) = encoder.parts();
encoder.draw_primitives_indirect(live, *primitive, buffer, *offset)
}
C::DrawIndexedPrimitivesIndirect(call) => {
let (encoder, live) = encoder.parts();
encoder.draw_indexed_primitives_indirect(
live,
call.primitive_type,
call.index_type,
&call.index_buffer,
call.index_buffer_offset,
&call.indirect_buffer,
call.indirect_buffer_offset,
)
}
C::DrawPatches(call) => {
let (encoder, live) = encoder.parts();
encoder.draw_patches(
live,
call.control_points,
call.patch_start,
call.patch_count,
call.patch_index_buffer.as_ref(),
call.patch_index_buffer_offset,
call.instance_count,
call.base_instance,
)
}
C::DrawPatchesIndirect(call) => {
let (encoder, live) = encoder.parts();
encoder.draw_patches_indirect(
live,
call.control_points,
call.patch_index_buffer.as_ref(),
call.patch_index_buffer_offset,
&call.indirect_buffer,
call.indirect_buffer_offset,
)
}
C::DrawIndexedPatches(call) => {
let (encoder, live) = encoder.parts();
encoder.draw_indexed_patches(
live,
call.control_points,
call.patch_start,
call.patch_count,
call.patch_index_buffer.as_ref(),
call.patch_index_buffer_offset,
&call.control_point_index_buffer,
call.control_point_index_buffer_offset,
call.instance_count,
call.base_instance,
)
}
C::DrawIndexedPatchesIndirect(call) => {
let (encoder, live) = encoder.parts();
encoder.draw_indexed_patches_indirect(
live,
call.control_points,
call.patch_index_buffer.as_ref(),
call.patch_index_buffer_offset,
&call.control_point_index_buffer,
call.control_point_index_buffer_offset,
&call.indirect_buffer,
call.indirect_buffer_offset,
)
}
C::DrawIndexedPrimitivesIndirect(call) => encoder.draw_indexed_primitives_indirect(
call.primitive_type,
call.index_type,
&call.index_buffer,
call.index_buffer_offset,
&call.indirect_buffer,
call.indirect_buffer_offset,
),
C::DrawPatches(call) => encoder.draw_patches(
call.control_points,
call.patch_start,
call.patch_count,
call.patch_index_buffer.as_ref(),
call.patch_index_buffer_offset,
call.instance_count,
call.base_instance,
),
C::DrawPatchesIndirect(call) => encoder.draw_patches_indirect(
call.control_points,
call.patch_index_buffer.as_ref(),
call.patch_index_buffer_offset,
&call.indirect_buffer,
call.indirect_buffer_offset,
),
C::DrawIndexedPatches(call) => encoder.draw_indexed_patches(
call.control_points,
call.patch_start,
call.patch_count,
call.patch_index_buffer.as_ref(),
call.patch_index_buffer_offset,
&call.control_point_index_buffer,
call.control_point_index_buffer_offset,
call.instance_count,
call.base_instance,
),
C::DrawIndexedPatchesIndirect(call) => encoder.draw_indexed_patches_indirect(
call.control_points,
call.patch_index_buffer.as_ref(),
call.patch_index_buffer_offset,
&call.control_point_index_buffer,
call.control_point_index_buffer_offset,
&call.indirect_buffer,
call.indirect_buffer_offset,
),
C::VisibilityResultMode(mode, offset) => encoder.set_visibility_result_mode(*mode, *offset),
C::UseResource(resource, usage, stages) => {
encoder.use_resource(resource.as_ref(), *usage, *stages)
@@ -995,15 +1023,24 @@ pub(super) fn render(
}
C::MeshThreadgroups(call) => {
let (groups, object, mesh) = **call;
encoder.draw_mesh_threadgroups(groups, object, mesh)
{
let (encoder, live) = encoder.parts();
encoder.draw_mesh_threadgroups(live, groups, object, mesh)
}
}
C::MeshThreads(call) => {
let (threads, object, mesh) = **call;
encoder.draw_mesh_threads(threads, object, mesh)
{
let (encoder, live) = encoder.parts();
encoder.draw_mesh_threads(live, threads, object, mesh)
}
}
C::MeshThreadgroupsIndirect(call) => {
let (buffer, offset, object, mesh) = &**call;
encoder.draw_mesh_threadgroups_indirect(buffer, *offset, *object, *mesh)
{
let (encoder, live) = encoder.parts();
encoder.draw_mesh_threadgroups_indirect(live, buffer, *offset, *object, *mesh)
}
}
C::PrimitiveRestartEnabled(enabled, index) => {
encoder.set_primitive_restart_enabled(*enabled, *index);
@@ -41,6 +41,7 @@ use crate::amplification::Amplification;
use crate::argument_table::ArgumentTable;
use crate::draw_source::{DrawSource, KeptSource};
use crate::error::Error;
use crate::live_bind::LiveBind;
use crate::pipeline::{GraphicsState as DrawingState, RenderPipelineState};
use crate::render_state::EncoderState;
use crate::render_target::PassShape;
@@ -117,9 +118,10 @@ impl RenderLowering {
/// where its shape is the current one's; nothing where it would follow the current one (the
/// pass's first instance leaving out a color it cannot clear before the pass, among others)
/// or the draws cannot record so, which leaves the instance and the pass's layout as they
/// were.
/// were. The draws bring the encoder's bind state `live` in place.
pub(super) fn record_leaving_out(
&mut self,
live: &mut LiveBind,
colors: Vec<u32>,
by: &DrawingState,
) -> Result<bool, Error> {
@@ -191,7 +193,7 @@ impl RenderLowering {
let mut again = Ok(());
if let Some(draws) = previous.rerecord.as_mut() {
for kept in draws.iter_mut() {
again = self.record_again(kept);
again = self.record_again(live, kept);
if again.is_err() {
break;
}
@@ -226,15 +228,20 @@ impl RenderLowering {
}
}
/// Record `kept` again into the current instance, with the encoder's state at it.
pub(super) fn record_again(&mut self, kept: &mut Rerecord) -> Result<(), Error> {
/// Record `kept` again into the current instance, with the encoder's state at it and its
/// bind state `live`.
pub(super) fn record_again(
&mut self,
live: &mut LiveBind,
kept: &mut Rerecord,
) -> Result<(), Error> {
let index = kept.index.as_ref().map(KeptSource::source).transpose()?;
let buffers = DrawBuffers {
index,
..DrawBuffers::default()
};
self.swap_held(&mut kept.held);
let drawn = self.draw_resident(kept.call, kept.assembly, kept.draw, buffers, false);
let drawn = self.draw_resident(live, kept.call, kept.assembly, kept.draw, buffers, false);
self.swap_held(&mut kept.held);
drawn
}