use naga::FastIndexMap;
use smol_str::SmolStr;
use std::collections::{BTreeMap, HashSet};
use crate::quote_gen::RustSourceItem;
use crate::wgsl::{get_all_vertex_input_structs, VertexInput};
pub struct RawShaderVertexInputs<'a> {
pub containing_module: SmolStr,
pub vertex_inputs: Vec<VertexInput<'a>>,
}
pub struct RawShadersVertexInputs<'a> {
shader_vertex_inputs: FastIndexMap<SmolStr, RawShaderVertexInputs<'a>>,
}
impl<'a> RawShadersVertexInputs<'a> {
pub fn new() -> Self {
Self {
shader_vertex_inputs: FastIndexMap::default(),
}
}
pub fn add(&mut self, shader_vertex_inputs: RawShaderVertexInputs<'a>) {
self
.shader_vertex_inputs
.insert(shader_vertex_inputs.containing_module.clone(), shader_vertex_inputs);
}
pub fn from_module(
mod_name: &str,
naga_module: &'a naga::Module,
) -> RawShaderVertexInputs<'a> {
let vertex_inputs = get_all_vertex_input_structs(mod_name, naga_module);
RawShaderVertexInputs {
containing_module: SmolStr::new(mod_name),
vertex_inputs,
}
}
pub fn generate_vertex_input_impls(self) -> Vec<RustSourceItem> {
let mut unique_vertex_inputs = BTreeMap::new();
for (_, shader) in &self.shader_vertex_inputs {
for vertex_input in &shader.vertex_inputs {
let item_path_key = vertex_input.item_path.get_fully_qualified_name();
unique_vertex_inputs
.entry(item_path_key)
.or_insert(vertex_input);
}
}
unique_vertex_inputs
.into_values()
.map(super::entry::generate_vertex_input_impl)
.collect()
}
}