Skip to content

Commit c34eed5

Browse files
committed
Address Jim's review notes, use typegen module for atomic struct
1 parent 024c197 commit c34eed5

File tree

9 files changed

+92
-68
lines changed

9 files changed

+92
-68
lines changed

src/back/dot/mod.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,19 +254,25 @@ impl StatementGraph {
254254
}
255255
S::RayQuery { query, ref fun } => {
256256
self.dependencies.push((id, query, "query"));
257-
if let crate::RayQueryFunction::Initialize {
258-
acceleration_structure,
259-
descriptor,
260-
} = *fun
261-
{
262-
self.dependencies.push((
263-
id,
257+
match *fun {
258+
crate::RayQueryFunction::Initialize {
264259
acceleration_structure,
265-
"acceleration_structure",
266-
));
267-
self.dependencies.push((id, descriptor, "descriptor"));
260+
descriptor,
261+
} => {
262+
self.dependencies.push((
263+
id,
264+
acceleration_structure,
265+
"acceleration_structure",
266+
));
267+
self.dependencies.push((id, descriptor, "descriptor"));
268+
"RayQueryInitialize"
269+
}
270+
crate::RayQueryFunction::Proceed { result } => {
271+
self.emits.push((id, result));
272+
"RayQueryProceed"
273+
}
274+
crate::RayQueryFunction::Terminate => "RayQueryTerminate",
268275
}
269-
"RayQuery"
270276
}
271277
};
272278
// Set the last node to the merge node

