1use alloc::{vec, vec::Vec};
2
3use arrayvec::ArrayVec;
4use spirv::Word;
5
6use crate::{Handle, UniqueArena};
7
8pub(super) fn bytes_to_words(bytes: &[u8]) -> Vec<Word> {
9 bytes
10 .chunks(4)
11 .map(|chars| chars.iter().rev().fold(0u32, |u, c| (u << 8) | *c as u32))
12 .collect()
13}
14
15pub(super) fn string_to_words(input: &str) -> Vec<Word> {
16 let bytes = input.as_bytes();
17
18 str_bytes_to_words(bytes)
19}
20
21pub(super) fn str_bytes_to_words(bytes: &[u8]) -> Vec<Word> {
22 let mut words = bytes_to_words(bytes);
23 if bytes.len().is_multiple_of(4) {
24 words.push(0x0u32);
26 }
27
28 words
29}
30
31#[allow(unstable_name_collisions)]
33pub(super) fn string_to_byte_chunks(input: &str, limit: usize) -> Vec<&[u8]> {
34 let mut offset: usize = 0;
35 let mut start: usize = 0;
36 let mut words = vec![];
37 while offset < input.len() {
38 offset = input.floor_char_boundary_polyfill(offset + limit);
39 #[allow(clippy::sliced_string_as_bytes)]
42 words.push(input[start..offset].as_bytes());
43 start = offset;
44 }
45
46 words
47}
48
49pub(super) const fn map_storage_class(space: crate::AddressSpace) -> spirv::StorageClass {
50 match space {
51 crate::AddressSpace::Handle => spirv::StorageClass::UniformConstant,
52 crate::AddressSpace::Function => spirv::StorageClass::Function,
53 crate::AddressSpace::Private => spirv::StorageClass::Private,
54 crate::AddressSpace::Storage { .. } => spirv::StorageClass::StorageBuffer,
55 crate::AddressSpace::Uniform => spirv::StorageClass::Uniform,
56 crate::AddressSpace::WorkGroup => spirv::StorageClass::Workgroup,
57 crate::AddressSpace::Immediate => spirv::StorageClass::PushConstant,
58 crate::AddressSpace::TaskPayload => spirv::StorageClass::TaskPayloadWorkgroupEXT,
59 crate::AddressSpace::IncomingRayPayload | crate::AddressSpace::RayPayload => unreachable!(),
60 }
61}
62
63pub(super) fn contains_builtin(
64 binding: Option<&crate::Binding>,
65 ty: Handle<crate::Type>,
66 arena: &UniqueArena<crate::Type>,
67 built_in: crate::BuiltIn,
68) -> bool {
69 if let Some(&crate::Binding::BuiltIn(bi)) = binding {
70 bi == built_in
71 } else if let crate::TypeInner::Struct { ref members, .. } = arena[ty].inner {
72 members
73 .iter()
74 .any(|member| contains_builtin(member.binding.as_ref(), member.ty, arena, built_in))
75 } else {
76 false }
78}
79
80impl crate::AddressSpace {
81 pub(super) const fn to_spirv_semantics_and_scope(
82 self,
83 ) -> (spirv::MemorySemantics, spirv::Scope) {
84 match self {
85 Self::Storage { .. } => (spirv::MemorySemantics::empty(), spirv::Scope::Device),
86 Self::WorkGroup => (spirv::MemorySemantics::empty(), spirv::Scope::Workgroup),
87 Self::Uniform => (spirv::MemorySemantics::empty(), spirv::Scope::Device),
88 Self::Handle => (spirv::MemorySemantics::empty(), spirv::Scope::Device),
89 _ => (spirv::MemorySemantics::empty(), spirv::Scope::Invocation),
90 }
91 }
92}
93
94pub fn global_needs_wrapper(ir_module: &crate::Module, var: &crate::GlobalVariable) -> bool {
100 match var.space {
101 crate::AddressSpace::Uniform
102 | crate::AddressSpace::Storage { .. }
103 | crate::AddressSpace::Immediate => {}
104 _ => return false,
105 };
106 match ir_module.types[var.ty].inner {
107 crate::TypeInner::Struct {
108 ref members,
109 span: _,
110 } => match members.last() {
111 Some(member) => match ir_module.types[member.ty].inner {
112 crate::TypeInner::Array {
114 size: crate::ArraySize::Dynamic,
115 ..
116 } => false,
117 _ => true,
118 },
119 None => false,
120 },
121 crate::TypeInner::BindingArray { .. } => false,
122 _ => true,
124 }
125}
126
127pub fn is_uniform_matcx2_struct_member_access(
130 ir_function: &crate::Function,
131 fun_info: &crate::valid::FunctionInfo,
132 ir_module: &crate::Module,
133 pointer: Handle<crate::Expression>,
134) -> bool {
135 if let crate::TypeInner::Pointer {
136 base: pointer_base_type,
137 space: crate::AddressSpace::Uniform,
138 } = *fun_info[pointer].ty.inner_with(&ir_module.types)
139 {
140 if let crate::TypeInner::Matrix {
141 rows: crate::VectorSize::Bi,
142 ..
143 } = ir_module.types[pointer_base_type].inner
144 {
145 if let crate::Expression::AccessIndex {
146 base: parent_pointer,
147 ..
148 } = ir_function.expressions[pointer]
149 {
150 if let crate::TypeInner::Pointer {
151 base: parent_type, ..
152 } = *fun_info[parent_pointer].ty.inner_with(&ir_module.types)
153 {
154 if let crate::TypeInner::Struct { .. } = ir_module.types[parent_type].inner {
155 return true;
156 }
157 }
158 }
159 }
160 }
161
162 false
163}
164
165trait U8Internal {
168 fn is_utf8_char_boundary_polyfill(&self) -> bool;
169}
170
171impl U8Internal for u8 {
172 fn is_utf8_char_boundary_polyfill(&self) -> bool {
173 (*self as i8) >= -0x40
175 }
176}
177
178trait StrUnstable {
179 fn floor_char_boundary_polyfill(&self, index: usize) -> usize;
180}
181
182impl StrUnstable for str {
183 fn floor_char_boundary_polyfill(&self, index: usize) -> usize {
184 if index >= self.len() {
185 self.len()
186 } else {
187 let lower_bound = index.saturating_sub(3);
188 let new_index = self.as_bytes()[lower_bound..=index]
189 .iter()
190 .rposition(|b| b.is_utf8_char_boundary_polyfill());
191
192 lower_bound + new_index.unwrap()
193 }
194 }
195}
196
197pub enum BindingDecorations {
198 BuiltIn(spirv::BuiltIn, ArrayVec<spirv::Decoration, 2>),
199 Location {
200 location: u32,
201 others: ArrayVec<spirv::Decoration, 5>,
202 blend_src: Option<Word>,
204 },
205 None,
206}