1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use super::DescriptorSetWriteSet;
use super::ManagedDescriptorSetPool;
use super::{DescriptorSetArc, FrameInFlightIndex};
use crate::resources::resource_lookup::{DescriptorSetLayoutResource, ResourceHash};
use crate::resources::{DynDescriptorSet, ResourceArc};
use fnv::FnvHashMap;
use rafx_api::{RafxDeviceContext, RafxResult};
#[derive(Debug)]
pub struct DescriptorSetPoolMetrics {
pub hash: ResourceHash,
pub allocated_count: usize,
}
#[derive(Debug)]
pub struct DescriptorSetAllocatorMetrics {
pub pools: Vec<DescriptorSetPoolMetrics>,
}
pub struct DescriptorSetAllocator {
device_context: RafxDeviceContext,
pools: FnvHashMap<ResourceHash, ManagedDescriptorSetPool>,
frame_in_flight_index: FrameInFlightIndex,
}
impl DescriptorSetAllocator {
pub fn new(device_context: &RafxDeviceContext) -> Self {
DescriptorSetAllocator {
device_context: device_context.clone(),
pools: Default::default(),
frame_in_flight_index: 0,
}
}
pub fn metrics(&self) -> DescriptorSetAllocatorMetrics {
let mut registered_descriptor_sets_stats = Vec::with_capacity(self.pools.len());
for (hash, value) in &self.pools {
let pool_stats = DescriptorSetPoolMetrics {
hash: *hash,
allocated_count: value.slab.allocated_count(),
};
registered_descriptor_sets_stats.push(pool_stats);
}
DescriptorSetAllocatorMetrics {
pools: registered_descriptor_sets_stats,
}
}
#[profiling::function]
pub fn flush_changes(&mut self) -> RafxResult<()> {
for pool in self.pools.values_mut() {
pool.flush_changes(self.frame_in_flight_index)?;
}
Ok(())
}
#[profiling::function]
pub fn on_frame_complete(&mut self) {
self.frame_in_flight_index =
super::add_to_frame_in_flight_index(self.frame_in_flight_index, 1);
}
pub fn destroy(&mut self) -> RafxResult<()> {
for pool in self.pools.values_mut() {
pool.destroy(&self.device_context)?;
}
self.pools.clear();
Ok(())
}
fn do_create_dyn_descriptor_set(
&mut self,
descriptor_set_layout: &ResourceArc<DescriptorSetLayoutResource>,
write_set: DescriptorSetWriteSet,
) -> RafxResult<DynDescriptorSet> {
let descriptor_set =
self.create_descriptor_set_with_writes(descriptor_set_layout, write_set.clone())?;
let dyn_descriptor_set =
DynDescriptorSet::new(descriptor_set_layout, descriptor_set, write_set);
Ok(dyn_descriptor_set)
}
pub fn create_descriptor_set_with_writes(
&mut self,
descriptor_set_layout: &ResourceArc<DescriptorSetLayoutResource>,
write_set: DescriptorSetWriteSet,
) -> RafxResult<DescriptorSetArc> {
let hash = descriptor_set_layout.get_hash().into();
let device_context = self.device_context.clone();
let pool = self.pools.entry(hash).or_insert_with(|| {
ManagedDescriptorSetPool::new(&device_context, descriptor_set_layout.clone())
});
pool.insert(&self.device_context, write_set)
}
pub fn create_descriptor_set<'a, T: DescriptorSetInitializer<'a>>(
&mut self,
descriptor_set_layout: &ResourceArc<DescriptorSetLayoutResource>,
args: T,
) -> RafxResult<DescriptorSetArc> {
let descriptor_set = self.create_dyn_descriptor_set_uninitialized(descriptor_set_layout)?;
T::create_descriptor_set(self, descriptor_set, args)
}
pub fn create_dyn_descriptor_set_uninitialized(
&mut self,
descriptor_set_layout: &ResourceArc<DescriptorSetLayoutResource>,
) -> RafxResult<DynDescriptorSet> {
let write_set = super::create_uninitialized_write_set_for_layout(
&descriptor_set_layout.get_raw().descriptor_set_layout_def,
);
self.do_create_dyn_descriptor_set(descriptor_set_layout, write_set)
}
pub fn create_dyn_descriptor_set<'a, T: DescriptorSetInitializer<'a>>(
&mut self,
descriptor_set_layout: &ResourceArc<DescriptorSetLayoutResource>,
args: T,
) -> RafxResult<<T as DescriptorSetInitializer<'a>>::Output> {
Ok(T::create_dyn_descriptor_set(
self.create_dyn_descriptor_set_uninitialized(descriptor_set_layout)?,
args,
))
}
}
pub trait DescriptorSetInitializer<'a> {
type Output;
fn create_dyn_descriptor_set(
descriptor_set: DynDescriptorSet,
args: Self,
) -> Self::Output;
fn create_descriptor_set(
descriptor_set_allocator: &mut DescriptorSetAllocator,
descriptor_set: DynDescriptorSet,
args: Self,
) -> RafxResult<DescriptorSetArc>;
}