Skip to content

Commit edeaaba

Browse files
committed
Uses MTLResidencySet for TLAS dependencies.
1 parent a5378f3 commit edeaaba

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

wgpu-hal/src/metal/command.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use hashbrown::HashSet;
12
use objc2::{
23
rc::{autoreleasepool, Retained},
34
runtime::ProtocolObject,
@@ -7,8 +8,8 @@ use objc2_metal::{
78
MTLAccelerationStructureCommandEncoder, MTLBlitCommandEncoder, MTLBlitPassDescriptor,
89
MTLCommandBuffer, MTLCommandEncoder, MTLCommandQueue, MTLComputeCommandEncoder,
910
MTLComputePassDescriptor, MTLCounterDontSample, MTLLoadAction, MTLPrimitiveType,
10-
MTLRenderCommandEncoder, MTLRenderPassDescriptor, MTLScissorRect, MTLSize, MTLStoreAction,
11-
MTLTexture, MTLViewport, MTLVisibilityResultMode,
11+
MTLRenderCommandEncoder, MTLRenderPassDescriptor, MTLResidencySet, MTLScissorRect, MTLSize,
12+
MTLStoreAction, MTLTexture, MTLViewport, MTLVisibilityResultMode,
1213
};
1314

1415
use super::{conv, TimestampQuerySupport};
@@ -37,6 +38,7 @@ impl Default for super::CommandState {
3738
height: 0,
3839
},
3940
stage_infos: Default::default(),
41+
residency_sets: Default::default(),
4042
storage_buffer_length_map: Default::default(),
4143
vertex_buffer_size_map: Default::default(),
4244
work_group_memory_sizes: Vec::new(),
@@ -284,8 +286,11 @@ impl crate::CommandEncoder for super::CommandEncoder {
284286
debug_assert!(self.state.compute.is_none());
285287
debug_assert!(self.state.pending_timer_queries.is_empty());
286288

289+
let mut residency_sets = HashSet::new();
290+
core::mem::swap(&mut residency_sets, &mut self.state.residency_sets);
287291
Ok(super::CommandBuffer {
288292
raw: self.raw_cmd_buf.take().unwrap(),
293+
residency_sets,
289294
})
290295
}
291296

@@ -455,6 +460,14 @@ impl crate::CommandEncoder for super::CommandEncoder {
455460
dst: &super::AccelerationStructure,
456461
copy: wgt::AccelerationStructureCopy,
457462
) {
463+
self.state.residency_sets.insert(dst.residency_set.clone());
464+
dst.residency_set.requestResidency();
465+
dst.residency_set.removeAllAllocations();
466+
let allocations = src.residency_set.allAllocations();
467+
for index in 0..allocations.count() {
468+
dst.residency_set
469+
.addAllocation(&allocations.objectAtIndex(index));
470+
}
458471
let command_encoder = self.enter_acceleration_structure_builder();
459472
match copy {
460473
wgt::AccelerationStructureCopy::Clone => {
@@ -1479,6 +1492,31 @@ impl crate::CommandEncoder for super::CommandEncoder {
14791492
for descriptor in descriptors {
14801493
let acceleration_structure_descriptor =
14811494
conv::map_acceleration_structure_descriptor(descriptor.entries, descriptor.flags);
1495+
if matches!(
1496+
descriptor.entries,
1497+
crate::AccelerationStructureEntries::Instances(_)
1498+
) {
1499+
self.state.residency_sets.insert(
1500+
descriptor
1501+
.destination_acceleration_structure
1502+
.residency_set
1503+
.clone(),
1504+
);
1505+
descriptor
1506+
.destination_acceleration_structure
1507+
.residency_set
1508+
.requestResidency();
1509+
descriptor
1510+
.destination_acceleration_structure
1511+
.residency_set
1512+
.removeAllAllocations();
1513+
for dependency in descriptor.dependencies.iter() {
1514+
descriptor
1515+
.destination_acceleration_structure
1516+
.residency_set
1517+
.addAllocation(dependency.raw.as_ref());
1518+
}
1519+
}
14821520
match descriptor.mode {
14831521
crate::AccelerationStructureBuildMode::Build => {
14841522
command_encoder

wgpu-hal/src/metal/device.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ use objc2_metal::{
1717
MTLDevice, MTLFunction, MTLIndirectAccelerationStructureInstanceDescriptor, MTLLanguageVersion,
1818
MTLLibrary, MTLMutability, MTLPackedFloat3, MTLPackedFloat4x3,
1919
MTLPipelineBufferDescriptorArray, MTLPixelFormat, MTLPrimitiveTopologyClass,
20-
MTLRenderPipelineDescriptor, MTLResource, MTLResourceID, MTLResourceOptions,
21-
MTLSamplerAddressMode, MTLSamplerDescriptor, MTLSamplerMipFilter, MTLSamplerState, MTLSize,
22-
MTLStencilDescriptor, MTLStorageMode, MTLTexture, MTLTextureDescriptor, MTLTextureType,
23-
MTLTriangleFillMode, MTLVertexDescriptor, MTLVertexStepFunction,
20+
MTLRenderPipelineDescriptor, MTLResidencySetDescriptor, MTLResource, MTLResourceID,
21+
MTLResourceOptions, MTLSamplerAddressMode, MTLSamplerDescriptor, MTLSamplerMipFilter,
22+
MTLSamplerState, MTLSize, MTLStencilDescriptor, MTLStorageMode, MTLTexture,
23+
MTLTextureDescriptor, MTLTextureType, MTLTriangleFillMode, MTLVertexDescriptor,
24+
MTLVertexStepFunction,
2425
};
2526
use parking_lot::Mutex;
2627

@@ -1694,10 +1695,18 @@ impl crate::Device for super::Device {
16941695
// self.counters.acceleration_structures.add(1);
16951696
let device = self.shared.device.lock();
16961697
autoreleasepool(|_| {
1698+
let residency_set_descriptor = MTLResidencySetDescriptor::new();
1699+
residency_set_descriptor.setInitialCapacity(1);
16971700
Ok(super::AccelerationStructure {
16981701
raw: device
16991702
.newAccelerationStructureWithSize(descriptor.size as usize)
17001703
.ok_or(crate::DeviceError::OutOfMemory)?,
1704+
residency_set: device
1705+
.newResidencySetWithDescriptor_error(&residency_set_descriptor)
1706+
.map_err(|err| {
1707+
log::error!("Failed to create residency set: {err:?}");
1708+
crate::DeviceError::Unexpected
1709+
})?,
17011710
})
17021711
})
17031712
}

wgpu-hal/src/metal/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use core::{fmt, iter, ops, ptr::NonNull, sync::atomic};
3737

3838
use arrayvec::ArrayVec;
3939
use bitflags::bitflags;
40-
use hashbrown::HashMap;
40+
use hashbrown::{HashMap, HashSet};
4141
use naga::FastHashMap;
4242
use objc2::{
4343
rc::{autoreleasepool, Retained},
@@ -50,7 +50,7 @@ use objc2_metal::{
5050
MTLComputeCommandEncoder, MTLComputePipelineState, MTLCounterSampleBuffer, MTLCullMode,
5151
MTLDepthClipMode, MTLDepthStencilState, MTLDevice, MTLDrawable, MTLFunction, MTLIndexType,
5252
MTLLanguageVersion, MTLLibrary, MTLPrimitiveType, MTLReadWriteTextureTier,
53-
MTLRenderCommandEncoder, MTLRenderPipelineState, MTLRenderStages, MTLResource,
53+
MTLRenderCommandEncoder, MTLRenderPipelineState, MTLRenderStages, MTLResidencySet, MTLResource,
5454
MTLResourceUsage, MTLSamplerState, MTLSharedEvent, MTLSize, MTLTexture, MTLTextureType,
5555
MTLTriangleFillMode, MTLWinding,
5656
};
@@ -489,6 +489,9 @@ impl crate::Queue for Queue {
489489

490490
for cmd_buffer in command_buffers {
491491
cmd_buffer.raw.commit();
492+
for residency_set in &cmd_buffer.residency_sets {
493+
residency_set.commit();
494+
}
492495
}
493496

494497
if let Some(raw) = extra_command_buffer {
@@ -929,6 +932,7 @@ struct CommandState {
929932
index: Option<IndexState>,
930933
raw_wg_size: MTLSize,
931934
stage_infos: MultiStageData<PipelineStageInfo>,
935+
residency_sets: HashSet<Retained<ProtocolObject<dyn MTLResidencySet>>>,
932936

933937
/// Sizes of currently bound [`wgt::BufferBindingType::Storage`] buffers.
934938
///
@@ -984,6 +988,7 @@ unsafe impl Sync for CommandEncoder {}
984988
#[derive(Debug)]
985989
pub struct CommandBuffer {
986990
raw: Retained<ProtocolObject<dyn MTLCommandBuffer>>,
991+
residency_sets: HashSet<Retained<ProtocolObject<dyn MTLResidencySet>>>,
987992
}
988993

989994
impl crate::DynCommandBuffer for CommandBuffer {}
@@ -999,6 +1004,7 @@ impl crate::DynPipelineCache for PipelineCache {}
9991004
#[derive(Debug)]
10001005
pub struct AccelerationStructure {
10011006
raw: Retained<ProtocolObject<dyn MTLAccelerationStructure>>,
1007+
residency_set: Retained<ProtocolObject<dyn MTLResidencySet>>,
10021008
}
10031009

10041010
impl AccelerationStructure {

0 commit comments

Comments
 (0)