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
use std::cmp;
use std::error;
use std::fmt;
use std::mem;
use VulkanObject;
use buffer::TypedBufferAccess;
use device::Device;
use device::DeviceOwned;
pub fn check_update_buffer<B, D>(device: &Device, buffer: &B, data: &D)
-> Result<(), CheckUpdateBufferError>
where B: ?Sized + TypedBufferAccess<Content = D>,
D: ?Sized
{
assert_eq!(buffer.inner().buffer.device().internal_object(),
device.internal_object());
if !buffer.inner().buffer.usage_transfer_destination() {
return Err(CheckUpdateBufferError::BufferMissingUsage);
}
if buffer.inner().offset % 4 != 0 {
return Err(CheckUpdateBufferError::WrongAlignment);
}
let size = cmp::min(buffer.size(), mem::size_of_val(data));
if size % 4 != 0 {
return Err(CheckUpdateBufferError::WrongAlignment);
}
if size > 65536 {
return Err(CheckUpdateBufferError::DataTooLarge);
}
Ok(())
}
#[derive(Debug, Copy, Clone)]
pub enum CheckUpdateBufferError {
BufferMissingUsage,
WrongAlignment,
DataTooLarge,
}
impl error::Error for CheckUpdateBufferError {
#[inline]
fn description(&self) -> &str {
match *self {
CheckUpdateBufferError::BufferMissingUsage => {
"the transfer destination usage must be enabled on the buffer"
},
CheckUpdateBufferError::WrongAlignment => {
"the offset or size are not aligned to 4 bytes"
},
CheckUpdateBufferError::DataTooLarge => "data is too large",
}
}
}
impl fmt::Display for CheckUpdateBufferError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "{}", error::Error::description(self))
}
}
#[cfg(test)]
mod tests {
use super::*;
use buffer::BufferAccess;
use buffer::BufferUsage;
use buffer::CpuAccessibleBuffer;
#[test]
fn missing_usage() {
let (device, queue) = gfx_dev_and_queue!();
let buffer =
CpuAccessibleBuffer::from_data(device.clone(), BufferUsage::vertex_buffer(), 0u32)
.unwrap();
match check_update_buffer(&device, &buffer, &0) {
Err(CheckUpdateBufferError::BufferMissingUsage) => (),
_ => panic!(),
}
}
#[test]
fn data_too_large() {
let (device, queue) = gfx_dev_and_queue!();
let buffer = CpuAccessibleBuffer::from_iter(device.clone(),
BufferUsage::transfer_destination(),
0 .. 65536)
.unwrap();
let data = (0 .. 65536).collect::<Vec<u32>>();
match check_update_buffer(&device, &buffer, &data[..]) {
Err(CheckUpdateBufferError::DataTooLarge) => (),
_ => panic!(),
}
}
#[test]
fn data_just_large_enough() {
let (device, queue) = gfx_dev_and_queue!();
let buffer = CpuAccessibleBuffer::from_iter(device.clone(),
BufferUsage::transfer_destination(),
(0 .. 100000).map(|_| 0))
.unwrap();
let data = (0 .. 65536).map(|_| 0).collect::<Vec<u8>>();
match check_update_buffer(&device, &buffer, &data[..]) {
Ok(_) => (),
_ => panic!(),
}
}
#[test]
fn wrong_alignment() {
let (device, queue) = gfx_dev_and_queue!();
let buffer = CpuAccessibleBuffer::from_iter(device.clone(),
BufferUsage::transfer_destination(),
0 .. 100)
.unwrap();
let data = (0 .. 30).collect::<Vec<u8>>();
match check_update_buffer(&device, &buffer.slice(1 .. 50).unwrap(), &data[..]) {
Err(CheckUpdateBufferError::WrongAlignment) => (),
_ => panic!(),
}
}
#[test]
fn wrong_device() {
let (dev1, queue) = gfx_dev_and_queue!();
let (dev2, _) = gfx_dev_and_queue!();
let buffer = CpuAccessibleBuffer::from_data(dev1, BufferUsage::all(), 0u32).unwrap();
assert_should_panic!({
let _ = check_update_buffer(&dev2, &buffer, &0);
});
}
}