Skip to content

Commit

Permalink
fix clippy and fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Vecvec committed Jan 27, 2024
1 parent a39228e commit d9e915a
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 72 deletions.
5 changes: 2 additions & 3 deletions examples/src/ray_cube_compute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use glam::{Affine3A, Mat4, Quat, Vec3};
use wgpu::util::DeviceExt;

use rt::traits::*;
use wgpu::{CommandEncoderDescriptor, ray_tracing as rt, StoreOp};
use wgpu::{ray_tracing as rt, CommandEncoderDescriptor, StoreOp};

// from cube
#[repr(C)]
Expand Down Expand Up @@ -462,8 +462,7 @@ impl crate::framework::Example for Example {

let dist = 3.0;

let mut encoder =
device.create_command_encoder(&CommandEncoderDescriptor { label: None });
let mut encoder = device.create_command_encoder(&CommandEncoderDescriptor { label: None });

encoder.build_acceleration_structures(
iter::once(&rt::BlasBuildEntry {
Expand Down
99 changes: 71 additions & 28 deletions wgpu-core/src/command/ray_tracing.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
use crate::{command::CommandBuffer, device::queue::TempResource, global::Global, hal_api::HalApi, id::{BlasId, CommandEncoderId, TlasId}, identity::GlobalIdentityHandlerFactory, init_tracker::MemoryInitKind, ray_tracing::{
tlas_instance_into_bytes, BlasAction, BlasBuildEntry, BlasGeometries,
BuildAccelerationStructureError, TlasAction, TlasBuildEntry, TlasPackage,
ValidateBlasActionsError, ValidateTlasActionsError,
}, resource::{Blas, Tlas}, storage::Storage, FastHashSet};

use wgt::{math::align_to, BufferUsages, BlasGeometrySizeDescriptors};
use crate::{
command::CommandBuffer,
device::queue::TempResource,
global::Global,
hal_api::HalApi,
id::{BlasId, CommandEncoderId, TlasId},
identity::GlobalIdentityHandlerFactory,
init_tracker::MemoryInitKind,
ray_tracing::{
tlas_instance_into_bytes, BlasAction, BlasBuildEntry, BlasGeometries,
BuildAccelerationStructureError, TlasAction, TlasBuildEntry, TlasPackage,
ValidateBlasActionsError, ValidateTlasActionsError,
},
resource::{Blas, Tlas},
storage::Storage,
FastHashSet,
};

use wgt::{math::align_to, BlasGeometrySizeDescriptors, BufferUsages};

use crate::identity::Input;
use crate::ray_tracing::{BlasTriangleGeometry, CompactBlasError};
use crate::resource::{Buffer, Resource, ResourceInfo, StagingBuffer};
use crate::track::PendingTransition;
Expand All @@ -14,56 +27,86 @@ use parking_lot::{Mutex, RwLock, RwLockReadGuard};
use std::ops::Deref;
use std::sync::Arc;
use std::{cmp::max, iter, num::NonZeroU64, ops::Range, ptr};
use crate::identity::Input;

use super::BakedCommands;

// This should be queried from the device, maybe the the hal api should pre aline it, since I am unsure how else we can idiomatically get this value.
const SCRATCH_BUFFER_ALIGNMENT: u32 = 256;

impl<G: GlobalIdentityHandlerFactory> Global<G> {
fn internal_command_encoder_compact_blas<A:HalApi>(&self, src_blas: &Arc<Blas<A>>, raw_device: &A::Device, cmd_buf: &Arc<CommandBuffer<A>>) -> Result<Blas<A>, CompactBlasError> {
fn internal_command_encoder_compact_blas<A: HalApi>(
&self,
src_blas: &Arc<Blas<A>>,
raw_device: &A::Device,
cmd_buf: &Arc<CommandBuffer<A>>,
) -> Result<Blas<A>, CompactBlasError> {
profiling::scope!("CommandEncoder::compact_blas");

let acc_struct_size = unsafe { raw_device.get_acceleration_structure_compact_size(src_blas.raw.as_ref().unwrap()) };
let acc_struct_size = unsafe {
raw_device.get_acceleration_structure_compact_size(src_blas.raw.as_ref().unwrap())
};

let acc_struct = unsafe { raw_device.create_acceleration_structure(&hal::AccelerationStructureDescriptor {
label: None,
size: acc_struct_size,
format: hal::AccelerationStructureFormat::BottomLevel,
}).map_err(CompactBlasError::from)? };
let acc_struct = unsafe {
raw_device
.create_acceleration_structure(&hal::AccelerationStructureDescriptor {
label: None,
size: acc_struct_size,
format: hal::AccelerationStructureFormat::BottomLevel,
})
.map_err(CompactBlasError::from)?
};

let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.as_mut().unwrap();
let encoder = cmd_buf_data.encoder.open().map_err(CompactBlasError::from)?;
let encoder = cmd_buf_data
.encoder
.open()
.map_err(CompactBlasError::from)?;

let ty = match &src_blas.sizes {
BlasGeometrySizeDescriptors::Triangles { .. } => wgt::AccelerationStructureType::Triangles,
BlasGeometrySizeDescriptors::Triangles { .. } => {
wgt::AccelerationStructureType::Triangles
}
};

unsafe { encoder.copy_acceleration_structure_to_acceleration_structure(src_blas.raw.as_ref().unwrap(), &acc_struct, hal::AccelerationStructureCopy {
copy_flags: wgt::AccelerationStructureCopy::Compact,
type_flags: ty,
}) }
unsafe {
encoder.copy_acceleration_structure_to_acceleration_structure(
src_blas.raw.as_ref().unwrap(),
&acc_struct,
hal::AccelerationStructureCopy {
copy_flags: wgt::AccelerationStructureCopy::Compact,
type_flags: ty,
},
)
}
let handle = unsafe { raw_device.get_acceleration_structure_device_address(&acc_struct) };

let mut blas = Blas {
raw: Some(acc_struct),
device: src_blas.device.clone(),
info: ResourceInfo::new(src_blas.info.label.as_str()),
size_info: src_blas.size_info.clone(),
size_info: src_blas.size_info,
sizes: src_blas.sizes.clone(),
flags: src_blas.flags.clone(),
flags: src_blas.flags,
update_mode: src_blas.update_mode,
built_index: RwLock::new(src_blas.built_index.read().clone()),
built_index: RwLock::new(*src_blas.built_index.read()),
handle,
};
blas.size_info.acceleration_structure_size = acc_struct_size;
log::info!("src: {}, compacted: {}", src_blas.size_info.acceleration_structure_size, blas.size_info.acceleration_structure_size);
log::info!(
"src: {}, compacted: {}",
src_blas.size_info.acceleration_structure_size,
blas.size_info.acceleration_structure_size
);
Ok(blas)
}

pub fn command_encoder_compact_blas<A: HalApi>(&self, encoder_id: CommandEncoderId, blas_id: BlasId, id_in: Input<G, BlasId>) -> (BlasId, Option<u64>, Option<CompactBlasError>) {
pub fn command_encoder_compact_blas<A: HalApi>(
&self,
encoder_id: CommandEncoderId,
blas_id: BlasId,
id_in: Input<G, BlasId>,
) -> (BlasId, Option<u64>, Option<CompactBlasError>) {
let hub = A::hub(self);
let fid = hub.blas_s.prepare::<G>(id_in);
let blas_guard = hub.blas_s.read();
Expand All @@ -73,7 +116,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
Ok(cmd_buf) => cmd_buf,
Err(err) => {
let id = fid.assign_error(src_blas.label().as_str());
return (id, None, Some(CompactBlasError::from(err)))
return (id, None, Some(CompactBlasError::from(err)));
}
};
let device = &mut &cmd_buf.device;
Expand All @@ -90,7 +133,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let id = fid.assign_error(src_blas.label().as_str());
(id, None, Some(err))
}
}
};
}
pub fn command_encoder_build_acceleration_structures_unsafe_tlas<'a, A: HalApi>(
&self,
Expand Down
11 changes: 8 additions & 3 deletions wgpu-hal/src/dx12/command.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::auxil::{self, dxgi::result::HResult as _};

use super::conv;
use crate::AccelerationStructureCopy;
use std::{mem, ops::Range, ptr};
use winapi::um::d3d12 as d3d12_ty;
use crate::AccelerationStructureCopy;

fn make_box(origin: &wgt::Origin3d, size: &crate::CopyExtent) -> d3d12_ty::D3D12_BOX {
d3d12_ty::D3D12_BOX {
Expand Down Expand Up @@ -1219,7 +1219,12 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
) {
todo!()
}
unsafe fn copy_acceleration_structure_to_acceleration_structure(&mut self, _src: &super::AccelerationStructure, _dst: &super::AccelerationStructure, _copy: AccelerationStructureCopy) {
unsafe fn copy_acceleration_structure_to_acceleration_structure(
&mut self,
_src: &super::AccelerationStructure,
_dst: &super::AccelerationStructure,
_copy: AccelerationStructureCopy,
) {
todo!()
}
}
}
5 changes: 4 additions & 1 deletion wgpu-hal/src/dx12/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1589,7 +1589,10 @@ impl crate::Device<super::Api> for super::Device {
todo!()
}

unsafe fn get_acceleration_structure_compact_size(&self, _acceleration_structure: &super::AccelerationStructure) -> wgt::BufferAddress {
unsafe fn get_acceleration_structure_compact_size(
&self,
_acceleration_structure: &super::AccelerationStructure,
) -> wgt::BufferAddress {
todo!()
}

Expand Down
14 changes: 11 additions & 3 deletions wgpu-hal/src/empty.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(unused_variables)]

use std::ops::Range;
use crate::AccelerationStructureCopy;
use std::ops::Range;

#[derive(Clone, Debug)]
pub struct Api;
Expand Down Expand Up @@ -251,7 +251,10 @@ impl crate::Device<Api> for Context {
Default::default()
}

unsafe fn get_acceleration_structure_compact_size(&self, acceleration_structure: &Resource) -> wgt::BufferAddress {
unsafe fn get_acceleration_structure_compact_size(
&self,
acceleration_structure: &Resource,
) -> wgt::BufferAddress {
Default::default()
}

Expand Down Expand Up @@ -453,6 +456,11 @@ impl crate::CommandEncoder<Api> for Encoder {
) {
}

unsafe fn copy_acceleration_structure_to_acceleration_structure(&mut self, src: &Resource, dst: &Resource, copy: AccelerationStructureCopy) {
unsafe fn copy_acceleration_structure_to_acceleration_structure(
&mut self,
src: &Resource,
dst: &Resource,
copy: AccelerationStructureCopy,
) {
}
}
9 changes: 7 additions & 2 deletions wgpu-hal/src/gles/command.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{conv, Command as C};
use crate::AccelerationStructureCopy;
use arrayvec::ArrayVec;
use std::{mem, ops::Range};
use crate::AccelerationStructureCopy;

#[derive(Clone, Copy, Debug, Default)]
struct TextureSlotDesc {
Expand Down Expand Up @@ -1186,7 +1186,12 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
unimplemented!()
}

unsafe fn copy_acceleration_structure_to_acceleration_structure(&mut self, _src: &(), _dst: &(), _copy: AccelerationStructureCopy) {
unsafe fn copy_acceleration_structure_to_acceleration_structure(
&mut self,
_src: &(),
_dst: &(),
_copy: AccelerationStructureCopy,
) {
unimplemented!()
}
}
5 changes: 4 additions & 1 deletion wgpu-hal/src/gles/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,10 @@ impl crate::Device<super::Api> for super::Device {
unimplemented!()
}

unsafe fn get_acceleration_structure_compact_size(&self, _acceleration_structure: &()) -> wgt::BufferAddress {
unsafe fn get_acceleration_structure_compact_size(
&self,
_acceleration_structure: &(),
) -> wgt::BufferAddress {
todo!()
}

Expand Down
14 changes: 11 additions & 3 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,10 @@ pub trait Device<A: Api>: WasmNotSendSync {
&self,
desc: &GetAccelerationStructureBuildSizesDescriptor<A>,
) -> AccelerationStructureBuildSizes;
unsafe fn get_acceleration_structure_compact_size(&self, acceleration_structure:&A::AccelerationStructure) -> wgt::BufferAddress;
unsafe fn get_acceleration_structure_compact_size(
&self,
acceleration_structure: &A::AccelerationStructure,
) -> wgt::BufferAddress;
unsafe fn get_acceleration_structure_device_address(
&self,
acceleration_structure: &A::AccelerationStructure,
Expand Down Expand Up @@ -507,7 +510,12 @@ pub trait CommandEncoder<A: Api>: WasmNotSendSync + fmt::Debug {
) where
T: Iterator<Item = BufferTextureCopy>;

unsafe fn copy_acceleration_structure_to_acceleration_structure(&mut self, src: &A::AccelerationStructure, dst: &A::AccelerationStructure, copy: AccelerationStructureCopy);
unsafe fn copy_acceleration_structure_to_acceleration_structure(
&mut self,
src: &A::AccelerationStructure,
dst: &A::AccelerationStructure,
copy: AccelerationStructureCopy,
);
// pass common

/// Sets the bind group at `index` to `group`, assuming the layout
Expand Down Expand Up @@ -1562,7 +1570,7 @@ pub struct AccelerationStructureAABBs<'a, A: Api> {
}

pub struct AccelerationStructureCopy {
pub copy_flags:wgt::AccelerationStructureCopy,
pub copy_flags: wgt::AccelerationStructureCopy,
pub type_flags: wgt::AccelerationStructureType,
}

Expand Down
6 changes: 5 additions & 1 deletion wgpu-hal/src/metal/command.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{conv, AsNative, TimestampQuerySupport};
use crate::CommandEncoder as _;
use crate::{AccelerationStructureCopy, CommandEncoder as _};
use std::{borrow::Cow, mem, ops::Range};

// has to match `Temp::binding_sizes`
Expand Down Expand Up @@ -379,6 +379,10 @@ impl crate::CommandEncoder<super::Api> for super::CommandEncoder {
}
}

unsafe fn copy_acceleration_structure_to_acceleration_structure(&mut self, _src: &crate::metal::Api::AccelerationStructure, _dst: &crate::metal::Api::AccelerationStructure, copy: AccelerationStructureCopy) {
unimplemented!()
}

unsafe fn begin_query(&mut self, set: &super::QuerySet, index: u32) {
match set.ty {
wgt::QueryType::Occlusion => {
Expand Down
5 changes: 5 additions & 0 deletions wgpu-hal/src/metal/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
sync::{atomic, Arc},
thread, time,
};
use wgt::BufferAddress;

use super::conv;
use crate::auxil::map_naga_stage;
Expand Down Expand Up @@ -1228,6 +1229,10 @@ impl crate::Device<super::Api> for super::Device {
unimplemented!()
}

unsafe fn get_acceleration_structure_compact_size(&self, acceleration_structure: &crate::metal::Api::AccelerationStructure) -> BufferAddress {
unimplemented!()
}

unsafe fn get_acceleration_structure_device_address(
&self,
_acceleration_structure: &super::AccelerationStructure,
Expand Down
Loading

0 comments on commit d9e915a

Please sign in to comment.