Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
FlannyH committed May 3, 2024
1 parent 437c372 commit 1e60901
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use std::{
)]
mod bindings;

use bindings::IRError;
pub use bindings::{
IRCSInfo_1_0, IRComparisonFunction, IRDescriptorRangeType, IRFilter, IRHitGroupType,
IRObjectType, IRRaytracingPipelineFlags, IRReflectionVersion, IRResourceLocation,
Expand All @@ -26,6 +25,7 @@ pub use bindings::{
IRVersionedCSInfo, IRVersionedRootSignatureDescriptor,
IRVersionedRootSignatureDescriptor__bindgen_ty_1 as IRVersionedRootSignatureDescriptor_u,
};
use bindings::{IRError, IRErrorCode};

pub struct IRShaderReflection {
me: *mut bindings::IRShaderReflection,
Expand Down Expand Up @@ -98,12 +98,12 @@ impl IRObject {
pub fn gather_raytracing_intrinsics(&self, entry_point: &CStr) -> u64 {
unsafe {
self.funcs
.IRObjectGatherRaytracingIntrinsics(self.me, entry_point.as_ptr().cast())
.IRObjectGatherRaytracingIntrinsics(self.me, entry_point.as_ptr())
}
}

pub fn get_type(&self) -> IRObjectType {
unsafe { self.funcs.IRObjectGetType(self.me.cast_const()) }
unsafe { self.funcs.IRObjectGetType(self.me) }
}

pub fn get_metal_ir_shader_stage(&self) -> IRShaderStage {
Expand Down Expand Up @@ -158,10 +158,11 @@ impl IRMetalLibBinary {
pub fn get_byte_code(&self) -> Vec<u8> {
let size_in_bytes = unsafe { self.funcs.IRMetalLibGetBytecodeSize(self.me) };
let mut bytes = vec![0u8; size_in_bytes];
let _written = unsafe {
let written = unsafe {
self.funcs
.IRMetalLibGetBytecode(self.me, bytes.as_mut_ptr())
};
assert_eq!(written, size_in_bytes);
bytes
}
}
Expand All @@ -181,26 +182,34 @@ impl IRRootSignature {
pub fn create_from_descriptor(
compiler: &IRCompiler,
desc: &IRVersionedRootSignatureDescriptor,
) -> Result<IRRootSignature, Box<dyn std::error::Error>> {
) -> Result<IRRootSignature, String> {
unsafe {
let mut error: *mut IRError = std::ptr::null_mut::<IRError>();

let me = compiler
.funcs
.IRRootSignatureCreateFromDescriptor(desc, &mut error);

// If the root signature failed to create
if !error.is_null() {
// IRErrorCode is #[repr(u32)], so this transmute should be fine
let code: u32 = compiler.funcs.IRErrorGetCode(error);
let code: IRErrorCode = std::mem::transmute(code);
return Err(format!(
"Root Signature creation failed with error code {code:?}"
));
}

Ok(Self {
funcs: compiler.funcs.clone(),
me,
funcs: compiler.funcs.clone(),
})
}
}

pub fn get_resource_locations(&self) -> Vec<IRResourceLocation> {
unsafe {
let n_resources = self
.funcs
.IRRootSignatureGetResourceCount(self.me as *const _);
let n_resources = self.funcs.IRRootSignatureGetResourceCount(self.me);
let empty_location = IRResourceLocation {
resourceType: IRResourceType::IRResourceTypeInvalid,
space: 0,
Expand All @@ -210,15 +219,16 @@ impl IRRootSignature {
resourceName: std::ptr::null(),
};
let mut resource_locations = vec![empty_location; n_resources];
self.funcs.IRRootSignatureGetResourceLocations(
self.me as *const _,
resource_locations.as_mut_ptr(),
);
self.funcs
.IRRootSignatureGetResourceLocations(self.me, resource_locations.as_mut_ptr());
resource_locations
}
}
}

/// [IRCompilerFactory] is used to load the metal_irconverter dynamic library and holds its functions in an Arc.
/// Since [IRCompiler] is not thread-safe, this struct provides an interface to create [IRCompiler] instances.
/// This way, the library only has to be loaded once, but each thread can have its own [IRCompiler] instance.
pub struct IRCompilerFactory {
funcs: Arc<bindings::metal_irconverter>,
}
Expand All @@ -237,8 +247,8 @@ impl IRCompilerFactory {
pub fn create_compiler(&self) -> IRCompiler {
let compiler = unsafe { self.funcs.IRCompilerCreate() };
IRCompiler {
funcs: self.funcs.clone(),
me: compiler,
funcs: self.funcs.clone(),
}
}
}
Expand All @@ -247,8 +257,8 @@ impl IRCompilerFactory {
///
/// [the Metal shader converter documentation]: https://developer.apple.com/metal/shader-converter/
pub struct IRCompiler {
funcs: Arc<bindings::metal_irconverter>,
me: *mut bindings::IRCompiler,
funcs: Arc<bindings::metal_irconverter>,
}

impl Drop for IRCompiler {
Expand Down Expand Up @@ -299,21 +309,31 @@ impl IRCompiler {

pub fn synthesize_indirect_intersection_function(
&mut self,
target_metallib: &mut IRMetalLibBinary,
) -> bool {
unsafe {
) -> Result<IRMetalLibBinary, String> {
let target_metallib = IRMetalLibBinary::new(&self).unwrap();
if unsafe {
self.funcs
.IRMetalLibSynthesizeIndirectIntersectionFunction(self.me, target_metallib.me)
} {
return Ok(target_metallib);
}
{
Err("Failed to synthezize indirect intersection function".to_string())
}
}

pub fn synthesize_indirect_ray_dispatch_function(
&mut self,
target_metallib: &mut IRMetalLibBinary,
) -> bool {
unsafe {
) -> Result<IRMetalLibBinary, String> {
let target_metallib = IRMetalLibBinary::new(&self).unwrap();
if unsafe {
self.funcs
.IRMetalLibSynthesizeIndirectRayDispatchFunction(self.me, target_metallib.me)
} {
return Ok(target_metallib);
}
{
Err("Failed to synthezize indirect intersection function".to_string())
}
}

Expand Down Expand Up @@ -342,8 +362,8 @@ impl IRCompiler {

if error.is_null() {
Ok(IRObject {
funcs: input.funcs.clone(),
me: v,
funcs: input.funcs.clone(),
})
} else {
panic!("{:?}", error);
Expand Down

0 comments on commit 1e60901

Please sign in to comment.