Created
December 1, 2025 23:21
-
-
Save kanerogers/56c9925c87b2714899bc1c9dc34e7544 to your computer and use it in GitHub Desktop.
slang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use crate::{ | |
| camera::Camera, | |
| simulation::{Grid, Simulation}, | |
| }; | |
| use lazy_vulkan::StateFamily; | |
| use shader_slang::{self as slang, Downcast}; | |
| use std::{ | |
| cell::RefCell, | |
| rc::Rc, | |
| time::{Duration, Instant}, | |
| }; | |
| pub fn build_shaders() { | |
| println!("Rebuilding Slang shaders!"); | |
| let global_session = slang::GlobalSession::new().unwrap(); | |
| let search_path = std::ffi::CString::new("shaders/").unwrap(); | |
| // All compiler options are available through this builder. | |
| let session_options = slang::CompilerOptions::default() | |
| .optimization(slang::OptimizationLevel::High) | |
| .debug_information(slang::DebugInfoLevel::Maximal) | |
| .matrix_layout_row(true); | |
| let target_desc = slang::TargetDesc::default() | |
| .format(slang::CompileTarget::Spirv) | |
| .profile(global_session.find_profile("glsl_460")); | |
| let targets = [target_desc]; | |
| let search_paths = [search_path.as_ptr()]; | |
| let session_desc = slang::SessionDesc::default() | |
| .targets(&targets) | |
| .search_paths(&search_paths) | |
| .options(&session_options); | |
| let session = global_session.create_session(&session_desc).unwrap(); | |
| let module = session.load_module("debug_line.slang").unwrap(); | |
| for (entry_point_name, file_type) in [ | |
| ("generate_vertices", "comp"), | |
| ("vs_main", "vert"), | |
| ("fs_main", "frag"), | |
| ] { | |
| let entry_point = module.find_entry_point_by_name(entry_point_name).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.entry_point_code(0, 0).unwrap(); | |
| std::fs::write( | |
| format!("shaders/debug_line.{file_type}.spv"), | |
| shader_bytecode.as_slice(), | |
| ) | |
| .unwrap(); | |
| } | |
| } | |
| pub fn watch_shaders() { | |
| build_shaders(); | |
| std::thread::spawn(|| { | |
| println!("Shader watcher thread spawned!"); | |
| let mut last_modified = get_last_modified("shaders/debug_line.slang"); | |
| loop { | |
| let new_last_modified = get_last_modified("shaders/debug_line.slang"); | |
| if new_last_modified.duration_since(last_modified) < Duration::from_millis(100) { | |
| std::thread::sleep(Duration::from_secs(1)); | |
| continue; | |
| } | |
| println!("Shaders updated!"); | |
| build_shaders(); | |
| last_modified = new_last_modified; | |
| } | |
| }); | |
| } | |
| pub fn get_last_modified(file_name: &str) -> Instant { | |
| let metadata = std::fs::metadata(file_name).unwrap(); | |
| let modified = metadata.modified().unwrap(); | |
| let duration_since = modified.elapsed().unwrap(); | |
| Instant::now() - duration_since | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment