Skip to content

Commit

Permalink
style: convert to tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
sylv256 committed Dec 27, 2024
1 parent dd36420 commit 0a0eb26
Show file tree
Hide file tree
Showing 21 changed files with 1,689 additions and 1,688 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_style = tab
indent_size = 4
trim_trailing_whitespace = true
2 changes: 1 addition & 1 deletion assets/shader/triangle.frag
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ layout(location = 0) in vec3 frag_color;
layout(location = 0) out vec4 out_color;

void main() {
out_color = vec4(frag_color, 1.0);
out_color = vec4(frag_color, 1.0);
}
16 changes: 8 additions & 8 deletions assets/shader/triangle.vert
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
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)
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)
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];
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
frag_color = colors[gl_VertexIndex];
}
162 changes: 81 additions & 81 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,98 +2,98 @@ use std::{fs::{self, DirEntry}, path::Path};

#[allow(unused)]
macro_rules! p {
($($tokens: tt)*) => {
println!("cargo:warning={}", format!($($tokens)*))
}
($($tokens: tt)*) => {
println!("cargo:warning={}", format!($($tokens)*))
}
}

pub fn main() -> anyhow::Result<()> {
let compiler = shaderc::Compiler::new().unwrap();
let mut options = shaderc::CompileOptions::new().unwrap();
options.set_include_callback(|requested, include_type, source, include_depth| {
if include_depth > 127 {
return shaderc::IncludeCallbackResult::Err(format!("Maximum include depth reached in {source} including {requested}! Check for recursive include directives."))
}
if include_type == shaderc::IncludeType::Standard {
return shaderc::IncludeCallbackResult::Err(format!("Cannot find requested {requested} from {source}!"))
}
let source = fs::read_to_string(format!("{source}/../{requested}")).expect(format!("Failed to find {requested} from {source}").as_str()).to_string();
Ok(
shaderc::ResolvedInclude {
resolved_name: requested.to_string(),
content: source,
}
)
});
let shader_files = recurse_dir("./assets/shader")?;
let compiler = shaderc::Compiler::new().unwrap();
let mut options = shaderc::CompileOptions::new().unwrap();
options.set_include_callback(|requested, include_type, source, include_depth| {
if include_depth > 127 {
return shaderc::IncludeCallbackResult::Err(format!("Maximum include depth reached in {source} including {requested}! Check for recursive include directives."))
}
if include_type == shaderc::IncludeType::Standard {
return shaderc::IncludeCallbackResult::Err(format!("Cannot find requested {requested} from {source}!"))
}
let source = fs::read_to_string(format!("{source}/../{requested}")).expect(format!("Failed to find {requested} from {source}").as_str()).to_string();
Ok(
shaderc::ResolvedInclude {
resolved_name: requested.to_string(),
content: source,
}
)
});
let shader_files = recurse_dir("./assets/shader")?;

for file in shader_files {
let path = file.path();
if let Some(file_name) = path.file_name() {
if file_name.to_string_lossy().to_string().ends_with(".spv") {
continue;
}
}
let source = fs::read_to_string(path.clone())?;
let file_name = path.to_string_lossy().to_string();
let extension = file_name.split(".").last();
if extension.is_none() {
continue;
}
let shader_kind = extension_to_shader_kind(extension.unwrap());
if shader_kind.is_none() {
continue;
}
let shader_binary = compiler.compile_into_spirv(
&source,
shader_kind.unwrap(),
&file_name,
"main",
Some(&options),
)?;
let target_path = &format!("{}_{}.spv", path.with_extension("").to_string_lossy().to_string(), extension.unwrap());
fs::write(Path::new(target_path.as_str()), shader_binary.as_binary_u8())?;
}
for file in shader_files {
let path = file.path();
if let Some(file_name) = path.file_name() {
if file_name.to_string_lossy().to_string().ends_with(".spv") {
continue;
}
}
let source = fs::read_to_string(path.clone())?;
let file_name = path.to_string_lossy().to_string();
let extension = file_name.split(".").last();
if extension.is_none() {
continue;
}
let shader_kind = extension_to_shader_kind(extension.unwrap());
if shader_kind.is_none() {
continue;
}
let shader_binary = compiler.compile_into_spirv(
&source,
shader_kind.unwrap(),
&file_name,
"main",
Some(&options),
)?;
let target_path = &format!("{}_{}.spv", path.with_extension("").to_string_lossy().to_string(), extension.unwrap());
fs::write(Path::new(target_path.as_str()), shader_binary.as_binary_u8())?;
}

Ok(())
Ok(())
}

fn extension_to_shader_kind(extension: &str) -> Option<shaderc::ShaderKind> {
match extension {
"frag" => Some(shaderc::ShaderKind::Fragment),
"vert" => Some(shaderc::ShaderKind::Vertex),
_ => None,
}
match extension {
"frag" => Some(shaderc::ShaderKind::Fragment),
"vert" => Some(shaderc::ShaderKind::Vertex),
_ => None,
}
}

fn recurse_dir(path: impl AsRef<Path>) -> std::io::Result<Vec<DirEntry>> {
let mut entries = Vec::new();
let dir = fs::read_dir(path)?;
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>(())
})?;
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)
Ok(entries)
}
Loading

0 comments on commit 0a0eb26

Please sign in to comment.