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
use std::num::NonZeroU32;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct BufferInitDescriptor<'a> {
pub label: crate::Label<'a>,
pub contents: &'a [u8],
pub usage: crate::BufferUsages,
}
pub trait DeviceExt {
fn create_buffer_init(&self, desc: &BufferInitDescriptor) -> crate::Buffer;
fn create_texture_with_data(
&self,
queue: &crate::Queue,
desc: &crate::TextureDescriptor,
data: &[u8],
) -> crate::Texture;
}
impl DeviceExt for crate::Device {
fn create_buffer_init(&self, descriptor: &BufferInitDescriptor<'_>) -> crate::Buffer {
if descriptor.contents.is_empty() {
let wgt_descriptor = crate::BufferDescriptor {
label: descriptor.label,
size: 0,
usage: descriptor.usage,
mapped_at_creation: false,
};
self.create_buffer(&wgt_descriptor)
} else {
let unpadded_size = descriptor.contents.len() as crate::BufferAddress;
let align_mask = crate::COPY_BUFFER_ALIGNMENT - 1;
let padded_size =
((unpadded_size + align_mask) & !align_mask).max(crate::COPY_BUFFER_ALIGNMENT);
let wgt_descriptor = crate::BufferDescriptor {
label: descriptor.label,
size: padded_size,
usage: descriptor.usage,
mapped_at_creation: true,
};
let buffer = self.create_buffer(&wgt_descriptor);
buffer.slice(..).get_mapped_range_mut()[..unpadded_size as usize]
.copy_from_slice(descriptor.contents);
buffer.unmap();
buffer
}
}
fn create_texture_with_data(
&self,
queue: &crate::Queue,
desc: &crate::TextureDescriptor,
data: &[u8],
) -> crate::Texture {
let mut desc = desc.to_owned();
desc.usage |= crate::TextureUsages::COPY_DST;
let texture = self.create_texture(&desc);
let format_info = desc.format.describe();
let layer_iterations = desc.array_layer_count();
let mut binary_offset = 0;
for layer in 0..layer_iterations {
for mip in 0..desc.mip_level_count {
let mut mip_size = desc.mip_level_size(mip).unwrap();
if desc.dimension != wgt::TextureDimension::D3 {
mip_size.depth_or_array_layers = 1;
}
let mip_physical = mip_size.physical_size(desc.format);
let width_blocks = mip_physical.width / format_info.block_dimensions.0 as u32;
let height_blocks = mip_physical.height / format_info.block_dimensions.1 as u32;
let bytes_per_row = width_blocks * format_info.block_size as u32;
let data_size = bytes_per_row * height_blocks * mip_size.depth_or_array_layers;
let end_offset = binary_offset + data_size as usize;
queue.write_texture(
crate::ImageCopyTexture {
texture: &texture,
mip_level: mip as u32,
origin: crate::Origin3d {
x: 0,
y: 0,
z: layer,
},
aspect: wgt::TextureAspect::All,
},
&data[binary_offset..end_offset],
crate::ImageDataLayout {
offset: 0,
bytes_per_row: Some(
NonZeroU32::new(bytes_per_row).expect("invalid bytes per row"),
),
rows_per_image: Some(
NonZeroU32::new(height_blocks).expect("invalid height"),
),
},
mip_physical,
);
binary_offset = end_offset;
}
}
texture
}
}