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
use {
super::{Device, DriverError},
crate::ptr::Shared,
archery::SharedPointerKind,
ash::vk,
derive_builder::Builder,
gpu_allocator::{
vulkan::{Allocation, AllocationCreateDesc},
MemoryLocation,
},
log::trace,
log::warn,
std::{
ops::{Deref, Range},
thread::panicking,
},
};
#[derive(Debug)]
pub struct Buffer<P>
where
P: SharedPointerKind,
{
allocation: Option<Allocation>,
buffer: vk::Buffer,
device: Shared<Device<P>, P>,
pub info: BufferInfo,
}
impl<P> Buffer<P>
where
P: SharedPointerKind,
{
pub fn create(
device: &Shared<Device<P>, P>,
info: impl Into<BufferInfo>,
) -> Result<Self, DriverError> {
trace!("create");
let info = info.into();
let device = Shared::clone(device);
let buffer_info = vk::BufferCreateInfo {
size: info.size as u64,
usage: info.usage,
sharing_mode: vk::SharingMode::EXCLUSIVE,
..Default::default()
};
let buffer = unsafe {
device
.create_buffer(&buffer_info, None)
.map_err(|_| DriverError::Unsupported)?
};
let mut requirements = unsafe { device.get_buffer_memory_requirements(buffer) };
if info
.usage
.contains(vk::BufferUsageFlags::SHADER_BINDING_TABLE_KHR)
{
requirements.alignment = requirements.alignment.max(64);
}
let memory_location = if info.can_map {
MemoryLocation::CpuToGpu
} else {
MemoryLocation::GpuOnly
};
let allocation = device
.allocator
.as_ref()
.unwrap()
.lock()
.allocate(&AllocationCreateDesc {
name: "buffer",
requirements,
location: memory_location,
linear: true,
})
.map_err(|_| DriverError::Unsupported)?;
unsafe {
device
.bind_buffer_memory(buffer, allocation.memory(), allocation.offset())
.map_err(|_| DriverError::Unsupported)?
};
Ok(Self {
allocation: Some(allocation),
buffer,
device,
info,
})
}
pub fn device_address(this: &Self) -> u64 {
unsafe {
this.device.get_buffer_device_address(
&ash::vk::BufferDeviceAddressInfo::builder().buffer(this.buffer),
)
}
}
pub fn mapped_slice_mut(this: &mut Self) -> &mut [u8] {
&mut this
.allocation
.as_mut()
.unwrap()
.mapped_slice_mut()
.unwrap()[0..this.info.size as usize]
}
}
impl<P> Deref for Buffer<P>
where
P: SharedPointerKind,
{
type Target = vk::Buffer;
fn deref(&self) -> &Self::Target {
&self.buffer
}
}
impl<P> Drop for Buffer<P>
where
P: SharedPointerKind,
{
fn drop(&mut self) {
if panicking() {
return;
}
self.device
.allocator
.as_ref()
.unwrap()
.lock()
.free(self.allocation.take().unwrap())
.unwrap_or_else(|_| warn!("Unable to free buffer allocation"));
unsafe {
self.device.destroy_buffer(self.buffer, None);
}
}
}
#[derive(Builder, Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[builder(pattern = "owned")]
pub struct BufferInfo {
pub size: u64,
pub usage: vk::BufferUsageFlags,
#[builder(default)]
pub can_map: bool,
}
impl BufferInfo {
#[allow(clippy::new_ret_no_self)]
pub fn new(size: u64, usage: vk::BufferUsageFlags) -> BufferInfoBuilder {
BufferInfoBuilder::default().size(size).usage(usage)
}
}
impl From<BufferInfoBuilder> for BufferInfo {
fn from(info: BufferInfoBuilder) -> Self {
info.build().unwrap()
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BufferSubresource {
pub range: Range<u64>,
}
impl From<BufferInfo> for BufferSubresource {
fn from(info: BufferInfo) -> Self {
Self {
range: 0..info.size as u64,
}
}
}
impl From<Range<u64>> for BufferSubresource {
fn from(range: Range<u64>) -> Self {
Self { range }
}
}
impl From<Option<Range<u64>>> for BufferSubresource {
fn from(range: Option<Range<u64>>) -> Self {
Self {
range: range.unwrap_or(0..vk::WHOLE_SIZE),
}
}
}