rafx_framework/resources/descriptor_sets/
mod.rs

1use fnv::FnvHashMap;
2
3mod descriptor_set_arc;
4pub use descriptor_set_arc::DescriptorSetArc;
5
6mod dynamic_descriptor_sets;
7pub use dynamic_descriptor_sets::DescriptorSetBindings;
8pub use dynamic_descriptor_sets::DynDescriptorSet;
9
10mod descriptor_set_pool;
11use descriptor_set_pool::ManagedDescriptorSetPool;
12
13mod descriptor_set_pool_chunk;
14pub use descriptor_set_pool_chunk::DescriptorSetWriter;
15pub use descriptor_set_pool_chunk::DescriptorSetWriterContext;
16use descriptor_set_pool_chunk::ManagedDescriptorSetPoolChunk;
17
18mod descriptor_set_buffers;
19use descriptor_set_buffers::DescriptorLayoutBufferSet;
20use descriptor_set_buffers::DescriptorSetPoolRequiredBufferInfo;
21
22mod descriptor_write_set;
23pub use descriptor_write_set::create_uninitialized_write_set_for_layout;
24pub use descriptor_write_set::DescriptorSetBindingKey;
25pub use descriptor_write_set::DescriptorSetElementKey;
26pub use descriptor_write_set::DescriptorSetElementWrite;
27pub use descriptor_write_set::DescriptorSetWriteElementBuffer;
28pub use descriptor_write_set::DescriptorSetWriteElementBufferData;
29pub use descriptor_write_set::DescriptorSetWriteElementBufferDataBufferRef;
30pub use descriptor_write_set::DescriptorSetWriteElementImage;
31pub use descriptor_write_set::DescriptorSetWriteElementImageValue;
32pub use descriptor_write_set::DescriptorSetWriteSet;
33
34mod descriptor_set_allocator;
35pub use descriptor_set_allocator::DescriptorSetAllocator;
36pub use descriptor_set_allocator::DescriptorSetAllocatorMetrics;
37pub use descriptor_set_allocator::DescriptorSetInitializer;
38pub use descriptor_set_allocator::DescriptorSetPoolMetrics;
39
40mod descriptor_set_allocator_manager;
41use crate::{DescriptorSetLayoutResource, ResourceArc};
42pub(super) use descriptor_set_allocator_manager::DescriptorSetAllocatorManager;
43pub use descriptor_set_allocator_manager::DescriptorSetAllocatorProvider;
44pub use descriptor_set_allocator_manager::DescriptorSetAllocatorRef;
45use rafx_api::RafxResourceType;
46
47const MAX_FRAMES_IN_FLIGHT: usize = crate::MAX_FRAMES_IN_FLIGHT;
48const MAX_FRAMES_IN_FLIGHT_PLUS_1: usize = MAX_FRAMES_IN_FLIGHT + 1;
49
50// A set of write to buffers that back a descriptor set
51#[derive(Debug, Default, Clone)]
52pub struct DescriptorSetWriteBuffer {
53    pub elements: FnvHashMap<DescriptorSetElementKey, Vec<u8>>,
54}
55
56// Slab keys to identify descriptors can carry a payload. Anything we'd want to store per descriptor
57// set can go here, but don't have anything yet
58struct ManagedDescriptorSet {
59    //write_set: DescriptorSetWriteSet,
60}
61
62// We need to delay dropping descriptor sets for MAX_FRAMES_IN_FLIGHT frames
63type FrameInFlightIndex = u32;
64
65fn add_to_frame_in_flight_index(
66    index: FrameInFlightIndex,
67    value: u32,
68) -> FrameInFlightIndex {
69    (index + value) % MAX_FRAMES_IN_FLIGHT_PLUS_1 as u32
70}
71
72// fn subtract_from_frame_in_flight_index(
73//     index: FrameInFlightIndex,
74//     value: u32,
75// ) -> FrameInFlightIndex {
76//     (value + MAX_FRAMES_IN_FLIGHT_PLUS_1 as u32 - index) % MAX_FRAMES_IN_FLIGHT_PLUS_1 as u32
77// }
78
79#[derive(Default, Debug)]
80pub struct WhatToBind {
81    pub bind_samplers: bool,
82    pub bind_images: bool,
83    pub bind_buffers: bool,
84}
85
86pub fn what_to_bind(element_write: &DescriptorSetElementWrite) -> WhatToBind {
87    let mut what = WhatToBind::default();
88
89    // See https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkWriteDescriptorSet.html
90    match element_write.descriptor_type {
91        RafxResourceType::SAMPLER => {
92            what.bind_samplers = !element_write.has_immutable_sampler;
93        }
94        RafxResourceType::COMBINED_IMAGE_SAMPLER => {
95            what.bind_samplers = !element_write.has_immutable_sampler;
96            what.bind_images = true;
97        }
98        RafxResourceType::TEXTURE => {
99            what.bind_images = true;
100        }
101        RafxResourceType::TEXTURE_READ_WRITE => {
102            what.bind_images = true;
103        }
104        RafxResourceType::UNIFORM_BUFFER => {
105            what.bind_buffers = true;
106        }
107        RafxResourceType::BUFFER => {
108            what.bind_buffers = true;
109        }
110        RafxResourceType::BUFFER_READ_WRITE => {
111            what.bind_buffers = true;
112        }
113        _ => {
114            unimplemented!(
115                "what_to_bind not implemented for descriptor type {:?}",
116                element_write.descriptor_type
117            );
118        }
119    }
120
121    what
122}
123
124pub fn get_descriptor_set_element_write(
125    descriptor_set_layout: &ResourceArc<DescriptorSetLayoutResource>,
126    key: &DescriptorSetElementKey,
127) -> Option<DescriptorSetElementWrite> {
128    for binding in &descriptor_set_layout
129        .get_raw()
130        .descriptor_set_layout_def
131        .bindings
132    {
133        let element_count = binding.resource.element_count_normalized() as usize;
134        if key.dst_binding != binding.resource.binding || key.array_index >= element_count {
135            continue;
136        }
137
138        return Some(DescriptorSetElementWrite {
139            has_immutable_sampler: binding.immutable_samplers.is_some(),
140            descriptor_type: binding.resource.resource_type,
141            image_info: DescriptorSetWriteElementImage::default(),
142            buffer_info: DescriptorSetWriteElementBuffer::default(),
143        });
144    }
145
146    None
147}