mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 14:18:04 +08:00
49 lines
1.2 KiB
Rust
49 lines
1.2 KiB
Rust
use std::collections::HashMap;
|
|
|
|
#[derive(Copy, Clone)]
|
|
pub struct ShaderID {
|
|
pub index: usize,
|
|
}
|
|
|
|
pub struct ShaderCache {
|
|
pub shaders: Vec<wgpu::ShaderModule>,
|
|
pub path_to_id: HashMap<String, ShaderID>,
|
|
}
|
|
|
|
impl ShaderCache {
|
|
pub fn new() -> Self {
|
|
let shaders = Vec::new();
|
|
let path_to_id = HashMap::new();
|
|
|
|
Self {
|
|
shaders,
|
|
path_to_id,
|
|
}
|
|
}
|
|
|
|
pub fn get_by_path(&self, path: &str) -> Option<&wgpu::ShaderModule> {
|
|
match self.path_to_id.get(path) {
|
|
Some(id) => self.shaders.get(id.index),
|
|
None => None,
|
|
}
|
|
}
|
|
|
|
// pub fn get_by_id(&self, id: ShaderID) -> Option<&wgpu::ShaderModule> {
|
|
// self.shaders.get(id.index)
|
|
// }
|
|
|
|
pub fn load(&mut self, device: &wgpu::Device, path: &str, shader_type: glsl_to_spirv::ShaderType) -> Result<(), std::io::Error> {
|
|
if self.path_to_id.get(path).is_none() {
|
|
let source = std::fs::read_to_string(path)?;
|
|
let spirv = glsl_to_spirv::compile(&source[..], shader_type).unwrap();
|
|
let compiled = wgpu::read_spirv(spirv).unwrap();
|
|
let shader = device.create_shader_module(&compiled);
|
|
|
|
let length = self.path_to_id.len();
|
|
self.path_to_id.insert(String::from(path), ShaderID { index: length });
|
|
self.shaders.push(shader);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
} |