Skip to content

Commit

Permalink
feat: shader compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
sylv256 committed Sep 4, 2024
1 parent ee1bc86 commit e60ef83
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@

# MacOS
.DS_Store

# SPIR-V
*.spv
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.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ sigill-derive = { path = "sigill-derive" }
ash = "0.38"
ash-window = "0.13.0"
winit = "0.30"

[build-dependencies]
# Rendering
shaderc = "0.8.3"

# Housekeeping
anyhow = "1"
9 changes: 9 additions & 0 deletions assets/shader/triangle.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#version 450

layout(location = 0) in vec3 frag_color;

layout(location = 0) out vec4 out_color;

void main() {
out_color = vec4(frag_color, 1.0);
}
20 changes: 20 additions & 0 deletions assets/shader/triangle.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#version 450

layout(location = 0) out vec3 frag_color;

vec2 positions[3] = vec2[](
vec2(0.0, -0.5),
vec2(0.5, 0.5),
vec2(-0.5, 0.5)
);

vec3 colors[3] = vec3[](
vec3(1.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, 1.0)
);

void main() {
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
frag_color = colors[gl_VertexIndex];
}
64 changes: 64 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::{fs::{self, DirEntry}, path::Path};

pub fn main() -> anyhow::Result<()> {
let compiler = shaderc::Compiler::new().unwrap();
let options = shaderc::CompileOptions::new().unwrap();
let shader_files = recurse_dir("./assets/shader")?;

for file in shader_files {
let path = file.path();
let source = fs::read_to_string(path.clone())?;
let file_name = path.to_string_lossy().to_string();
let extension = file_name.split(".").last().expect("shader files must have an extension");
let shader_binary = compiler.compile_into_spirv(
&source,
extension_to_shader_kind(extension),
&file_name,
"main",
Some(&options),
)?;
fs::write(Path::new(format!("{}_{extension}.spv", path.with_extension("").to_string_lossy().to_string()).as_str()), shader_binary.as_binary_u8())?;
}

Ok(())
}

fn extension_to_shader_kind(extension: &str) -> shaderc::ShaderKind {
match extension {
"frag" => shaderc::ShaderKind::Fragment,
"vert" => shaderc::ShaderKind::Vertex,
_ => panic!("unsupported shader kind"),
}
}

fn recurse_dir(path: impl AsRef<Path>) -> std::io::Result<Vec<DirEntry>> {
let mut entries = Vec::new();
let dir = fs::read_dir(path)?;

dir
.into_iter()
// Fallibly extract entry metadata
.map(|entry| {
let entry = entry?;
Ok((entry.metadata()?, entry))
})
.collect::<std::io::Result<Vec<_>>>()?
.into_iter()
// Add files to entries and discard them
.filter_map(|(metadata, entry)| {
if metadata.is_file() {
entries.push(entry);
return None
}
metadata.is_dir().then_some(entry)
})
.collect::<Vec<_>>()
.iter()
// Recurse child directories
.try_for_each(|entry| {
entries.extend(recurse_dir(entry.path())?);
Ok::<(), std::io::Error>(())
})?;

Ok(entries)
}
2 changes: 2 additions & 0 deletions src/client/rendering/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ use winit::raw_window_handle::{RawDisplayHandle, RawWindowHandle};
use super::RenderResult;

pub use swapchain::*;
pub use pipeline::*;

pub mod swapchain;
pub mod pipeline;

pub type QueueFamilyIndex = u32;
pub type QueueIndex = u32;
Expand Down
4 changes: 4 additions & 0 deletions src/client/rendering/vulkan/pipeline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! # Graphics Pipeline
//! An interface with the graphics pipeline.
pub struct Pipeline {}

0 comments on commit e60ef83

Please sign in to comment.