Skip to content

Commit

Permalink
Remove reliance on IRCompiler in unrelated objects
Browse files Browse the repository at this point in the history
In the previous PR we separated the loaded function pointers (strictly:
"just the library") from a created `IRCompiler` instance, because those
instances need to be mutated and are not thread-safe.  We also borrowed
the `IRCompiler` inside `IRObject` to pass into some other `fn new()`
constructors, making sure that they would use the same compiler instead
of taking that as a separate argument.

This is super inconvenient, because `IRCompiler` cannot be mutated
(mutably borrowed) while an `IRObject` is live.  And for no good reason:
when splitting `IRCompiler` we forgot to make sure that most constructor
functions only take the function pointers as they don't need the full
`IRCompiler` in the first place.  By replacing it with the function
pointers in every constructor we no longer need to borrow `IRCompiler`
inside `IRObject`.

Also move some public constructor functions directly on the
`MetalIrConverter` struct instead of having to pass it in as borrowed
argument.  For constructor functions that don't need to be used
publicly (i.e. only via special getter functions that create an object
before filling it with data, specifically `IRShaderReflection` and
`IRMetalLibBinary`), the constructor is made private and takes the
function pointer structure directly.
  • Loading branch information
MarijnS95 committed Jul 12, 2024
1 parent fac0ee8 commit 00e4978
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 133 deletions.
12 changes: 6 additions & 6 deletions examples/compute_shader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use saxaboom::{ffi, IRCompilerFactory, IRObject, IRRootSignature};
use saxaboom::{ffi, MetalIrConverter};

fn create_static_sampler(
min_mag_mip_mode: ffi::IRFilter,
Expand Down Expand Up @@ -28,8 +28,8 @@ fn create_static_sampler(
fn main() -> Result<(), Box<dyn std::error::Error>> {
unsafe {
// Create an instance of IRCompiler
let compiler_factory = IRCompilerFactory::new("libmetalirconverter.dylib").unwrap();
let mut compiler = compiler_factory.create_compiler();
let metal_irconverter = MetalIrConverter::new("libmetalirconverter.dylib").unwrap();
let mut compiler = metal_irconverter.create_compiler();

// Create an explicit root signature layout
let mut parameters = create_root_parameters();
Expand All @@ -48,16 +48,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
u_1: ffi::IRVersionedRootSignatureDescriptor_u { desc_1_1 },
};

let root_sig = IRRootSignature::create_from_descriptor(&compiler, &desc)?;
let root_sig = metal_irconverter.create_root_signature_from_descriptor(&desc)?;
compiler.set_global_root_signature(&root_sig);

// Load DXIL
let dxil = include_bytes!("assets/memcpy.cs.dxil");
let obj = IRObject::create_from_dxil(&compiler, dxil);
let obj = metal_irconverter.create_object_from_dxil(dxil);

// Convert to Metal
let mtllib = compiler.alloc_compile_and_link(c"main", &obj)?;
let mtl_binary = mtllib.metal_lib_binary()?;
let mtl_binary = mtllib.metal_lib_binary().unwrap();

// Get Metal bytecode
let metal_bytecode = mtl_binary.byte_code();
Expand Down
Loading

0 comments on commit 00e4978

Please sign in to comment.