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
use rafx_framework::ResourceArc;
use rafx_framework::{DescriptorSetAllocator, RafxResult};
use rafx_framework::{DescriptorSetBindings, ImageViewResource};
use rafx_framework::{DynDescriptorSet, SlotNameLookup};
use std::sync::Arc;

pub struct DynPassMaterialInstance {
    descriptor_sets: Vec<DynDescriptorSet>,
    slot_name_lookup: Arc<SlotNameLookup>,
}

impl DynPassMaterialInstance {
    pub fn new(
        descriptor_sets: Vec<DynDescriptorSet>,
        slot_name_lookup: Arc<SlotNameLookup>,
    ) -> Self {
        DynPassMaterialInstance {
            descriptor_sets,
            slot_name_lookup,
        }
    }

    pub fn descriptor_set_layout(
        &self,
        layout_index: u32,
    ) -> &DynDescriptorSet {
        &self.descriptor_sets[layout_index as usize]
    }

    pub fn flush(
        &mut self,
        descriptor_set_allocator: &mut DescriptorSetAllocator,
    ) -> RafxResult<()> {
        for set in &mut self.descriptor_sets {
            set.flush(descriptor_set_allocator)?
        }

        Ok(())
    }

    pub fn set_image(
        &mut self,
        slot_name: &str,
        image_view: &ResourceArc<ImageViewResource>,
    ) {
        if let Some(slot_locations) = self.slot_name_lookup.get(slot_name) {
            for slot_location in slot_locations {
                if let Some(dyn_descriptor_set) = self
                    .descriptor_sets
                    .get_mut(slot_location.layout_index as usize)
                {
                    dyn_descriptor_set.set_image(slot_location.binding_index, image_view);
                }
            }
        }
    }

    // Requiring 'static helps us catch accidentally trying to store a reference in the buffer
    pub fn set_buffer_data<T: Copy + 'static>(
        &mut self,
        slot_name: &str,
        data: &T,
    ) {
        if let Some(slot_locations) = self.slot_name_lookup.get(slot_name) {
            for slot_location in slot_locations {
                if let Some(dyn_descriptor_set) = self
                    .descriptor_sets
                    .get_mut(slot_location.layout_index as usize)
                {
                    dyn_descriptor_set.set_buffer_data(slot_location.binding_index, data);
                }
            }
        }
    }
}

pub struct DynMaterialInstance {
    passes: Vec<DynPassMaterialInstance>,
}

impl DynMaterialInstance {
    pub fn new(passes: Vec<DynPassMaterialInstance>) -> Self {
        DynMaterialInstance { passes }
    }

    pub fn pass(
        &self,
        pass_index: u32,
    ) -> &DynPassMaterialInstance {
        &self.passes[pass_index as usize]
    }

    pub fn flush(
        &mut self,
        descriptor_set_allocator: &mut DescriptorSetAllocator,
    ) -> RafxResult<()> {
        for pass in &mut self.passes {
            pass.flush(descriptor_set_allocator)?
        }

        Ok(())
    }

    pub fn set_image(
        &mut self,
        slot_name: &str,
        image_view: &ResourceArc<ImageViewResource>,
    ) {
        for pass in &mut self.passes {
            pass.set_image(slot_name, image_view)
        }
    }

    // Requiring 'static helps us catch accidentally trying to store a reference in the buffer
    pub fn set_buffer_data<T: Copy + 'static>(
        &mut self,
        slot_name: &str,
        data: &T,
    ) {
        for pass in &mut self.passes {
            pass.set_buffer_data(slot_name, data)
        }
    }
}