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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use crate::{
DescriptorSetLayout, DescriptorSetLayoutBinding, MaterialPassVertexInput, ShaderModuleHash,
};
use fnv::{FnvHashMap, FnvHashSet};
use rafx_api::{
RafxResult, RafxSamplerDef, RafxShaderPackage, RafxShaderResource, RafxShaderStageFlags,
RafxShaderStageReflection,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct SlotLocation {
pub layout_index: u32,
pub binding_index: u32,
}
pub type SlotNameLookup = FnvHashMap<String, FnvHashSet<SlotLocation>>;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub struct ReflectedDescriptorSetLayoutBinding {
pub resource: RafxShaderResource,
pub immutable_samplers: Option<Vec<RafxSamplerDef>>,
pub internal_buffer_per_descriptor_size: Option<u32>,
}
impl Into<DescriptorSetLayoutBinding> for ReflectedDescriptorSetLayoutBinding {
fn into(self) -> DescriptorSetLayoutBinding {
DescriptorSetLayoutBinding {
resource: self.resource.clone(),
immutable_samplers: self.immutable_samplers.clone(),
internal_buffer_per_descriptor_size: self.internal_buffer_per_descriptor_size,
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ReflectedDescriptorSetLayout {
pub bindings: Vec<ReflectedDescriptorSetLayoutBinding>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ReflectedVertexInput {
pub name: String,
pub semantic: String,
pub location: u32,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ReflectedEntryPoint {
pub rafx_api_reflection: RafxShaderStageReflection,
pub descriptor_set_layouts: Vec<Option<ReflectedDescriptorSetLayout>>,
pub vertex_inputs: Vec<ReflectedVertexInput>,
}
#[derive(Serialize, Deserialize)]
pub struct CookedShaderPackage {
pub hash: ShaderModuleHash,
pub shader_package: RafxShaderPackage,
pub entry_points: Vec<ReflectedEntryPoint>,
}
impl CookedShaderPackage {
pub fn find_entry_point(
&self,
entry_point_name: &str,
) -> Option<&ReflectedEntryPoint> {
self.entry_points
.iter()
.find(|x| x.rafx_api_reflection.entry_point_name == entry_point_name)
}
}
pub struct ReflectedShader {
pub descriptor_set_layout_defs: Vec<DescriptorSetLayout>,
pub slot_name_lookup: SlotNameLookup,
pub vertex_inputs: Option<Arc<Vec<MaterialPassVertexInput>>>,
}
impl ReflectedShader {
pub fn new(entry_points: &[&ReflectedEntryPoint]) -> RafxResult<ReflectedShader> {
let mut descriptor_set_layout_defs = Vec::default();
let mut slot_name_lookup: SlotNameLookup = Default::default();
let mut vertex_inputs = None;
for reflection_data in entry_points {
log::trace!(" Reflection data:\n{:#?}", reflection_data);
if reflection_data
.rafx_api_reflection
.shader_stage
.intersects(RafxShaderStageFlags::VERTEX)
{
let inputs: Vec<_> = reflection_data
.vertex_inputs
.iter()
.map(|x| MaterialPassVertexInput {
semantic: x.semantic.clone(),
location: x.location,
})
.collect();
assert!(vertex_inputs.is_none());
vertex_inputs = Some(Arc::new(inputs));
}
for (set_index, layout) in reflection_data.descriptor_set_layouts.iter().enumerate() {
while descriptor_set_layout_defs.len() <= set_index {
descriptor_set_layout_defs.push(DescriptorSetLayout::default());
}
if let Some(layout) = layout.as_ref() {
for binding in &layout.bindings {
let existing_binding = descriptor_set_layout_defs[set_index]
.bindings
.iter_mut()
.find(|x| x.resource.binding == binding.resource.binding);
if let Some(existing_binding) = existing_binding {
if existing_binding.resource.resource_type
!= binding.resource.resource_type
{
let error = format!(
"Load Material Failed - Pass is using shaders in different stages with different descriptor types for set={} binding={}",
set_index,
binding.resource.binding
);
log::error!("{}", error);
return Err(error)?;
}
if existing_binding.resource.element_count_normalized()
!= binding.resource.element_count_normalized()
{
let error = format!(
"Load Material Failed - Pass is using shaders in different stages with different descriptor counts for set={} binding={}",
set_index,
binding.resource.binding
);
log::error!("{}", error);
return Err(error)?;
}
if existing_binding.immutable_samplers != binding.immutable_samplers {
let error = format!(
"Load Material Failed - Pass is using shaders in different stages with different immutable samplers for set={} binding={}",
set_index,
binding.resource.binding
);
log::error!("{}", error);
return Err(error)?;
}
if existing_binding.internal_buffer_per_descriptor_size
!= binding.internal_buffer_per_descriptor_size
{
let error = format!(
"Load Material Failed - Pass is using shaders in different stages with different internal buffer configuration for set={} binding={}",
set_index,
binding.resource.binding
);
log::error!("{}", error);
return Err(error)?;
}
log::trace!(" Descriptor for binding set={} binding={} already exists, adding stage {:?}", set_index, binding.resource.binding, binding.resource.used_in_shader_stages);
existing_binding.resource.used_in_shader_stages |=
binding.resource.used_in_shader_stages;
} else {
log::trace!(
" Add descriptor binding set={} binding={} for stage {:?}",
set_index,
binding.resource.binding,
binding.resource.used_in_shader_stages
);
let def = binding.clone().into();
descriptor_set_layout_defs[set_index].bindings.push(def);
}
if let Some(slot_name) = &binding.resource.name {
log::trace!(
" Assign slot name '{}' to binding set={} binding={}",
slot_name,
set_index,
binding.resource.binding
);
slot_name_lookup
.entry(slot_name.clone())
.or_default()
.insert(SlotLocation {
layout_index: set_index as u32,
binding_index: binding.resource.binding,
});
}
}
}
}
}
Ok(ReflectedShader {
vertex_inputs,
descriptor_set_layout_defs,
slot_name_lookup,
})
}
}