Skip to content

Commit

Permalink
Clippy, format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vecvec committed Jan 16, 2025
1 parent d8af969 commit c5da888
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 177 deletions.
216 changes: 42 additions & 174 deletions examples/src/ray_cube_normals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,153 +70,30 @@ fn create_vertices() -> (Vec<Vertex>, Vec<u16>) {
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct Uniforms {
view_inverse: [[f32; 4]; 4],
proj_inverse: [[f32; 4]; 4],
view_inverse: Mat4,
proj_inverse: Mat4,
}

#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct AccelerationStructureInstance {
transform: [f32; 12],
custom_index_and_mask: u32,
shader_binding_table_record_offset_and_flags: u32,
acceleration_structure_reference: u64,
}

impl std::fmt::Debug for AccelerationStructureInstance {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Instance")
.field("transform", &self.transform)
.field("custom_index()", &self.custom_index())
.field("mask()", &self.mask())
.field(
"shader_binding_table_record_offset()",
&self.shader_binding_table_record_offset(),
)
.field("flags()", &self.flags())
.field(
"acceleration_structure_reference",
&self.acceleration_structure_reference,
)
.finish()
}
}

#[allow(dead_code)]
impl AccelerationStructureInstance {
const LOW_24_MASK: u32 = 0x00ff_ffff;
const MAX_U24: u32 = (1u32 << 24u32) - 1u32;

#[inline]
fn affine_to_rows(mat: &Affine3A) -> [f32; 12] {
let row_0 = mat.matrix3.row(0);
let row_1 = mat.matrix3.row(1);
let row_2 = mat.matrix3.row(2);
let translation = mat.translation;
[
row_0.x,
row_0.y,
row_0.z,
translation.x,
row_1.x,
row_1.y,
row_1.z,
translation.y,
row_2.x,
row_2.y,
row_2.z,
translation.z,
]
}

#[inline]
fn rows_to_affine(rows: &[f32; 12]) -> Affine3A {
Affine3A::from_cols_array(&[
rows[0], rows[3], rows[6], rows[9], rows[1], rows[4], rows[7], rows[10], rows[2],
rows[5], rows[8], rows[11],
])
}

pub fn transform_as_affine(&self) -> Affine3A {
Self::rows_to_affine(&self.transform)
}
pub fn set_transform(&mut self, transform: &Affine3A) {
self.transform = Self::affine_to_rows(transform);
}

pub fn custom_index(&self) -> u32 {
self.custom_index_and_mask & Self::LOW_24_MASK
}

pub fn mask(&self) -> u8 {
(self.custom_index_and_mask >> 24) as u8
}

pub fn shader_binding_table_record_offset(&self) -> u32 {
self.shader_binding_table_record_offset_and_flags & Self::LOW_24_MASK
}

pub fn flags(&self) -> u8 {
(self.shader_binding_table_record_offset_and_flags >> 24) as u8
}

pub fn set_custom_index(&mut self, custom_index: u32) {
debug_assert!(
custom_index <= Self::MAX_U24,
"custom_index uses more than 24 bits! {custom_index} > {}",
Self::MAX_U24
);
self.custom_index_and_mask =
(custom_index & Self::LOW_24_MASK) | (self.custom_index_and_mask & !Self::LOW_24_MASK)
}

pub fn set_mask(&mut self, mask: u8) {
self.custom_index_and_mask =
(self.custom_index_and_mask & Self::LOW_24_MASK) | (u32::from(mask) << 24)
}

pub fn set_shader_binding_table_record_offset(
&mut self,
shader_binding_table_record_offset: u32,
) {
debug_assert!(shader_binding_table_record_offset <= Self::MAX_U24, "shader_binding_table_record_offset uses more than 24 bits! {shader_binding_table_record_offset} > {}", Self::MAX_U24);
self.shader_binding_table_record_offset_and_flags = (shader_binding_table_record_offset
& Self::LOW_24_MASK)
| (self.shader_binding_table_record_offset_and_flags & !Self::LOW_24_MASK)
}

pub fn set_flags(&mut self, flags: u8) {
self.shader_binding_table_record_offset_and_flags =
(self.shader_binding_table_record_offset_and_flags & Self::LOW_24_MASK)
| (u32::from(flags) << 24)
}

pub fn new(
transform: &Affine3A,
custom_index: u32,
mask: u8,
shader_binding_table_record_offset: u32,
flags: u8,
acceleration_structure_reference: u64,
) -> Self {
debug_assert!(
custom_index <= Self::MAX_U24,
"custom_index uses more than 24 bits! {custom_index} > {}",
Self::MAX_U24
);
debug_assert!(
shader_binding_table_record_offset <= Self::MAX_U24,
"shader_binding_table_record_offset uses more than 24 bits! {shader_binding_table_record_offset} > {}", Self::MAX_U24
);
AccelerationStructureInstance {
transform: Self::affine_to_rows(transform),
custom_index_and_mask: (custom_index & Self::MAX_U24) | (u32::from(mask) << 24),
shader_binding_table_record_offset_and_flags: (shader_binding_table_record_offset
& Self::MAX_U24)
| (u32::from(flags) << 24),
acceleration_structure_reference,
}
}
#[inline]
fn affine_to_rows(mat: &Affine3A) -> [f32; 12] {
let row_0 = mat.matrix3.row(0);
let row_1 = mat.matrix3.row(1);
let row_2 = mat.matrix3.row(2);
let translation = mat.translation;
[
row_0.x,
row_0.y,
row_0.z,
translation.x,
row_1.x,
row_1.y,
row_1.z,
translation.y,
row_2.x,
row_2.y,
row_2.z,
translation.z,
]
}

/// A wrapper for `pop_error_scope` futures that panics if an error occurs.
Expand All @@ -241,15 +118,18 @@ impl<F: Future<Output = Option<wgpu::Error>>> Future for ErrorFuture<F> {
}
}

