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
use {
    crate::{graphics::vulkan::allocator::HumanizedSize, trace},
    anyhow::{bail, Result},
    ash::vk,
};

/// Allocators return blocks of memory that can be used for Vulkan operations.
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Block {
    offset: u64,
    size: u64,
    memory: vk::DeviceMemory,
    mapped_ptr: *mut std::ffi::c_void,
    memory_type_index: u32,
}

/// Blocks are not Send by default because they include the mapped_ptr.
///
/// It is safe to send the mapped ptr between threads, but the application is
/// responsible for synchronizing access to the underlying buffer via the
/// pointer.
unsafe impl Send for Block {}

/// It is generally safe to share blocks between threads. The application is
/// responsible for synchronizing access to device memory.
unsafe impl Sync for Block {}

impl Block {
    /// Creates a new block.
    pub(super) fn new(
        offset: u64,
        size: u64,
        memory: vk::DeviceMemory,
        mapped_ptr: *mut std::ffi::c_void,
        memory_type_index: u32,
    ) -> Self {
        Self {
            offset,
            size,
            memory,
            mapped_ptr,
            memory_type_index,
        }
    }

    /// Returns true when self is entirely contained by other, false otherwise.
    ///
    /// Note: partial overlaps are not considered 'subregions'.
    /// A.is_subregion_of(B) will return true iff A is fully contained by B.
    pub fn is_subregion_of(&self, other: &Block) -> bool {
        if self.memory() != other.memory() {
            // The blocks refer to different underlying DeviceMemory allocations
            return false;
        }

        let start = self.offset();
        let end = (self.offset() + self.size()) - 1;

        let starts_within =
            start >= other.offset() && start < other.offset() + other.size();
        let ends_within =
            end >= other.offset() && end < other.offset() + other.size();

        starts_within && ends_within
    }

    /// Returns a subregion of the current Block.
    ///
    /// Fails if the requested subregion is out of bounds or cannot fit within
    /// the current block.
    ///
    /// ## Params
    ///
    /// - offset: the offset in bytes from the beginning of the current block
    /// - size: the size of the subregion to return
    pub fn subregion(&self, offset: u64, size: u64) -> Result<Self> {
        if offset >= self.size || offset + size > self.size {
            bail!(trace!(
                "Subregion at {} with size {:?} is out of bounds! {:#?}",
                offset,
                HumanizedSize(size),
                self,
            )());
        }

        let mapped_ptr: *mut std::ffi::c_void = if self.mapped_ptr.is_null() {
            std::ptr::null_mut()
        } else {
            unsafe { self.mapped_ptr.byte_offset(offset as isize) }
        };

        Ok(Block {
            offset: self.offset + offset,
            size,
            memory: self.memory,
            mapped_ptr,
            memory_type_index: self.memory_type_index,
        })
    }

    /// Returns the start of the block as a byte offset into device memory.
    pub fn offset(&self) -> u64 {
        self.offset
    }

    /// Returns the size of the block in bytes.
    pub fn size(&self) -> u64 {
        self.size
    }

    /// Gets a non-owning copy of the underlying DeviceMemory handle.
    ///
    /// Multiple blocks can reference the underlying memory. Blocks returned by
    /// the allocator are guaranteed to not overlap.
    pub fn memory(&self) -> vk::DeviceMemory {
        self.memory
    }

    /// Returns the memory-mapped pointer for the DeviceMemory.
    ///
    /// The pointer is null() if the memory is not mapped.
    ///
    /// The pointer always points to the start of the Block and does not need to
    /// be manually adjusted to account for the offset.
    pub fn mapped_ptr(&self) -> *mut std::ffi::c_void {
        self.mapped_ptr
    }

    /// Returns the memory type index for the Block's device memory.
    pub fn memory_type_index(&self) -> u32 {
        self.memory_type_index
    }
}

impl std::fmt::Debug for Block {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Block")
            .field("offset", &self.offset)
            .field("size", &HumanizedSize(self.size))
            .field("memory_type_index", &self.memory_type_index)
            .field("memory", &self.memory)
            .field("mapped_ptr", &self.mapped_ptr)
            .finish()
    }
}

