summaryrefslogtreecommitdiff
path: root/engine/src/shader.rs
blob: 65872f1756d0e7d42c0e0c20f1407d5bd515f12a (plain)
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
use std::any::type_name;
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::Debug;
use std::path::Path;
use std::str::Utf8Error;

use bitflags::bitflags;
use ecs::pair::{ChildOf, Pair};
use ecs::phase::{Phase, START as START_PHASE};
use ecs::sole::Single;
use ecs::{Component, Sole, declare_entity};
use shader_slang::{
    Blob as SlangBlob,
    ComponentType as SlangComponentType,
    DebugInfoLevel as SlangDebugInfoLevel,
    EntryPoint as SlangEntryPoint,
    GlobalSession as SlangGlobalSession,
    Module as SlangModule,
    ParameterCategory as SlangParameterCategory,
    Session as SlangSession,
    TypeKind as SlangTypeKind,
};

use crate::asset::{
    Assets,
    Event as AssetEvent,
    HANDLE_ASSETS_PHASE,
    Handle as AssetHandle,
    Id as AssetId,
    Submitter as AssetSubmitter,
};
use crate::builder;
use crate::renderer::PRE_RENDER_PHASE;
use crate::shader::default::{
    ASSET_LABEL,
    enqueue_set_shader_bindings as default_shader_enqueue_set_shader_bindings,
};

pub mod cursor;
pub mod default;

#[derive(Debug, Clone, Component)]
pub struct Shader
{
    pub asset_handle: AssetHandle<ModuleSource>,
}

/// 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,
}

bitflags! {
    #[derive(Debug, Clone, Copy, Default)]
    pub struct EntrypointFlags: usize
    {
        const FRAGMENT = 1 << 0;
        const VERTEX = 1 << 1;
    }
}

pub struct Module
{
    inner: SlangModule,
}

impl Module
{
    pub fn entry_points(&self) -> impl ExactSizeIterator<Item = EntryPoint>
    {
        self.inner
            .entry_points()
            .map(|entry_point| EntryPoint { inner: entry_point })
    }

    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 })
    }
}

pub struct EntryPoint
{
    inner: SlangEntryPoint,
}

impl EntryPoint
{
    pub fn function_name(&self) -> Option<&str>
    {
        self.inner.function_reflection().name()
    }
}

pub struct EntryPointReflection<'a>
{
    inner: &'a shader_slang::reflection::EntryPoint,
}

impl<'a> EntryPointReflection<'a>
{
    pub fn name(&self) -> Option<&str>
    {
        self.inner.name()
    }

    pub fn name_override(&self) -> Option<&str>
    {
        self.inner.name_override()
    }

    pub fn stage(&self) -> Stage
    {
        Stage::from_slang_stage(self.inner.stage())
    }

    pub fn parameters(&self) -> impl ExactSizeIterator<Item = VariableLayout<'a>>
    {
        self.inner
            .parameters()
            .map(|param| VariableLayout { inner: param })
    }

    pub fn var_layout(&self) -> Option<VariableLayout<'a>>
    {
        Some(VariableLayout { inner: self.inner.var_layout()? })
    }
}

#[derive(Clone)]
pub struct Program
{
    inner: SlangComponentType,
}

