use ahash::RandomState;
use std::fmt::Debug;
use std::sync::Arc;
use educe::Educe;
use vulkano::descriptor_set::WriteDescriptorSet;
use vulkano::image::view::ImageView;
use crate::render::descriptor_set::VKDescriptorSource;
use crate::render::frame::FrameManager;
#[derive(Debug)]
pub enum AttachmentType {
Transient(Arc<ImageView>),
Output,
}
impl AttachmentType {
pub fn unwrap_transient(self) -> Arc<ImageView> {
match self {
Self::Transient(image_view) => image_view,
_ => panic!("attachment was not transient"),
}
}
}
impl Clone for AttachmentType {
fn clone(&self) -> Self {
match self {
AttachmentType::Transient(image)
=> AttachmentType::Transient(image.clone()),
AttachmentType::Output => AttachmentType::Output,
}
}
}
#[derive(Educe)]
#[educe(Debug)]
pub struct AttachmentDescriptor {
frames: Arc<dyn FrameManager>,
input_name: String,
#[educe(Debug(ignore))]
random_state: RandomState,
}
impl AttachmentDescriptor {
pub fn new(
frames: Arc<dyn FrameManager>,
input_name: impl Into<String>,
) -> Self {
Self {
frames,
input_name: input_name.into(),
random_state: RandomState::new(),
}
}
}
impl VKDescriptorSource for AttachmentDescriptor {
fn write_descriptor(&self, frame_idx: usize, binding: u32) -> (WriteDescriptorSet, u64) {
let image_view = self.frames
.framebuffer(frame_idx).unwrap()
.get_attachment(self.input_name.clone())
.expect(&format!("No attachment '{}'", &self.input_name))
.unwrap_transient();
(
WriteDescriptorSet::image_view(binding, image_view.clone()),
self.random_state.hash_one(image_view),
)
}
fn update_descriptor_source(&self, frame_idx: usize) -> u64 {
let image_view = self.frames
.framebuffer(frame_idx).unwrap()
.get_attachment(self.input_name.clone()).unwrap()
.unwrap_transient();
self.random_state.hash_one(image_view)
}
}