master
Tom Gowan 6 years ago
parent ac7992e139
commit 203f0c8074

@ -1,8 +1,9 @@
use crate::error::CompileError;
use shaderc::{IncludeType, ResolvedInclude};
use shaderc::{ShaderKind, CompileOptions};
use std::fs::File; use std::fs::File;
use std::path::Path;
use shaderc::ShaderKind;
use std::io::Read; use std::io::Read;
use crate::error::CompileError; use std::path::Path;
pub fn compile<T>(path: T, shader_kind: ShaderKind) -> Result<Vec<u32>, CompileError> pub fn compile<T>(path: T, shader_kind: ShaderKind) -> Result<Vec<u32>, CompileError>
where where
@ -10,16 +11,53 @@ where
{ {
// TODO Probably shouldn't create this every time. // TODO Probably shouldn't create this every time.
let mut compiler = shaderc::Compiler::new().ok_or(CompileError::CreateCompiler)?; let mut compiler = shaderc::Compiler::new().ok_or(CompileError::CreateCompiler)?;
let mut options = CompileOptions::new().ok_or(CompileError::CreateCompiler)?;
let mut f = File::open(&path).map_err(CompileError::Open)?; let mut f = File::open(&path).map_err(CompileError::Open)?;
let mut src = String::new(); let mut src = String::new();
f.read_to_string(&mut src).map_err(CompileError::Open)?; f.read_to_string(&mut src).map_err(CompileError::Open)?;
let result = compiler.compile_into_spirv( options.set_include_callback(|path, include_type, folder_path, depth| {
get_include(path, include_type, folder_path, depth)
});
let result = compiler
.compile_into_spirv(
src.as_str(), src.as_str(),
shader_kind, shader_kind,
path.as_ref().to_str().ok_or(CompileError::InvalidPath)?, path.as_ref().to_str().ok_or(CompileError::InvalidPath)?,
"main", "main",
None, Some(&options),
).map_err(CompileError::Compile)?; )
.map_err(CompileError::Compile)?;
let data = result.as_binary(); let data = result.as_binary();
Ok(data.to_owned()) Ok(data.to_owned())
} }
fn get_include(
path: &str,
include_type: IncludeType,
_folder_path: &str,
_depth: usize,
) -> Result<ResolvedInclude, String> {
match include_type {
IncludeType::Relative => {
let p = Path::new(path);
if !p.is_file() {
return Err("Include doesn't point to file".to_string());
}
let resolved_name = p
.to_str()
.ok_or("Path has invalid characters".to_string())?
.to_owned();
let p = p.canonicalize().map_err(|_|"Failed to parse include path".to_string())?;
let mut content = String::new();
File::open(p)
.map_err(|_|"Couldn't open include directory".to_string())?
.read_to_string(&mut content)
.map_err(|_|"Failed to read included shader".to_string())?;
Ok(ResolvedInclude {
resolved_name,
content,
})
}
IncludeType::Standard => Err("Standard includes are unimplemented".to_string()),
}
}

Loading…
Cancel
Save