Go to file
2024-09-18 18:26:08 +02:00
slang-sys Improve error handling 2024-09-18 18:26:08 +02:00
src Improve error handling 2024-09-18 18:26:08 +02:00
.gitignore Initial commit 2024-03-02 10:51:46 +01:00
Cargo.toml Initial commit 2024-03-02 10:51:46 +01:00
LICENSE-APACHE Initial commit 2024-03-02 10:51:46 +01:00
LICENSE-MIT Initial commit 2024-03-02 10:51:46 +01:00
README.md Improve error handling 2024-09-18 18:26:08 +02:00
rustfmt.toml Add rustfmt config and format code 2024-05-11 15:42:42 +02:00

Slang Rust Bindings

Rust bindings for the Slang shader language compiler. In contrast to existing bindings, these internally use Slang's COM/C++ API because the old C API is soon to be deprecated.

Currently mostly reflects the needs of our own engine but issues and pull requests are more than welcome.

Example

let global_session = slang::GlobalSession::new().unwrap();

let search_path = std::ffi::CString::new("shaders/directory").unwrap();

// All compiler options are available through this builder.
let session_options = slang::OptionsBuilder::new()
	.optimization(slang::OptimizationLevel::High)
	.matrix_layout_row(true);

let target_desc = slang::TargetDescBuilder::new()
	.format(slang::CompileTarget::Dxil)
	.profile(self.global_session.find_profile("sm_6_5"));

let session_desc = slang::SessionDescBuilder::new()
	.targets(&[*target_desc])
	.search_paths(&[include_path.as_ptr()])
	.options(&session_options);

let session = self.global_session.create_session(&session_desc).unwrap();

let module = session.load_module("filename.slang").unwrap();

let entry_point = module.find_entry_point_by_name("main").unwrap();

let program = session.create_composite_component_type(&[
	module.downcast().clone(), entry_point.downcast().clone(),
]).unwrap();

let linked_program = program.link().unwrap();

let shader_bytecode = linked_program.get_entry_point_code(0, 0).unwrap();

Installation

Add the following to the [dependencies] section of your Cargo.toml:

slang = { git = "https://github.com/FloatyMonkey/slang-rs.git" }

Set the SLANG_DIR environment variable to the path of your Slang installation. Download the latest release from their releases page. Copy slang.dll to your executable's directory.

To compile to DXIL bytecode you need the Microsoft DirectXShaderCompiler. Download the latest release from their releases page. Copy dxil.dll and dxcompiler.dll to your executable's directory.

Credits

Maintained by Lauro Oyen (@laurooyen).

Licensed under MIT or Apache-2.0.