vertex +normals

This commit is contained in:
Jérôme Bousquié
2026-07-06 12:31:35 +02:00
parent 6c94fc96d6
commit 47851b8f61
3 changed files with 108 additions and 12 deletions
+37 -4
View File
@@ -24,6 +24,7 @@ use std::sync::Arc;
pub struct PipelineCache {
/// Cached pipelines keyed by their shader identifier string.
pipelines: HashMap<String, Arc<wgpu::RenderPipeline>>,
shader_paths: HashMap<String, String>,
}
impl PipelineCache {
@@ -31,8 +32,31 @@ impl PipelineCache {
pub fn new() -> Self {
Self {
pipelines: HashMap::new(),
shader_paths: HashMap::new(),
}
}
/// Enregistre un chemin de shader associé à un ID.
/// Renvoie Ok(id) si réussi, ou une erreur si l'ID existe déjà.
pub fn register_shader(&mut self, id: &str, path: &str) -> Result<String, String> {
if self.shader_paths.contains_key(id) {
return Err(format!("ID '{}' already exists.", id));
}
self.shader_paths.insert(id.to_string(), path.to_string());
Ok(id.to_string())
}
/// Supprime un ID et son chemin associé.
/// Renvoie Ok(id) si réussi, ou une erreur si l'ID est inconnu.
pub fn unregister_shader(&mut self, id: &str) -> Result<String, String> {
if self.shader_paths.remove(id).is_none() {
return Err(format!("ID '{}' does not exist.", id));
}
// Optionnel : tu pourrais aussi supprimer le pipeline compilé du cache
// si tu veux libérer la mémoire GPU immédiatement :
self.pipelines.remove(id);
Ok(id.to_string())
}
/// Retrieves a cached RenderPipeline by shader_id, or creates one on-demand if not present.
/// Inputs: device (GPU command source), format (surface texture format for fragment output),
@@ -50,8 +74,12 @@ impl PipelineCache {
}
// Step 2: Compile a new pipeline — loads shader and builds the GPU render pipeline
// Note: In production you would load shaders specific to shader_id from files or embedded resources.
let shader = self.load_shader(device, shader_id);
let path = self
.shader_paths
.get(shader_id)
.map(|s| s.as_str())
.unwrap_or(shader_id);
let shader = self.load_shader(device, path);
let pipeline = Self::build_pipeline(device, format, &shader);
// Step 3: Cache the new pipeline behind Arc and return it
@@ -97,11 +125,16 @@ impl PipelineCache {
wgpu::VertexAttribute {
offset: 12,
shader_location: 1,
format: wgpu::VertexFormat::Float32x3,
}, // normal
wgpu::VertexAttribute {
offset: 24,
shader_location: 2,
format: wgpu::VertexFormat::Float32x2,
}, // uv
wgpu::VertexAttribute {
offset: 20,
shader_location: 2,
offset: 32,
shader_location: 3,
format: wgpu::VertexFormat::Float32x4,
}, // color
],