Skip to content

Commit

Permalink
Switch to objc2
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper-Bekkers committed Oct 2, 2024
1 parent 98b128d commit 2c5a5c7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
3 changes: 2 additions & 1 deletion runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ targets = [
]

[dependencies]
metal = { version = "0.29", default-features = false }
objc2 = { version = "0.5", default-features = false }
objc2-metal = { version = "0.2.2", default-features = false, features = ["alloc", "std", "MTLResource", "MTLBuffer", "MTLTexture", "MTLSampler", "MTLTypes"] }
27 changes: 18 additions & 9 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ pub mod bindings {
// Use an include to be able to import more items into this module as below
include!("bindings.rs");

pub use metal::MTLResourceID;
pub use objc2_metal::MTLResourceID;
}
pub use bindings as ffi;

use objc2::runtime::ProtocolObject;
use objc2_metal::{MTLBuffer, MTLSamplerState, MTLTexture};

/// Rust version of `IRBufferView` using [`metal`] types.
#[doc(alias = "IRBufferView")]
pub struct BufferView<'a> {
pub buffer: &'a metal::Buffer,
pub buffer: &'a ProtocolObject<dyn MTLBuffer>,
pub buffer_offset: u64,
pub buffer_size: u64,
pub texture_buffer_view: Option<&'a metal::Texture>,
pub texture_buffer_view: Option<&'a ProtocolObject<dyn MTLTexture>>,
pub texture_view_offset_in_elements: u32,
pub typed_buffer: bool,
}
Expand Down Expand Up @@ -52,9 +55,11 @@ impl ffi::IRDescriptorTableEntry {
#[doc(alias = "IRDescriptorTableSetBufferView")]
pub fn buffer_view(buffer_view: &BufferView<'_>) -> Self {
Self {
gpuVA: buffer_view.buffer.gpu_address() + buffer_view.buffer_offset,
gpuVA: buffer_view.buffer.gpuAddress() + buffer_view.buffer_offset,
textureViewID: match buffer_view.texture_buffer_view {
Some(texture) => texture.gpu_resource_id()._impl,
Some(texture) => unsafe {
std::mem::transmute::<objc2_metal::MTLResourceID, u64>(texture.gpuResourceID())
},
None => 0,
},
metadata: Self::buffer_metadata(buffer_view),
Expand All @@ -66,11 +71,13 @@ impl ffi::IRDescriptorTableEntry {
/// This function is a port of the `IRDescriptorTableSetTexture` function in the `metal_irconverter_runtime.h` header.
/// See <https://developer.apple.com/metal/shader-converter/> for more info.
#[doc(alias = "IRDescriptorTableSetTexture")]
pub fn texture(argument: &metal::Texture, min_lod_clamp: f32) -> Self {
pub fn texture(argument: &ProtocolObject<dyn MTLTexture>, min_lod_clamp: f32) -> Self {
const METADATA: u32 = 0; // According to the current docs, the metadata must be 0
Self {
gpuVA: 0,
textureViewID: argument.gpu_resource_id()._impl,
textureViewID: unsafe {
std::mem::transmute::<objc2_metal::MTLResourceID, u64>(argument.gpuResourceID())
},
metadata: min_lod_clamp.to_bits() as u64 | (METADATA as u64) << 32,
}
}
Expand All @@ -83,9 +90,11 @@ impl ffi::IRDescriptorTableEntry {
#[allow(unused_variables, unreachable_code, dead_code)]
// TODO: Expose this function when metal-rs contains the update
/* pub */
fn sampler(argument: &metal::SamplerState, lod_bias: f32) -> Self {
fn sampler(argument: &ProtocolObject<dyn MTLSamplerState>, lod_bias: f32) -> Self {
Self {
gpuVA: todo!("Add gpu_resource_id() to SamplerState: https://github.com/gfx-rs/metal-rs/pull/328"), // argument.gpu_resource_id()._impl,
gpuVA: todo!(
"Add gpuResourceID() to SamplerState: https://github.com/gfx-rs/metal-rs/pull/328"
), // argument.gpuResourceID()._impl,
textureViewID: 0,
metadata: lod_bias.to_bits() as u64,
}
Expand Down

0 comments on commit 2c5a5c7

Please sign in to comment.