bevy/src/render/pipeline/bind_group.rs

63 lines
1.5 KiB
Rust
Raw Normal View History

use super::BindingDescriptor;
2020-03-10 06:08:09 +00:00
use std::{
collections::{hash_map::DefaultHasher, BTreeSet},
hash::{Hash, Hasher},
};
#[derive(Clone, Debug)]
pub struct BindGroupDescriptor {
2020-03-10 06:08:09 +00:00
pub index: u32,
pub bindings: BTreeSet<BindingDescriptor>,
hash: Option<BindGroupDescriptorId>,
2020-03-10 06:08:09 +00:00
}
#[derive(Hash, Copy, Clone, Eq, PartialEq, Debug)]
pub struct BindGroupDescriptorId(u64);
impl BindGroupDescriptor {
pub fn new(index: u32, bindings: Vec<BindingDescriptor>) -> Self {
2020-03-26 02:20:52 +00:00
let mut descriptor = BindGroupDescriptor {
2020-03-10 06:08:09 +00:00
index,
bindings: bindings.iter().cloned().collect(),
hash: None,
2020-03-26 02:20:52 +00:00
};
// TODO: remove all instances of get_or_update_id
descriptor.update_id();
descriptor
2020-03-10 06:08:09 +00:00
}
pub fn get_id(&self) -> Option<BindGroupDescriptorId> {
2020-03-10 06:08:09 +00:00
self.hash
}
pub fn get_or_update_id(&mut self) -> BindGroupDescriptorId {
2020-03-10 06:08:09 +00:00
if self.hash.is_none() {
self.update_id();
2020-03-10 06:08:09 +00:00
}
self.hash.unwrap()
}
pub fn update_id(&mut self) {
2020-03-10 06:08:09 +00:00
let mut hasher = DefaultHasher::new();
self.hash(&mut hasher);
self.hash = Some(BindGroupDescriptorId(hasher.finish()));
2020-03-10 06:08:09 +00:00
}
}
impl Hash for BindGroupDescriptor {
2020-03-10 06:08:09 +00:00
fn hash<H: Hasher>(&self, state: &mut H) {
self.index.hash(state);
self.bindings.hash(state);
}
}
impl PartialEq for BindGroupDescriptor {
fn eq(&self, other: &BindGroupDescriptor) -> bool {
2020-03-10 06:08:09 +00:00
self.index == other.index && self.bindings == other.bindings
}
}
impl Eq for BindGroupDescriptor {}