Skip to content

Commit

Permalink
more format fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vecvec committed Dec 11, 2023
1 parent 14e65dd commit c9c027e
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 93 deletions.
2 changes: 1 addition & 1 deletion tests/tests/ray_tracing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{iter, mem};

use wgpu_test::{gpu_test, GpuTestConfiguration, TestingContext, TestParameters};
use wgpu_test::{gpu_test, GpuTestConfiguration, TestParameters, TestingContext};

use rt::traits::*;
use wgpu::ray_tracing as rt;
Expand Down
16 changes: 10 additions & 6 deletions wgpu-core/src/command/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,12 +540,16 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.extend(texture_memory_actions.register_init_action(action));
}

let used_resource = bind_group.used.acceleration_structures.used_resources().map(|tlas| {
tracker.tlas_s.add_single(&tlas_guard, tlas.as_info().id());
crate::ray_tracing::TlasAction {
id: tlas.as_info().id(),
kind: crate::ray_tracing::TlasActionKind::Use,
}
let used_resource = bind_group
.used
.acceleration_structures
.used_resources()
.map(|tlas| {
tracker.tlas_s.add_single(&tlas_guard, tlas.as_info().id());
crate::ray_tracing::TlasAction {
id: tlas.as_info().id(),
kind: crate::ray_tracing::TlasActionKind::Use,
}
});

cmd_buf_data.tlas_actions.extend(
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use crate::hub::Hub;
use crate::id::CommandBufferId;

use crate::init_tracker::BufferInitTrackerAction;
use crate::resource::{Resource, ResourceInfo, ResourceType};
use crate::ray_tracing::{BlasAction, TlasAction};
use crate::resource::{Resource, ResourceInfo, ResourceType};
use crate::track::{Tracker, UsageScope};
use crate::{
api_log, global::Global, hal_api::HalApi, id, identity::GlobalIdentityHandlerFactory,
Expand Down
100 changes: 57 additions & 43 deletions wgpu-core/src/command/ray_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ use crate::{

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

use crate::ray_tracing::BlasTriangleGeometry;
use crate::resource::{Buffer, Resource, ResourceInfo, StagingBuffer};
use crate::track::PendingTransition;
use std::{cmp::max, iter, num::NonZeroU64, ops::Range, ptr};
use std::ops::Deref;
use std::sync::Arc;
use parking_lot::{Mutex, RwLockReadGuard};
use hal::{BufferUses, CommandEncoder, Device};
use crate::ray_tracing::BlasTriangleGeometry;
use crate::resource::{Buffer, Resource, ResourceInfo, StagingBuffer};
use crate::track::PendingTransition;

use super::BakedCommands;

Expand Down Expand Up @@ -108,17 +108,15 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
&crate::ray_tracing::TraceBlasGeometries::TriangleGeometries(
ref triangle_geometries,
) => {
let iter = triangle_geometries.iter().map(|tg| {
BlasTriangleGeometry {
size: &tg.size,
vertex_buffer: tg.vertex_buffer,
index_buffer: tg.index_buffer,
transform_buffer: tg.transform_buffer,
first_vertex: tg.first_vertex,
vertex_stride: tg.vertex_stride,
index_buffer_offset: tg.index_buffer_offset,
transform_buffer_offset: tg.transform_buffer_offset,
}
let iter = triangle_geometries.iter().map(|tg| BlasTriangleGeometry {
size: &tg.size,
vertex_buffer: tg.vertex_buffer,
index_buffer: tg.index_buffer,
transform_buffer: tg.transform_buffer,
first_vertex: tg.first_vertex,
vertex_stride: tg.vertex_stride,
index_buffer_offset: tg.index_buffer_offset,
transform_buffer_offset: tg.transform_buffer_offset,
});
BlasGeometries::TriangleGeometries(Box::new(iter))
}
Expand All @@ -133,7 +131,14 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let tlas_iter = trace_tlas.iter();

let mut input_barriers = Vec::<hal::BufferBarrier<A>>::new();
let mut buf_storage = Vec::<(Arc<Buffer<A>>, Option<PendingTransition<BufferUses>>, Option<(Arc<Buffer<A>>, Option<PendingTransition<BufferUses>>)>, Option<(Arc<Buffer<A>>, Option<PendingTransition<BufferUses>>)>, BlasTriangleGeometry, Option<Arc<Blas<A>>>)>::new();
let mut buf_storage = Vec::<(
Arc<Buffer<A>>,
Option<PendingTransition<BufferUses>>,
Option<(Arc<Buffer<A>>, Option<PendingTransition<BufferUses>>)>,
Option<(Arc<Buffer<A>>, Option<PendingTransition<BufferUses>>)>,
BlasTriangleGeometry,
Option<Arc<Blas<A>>>
)>::new();

let mut scratch_buffer_blas_size = 0;
let mut blas_storage = Vec::<(&Blas<A>, hal::AccelerationStructureEntries<A>, u64)>::new();
Expand All @@ -157,7 +162,6 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {

match entry.geometries {
BlasGeometries::TriangleGeometries(triangle_geometries) => {

for (i, mesh) in triangle_geometries.enumerate() {
let size_desc = match &blas.sizes {
&wgt::BlasGeometrySizeDescriptors::Triangles { ref desc } => desc,
Expand All @@ -176,11 +180,11 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
|| size_desc.vertex_format != mesh.size.vertex_format
|| size_desc.index_count.is_none() != mesh.size.index_count.is_none()
|| (size_desc.index_count.is_none()
|| size_desc.index_count.unwrap() < mesh.size.index_count.unwrap())
|| size_desc.index_count.unwrap() < mesh.size.index_count.unwrap())
|| size_desc.index_format.is_none() != mesh.size.index_format.is_none()
|| (size_desc.index_format.is_none()
|| size_desc.index_format.unwrap()
!= mesh.size.index_format.unwrap())
|| size_desc.index_format.unwrap()
!= mesh.size.index_format.unwrap())
{
return Err(
BuildAccelerationStructureError::IncompatibleBlasBuildSizes(
Expand Down Expand Up @@ -228,7 +232,13 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.set_single(
match buffer_guard.get(index_id) {
Ok(buffer) => buffer,
Err(_) => { return Err(BuildAccelerationStructureError::InvalidBuffer(index_id)) },
Err(_) => {
return Err(
BuildAccelerationStructureError::InvalidBuffer(
index_id,
),
)
},
},
hal::BufferUses::BOTTOM_LEVEL_ACCELERATION_STRUCTURE_INPUT,
)
Expand All @@ -251,7 +261,13 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.set_single(
match buffer_guard.get(transform_id) {
Ok(buffer) => buffer,
Err(_) => { return Err(BuildAccelerationStructureError::InvalidBuffer(transform_id)) },
Err(_) => {
return Err(
BuildAccelerationStructureError::InvalidBuffer(
transform_id,
),
)
},
},
BufferUses::BOTTOM_LEVEL_ACCELERATION_STRUCTURE_INPUT,
)
Expand Down Expand Up @@ -281,32 +297,23 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
BuildAccelerationStructureError::InvalidBuffer(mesh.vertex_buffer),
)?;
if !vertex_buffer.usage.contains(BufferUsages::BLAS_INPUT) {
return Err(
BuildAccelerationStructureError::MissingBlasInputUsageFlag(
mesh.vertex_buffer,
),
);
return Err(BuildAccelerationStructureError::MissingBlasInputUsageFlag(
mesh.vertex_buffer,
));
}
if let Some(barrier) =
buf.1.take().map(|pending| pending.into_hal(vertex_buffer))
{
if let Some(barrier) = buf.1.take().map(|pending| pending.into_hal(vertex_buffer)) {
input_barriers.push(barrier);
}
if vertex_buffer.size
< (mesh.size.vertex_count + mesh.first_vertex) as u64
* mesh.vertex_stride
< (mesh.size.vertex_count + mesh.first_vertex) as u64 * mesh.vertex_stride
{
return Err(
BuildAccelerationStructureError::InsufficientBufferSize(
mesh.vertex_buffer,
vertex_buffer.size,
(mesh.size.vertex_count + mesh.first_vertex) as u64
* mesh.vertex_stride,
),
);
return Err(BuildAccelerationStructureError::InsufficientBufferSize(
mesh.vertex_buffer,
vertex_buffer.size,
(mesh.size.vertex_count + mesh.first_vertex) as u64 * mesh.vertex_stride,
));
}
let vertex_buffer_offset =
mesh.first_vertex as u64 * mesh.vertex_stride;
let vertex_buffer_offset = mesh.first_vertex as u64 * mesh.vertex_stride;
cmd_buf_data.buffer_memory_init_actions.extend(
vertex_buffer.initialization_status.read().create_action(
buffer_guard.get(mesh.vertex_buffer).unwrap(),
Expand Down Expand Up @@ -957,7 +964,14 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
None
};

buf_storage.push((vertex_buffer, vertex_pending, index_data, transform_data, mesh, None))
buf_storage.push((
vertex_buffer,
vertex_pending,
index_data,
transform_data,
mesh,
None,
))
}

if let Some(last) = buf_storage.last_mut() {
Expand Down Expand Up @@ -1007,7 +1021,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
buffer_guard.get(mesh.vertex_buffer).unwrap(),
vertex_buffer_offset
..(vertex_buffer_offset
+ mesh.size.vertex_count as u64 * mesh.vertex_stride),
+ mesh.size.vertex_count as u64 * mesh.vertex_stride),
MemoryInitKind::NeedsInitializedMemory,
),
);
Expand Down
12 changes: 6 additions & 6 deletions wgpu/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ pub(crate) struct DeviceRequest {
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
not(target_feature = "atomics")
)
))]
pub type BufferMapCallback = Box<dyn FnOnce(Result<(), BufferAsyncError>) + Send + 'static>;
Expand All @@ -1172,17 +1172,17 @@ pub type BufferMapCallback = Box<dyn FnOnce(Result<(), BufferAsyncError>) + 'sta
#[cfg(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
))]
pub(crate) type AdapterRequestDeviceFuture =
Box<dyn Future<Output = Result<DeviceRequest, RequestDeviceError>> + Send>;
#[cfg(not(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
)))]
pub(crate) type AdapterRequestDeviceFuture =
Expand Down Expand Up @@ -1219,7 +1219,7 @@ pub type DevicePopErrorFuture = Box<dyn Future<Output = Option<Error>> + Send>;
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
not(target_feature = "atomics")
)
)))]
pub type DevicePopErrorFuture = Box<dyn Future<Output = Option<Error>>>;
Expand Down
84 changes: 48 additions & 36 deletions wgpu/src/ray_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,23 @@ pub trait DeviceRayTracing {
/// Create a bottom level acceleration structure, used inside a top level acceleration structure for ray tracing.
/// - desc: The descriptor of the acceleration structure.
/// - sizes: Size descriptor limiting what can be built into the acceleration structure.
fn create_blas(&self, desc: &CreateBlasDescriptor<'_>, sizes: BlasGeometrySizeDescriptors) -> Blas;
fn create_blas(
&self,
desc: &CreateBlasDescriptor<'_>,
sizes: BlasGeometrySizeDescriptors,
) -> Blas;

/// Create a top level acceleration structure, used for ray tracing.
/// - desc: The descriptor of the acceleration structure.
fn create_tlas(&self, desc: &CreateTlasDescriptor<'_>) -> Tlas;
}

impl DeviceRayTracing for Device {
fn create_blas(&self, desc: &CreateBlasDescriptor<'_>, sizes: BlasGeometrySizeDescriptors) -> Blas {
fn create_blas(
&self,
desc: &CreateBlasDescriptor<'_>,
sizes: BlasGeometrySizeDescriptors,
) -> Blas {
let (id, handle, data) = DynContext::device_create_blas(
&*self.context,
&self.id,
Expand Down Expand Up @@ -434,23 +442,25 @@ impl CommandEncoderRayTracing for CommandEncoder {
let mut blas = blas.into_iter().map(|e: &BlasBuildEntry<'_>| {
let geometries = match &e.geometry {
BlasGeometries::TriangleGeometries(triangle_geometries) => {
let iter = triangle_geometries.iter().map(|tg: &BlasTriangleGeometry<'_>| {
DynContextBlasTriangleGeometry {
size: tg.size,
vertex_buffer: tg.vertex_buffer.id,

index_buffer: tg.index_buffer.map(|index_buffer| index_buffer.id),

transform_buffer: tg
.transform_buffer
.map(|transform_buffer| transform_buffer.id),

first_vertex: tg.first_vertex,
vertex_stride: tg.vertex_stride,
index_buffer_offset: tg.index_buffer_offset,
transform_buffer_offset: tg.transform_buffer_offset,
}
});
let iter = triangle_geometries
.iter()
.map(
|tg: &BlasTriangleGeometry<'_>| DynContextBlasTriangleGeometry {
size: tg.size,
vertex_buffer: tg.vertex_buffer.id,

index_buffer: tg.index_buffer.map(|index_buffer| index_buffer.id),

transform_buffer: tg
.transform_buffer
.map(|transform_buffer| transform_buffer.id),

first_vertex: tg.first_vertex,
vertex_stride: tg.vertex_stride,
index_buffer_offset: tg.index_buffer_offset,
transform_buffer_offset: tg.transform_buffer_offset,
},
);
DynContextBlasGeometries::TriangleGeometries(Box::new(iter))
}
};
Expand Down Expand Up @@ -495,23 +505,25 @@ impl CommandEncoderRayTracing for CommandEncoder {
let mut blas = blas.into_iter().map(|e: &BlasBuildEntry<'_>| {
let geometries = match &e.geometry {
BlasGeometries::TriangleGeometries(triangle_geometries) => {
let iter = triangle_geometries.iter().map(|tg: &BlasTriangleGeometry<'_>| {
DynContextBlasTriangleGeometry {
size: tg.size,
vertex_buffer: tg.vertex_buffer.id,

index_buffer: tg.index_buffer.map(|index_buffer| index_buffer.id),

transform_buffer: tg
.transform_buffer
.map(|transform_buffer| transform_buffer.id),

first_vertex: tg.first_vertex,
vertex_stride: tg.vertex_stride,
index_buffer_offset: tg.index_buffer_offset,
transform_buffer_offset: tg.transform_buffer_offset,
}
});
let iter = triangle_geometries
.iter()
.map(
|tg: &BlasTriangleGeometry<'_>| DynContextBlasTriangleGeometry {
size: tg.size,
vertex_buffer: tg.vertex_buffer.id,

index_buffer: tg.index_buffer.map(|index_buffer| index_buffer.id),

transform_buffer: tg
.transform_buffer
.map(|transform_buffer| transform_buffer.id),

first_vertex: tg.first_vertex,
vertex_stride: tg.vertex_stride,
index_buffer_offset: tg.index_buffer_offset,
transform_buffer_offset: tg.transform_buffer_offset,
},
);
DynContextBlasGeometries::TriangleGeometries(Box::new(iter))
}
};
Expand Down

0 comments on commit c9c027e

Please sign in to comment.