Skip to content

Commit

Permalink
Add support for SPV_KHR_ray_tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
XAMPPRocky committed Apr 16, 2021
1 parent 6c8dece commit 1f51975
Show file tree
Hide file tree
Showing 24 changed files with 470 additions and 72 deletions.
43 changes: 22 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/rustc_codegen_spirv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ syn = { version = "1", features = ["visit", "visit-mut"] }
# Normal dependencies.
bimap = "0.6"
indexmap = "1.6.0"
rspirv = { git = "https://github.com/gfx-rs/rspirv.git", rev = "ee1e913" }
rspirv = { git = "https://github.com/gfx-rs/rspirv.git", rev = "719cf08" }
rustc-demangle = "0.1.18"
sanitize-filename = "0.3"
serde = { version = "1.0", features = ["derive"] }
Expand Down
3 changes: 3 additions & 0 deletions crates/rustc_codegen_spirv/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,9 @@ fn trans_intrinsic_type<'tcx>(
}
Ok(SpirvType::Sampler.def(span, cx))
}
IntrinsicType::AccelerationStructureKhr => {
Ok(SpirvType::AccelerationStructureKhr.def(span, cx))
}
IntrinsicType::SampledImage => {
// see SpirvType::sizeof
if ty.size != Size::from_bytes(4) {
Expand Down
1 change: 1 addition & 0 deletions crates/rustc_codegen_spirv/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub enum IntrinsicType {
access_qualifier: Option<AccessQualifier>,
},
Sampler,
AccelerationStructureKhr,
SampledImage,
}

Expand Down
6 changes: 6 additions & 0 deletions crates/rustc_codegen_spirv/src/builder/builder_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
SpirvType::Sampler => self.fatal("cannot memset sampler"),
SpirvType::SampledImage { .. } => self.fatal("cannot memset sampled image"),
SpirvType::InterfaceBlock { .. } => self.fatal("cannot memset interface block"),
SpirvType::AccelerationStructureKhr => {
self.fatal("cannot memset acceleration structure")
}
}
}