src/back/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ bitflags::bitflags! {
234234
const NO_OPAQUE = 0x02;
235235
const TERMINATE_ON_FIRST_HIT = 0x04;
236236
const SKIP_CLOSEST_HIT_SHADER = 0x08;
237-
const CULL_FRONT_FACING = 0x10;
238-
const CULL_BACK_FACING = 0x20;
237+
const CULL_BACK_FACING = 0x10;
238+
const CULL_FRONT_FACING = 0x20;
239239
const CULL_OPAQUE = 0x40;
240240
const CULL_NO_OPAQUE = 0x80;
241241
const SKIP_TRIANGLES = 0x100;

src/back/spv/writer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ impl Writer {
973973

974974
self.write_type_declaration_local(id, local);
975975

976-
// If it's an type that needs SPIR-V capabilities, request them now,
976+
// If it's a type that needs SPIR-V capabilities, request them now,
977977
// so write_type_declaration_local can stay infallible.
978978
self.request_type_capabilities(&ty.inner)?;
979979

src/front/type_gen.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,55 @@ Type generators.
55
use crate::{arena::Handle, span::Span};
66

77
impl crate::Module {
8+
pub fn generate_atomic_compare_exchange_result(
9+
&mut self,
10+
kind: crate::ScalarKind,
11+
width: crate::Bytes,
12+
) -> Handle<crate::Type> {
13+
let bool_ty = self.types.insert(
14+
crate::Type {
15+
name: None,
16+
inner: crate::TypeInner::Scalar {
17+
kind: crate::ScalarKind::Bool,
18+
width: crate::BOOL_WIDTH,
19+
},
20+
},
21+
Span::UNDEFINED,
22+
);
23+
let scalar_ty = self.types.insert(
24+
crate::Type {
25+
name: None,
26+
inner: crate::TypeInner::Scalar { kind, width },
27+
},
28+
Span::UNDEFINED,
29+
);
30+
31+
self.types.insert(
32+
crate::Type {
33+
name: Some(format!(
34+
"__atomic_compare_exchange_result<{kind:?},{width}>"
35+
)),
36+
inner: crate::TypeInner::Struct {
37+
members: vec![
38+
crate::StructMember {
39+
name: Some("old_value".to_string()),
40+
ty: scalar_ty,
41+
binding: None,
42+
offset: 0,
43+
},
44+
crate::StructMember {
45+
name: Some("exchanged".to_string()),
46+
ty: bool_ty,
47+
binding: None,
48+
offset: 4,
49+
},
50+
],
51+
span: 8,
52+
},
53+
},
54+
Span::UNDEFINED,
55+
)
56+
}
857
/// Populate this module's [`SpecialTypes::ray_desc`] type.
958
///
1059
/// [`SpecialTypes::ray_desc`] is the type of the [`descriptor`] operand of
@@ -20,7 +69,7 @@ impl crate::Module {
2069
/// [`Initialize`]: crate::RayQueryFunction::Initialize
2170
/// [`RayQuery`]: crate::Statement::RayQuery
2271
/// [`RayQueryFunction::Initialize`]: crate::RayQueryFunction::Initialize
23-
pub(super) fn generate_ray_desc_type(&mut self) -> Handle<crate::Type> {
72+
pub fn generate_ray_desc_type(&mut self) -> Handle<crate::Type> {
2473
if let Some(handle) = self.special_types.ray_desc {
2574
return handle;
2675
}
@@ -122,7 +171,7 @@ impl crate::Module {
122171
///
123172
/// [`SpecialTypes::ray_intersection`]: crate::SpecialTypes::ray_intersection
124173
/// [`Expression::RayQueryGetIntersection`]: crate::Expression::RayQueryGetIntersection
125-
pub(super) fn generate_ray_intersection_type(&mut self) -> Handle<crate::Type> {
174+
pub fn generate_ray_intersection_type(&mut self) -> Handle<crate::Type> {
126175
if let Some(handle) = self.special_types.ray_intersection {
127176
return handle;
128177
}

src/front/wgsl/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ pub enum Error<'a> {
188188
MissingAttribute(&'static str, Span),
189189
InvalidAtomicPointer(Span),
190190
InvalidAtomicOperandType(Span),
191+
InvalidRayQueryPointer(Span),
191192
Pointer(&'static str, Span),
192193
NotPointer(Span),
193194
NotReference(&'static str, Span),
@@ -526,6 +527,11 @@ impl<'a> Error<'a> {
526527
labels: vec![(span, "atomic operand type is invalid".into())],
527528
notes: vec![],
528529
},
530+
Error::InvalidRayQueryPointer(span) => ParseError {
531+
message: "ray query operation is done on a pointer to a non-ray-query".to_string(),
532+
labels: vec![(span, "ray query pointer is invalid".into())],
533+
notes: vec![],
534+
},
529535
Error::NotPointer(span) => ParseError {
530536
message: "the operand of the `*` operator must be a pointer".to_string(),
531537
labels: vec![(span, "expression is not a pointer".into())],

src/front/wgsl/lower/mod.rs

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,8 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
641641
let span = tu.decls.get_span(decl_handle);
642642
let decl = &tu.decls[decl_handle];
643643

644+
//NOTE: This is done separately from `resolve_ast_type` because `RayDesc` may be
645+
// first encountered in a local constructor invocation.
644646
//TODO: find a nicer way?
645647
if let Some(dep) = decl.dependencies.iter().find(|dep| dep.ident == "RayDesc") {
646648
let ty_handle = ctx.module.generate_ray_desc_type();
@@ -1733,50 +1735,11 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
17331735

17341736
let expression = match *ctx.resolved_inner(value) {
17351737
crate::TypeInner::Scalar { kind, width } => {
1736-
let bool_ty = ctx.module.types.insert(
1737-
crate::Type {
1738-
name: None,
1739-
inner: crate::TypeInner::Scalar {
1740-
kind: crate::ScalarKind::Bool,
1741-
width: crate::BOOL_WIDTH,
1742-
},
1743-
},
1744-
Span::UNDEFINED,
1745-
);
1746-
let scalar_ty = ctx.module.types.insert(
1747-
crate::Type {
1748-
name: None,
1749-
inner: crate::TypeInner::Scalar { kind, width },
1750-
},
1751-
Span::UNDEFINED,
1752-
);
1753-
let struct_ty = ctx.module.types.insert(
1754-
crate::Type {
1755-
name: Some(
1756-
"__atomic_compare_exchange_result".to_string(),
1757-
),
1758-
inner: crate::TypeInner::Struct {
1759-
members: vec![
1760-
crate::StructMember {
1761-
name: Some("old_value".to_string()),
1762-
ty: scalar_ty,
1763-
binding: None,
1764-
offset: 0,
1765-
},
1766-
crate::StructMember {
1767-
name: Some("exchanged".to_string()),
1768-
ty: bool_ty,
1769-
binding: None,
1770-
offset: 4,
1771-
},
1772-
],
1773-
span: 8,
1774-
},
1775-
},
1776-
Span::UNDEFINED,
1777-
);
17781738
crate::Expression::AtomicResult {
1779-
ty: struct_ty,
1739+
//TODO: cache this to avoid generating duplicate types
1740+
ty: ctx
1741+
.module
1742+
.generate_atomic_compare_exchange_result(kind, width),
17801743
comparison: true,
17811744
}
17821745
}
@@ -2449,12 +2412,12 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
24492412
crate::TypeInner::RayQuery => Ok(pointer),
24502413
ref other => {
24512414
log::error!("Pointer type to {:?} passed to ray query op", other);
2452-
Err(Error::InvalidAtomicPointer(span))
2415+
Err(Error::InvalidRayQueryPointer(span))
24532416
}
24542417
},
24552418
ref other => {
24562419
log::error!("Type {:?} passed to ray query op", other);
2457-
Err(Error::InvalidAtomicPointer(span))
2420+
Err(Error::InvalidRayQueryPointer(span))
24582421
}
24592422
}
24602423
}

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,7 @@ pub enum Expression {
14601460

14611461
/// Return an intersection found by `query`.
14621462
///
1463-
/// If `committed` is true, return the committed result available when
1463+
/// If `committed` is true, return the committed result available when
14641464
RayQueryGetIntersection {
14651465
query: Handle<Expression>,
14661466
committed: bool,
@@ -1848,13 +1848,13 @@ pub struct SpecialTypes {
18481848
///
18491849
/// Call [`Module::generate_ray_desc_type`] to populate this if
18501850
/// needed and return the handle.
1851-
ray_desc: Option<Handle<Type>>,
1851+
pub ray_desc: Option<Handle<Type>>,
18521852

18531853
/// Type for `RayIntersection`.
18541854
///
18551855
/// Call [`Module::generate_ray_intersection_type`] to populate
18561856
/// this if needed and return the handle.
1857-
ray_intersection: Option<Handle<Type>>,
1857+
pub ray_intersection: Option<Handle<Type>>,
18581858
}
18591859

18601860
/// Shader module.

tests/in/ray-query.wgsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ let RAY_FLAG_OPAQUE = 0x01u;
77
let RAY_FLAG_NO_OPAQUE = 0x02u;
88
let RAY_FLAG_TERMINATE_ON_FIRST_HIT = 0x04u;
99
let RAY_FLAG_SKIP_CLOSEST_HIT_SHADER = 0x08u;
10-
let RAY_FLAG_CULL_FRONT_FACING = 0x10u;
11-
let RAY_FLAG_CULL_BACK_FACING = 0x20u;
10+
let RAY_FLAG_CULL_BACK_FACING = 0x10u;
11+
let RAY_FLAG_CULL_FRONT_FACING = 0x20u;
1212
let RAY_FLAG_CULL_OPAQUE = 0x40u;
1313
let RAY_FLAG_CULL_NO_OPAQUE = 0x80u;
1414
let RAY_FLAG_SKIP_TRIANGLES = 0x100u;

tests/out/wgsl/atomicCompareExchange.wgsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
struct gen___atomic_compare_exchange_result {
1+
struct gen___atomic_compare_exchange_resultSint4_ {
22
old_value: i32,
33
exchanged: bool,
44
}
55

6-
struct gen___atomic_compare_exchange_result_1 {
6+
struct gen___atomic_compare_exchange_resultUint4_ {
77
old_value: u32,
88
exchanged: bool,
99
}

0 commit comments

Comments
 (0)