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
use std::error;
use std::fmt;
use VulkanObject;
use buffer::BufferAccess;
use buffer::TypedBufferAccess;
use device::Device;
use device::DeviceOwned;
use pipeline::input_assembly::Index;
pub fn check_index_buffer<B, I>(device: &Device, buffer: &B)
-> Result<CheckIndexBuffer, CheckIndexBufferError>
where B: ?Sized + BufferAccess + TypedBufferAccess<Content = [I]>,
I: Index
{
assert_eq!(buffer.inner().buffer.device().internal_object(),
device.internal_object());
if !buffer.inner().buffer.usage_index_buffer() {
return Err(CheckIndexBufferError::BufferMissingUsage);
}
Ok(CheckIndexBuffer { num_indices: buffer.len() })
}
pub struct CheckIndexBuffer {
pub num_indices: usize,
}
#[derive(Debug, Copy, Clone)]
pub enum CheckIndexBufferError {
BufferMissingUsage,
WrongAlignment,
UnsupportIndexType,
}
impl error::Error for CheckIndexBufferError {
#[inline]
fn description(&self) -> &str {
match *self {
CheckIndexBufferError::BufferMissingUsage => {
"the index buffer usage must be enabled on the index buffer"
},
CheckIndexBufferError::WrongAlignment => {
"the sum of offset and the address of the range of VkDeviceMemory object that is \
backing buffer, must be a multiple of the type indicated by indexType"
},
CheckIndexBufferError::UnsupportIndexType => {
"the type of the indices is not supported by the device"
},
}
}
}
impl fmt::Display for CheckIndexBufferError {
#[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::BufferUsage;
use buffer::CpuAccessibleBuffer;
#[test]
fn num_indices() {
let (device, queue) = gfx_dev_and_queue!();
let buffer = CpuAccessibleBuffer::from_iter(device.clone(),
BufferUsage::index_buffer(),
0 .. 500u32)
.unwrap();
match check_index_buffer(&device, &buffer) {
Ok(CheckIndexBuffer { num_indices }) => {
assert_eq!(num_indices, 500);
},
_ => panic!(),
}
}
#[test]
fn missing_usage() {
let (device, queue) = gfx_dev_and_queue!();
let buffer = CpuAccessibleBuffer::from_iter(device.clone(),
BufferUsage::vertex_buffer(),
0 .. 500u32)
.unwrap();
match check_index_buffer(&device, &buffer) {
Err(CheckIndexBufferError::BufferMissingUsage) => (),
_ => panic!(),
}
}
#[test]
fn wrong_device() {
let (dev1, queue) = gfx_dev_and_queue!();
let (dev2, _) = gfx_dev_and_queue!();
let buffer = CpuAccessibleBuffer::from_iter(dev1, BufferUsage::all(), 0 .. 500u32).unwrap();
assert_should_panic!({
let _ = check_index_buffer(&dev2, &buffer);
});
}
}