38 lines
1.7 KiB
Rust
38 lines
1.7 KiB
Rust
//! # Material Module
|
|
//!
|
|
//! Defines `Material`, a lightweight appearance descriptor that pairs a shader identifier with
|
|
//! a shared RenderPipeline. Materials are created via PipelineCache to ensure pipeline reuse—
|
|
//! multiple materials referencing the same shader_id point to the identical compiled GPU pipeline.
|
|
|
|
use crate::pipeline_cache::PipelineCache;
|
|
use std::sync::Arc;
|
|
|
|
/// Lightweight appearance descriptor: links a shader ID to a shared RenderPipeline.
|
|
/// Does not own the pipeline; holds an Arc for zero-copy sharing across objects using the same shader.
|
|
pub struct Material {
|
|
/// Unique shader identifier used to look up or create a compiled RenderPipeline in PipelineCache.
|
|
pub shader_id: String,
|
|
/// Shared reference to the compiled GPU render pipeline. Multiple Materials can share one through Arc cloning.
|
|
pub pipeline: Arc<wgpu::RenderPipeline>,
|
|
}
|
|
|
|
impl Material {
|
|
/// Creates a new Material by requesting the cache to provide (or create) its RenderPipeline.
|
|
/// Inputs: device (GPU command source for pipeline creation), format (surface texture format),
|
|
/// shader_id (unique key into PipelineCache), cache (mutable ref for potential insertion).
|
|
/// Returns a Material holding the Arc-wrapped pipeline. Called at scene initialization time.
|
|
pub fn new(
|
|
device: &wgpu::Device,
|
|
format: wgpu::TextureFormat,
|
|
shader_id: &str,
|
|
cache: &mut PipelineCache,
|
|
) -> Self {
|
|
// Request pipeline from cache — returns cached instance if already exists, creates new otherwise
|
|
let pipeline = cache.get_or_create(device, format, shader_id);
|
|
Self {
|
|
shader_id: shader_id.to_string(),
|
|
pipeline,
|
|
}
|
|
}
|
|
}
|