#[allow(dead_code)]
struct Example {
rt_target: wgpu::Texture,
#[expect(dead_code)]
rt_view: wgpu::TextureView,
#[expect(dead_code)]
sampler: wgpu::Sampler,
#[expect(dead_code)]
uniform_buf: wgpu::Buffer,
#[expect(dead_code)]
vertex_buf: wgpu::Buffer,
#[expect(dead_code)]
index_buf: wgpu::Buffer,
blas: wgpu::Blas,
tlas_package: wgpu::TlasPackage,
compute_pipeline: wgpu::ComputePipeline,
compute_bind_group: wgpu::BindGroup,
Expand All @@ -263,7 +143,6 @@ impl crate::framework::Example for Example {
const SRGB: bool = false;
fn required_features() -> wgpu::Features {
wgpu::Features::TEXTURE_BINDING_ARRAY
| wgpu::Features::STORAGE_RESOURCE_BINDING_ARRAY
| wgpu::Features::VERTEX_WRITABLE_STORAGE
| wgpu::Features::EXPERIMENTAL_RAY_QUERY
| wgpu::Features::EXPERIMENTAL_RAY_TRACING_ACCELERATION_STRUCTURE
Expand Down Expand Up @@ -333,8 +212,8 @@ impl crate::framework::Example for Example {
);

Uniforms {
view_inverse: view.inverse().to_cols_array_2d(),
proj_inverse: proj.inverse().to_cols_array_2d(),
view_inverse: view.inverse(),
proj_inverse: proj.inverse(),
}
};

Expand Down Expand Up @@ -474,20 +353,16 @@ impl crate::framework::Example for Example {

for x in 0..side_count {
for y in 0..side_count {
*tlas_package
.get_mut_single((x + y * side_count) as usize)
.unwrap() = Some(wgpu::TlasInstance::new(
tlas_package[(x + y * side_count) as usize] = Some(wgpu::TlasInstance::new(
&blas,
AccelerationStructureInstance::affine_to_rows(
&Affine3A::from_rotation_translation(
Quat::from_rotation_y(45.9_f32.to_radians()),
Vec3 {
x: x as f32 * dist,
y: y as f32 * dist,
z: -30.0,
},
),
),
affine_to_rows(&Affine3A::from_rotation_translation(
Quat::from_rotation_y(45.9_f32.to_radians()),
Vec3 {
x: x as f32 * dist,
y: y as f32 * dist,
z: -30.0,
},
)),
0,
0xff,
));
Expand All @@ -513,7 +388,6 @@ impl crate::framework::Example for Example {
},
]),
}),
// iter::empty(),
iter::once(&tlas_package),
);

Expand All @@ -528,7 +402,6 @@ impl crate::framework::Example for Example {
uniform_buf,
vertex_buf,
index_buf,
blas,
tlas_package,
compute_pipeline,
compute_bind_group,
Expand All @@ -555,13 +428,8 @@ impl crate::framework::Example for Example {

let anim_time = self.start_inst.elapsed().as_secs_f64() as f32;

self.tlas_package
.get_mut_single(0)
.unwrap()
.as_mut()
.unwrap()
.transform =
AccelerationStructureInstance::affine_to_rows(&Affine3A::from_rotation_translation(
self.tlas_package[0].as_mut().unwrap().transform =
affine_to_rows(&Affine3A::from_rotation_translation(
Quat::from_euler(
glam::EulerRot::XYZ,
anim_time * 0.342,
Expand All @@ -586,7 +454,7 @@ impl crate::framework::Example for Example {
timestamp_writes: None,
});
cpass.set_pipeline(&self.compute_pipeline);
cpass.set_bind_group(0, &self.compute_bind_group, &[]);
cpass.set_bind_group(0, Some(&self.compute_bind_group), &[]);
cpass.dispatch_workgroups(self.rt_target.width() / 8, self.rt_target.height() / 8, 1);
}

Expand All @@ -607,7 +475,7 @@ impl crate::framework::Example for Example {
});

rpass.set_pipeline(&self.blit_pipeline);
rpass.set_bind_group(0, &self.blit_bind_group, &[]);
rpass.set_bind_group(0, Some(&self.blit_bind_group), &[]);
rpass.draw(0..3, 0..1);
}

Expand Down
14 changes: 12 additions & 2 deletions naga/src/back/hlsl/ray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ impl<W: Write> super::Writer<'_, W> {
) -> BackendResult {
self.write_type(module, module.special_types.ray_intersection.unwrap())?;
write!(self.out, " GetCommittedIntersection(")?;
self.write_value_type(module, &TypeInner::RayQuery { vertex_return: false })?;
self.write_value_type(
module,
&TypeInner::RayQuery {
vertex_return: false,
},
)?;
writeln!(self.out, " rq) {{")?;
write!(self.out, " ")?;
self.write_type(module, module.special_types.ray_intersection.unwrap())?;
Expand Down Expand Up @@ -93,7 +98,12 @@ impl<W: Write> super::Writer<'_, W> {
) -> BackendResult {
self.write_type(module, module.special_types.ray_intersection.unwrap())?;
write!(self.out, " GetCandidateIntersection(")?;
self.write_value_type(module, &TypeInner::RayQuery { vertex_return: false })?;
self.write_value_type(
module,
&TypeInner::RayQuery {
vertex_return: false,
},
)?;
writeln!(self.out, " rq) {{")?;
write!(self.out, " ")?;
self.write_type(module, module.special_types.ray_intersection.unwrap())?;
Expand Down
4 changes: 3 additions & 1 deletion wgpu-hal/src/dx12/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,9 @@ impl crate::Device for super::Device {
num_texture_views += count
}
wgt::BindingType::Sampler { .. } => num_samplers += count,
wgt::BindingType::AccelerationStructure { .. } => num_acceleration_structures += count,
wgt::BindingType::AccelerationStructure { .. } => {
num_acceleration_structures += count
}
}
}

Expand Down

0 comments on commit c5da888

Please sign in to comment.