1use alloc::borrow::Cow;
4use core::mem;
5
6#[cfg_attr(not(any(feature = "spirv", doc)), expect(unused_imports))]
7use crate::ShaderSource;
8
9#[cfg(doc)]
10use crate::Device;
11
12const SPIRV_MAGIC_NUMBER: u32 = 0x0723_0203;
13
14#[cfg(feature = "spirv")] pub fn make_spirv(data: &[u8]) -> ShaderSource<'_> {
26 ShaderSource::SpirV(make_spirv_raw(data))
27}
28
29#[track_caller]
38const fn assert_has_spirv_magic_number_and_length(bytes: &[u8]) -> bool {
39 let found_magic_number: Option<bool> = match *bytes {
43 [] => panic!("byte slice is empty, not SPIR-V"),
44 [b1, b2, b3, b4, ..] => {
46 let prefix = u32::from_ne_bytes([b1, b2, b3, b4]);
47 if prefix == SPIRV_MAGIC_NUMBER {
48 Some(false)
49 } else if prefix == const { SPIRV_MAGIC_NUMBER.swap_bytes() } {
50 Some(true)
52 } else {
53 None
54 }
55 }
56 _ => None, };
58
59 match found_magic_number {
60 Some(needs_byte_swap) => {
61 assert!(
63 bytes.len() % size_of::<u32>() == 0,
64 "SPIR-V data must be a multiple of 4 bytes long"
65 );
66
67 needs_byte_swap
68 }
69 None => {
70 panic!(
71 "byte slice does not start with SPIR-V magic number. \
72 Make sure you are using a binary SPIR-V file."
73 );
74 }
75 }
76}
77
78pub fn make_spirv_raw(bytes: &[u8]) -> Cow<'_, [u32]> {
92 let needs_byte_swap = assert_has_spirv_magic_number_and_length(bytes);
93
94 let mut words: Cow<'_, [u32]> = match bytemuck::try_cast_slice(bytes) {
97 Ok(words) => Cow::Borrowed(words),
98 Err(_) => Cow::Owned(bytemuck::pod_collect_to_vec(bytes)),
100 };
101
102 if needs_byte_swap {
104 for word in Cow::to_mut(&mut words) {
105 *word = word.swap_bytes();
106 }
107 }
108
109 assert!(
110 words[0] == SPIRV_MAGIC_NUMBER,
111 "can't happen: wrong magic number after swap_bytes"
112 );
113 words
114}
115
116#[doc(hidden)]
122pub const fn make_spirv_const<const IN: usize, const OUT: usize>(bytes: [u8; IN]) -> [u32; OUT] {
123 let needs_byte_swap = assert_has_spirv_magic_number_and_length(&bytes);
124
125 assert!(mem::size_of_val(&bytes) == mem::size_of::<[u32; OUT]>());
129
130 let mut words: [u32; OUT] = unsafe { mem::transmute_copy(&bytes) };
135
136 if needs_byte_swap {
138 let mut idx = 0;
139 while idx < words.len() {
140 words[idx] = words[idx].swap_bytes();
141 idx += 1;
142 }
143 }
144
145 assert!(
146 words[0] == SPIRV_MAGIC_NUMBER,
147 "can't happen: wrong magic number after swap_bytes"
148 );
149
150 words
151}
152
153#[cfg(test)]
154mod tests {
155 use super::*;
156 use alloc::vec;
157
158 fn test_success_with_misalignments<const IN: usize, const OUT: usize>(
159 input: &[u8; IN],
160 expected: [u32; OUT],
161 ) {
162 let mut buffer = vec![0; input.len() + 4];
166 for offset in 0..4 {
167 let misaligned_slice: &mut [u8; IN] =
168 (&mut buffer[offset..][..input.len()]).try_into().unwrap();
169
170 misaligned_slice.copy_from_slice(input);
171 assert_eq!(*make_spirv_raw(misaligned_slice), expected);
172 assert_eq!(make_spirv_const(*misaligned_slice), expected);
173 }
174 }
175
176 #[test]
177 fn success_be() {
178 let input = b"\x07\x23\x02\x03\xF1\xF2\xF3\xF4";
180 let expected: [u32; 2] = [SPIRV_MAGIC_NUMBER, 0xF1F2F3F4];
181 test_success_with_misalignments(input, expected);
182 }
183
184 #[test]
185 fn success_le() {
186 let input = b"\x03\x02\x23\x07\xF1\xF2\xF3\xF4";
187 let expected: [u32; 2] = [SPIRV_MAGIC_NUMBER, 0xF4F3F2F1];
188 test_success_with_misalignments(input, expected);
189 }
190
191 #[should_panic = "multiple of 4"]
192 #[test]
193 fn nonconst_le_fail() {
194 let _: Cow<'_, [u32]> = make_spirv_raw(&[0x03, 0x02, 0x23, 0x07, 0x44, 0x33]);
195 }
196
197 #[should_panic = "multiple of 4"]
198 #[test]
199 fn nonconst_be_fail() {
200 let _: Cow<'_, [u32]> = make_spirv_raw(&[0x07, 0x23, 0x02, 0x03, 0x11, 0x22]);
201 }
202
203 #[should_panic = "multiple of 4"]
204 #[test]
205 fn const_le_fail() {
206 let _: [u32; 1] = make_spirv_const([0x03, 0x02, 0x23, 0x07, 0x44, 0x33]);
207 }
208
209 #[should_panic = "multiple of 4"]
210 #[test]
211 fn const_be_fail() {
212 let _: [u32; 1] = make_spirv_const([0x07, 0x23, 0x02, 0x03, 0x11, 0x22]);
213 }
214
215 #[should_panic = "byte slice is empty, not SPIR-V"]
216 #[test]
217 fn make_spirv_empty() {
218 let _: [u32; 0] = make_spirv_const([]);
219 }
220}