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
|
//! Used to represent access different memory 'pools'.
//! Ideally, each pool is optimised for a specific use case.
//! You can implement your own pools using whatever algorithm you'd like. You just need to implement [`MemoryPool`] and optionally [`Block`], then access it
//! using [`RenderingContext.pool_allocator`]
//! Alternatively, some default memory pools are availble when the feature `rendy_pools` is used (on by default).
use crate::{context::RenderingContext, types::*};
use std::{
ops::Range,
sync::{Arc, RwLock},
};
use anyhow::Result;
use hal::memory::Properties;
/// An allocator whose memory and allocation pattern is optimised for a specific use case.
pub trait MemoryPool: Send + Sync + 'static {
/// The block returned by this pool
type Block: Block + Send + Sync;
/// Create a new memory pool from the given context
/// This is called to lazily initialise the memory pool when it is first requested.
/// It can do any sort of filtering on memory types required.
fn from_context(context: &RenderingContext) -> Result<Arc<RwLock<Self>>>;
/// Allocate block of memory.
/// On success returns allocated block and amount of memory consumed from device.
/// The returned block must not overlap with any other allocated block, the start of it must be `0 mod(align)`,
/// and it must be at least `size` bytes.
fn alloc(&mut self, device: &DeviceT, size: u64, align: u64) -> Result<(Self::Block, u64)>;
/// Free block of memory.
/// Returns amount of memory returned to the device.
/// If the given block was not allocated from this pool, this should be a no-op and should return 0.
fn free(&mut self, device: &DeviceT, block: Self::Block) -> u64;
/// Deactivate this memory pool, freeing any allocated memory objects.
fn deactivate(self, context: &mut RenderingContext);
}
/// Block that owns a `Range` of the `Memory`.
/// Provides access to safe memory range mapping.
pub trait Block {
/// Get memory properties of the block.
fn properties(&self) -> Properties;
/// Get raw memory object.
fn memory(&self) -> &MemoryT;
/// Get memory range owned by this block.
fn range(&self) -> Range<u64>;
/// Get size of the block.
fn size(&self) -> u64 {
let range = self.range();
range.end - range.start
}
}
/// An additional trait for [`Block`]s that can be mapped to CPU-visible memory.
///
/// This should only be implemented for blocks that are *guaranteed* to be visible to the CPU
/// and may panic if this is not the case.
pub trait MappableBlock: Block {
/// Attempt to map this block to CPU-visible memory.
/// `inner_range` is counted from only inside this block, not the wider memory object this block is a part of
fn map(&mut self, device: &mut DeviceT, inner_range: Range<u64>) -> Result<*mut u8>;
/// Unmap this block from CPU-visible memory.
/// If this block is not mapped, this should be a no-op.
/// Implementors should ensure that this does not accidentally unmap other blocks using the same memory block.
fn unmap(&mut self, device: &mut DeviceT) -> Result<()>;
}
#[cfg(feature = "rendy-pools")]
mod rendy {
use super::*;
use crate::{
error::{EnvironmentError, LockPoisoned, UsageError},
utils::find_memory_type_id,
};
use anyhow::{anyhow, Context, Result};
use hal::{
format::Format,
memory::{Properties as MemProps, SparseFlags},
};
use rendy_memory::{Allocator, Block as RBlock, DynamicAllocator, DynamicBlock, DynamicConfig};
/// So we can use rendy blocks as our blocks
impl<T: RBlock<back::Backend>> Block for T {
fn properties(&self) -> Properties {
<T as RBlock<back::Backend>>::properties(self)
}
fn memory(&self) -> &MemoryT {
<T as RBlock<back::Backend>>::memory(self)
}
fn range(&self) -> Range<u64> {
<T as RBlock<back::Backend>>::range(self)
}
}
/// Intended to be used for textures.
/// The allocated memory is guaranteed to be suitable for any colour image with optimal tiling and no extra sparse flags or view capabilities.
pub struct TexturesPool(DynamicAllocator<back::Backend>);
impl MemoryPool for TexturesPool {
type Block = DynamicBlock<back::Backend>;
fn alloc(&mut self, device: &DeviceT, size: u64, align: u64) -> Result<(Self::Block, u64)> {
Ok(self.0.alloc(device, size, align)?)
}
fn free(&mut self, device: &DeviceT, block: Self::Block) -> u64 {
self.0.free(device, block)
}
fn from_context(context: &RenderingContext) -> Result<Arc<RwLock<Self>>> {
let type_mask = unsafe {
use hal::image::{Kind, Tiling, Usage, ViewCapabilities};
// We create an empty image with the same format as used for textures
// this is to get the type_mask required, which will stay the same for
// all colour images of the same tiling. (certain memory flags excluded).
// Size and alignment don't necessarily stay the same, so we're forced to
// guess at the alignment for our allocator.
let device = context.device().write().map_err(|_| LockPoisoned::Device)?;
let img = device
.create_image(
Kind::D2(16, 16, 1, 1),
1,
Format::Rgba8Srgb,
Tiling::Optimal,
Usage::SAMPLED,
SparseFlags::empty(),
ViewCapabilities::empty(),
)
.context("Error creating test image to get buffer settings")?;
let type_mask = device.get_image_requirements(&img).type_mask;
device.destroy_image(img);
type_mask
};
let allocator = {
let props = MemProps::DEVICE_LOCAL;
DynamicAllocator::new(
find_memory_type_id(context.adapter(), type_mask, props)
.ok_or(EnvironmentError::NoMemoryTypes)?,
props,
DynamicConfig {
block_size_granularity: 4 * 32 * 32, // 32x32 image
max_chunk_size: u64::pow(2, 63),
min_device_allocation: 4 * 32 * 32,
},
context
.physical_device_properties()
.limits
.non_coherent_atom_size as u64,
)
};
Ok(Arc::new(RwLock::new(Self(allocator))))
}
fn deactivate(self, _context: &mut RenderingContext) {
self.0.dispose();
}
}
/// Used for depth buffers.
/// Memory returned is guaranteed to be suitable for any image using `context.target_chain().properties().depth_format` with optimal tiling, and no sparse flags or view capabilities.
pub struct DepthBufferPool(DynamicAllocator<back::Backend>);
impl MemoryPool for DepthBufferPool {
type Block = DynamicBlock<back::Backend>;
fn alloc(&mut self, device: &DeviceT, size: u64, align: u64) -> Result<(Self::Block, u64)> {
Ok(self.0.alloc(device, size, align)?)
}
fn free(&mut self, device: &DeviceT, block: Self::Block) -> u64 {
self.0.free(device, block)
}
fn from_context(context: &RenderingContext) -> Result<Arc<RwLock<Self>>> {
let type_mask = unsafe {
use hal::image::{Kind, Tiling, Usage, ViewCapabilities};
let device = context.device().write().map_err(|_| LockPoisoned::Device)?;
let img = device
.create_image(
Kind::D2(16, 16, 1, 1),
1,
context.target_chain().properties().depth_format,
Tiling::Optimal,
Usage::SAMPLED,
SparseFlags::empty(),
ViewCapabilities::empty(),
)
.context("Error creating test image to get buffer settings")?;
let type_mask = device.get_image_requirements(&img).type_mask;
device.destroy_image(img);
type_mask
};
let allocator = {
let props = MemProps::DEVICE_LOCAL;
DynamicAllocator::new(
find_memory_type_id(context.adapter(), type_mask, props)
.ok_or(EnvironmentError::NoMemoryTypes)?,
props,
DynamicConfig {
block_size_granularity: 4 * 32 * 32, // 32x32 image
max_chunk_size: u64::pow(2, 63),
min_device_allocation: 4 * 32 * 32,
},
context
.physical_device_properties()
.limits
.non_coherent_atom_size as u64,
)
};
Ok(Arc::new(RwLock::new(Self(allocator))))
}
fn deactivate(self, _context: &mut RenderingContext) {
self.0.dispose()
}
}
/// Used for staging buffers
pub struct StagingPool(DynamicAllocator<back::Backend>);
impl MemoryPool for StagingPool {
type Block = MappableRBlock<DynamicBlock<back::Backend>>;
fn alloc(&mut self, device: &DeviceT, size: u64, align: u64) -> Result<(Self::Block, u64)> {
let (b, size) = self.0.alloc(device, size, align)?;
Ok((MappableRBlock::new_unchecked(b), size))
}
fn free(&mut self, device: &DeviceT, block: Self::Block) -> u64 {
self.0.free(device, block.0)
}
fn from_context(context: &RenderingContext) -> Result<Arc<RwLock<Self>>> {
let allocator = {
let props = MemProps::CPU_VISIBLE | MemProps::COHERENT;
let t = find_memory_type_id(context.adapter(), u32::MAX, props)
.ok_or(EnvironmentError::NoMemoryTypes)?;
DynamicAllocator::new(
t,
props,
DynamicConfig {
block_size_granularity: 4 * 32 * 32, // 32x32 image
max_chunk_size: u64::pow(2, 63),
min_device_allocation: 4 * 32 * 32,
},
context
.physical_device_properties()
.limits
.non_coherent_atom_size as u64,
)
};
Ok(Arc::new(RwLock::new(StagingPool(allocator))))
}
fn deactivate(self, _context: &mut RenderingContext) {
self.0.dispose()
}
}
/// Suitable for input data, such as vertices and indices.
pub struct DataPool(DynamicAllocator<back::Backend>);
impl MemoryPool for DataPool {
type Block = DynamicBlock<back::Backend>;
fn alloc(&mut self, device: &DeviceT, size: u64, align: u64) -> Result<(Self::Block, u64)> {
Ok(self.0.alloc(device, size, align)?)
}
fn free(&mut self, device: &DeviceT, block: Self::Block) -> u64 {
self.0.free(device, block)
}
fn from_context(context: &RenderingContext) -> Result<Arc<RwLock<Self>>> {
let allocator = {
let props = MemProps::CPU_VISIBLE | MemProps::COHERENT;
let t = find_memory_type_id(context.adapter(), u32::MAX, props)
.ok_or(EnvironmentError::NoMemoryTypes)?;
DynamicAllocator::new(
t,
props,
DynamicConfig {
block_size_granularity: 4 * 4 * 128, // 128 f32 XYZ[?] vertices
max_chunk_size: u64::pow(2, 63),
min_device_allocation: 4 * 4 * 128,
},
context
.physical_device_properties()
.limits
.non_coherent_atom_size as u64,
)
};
Ok(Arc::new(RwLock::new(DataPool(allocator))))
}
fn deactivate(self, _context: &mut RenderingContext) {
self.0.dispose()
}
}
/// A rendy memory block that is guaranteed to be CPU visible.
pub struct MappableRBlock<B: RBlock<back::Backend>>(B);
impl<B: RBlock<back::Backend>> MappableRBlock<B> {
/// Create a new mappable memory block, returning an error if the block is not CPU visible
pub fn new(block: B) -> Result<Self> {
if !block.properties().contains(MemProps::CPU_VISIBLE) {
return Err(anyhow!(UsageError::NonMappableMemory));
}
Ok(Self::new_unchecked(block))
}
/// Create a new mappable memory block, without checking if the block is CPU visible.
pub fn new_unchecked(block: B) -> Self {
Self(block)
}
}
impl<B: RBlock<back::Backend>> Block for MappableRBlock<B> {
fn properties(&self) -> MemProps {
self.0.properties()
}
fn memory(&self) -> &MemoryT {
self.0.memory()
}
fn range(&self) -> Range<u64> {
self.0.range()
}
}
impl<B: RBlock<back::Backend>> MappableBlock for MappableRBlock<B> {
fn map(&mut self, device: &mut DeviceT, inner_range: Range<u64>) -> Result<*mut u8> {
unsafe { Ok(self.0.map(device, inner_range)?.ptr().as_mut()) }
}
fn unmap(&mut self, device: &mut DeviceT) -> Result<()> {
self.0.unmap(device);
Ok(())
}
}
}
#[cfg(feature = "rendy-pools")]
pub use rendy::*;
|