Skip to content

Commit

Permalink
move ray_cube_cube example and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Vecvec committed Dec 12, 2023
1 parent 4e06007 commit 331625a
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 50 deletions.
22 changes: 0 additions & 22 deletions examples/ray-cube-compute/Cargo.toml

This file was deleted.

1 change: 1 addition & 0 deletions examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod texture_arrays;
pub mod timestamp_queries;
pub mod uniform_values;
pub mod water;
pub mod ray_cube_compute;

#[cfg(test)]
wgpu_test::gpu_test_main!();
6 changes: 6 additions & 0 deletions examples/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ const EXAMPLES: &[ExampleDesc] = &[
webgl: false, // No RODS
webgpu: true,
},
ExampleDesc {
name: "ray_cube_compute",
function: wgpu_examples::ray_cube_compute::main,
webgl: false, // No Ray-tracing extensions
webgpu: false, // No Ray-tracing extensions (yet)
}
];

fn get_example_name() -> Option<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ cargo run --example ray-cube-compute

## Screenshots

![Cube example](./screenshot.png)
![Cube example](screenshot.png)
File renamed without changes.
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::ray_tracing as rt;
use wgpu::{ray_tracing as rt, StoreOp};

// from cube
#[repr(C)]
Expand Down Expand Up @@ -259,7 +259,7 @@ struct Example {
start_inst: Instant,
}

impl wgpu_example::framework::Example for Example {
impl crate::framework::Example for Example {
fn required_features() -> wgpu::Features {
wgpu::Features::TEXTURE_BINDING_ARRAY
| wgpu::Features::STORAGE_RESOURCE_BINDING_ARRAY
Expand Down Expand Up @@ -543,7 +543,6 @@ impl wgpu_example::framework::Example for Example {
view: &wgpu::TextureView,
device: &wgpu::Device,
queue: &wgpu::Queue,
spawner: &wgpu_example::framework::Spawner,
) {
device.push_error_scope(wgpu::ErrorFilter::Validation);

Expand Down Expand Up @@ -592,7 +591,7 @@ impl wgpu_example::framework::Example for Example {
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::GREEN),
store: true,
store: StoreOp::Store,
},
})],
depth_stencil_attachment: None,
Expand All @@ -607,20 +606,16 @@ impl wgpu_example::framework::Example for Example {

queue.submit(Some(encoder.finish()));

// If an error occurs, report it and panic.
spawner.spawn_local(ErrorFuture {
inner: device.pop_error_scope(),
});
}
}

fn main() {
wgpu_example::framework::run::<Example>("ray-cube");
pub fn main() {
crate::framework::run::<Example>("ray-cube");
}

#[test]
fn ray_cube_compute() {
wgpu_example::framework::test::<Example>(wgpu_example::framework::FrameworkRefTest {
crate::framework::test::<Example>(wgpu_example::framework::FrameworkRefTest {
image_path: "/examples/ray-cube-compute/screenshot.png",
width: 1024,
height: 768,
Expand Down
File renamed without changes
File renamed without changes.
37 changes: 21 additions & 16 deletions wgpu-core/src/command/ray_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
raw: Mutex::new(Some(scratch_buffer)),
device: device.clone(),
size: max(scratch_buffer_blas_size, scratch_buffer_tlas_size),
info: ResourceInfo::new("Ratracing scratch buffer"),
info: ResourceInfo::new("Raytracing scratch buffer"),
is_coherent: scratch_mapping.is_coherent,
})));

Expand Down Expand Up @@ -1300,12 +1300,11 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.create_buffer(&hal::BufferDescriptor {
label: Some("(wgpu) scratch buffer"),
size: max(scratch_buffer_blas_size, scratch_buffer_tlas_size),
usage: hal::BufferUses::ACCELERATION_STRUCTURE_SCRATCH,
usage: hal::BufferUses::ACCELERATION_STRUCTURE_SCRATCH | BufferUses::MAP_WRITE,
memory_flags: hal::MemoryFlags::empty(),
})
.map_err(crate::device::DeviceError::from)?
};

let staging_buffer = if !instance_buffer_staging_source.is_empty() {
unsafe {
let staging_buffer = device
Expand All @@ -1324,7 +1323,6 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
0..instance_buffer_staging_source.len() as u64,
)
.map_err(crate::device::DeviceError::from)?;

ptr::copy_nonoverlapping(
instance_buffer_staging_source.as_ptr(),
mapping.ptr.as_ptr(),
Expand All @@ -1335,14 +1333,16 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.unmap_buffer(&staging_buffer)
.map_err(crate::device::DeviceError::from)?;
assert!(mapping.is_coherent);

Some(StagingBuffer {
let buf = StagingBuffer {
raw: Mutex::new(Some(staging_buffer)),
device: device.clone(),
size: instance_buffer_staging_source.len() as u64,
info: ResourceInfo::new("Raytracing staging buffer"),
is_coherent: mapping.is_coherent,
})
};
let staging_fid = hub.staging_buffers.request();
let stage_buf = staging_fid.init(buf);
Some(stage_buf)
}
} else {
None
Expand Down Expand Up @@ -1508,31 +1508,36 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.as_mut()
.unwrap()
.temp_resources
.push(TempResource::StagingBuffer(Arc::new(staging_buffer)));
.push(TempResource::StagingBuffer(staging_buffer));
}
}
let scratch_mapping = unsafe {
device
.raw()
.map_buffer(
&scratch_buffer,
0..instance_buffer_staging_source.len() as u64,
0..max(scratch_buffer_blas_size, scratch_buffer_tlas_size),
)
.map_err(crate::device::DeviceError::from)?
};

let buf = StagingBuffer {
raw: Mutex::new(Some(scratch_buffer)),
device: device.clone(),
size: max(scratch_buffer_blas_size, scratch_buffer_tlas_size),
info: ResourceInfo::new("Ratracing scratch buffer"),
is_coherent: scratch_mapping.is_coherent,
};
let staging_fid = hub.staging_buffers.request();
let stage_buf = staging_fid.init(buf);

device
.pending_writes
.lock()
.as_mut()
.unwrap()
.temp_resources
.push(TempResource::StagingBuffer(Arc::new(StagingBuffer {
raw: Mutex::new(Some(scratch_buffer)),
device: device.clone(),
size: max(scratch_buffer_blas_size, scratch_buffer_tlas_size),
info: ResourceInfo::new("Ratracing scratch buffer"),
is_coherent: scratch_mapping.is_coherent,
})));
.push(TempResource::StagingBuffer(stage_buf));

Ok(())
}
Expand Down

0 comments on commit 331625a

Please sign in to comment.