Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions crates/metal2vulkan-runtime/src/tests/attachment_views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,148 @@ mod on_device {
drop((queue, other, device));
});
}

/// A 1-mip, 1-layer 2D color render target with identity swizzle and a sampled view reuses
/// its sampled `VkImageView` directly for render-pass attachments without calling
/// `vkCreateImageView` a second time or bumping `Gauge::AttachmentViews`.
#[test]
fn every_device_reuses_sampled_view_for_single_slice_2d_color_attachment() {
each_device("attachment_views_reuse_sampled", |name, _, device| {
device
.grant_indices(IndexClass::Texture, 4096, 1)
.expect("a grant");
let queue = device.new_command_queue(4).expect("a queue");
let counters = device.counters();
let mut d = TextureDescriptor::new(TextureType::Type2D, PixelFormat::BGRA8Unorm, 8, 8);
d.usage = TextureUsage::SHADER_READ | TextureUsage::RENDER_TARGET;
d.storage_mode = StorageMode::Private;
let tex = device.new_texture(&d, Some(4096)).expect("a 2D target");

let before_creates = counters.metrics().image_view_creates.count;
let before_gauge = counters.gauges().attachment_views;
run(name, &queue, &tex, 0, 1);
assert_eq!(
counters.metrics().image_view_creates.count,
before_creates,
"{name}: render pass must reuse sampled view without vkCreateImageView"
);
assert_eq!(
counters.gauges().attachment_views,
before_gauge,
"{name}: AttachmentViews gauge stays unchanged on sampled-view reuse"
);
assert!(
views(&tex).is_empty(),
"{name}: no AttachmentView pushed into attachments list"
);
drop(tex);
within("reused sampled view drops cleanly", || {
counters.gauges().attachment_views == before_gauge
});
drop((queue, device));
});
}

/// Combined depth-stencil, swizzled, and multi-mip textures must NOT reuse their sampled view
/// for render-pass attachments (Vulkan requires full attachment aspects, identity swizzle,
/// and a single mip level).
#[test]
fn every_device_allocates_dedicated_attachment_view_when_sampled_view_is_ineligible() {
each_device("attachment_views_ineligible_sampled", |name, _, device| {
use crate::{TextureSwizzle, TextureSwizzleChannels};
device
.grant_indices(IndexClass::Texture, 4096, 4)
.expect("a grant");
let queue = device.new_command_queue(4).expect("a queue");
let counters = device.counters();

// (1) Combined Depth32FloatStencil8: sampled view has DEPTH aspect only, attachment needs DEPTH|STENCIL.
let mut ds_desc = TextureDescriptor::new(
TextureType::Type2D,
PixelFormat::Depth32FloatStencil8,
8,
8,
);
ds_desc.usage = TextureUsage::SHADER_READ | TextureUsage::RENDER_TARGET;
ds_desc.storage_mode = StorageMode::Private;
let ds = device.new_texture(&ds_desc, Some(4096)).expect("ds target");

// (2) Non-identity swizzle on a 1-mip 2D color target (created via view, since Metal
// permits swizzled views over RENDER_TARGET textures while rejecting swizzle on the
// texture's own descriptor).
let mut base_desc =
TextureDescriptor::new(TextureType::Type2D, PixelFormat::RGBA8Unorm, 8, 8);
base_desc.usage = TextureUsage::SHADER_READ | TextureUsage::RENDER_TARGET;
base_desc.storage_mode = StorageMode::Private;
let base = device
.new_texture(&base_desc, Some(4097))
.expect("base target");
let swz = base
.new_texture_view_with_swizzle(
PixelFormat::RGBA8Unorm,
TextureType::Type2D,
0..1,
0..1,
TextureSwizzleChannels {
red: TextureSwizzle::Blue,
green: TextureSwizzle::Green,
blue: TextureSwizzle::Red,
alpha: TextureSwizzle::Alpha,
},
Some(4098),
)
.expect("swizzled view");

// (3) Multi-mip 2D color target.
let mut mip_desc =
TextureDescriptor::new(TextureType::Type2D, PixelFormat::RGBA8Unorm, 8, 8);
mip_desc.mipmap_level_count = 2;
mip_desc.usage = TextureUsage::SHADER_READ | TextureUsage::RENDER_TARGET;
mip_desc.storage_mode = StorageMode::Private;
let mip = device
.new_texture(&mip_desc, Some(4099))
.expect("mipped target");

let before_creates = counters.metrics().image_view_creates.count;
let before_gauge = counters.gauges().attachment_views;

// Clear depth-stencil + swizzled color together, then clear level 0 of multi-mip.
let cb = queue.command_buffer().expect("cb");
let mut rp = RenderPassDescriptor::default();
rp.color_attachments[0].texture = Some(swz.clone());
rp.color_attachments[0].load_action = LoadAction::Clear;
rp.color_attachments[0].store_action = StoreAction::Store;
rp.depth_attachment.texture = Some(ds.clone());
rp.depth_attachment.load_action = LoadAction::Clear;
rp.depth_attachment.store_action = StoreAction::Store;
rp.stencil_attachment.texture = Some(ds.clone());
rp.stencil_attachment.load_action = LoadAction::Clear;
rp.stencil_attachment.store_action = StoreAction::Store;
cb.render_command_encoder(&rp)
.expect("swz+ds pass")
.end_encoding();
cb.commit().expect("commit");
finished(name, &cb);
assert_eq!(cb.error(), None, "{name}: swz+ds pass");

run(name, &queue, &mip, 0, 1);

assert_eq!(
counters.metrics().image_view_creates.count,
before_creates + 3,
"{name}: ds, swizzled, and multi-mip must each allocate a dedicated attachment view"
);
assert_eq!(
counters.gauges().attachment_views,
before_gauge + 3,
"{name}: AttachmentViews gauge must reflect all 3 dedicated views"
);

drop((ds, swz, base, mip, rp, cb));
within("all 3 dedicated views freed", || {
counters.gauges().attachment_views == before_gauge
});
drop((queue, device));
});
}
}
8 changes: 5 additions & 3 deletions crates/metal2vulkan-runtime/src/tests/pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,12 @@ mod on_device {
(1, 3, 0, 1),
"{name}: submissions, barriers, bytes copied, passes"
);
// `colors` (2 mips) and `stencil` (no sampled view) allocate attachment views;
// 1-mip 1-layer 2D `words` reuses its compatible sampled view directly.
assert_eq!(
counters.gauges().attachment_views - views,
3,
"{name}: a view of each attachment"
2,
"{name}: a view of each attachment without a reusable sampled view"
);
assert_eq!(read(&colors, 0, 0, 4), level0, "{name}: level 0 kept");
assert_eq!(
Expand Down Expand Up @@ -488,7 +490,7 @@ mod on_device {
);
assert_eq!(
counters.gauges().attachment_views - views,
3,
2,
"{name}: views kept"
);
assert_eq!(
Expand Down
Loading
Loading