Expand Down Expand Up @@ -293,6 +296,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
SpirvType::Sampler => self.fatal("cannot memset sampler"),
SpirvType::SampledImage { .. } => self.fatal("cannot memset sampled image"),
SpirvType::InterfaceBlock { .. } => self.fatal("cannot memset interface block"),
SpirvType::AccelerationStructureKhr => {
self.fatal("cannot memset acceleration structure")
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/rustc_codegen_spirv/src/codegen_cx/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ impl<'tcx> CodegenCx<'tcx> {
.tcx
.sess
.fatal("Cannot create a constant interface block value"),
SpirvType::AccelerationStructureKhr => self
.tcx
.sess
.fatal("Cannot create a constant acceleration structure"),
}
}
}
5 changes: 4 additions & 1 deletion crates/rustc_codegen_spirv/src/codegen_cx/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ impl<'tcx> CodegenCx<'tcx> {
let spirv_ty = self.layout_of(value_ty).spirv_type(hir_param.ty_span, self);
// Some types automatically specify a storage class. Compute that here.
let inferred_storage_class_from_ty = match self.lookup_type(spirv_ty) {
SpirvType::Image { .. } | SpirvType::Sampler | SpirvType::SampledImage { .. } => {
SpirvType::Image { .. }
| SpirvType::Sampler
| SpirvType::SampledImage { .. }
| SpirvType::AccelerationStructureKhr => {
if is_ref {
Some(StorageClass::UniformConstant)
} else {
Expand Down
8 changes: 5 additions & 3 deletions crates/rustc_codegen_spirv/src/codegen_cx/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,11 @@ impl<'tcx> BaseTypeMethods<'tcx> for CodegenCx<'tcx> {
SpirvType::Function { .. } => TypeKind::Function,
// HACK(eddyb) this is probably the closest `TypeKind` (which is still
// very much LLVM-specific, sadly) has to offer to "resource handle".
SpirvType::Image { .. } | SpirvType::Sampler | SpirvType::SampledImage { .. } => {
TypeKind::Token
}
| SpirvType::Image { .. }
| SpirvType::Sampler
| SpirvType::SampledImage { .. }
| SpirvType::AccelerationStructureKhr
=> TypeKind::Token,
}
}
fn type_ptr_to(&self, ty: Self::Type) -> Self::Type {
Expand Down
12 changes: 9 additions & 3 deletions crates/rustc_codegen_spirv/src/linker/new_structurizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ struct Region {
/// After structurizing a region, all paths through it must lead to a single
/// "merge" block (i.e. `merge` post-dominates the entire region).
/// The `merge` block must be terminated by one of `OpReturn`, `OpReturnValue`,
/// `OpKill`, or `OpUnreachable`. If `exits` isn't empty, `merge` will
/// receive an `OpBranch` from its parent region (to an outer merge block).
/// `OpKill`, `OpIgnoreIntersectionKHR`, `OpTerminateRayKHR` or
/// `OpUnreachable`. If `exits` isn't empty, `merge` will receive an
/// `OpBranch` from its parent region (to an outer merge block).
merge: BlockIdx,
merge_id: BlockId,

Expand Down Expand Up @@ -168,7 +169,12 @@ impl Structurizer<'_> {
let block_id = self.func.blocks()[block].label_id().unwrap();
let terminator = self.func.blocks()[block].instructions.last().unwrap();
let mut region = match terminator.class.opcode {
Op::Return | Op::ReturnValue | Op::Kill | Op::Unreachable => Region {
Op::Return
| Op::ReturnValue
| Op::Kill
| Op::IgnoreIntersectionKHR
| Op::TerminateRayKHR
| Op::Unreachable => Region {
merge: block,
merge_id: block_id,
exits: indexmap! {},
Expand Down
7 changes: 6 additions & 1 deletion crates/rustc_codegen_spirv/src/linker/simple_passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ pub fn outgoing_edges(block: &Block) -> impl Iterator<Item = Word> + '_ {
Op::Branch => (0..1).step_by(1),
Op::BranchConditional => (1..3).step_by(1),
Op::Switch => (1..terminator.operands.len()).step_by(2),
Op::Return | Op::ReturnValue | Op::Kill | Op::Unreachable => (0..0).step_by(1),
Op::Return
| Op::ReturnValue
| Op::Kill
| Op::Unreachable
| Op::IgnoreIntersectionKHR
| Op::TerminateRayKHR => (0..0).step_by(1),
_ => panic!("Invalid block terminator: {:?}", terminator),
};
operand_indices.map(move |i| terminator.operands[i].unwrap_id_ref())
Expand Down
19 changes: 13 additions & 6 deletions crates/rustc_codegen_spirv/src/spirv_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub enum SpirvType {
InterfaceBlock {
inner_type: Word,
},

AccelerationStructureKhr,
}

impl SpirvType {
Expand Down Expand Up @@ -248,6 +250,7 @@ impl SpirvType {
access_qualifier,
),
Self::Sampler => cx.emit_global().type_sampler(),
Self::AccelerationStructureKhr => cx.emit_global().type_acceleration_structure_khr(),
Self::SampledImage { image_type } => cx.emit_global().type_sampled_image(image_type),

Self::InterfaceBlock { inner_type } => {
Expand Down Expand Up @@ -347,7 +350,10 @@ impl SpirvType {
cx.lookup_type(element).sizeof(cx)? * cx.builder.lookup_const_u64(count).unwrap()
}
Self::Pointer { .. } => cx.tcx.data_layout.pointer_size,
Self::Image { .. } | Self::Sampler | Self::SampledImage { .. } => Size::from_bytes(4),
Self::Image { .. }
| Self::AccelerationStructureKhr
| Self::Sampler
| Self::SampledImage { .. } => Size::from_bytes(4),

Self::InterfaceBlock { inner_type } => cx.lookup_type(inner_type).sizeof(cx)?,
};
Expand Down Expand Up @@ -375,9 +381,10 @@ impl SpirvType {
cx.lookup_type(element).alignof(cx)
}
Self::Pointer { .. } => cx.tcx.data_layout.pointer_align.abi,
Self::Image { .. } | Self::Sampler | Self::SampledImage { .. } => {
Align::from_bytes(4).unwrap()
}
Self::Image { .. }
| Self::AccelerationStructureKhr
| Self::Sampler
| Self::SampledImage { .. } => Align::from_bytes(4).unwrap(),

Self::InterfaceBlock { inner_type } => cx.lookup_type(inner_type).alignof(cx),
}
Expand Down Expand Up @@ -516,12 +523,12 @@ impl fmt::Debug for SpirvTypePrinter<'_, '_> {
.field("id", &self.id)
.field("image_type", &self.cx.debug_type(image_type))
.finish(),

SpirvType::InterfaceBlock { inner_type } => f
.debug_struct("InterfaceBlock")
.field("id", &self.id)
.field("inner_type", &self.cx.debug_type(inner_type))
.finish(),
SpirvType::AccelerationStructureKhr => f.debug_struct("AccelerationStructure").finish(),
};
{
let mut debug_stack = DEBUG_STACK.lock().unwrap();
Expand Down Expand Up @@ -671,12 +678,12 @@ impl SpirvTypePrinter<'_, '_> {
.debug_struct("SampledImage")
.field("image_type", &self.cx.debug_type(image_type))
.finish(),

SpirvType::InterfaceBlock { inner_type } => {
f.write_str("interface block { ")?;
ty(self.cx, stack, f, inner_type)?;
f.write_str(" }")
}
SpirvType::AccelerationStructureKhr => f.write_str("AccelerationStructureKhr"),
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions crates/rustc_codegen_spirv/src/spirv_type_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,19 +723,20 @@ pub fn instruction_signatures(op: Op) -> Option<&'static [InstSig<'static>]> {

// 3.37.25. Reserved Instructions
// SPV_KHR_ray_tracing
// NOTE(eddyb) we actually use these despite not being in the standard yet.
Op::TraceRayKHR
| Op::ExecuteCallableKHR
| Op::ConvertUToAccelerationStructureKHR
| Op::IgnoreIntersectionKHR
| Op::TerminateRayKHR => reserved!(SPV_KHR_ray_tracing),
| Op::TerminateRayKHR => {} // reserved!(SPV_KHR_ray_tracing),
// SPV_KHR_ray_query
Op::TypeRayQueryKHR
| Op::RayQueryInitializeKHR
| Op::RayQueryTerminateKHR
| Op::RayQueryGenerateIntersectionKHR
| Op::RayQueryConfirmIntersectionKHR
| Op::RayQueryProceedKHR
| Op::RayQueryGetIntersectionTypeKHR => reserved!(SPV_KHR_ray_query),
| Op::RayQueryGetIntersectionTypeKHR => {} // reserved!(SPV_KHR_ray_query),
// SPV_AMD_shader_fragment_mask
Op::FragmentMaskFetchAMD | Op::FragmentFetchAMD => reserved!(SPV_AMD_shader_fragment_mask),
// SPV_KHR_shader_clock
Expand All @@ -748,7 +749,7 @@ pub fn instruction_signatures(op: Op) -> Option<&'static [InstSig<'static>]> {
| Op::TerminateRayNV
| Op::TraceNV
| Op::TypeAccelerationStructureNV
| Op::ExecuteCallableNV => reserved!(SPV_NV_ray_tracing),
| Op::ExecuteCallableNV => {}
// SPV_NV_cooperative_matrix
Op::TypeCooperativeMatrixNV
| Op::CooperativeMatrixLoadNV
Expand Down
Loading

0 comments on commit 1f51975

Please sign in to comment.