1#![cfg_attr(
5 not(any(dot_out, glsl_out, hlsl_out, msl_out, spv_out, wgsl_out)),
6 allow(
7 dead_code,
8 reason = "shared helpers can be dead if none of the enabled backends need it"
9 )
10)]
11
12use alloc::string::String;
13
14#[cfg(dot_out)]
15pub mod dot;
16#[cfg(glsl_out)]
17pub mod glsl;
18#[cfg(hlsl_out)]
19pub mod hlsl;
20#[cfg(msl_out)]
21pub mod msl;
22#[cfg(spv_out)]
23pub mod spv;
24#[cfg(wgsl_out)]
25pub mod wgsl;
26
27#[cfg(any(hlsl_out, msl_out, spv_out, glsl_out))]
28pub mod pipeline_constants;
29
30#[cfg(any(hlsl_out, glsl_out))]
31mod continue_forward;
32
33pub const COMPONENTS: &[char] = &['x', 'y', 'z', 'w'];
35pub const INDENT: &str = " ";
37
38pub type NeedBakeExpressions = crate::FastHashSet<crate::Handle<crate::Expression>>;
40
41#[cfg_attr(
50 not(any(glsl_out, hlsl_out, msl_out, wgsl_out)),
51 allow(
52 dead_code,
53 reason = "shared helpers can be dead if none of the enabled backends need it"
54 )
55)]
56struct Baked(crate::Handle<crate::Expression>);
57
58impl core::fmt::Display for Baked {
59 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
60 self.0.write_prefixed(f, "_e")
61 }
62}
63
64bitflags::bitflags! {
65 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
67 #[cfg_attr(
68 not(any(hlsl_out, spv_out)),
69 allow(
70 dead_code,
71 reason = "shared helpers can be dead if none of the enabled backends need it"
72 )
73 )]
74 pub(super) struct RayQueryPoint: u32 {
75 const INITIALIZED = 1 << 0;
77 const PROCEED = 1 << 1;
79 const FINISHED_TRAVERSAL = 1 << 2;
81 }
82}
83
84pub type PipelineConstants = hashbrown::HashMap<String, f64>;
92
93#[derive(Clone, Copy)]
95pub struct Level(pub usize);
96
97impl Level {
98 pub const fn next(&self) -> Self {
99 Level(self.0 + 1)
100 }
101}
102
103impl core::fmt::Display for Level {
104 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
105 (0..self.0).try_for_each(|_| formatter.write_str(INDENT))
106 }
107}
108
109#[cfg(any(hlsl_out, msl_out))]
116fn get_entry_points(
117 module: &crate::ir::Module,
118 entry_point: Option<&(crate::ir::ShaderStage, String)>,
119) -> Result<core::ops::Range<usize>, (crate::ir::ShaderStage, String)> {
120 use alloc::borrow::ToOwned;
121
122 if let Some(&(stage, ref name)) = entry_point {
123 let Some(ep_index) = module
124 .entry_points
125 .iter()
126 .position(|ep| ep.stage == stage && ep.name == *name)
127 else {
128 return Err((stage, name.to_owned()));
129 };
130 Ok(ep_index..ep_index + 1)
131 } else {
132 Ok(0..module.entry_points.len())
133 }
134}
135
136pub enum FunctionType {
152 Function(crate::Handle<crate::Function>),
154 EntryPoint(crate::proc::EntryPointIndex),
159}
160
161impl FunctionType {
162 pub fn is_compute_like_entry_point(&self, module: &crate::Module) -> bool {
164 match *self {
165 FunctionType::EntryPoint(index) => {
166 module.entry_points[index as usize].stage.compute_like()
167 }
168 FunctionType::Function(_) => false,
169 }
170 }
171}
172
173pub struct FunctionCtx<'a> {
175 pub ty: FunctionType,
177 pub info: &'a crate::valid::FunctionInfo,
179 pub expressions: &'a crate::Arena<crate::Expression>,
181 pub named_expressions: &'a crate::NamedExpressions,
183}
184
185impl FunctionCtx<'_> {
186 pub fn resolve_type<'a>(
188 &'a self,
189 handle: crate::Handle<crate::Expression>,
190 types: &'a crate::UniqueArena<crate::Type>,
191 ) -> &'a crate::TypeInner {
192 self.info[handle].ty.inner_with(types)
193 }
194
195 pub const fn name_key(
197 &self,
198 local: crate::Handle<crate::LocalVariable>,
199 ) -> crate::proc::NameKey {
200 match self.ty {
201 FunctionType::Function(handle) => crate::proc::NameKey::FunctionLocal(handle, local),
202 FunctionType::EntryPoint(idx) => crate::proc::NameKey::EntryPointLocal(idx, local),
203 }
204 }
205
206 pub const fn argument_key(&self, arg: u32) -> crate::proc::NameKey {
211 match self.ty {
212 FunctionType::Function(handle) => crate::proc::NameKey::FunctionArgument(handle, arg),
213 FunctionType::EntryPoint(ep_index) => {
214 crate::proc::NameKey::EntryPointArgument(ep_index, arg)
215 }
216 }
217 }
218
219 pub const fn external_texture_argument_key(
226 &self,
227 arg: u32,
228 external_texture_key: crate::proc::ExternalTextureNameKey,
229 ) -> crate::proc::NameKey {
230 match self.ty {
231 FunctionType::Function(handle) => {
232 crate::proc::NameKey::ExternalTextureFunctionArgument(
233 handle,
234 arg,
235 external_texture_key,
236 )
237 }
238 #[expect(clippy::allow_attributes)]
241 #[allow(clippy::panic)]
242 FunctionType::EntryPoint(_) => {
243 panic!("External textures cannot be used as arguments to entry points")
244 }
245 }
246 }
247
248 pub fn is_fixed_function_input(
250 &self,
251 mut expression: crate::Handle<crate::Expression>,
252 module: &crate::Module,
253 ) -> Option<crate::BuiltIn> {
254 let ep_function = match self.ty {
255 FunctionType::Function(_) => return None,
256 FunctionType::EntryPoint(ep_index) => &module.entry_points[ep_index as usize].function,
257 };
258 let mut built_in = None;
259 loop {
260 match self.expressions[expression] {
261 crate::Expression::FunctionArgument(arg_index) => {
262 return match ep_function.arguments[arg_index as usize].binding {
263 Some(crate::Binding::BuiltIn(bi)) => Some(bi),
264 _ => built_in,
265 };
266 }
267 crate::Expression::AccessIndex { base, index } => {
268 match *self.resolve_type(base, &module.types) {
269 crate::TypeInner::Struct { ref members, .. } => {
270 if let Some(crate::Binding::BuiltIn(bi)) =
271 members[index as usize].binding
272 {
273 built_in = Some(bi);
274 }
275 }
276 _ => return None,
277 }
278 expression = base;
279 }
280 _ => return None,
281 }
282 }
283 }
284}
285
286impl crate::Expression {
287 pub const fn bake_ref_count(&self) -> usize {
296 match *self {
297 crate::Expression::Access { .. } | crate::Expression::AccessIndex { .. } => usize::MAX,
299 crate::Expression::ImageSample { .. } | crate::Expression::ImageLoad { .. } => 1,
301 crate::Expression::Derivative { .. } => 1,
303 crate::Expression::Load { .. } => 1,
307 _ => 2,
309 }
310 }
311}
312
313pub const fn binary_operation_str(op: crate::BinaryOperator) -> &'static str {
315 use crate::BinaryOperator as Bo;
316 match op {
317 Bo::Add => "+",
318 Bo::Subtract => "-",
319 Bo::Multiply => "*",
320 Bo::Divide => "/",
321 Bo::Modulo => "%",
322 Bo::Equal => "==",
323 Bo::NotEqual => "!=",
324 Bo::Less => "<",
325 Bo::LessEqual => "<=",
326 Bo::Greater => ">",
327 Bo::GreaterEqual => ">=",
328 Bo::And => "&",
329 Bo::ExclusiveOr => "^",
330 Bo::InclusiveOr => "|",
331 Bo::LogicalAnd => "&&",
332 Bo::LogicalOr => "||",
333 Bo::ShiftLeft => "<<",
334 Bo::ShiftRight => ">>",
335 }
336}
337
338impl crate::TypeInner {
339 pub const fn is_handle(&self) -> bool {
341 match *self {
342 Self::Image { .. } | Self::Sampler { .. } | Self::AccelerationStructure { .. } => true,
343 _ => false,
344 }
345 }
346}
347
348impl crate::Statement {
349 pub const fn is_terminator(&self) -> bool {
353 match *self {
354 crate::Statement::Break
355 | crate::Statement::Continue
356 | crate::Statement::Return { .. }
357 | crate::Statement::Kill => true,
358 _ => false,
359 }
360 }
361}
362
363bitflags::bitflags! {
364 #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
374 pub struct RayFlag: u32 {
375 const OPAQUE = 0x01;
376 const NO_OPAQUE = 0x02;
377 const TERMINATE_ON_FIRST_HIT = 0x04;
378 const SKIP_CLOSEST_HIT_SHADER = 0x08;
379 const CULL_BACK_FACING = 0x10;
380 const CULL_FRONT_FACING = 0x20;
381 const CULL_OPAQUE = 0x40;
382 const CULL_NO_OPAQUE = 0x80;
383 const SKIP_TRIANGLES = 0x100;
384 const SKIP_AABBS = 0x200;
385 }
386}
387
388#[repr(u32)]
390pub enum RayIntersectionType {
391 Triangle = 1,
392 BoundingBox = 4,
393}