Skip to content

Commit

Permalink
Fix unsound MTLPackedFloat3
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jan 10, 2023
1 parent b86f64f commit 0c5bde7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
17 changes: 9 additions & 8 deletions crates/header-translator/src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ fn parse_struct(entity: &Entity<'_>, context: &Context<'_>) -> (bool, Vec<(Strin
EntityKind::ObjCBoxable => {
boxable = true;
}
EntityKind::UnionDecl => error!("can't handle unions in structs yet"),
_ => warn!("unknown"),
});

Expand Down Expand Up @@ -782,17 +783,17 @@ impl Stmt {
}]
}
EntityKind::UnionDecl => {
// debug!(
// "union: {:?}, {:?}, {:#?}, {:#?}",
// entity.get_display_name(),
// entity.get_name(),
// entity.has_attributes(),
// entity.get_children(),
// );
error!(
name = ?entity.get_name(),
has_attributes = ?entity.has_attributes(),
children = ?entity.get_children(),
"union",
);
vec![]
}
_ => {
panic!("Unknown: {entity:?}")
error!("unknown");
vec![]
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/header-translator/translation-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -777,3 +777,9 @@ newRenderPipelineStateWithDescriptor_options_reflection_error = { skipped = true
newComputePipelineStateWithFunction_options_reflection_error = { skipped = true }
newComputePipelineStateWithDescriptor_options_reflection_error = { skipped = true }
newRenderPipelineStateWithTileDescriptor_options_reflection_error = { skipped = true }

# Uses unions internally
[struct.MTLPackedFloat3]
skipped = true
[fn.MTLPackedFloat3Make]
skipped = true
58 changes: 56 additions & 2 deletions crates/icrate/src/Metal/fixes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::os::raw::c_double;
use core::fmt;

use objc2::ffi::NSUInteger;
use crate::common::*;
use objc2::encode::Encoding;

pub type CFTimeInterval = c_double;

Expand All @@ -12,3 +13,56 @@ pub const MTLResourceStorageModeMask: NSUInteger = 0xf << MTLResourceStorageMode

pub const MTLResourceHazardTrackingModeShift: NSUInteger = 8;
pub const MTLResourceHazardTrackingModeMask: NSUInteger = 0x3 << MTLResourceHazardTrackingModeShift;

// TODO: Investigate the need for this helper struct?
//
// I'm fairly sure that just replacing `MTLPackedFloat3` with this struct has
// the same effect in Rust as doing the trick with putting it in a union?
//
// Or is the intention actually to create something similar to `#[packed]`?
//
// <https://users.rust-lang.org/t/mapping-nested-packed-union-from-c-to-rust/87334>

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct __MTLPackedFloat3 {
x: c_float,
y: c_float,
z: c_float,
}

#[repr(C)]
#[derive(Clone, Copy)]
pub union MTLPackedFloat3 {
struct_: __MTLPackedFloat3,
elements: [c_float; 3],
}

impl PartialEq for MTLPackedFloat3 {
fn eq(&self, other: &Self) -> bool {
unsafe { self.struct_ == other.struct_ }
}
}

impl fmt::Debug for MTLPackedFloat3 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
unsafe { self.struct_.fmt(f) }
}
}

impl_encode! {
MTLPackedFloat3 = Encoding::Struct(
"_MTLPackedFloat3",
&[Encoding::Union(
"?",
&[Encoding::Struct(
"?",
&[c_float::ENCODING, c_float::ENCODING, c_float::ENCODING],
),
Encoding::Array(
3,
&c_float::ENCODING,
)],
)],
);
}
2 changes: 1 addition & 1 deletion crates/icrate/src/generated

0 comments on commit 0c5bde7

Please sign in to comment.