Skip to content

Commit 8bb7921

Browse files
committed
[mtl] remove the deferred image resolution (yay)
1 parent 98999ca commit 8bb7921

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
@@ -547,7 +547,7 @@ impl State {
547547
#[derive(Clone, Debug)]
548548
struct StageResources {
549549
buffers: Vec<Option<(metal::Buffer, buffer::Offset)>>,
550-
textures: Vec<Option<native::ImageRoot>>,
550+
textures: Vec<Option<metal::Texture>>,
551551
samplers: Vec<Option<metal::SamplerState>>,
552552
push_constants_buffer_id: Option<u32>,
553553
}
@@ -576,12 +576,12 @@ impl StageResources {
576576
self.buffers[slot] = Some((buffer.to_owned(), offset));
577577
}
578578

579-
fn add_textures(&mut self, start: usize, roots: &[Option<(native::ImageRoot, Layout)>]) {
580-
while self.textures.len() < start + roots.len() {
579+
fn add_textures(&mut self, start: usize, textures: &[Option<(metal::Texture, Layout)>]) {
580+
while self.textures.len() < start + textures.len() {
581581
self.textures.push(None)
582582
}
583-
for (out, root) in self.textures[start..].iter_mut().zip(roots.iter()) {
584-
*out = root.as_ref().map(|&(ref root, _)| root.clone());
583+
for (out, tex) in self.textures[start..].iter_mut().zip(textures.iter()) {
584+
*out = tex.as_ref().map(|&(ref t, _)| t.to_owned());
585585
}
586586
}
587587

@@ -762,22 +762,19 @@ impl CommandSink {
762762
}
763763
}
764764

765-
fn begin_render_pass<'a, F, I>(
765+
fn begin_render_pass<'a, I>(
766766
&mut self,
767767
keep_open: bool,
768768
descriptor: &'a metal::RenderPassDescriptorRef,
769-
frames: F,
770769
init_commands: I,
771770
) where
772-
F: Iterator<Item = (usize, native::Frame)>,
773771
I: Iterator<Item = soft::RenderCommand<&'a soft::Own>>,
774772
{
775773
self.stop_encoding();
776774

777775
match *self {
778776
CommandSink::Immediate { ref cmd_buffer, ref mut encoder_state, .. } => {
779777
let _ap = AutoreleasePool::new();
780-
resolve_frames(descriptor, frames);
781778
let encoder = cmd_buffer.new_render_command_encoder(descriptor);
782779
for command in init_commands {
783780
exec_render(encoder, command);
@@ -792,7 +789,6 @@ impl CommandSink {
792789
*is_encoding = keep_open;
793790
passes.push(soft::Pass::Render {
794791
desc: descriptor.to_owned(),
795-
frames: frames.collect(),
796792
commands: init_commands.map(soft::RenderCommand::own).collect(),
797793
});
798794
}
@@ -974,14 +970,6 @@ fn exec_render<'a>(encoder: &metal::RenderCommandEncoderRef, command: soft::Rend
974970
}
975971
}
976972
Cmd::BindTexture { stage, index, texture } => {
977-
let guard;
978-
let texture = match texture {
979-
Some(root) => {
980-
guard = root.resolve();
981-
Some(&*guard)
982-
}
983-
None => None,
984-
};
985973
match stage {
986974
pso::Stage::Vertex =>
987975
encoder.set_vertex_texture(index as _, texture),
@@ -1095,12 +1083,12 @@ fn exec_blit<'a>(encoder: &metal::BlitCommandEncoderRef, command: soft::BlitComm
10951083
let layers = region.src_subresource.layers.zip(region.dst_subresource.layers);
10961084
for (src_layer, dst_layer) in layers {
10971085
encoder.copy_from_texture(
1098-
&*src.resolve(),
1086+
src,
10991087
src_layer as _,
11001088
region.src_subresource.level as _,
11011089
src_offset,
11021090
size,
1103-
&*dst.resolve(),
1091+
dst,
11041092
dst_layer as _,
11051093
region.dst_subresource.level as _,
11061094
dst_offset,
@@ -1121,7 +1109,7 @@ fn exec_blit<'a>(encoder: &metal::BlitCommandEncoderRef, command: soft::BlitComm
11211109
row_pitch as NSUInteger,
11221110
slice_pitch as NSUInteger,
11231111
extent,
1124-
&*dst.resolve(),
1112+
dst,
11251113
layer as NSUInteger,
11261114
r.level as NSUInteger,
11271115
origin,
@@ -1138,7 +1126,7 @@ fn exec_blit<'a>(encoder: &metal::BlitCommandEncoderRef, command: soft::BlitComm
11381126
for layer in r.layers.clone() {
11391127
let offset = region.buffer_offset + slice_pitch as NSUInteger * (layer - r.layers.start) as NSUInteger;
11401128
encoder.copy_from_texture_to_buffer(
1141-
&*src.resolve(),
1129+
src,
11421130
layer as NSUInteger,
11431131
r.level as NSUInteger,
11441132
origin,
@@ -1164,14 +1152,6 @@ fn exec_compute<'a>(encoder: &metal::ComputeCommandEncoderRef, command: soft::Co
11641152
encoder.set_bytes(index as _, words.len() as u64 * 4, words.as_ptr() as _);
11651153
}
11661154
Cmd::BindTexture { index, texture } => {
1167-
let guard;
1168-
let texture = match texture {
1169-
Some(ref root) => {
1170-
guard = root.resolve();
1171-
Some(&*guard)
1172-
}
1173-
None => None,
1174-
};
11751155
encoder.set_texture(index as _, texture);
11761156
}
11771157
Cmd::BindSampler { index, sampler } => {
@@ -1189,28 +1169,11 @@ fn exec_compute<'a>(encoder: &metal::ComputeCommandEncoderRef, command: soft::Co
11891169
}
11901170
}
11911171

1192-
fn resolve_frames<I>(desc: &metal::RenderPassDescriptorRef, frames: I)
1193-
where
1194-
I: IntoIterator,
1195-
I::Item: Borrow<(usize, native::Frame)>,
1196-
{
1197-
for f in frames {
1198-
let (index, ref frame) = *f.borrow();
1199-
let swapchain = frame.swapchain.read().unwrap();
1200-
desc
1201-
.color_attachments()
1202-
.object_at(index as _)
1203-
.unwrap()
1204-
.set_texture(Some(&swapchain[frame.index]))
1205-
}
1206-
}
1207-
12081172
fn record_commands(command_buf: &metal::CommandBufferRef, passes: &[soft::Pass]) {
12091173
let _ap = AutoreleasePool::new(); // for encoder creation
12101174
for pass in passes {
12111175
match *pass {
1212-
soft::Pass::Render { ref desc, ref frames, ref commands } => {
1213-
resolve_frames(desc, frames);
1176+
soft::Pass::Render { ref desc, ref commands } => {
12141177
let encoder = command_buf.new_render_command_encoder(desc);
12151178
for command in commands {
12161179
exec_render(&encoder, command.as_ref());
@@ -1632,38 +1595,30 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
16321595
for subresource_range in subresource_ranges {
16331596
let sub = subresource_range.borrow();
16341597

1635-
let mut frame = None;
16361598
let num_layers = (sub.layers.end - sub.layers.start) as u64;
16371599
let layers = if CLEAR_IMAGE_ARRAY {
16381600
0 .. 1
16391601
} else {
16401602
sub.layers.clone()
16411603
};
16421604
let texture = if CLEAR_IMAGE_ARRAY && sub.layers.start > 0 {
1643-
let image_raw = image.root.as_ref().resolve();
16441605
// aliasing is necessary for bulk-clearing all layers starting with 0
1645-
let tex = image_raw.new_texture_view_from_slice(
1606+
let tex = image.raw.new_texture_view_from_slice(
16461607
image.mtl_format,
16471608
image.mtl_type,
16481609
NSRange {
16491610
location: 0,
1650-
length: image_raw.mipmap_level_count(),
1611+
length: image.raw.mipmap_level_count(),
16511612
},
16521613
NSRange {
16531614
location: sub.layers.start as _,
16541615
length: num_layers,
16551616
},
16561617
);
16571618
retained_textures.push(tex);
1658-
retained_textures.last().map(|tex| tex.as_ref())
1619+
retained_textures.last().unwrap()
16591620
} else {
1660-
match image.root {
1661-
native::ImageRoot::Texture(ref tex) => Some(tex.as_ref()),
1662-
native::ImageRoot::Frame(ref f) => {
1663-
frame = Some((0usize, f.clone()));
1664-
None
1665-
}
1666-
}
1621+
&*image.raw
16671622
};
16681623

16691624
for layer in layers {
@@ -1683,7 +1638,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
16831638
.color_attachments()
16841639
.object_at(0)
16851640
.unwrap();
1686-
attachment.set_texture(texture);
1641+
attachment.set_texture(Some(texture));
16871642
attachment.set_level(level as _);
16881643
attachment.set_store_action(metal::MTLStoreAction::Store);
16891644
if !CLEAR_IMAGE_ARRAY {
@@ -1702,7 +1657,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
17021657
let attachment = descriptor
17031658
.depth_attachment()
17041659
.unwrap();
1705-
attachment.set_texture(texture);
1660+
attachment.set_texture(Some(texture));
17061661
attachment.set_level(level as _);
17071662
attachment.set_store_action(metal::MTLStoreAction::Store);
17081663
if !CLEAR_IMAGE_ARRAY {
@@ -1721,7 +1676,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
17211676
let attachment = descriptor
17221677
.stencil_attachment()
17231678
.unwrap();
1724-
attachment.set_texture(texture);
1679+
attachment.set_texture(Some(texture));
17251680
attachment.set_level(level as _);
17261681
attachment.set_store_action(metal::MTLStoreAction::Store);
17271682
if !CLEAR_IMAGE_ARRAY {
@@ -1737,7 +1692,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
17371692

17381693
sink.as_mut()
17391694
.unwrap()
1740-
.begin_render_pass(false, descriptor, frame.clone().into_iter(), None.into_iter());
1695+
.begin_render_pass(false, descriptor, None.into_iter());
17411696
// no actual pass body - everything is in the attachment clear operations
17421697
}
17431698
}
@@ -1977,15 +1932,6 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
19771932
let vertices = &mut self.temp.blit_vertices;
19781933
vertices.clear();
19791934

1980-
let mut frame = None;
1981-
let dst_texture = match dst.root {
1982-
native::ImageRoot::Texture(ref tex) => Some(tex.as_ref()),
1983-
native::ImageRoot::Frame(ref f) => {
1984-
frame = Some((0usize, f.clone()));
1985-
None
1986-
}
1987-
};
1988-
19891935
for region in regions {
19901936
let r = region.borrow();
19911937

@@ -2090,7 +2036,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
20902036
soft::RenderCommand::BindTexture {
20912037
stage: pso::Stage::Fragment,
20922038
index: 0,
2093-
texture: Some(src.root.as_ref())
2039+
texture: Some(&*src.raw)
20942040
},
20952041
];
20962042

@@ -2145,21 +2091,21 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
21452091
.color_attachments()
21462092
.object_at(0)
21472093
.unwrap();
2148-
attachment.set_texture(dst_texture);
2094+
attachment.set_texture(Some(&dst.raw));
21492095
attachment.set_level(level as _);
21502096
}
21512097
if aspects.contains(Aspects::DEPTH) {
21522098
let attachment = descriptor
21532099
.depth_attachment()
21542100
.unwrap();
2155-
attachment.set_texture(dst_texture);
2101+
attachment.set_texture(Some(&dst.raw));
21562102
attachment.set_level(level as _);
21572103
}
21582104
if aspects.contains(Aspects::STENCIL) {
21592105
let attachment = descriptor
21602106
.stencil_attachment()
21612107
.unwrap();
2162-
attachment.set_texture(dst_texture);
2108+
attachment.set_texture(Some(&dst.raw));
21632109
attachment.set_level(level as _);
21642110
}
21652111

@@ -2169,7 +2115,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
21692115
.chain(&extra)
21702116
.cloned();
21712117

2172-
inner.sink().begin_render_pass(false, descriptor, frame.clone().into_iter(), commands);
2118+
inner.sink().begin_render_pass(false, descriptor, commands);
21732119
}
21742120
}
21752121

@@ -2393,14 +2339,10 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
23932339
};
23942340

23952341
self.state.framebuffer_inner = framebuffer.inner.clone();
2396-
let frames = framebuffer.inner.colors
2397-
.iter()
2398-
.enumerate()
2399-
.filter_map(|(index, ref cat)| cat.frame.clone().map(|f| (index, f)));
24002342
let init_commands = self.state.make_render_commands(full_aspects);
24012343
inner
24022344
.sink()
2403-
.begin_render_pass(true, &descriptor, frames, init_commands);
2345+
.begin_render_pass(true, &descriptor, init_commands);
24042346
}
24052347

24062348
fn next_subpass(&mut self, _contents: com::SubpassContents) {
@@ -2933,18 +2875,18 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
29332875
} = *self.inner.borrow_mut();
29342876

29352877
let new_src = if src.mtl_format == dst.mtl_format {
2936-
src.root.clone()
2878+
&*src.raw
29372879
} else {
29382880
assert_eq!(src.format_desc.bits, dst.format_desc.bits);
2939-
let tex = src.root.as_ref().resolve().new_texture_view(dst.mtl_format);
2940-
retained_textures.push(tex.clone());
2941-
native::ImageRoot::Texture(tex)
2881+
let tex = src.raw.new_texture_view(dst.mtl_format);
2882+
retained_textures.push(tex);
2883+
retained_textures.last().unwrap()
29422884
};
29432885

29442886
let commands = regions.into_iter().map(|region| {
29452887
soft::BlitCommand::CopyImage {
2946-
src: new_src.as_ref(),
2947-
dst: dst.root.as_ref(),
2888+
src: new_src,
2889+
dst: &*dst.raw,
29482890
region: region.borrow().clone(),
29492891
}
29502892
});
@@ -2967,7 +2909,7 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
29672909
let commands = regions.into_iter().map(|region| {
29682910
soft::BlitCommand::CopyBufferToImage {
29692911
src: &*src.raw,
2970-
dst: dst.root.as_ref(),
2912+
dst: &*dst.raw,
29712913
dst_desc: dst.format_desc,
29722914
region: region.borrow().clone(),
29732915
}
@@ -2991,9 +2933,9 @@ impl com::RawCommandBuffer<Backend> for CommandBuffer {
29912933
// FIXME: layout
29922934
let commands = regions.into_iter().map(|region| {
29932935
soft::BlitCommand::CopyImageToBuffer {
2994-
src: src.root.as_ref(),
2936+
src: &*src.raw,
29952937
src_desc: src.format_desc,
2996-
dst: dst.raw.as_ref(),
2938+
dst: &*dst.raw,
29972939
region: region.borrow().clone(),
29982940
}
29992941
});

0 commit comments

Comments
 (0)