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() % 4 == 0 {
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 }
60}
61
62pub(super) fn contains_builtin(
63 binding: Option<&crate::Binding>,
64 ty: Handle<crate::Type>,
65 arena: &UniqueArena<crate::Type>,
66 built_in: crate::BuiltIn,
67) -> bool {
68 if let Some(&crate::Binding::BuiltIn(bi)) = binding {
69 bi == built_in
70 } else if let crate::TypeInner::Struct { ref members, .. } = arena[ty].inner {
71 members
72 .iter()
73 .any(|member| contains_builtin(member.binding.as_ref(), member.ty, arena, built_in))
74 } else {
75 false }
77}
78
79impl crate::AddressSpace {
80 pub(super) const fn to_spirv_semantics_and_scope(
81 self,
82 ) -> (spirv::MemorySemantics, spirv::Scope) {
83 match self {
84 Self::Storage { .. } => (spirv::MemorySemantics::empty(), spirv::Scope::Device),
85 Self::WorkGroup => (spirv::MemorySemantics::empty(), spirv::Scope::Workgroup),
86 Self::Uniform => (spirv::MemorySemantics::empty(), spirv::Scope::Device),
87 Self::Handle => (spirv::MemorySemantics::empty(), spirv::Scope::Device),
88 _ => (spirv::MemorySemantics::empty(), spirv::Scope::Invocation),
89 }
90 }
91}
92
93pub fn global_needs_wrapper(ir_module: &crate::Module, var: &crate::GlobalVariable) -> bool {
99 match var.space {
100 crate::AddressSpace::Uniform
101 | crate::AddressSpace::Storage { .. }
102 | crate::AddressSpace::Immediate => {}
103 _ => return false,
104 };
105 match ir_module.types[var.ty].inner {
106 crate::TypeInner::Struct {
107 ref members,
108 span: _,
109 } => match members.last() {
110 Some(member) => match ir_module.types[member.ty].inner {
111 crate::TypeInner::Array {
113 size: crate::ArraySize::Dynamic,
114 ..
115 } => false,
116 _ => true,
117 },
118 None => false,
119 },
120 crate::TypeInner::BindingArray { .. } => false,
121 _ => true,
123 }
124}
125
126pub fn is_uniform_matcx2_struct_member_access(
129 ir_function: &crate::Function,
130 fun_info: &crate::valid::FunctionInfo,
131 ir_module: &crate::Module,
132 pointer: Handle<crate::Expression>,
133) -> bool {
134 if let crate::TypeInner::Pointer {
135 base: pointer_base_type,
136 space: crate::AddressSpace::Uniform,
137 } = *fun_info[pointer].ty.inner_with(&ir_module.types)
138 {
139 if let crate::TypeInner::Matrix {
140 rows: crate::VectorSize::Bi,
141 ..
142 } = ir_module.types[pointer_base_type].inner
143 {
144 if let crate::Expression::AccessIndex {
145 base: parent_pointer,
146 ..
147 } = ir_function.expressions[pointer]
148 {
149 if let crate::TypeInner::Pointer {
150 base: parent_type, ..
151 } = *fun_info[parent_pointer].ty.inner_with(&ir_module.types)
152 {
153 if let crate::TypeInner::Struct { .. } = ir_module.types[parent_type].inner {
154 return true;
155 }
156 }
157 }
158 }
159 }
160
161 false
162}
163
164trait U8Internal {
167 fn is_utf8_char_boundary_polyfill(&self) -> bool;
168}
169
170impl U8Internal for u8 {
171 fn is_utf8_char_boundary_polyfill(&self) -> bool {
172 (*self as i8) >= -0x40
174 }
175}
176
177trait StrUnstable {
178 fn floor_char_boundary_polyfill(&self, index: usize) -> usize;
179}
180
181impl StrUnstable for str {
182 fn floor_char_boundary_polyfill(&self, index: usize) -> usize {
183 if index >= self.len() {
184 self.len()
185 } else {
186 let lower_bound = index.saturating_sub(3);
187 let new_index = self.as_bytes()[lower_bound..=index]
188 .iter()
189 .rposition(|b| b.is_utf8_char_boundary_polyfill());
190
191 unsafe { lower_bound + new_index.unwrap_unchecked() }
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}