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

📚Separate main() into its own function in lib.rs #5

Closed
wants to merge 2 commits into from

Conversation

FlannyH
Copy link
Contributor

@FlannyH FlannyH commented Sep 19, 2023

Refactored the main() function into its own function in lib.rs, so it's also accessible when used as a library rather than a standalone application.

compile_dxil_to_metallib() takes in a Vec<u8> containing a DXIL binary, compiles it, and returns a Vec<u8> containing a Metallib binary
compile_dxil_to_metallib_from_path() takes in a path to a DXIL binary file, and returns a Vec<u8> containing a Metallib binary

@FlannyH
Copy link
Contributor Author

FlannyH commented Sep 19, 2023

I have not tested this on Windows yet, and there seem to be some issues the CI caught

@FlannyH FlannyH marked this pull request as ready for review September 20, 2023 09:05
@FlannyH FlannyH requested review from Jasper-Bekkers, a team and tosti007 and removed request for a team September 20, 2023 09:06
@@ -1,2 +1,5 @@
/target
/Cargo.lock
.DS_Store
.vscode/launch.json
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just add the entire .vscode folder?

@@ -601,3 +597,149 @@ impl<'lib> IRCompiler<'lib> {
}
}
}

// Moved from main.rs to lib.rs
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this comment doesn't really add anything.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intended addition?

Comment on lines +737 to +740
pub fn compile_dxil_to_metallib_from_path(
path_compiler_lib: &Path,
path_dxil: &Path,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're turning these into public functions, they should have doc-comments.

path_dxil: &Path,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
// Load DXIL binary from path
let dxil_binary = std::fs::read(path_dxil)?;
Copy link
Member

@MarijnS95 MarijnS95 Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're only reading a file into binary, let the user pass a byte slice. This would make it compatible with our asset pipeline that does not give you access to plain files.
I.e. there is no point to provide a "convenience _from_path() variant, only the byte-slice version suffices.

Perhaps the same for the compiler library itself. Even better: how expensive is it to load the compiler and interface for every shader? Perhaps we need to provide an object-based API of this sort:

let compiler = MetalCompiler::new("path/to/libirmetallibbinary.so")?; // TODO: Should there be a function with a sensible default path? Is it available on the system?

let _compiled = compiler.compile(&[0x13, 0x37, ...])?;

print!("Testing shader \"{shader_path}\": ");
match compile_dxil_to_metallib_from_path(&Path::new("/usr/local/lib/libmetalirconverter.dylib"), &Path::new(shader_path)) {
Ok(_) => println!("\tOk."),
Err(e) => println!("Error: {e:?}"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's return anyhow::Result from fn main()?

/// Takes a DXIL binary, cross-compiles it to metal and returns a metallib binary
pub fn compile_dxil_to_metallib(
path_compiler_lib: &Path,
dxil_binary: &Vec<u8>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clippy will probably complain that this should be a slice, instead of a borrow of a forcibly-owned Vec.

}

/// Takes a DXIL binary, cross-compiles it to metal and returns a metallib binary
pub fn compile_dxil_to_metallib(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of this code should live in saxaboom, since it's specific to how breda sets up it's bindings.

@FlannyH
Copy link
Contributor Author

FlannyH commented Oct 12, 2023

This entire PR can be closed. All the functionality added here was also added in the breda repository, making this redundant

@FlannyH FlannyH closed this Oct 12, 2023
@MarijnS95 MarijnS95 deleted the separate-function branch October 12, 2023 13:25
@MarijnS95
Copy link
Member

Sounds good! It looks like all of this example code is more-or-less add-hoc depending on project-specifics :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants