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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
|
use std::any::type_name;
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::fmt::{Debug, Display, Write};
use std::path::Path;
use std::str::Utf8Error;
use std::sync::Arc;
use bitflags::{bitflags, bitflags_match};
use ecs::phase::INIT as INIT_PHASE;
use shader_slang::{
Blob as SlangBlob,
ComponentType as SlangComponentType,
DebugInfoLevel as SlangDebugInfoLevel,
EntryPoint as SlangEntryPoint,
GlobalSession as SlangGlobalSession,
Module as SlangModule,
Session as SlangSession,
};
use crate::asset::{
Asset,
Assets,
Event as AssetEvent,
Events as AssetEvents,
Handle as AssetHandle,
Id as AssetId,
Submitter as AssetSubmitter,
HANDLE_ASSETS_PHASE,
};
use crate::builder;
use crate::ecs::pair::ChildOf;
use crate::ecs::phase::Phase;
use crate::ecs::sole::Single;
use crate::ecs::{declare_entity, pair, Sole};
use crate::mesh::vertex_buffer::VertexLabel;
pub mod cursor;
pub mod reflection;
pub const STD_VERT_IN_SEM_NAME_POSITION: &str = "STD_POSITION";
pub const STD_VERT_IN_SEM_NAME_NORMAL: &str = "STD_NORMAL";
pub const STD_VERT_IN_SEM_NAME_UV_FROM_TOP_LEFT: &str = "STD_UV_FROM_TOP_LEFT";
pub const STD_VERT_IN_SEM_NAME_COLOR: &str = "STD_COLOR";
/// Shader module.
#[derive(Debug)]
pub struct ModuleSource
{
pub name: Cow<'static, str>,
pub file_path: Cow<'static, Path>,
pub source: Cow<'static, str>,
pub link_entrypoints: EntrypointFlags,
}
impl Asset for ModuleSource
{
fn visit_dependencies<'this>(
&'this self,
_func: &mut dyn FnMut(crate::asset::Dependency<'this>),
)
{
}
}
bitflags! {
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct EntrypointFlags: usize
{
const FRAGMENT = 1 << 0;
const VERTEX = 1 << 1;
}
}
#[derive(Clone)]
pub struct Module
{
inner: SlangModule,
}
impl Module
{
#[must_use]
pub fn entry_points(&self) -> impl ExactSizeIterator<Item = EntryPoint> + use<'_>
{
self.inner
.entry_points()
.map(|entry_point| EntryPoint { inner: entry_point })
}
#[must_use]
pub fn get_entry_point(&self, entry_point: &str) -> Option<EntryPoint>
{
let entry_point = self.inner.find_entry_point_by_name(entry_point)?;
Some(EntryPoint { inner: entry_point })
}
#[must_use]
pub fn file_path(&self) -> &str
{
self.inner.file_path()
}
}
pub struct EntryPoint
{
inner: SlangEntryPoint,
}
impl EntryPoint
{
#[must_use]
pub fn function_reflection(&self) -> self::reflection::Function<'_>
{
self::reflection::Function {
inner: self.inner.function_reflection(),
}
}
}
#[derive(Clone)]
pub struct Program
{
inner: SlangComponentType,
metadata: ProgramMetadata,
}
impl Program
{
pub fn into_linked(self) -> Result<Program, Error>
{
let linked_program = self.inner.link()?;
Ok(Program {
inner: linked_program,
metadata: self.metadata,
})
}
#[must_use]
pub fn metadata(&self) -> &ProgramMetadata
{
&self.metadata
}
pub fn get_entry_point_code(&self, entry_point_index: u32) -> Result<Blob, Error>
{
let blob = self.inner.entry_point_code(entry_point_index.into(), 0)?;
Ok(Blob { inner: blob })
}
pub fn reflection(&self, target: u32)
-> Result<self::reflection::Program<'_>, Error>
{
let reflection = self.inner.layout(i64::from(target))?;
Ok(self::reflection::Program { inner: reflection })
}
}
impl Debug for Program
{
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
{
formatter
.debug_struct(type_name::<Self>())
.finish_non_exhaustive()
}
}
pub struct Blob
{
inner: SlangBlob,
}
impl Blob
{
#[must_use]
pub fn as_bytes(&self) -> &[u8]
{
self.inner.as_slice()
}
pub fn as_str(&self) -> Result<&str, Utf8Error>
{
self.inner.as_str()
}
}
builder! {
#[builder(name = SettingsBuilder, derives=(Debug))]
#[derive(Debug)]
#[non_exhaustive]
pub struct Settings
{
link_entrypoints: EntrypointFlags,
}
}
#[derive(Sole)]
pub struct Context
{
_global_session: SlangGlobalSession,
session: SlangSession,
modules: HashMap<AssetId, Module>,
programs: HashMap<AssetId, Program>,
}
impl Context
{
#[must_use]
pub fn get_module(&self, asset_id: &AssetId) -> Option<&Module>
{
self.modules.get(asset_id)
}
#[must_use]
pub fn get_program(&self, asset_id: &AssetId) -> Option<&Program>
{
self.programs.get(asset_id)
}
#[tracing::instrument(skip_all, fields(module_file = module.file_path()))]
pub fn compose_into_program(
&self,
module: Module,
link_entrypoints: EntrypointFlags,
) -> Result<Program, ComposeProgramError>
{
let entry_points = link_entrypoints
.iter()
.filter_map(|entrypoint_flag| {
let entrypoint_name = bitflags_match!(entrypoint_flag, {
EntrypointFlags::VERTEX => Some("vertex_main"),
EntrypointFlags::FRAGMENT => Some("fragment_main"),
_ => None
})?;
let Some(entry_point) = module.get_entry_point(entrypoint_name) else {
return Some(Err(ComposeProgramError::EntrypointNotFoundError {
entrypoint_name,
}));
};
Some(Ok(entry_point))
})
.collect::<Result<Vec<_>, _>>()?;
let components = entry_points
.into_iter()
.map(|entry_point| SlangComponentType::from(entry_point.inner))
.chain([SlangComponentType::from(module.inner)])
.collect::<Vec<_>>();
let program = self
.session
.create_composite_component_type(&components)
.map_err(|err| ComposeProgramError::Other(Error(err)))?;
let vertex_desc = if link_entrypoints.contains(EntrypointFlags::VERTEX) {
// TODO: Do not have hard coded target here
let Ok(program_reflection) = program.layout(0) else {
unreachable!();
};
let program_reflection =
self::reflection::Program { inner: program_reflection };
Some(VertexDescription::new(
&program_reflection
.get_entry_point_by_name("vertex_main")
.expect("Not possible"),
)?)
} else {
None
};
Ok(Program {
inner: program,
metadata: ProgramMetadata { vertex_desc },
})
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ProgramMetadata
{
/// If the program has a entry point in the vertex stage, this field will contain a
/// description of the vertex type passed to the entry point.
pub vertex_desc: Option<VertexDescription>,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct VertexDescription
{
pub inputs: Arc<[VertexInputDescription]>,
}
impl VertexDescription
{
#[tracing::instrument(skip_all, fields(vs_entry_point_name = vs_entrypoint.name()))]
pub fn new(
vs_entrypoint: &self::reflection::EntryPointReflection<'_>,
) -> Result<Self, VertexDescriptionError>
{
if vs_entrypoint.stage() != self::reflection::Stage::Vertex {
return Err(VertexDescriptionError::EntrypointNotInVertexStage);
}
let inputs = VarInputDfsIter::new(vs_entrypoint).inspect(|var_input| {
if var_input.type_layout.kind() != self::reflection::TypeKind::Struct {
return;
}
let Some(semantic_name) = var_input.var_layout.semantic_name() else {
return;
};
tracing::warn!(
"Semantic name '{}' of '{}' will be inherited by it's fields",
semantic_name,
var_input.var_layout.name().unwrap_or("<unnamed>")
);
});
let mut seen_inputs = HashSet::<VertexInputSemName>::new();
Ok(Self {
inputs: inputs
.filter(|var_input| {
var_input.type_layout.kind() != self::reflection::TypeKind::Struct
})
.map(|var_input| {
let name = var_input.var_layout.name().unwrap_or("<unnamed>");
let semantic_name =
var_input.var_layout.semantic_name().ok_or_else(|| {
VertexDescriptionError::VertexInputMissingSemanticName {
name: name.to_owned(),
}
})?;
let semantic_name =
VertexInputSemName::from_semantic_name(semantic_name);
if seen_inputs.contains(&semantic_name) {
return Err(
VertexDescriptionError::VertexInputHasOccupiedSemanticName {
name: name.to_owned(),
},
);
}
let (
self::reflection::TypeKind::Scalar
| self::reflection::TypeKind::Vector,
Some(scalar_type),
) = (
var_input.type_layout.kind(),
var_input.type_layout.scalar_type(),
)
else {
return Err(VertexDescriptionError::UnsupportedVertexInputType {
name: name.to_owned(),
});
};
seen_inputs.insert(semantic_name.clone());
Ok(VertexInputDescription {
semantic_name,
index: var_input.index,
type_kind: var_input.type_layout.kind(),
scalar_type,
})
})
.collect::<Result<Vec<_>, _>>()?
.into(),
})
}
}
#[derive(Debug)]
#[non_exhaustive]
pub struct VertexInputDescription
{
pub semantic_name: VertexInputSemName,
pub index: usize,
pub type_kind: self::reflection::TypeKind,
pub scalar_type: self::reflection::ScalarType,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum VertexInputSemName
{
Position,
Normal,
UvFromTopLeft,
Color,
Other(Box<str>),
}
impl VertexInputSemName
{
fn from_semantic_name(semantic_name: &str) -> Self
{
match semantic_name {
STD_VERT_IN_SEM_NAME_POSITION => Self::Position,
STD_VERT_IN_SEM_NAME_NORMAL => Self::Normal,
STD_VERT_IN_SEM_NAME_UV_FROM_TOP_LEFT => Self::UvFromTopLeft,
STD_VERT_IN_SEM_NAME_COLOR => Self::Color,
_ => Self::Other(semantic_name.to_lowercase().into_boxed_str()),
}
}
#[must_use]
pub fn matches_vertex_label(&self, vertex_label: &VertexLabel) -> bool
{
match (self, vertex_label) {
(Self::Position, VertexLabel::Position)
| (Self::Normal, VertexLabel::Normal)
| (Self::UvFromTopLeft, VertexLabel::UvFromTopLeft)
| (Self::Color, VertexLabel::Color) => true,
(Self::Other(other), VertexLabel::Other(other_vertex_label)) => {
**other == *other_vertex_label
}
_ => false,
}
}
}
impl Display for VertexInputSemName
{
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
{
match self {
Self::Position => formatter.write_str(STD_VERT_IN_SEM_NAME_POSITION),
Self::Normal => formatter.write_str(STD_VERT_IN_SEM_NAME_NORMAL),
Self::UvFromTopLeft => {
formatter.write_str(STD_VERT_IN_SEM_NAME_UV_FROM_TOP_LEFT)
}
Self::Color => formatter.write_str(STD_VERT_IN_SEM_NAME_COLOR),
Self::Other(other) => {
for character in other.chars() {
formatter.write_char(character.to_ascii_uppercase())?;
}
Ok(())
}
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum VertexDescriptionError
{
#[error("Entrypoint is not in vertex stage")]
EntrypointNotInVertexStage,
#[error("Type of vertex input '{name}' is not supported")]
UnsupportedVertexInputType
{
name: String
},
#[error("Vertex input '{name}' is missing a semantic name")]
VertexInputMissingSemanticName
{
name: String
},
#[error(
"Vertex input '{name}' has a semantic name already used by another vertex input"
)]
VertexInputHasOccupiedSemanticName
{
name: String
},
}
#[derive(Debug, thiserror::Error)]
pub enum ComposeProgramError
{
#[error("Shader module does not have a '{entrypoint_name}' entry point")]
EntrypointNotFoundError
{
entrypoint_name: &'static str
},
#[error("Failed to create vertex description")]
VertexDescriptionCreationFailed(#[from] VertexDescriptionError),
#[error(transparent)]
Other(Error),
}
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct Error(#[from] shader_slang::Error);
struct VarInputDfsIter<'a>
{
stack: Vec<(
self::reflection::VariableLayout<'a>,
self::reflection::TypeLayout<'a>,
usize,
)>,
}
impl<'a> VarInputDfsIter<'a>
{
fn new(entry_point: &self::reflection::EntryPointReflection<'a>) -> Self
{
Self {
stack: entry_point
.parameters()
.map(|param| {
let Some(param_type_layout) = param.type_layout() else {
// I do not know in which cases type_layout can return None
unimplemented!();
};
(param, param_type_layout, 0)
})
.collect(),
}
}
}
impl<'a> Iterator for VarInputDfsIter<'a>
{
type Item = VarInput<'a>;
fn next(&mut self) -> Option<Self::Item>
{
let (var_layout, type_layout, index) = loop {
let (var_layout, type_layout, acc_offset) = self.stack.pop()?;
if let Some(offset) = var_layout.varying_input_offset() {
break (var_layout, type_layout, acc_offset + offset);
}
};
if type_layout.kind() == self::reflection::TypeKind::Struct {
self.stack.extend(type_layout.fields().map(|field| {
let Some(field_type_layout) = field.type_layout() else {
// I do not know in which cases type_layout can return
// None
unimplemented!();
};
(field, field_type_layout, index)
}));
}
Some(VarInput { var_layout, type_layout, index })
}
}
struct VarInput<'a>
{
var_layout: self::reflection::VariableLayout<'a>,
type_layout: self::reflection::TypeLayout<'a>,
index: usize,
}
fn import(
asset_submitter: &mut AssetSubmitter<'_>,
file_path: &Path,
settings: Option<&'_ Settings>,
) -> Result<(), ImportError>
{
if file_path.extension().is_none_or(|ext| ext != "slang") {
return Err(ImportError::UnsupportedFileType);
}
let file_name = file_path
.file_name()
.ok_or(ImportError::NoPathFileName)?
.to_str()
.ok_or(ImportError::PathFileNameNotUtf8)?;
let file_path_canonicalized = file_path
.canonicalize()
.map_err(ImportError::CanonicalizePathFailed)?;
asset_submitter.submit_store(ModuleSource {
name: file_name.to_owned().into(),
file_path: file_path_canonicalized.into(),
source: std::fs::read_to_string(file_path)
.map_err(ImportError::ReadFileFailed)?
.into(),
link_entrypoints: settings
.map(|settings| settings.link_entrypoints)
.unwrap_or_default(),
});
Ok(())
}
#[derive(Debug, thiserror::Error)]
enum ImportError
{
#[error("File type is unsupported")]
UnsupportedFileType,
#[error("Failed to read file")]
ReadFileFailed(#[source] std::io::Error),
#[error("Asset path does not have a file name")]
NoPathFileName,
#[error("Asset path file name is not valid UTF8")]
PathFileNameNotUtf8,
#[error("Failed to canonicalize asset path")]
CanonicalizePathFailed(#[source] std::io::Error),
}
declare_entity! {
pub IMPORT_SHADERS_PHASE: (Phase, pair!(ChildOf, { *HANDLE_ASSETS_PHASE }));
}
pub(super) fn prepare(collector: &mut crate::ecs::extension::Collector<'_>)
{
let Some(global_session) = SlangGlobalSession::new() else {
tracing::error!("Unable to create global shader-slang session");
return;
};
let session_options = shader_slang::CompilerOptions::default()
.optimization(shader_slang::OptimizationLevel::None)
.matrix_layout_row(true)
.debug_information(SlangDebugInfoLevel::Maximal)
.no_mangle(true);
let target_desc = shader_slang::TargetDesc::default()
.format(shader_slang::CompileTarget::Glsl)
// .format(shader_slang::CompileTarget::Spirv)
.profile(global_session.find_profile("glsl_330"));
// .profile(global_session.find_profile("spirv_1_5"));
let targets = [target_desc];
let session_desc = shader_slang::SessionDesc::default()
.targets(&targets)
.search_paths([""])
.options(&session_options);
let Some(session) = global_session.create_session(&session_desc) else {
tracing::error!("Failed to create shader-slang session");
return;
};
collector
.add_sole(Context {
_global_session: global_session,
session,
modules: HashMap::new(),
programs: HashMap::new(),
})
.ok();
collector.spawn_declared_entity(&IMPORT_SHADERS_PHASE);
collector.add_system(*INIT_PHASE, initialize);
collector.add_system(*IMPORT_SHADERS_PHASE, load_modules);
}
fn initialize(mut assets: Single<Assets>) -> Result<(), crate::Error>
{
let assets = assets.get_mut()?;
assets.set_importer::<ModuleSource, Settings, ImportError>(import);
Ok(())
}
#[tracing::instrument(skip_all)]
fn load_modules(
mut context: Single<Context>,
assets: Single<Assets>,
asset_events: Single<AssetEvents>,
) -> Result<(), crate::Error>
{
let Ok(context) = context.get_mut() else {
unreachable!();
};
let assets = assets.get()?;
let asset_events = asset_events.get()?;
for AssetEvent::Loaded(asset_id, asset_label) in asset_events.iter_last_tick() {
let asset_handle = AssetHandle::<ModuleSource>::from_id(*asset_id);
if !assets.is_loaded_and_has_type(&asset_handle) {
continue;
}
let Some(module_source) = assets.get(&asset_handle) else {
unreachable!();
};
tracing::debug!(asset_label=?asset_label, "Loading shader module");
let module = match load_module(&context.session, module_source) {
Ok(module) => module,
Err(err) => {
tracing::error!("Failed to load shader module: {err}");
continue;
}
};
context.modules.insert(*asset_id, module.clone());
if !module_source.link_entrypoints.is_empty() {
assert!(!context.programs.contains_key(asset_id));
let shader_program = match context
.compose_into_program(module, module_source.link_entrypoints)
{
Ok(shader_program) => shader_program,
Err(err) => {
tracing::error!(
"Failed to compose shader into program: {:#}",
crate::Error::new(err)
);
continue;
}
};
let linked_shader_program = match shader_program.into_linked() {
Ok(linked_shader_program) => linked_shader_program,
Err(err) => {
tracing::error!("Failed to link shader: {err}");
continue;
}
};
context.programs.insert(*asset_id, linked_shader_program);
}
}
Ok(())
}
fn load_module(
session: &SlangSession,
module_source: &ModuleSource,
) -> Result<Module, Error>
{
let module = session.load_module_from_source_string(
&module_source.name,
&module_source.file_path.to_string_lossy(),
&module_source.source,
)?;
Ok(Module { inner: module })
}
|