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
//! # ash_shader_creator
//!
//! A library for easy to way automatically create multiple shader stages from the directory path.
use std::{
collections::HashMap,
ffi::{c_void, CString},
fs::{read_dir, File},
io::Read,
path::{Path, PathBuf},
ptr,
};
use ash::{
vk::{
AllocationCallbacks, PipelineShaderStageCreateFlags, PipelineShaderStageCreateInfo,
ShaderModule, ShaderModuleCreateFlags, ShaderModuleCreateInfo, ShaderStageFlags,
SpecializationInfo, StructureType,
},
Device,
};
pub struct ShaderStage<'a> {
pub device: &'a Device,
pub dir_path: &'a Path,
pub shader_flags: ShaderModuleCreateFlags,
pub shader_p_next: *const c_void,
pub main_function_name: CString,
pub shader_stage_flags: PipelineShaderStageCreateFlags,
pub shader_stage_p_next: *const c_void,
pub spec_info: *const SpecializationInfo,
allocation_callbacks: Option<&'a AllocationCallbacks>,
}
impl<'a> ShaderStage<'a> {
/// Initiating the instance of ShaderStage struct, requires only the device and directory path, after that can be build shader stages.
/// Can be customized flags and pointers to structs if it needed.
/// # Examples
///
/// ```rust
/// use ash::{Device, PipelineShaderStageCreateFlags, PipelineShaderStageCreateInfo};
/// use std::path::Path;
///
/// let shader_stage_flags = PipelineShaderStageCreateFlags::RESERVED_2_NV | PipelineShaderStageCreateFlags::ALLOW_VARYING_SUBGROUP_SIZE_EXT;
/// let shader_stages_create_info: Vec<PipelineShaderStageCreateInfo> =
/// ShaderStage::new(&device, &Path::new("example_path/compiled_shaders"))
/// .with_shader_stage_flags(shader_stage_flags)
/// .build();
/// ```
pub fn new(device: &'a Device, dir_path: &'a Path) -> Self {
Self {
device,
dir_path,
shader_flags: ShaderModuleCreateFlags::empty(),
shader_p_next: ptr::null(),
shader_stage_flags: PipelineShaderStageCreateFlags::empty(),
shader_stage_p_next: ptr::null(),
spec_info: ptr::null(),
main_function_name: CString::new("main").unwrap(),
allocation_callbacks: None,
}
}
/// Specifies `ShaderModuleCreateFlags` for the `self.shader_flags` field.
/// # Examples
///
/// ```rust
/// use ash::{Device, ShaderModuleCreateFlags, PipelineShaderStageCreateInfo};
/// use std::path::Path;
///
/// let shader_flags = ShaderModuleCreateFlags::RESERVED_0_NV;
/// let shader_stages_create_info: Vec<PipelineShaderStageCreateInfo> =
/// ShaderStage::new(&device, &Path::new("example_path/compiled_shaders"))
/// .with_shader_stage_flags(shader_flags)
/// .build();
/// ```
pub fn with_shader_flags(&mut self, shader_flags: ShaderModuleCreateFlags) {
self.shader_flags = shader_flags;
}
/// Specifies `pointer` to the struct for the `self.shader_p_next` field.
pub fn with_shader_p_next(&mut self, p_next: *const c_void) {
self.shader_p_next = p_next;
}
/// Specifies `PipelineShaderStageCreateFlags` for the `self.shader_stage_flags` field.
/// # Examples
///
/// ```rust
/// use ash::{Device, PipelineShaderStageCreateFlags, PipelineShaderStageCreateInfo};
/// use std::path::Path;
///
/// let shader_stage_flags = PipelineShaderStageCreateFlags::RESERVED_2_NV | PipelineShaderStageCreateFlags::ALLOW_VARYING_SUBGROUP_SIZE_EXT;
/// let shader_stages_create_info: Vec<PipelineShaderStageCreateInfo> =
/// ShaderStage::new(&device, &Path::new("example_path/compiled_shaders"))
/// .with_shader_stage_flags(shader_stage_flags)
/// .build();
/// ```
pub fn with_shader_stage_flags(&mut self, shader_stage_flags: PipelineShaderStageCreateFlags) {
self.shader_stage_flags = shader_stage_flags;
}
/// Specifies `pointer` to the struct for the `self.shader_stage_p_next` field.
pub fn with_shader_stage_p_next(&mut self, p_next: *const c_void) {
self.shader_stage_p_next = p_next;
}
/// Specifies `SpecializationInfo` for the `self.spec_info` field.
pub fn with_spec_info(&mut self, spec_info: *const SpecializationInfo) {
self.spec_info = spec_info;
}
/// Specifies `main function name` for the `self.main_function_name` field.
/// # Examples
///
/// ```rust
/// use ash::{Device, PipelineShaderStageCreateInfo};
/// use std::path::Path;
///
/// let shader_stages_create_info: Vec<PipelineShaderStageCreateInfo> =
/// ShaderStage::new(&device, &Path::new("example_path/compiled_shaders"))
/// .with_main_function_name("my_main_function")
/// .build();
/// ```
pub fn with_main_function_name(&mut self, main_function_name: &str) {
self.main_function_name = CString::new(main_function_name).unwrap();
}
/// Specifies `AllocationCallbacks` for the creating shader modules for the `self.allocation_callbacks` field.
pub fn with_allocation_callbacks(
&mut self,
allocation_callbacks: Option<&'a AllocationCallbacks>,
) {
self.allocation_callbacks = allocation_callbacks;
}
/// Consumes struct's `instance` and builds vector of shader stages.
/// # Examples
///
/// ```rust
/// use ash::{Device, PipelineShaderStageCreateFlags, PipelineShaderStageCreateInfo};
/// use std::path::Path;
///
/// let shader_stages_create_info: Vec<PipelineShaderStageCreateInfo> =
/// ShaderStage::new(&device, &Path::new("example_path/compiled_shaders"))
/// .build();
/// ```
pub fn build(self) -> Vec<PipelineShaderStageCreateInfo> {
let shader_modules = create_shader_modules(
self.device,
self.dir_path,
self.shader_flags,
self.shader_p_next,
self.allocation_callbacks,
);
let file_paths = read_dir(self.dir_path)
.unwrap()
.into_iter()
.filter(|file_name| {
file_name
.as_ref()
.unwrap()
.path()
.to_str()
.unwrap()
.contains(".spv")
})
.map(|path| path.unwrap().path());
let shader_path: HashMap<&ShaderModule, PathBuf> =
shader_modules.iter().zip(file_paths.into_iter()).collect();
shader_modules
.iter()
.map(|module| {
let path = shader_path.get(&module).unwrap().to_str().unwrap();
PipelineShaderStageCreateInfo {
s_type: StructureType::PIPELINE_SHADER_STAGE_CREATE_INFO,
p_next: self.shader_stage_p_next,
flags: self.shader_stage_flags,
stage: if path.contains(".vert.spv") || path.contains(".vs") {
ShaderStageFlags::VERTEX
} else if path.contains(".frag.spv") || path.contains(".fs") {
ShaderStageFlags::FRAGMENT
} else {
panic!("Failed to define shader type!")
},
module: *module,
p_name: self.main_function_name.as_ptr(),
p_specialization_info: self.spec_info,
}
})
.collect()
}
}
fn create_shader_modules(
device: &Device,
dir_path: &Path,
flags: ShaderModuleCreateFlags,
p_next: *const c_void,
allocation_callbacks: Option<&AllocationCallbacks>,
) -> Vec<ShaderModule> {
let compiled_shader_path = read_dir(dir_path)
.unwrap_or_else(|_| panic!("Failed to read directory path at {:?}", dir_path));
let files_path_buf: Vec<PathBuf> = compiled_shader_path
.into_iter()
.filter(|file_name| {
let file_name = file_name
.as_ref()
.unwrap()
.path()
.to_str()
.unwrap()
.to_owned();
file_name.contains(".spv") || file_name.contains(".vs") || file_name.contains(".fs")
})
.map(|compiled_shader| compiled_shader.unwrap().path())
.collect();
let files = files_path_buf.iter().map(|path_buf| {
File::open(path_buf)
.unwrap_or_else(|_| panic!("Failed to open compiled shader file at {:?}", path_buf))
});
files
.map(|file| {
let shader_code: Vec<u8> = file.bytes().filter_map(|byte| byte.ok()).collect();
let shader_module_create_info = ShaderModuleCreateInfo {
s_type: StructureType::SHADER_MODULE_CREATE_INFO,
p_next,
flags,
code_size: shader_code.len(),
p_code: shader_code.as_ptr() as *const u32,
};
unsafe {
device
.create_shader_module(&shader_module_create_info, allocation_callbacks)
.expect("Failed to create shader module!")
}
})
.collect::<Vec<ShaderModule>>()
}