2024-03-02 04:51:46 -05:00
# Slang Rust Bindings
2024-10-28 15:50:45 -04:00
Rust bindings for the [Slang ](https://github.com/shader-slang/slang/ ) shader language. Supporting both the modern compilation and reflection API.
2024-03-02 04:51:46 -05:00
2024-10-28 15:50:45 -04:00
Currently mostly reflects the needs of our own [engine ](https://github.com/FloatyMonkey/engine ) but contributions are more than welcome.
2024-03-02 04:51:46 -05:00
## Example
```rust
2024-04-04 07:32:30 -04:00
let global_session = slang::GlobalSession::new().unwrap();
2024-03-02 04:51:46 -05:00
2024-04-04 07:32:30 -04:00
let search_path = std::ffi::CString::new("shaders/directory").unwrap();
2024-03-02 04:51:46 -05:00
2024-04-04 07:32:30 -04:00
// All compiler options are available through this builder.
let session_options = slang::OptionsBuilder::new()
.optimization(slang::OptimizationLevel::High)
.matrix_layout_row(true);
2024-03-02 04:51:46 -05:00
2024-04-04 07:32:30 -04:00
let target_desc = slang::TargetDescBuilder::new()
.format(slang::CompileTarget::Dxil)
.profile(self.global_session.find_profile("sm_6_5"));
2024-03-02 04:51:46 -05:00
2024-04-04 07:32:30 -04:00
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(& [
2024-09-18 12:26:08 -04:00
module.downcast().clone(), entry_point.downcast().clone(),
]).unwrap();
2024-04-04 07:32:30 -04:00
2024-09-18 12:26:08 -04:00
let linked_program = program.link().unwrap();
2024-04-04 07:32:30 -04:00
2024-10-28 15:50:45 -04:00
// Entry point to the reflection API.
let reflection = linked_program.layout(0).unwrap();
2024-09-18 12:26:08 -04:00
let shader_bytecode = linked_program.get_entry_point_code(0, 0).unwrap();
2024-03-02 04:51:46 -05:00
```
## Installation
Add the following to the `[dependencies]` section of your `Cargo.toml` :
```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 ](https://github.com/shader-slang/slang/releases ). 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 ](https://github.com/microsoft/DirectXShaderCompiler/releases ). Copy `dxil.dll` and `dxcompiler.dll` to your executable's directory.
## Credits
Maintained by Lauro Oyen ([@laurooyen](https://github.com/laurooyen)).
Licensed under [MIT ](LICENSE-MIT ) or [Apache-2.0 ](LICENSE-APACHE ).