use crate::resources::resource_lookup::{ImageViewResource, SamplerResource};
use crate::resources::ResourceArc;
use crate::BufferResource;
use fnv::FnvHashMap;
use rafx_api::RafxTexture;
use rafx_api::{RafxReflectedDescriptorSetLayout, RafxResourceType};
#[derive(Debug, Clone)]
pub enum DescriptorSetWriteElementImageValue {
Resource(ResourceArc<ImageViewResource>),
}
impl DescriptorSetWriteElementImageValue {
pub fn get_image(&self) -> RafxTexture {
match self {
DescriptorSetWriteElementImageValue::Resource(resource) => {
resource.get_raw().image.get_raw().image.clone()
}
}
}
}
#[derive(Debug, Clone, Default)]
pub struct DescriptorSetWriteElementImage {
pub sampler: Option<ResourceArc<SamplerResource>>,
pub image_view: Option<DescriptorSetWriteElementImageValue>,
}
#[derive(Debug, Clone)]
pub struct DescriptorSetWriteElementBufferDataBufferRef {
pub buffer: ResourceArc<BufferResource>,
pub byte_offset: Option<u64>,
pub size: Option<u64>,
}
#[derive(Debug, Clone)]
pub enum DescriptorSetWriteElementBufferData {
BufferRef(DescriptorSetWriteElementBufferDataBufferRef),
Data(Vec<u8>),
}
#[derive(Debug, Clone, Default)]
pub struct DescriptorSetWriteElementBuffer {
pub buffer: Option<DescriptorSetWriteElementBufferData>,
}
#[derive(Debug, Clone)]
pub struct DescriptorSetElementWrite {
pub descriptor_type: RafxResourceType,
pub image_info: DescriptorSetWriteElementImage,
pub buffer_info: DescriptorSetWriteElementBuffer,
pub has_immutable_sampler: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DescriptorSetElementKey {
pub dst_binding: u32,
pub array_index: usize,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DescriptorSetBindingKey {
pub dst_binding: u32,
}
#[derive(Debug, Default, Clone)]
pub struct DescriptorSetWriteSet {
pub elements: FnvHashMap<DescriptorSetElementKey, DescriptorSetElementWrite>,
}
impl DescriptorSetWriteSet {
pub fn copy_from(
&mut self,
other: &DescriptorSetWriteSet,
) {
for (k, v) in other.elements.iter() {
self.elements.insert(*k, v.clone());
}
}
}
pub fn create_uninitialized_write_set_for_layout(
layout: &RafxReflectedDescriptorSetLayout
) -> DescriptorSetWriteSet {
let mut write_set = DescriptorSetWriteSet::default();
for binding in &layout.bindings {
for array_index in 0..binding.resource.element_count_normalized() {
let element_write = DescriptorSetElementWrite {
has_immutable_sampler: binding.immutable_samplers.is_some(),
descriptor_type: binding.resource.resource_type,
image_info: DescriptorSetWriteElementImage::default(),
buffer_info: DescriptorSetWriteElementBuffer::default(),
};
let key = DescriptorSetElementKey {
dst_binding: binding.resource.binding as u32,
array_index: array_index as usize,
};
write_set.elements.insert(key, element_write);
}
}
write_set
}