Skip to content

Commit

Permalink
fix crash on generating SPIR-V (still untested)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vecvec committed Mar 5, 2024
1 parent 8a7ee4f commit c6e4018
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 10 deletions.
3 changes: 2 additions & 1 deletion naga/src/back/spv/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ impl<'w> BlockContext<'w> {
if self.fun_info[expr_handle].ref_count == 0 && !is_named_expression {
return Ok(());
}

println!("{:?}", self.ir_function.expressions[expr_handle]);
println!("{:?}", self.fun_info[expr_handle].ty);
let result_type_id = self.get_expression_type_id(&self.fun_info[expr_handle].ty);
let id = match self.ir_function.expressions[expr_handle] {
crate::Expression::Literal(literal) => self.writer.get_constant_scalar(literal),
Expand Down
5 changes: 5 additions & 0 deletions naga/src/back/spv/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,7 @@ impl Writer {
.any(|arg| has_view_index_check(ir_module, arg.binding.as_ref(), arg.ty));
let has_ray_query = ir_module.special_types.ray_desc.is_some()
| ir_module.special_types.ray_intersection.is_some();
let has_vertex_return = ir_module.special_types.ray_vertex_return.is_some();

if self.physical_layout.version < 0x10300 && has_storage_buffers {
// enable the storage buffer class on < SPV-1.3
Expand All @@ -1881,6 +1882,10 @@ impl Writer {
Instruction::extension("SPV_KHR_ray_query")
.to_words(&mut self.logical_layout.extensions)
}
if has_vertex_return {
Instruction::extension("SPV_KHR_ray_tracing_position_fetch")
.to_words(&mut self.logical_layout.extensions)
}
Instruction::type_void(self.void_type).to_words(&mut self.logical_layout.declarations);
Instruction::ext_inst_import(self.gl450_ext_inst_id, "GLSL.std.450")
.to_words(&mut self.logical_layout.ext_inst_imports);
Expand Down
9 changes: 9 additions & 0 deletions naga/src/compact/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ impl<'module> ModuleTracer<'module> {
let crate::SpecialTypes {
ref ray_desc,
ref ray_intersection,
ref ray_vertex_return,
ref predeclared_types,
} = *special_types;

Expand All @@ -219,6 +220,9 @@ impl<'module> ModuleTracer<'module> {
if let Some(ray_intersection) = *ray_intersection {
self.types_used.insert(ray_intersection);
}
if let Some(ray_vertex_return) = *ray_vertex_return {
self.types_used.insert(ray_vertex_return);
}
for (_, &handle) in predeclared_types {
self.types_used.insert(handle);
}
Expand Down Expand Up @@ -278,6 +282,7 @@ impl ModuleMap {
let crate::SpecialTypes {
ref mut ray_desc,
ref mut ray_intersection,
ref mut ray_vertex_return,
ref mut predeclared_types,
} = *special;

Expand All @@ -288,6 +293,10 @@ impl ModuleMap {
self.types.adjust(ray_intersection);
}

if let Some(ref mut ray_vertex_return) = *ray_vertex_return {
self.types.adjust(ray_vertex_return);
}

for handle in predeclared_types.values_mut() {
self.types.adjust(handle);
}
Expand Down
12 changes: 9 additions & 3 deletions naga/src/front/type_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,26 @@ impl crate::Module {
}

/// Make sure the types for the vertex return are in the module's type
pub fn add_vertex_return_type(&mut self) {
pub fn generate_vertex_return_type(&mut self) -> Handle<crate::Type> {
if let Some(handle) = self.special_types.ray_vertex_return {
return handle;
}
let ty_vec3f = self.types.insert(
crate::Type {
name: None,
inner: crate::TypeInner::Vector { size: crate::VectorSize::Tri, scalar: crate::Scalar::F32 },
},
Span::UNDEFINED,
);
self.types.insert(
let array = self.types.insert(
crate::Type {
name: None,
inner: crate::TypeInner::Array { base: ty_vec3f, size: crate::ArraySize::Constant(std::num::NonZeroU32::new(3).unwrap()), stride: 16}
},
Span::UNDEFINED,
);
self.special_types.ray_vertex_return = Some(array);
array
}

/// Populate this module's [`SpecialTypes::ray_intersection`] type.
Expand All @@ -133,7 +138,8 @@ impl crate::Module {
/// [`SpecialTypes::ray_intersection`]: crate::SpecialTypes::ray_intersection
/// [`Expression::RayQueryGetIntersection`]: crate::Expression::RayQueryGetIntersection
pub fn generate_ray_intersection_type(&mut self) -> Handle<crate::Type> {
if let Some(handle) = self.special_types.ray_intersection {
if let Some(handle) = self.special_types.
ray_intersection {
return handle;
}

Expand Down
2 changes: 1 addition & 1 deletion naga/src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2276,7 +2276,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
let query = self.ray_query_pointer(args.next()?, ctx)?;
args.finish()?;

ctx.module.add_vertex_return_type();
let _ = ctx.module.generate_vertex_return_type();

crate::Expression::RayQueryVertexPositions {
query,
Expand Down
5 changes: 5 additions & 0 deletions naga/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,11 @@ pub struct SpecialTypes {
/// this if needed and return the handle.
pub ray_intersection: Option<Handle<Type>>,

/// Type for `RayVertexReturn
///
/// Call [`Module::generate_vertex_return_type`]
pub ray_vertex_return: Option<Handle<Type>>,

/// Types for predeclared wgsl types instantiated on demand.
///
/// Call [`Module::generate_predeclared_type`] to populate this if
Expand Down
10 changes: 5 additions & 5 deletions naga/src/proc/typifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,11 +885,11 @@ impl<'a> ResolveContext<'a> {
TypeResolution::Handle(result)
}
crate::Expression::RayQueryVertexPositions { .. } => {
let base = self.types.get(&crate::Type { name: None, inner: Ti::Vector {
size: crate::VectorSize::Tri,
scalar: crate::Scalar::F32
} }).ok_or(ResolveError::MissingSpecialType)?;
TypeResolution::Value(Ti::Array { size: crate::ArraySize::Constant(unsafe {NonZeroU32::new_unchecked(3)}), base, stride: 16})
let result = self
.special_types
.ray_vertex_return
.ok_or(ResolveError::MissingSpecialType)?;
TypeResolution::Handle(result)
}
})
}
Expand Down

0 comments on commit c6e4018

Please sign in to comment.