Skip to content

Commit

Permalink
Automate shader compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyas-taouaou committed Oct 30, 2024
1 parent 68601d2 commit ebfe411
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 8 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/target
/.idea
engine/target
/.idea
46 changes: 46 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions engine/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
res
47 changes: 47 additions & 0 deletions engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ nalgebra = { version = "0.33.1", features = ["bytemuck"] }
tracing = "0.1.40"
winit = "0.30.5"
anyhow = "1.0.91"
gpu-allocator = { version = "0.27.0", default-features = false, features = ["vulkan"] }
gpu-allocator = { version = "0.27.0", default-features = false, features = ["vulkan"] }

[build-dependencies]
shaderc = "0.8.3"
anyhow = "1.0.91"
49 changes: 49 additions & 0 deletions engine/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
fn main() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed=devres");
println!("cargo:rerun-if-changed=res");

let compiler = shaderc::Compiler::new().unwrap();
let mut options = shaderc::CompileOptions::new().unwrap();
options.set_target_env(
shaderc::TargetEnv::Vulkan,
shaderc::EnvVersion::Vulkan1_3 as u32,
);
options.set_source_language(shaderc::SourceLanguage::GLSL);

let is_debug_build = std::env::var("OPT_LEVEL")? == "0";

if is_debug_build {
options.set_optimization_level(shaderc::OptimizationLevel::Zero);
options.set_generate_debug_info();
} else {
options.set_optimization_level(shaderc::OptimizationLevel::Performance);
}

std::fs::create_dir_all("res/shaders")?;

for entry in std::fs::read_dir("devres/shaders")? {
let entry = entry?;
let path = entry.path();
let extension = path.extension().unwrap().to_str().unwrap();
let file_name = path.file_name().unwrap().to_str().unwrap();
let shader_kind = match extension {
"vert" => shaderc::ShaderKind::Vertex,
"frag" => shaderc::ShaderKind::Fragment,
"comp" => shaderc::ShaderKind::Compute,
"geom" => shaderc::ShaderKind::Geometry,
"tesc" => shaderc::ShaderKind::TessControl,
"tese" => shaderc::ShaderKind::TessEvaluation,
_ => continue,
};

let source = std::fs::read_to_string(&path)?;
let binary_result =
compiler.compile_into_spirv(&source, shader_kind, file_name, "main", Some(&options))?;

let binary = binary_result.as_binary_u8();
let output_path = format!("res/shaders/{}.spv", file_name);
std::fs::write(output_path, binary)?;
}

Ok(())
}
File renamed without changes.
File renamed without changes.
16 changes: 11 additions & 5 deletions engine/src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use ash::vk;
use geometry::Geometry;
use gpu_allocator::vulkan::{AllocationScheme, Allocator};
use gpu_allocator::MemoryLocation;
use std::path::Path;
use std::sync::Arc;
use std::time::Instant;

Expand All @@ -37,10 +38,13 @@ pub struct Renderer {
instances: Vec<Instance>,
}

const SHADERS_DIR: &str = "res/shaders/";
const SHADERS_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/res/shaders/");

fn load_shader_module(context: &RenderingContext, path: &str) -> Result<vk::ShaderModule> {
let code = std::fs::read(format!("{}{}", SHADERS_DIR, path))?;
fn load_shader_module(
context: &RenderingContext,
path: impl AsRef<Path>,
) -> Result<vk::ShaderModule> {
let code = std::fs::read(path)?;
context.create_shader_module(&code)
}

Expand Down Expand Up @@ -115,8 +119,10 @@ impl Renderer {
commands: &Commands,
attributes: RendererAttributes,
) -> Result<Self> {
let vertex_shader = load_shader_module(context.as_ref(), "vert.spv")?;
let fragment_shader = load_shader_module(context.as_ref(), "frag.spv")?;
let vertex_shader =
load_shader_module(context.as_ref(), SHADERS_DIR.to_owned() + "shader.vert.spv")?;
let fragment_shader =
load_shader_module(context.as_ref(), SHADERS_DIR.to_owned() + "shader.frag.spv")?;

let mut allocator = context.create_allocator(Default::default(), Default::default())?;

Expand Down
Binary file removed res/shaders/frag.spv
Binary file not shown.
Binary file removed res/shaders/vert.spv
Binary file not shown.

0 comments on commit ebfe411

Please sign in to comment.