Create build file for shaders

This commit is contained in:
hodasemi 2024-03-25 07:12:51 +01:00
parent e87b6a4ec6
commit 13aa2ba04d
13 changed files with 88 additions and 1 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
Cargo.lock Cargo.lock
target/ target/
*.spv

53
build.rs Normal file
View file

@ -0,0 +1,53 @@
use std::{fs, path::Path, process::Command};
const FILE_ENDINGS: &'static [&'static str] = &[
"vert", "frag", "geom", "comp", "rchit", "rmiss", "rgen", "rahit",
];
fn find_shader_files(path: impl AsRef<Path>) -> Vec<String> {
let mut v = Vec::new();
if !path.as_ref().is_dir() {
panic!("path ({:?}) is not a directory!", path.as_ref());
}
for entry in fs::read_dir(path).unwrap() {
let child_path = entry.unwrap().path();
if child_path.is_dir() {
v.extend(find_shader_files(child_path));
} else if child_path.is_file() {
for ending in FILE_ENDINGS.iter() {
if child_path.extension().unwrap() == *ending {
v.push(child_path.to_str().unwrap().to_string());
break;
}
}
}
}
v
}
fn compile_shader(shader_files: &[String]) {
Command::new("glslangValidator")
.arg("--help")
.output()
.expect("Failed to execute glslangValidator. Maybe you need to install it first?");
for shader in shader_files {
Command::new("glslangValidator")
.arg("-V")
.arg(shader)
.arg("-o")
.arg(&format!("{}.spv", shader))
.output()
.expect(&format!("Failed to compile {}", shader));
}
}
fn main() {
let shader_files = find_shader_files("src");
compile_shader(&shader_files);
}

View file

@ -0,0 +1,12 @@
#version 450
layout (set = 0, binding = 0) uniform Descriptor {
vec3 color;
} descriptor;
layout (location = 0) out vec4 out_color;
void main()
{
out_color = vec4(descriptor.color, 1.0);
}

View file

@ -0,0 +1,8 @@
#version 450
layout (location = 0) in vec4 position;
void main()
{
gl_Position = position;
}

View file

@ -0,0 +1,12 @@
#version 450
layout (location = 0) in vec4 position;
layout (location = 1) in vec2 texcoord;
layout (location = 0) out vec2 uv;
void main()
{
uv = texcoord;
gl_Position = position;
}