Skip to content

Commit 94caaaa

Browse files
committed
[mtl] remove the deferred image resolution (yay)
1 parent b7fece7 commit 94caaaa

File tree

5 files changed

+123
-304
lines changed

5 files changed

+123
-304
lines changed

src/backend/metal/src/command.rs

Lines changed: 34 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ impl State {
533533
#[derive(Clone, Debug)]
534534
struct StageResources {
535535
buffers: Vec<Option<(metal::Buffer, buffer::Offset)>>,
536-
textures: Vec<Option<native::ImageRoot>>,
536+
textures: Vec<Option<metal::Texture>>,
537537
samplers: Vec<Option<metal::SamplerState>>,
538538
push_constants_buffer_id: Option<u32>,
539539
}
@@ -562,12 +562,12 @@ impl StageResources {
562562
self.buffers[slot] = Some((buffer.to_owned(), offset));
563563
}
564564

565-
fn add_textures(&mut self, start: usize, roots: &[Option<(native::ImageRoot, Layout)>]) {
566-
while self.textures.len() < start + roots.len() {
565+
fn add_textures(&mut self, start: usize, textures: &[Option<(metal::Texture, Layout)>]) {
566+
while self.textures.len() < start + textures.len() {
567567
self.textures.push(None)
568568
}
569-
for (out, root) in self.textures[start..].iter_mut().zip(roots.iter()) {
570-
*out = root.as_ref().map(|&(ref root, _)| root.clone());
569+
for (out, tex) in self.textures[start..].iter_mut().zip(textures.iter()) {
570+
*out = tex.as_ref().map(|&(ref t, _)| t.to_owned());
571571
}
572572
}
573573

@@ -748,22 +748,19 @@ impl CommandSink {
748748
}
749749
}
750750

751-
fn begin_render_pass<'a, F, I>(
751+
fn begin_render_pass<'a, I>(
752752
&mut self,
753753
keep_open: bool,
754754
descriptor: &'a metal::RenderPassDescriptorRef,
755-
frames: F,
756755
init_commands: I,
757756
) where
758-
F: Iterator<Item = (usize, native::Frame)>,
759757
I: Iterator<Item = soft::RenderCommand<&'a soft::Own>>,
760758
{
761759
self.stop_encoding();
762760

763761
match *self {
764762
CommandSink::Immediate { ref cmd_buffer, ref mut encoder_state, .. } => {
765763
let _ap = AutoreleasePool::new();
766-
resolve_frames(descriptor, frames);
767764
let encoder = cmd_buffer.new_render_command_encoder(descriptor);
768765
for command in init_commands {
769766
exec_render(encoder, command);
@@ -778,7 +775,6 @@ impl CommandSink {
778775
*is_encoding = keep_open;
779776
passes.push(soft::Pass::Render {
780777
desc: descriptor.to_owned(),
781-
frames: frames.collect(),
782778
commands: init_commands.map(soft::RenderCommand::own).collect(),
783779
});
784780
}
@@ -960,14 +956,6 @@ fn exec_render<'a>(encoder: &metal::RenderCommandEncoderRef, command: soft::Rend
960956
}
961957
}
962958
Cmd::BindTexture { stage, index, texture } => {
963-
let guard;
964-
let texture = match texture {
965-
Some(root) => {
966-
guard = root.resolve();
967-
Some(&*guard)
968-
}
969-
None => None,
970-
};
971959
match stage {
972960
pso::Stage::Vertex =>
973961
encoder.set_vertex_texture(index as _, texture),
@@ -1081,12 +1069,12 @@ fn exec_blit<'a>(encoder: &metal::BlitCommandEncoderRef, command: soft::BlitComm
10811069
let layers = region.src_subresource.layers.zip(region.dst_subresource.layers);
10821070
for (src_layer, dst_layer) in layers {
10831071
encoder.copy_from_texture(
1084-
&*src.resolve(),
1072+
src,
10851073
src_layer as _,
10861074
region.src_subresource.level as _,
10871075
src_offset,
10881076
size,
1089-
&*dst.resolve(),
1077+
dst,
10901078
dst_layer as _,
10911079
region.dst_subresource.level as _,
10921080
dst_offset,
@@ -1107,7 +1095,7 @@ fn exec_blit<'a>(encoder: &metal::BlitCommandEncoderRef, command: soft::BlitComm
11071095
row_pitch as NSUInteger,
11081096
slice_pitch as NSUInteger,
11091097
extent,
1110-
&*dst.resolve(),
1098+
dst,
11111099
layer as NSUInteger,
11121100
r.level as NSUInteger,
11131101
origin,
@@ -1124,7 +1112,7 @@ fn exec_blit<'a>(encoder: &metal::BlitCommandEncoderRef, command: soft::BlitComm
11241112
for layer in r.layers.clone() {
11251113
let offset = region.buffer_offset + slice_pitch as NSUInteger * (layer - r.layers.start) as NSUInteger;
11261114
encoder.copy_from_texture_to_buffer(
1127-
&*src.resolve(),
1115+
src,
11281116
layer as NSUInteger,
11291117
r.level as NSUInteger,
11301118
origin,
@@ -1150,14 +1138,6 @@ fn exec_compute<'a>(encoder: &metal::ComputeCommandEncoderRef, command: soft::Co
11501138
encoder.set_bytes(index as _, (words.len() * WORD_SIZE) as u64, words.as_ptr() as _);
11511139
}
11521140
Cmd::BindTexture { index, texture } => {
1153-
let guard;
1154-
let texture = match texture {
1155-
Some(ref root) => {
1156-
guard = root.resolve();
1157-
Some(&*guard)
1158-
}
1159-
None => None,
1160-
};
11611141
encoder.set_texture(index as _, texture);
11621142
}
11631143
Cmd::BindSampler { index, sampler } => {
@@ -1175,28 +1155,11 @@ fn exec_compute<'a>(encoder: &metal::ComputeCommandEncoderRef, command: soft::Co
11751155
}
11761156
}
11771157

1178-
fn resolve_frames<I>(desc: &metal::RenderPassDescriptorRef, frames: I)
1179-
where
1180-
I: IntoIterator,
1181-
I::Item: Borrow<(usize, native::Frame)>,
1182-
{
1183-
for f in frames {
1184-
let (index, ref frame) = *f.borrow();
1185-
let swapchain = frame.swapchain.read().unwrap();
1186-
desc
1187-
.color_attachments()
1188-
.object_at(index as _)
1189-
.unwrap()
1190-
.set_texture(Some(&swapchain[frame.index]))
1191-
}
1192-
}
1193-
11941158
fn record_commands(command_buf: &metal::CommandBufferRef, passes: &[soft::Pass]) {
11951159
let _ap = AutoreleasePool::new(); // for encoder creation
11961160
for pass in passes {
11971161
match *pass {
1198-
soft::Pass::Render { ref desc, ref frames, ref commands } => {
1199-
resolve_frames(desc, frames);
1162+
soft::Pass::Render { ref desc, ref commands } => {
12001163
let encoder = command_buf.new_render_command_encoder(desc);
12011164
for command in commands {
12021165
exec_render(&encoder, command.as_ref());
@@ -1618,38 +1581,30 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
16181581
for subresource_range in subresource_ranges {
16191582
let sub = subresource_range.borrow();
16201583

1621-
let mut frame = None;
16221584
let num_layers = (sub.layers.end - sub.layers.start) as u64;
16231585
let layers = if CLEAR_IMAGE_ARRAY {
16241586
0 .. 1
16251587
} else {
16261588
sub.layers.clone()
16271589
};
16281590
let texture = if CLEAR_IMAGE_ARRAY && sub.layers.start > 0 {
1629-
let image_raw = image.root.as_ref().resolve();
16301591
// aliasing is necessary for bulk-clearing all layers starting with 0
1631-
let tex = image_raw.new_texture_view_from_slice(
1592+
let tex = image.raw.new_texture_view_from_slice(
16321593
image.mtl_format,
16331594
image.mtl_type,
16341595
NSRange {
16351596
location: 0,
1636-
length: image_raw.mipmap_level_count(),
1597+
length: image.raw.mipmap_level_count(),
16371598
},
16381599
NSRange {
16391600
location: sub.layers.start as _,
16401601
length: num_layers,
16411602
},
16421603
);
16431604
retained_textures.push(tex);
1644-
retained_textures.last().map(|tex| tex.as_ref())
1605+
retained_textures.last().unwrap()
16451606
} else {
1646-
match image.root {
1647-
native::ImageRoot::Texture(ref tex) => Some(tex.as_ref()),
1648-
native::ImageRoot::Frame(ref f) => {
1649-
frame = Some((0usize, f.clone()));
1650-
None
1651-
}
1652-
}
1607+
&*image.raw
16531608
};
16541609

16551610
for layer in layers {
@@ -1669,7 +1624,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
16691624
.color_attachments()
16701625
.object_at(0)
16711626
.unwrap();
1672-
attachment.set_texture(texture);
1627+
attachment.set_texture(Some(texture));
16731628
attachment.set_level(level as _);
16741629
attachment.set_store_action(metal::MTLStoreAction::Store);
16751630
if !CLEAR_IMAGE_ARRAY {
@@ -1688,7 +1643,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
16881643
let attachment = descriptor
16891644
.depth_attachment()
16901645
.unwrap();
1691-
attachment.set_texture(texture);
1646+
attachment.set_texture(Some(texture));
16921647
attachment.set_level(level as _);
16931648
attachment.set_store_action(metal::MTLStoreAction::Store);
16941649
if !CLEAR_IMAGE_ARRAY {
@@ -1707,7 +1662,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
17071662
let attachment = descriptor
17081663
.stencil_attachment()
17091664
.unwrap();
1710-
attachment.set_texture(texture);
1665+
attachment.set_texture(Some(texture));
17111666
attachment.set_level(level as _);
17121667
attachment.set_store_action(metal::MTLStoreAction::Store);
17131668
if !CLEAR_IMAGE_ARRAY {
@@ -1723,7 +1678,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
17231678

17241679
sink.as_mut()
17251680
.unwrap()
1726-
.begin_render_pass(false, descriptor, frame.clone().into_iter(), None.into_iter());
1681+
.begin_render_pass(false, descriptor, None.into_iter());
17271682
// no actual pass body - everything is in the attachment clear operations
17281683
}
17291684
}
@@ -1964,15 +1919,6 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
19641919
let vertices = &mut self.temp.blit_vertices;
19651920
vertices.clear();
19661921

1967-
let mut frame = None;
1968-
let dst_texture = match dst.root {
1969-
native::ImageRoot::Texture(ref tex) => Some(tex.as_ref()),
1970-
native::ImageRoot::Frame(ref f) => {
1971-
frame = Some((0usize, f.clone()));
1972-
None
1973-
}
1974-
};
1975-
19761922
for region in regions {
19771923
let r = region.borrow();
19781924

@@ -2077,7 +2023,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
20772023
soft::RenderCommand::BindTexture {
20782024
stage: pso::Stage::Fragment,
20792025
index: 0,
2080-
texture: Some(src.root.as_ref())
2026+
texture: Some(&*src.raw)
20812027
},
20822028
];
20832029

@@ -2132,21 +2078,21 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
21322078
.color_attachments()
21332079
.object_at(0)
21342080
.unwrap();
2135-
attachment.set_texture(dst_texture);
2081+
attachment.set_texture(Some(&dst.raw));
21362082
attachment.set_level(level as _);
21372083
}
21382084
if aspects.contains(Aspects::DEPTH) {
21392085
let attachment = descriptor
21402086
.depth_attachment()
21412087
.unwrap();
2142-
attachment.set_texture(dst_texture);
2088+
attachment.set_texture(Some(&dst.raw));
21432089
attachment.set_level(level as _);
21442090
}
21452091
if aspects.contains(Aspects::STENCIL) {
21462092
let attachment = descriptor
21472093
.stencil_attachment()
21482094
.unwrap();
2149-
attachment.set_texture(dst_texture);
2095+
attachment.set_texture(Some(&dst.raw));
21502096
attachment.set_level(level as _);
21512097
}
21522098

@@ -2156,7 +2102,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
21562102
.chain(&extra)
21572103
.cloned();
21582104

2159-
inner.sink().begin_render_pass(false, descriptor, frame.clone().into_iter(), commands);
2105+
inner.sink().begin_render_pass(false, descriptor, commands);
21602106
}
21612107
}
21622108

@@ -2380,14 +2326,10 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
23802326
};
23812327

23822328
self.state.framebuffer_inner = framebuffer.inner.clone();
2383-
let frames = framebuffer.inner.colors
2384-
.iter()
2385-
.enumerate()
2386-
.filter_map(|(index, ref cat)| cat.frame.clone().map(|f| (index, f)));
23872329
let init_commands = self.state.make_render_commands(full_aspects);
23882330
inner
23892331
.sink()
2390-
.begin_render_pass(true, &descriptor, frames, init_commands);
2332+
.begin_render_pass(true, &descriptor, init_commands);
23912333
}
23922334

23932335
fn next_subpass(&mut self, _contents: com::SubpassContents) {
@@ -2918,18 +2860,18 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
29182860
} = *self.inner.borrow_mut();
29192861

29202862
let new_src = if src.mtl_format == dst.mtl_format {
2921-
src.root.clone()
2863+
&*src.raw
29222864
} else {
29232865
assert_eq!(src.format_desc.bits, dst.format_desc.bits);
2924-
let tex = src.root.as_ref().resolve().new_texture_view(dst.mtl_format);
2925-
retained_textures.push(tex.clone());
2926-
native::ImageRoot::Texture(tex)
2866+
let tex = src.raw.new_texture_view(dst.mtl_format);
2867+
retained_textures.push(tex);
2868+
retained_textures.last().unwrap()
29272869
};
29282870

29292871
let commands = regions.into_iter().map(|region| {
29302872
soft::BlitCommand::CopyImage {
2931-
src: new_src.as_ref(),
2932-
dst: dst.root.as_ref(),
2873+
src: new_src,
2874+
dst: &*dst.raw,
29332875
region: region.borrow().clone(),
29342876
}
29352877
});
@@ -2952,7 +2894,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
29522894
let commands = regions.into_iter().map(|region| {
29532895
soft::BlitCommand::CopyBufferToImage {
29542896
src: &*src.raw,
2955-
dst: dst.root.as_ref(),
2897+
dst: &*dst.raw,
29562898
dst_desc: dst.format_desc,
29572899
region: region.borrow().clone(),
29582900
}
@@ -2976,9 +2918,9 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
29762918
// FIXME: layout
29772919
let commands = regions.into_iter().map(|region| {
29782920
soft::BlitCommand::CopyImageToBuffer {
2979-
src: src.root.as_ref(),
2921+
src: &*src.raw,
29802922
src_desc: src.format_desc,
2981-
dst: dst.raw.as_ref(),
2923+
dst: &*dst.raw,
29822924
region: region.borrow().clone(),
29832925
}
29842926
});

0 commit comments

Comments
 (0)