Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏊‍♀️ Switch saxaboom-runtime to objc2 #28

Merged
merged 2 commits into from
Oct 3, 2024
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
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 = ["std", "MTLResource", "MTLBuffer", "MTLTexture", "MTLSampler", "MTLTypes"] }
27 changes: 16 additions & 11 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, MTLResourceID, 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::<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::<MTLResourceID, u64>(argument.gpuResourceID())
},
metadata: min_lod_clamp.to_bits() as u64 | (METADATA as u64) << 32,
}
}
Expand All @@ -81,11 +88,9 @@ impl ffi::IRDescriptorTableEntry {
/// See <https://developer.apple.com/metal/shader-converter/> for more info.
#[doc(alias = "IRDescriptorTableSetSampler")]
#[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 {
pub 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: unsafe { std::mem::transmute::<MTLResourceID, u64>(argument.gpuResourceID()) },
textureViewID: 0,
metadata: lod_bias.to_bits() as u64,
}
Expand Down