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
use super::{MipmapExtent, TextureImage};
use std::sync::Arc;
use crate::graphics::vulkan::{buffer::Buffer, Device};
use anyhow::{bail, Result};
use ash::{version::DeviceV1_0, vk};
impl TextureImage {
pub unsafe fn raw_image(&self) -> vk::Image {
self.image
}
pub unsafe fn raw_view(&self) -> vk::ImageView {
self.view
}
pub fn new(
device: Arc<Device>,
image_create_info: vk::ImageCreateInfo,
memory_property_flags: vk::MemoryPropertyFlags,
bytes_per_pixel: u64,
) -> Result<Self> {
let image = unsafe {
device
.logical_device
.create_image(&image_create_info, None)?
};
let allocation = unsafe {
let memory_requirements =
device.logical_device.get_image_memory_requirements(image);
device
.allocate_memory(memory_requirements, memory_property_flags)?
};
unsafe {
device.logical_device.bind_image_memory(
image,
allocation.memory,
allocation.offset,
)?;
}
let view_create_info = vk::ImageViewCreateInfo {
image,
view_type: vk::ImageViewType::TYPE_2D,
format: image_create_info.format,
subresource_range: vk::ImageSubresourceRange {
aspect_mask: vk::ImageAspectFlags::COLOR,
base_mip_level: 0,
level_count: image_create_info.mip_levels,
base_array_layer: 0,
layer_count: 1,
},
components: vk::ComponentMapping {
r: vk::ComponentSwizzle::R,
g: vk::ComponentSwizzle::G,
b: vk::ComponentSwizzle::B,
a: vk::ComponentSwizzle::A,
},
..Default::default()
};
let view = unsafe {
device
.logical_device
.create_image_view(&view_create_info, None)?
};
Ok(Self {
bytes_per_pixel,
image,
extent: image_create_info.extent,
view,
allocation,
device,
})
}
pub unsafe fn upload_from_buffer<Buf>(&mut self, src: &Buf) -> Result<()>
where
Buf: Buffer,
{
let mipmap_extent = MipmapExtent {
width: self.extent.width,
height: self.extent.height,
};
self.upload_mipmaps_from_buffer(src, &[mipmap_extent])
}
pub unsafe fn upload_mipmaps_from_buffer(
&mut self,
src: &impl Buffer,
mipmap_sizes: &[MipmapExtent],
) -> Result<()> {
let required_size: u64 = mipmap_sizes
.iter()
.map(|mipmap_size| mipmap_size.size_in_bytes(self.bytes_per_pixel))
.sum();
if required_size > src.size_in_bytes() {
bail!(
"The texture expects {:?} bytes, but the provided buffer includes only {:?} bytes of data!",
required_size,
src.size_in_bytes()
);
}
self.device.sync_graphics_commands(|command_buffer| {
let mut mip_level = 0;
let mut offset: u64 = 0;
for extent in mipmap_sizes {
self.write_barrier(command_buffer, mip_level);
self.copy_buffer_to_image(
command_buffer,
src.raw(),
offset,
extent,
mip_level,
);
self.read_barrier(command_buffer, mip_level);
mip_level += 1;
offset += extent.size_in_bytes(self.bytes_per_pixel);
}
Ok(())
})
}
pub unsafe fn write_barrier(
&self,
command_buffer: vk::CommandBuffer,
mip_level: u32,
) {
let write_barrier = vk::ImageMemoryBarrier {
old_layout: vk::ImageLayout::UNDEFINED,
new_layout: vk::ImageLayout::TRANSFER_DST_OPTIMAL,
image: self.image,
subresource_range: vk::ImageSubresourceRange {
aspect_mask: vk::ImageAspectFlags::COLOR,
base_mip_level: mip_level,
level_count: 1,
base_array_layer: 0,
layer_count: 1,
},
src_access_mask: vk::AccessFlags::empty(),
dst_access_mask: vk::AccessFlags::TRANSFER_WRITE,
..Default::default()
};
self.device.logical_device.cmd_pipeline_barrier(
command_buffer,
vk::PipelineStageFlags::TOP_OF_PIPE,
vk::PipelineStageFlags::TRANSFER,
vk::DependencyFlags::empty(),
&[],
&[],
&[write_barrier],
);
}
unsafe fn read_barrier(
&self,
command_buffer: vk::CommandBuffer,
mip_level: u32,
) {
let read_barrier = vk::ImageMemoryBarrier {
old_layout: vk::ImageLayout::TRANSFER_DST_OPTIMAL,
new_layout: vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL,
image: self.image,
subresource_range: vk::ImageSubresourceRange {
aspect_mask: vk::ImageAspectFlags::COLOR,
base_mip_level: mip_level,
level_count: 1,
base_array_layer: 0,
layer_count: 1,
},
src_access_mask: vk::AccessFlags::TRANSFER_WRITE,
dst_access_mask: vk::AccessFlags::SHADER_READ,
..Default::default()
};
self.device.logical_device.cmd_pipeline_barrier(
command_buffer,
vk::PipelineStageFlags::TRANSFER,
vk::PipelineStageFlags::FRAGMENT_SHADER,
vk::DependencyFlags::empty(),
&[],
&[],
&[read_barrier],
);
}
unsafe fn copy_buffer_to_image(
&self,
command_buffer: vk::CommandBuffer,
src_buffer: vk::Buffer,
offset: u64,
mipmap_extent: &MipmapExtent,
mip_level: u32,
) {
let region = vk::BufferImageCopy {
buffer_offset: offset,
buffer_row_length: 0,
buffer_image_height: 0,
image_subresource: vk::ImageSubresourceLayers {
aspect_mask: vk::ImageAspectFlags::COLOR,
mip_level,
base_array_layer: 0,
layer_count: 1,
},
image_offset: vk::Offset3D { x: 0, y: 0, z: 0 },
image_extent: vk::Extent3D {
width: mipmap_extent.width,
height: mipmap_extent.height,
depth: 1,
},
};
self.device.logical_device.cmd_copy_buffer_to_image(
command_buffer,
src_buffer,
self.image,
vk::ImageLayout::TRANSFER_DST_OPTIMAL,
&[region],
);
}
}
impl Drop for TextureImage {
fn drop(&mut self) {
log::trace!("DESTROY TEXTURE");
unsafe {
self.device
.logical_device
.destroy_image_view(self.view, None);
self.device.logical_device.destroy_image(self.image, None);
self.image = vk::Image::null();
self.device.free_memory(&self.allocation).unwrap();
}
}
}