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
64pub type PipelineConstants = hashbrown::HashMap<String, f64>;
72
73#[derive(Clone, Copy)]
75pub struct Level(pub usize);
76
77impl Level {
78 pub const fn next(&self) -> Self {
79 Level(self.0 + 1)
80 }
81}
82
83impl core::fmt::Display for Level {
84 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
85 (0..self.0).try_for_each(|_| formatter.write_str(INDENT))
86 }
87}
88
89#[cfg(any(hlsl_out, msl_out))]
96fn get_entry_points(
97 module: &crate::ir::Module,
98 entry_point: Option<&(crate::ir::ShaderStage, String)>,
99) -> Result<core::ops::Range<usize>, (crate::ir::ShaderStage, String)> {
100 use alloc::borrow::ToOwned;
101
102 if let Some(&(stage, ref name)) = entry_point {
103 let Some(ep_index) = module
104 .entry_points
105 .iter()
106 .position(|ep| ep.stage == stage && ep.name == *name)
107 else {
108 return Err((stage, name.to_owned()));
109 };
110 Ok(ep_index..ep_index + 1)
111 } else {
112 Ok(0..module.entry_points.len())
113 }
114}
115
116pub enum FunctionType {
132 Function(crate::Handle<crate::Function>),
134 EntryPoint(crate::proc::EntryPointIndex),
139}
140
141impl FunctionType {
142 pub fn is_compute_entry_point(&self, module: &crate::Module) -> bool {
144 match *self {
145 FunctionType::EntryPoint(index) => {
146 module.entry_points[index as usize].stage == crate::ShaderStage::Compute
147 }
148 FunctionType::Function(_) => false,
149 }
150 }
151}
152
153pub struct FunctionCtx<'a> {
155 pub ty: FunctionType,
157 pub info: &'a crate::valid::FunctionInfo,
159 pub expressions: &'a crate::Arena<crate::Expression>,
161 pub named_expressions: &'a crate::NamedExpressions,
163}
164
165impl FunctionCtx<'_> {
166 pub fn resolve_type<'a>(
168 &'a self,
169 handle: crate::Handle<crate::Expression>,
170 types: &'a crate::UniqueArena<crate::Type>,
171 ) -> &'a crate::TypeInner {
172 self.info[handle].ty.inner_with(types)
173 }
174
175 pub const fn name_key(
177 &self,
178 local: crate::Handle<crate::LocalVariable>,
179 ) -> crate::proc::NameKey {
180 match self.ty {
181 FunctionType::Function(handle) => crate::proc::NameKey::FunctionLocal(handle, local),
182 FunctionType::EntryPoint(idx) => crate::proc::NameKey::EntryPointLocal(idx, local),
183 }
184 }
185
186 pub const fn argument_key(&self, arg: u32) -> crate::proc::NameKey {
191 match self.ty {
192 FunctionType::Function(handle) => crate::proc::NameKey::FunctionArgument(handle, arg),
193 FunctionType::EntryPoint(ep_index) => {
194 crate::proc::NameKey::EntryPointArgument(ep_index, arg)
195 }
196 }
197 }
198
199 pub const fn external_texture_argument_key(
206 &self,
207 arg: u32,
208 external_texture_key: crate::proc::ExternalTextureNameKey,
209 ) -> crate::proc::NameKey {
210 match self.ty {
211 FunctionType::Function(handle) => {
212 crate::proc::NameKey::ExternalTextureFunctionArgument(
213 handle,
214 arg,
215 external_texture_key,
216 )
217 }
218 FunctionType::EntryPoint(_) => {
219 panic!("External textures cannot be used as arguments to entry points")
220 }
221 }
222 }
223
224 pub fn is_fixed_function_input(
226 &self,
227 mut expression: crate::Handle<crate::Expression>,
228 module: &crate::Module,
229 ) -> Option<crate::BuiltIn> {
230 let ep_function = match self.ty {
231 FunctionType::Function(_) => return None,
232 FunctionType::EntryPoint(ep_index) => &module.entry_points[ep_index as usize].function,
233 };
234 let mut built_in = None;
235 loop {
236 match self.expressions[expression] {
237 crate::Expression::FunctionArgument(arg_index) => {
238 return match ep_function.arguments[arg_index as usize].binding {
239 Some(crate::Binding::BuiltIn(bi)) => Some(bi),
240 _ => built_in,
241 };
242 }
243 crate::Expression::AccessIndex { base, index } => {
244 match *self.resolve_type(base, &module.types) {
245 crate::TypeInner::Struct { ref members, .. } => {
246 if let Some(crate::Binding::BuiltIn(bi)) =
247 members[index as usize].binding
248 {
249 built_in = Some(bi);
250 }
251 }
252 _ => return None,
253 }
254 expression = base;
255 }
256 _ => return None,
257 }
258 }
259 }
260}
261
262impl crate::Expression {
263 pub const fn bake_ref_count(&self) -> usize {
272 match *self {
273 crate::Expression::Access { .. } | crate::Expression::AccessIndex { .. } => usize::MAX,
275 crate::Expression::ImageSample { .. } | crate::Expression::ImageLoad { .. } => 1,
277 crate::Expression::Derivative { .. } => 1,
279 crate::Expression::Load { .. } => 1,
283 _ => 2,
285 }
286 }
287}
288
289pub const fn binary_operation_str(op: crate::BinaryOperator) -> &'static str {
291 use crate::BinaryOperator as Bo;
292 match op {
293 Bo::Add => "+",
294 Bo::Subtract => "-",
295 Bo::Multiply => "*",
296 Bo::Divide => "/",
297 Bo::Modulo => "%",
298 Bo::Equal => "==",
299 Bo::NotEqual => "!=",
300 Bo::Less => "<",
301 Bo::LessEqual => "<=",
302 Bo::Greater => ">",
303 Bo::GreaterEqual => ">=",
304 Bo::And => "&",
305 Bo::ExclusiveOr => "^",
306 Bo::InclusiveOr => "|",
307 Bo::LogicalAnd => "&&",
308 Bo::LogicalOr => "||",
309 Bo::ShiftLeft => "<<",
310 Bo::ShiftRight => ">>",
311 }
312}
313
314impl crate::TypeInner {
315 pub const fn is_handle(&self) -> bool {
317 match *self {
318 crate::TypeInner::Image { .. }
319 | crate::TypeInner::Sampler { .. }
320 | crate::TypeInner::AccelerationStructure { .. } => true,
321 _ => false,
322 }
323 }
324}
325
326impl crate::Statement {
327 pub const fn is_terminator(&self) -> bool {
331 match *self {
332 crate::Statement::Break
333 | crate::Statement::Continue
334 | crate::Statement::Return { .. }
335 | crate::Statement::Kill => true,
336 _ => false,
337 }
338 }
339}
340
341bitflags::bitflags! {
342 #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
352 pub struct RayFlag: u32 {
353 const OPAQUE = 0x01;
354 const NO_OPAQUE = 0x02;
355 const TERMINATE_ON_FIRST_HIT = 0x04;
356 const SKIP_CLOSEST_HIT_SHADER = 0x08;
357 const CULL_BACK_FACING = 0x10;
358 const CULL_FRONT_FACING = 0x20;
359 const CULL_OPAQUE = 0x40;
360 const CULL_NO_OPAQUE = 0x80;
361 const SKIP_TRIANGLES = 0x100;
362 const SKIP_AABBS = 0x200;
363 }
364}
365
366#[repr(u32)]
368pub enum RayIntersectionType {
369 Triangle = 1,
370 BoundingBox = 4,
371}