Skip to content

Commit

Permalink
Address Jim's review notes, use typegen module for atomic struct
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Mar 22, 2023
1 parent b52dd0f commit 85383aa
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 68 deletions.
28 changes: 17 additions & 11 deletions src/back/dot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,25 @@ impl StatementGraph {
}
S::RayQuery { query, ref fun } => {
self.dependencies.push((id, query, "query"));
if let crate::RayQueryFunction::Initialize {
acceleration_structure,
descriptor,
} = *fun
{
self.dependencies.push((
id,
match *fun {
crate::RayQueryFunction::Initialize {
acceleration_structure,
"acceleration_structure",
));
self.dependencies.push((id, descriptor, "descriptor"));
descriptor,
} => {
self.dependencies.push((
id,
acceleration_structure,
"acceleration_structure",
));
self.dependencies.push((id, descriptor, "descriptor"));
"RayQueryInitialize"
}
crate::RayQueryFunction::Proceed { result } => {
self.emits.push((id, result));
"RayQueryProceed"
}
crate::RayQueryFunction::Terminate => "RayQueryTerminate",
}
"RayQuery"
}
};
// Set the last node to the merge node
Expand Down
4 changes: 2 additions & 2 deletions src/back/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ bitflags::bitflags! {
const NO_OPAQUE = 0x02;
const TERMINATE_ON_FIRST_HIT = 0x04;
const SKIP_CLOSEST_HIT_SHADER = 0x08;
const CULL_FRONT_FACING = 0x10;
const CULL_BACK_FACING = 0x20;
const CULL_BACK_FACING = 0x10;
const CULL_FRONT_FACING = 0x20;
const CULL_OPAQUE = 0x40;
const CULL_NO_OPAQUE = 0x80;
const SKIP_TRIANGLES = 0x100;
Expand Down
2 changes: 1 addition & 1 deletion src/back/spv/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ impl Writer {

self.write_type_declaration_local(id, local);

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

Expand Down
53 changes: 51 additions & 2 deletions src/front/type_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,55 @@ Type generators.
use crate::{arena::Handle, span::Span};

impl crate::Module {
pub fn generate_atomic_compare_exchange_result(
&mut self,
kind: crate::ScalarKind,
width: crate::Bytes,
) -> Handle<crate::Type> {
let bool_ty = self.types.insert(
crate::Type {
name: None,
inner: crate::TypeInner::Scalar {
kind: crate::ScalarKind::Bool,
width: crate::BOOL_WIDTH,
},
},
Span::UNDEFINED,
);
let scalar_ty = self.types.insert(
crate::Type {
name: None,
inner: crate::TypeInner::Scalar { kind, width },
},
Span::UNDEFINED,
);

self.types.insert(
crate::Type {
name: Some(format!(
"__atomic_compare_exchange_result<{kind:?},{width}>"
)),
inner: crate::TypeInner::Struct {
members: vec![
crate::StructMember {
name: Some("old_value".to_string()),
ty: scalar_ty,
binding: None,
offset: 0,
},
crate::StructMember {
name: Some("exchanged".to_string()),
ty: bool_ty,
binding: None,
offset: 4,
},
],
span: 8,
},
},
Span::UNDEFINED,
)
}
/// Populate this module's [`SpecialTypes::ray_desc`] type.
///
/// [`SpecialTypes::ray_desc`] is the type of the [`descriptor`] operand of
Expand All @@ -20,7 +69,7 @@ impl crate::Module {
/// [`Initialize`]: crate::RayQueryFunction::Initialize
/// [`RayQuery`]: crate::Statement::RayQuery
/// [`RayQueryFunction::Initialize`]: crate::RayQueryFunction::Initialize
pub(super) fn generate_ray_desc_type(&mut self) -> Handle<crate::Type> {
pub fn generate_ray_desc_type(&mut self) -> Handle<crate::Type> {
if let Some(handle) = self.special_types.ray_desc {
return handle;
}
Expand Down Expand Up @@ -122,7 +171,7 @@ impl crate::Module {
///
/// [`SpecialTypes::ray_intersection`]: crate::SpecialTypes::ray_intersection
/// [`Expression::RayQueryGetIntersection`]: crate::Expression::RayQueryGetIntersection
pub(super) fn generate_ray_intersection_type(&mut self) -> Handle<crate::Type> {
pub fn generate_ray_intersection_type(&mut self) -> Handle<crate::Type> {
if let Some(handle) = self.special_types.ray_intersection {
return handle;
}
Expand Down
6 changes: 6 additions & 0 deletions src/front/wgsl/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ pub enum Error<'a> {
MissingAttribute(&'static str, Span),
InvalidAtomicPointer(Span),
InvalidAtomicOperandType(Span),
InvalidRayQueryPointer(Span),
Pointer(&'static str, Span),
NotPointer(Span),
NotReference(&'static str, Span),
Expand Down Expand Up @@ -526,6 +527,11 @@ impl<'a> Error<'a> {
labels: vec![(span, "atomic operand type is invalid".into())],
notes: vec![],
},
Error::InvalidRayQueryPointer(span) => ParseError {
message: "ray query operation is done on a pointer to a non-ray-query".to_string(),
labels: vec![(span, "ray query pointer is invalid".into())],
notes: vec![],
},
Error::NotPointer(span) => ParseError {
message: "the operand of the `*` operator must be a pointer".to_string(),
labels: vec![(span, "expression is not a pointer".into())],
Expand Down
53 changes: 8 additions & 45 deletions src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
let span = tu.decls.get_span(decl_handle);
let decl = &tu.decls[decl_handle];

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

let expression = match *ctx.resolved_inner(value) {
crate::TypeInner::Scalar { kind, width } => {
let bool_ty = ctx.module.types.insert(
crate::Type {
name: None,
inner: crate::TypeInner::Scalar {
kind: crate::ScalarKind::Bool,
width: crate::BOOL_WIDTH,
},
},
Span::UNDEFINED,
);
let scalar_ty = ctx.module.types.insert(
crate::Type {
name: None,
inner: crate::TypeInner::Scalar { kind, width },
},
Span::UNDEFINED,
);
let struct_ty = ctx.module.types.insert(
crate::Type {
name: Some(
"__atomic_compare_exchange_result".to_string(),
),
inner: crate::TypeInner::Struct {
members: vec![
crate::StructMember {
name: Some("old_value".to_string()),
ty: scalar_ty,
binding: None,
offset: 0,
},
crate::StructMember {
name: Some("exchanged".to_string()),
ty: bool_ty,
binding: None,
offset: 4,
},
],
span: 8,
},
},
Span::UNDEFINED,
);
crate::Expression::AtomicResult {
ty: struct_ty,
//TODO: cache this to avoid generating duplicate types
ty: ctx
.module
.generate_atomic_compare_exchange_result(kind, width),
comparison: true,
}
}
Expand Down Expand Up @@ -2449,12 +2412,12 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
crate::TypeInner::RayQuery => Ok(pointer),
ref other => {
log::error!("Pointer type to {:?} passed to ray query op", other);
Err(Error::InvalidAtomicPointer(span))
Err(Error::InvalidRayQueryPointer(span))
}
},
ref other => {
log::error!("Type {:?} passed to ray query op", other);
Err(Error::InvalidAtomicPointer(span))
Err(Error::InvalidRayQueryPointer(span))
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,7 @@ pub enum Expression {

/// Return an intersection found by `query`.
///
/// If `committed` is true, return the committed result available when
/// If `committed` is true, return the committed result available when
RayQueryGetIntersection {
query: Handle<Expression>,
committed: bool,
Expand Down Expand Up @@ -1848,13 +1848,13 @@ pub struct SpecialTypes {
///
/// Call [`Module::generate_ray_desc_type`] to populate this if
/// needed and return the handle.
ray_desc: Option<Handle<Type>>,
pub ray_desc: Option<Handle<Type>>,

/// Type for `RayIntersection`.
///
/// Call [`Module::generate_ray_intersection_type`] to populate
/// this if needed and return the handle.
ray_intersection: Option<Handle<Type>>,
pub ray_intersection: Option<Handle<Type>>,
}

/// Shader module.
Expand Down
4 changes: 2 additions & 2 deletions tests/in/ray-query.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ let RAY_FLAG_OPAQUE = 0x01u;
let RAY_FLAG_NO_OPAQUE = 0x02u;
let RAY_FLAG_TERMINATE_ON_FIRST_HIT = 0x04u;
let RAY_FLAG_SKIP_CLOSEST_HIT_SHADER = 0x08u;
let RAY_FLAG_CULL_FRONT_FACING = 0x10u;
let RAY_FLAG_CULL_BACK_FACING = 0x20u;
let RAY_FLAG_CULL_BACK_FACING = 0x10u;
let RAY_FLAG_CULL_FRONT_FACING = 0x20u;
let RAY_FLAG_CULL_OPAQUE = 0x40u;
let RAY_FLAG_CULL_NO_OPAQUE = 0x80u;
let RAY_FLAG_SKIP_TRIANGLES = 0x100u;
Expand Down
4 changes: 2 additions & 2 deletions tests/out/wgsl/atomicCompareExchange.wgsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
struct gen___atomic_compare_exchange_result {
struct gen___atomic_compare_exchange_resultSint4_ {
old_value: i32,
exchanged: bool,
}

struct gen___atomic_compare_exchange_result_1 {
struct gen___atomic_compare_exchange_resultUint4_ {
old_value: u32,
exchanged: bool,
}
Expand Down

0 comments on commit 85383aa

Please sign in to comment.