impl Program
{
    pub fn link(&self) -> Result<Program, Error>
    {
        let linked_program = self.inner.link()?;

        Ok(Program { inner: linked_program })
    }

    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<ProgramReflection<'_>, Error>
    {
        let reflection = self.inner.layout(target as i64)?;

        Ok(ProgramReflection { 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 ProgramReflection<'a>
{
    inner: &'a shader_slang::reflection::Shader,
}

impl<'a> ProgramReflection<'a>
{
    pub fn get_entry_point_by_index(&self, index: u32)
    -> Option<EntryPointReflection<'a>>
    {
        Some(EntryPointReflection {
            inner: self.inner.entry_point_by_index(index)?,
        })
    }

    pub fn get_entry_point_by_name(&self, name: &str)
    -> Option<EntryPointReflection<'a>>
    {
        Some(EntryPointReflection {
            inner: self.inner.find_entry_point_by_name(name)?,
        })
    }

    pub fn entry_points(&self)
    -> impl ExactSizeIterator<Item = EntryPointReflection<'a>>
    {
        self.inner
            .entry_points()
            .map(|entry_point| EntryPointReflection { inner: entry_point })
    }

    pub fn global_params_type_layout(&self) -> Option<TypeLayout<'a>>
    {
        Some(TypeLayout {
            inner: self.inner.global_params_type_layout()?,
        })
    }

    pub fn global_params_var_layout(&self) -> Option<VariableLayout<'a>>
    {
        Some(VariableLayout {
            inner: self.inner.global_params_var_layout()?,
        })
    }

    pub fn get_type(&self, name: &str) -> Option<TypeReflection<'a>>
    {
        Some(TypeReflection {
            inner: self.inner.find_type_by_name(name)?,
        })
    }

    pub fn get_type_layout(&self, ty: &TypeReflection<'a>) -> Option<TypeLayout<'a>>
    {
        Some(TypeLayout {
            inner: self
                .inner
                .type_layout(&ty.inner, shader_slang::LayoutRules::Default)?,
        })
    }
}

#[derive(Clone, Copy)]
pub struct VariableLayout<'a>
{
    inner: &'a shader_slang::reflection::VariableLayout,
}

impl<'a> VariableLayout<'a>
{
    pub fn name(&self) -> Option<&str>
    {
        self.inner.name()
    }

    pub fn semantic_name(&self) -> Option<&str>
    {
        self.inner.semantic_name()
    }

    pub fn binding_index(&self) -> u32
    {
        self.inner
            .offset(shader_slang::ParameterCategory::DescriptorTableSlot) as u32

        // self.inner.binding_index()
    }

    pub fn binding_space(&self) -> u32
    {
        self.inner.binding_space()
    }

    pub fn semantic_index(&self) -> usize
    {
        self.inner.semantic_index()
    }

    pub fn offset(&self) -> usize
    {
        self.inner.offset(shader_slang::ParameterCategory::Uniform)
    }

    pub fn ty(&self) -> Option<TypeReflection<'a>>
    {
        self.inner.ty().map(|ty| TypeReflection { inner: ty })
    }

    pub fn type_layout(&self) -> Option<TypeLayout<'a>>
    {
        Some(TypeLayout { inner: self.inner.type_layout()? })
    }
}

#[derive(Clone, Copy)]
pub struct TypeLayout<'a>
{
    inner: &'a shader_slang::reflection::TypeLayout,
}

impl<'a> TypeLayout<'a>
{
    pub fn get_field_by_name(&self, name: &str) -> Option<VariableLayout<'a>>
    {
        let index = self.inner.find_field_index_by_name(name);

        if index < 0 {
            return None;
        }

        let index = u32::try_from(index.cast_unsigned()).expect("Should not happend");

        let field = self.inner.field_by_index(index)?;

        Some(VariableLayout { inner: field })
    }

    pub fn binding_range_descriptor_set_index(&self, index: i64) -> i64
    {
        self.inner.binding_range_descriptor_set_index(index)
    }

    pub fn get_field_binding_range_offset_by_name(&self, name: &str) -> Option<u64>
    {
        let field_index = self.inner.find_field_index_by_name(name);

        if field_index < 0 {
            return None;
        }

        let field_binding_range_offset =
            self.inner.field_binding_range_offset(field_index);

        if field_binding_range_offset < 0 {
            return None;
        }

        Some(field_binding_range_offset.cast_unsigned())
    }

    pub fn ty(&self) -> Option<TypeReflection<'a>>
    {
        self.inner.ty().map(|ty| TypeReflection { inner: ty })
    }

    pub fn fields(&self) -> impl ExactSizeIterator<Item = VariableLayout<'a>>
    {
        self.inner
            .fields()
            .map(|field| VariableLayout { inner: field })
    }

    pub fn field_cnt(&self) -> u32
    {
        self.inner.field_count()
    }

    pub fn element_type_layout(&self) -> Option<TypeLayout<'a>>
    {
        self.inner
            .element_type_layout()
            .map(|type_layout| TypeLayout { inner: type_layout })
    }

    pub fn element_var_layout(&self) -> Option<VariableLayout<'a>>
    {
        self.inner
            .element_var_layout()
            .map(|var_layout| VariableLayout { inner: var_layout })
    }

    pub fn container_var_layout(&self) -> Option<VariableLayout<'a>>
    {
        self.inner
            .container_var_layout()
            .map(|var_layout| VariableLayout { inner: var_layout })
    }

    pub fn uniform_size(&self) -> Option<usize>
    {
        // tracing::debug!(
        //     "uniform_size: {:?} categories: {:?}",
        //     self.inner.name(),
        //     self.inner.categories().collect::<Vec<_>>(),
        // );

        if !self
            .inner
            .categories()
            .any(|category| category == SlangParameterCategory::Uniform)
        {
            return None;
        }

        // let category = self.inner.categories().next().unwrap();

        // println!(
        //     "AARGH size Category: {category:?}   Category count: {}",
        //     self.inner.category_count()
        // );

        // Some(self.inner.size(category))

        Some(self.inner.size(SlangParameterCategory::Uniform))
    }

    pub fn stride(&self) -> usize
    {
        self.inner.stride(self.inner.categories().next().unwrap())
    }
}

pub struct TypeReflection<'a>
{
    inner: &'a shader_slang::reflection::Type,
}

impl TypeReflection<'_>
{
    pub fn kind(&self) -> TypeKind
    {
        TypeKind::from_slang_type_kind(self.inner.kind())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum TypeKind
{
    None,
    Struct,
    Enum,
    Array,
    Matrix,
    Vector,
    Scalar,
    ConstantBuffer,
    Resource,
    SamplerState,
    TextureBuffer,
    ShaderStorageBuffer,
    ParameterBlock,
    GenericTypeParameter,
    Interface,
    OutputStream,
    MeshOutput,
    Specialized,
    Feedback,
    Pointer,
    DynamicResource,
    Count,
}

impl TypeKind
{
    fn from_slang_type_kind(type_kind: SlangTypeKind) -> Self
    {
        match type_kind {
            SlangTypeKind::None => Self::None,
            SlangTypeKind::Struct => Self::Struct,
            SlangTypeKind::Enum => Self::Enum,
            SlangTypeKind::Array => Self::Array,
            SlangTypeKind::Matrix => Self::Matrix,
            SlangTypeKind::Vector => Self::Vector,
            SlangTypeKind::Scalar => Self::Scalar,
            SlangTypeKind::ConstantBuffer => Self::ConstantBuffer,
            SlangTypeKind::Resource => Self::Resource,
            SlangTypeKind::SamplerState => Self::SamplerState,
            SlangTypeKind::TextureBuffer => Self::TextureBuffer,
            SlangTypeKind::ShaderStorageBuffer => Self::ShaderStorageBuffer,
            SlangTypeKind::ParameterBlock => Self::ParameterBlock,
            SlangTypeKind::GenericTypeParameter => Self::GenericTypeParameter,
            SlangTypeKind::Interface => Self::Interface,
            SlangTypeKind::OutputStream => Self::OutputStream,
            SlangTypeKind::MeshOutput => Self::MeshOutput,
            SlangTypeKind::Specialized => Self::Specialized,
            SlangTypeKind::Feedback => Self::Feedback,
            SlangTypeKind::Pointer => Self::Pointer,
            SlangTypeKind::DynamicResource => Self::DynamicResource,
            SlangTypeKind::Count => Self::Count,
        }
    }
}

pub struct Blob
{
    inner: SlangBlob,
}

impl Blob
{
    pub fn as_bytes(&self) -> &[u8]
    {
        self.inner.as_slice()
    }

    pub fn as_str(&self) -> Result<&str, Utf8Error>
    {
        self.inner.as_str()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Stage
{
    None,
    Vertex,
    Hull,
    Domain,
    Geometry,
    Fragment,
    Compute,
    RayGeneration,
    Intersection,
    AnyHit,
    ClosestHit,
    Miss,
    Callable,
    Mesh,
    Amplification,
    Dispatch,
    Count,
}

impl Stage
{
    fn from_slang_stage(stage: shader_slang::Stage) -> Self
    {
        match stage {
            shader_slang::Stage::None => Self::None,
            shader_slang::Stage::Vertex => Self::Vertex,
            shader_slang::Stage::Hull => Self::Hull,
            shader_slang::Stage::Domain => Self::Domain,
            shader_slang::Stage::Geometry => Self::Geometry,
            shader_slang::Stage::Fragment => Self::Fragment,
            shader_slang::Stage::Compute => Self::Compute,
            shader_slang::Stage::RayGeneration => Self::RayGeneration,
            shader_slang::Stage::Intersection => Self::Intersection,
            shader_slang::Stage::AnyHit => Self::AnyHit,
            shader_slang::Stage::ClosestHit => Self::ClosestHit,
            shader_slang::Stage::Miss => Self::Miss,
            shader_slang::Stage::Callable => Self::Callable,
            shader_slang::Stage::Mesh => Self::Mesh,
            shader_slang::Stage::Amplification => Self::Amplification,
            shader_slang::Stage::Dispatch => Self::Dispatch,
            shader_slang::Stage::Count => Self::Count,
        }
    }
}

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
{
    pub fn get_module(&self, asset_id: &AssetId) -> Option<&Module>
    {
        self.modules.get(asset_id)
    }

    pub fn get_program(&self, asset_id: &AssetId) -> Option<&Program>
    {
        self.programs.get(asset_id)
    }

    pub fn compose_into_program(
        &self,
        modules: &[&Module],
        entry_points: &[&EntryPoint],
    ) -> Result<Program, Error>
    {
        let components =
            modules
                .iter()
                .map(|module| SlangComponentType::from(module.inner.clone()))
                .chain(entry_points.iter().map(|entry_point| {
                    SlangComponentType::from(entry_point.inner.clone())
                }))
                .collect::<Vec<_>>();

        let program = self.session.create_composite_component_type(&components)?;

        Ok(Program { inner: program })
    }
}

#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct Error(#[from] shader_slang::Error);

pub(crate) fn add_asset_importers(assets: &mut Assets)
{
    assets.set_importer::<_, _>(["slang"], import_slang_asset);
}

fn import_slang_asset(
    asset_submitter: &mut AssetSubmitter<'_>,
    file_path: &Path,
    settings: Option<&'_ Settings>,
) -> Result<(), ImportError>
{
    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("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!(
    IMPORT_SHADERS_PHASE,
    (
        Phase,
        Pair::builder()
            .relation::<ChildOf>()
            .target_id(*HANDLE_ASSETS_PHASE)
            .build()
    )
);

pub(crate) struct Extension;

impl ecs::extension::Extension for Extension
{
    fn collect(self, mut collector: 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_column(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.add_declared_entity(&IMPORT_SHADERS_PHASE);

        collector.add_system(*START_PHASE, initialize);
        collector.add_system(*IMPORT_SHADERS_PHASE, load_modules);

        collector.add_system(
            *PRE_RENDER_PHASE,
            default_shader_enqueue_set_shader_bindings,
        );
    }
}

fn initialize(mut assets: Single<Assets>)
{
    assets.store_with_label(
        ASSET_LABEL.clone(),
        ModuleSource {
            name: "default_shader.slang".into(),
            file_path: Path::new("@engine/default_shader").into(),
            source: include_str!("../res/default_shader.slang").into(),
            link_entrypoints: EntrypointFlags::VERTEX | EntrypointFlags::FRAGMENT,
        },
    );
}

#[tracing::instrument(skip_all)]
fn load_modules(mut context: Single<Context>, assets: Single<Assets>)
{
    for AssetEvent::Stored(asset_id, asset_label) in assets.events().last_tick_events() {
        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;
            }
        };

        if !module_source.link_entrypoints.is_empty() {
            assert!(context.programs.get(asset_id).is_none());

            let Some(vertex_shader_entry_point) = module.get_entry_point("vertex_main")
            else {
                tracing::error!(
                    "Shader module does not contain a vertex shader entry point"
                );
                continue;
            };

            let Some(fragment_shader_entry_point) =
                module.get_entry_point("fragment_main")
            else {
                tracing::error!(
                    "Shader module don't contain a fragment_main entry point"
                );
                continue;
            };

            let shader_program = match context.compose_into_program(
                &[&module],
                &[&vertex_shader_entry_point, &fragment_shader_entry_point],
            ) {
                Ok(shader_program) => shader_program,
                Err(err) => {
                    tracing::error!("Failed to compose shader into program: {err}");
                    continue;
                }
            };

            let linked_shader_program = match shader_program.link() {
                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);
        }

        context.modules.insert(*asset_id, module);
    }
}

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 })
}