Skip to content

Commit 0231aa8

Browse files
committed
Implements Device::tlas_instance_to_bytes().
1 parent 97d4dda commit 0231aa8

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

wgpu-hal/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ metal = [
7575
"naga/msl-out",
7676
"dep:arrayvec",
7777
"dep:block2",
78+
"dep:bytemuck",
7879
"dep:hashbrown",
7980
"dep:libc",
8081
"dep:log",
@@ -275,6 +276,7 @@ mach-dxcompiler-rs = { workspace = true, optional = true }
275276
[target.'cfg(target_vendor = "apple")'.dependencies]
276277
# Backend: Metal
277278
block2 = { workspace = true, optional = true }
279+
bytemuck = { workspace = true, optional = true }
278280
objc2 = { workspace = true, optional = true }
279281
objc2-core-foundation = { workspace = true, optional = true }
280282
objc2-foundation = { workspace = true, optional = true }

wgpu-hal/src/metal/device.rs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ use alloc::{borrow::ToOwned as _, sync::Arc, vec::Vec};
22
use core::{ptr::NonNull, sync::atomic};
33
use std::{thread, time};
44

5+
use bytemuck::TransparentWrapper;
56
use objc2::{
67
msg_send,
78
rc::{autoreleasepool, Retained},
89
runtime::ProtocolObject,
910
};
1011
use objc2_foundation::{ns_string, NSError, NSRange, NSString};
1112
use objc2_metal::{
12-
MTLAccelerationStructure, MTLBuffer, MTLCaptureManager, MTLCaptureScope, MTLCommandBuffer,
13-
MTLCommandBufferStatus, MTLCompileOptions, MTLComputePipelineDescriptor,
14-
MTLComputePipelineState, MTLCounterSampleBufferDescriptor, MTLCounterSet, MTLDepthClipMode,
15-
MTLDepthStencilDescriptor, MTLDevice, MTLFunction, MTLLanguageVersion, MTLLibrary,
16-
MTLMutability, MTLPipelineBufferDescriptorArray, MTLPixelFormat, MTLPrimitiveTopologyClass,
13+
MTLAccelerationStructure, MTLAccelerationStructureInstanceOptions, MTLBuffer,
14+
MTLCaptureManager, MTLCaptureScope, MTLCommandBuffer, MTLCommandBufferStatus,
15+
MTLCompileOptions, MTLComputePipelineDescriptor, MTLComputePipelineState,
16+
MTLCounterSampleBufferDescriptor, MTLCounterSet, MTLDepthClipMode, MTLDepthStencilDescriptor,
17+
MTLDevice, MTLFunction, MTLIndirectAccelerationStructureInstanceDescriptor, MTLLanguageVersion,
18+
MTLLibrary, MTLMutability, MTLPackedFloat3, MTLPackedFloat4x3,
19+
MTLPipelineBufferDescriptorArray, MTLPixelFormat, MTLPrimitiveTopologyClass,
1720
MTLRenderPipelineDescriptor, MTLResource, MTLResourceID, MTLResourceOptions,
1821
MTLSamplerAddressMode, MTLSamplerDescriptor, MTLSamplerMipFilter, MTLSamplerState, MTLSize,
1922
MTLStencilDescriptor, MTLStorageMode, MTLTexture, MTLTextureDescriptor, MTLTextureType,
@@ -1790,8 +1793,42 @@ impl crate::Device for super::Device {
17901793
// self.counters.acceleration_structures.sub(1);
17911794
}
17921795

1793-
fn tlas_instance_to_bytes(&self, _instance: TlasInstance) -> Vec<u8> {
1794-
unimplemented!()
1796+
fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8> {
1797+
let temp = MTLIndirectAccelerationStructureInstanceDescriptor {
1798+
transformationMatrix: MTLPackedFloat4x3 {
1799+
columns: [
1800+
MTLPackedFloat3 {
1801+
x: instance.transform[0],
1802+
y: instance.transform[4],
1803+
z: instance.transform[8],
1804+
},
1805+
MTLPackedFloat3 {
1806+
x: instance.transform[1],
1807+
y: instance.transform[5],
1808+
z: instance.transform[9],
1809+
},
1810+
MTLPackedFloat3 {
1811+
x: instance.transform[2],
1812+
y: instance.transform[6],
1813+
z: instance.transform[10],
1814+
},
1815+
MTLPackedFloat3 {
1816+
x: instance.transform[3],
1817+
y: instance.transform[7],
1818+
z: instance.transform[11],
1819+
},
1820+
],
1821+
},
1822+
options: MTLAccelerationStructureInstanceOptions::None,
1823+
mask: instance.mask as u32,
1824+
intersectionFunctionTableOffset: 0,
1825+
userID: instance.custom_data,
1826+
accelerationStructureID: unsafe { MTLResourceID::from_raw(instance.blas_address) },
1827+
};
1828+
1829+
wgt::bytemuck_wrapper!(unsafe struct Desc(MTLIndirectAccelerationStructureInstanceDescriptor));
1830+
1831+
bytemuck::bytes_of(&Desc::wrap(temp)).to_vec()
17951832
}
17961833

17971834
fn get_internal_counters(&self) -> wgt::HalCounters {

0 commit comments

Comments
 (0)