🐛 Isolate Places allocations from buffer address replay

Keep texture blocks in non-replay Vulkan allocations while preserving exact-address replay for captured buffers.
This commit is contained in:
YXC0915 committed 2026-12-03 01:02:03 +08:00
1 parent 116ef68ae8
commit 28457772f4
3 files changed
+161

No files matched your search

@@ -22,6 +22,7 @@ Alpha 编号与挂起兼容判定见 [Alpha 运行时编号与挂起兼容性规
| [`metal2vulkan-psb-address-carry.patch`](metal2vulkan-psb-address-carry.patch) | `steelbrain/metal2vulkan` | 恢复 64 位 GPU 地址时显式处理低位进位。 [组织 fork 提交](https://github.com/reims-go/metal2vulkan/commit/729d91d23bb56a99f2b5b74d3d3445c65cc1cdf1) |
| [`metal2vulkan-dynamic-attachment-feedback.patch`](metal2vulkan-dynamic-attachment-feedback.patch) | `steelbrain/metal2vulkan` | 原生附件反馈要求设备同时报告反馈布局和动态反馈能力,其余设备通过影子纹理读取附件。 [组织 fork 提交](https://github.com/reims-go/metal2vulkan/commit/ea695f945626f15c27ac9b463ba4160c65530b43) |
| [`metal2vulkan-static-attachment-feedback.patch`](metal2vulkan-static-attachment-feedback.patch) | `steelbrain/metal2vulkan` | 保留上述能力要求,以静态颜色附件反馈管线替代逐 draw 切换动态状态,修复桌面暗屏。 |
| [`metal2vulkan-places-texture-pool.patch`](metal2vulkan-places-texture-pool.patch) | `steelbrain/metal2vulkan` | 将 `Places` 纹理分配与可重放缓冲区分开。挂起恢复仍可按捕获地址重建缓冲区。 |
构建通过 Cargo 的本地源码覆盖使用已打补丁的 `metal2vulkan`。客户端的运行时包格式见[便携运行时规范](https://github.com/Nwflower/Reims-Go/blob/main/docs/portable-runtime-spec.md)。
@@ -0,0 +1,159 @@
diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs
index 2864f4ba6..f574e3b30 100644
--- a/crates/metal2vulkan-runtime/src/memory.rs
+++ b/crates/metal2vulkan-runtime/src/memory.rs
@@ -648,6 +648,9 @@ struct Allocation {
/// Its block out is a texture block ([`crate::places`]), which fills its allocation: a whole
/// unit, or a long allocation of its own length. False once it is idle.
texture: bool,
+ /// Reserved for image placement when buffer address replay is enabled. This allocation
+ /// stays in the texture pool after its last block comes back.
+ texture_only: bool,
/// When it became idle, in nanoseconds of [`host_time`](crate::host_time): when it was made,
/// or when its last live block came back. The oldest idle allocation is freed first.
idle_since: u64,
@@ -1200,7 +1203,9 @@ impl Pool {
return Err(Error::Quiesced);
}
let texture = matches!(site, Site::TextureBlock);
- if let Some((carve, idle_since, long, ahead)) = state.fit(class, len, align, texture) {
+ if let Some((carve, idle_since, long, ahead)) =
+ state.fit(class, len, align, texture, core.capture_replay)
+ {
if ahead {
self.counters.site_take_ahead(site, len);
}
@@ -1404,7 +1409,7 @@ impl Pool {
let state = self.lock();
let alloc = state.allocations[memory.class.index()]
.iter()
- .find(|a| a.memory == memory.memory)?;
+ .find(|a| a.memory == memory.memory && !a.texture_only)?;
Some(BlockCaptureInfo {
class: memory.class,
@@ -1435,7 +1440,8 @@ impl Pool {
let id = number(&mut state)?;
let want_base = cap.gpu_address.saturating_sub(cap.offset);
if let Some(alloc) = state.allocations[index].iter_mut().find(|a| {
- a.address == want_base
+ !a.texture_only
+ && a.address == want_base
&& a.opaque_buffer_address == cap.opaque_buffer_address
&& a.opaque_memory_address == cap.opaque_memory_address
&& a.len == cap.alloc_len
@@ -1463,7 +1469,8 @@ impl Pool {
// Re-check if an ahead allocation that just finished landed at the exact target VA!
let want_base = cap.gpu_address.saturating_sub(cap.offset);
let hit = state.allocations[index].iter_mut().find_map(|a| {
- if a.address == want_base
+ if !a.texture_only
+ && a.address == want_base
&& a.opaque_buffer_address == cap.opaque_buffer_address
&& a.opaque_memory_address == cap.opaque_memory_address
&& a.len == cap.alloc_len
@@ -1705,8 +1712,12 @@ impl State {
/// free bytes would wait until they ran low; in return a take of up to a unit, the 16 MiB
/// ones among them, lands in memory made ahead once the block-maker thread has made it.
fn whole(&self, class: MemoryClass) -> bool {
- (self.allocations[class.index()].iter())
- .any(|allocation| allocation.unit() && !allocation.retiring && allocation.live == 0)
+ (self.allocations[class.index()].iter()).any(|allocation| {
+ allocation.unit()
+ && !allocation.texture_only
+ && !allocation.retiring
+ && allocation.live == 0
+ })
}
/// The allocation of `class` whose memory is `memory`.
@@ -1726,9 +1737,11 @@ impl State {
len: u64,
align: u64,
texture: bool,
+ split_textures: bool,
) -> Option<(Carve, Option<u64>, bool, bool)> {
+ let texture_only = split_textures && texture;
let (allocation, base) = (self.allocations[class.index()].iter_mut())
- .filter(|allocation| allocation.serves(len))
+ .filter(|allocation| allocation.texture_only == texture_only && allocation.serves(len))
.find_map(|allocation| {
let base = allocation.free.take(len, align)?;
Some((allocation, base))
@@ -1757,7 +1770,9 @@ impl State {
fn aging(&self, class: MemoryClass) -> (Vec<(u64, vk::DeviceMemory)>, u64) {
let idle = (self.allocations[class.index()].iter())
.filter(|allocation| allocation.live == 0 && !allocation.retiring);
- let reserve = idle.clone().rfind(|allocation| allocation.unit());
+ let reserve = idle
+ .clone()
+ .rfind(|allocation| allocation.unit() && !allocation.texture_only);
let mut weighed: Vec<(u64, vk::DeviceMemory)> = idle
.filter(|allocation| reserve.is_none_or(|reserve| reserve.memory != allocation.memory))
.map(|allocation| (allocation.idle_since, allocation.memory))
@@ -2218,6 +2233,8 @@ fn make_allocation_ex(
replay: Option<(u64, u64)>,
) -> Result<Allocation, Error> {
let device = &core.device;
+ let texture_only = core.capture_replay && matches!(site, Some(Site::TextureBlock));
+ let capture_replay = core.capture_replay && !texture_only;
let (id, freed_at) = {
let mut state = core.pool.lock();
(
@@ -2231,7 +2248,7 @@ fn make_allocation_ex(
.size(len)
.usage(floor::BUFFER_USAGE)
.sharing_mode(vk::SharingMode::EXCLUSIVE);
- if core.capture_replay {
+ if capture_replay {
info = info.flags(vk::BufferCreateFlags::DEVICE_ADDRESS_CAPTURE_REPLAY);
if replay.is_some_and(|(b, _)| b != 0) {
info = info.push_next(&mut buf_opaque);
@@ -2244,7 +2261,7 @@ fn make_allocation_ex(
// SAFETY: the buffer is the device's.
let requirements = unsafe { device.get_buffer_memory_requirements(buffer) };
let mut mem_flags = vk::MemoryAllocateFlags::DEVICE_ADDRESS;
- if core.capture_replay {
+ if capture_replay {
mem_flags |= vk::MemoryAllocateFlags::DEVICE_ADDRESS_CAPTURE_REPLAY;
}
let mut flags = vk::MemoryAllocateFlagsInfo::default().flags(mem_flags);
@@ -2254,7 +2271,7 @@ fn make_allocation_ex(
.allocation_size(requirements.size.max(len))
.memory_type_index(core.memory_type(class))
.push_next(&mut flags);
- if core.capture_replay && replay.is_some_and(|(_, m)| m != 0) {
+ if capture_replay && replay.is_some_and(|(_, m)| m != 0) {
allocate_info = allocate_info.push_next(&mut mem_opaque);
}
let bits = requirements.memory_type_bits;
@@ -2285,6 +2302,7 @@ fn make_allocation_ex(
live: 0,
placed: 0,
texture: false,
+ texture_only,
idle_since: crate::host_time(),
made: (0, 0),
wait: Wait::Progress(0),
@@ -2313,7 +2331,7 @@ fn make_allocation_ex(
let of = vk::BufferDeviceAddressInfo::default().buffer(buffer);
// SAFETY: the buffer has device-address usage and is bound.
allocation.address = unsafe { device.get_buffer_device_address(&of) };
- if core.capture_replay {
+ if capture_replay {
allocation.opaque_buffer_address =
unsafe { device.get_buffer_opaque_capture_address(&of) };
let mof = vk::DeviceMemoryOpaqueCaptureAddressInfo::default().memory(memory);
diff --git a/crates/metal2vulkan-runtime/src/places.rs b/crates/metal2vulkan-runtime/src/places.rs
index 3d679bbaa..774af81be 100644
--- a/crates/metal2vulkan-runtime/src/places.rs
+++ b/crates/metal2vulkan-runtime/src/places.rs
@@ -1,6 +1,8 @@
//! Places: the Private blocks the runtime places textures' images in, taken as the consumer takes
//! blocks. A texture block holds images alone, so no buffer lies beside an optimal image in one
//! block.
+//! When the device enables buffer address replay, the pool keeps texture allocations separate
+//! and leaves their replay flags clear. Restoring a buffer can still request its captured address.
//!
//! A place is a resource's bytes at a multiple of its alignment. One no longer than the class's
//! block length ([`MemoryClass::block_len`]) is the first that fits in the blocks of that length,
@@ -9,3 +9,4 @@ metal2vulkan metal2vulkan-snapshot-replay.patch
metal2vulkan metal2vulkan-psb-address-carry.patch
metal2vulkan metal2vulkan-dynamic-attachment-feedback.patch
metal2vulkan metal2vulkan-static-attachment-feedback.patch
metal2vulkan metal2vulkan-places-texture-pool.patch