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
#![deny(missing_docs)]
pub mod allocator;
#[cfg(feature = "loading")]
pub mod loading;
#[cfg(feature = "surface")]
pub mod surface;
use crate::{vk1_0::Result as RawResult, vk1_1::*, CoreLoader};
use std::{
convert::TryInto,
error::Error,
fmt::{self, Debug, Display},
io,
};
const SPV_MAGIC_NUMBER_LE: u32 = 0x07230203;
const SPV_MAGIC_NUMBER_BE: u32 = SPV_MAGIC_NUMBER_LE.swap_bytes();
impl<T> CoreLoader<T> {
pub fn instance_version(&mut self) -> u32 {
if self.vk1_1.is_none() {
let _ = self.load_vk1_1();
}
if self.vk1_1.is_some() {
unsafe { self.enumerate_instance_version(None) }.unwrap()
} else {
crate::make_version(1, 0, 0)
}
}
}
#[must_use = "this `VulkanResult` may be an error, which should be handled"]
#[derive(Copy, Clone, Default)]
pub struct VulkanResult<T> {
pub raw: RawResult,
pub value: Option<T>,
}
impl<T> VulkanResult<T> {
#[inline]
pub fn new(raw: RawResult, value: T) -> VulkanResult<T> {
let value = if raw.0.is_negative() {
None
} else {
Some(value)
};
VulkanResult { raw, value }
}
#[inline]
pub fn unwrap(self) -> T {
match self.value {
Some(value) => value,
None => panic!("{:?}", self.raw),
}
}
#[inline]
pub fn expect(self, msg: impl Display) -> T {
match self.value {
Some(value) => value,
None => panic!("{:?}: {}", self.raw, msg),
}
}
#[inline]
pub fn as_ref(&self) -> VulkanResult<&T> {
VulkanResult {
raw: self.raw.clone(),
value: self.value.as_ref(),
}
}
#[inline]
pub fn as_mut(&mut self) -> VulkanResult<&mut T> {
VulkanResult {
raw: self.raw.clone(),
value: self.value.as_mut(),
}
}
#[inline]
pub fn new_ok(value: T) -> VulkanResult<T> {
VulkanResult::new(RawResult::SUCCESS, value)
}
#[inline]
pub fn new_err(raw: RawResult) -> VulkanResult<T> {
VulkanResult { raw, value: None }
}
#[inline]
pub fn ok(self) -> Option<T> {
self.value
}
#[inline]
pub fn result(self) -> Result<T, RawResult> {
self.value.ok_or(self.raw)
}
#[inline]
pub fn is_ok(&self) -> bool {
self.value.is_some()
}
#[inline]
pub fn raw_is_ok(&self) -> bool {
self.raw.0.is_positive()
}
#[inline]
pub fn is_err(&self) -> bool {
self.value.is_none()
}
#[inline]
pub fn raw_is_err(&self) -> bool {
self.raw.0.is_negative()
}
}
impl Display for RawResult {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
Debug::fmt(self, fmt)
}
}
impl Error for RawResult {}
impl<T> Debug for VulkanResult<T>
where
T: Debug,
{
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match &self.value {
Some(value) => write!(fmt, "{:?}: {:?}", self.raw, value),
None => write!(fmt, "{:?}: (no value)", self.raw),
}
}
}
impl<T> Display for VulkanResult<T>
where
T: Display,
{
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match &self.value {
Some(value) => write!(fmt, "{:?}: {}", self.raw, value),
None => write!(fmt, "{:?}: (no value)", self.raw),
}
}
}
pub fn decode_spv(bytes: &[u8]) -> io::Result<Vec<u32>> {
if bytes.len() % 4 != 0 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"`bytes` is not 4-byte aligned",
));
}
let mut words: Vec<_> = bytes
.chunks(4)
.map(|word| u32::from_le_bytes(word.try_into().unwrap()))
.collect();
match words.get(0) {
Some(&SPV_MAGIC_NUMBER_LE) => (),
Some(&SPV_MAGIC_NUMBER_BE) => {
for word in &mut words {
*word = word.swap_bytes();
}
}
_ => {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"`bytes` is not valid SPIR-V data",
))
}
}
Ok(words)
}