Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ray-tracing] Change index offset to first index #6873

Open
wants to merge 8 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ By @wumpf in [#6849](https://github.com/gfx-rs/wgpu/pull/6849).
- Add actual sample type to `CreateBindGroupError::InvalidTextureSampleType` error message. By @ErichDonGubler in [#6530](https://github.com/gfx-rs/wgpu/pull/6530).
- Improve binding error to give a clearer message when there is a mismatch between resource binding as it is in the shader and as it is in the binding layout. By @eliemichel in [#6553](https://github.com/gfx-rs/wgpu/pull/6553).
- `Surface::configure` and `Surface::get_current_texture` are no longer fatal. By @alokedesai in [#6253](https://github.com/gfx-rs/wgpu/pull/6253)
- Rename `BlasTriangleGeometry::index_buffer_offset` to `BlasTriangleGeometry::first_index`. By @Vecvec in [#6873](https://github.com/gfx-rs/wgpu/pull/6873/files)

##### D3D12

Expand Down
2 changes: 1 addition & 1 deletion examples/src/ray_cube_compute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ impl crate::framework::Example for Example {
first_vertex: 0,
vertex_stride: mem::size_of::<Vertex>() as u64,
index_buffer: Some(&index_buf),
index_buffer_offset: Some(0),
first_index: Some(0),
transform_buffer: None,
transform_buffer_offset: None,
},
Expand Down
2 changes: 1 addition & 1 deletion examples/src/ray_cube_fragment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl crate::framework::Example for Example {
first_vertex: 0,
vertex_stride: mem::size_of::<Vertex>() as u64,
index_buffer: Some(&index_buf),
index_buffer_offset: Some(0),
first_index: Some(0),
transform_buffer: None,
transform_buffer_offset: None,
},
Expand Down
2 changes: 1 addition & 1 deletion examples/src/ray_scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ fn upload_scene_components(
first_vertex: vertex_range.start as u32,
vertex_stride: mem::size_of::<Vertex>() as u64,
index_buffer: Some(&indices),
index_buffer_offset: Some(scene.geometries[i].0.start as u64 * 4),
first_index: Some(scene.geometries[i].0.start as u32),
transform_buffer: None,
transform_buffer_offset: None,
})
Expand Down
2 changes: 1 addition & 1 deletion examples/src/ray_shadows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ impl crate::framework::Example for Example {
first_vertex: 0,
vertex_stride: mem::size_of::<Vertex>() as u64,
index_buffer: Some(&index_buf),
index_buffer_offset: Some(0),
first_index: Some(0),
transform_buffer: None,
transform_buffer_offset: None,
},
Expand Down
4 changes: 2 additions & 2 deletions examples/src/ray_traced_triangle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ impl crate::framework::Example for Example {
vertex_buffer: &vertex_buffer,
first_vertex: 0,
vertex_stride: mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
// in this case since one triangle gets no compression from an index buffer `index_buffer` and `index_buffer_offset` could be `None`.
// in this case since one triangle gets no compression from an index buffer `index_buffer` and `first_index` could be `None`.
index_buffer: Some(&index_buffer),
index_buffer_offset: Some(0),
first_index: Some(0),
transform_buffer: None,
transform_buffer_offset: None,
}]),
Expand Down
4 changes: 2 additions & 2 deletions player/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl GlobalPlay for wgc::global::Global {
transform_buffer: tg.transform_buffer,
first_vertex: tg.first_vertex,
vertex_stride: tg.vertex_stride,
index_buffer_offset: tg.index_buffer_offset,
first_index: tg.first_index,
transform_buffer_offset: tg.transform_buffer_offset,
}
});
Expand Down Expand Up @@ -171,7 +171,7 @@ impl GlobalPlay for wgc::global::Global {
transform_buffer: tg.transform_buffer,
first_vertex: tg.first_vertex,
vertex_stride: tg.vertex_stride,
index_buffer_offset: tg.index_buffer_offset,
first_index: tg.first_index,
transform_buffer_offset: tg.transform_buffer_offset,
}
});
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/ray_tracing/as_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl AsBuildContext {
first_vertex: 0,
vertex_stride: mem::size_of::<[f32; 3]>() as BufferAddress,
index_buffer: None,
index_buffer_offset: None,
first_index: None,
transform_buffer: None,
transform_buffer_offset: None,
}]),
Expand Down Expand Up @@ -406,7 +406,7 @@ fn build_with_transform(ctx: TestingContext) {
first_vertex: 0,
vertex_stride: mem::size_of::<[f32; 3]>() as BufferAddress,
index_buffer: None,
index_buffer_offset: None,
first_index: None,
transform_buffer: Some(&transform),
transform_buffer_offset: Some(0),
}]),
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/ray_tracing/as_use_after_free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn acceleration_structure_use_after_free(ctx: TestingContext) {
first_vertex: 0,
vertex_stride: mem::size_of::<[f32; 3]>() as BufferAddress,
index_buffer: None,
index_buffer_offset: None,
first_index: None,
transform_buffer: None,
transform_buffer_offset: None,
}]),
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/ray_tracing/scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn acceleration_structure_build(ctx: &TestingContext, use_index_buffer: bool) {
first_vertex: 0,
vertex_stride: mem::size_of::<mesh_gen::Vertex>() as u64,
index_buffer: use_index_buffer.then_some(&index_buffer),
index_buffer_offset: use_index_buffer.then_some(0),
first_index: use_index_buffer.then_some(0),
transform_buffer: None,
transform_buffer_offset: None,
}]),
Expand Down
5 changes: 1 addition & 4 deletions wgpu-core/src/command/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,10 +1174,7 @@ impl IndexState {
///
/// Panic if no index buffer has been set.
fn limit(&self) -> u64 {
let bytes_per_index = match self.format {
wgt::IndexFormat::Uint16 => 2,
wgt::IndexFormat::Uint32 => 4,
};
let bytes_per_index = self.format.byte_size() as u64;

(self.range.end - self.range.start) / bytes_per_index
}
Expand Down
47 changes: 18 additions & 29 deletions wgpu-core/src/command/ray_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Global {
transform_buffer: tg.transform_buffer,
first_vertex: tg.first_vertex,
vertex_stride: tg.vertex_stride,
index_buffer_offset: tg.index_buffer_offset,
first_index: tg.first_index,
transform_buffer_offset: tg.transform_buffer_offset,
})
.collect(),
Expand Down Expand Up @@ -149,7 +149,7 @@ impl Global {
transform_buffer: tg.transform_buffer,
first_vertex: tg.first_vertex,
vertex_stride: tg.vertex_stride,
index_buffer_offset: tg.index_buffer_offset,
first_index: tg.first_index,
transform_buffer_offset: tg.transform_buffer_offset,
});
BlasGeometries::TriangleGeometries(Box::new(iter))
Expand Down Expand Up @@ -408,7 +408,7 @@ impl Global {
transform_buffer: tg.transform_buffer,
first_vertex: tg.first_vertex,
vertex_stride: tg.vertex_stride,
index_buffer_offset: tg.index_buffer_offset,
first_index: tg.first_index,
transform_buffer_offset: tg.transform_buffer_offset,
})
.collect(),
Expand Down Expand Up @@ -461,7 +461,7 @@ impl Global {
transform_buffer: tg.transform_buffer,
first_vertex: tg.first_vertex,
vertex_stride: tg.vertex_stride,
index_buffer_offset: tg.index_buffer_offset,
first_index: tg.first_index,
transform_buffer_offset: tg.transform_buffer_offset,
});
BlasGeometries::TriangleGeometries(Box::new(iter))
Expand Down Expand Up @@ -979,7 +979,7 @@ fn iter_blas<'a>(
Ok(buffer) => buffer,
Err(_) => return Err(BuildAccelerationStructureError::InvalidBufferId),
};
if mesh.index_buffer_offset.is_none()
if mesh.first_index.is_none()
|| mesh.size.index_count.is_none()
|| mesh.size.index_count.is_none()
{
Expand Down Expand Up @@ -1106,15 +1106,8 @@ fn iter_buffers<'a, 'b>(
{
input_barriers.push(barrier);
}
let index_stride = match mesh.size.index_format.unwrap() {
wgt::IndexFormat::Uint16 => 2,
wgt::IndexFormat::Uint32 => 4,
};
if mesh.index_buffer_offset.unwrap() % index_stride != 0 {
return Err(BuildAccelerationStructureError::UnalignedIndexBufferOffset(
index_buffer.error_ident(),
));
}
let index_stride = mesh.size.index_format.unwrap().byte_size() as u64;
let offset = mesh.first_index.unwrap() as u64 * index_stride;
let index_buffer_size = mesh.size.index_count.unwrap() as u64 * index_stride;

if mesh.size.index_count.unwrap() % 3 != 0 {
Expand All @@ -1123,23 +1116,18 @@ fn iter_buffers<'a, 'b>(
mesh.size.index_count.unwrap(),
));
}
if index_buffer.size
< mesh.size.index_count.unwrap() as u64 * index_stride
+ mesh.index_buffer_offset.unwrap()
{
if index_buffer.size < mesh.size.index_count.unwrap() as u64 * index_stride + offset {
return Err(BuildAccelerationStructureError::InsufficientBufferSize(
index_buffer.error_ident(),
index_buffer.size,
mesh.size.index_count.unwrap() as u64 * index_stride
+ mesh.index_buffer_offset.unwrap(),
mesh.size.index_count.unwrap() as u64 * index_stride + offset,
));
}

cmd_buf_data.buffer_memory_init_actions.extend(
index_buffer.initialization_status.read().create_action(
index_buffer,
mesh.index_buffer_offset.unwrap()
..(mesh.index_buffer_offset.unwrap() + index_buffer_size),
offset..(offset + index_buffer_size),
MemoryInitKind::NeedsInitializedMemory,
),
);
Expand Down Expand Up @@ -1204,13 +1192,14 @@ fn iter_buffers<'a, 'b>(
first_vertex: mesh.first_vertex,
vertex_count: mesh.size.vertex_count,
vertex_stride: mesh.vertex_stride,
indices: index_buffer.map(|index_buffer| hal::AccelerationStructureTriangleIndices::<
dyn hal::DynBuffer,
> {
format: mesh.size.index_format.unwrap(),
buffer: Some(index_buffer.as_ref()),
offset: mesh.index_buffer_offset.unwrap() as u32,
count: mesh.size.index_count.unwrap(),
indices: index_buffer.map(|index_buffer| {
let index_stride = mesh.size.index_format.unwrap().byte_size() as u32;
hal::AccelerationStructureTriangleIndices::<dyn hal::DynBuffer> {
format: mesh.size.index_format.unwrap(),
buffer: Some(index_buffer.as_ref()),
offset: mesh.first_index.unwrap() * index_stride,
count: mesh.size.index_count.unwrap(),
}
}),
transform: transform_buffer.map(|transform_buffer| {
hal::AccelerationStructureTriangleTransform {
Expand Down
4 changes: 2 additions & 2 deletions wgpu-core/src/ray_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub struct BlasTriangleGeometry<'a> {
pub transform_buffer: Option<BufferId>,
pub first_vertex: u32,
pub vertex_stride: BufferAddress,
pub index_buffer_offset: Option<BufferAddress>,
pub first_index: Option<u32>,
pub transform_buffer_offset: Option<BufferAddress>,
}

Expand Down Expand Up @@ -243,7 +243,7 @@ pub struct TraceBlasTriangleGeometry {
pub transform_buffer: Option<BufferId>,
pub first_vertex: u32,
pub vertex_stride: BufferAddress,
pub index_buffer_offset: Option<BufferAddress>,
pub first_index: Option<u32>,
pub transform_buffer_offset: Option<BufferAddress>,
}

Expand Down
10 changes: 10 additions & 0 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5052,6 +5052,16 @@ pub enum IndexFormat {
Uint32 = 1,
}

impl IndexFormat {
/// Returns the size in bytes of the index format
pub fn byte_size(&self) -> usize {
match self {
IndexFormat::Uint16 => 2,
IndexFormat::Uint32 => 4,
}
}
}

/// Operation to perform on the stencil value.
///
/// Corresponds to [WebGPU `GPUStencilOperation`](
Expand Down
4 changes: 2 additions & 2 deletions wgpu/src/api/blas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ pub struct BlasTriangleGeometry<'a> {
pub vertex_stride: wgt::BufferAddress,
/// Index buffer (optional).
pub index_buffer: Option<&'a Buffer>,
/// Index buffer offset in bytes (optional, required if index buffer is present).
pub index_buffer_offset: Option<wgt::BufferAddress>,
/// Number of indexes to skip in the index buffer (optional, required if index buffer is present).
pub first_index: Option<u32>,
/// Transform buffer containing 3x4 (rows x columns, row major) affine transform matrices `[f32; 12]` (optional).
pub transform_buffer: Option<&'a Buffer>,
/// Transform buffer offset in bytes (optional, required if transform buffer is present).
Expand Down
4 changes: 2 additions & 2 deletions wgpu/src/backend/wgpu_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2445,7 +2445,7 @@ impl dispatch::CommandEncoderInterface for CoreCommandEncoder {
transform_buffer_offset: tg.transform_buffer_offset,
first_vertex: tg.first_vertex,
vertex_stride: tg.vertex_stride,
index_buffer_offset: tg.index_buffer_offset,
first_index: tg.first_index,
}
});
wgc::ray_tracing::BlasGeometries::TriangleGeometries(Box::new(iter))
Expand Down Expand Up @@ -2495,7 +2495,7 @@ impl dispatch::CommandEncoderInterface for CoreCommandEncoder {
transform_buffer_offset: tg.transform_buffer_offset,
first_vertex: tg.first_vertex,
vertex_stride: tg.vertex_stride,
index_buffer_offset: tg.index_buffer_offset,
first_index: tg.first_index,
}
});
wgc::ray_tracing::BlasGeometries::TriangleGeometries(Box::new(iter))
Expand Down
Loading