#[cfg(test)]
mod test {
    use {super::*, std::ffi::c_void, vk::Handle};

    #[test]
    pub fn is_subregion_of_should_be_true_for_contained_blocks() -> Result<()> {
        let block = Block::new(
            0,
            100,
            vk::DeviceMemory::null(),
            std::ptr::null_mut(),
            0,
        );
        assert!(block.subregion(99, 1)?.is_subregion_of(&block));
        assert!(block.subregion(0, 100)?.is_subregion_of(&block));
        assert!(block.subregion(50, 50)?.is_subregion_of(&block));
        assert!(block.subregion(2, 80)?.is_subregion_of(&block));
        Ok(())
    }

    #[test]
    pub fn is_subregion_of_should_be_false_for_partial_overlaps() -> Result<()>
    {
        let blk = Block::new(
            50,
            50,
            vk::DeviceMemory::null(),
            std::ptr::null_mut(),
            0,
        );
        let end_overlap = Block { offset: 75, ..blk };
        assert!(!end_overlap.is_subregion_of(&blk));

        let start_overlap = Block { offset: 25, ..blk };
        assert!(!start_overlap.is_subregion_of(&blk));
        Ok(())
    }

    #[test]
    pub fn is_subregion_of_should_be_true_for_identical_blocks() {
        let block = Block::new(
            0,
            100,
            vk::DeviceMemory::null(),
            std::ptr::null_mut(),
            0,
        );
        assert!(block.is_subregion_of(&block));
    }

    #[test]
    pub fn is_subregion_of_should_be_false_when_device_memory_does_not_match() {
        let a = Block::new(
            0,
            100,
            vk::DeviceMemory::from_raw(1), // INVALID - do not access
            std::ptr::null_mut(),
            0,
        );
        let b = Block::new(
            0,
            100,
            vk::DeviceMemory::from_raw(2), // INVALID - do not access
            std::ptr::null_mut(),
            0,
        );
        assert!(!a.is_subregion_of(&b));
    }

    #[test]
    pub fn subregion_should_fail_when_offset_out_of_bounds() {
        // initial offset is out of bounds
        assert!(Block {
            offset: 2,
            size: 100,
            memory: vk::DeviceMemory::null(),
            mapped_ptr: std::ptr::null_mut(),
            memory_type_index: 0
        }
        .subregion(100, 0)
        .is_err());

        // offset+size is out of bounds
        assert!(Block {
            offset: 2,
            size: 100,
            memory: vk::DeviceMemory::null(),
            mapped_ptr: std::ptr::null_mut(),
            memory_type_index: 0
        }
        .subregion(50, 51)
        .is_err());
    }

    #[test]
    pub fn subregion_should_use_cumulative_offset() -> Result<()> {
        let block = Block {
            offset: 5,
            size: 100,
            memory: vk::DeviceMemory::null(),
            mapped_ptr: std::ptr::null_mut(),
            memory_type_index: 0,
        };
        let sub = block.subregion(3, 80)?;
        assert!(sub.offset == 8);
        assert!(sub.size == 80);
        assert!(sub.mapped_ptr.is_null());
        Ok(())
    }

    #[test]
    pub fn subregion_should_update_mapped_ptr() -> Result<()> {
        let buffer = [0_u8; 100];
        let block = Block {
            offset: 0,
            size: 100,
            memory: vk::DeviceMemory::null(),
            mapped_ptr: buffer.as_ptr() as *mut c_void,
            memory_type_index: 0,
        };
        let sub = block.subregion(3, 80)?;
        let ptr_offset =
            unsafe { sub.mapped_ptr.byte_offset_from(block.mapped_ptr) };
        assert!(ptr_offset == (sub.offset as isize));

        let sub2 = sub.subregion(15, 30)?;
        assert!(
            unsafe { sub2.mapped_ptr.byte_offset_from(block.mapped_ptr) }
                == 15 + 3
        );
        assert!(
            unsafe { sub2.mapped_ptr.byte_offset_from(sub.mapped_ptr) } == 15
        );
        Ok(())
    }
}