diff --git a/shaders/compile.bat b/shaders/compile.bat deleted file mode 100644 index 2c88e69..0000000 --- a/shaders/compile.bat +++ /dev/null @@ -1,2 +0,0 @@ -for %%i in (*.frag) do glslangValidator -V %%i -o %%i.spv -for %%i in (*.vert) do glslangValidator -V %%i -o %%i.spv \ No newline at end of file diff --git a/shaders/compile.ps1 b/shaders/compile.ps1 deleted file mode 100644 index 1d22125..0000000 --- a/shaders/compile.ps1 +++ /dev/null @@ -1,33 +0,0 @@ -$compileHashFile = "compilehashes.xml" - -if (Get-ChildItem -File $compileHashFile -ErrorAction SilentlyContinue) { - $hashes = Import-Clixml $compileHashFile -} else { - $hashes = @{} -} - -function CompileShader($fileName) { - $spvFileName = "$fileName.spv" - $newHash = Get-FileHash -Path $fileName -Algorithm SHA1 - - $spvExists = $false - if (Get-ChildItem -File $spvFileName -ErrorAction SilentlyContinue) { - $spvExists = $true - } - - if ($spvExists -and $hashes.ContainsKey($fileName) -and $hashes[$fileName] -eq $newHash.Hash) { - Write-Output "Skipping $fileName" - } else { - glslangValidator -V $fileName -o $spvFileName - $hashes.$fileName = $newHash.Hash - } -} - -Get-ChildItem -Filter *.vert | ForEach-Object { - CompileShader $_.Name -} -Get-ChildItem -Filter *.frag | ForEach-Object { - CompileShader $_.Name -} - -Export-Clixml -Path $compileHashFile -InputObject $hashes diff --git a/shaders/triangle.frag b/shaders/triangle.frag index 2290422..34a17d2 100644 --- a/shaders/triangle.frag +++ b/shaders/triangle.frag @@ -26,7 +26,7 @@ void main() { normal_cam_u = normalize(tbn * normal_cam_u); // vec3 light_direction_cam_u = normalize(ubo.light_position - position_wld); - vec3 light_direction_cam_u = normalize(vec3(sin(ubo.time), 1.0, cos(ubo.time))); + vec3 light_direction_cam_u = normalize(vec3(1.0, 1.0, 1.0)); float ambient_strength = 0.1; vec3 light_color = vec3(1.0, 1.0, 1.0); diff --git a/shaders/triangle.frag.spv b/shaders/triangle.frag.spv index 8b419f3..b312f52 100644 Binary files a/shaders/triangle.frag.spv and b/shaders/triangle.frag.spv differ diff --git a/src/vulkan/pipelines.rs b/src/vulkan/pipelines.rs index 2fd00c1..23f04a9 100644 --- a/src/vulkan/pipelines.rs +++ b/src/vulkan/pipelines.rs @@ -1,4 +1,4 @@ -use std::{convert::TryInto, io::{Read, Write}, sync::Arc}; +use std::{convert::TryInto, io::{self, ErrorKind, Read, Write}, path::PathBuf, sync::Arc}; use vulkano::{command_buffer::AutoCommandBufferBuilder, descriptor::{descriptor::ShaderStages, descriptor_set::PersistentDescriptorSet}, pipeline::shader::ShaderModule}; use vulkano::command_buffer::DynamicState; @@ -53,6 +53,38 @@ fn shader_module_from_file(device: Arc, path: &str) -> Arc } } +fn matches_extension(path: &PathBuf, extensions: &Vec<&str>) -> bool { + if let Some(Some(path_extension)) = path.extension().map(|e| e.to_str()) { + for extension in extensions { + if *extension == path_extension { return true; } + } + } + return false; +} + +fn compile_shaders() -> io::Result<()> { + for file_maybe in std::fs::read_dir("./shaders")? { + let path = file_maybe?.path(); + if !path.is_dir() && matches_extension(&path, &vec!["frag", "vert"]) { + let mut target_path = path.to_str().ok_or(ErrorKind::Other)?.to_string(); + target_path.push_str(".spv"); + + let output = std::process::Command::new("glslangValidator") + .arg("-V") + .arg(path.to_str().ok_or(ErrorKind::Other)?) + .arg("-o") + .arg(target_path) + .output().unwrap(); + std::io::stdout().write_all(&output.stdout)?; + + if !output.status.success() { + eprintln!("Shader compiler {:?}", output.status); + } + } + } + Ok(()) +} + impl DefaultShader { pub fn new(device: Arc, render_pass: RP) -> Self { DefaultShader { @@ -66,12 +98,7 @@ impl DefaultShader { #[cfg(debug_assertions)] { println!("Compiling shaders..."); - let output = std::process::Command::new("cmd.exe") - .arg("/c") - .arg("compile.bat") - .current_dir("shaders") - .output().unwrap(); - std::io::stdout().write_all(&output.stdout).unwrap(); + compile_shaders().unwrap(); } unsafe {