mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-24 16:31:11 +01:00
DynDevice acceleration structure handling
This commit is contained in:
1 parent
522b98c582
commit
9e1f1487b4
11 files changed
+182
-42
No files matched your search
@@ -1190,7 +1190,13 @@ impl crate::CommandEncoder for super::CommandEncoder {
|
||||
_descriptors: T,
|
||||
) where
|
||||
super::Api: 'a,
|
||||
T: IntoIterator<Item = crate::BuildAccelerationStructureDescriptor<'a, super::Api>>,
|
||||
T: IntoIterator<
|
||||
Item = crate::BuildAccelerationStructureDescriptor<
|
||||
'a,
|
||||
super::Buffer,
|
||||
super::AccelerationStructure,
|
||||
>,
|
||||
>,
|
||||
{
|
||||
// Implement using `BuildRaytracingAccelerationStructure`:
|
||||
// https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html#buildraytracingaccelerationstructure
|
||||
|
||||
@@ -1745,7 +1745,7 @@ impl crate::Device for super::Device {
|
||||
|
||||
unsafe fn get_acceleration_structure_build_sizes<'a>(
|
||||
&self,
|
||||
_desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Api>,
|
||||
_desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Buffer>,
|
||||
) -> crate::AccelerationStructureBuildSizes {
|
||||
// Implement using `GetRaytracingAccelerationStructurePrebuildInfo`:
|
||||
// https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html#getraytracingaccelerationstructureprebuildinfo
|
||||
|
||||
@@ -2,17 +2,21 @@
|
||||
#![allow(trivial_casts)]
|
||||
|
||||
use crate::{
|
||||
Api, BindGroupDescriptor, BindGroupLayoutDescriptor, BufferDescriptor, BufferMapping,
|
||||
CommandEncoderDescriptor, ComputePipelineDescriptor, Device, DeviceError, DynBuffer,
|
||||
DynResource, FenceValue, Label, MemoryRange, PipelineCacheDescriptor, PipelineCacheError,
|
||||
PipelineError, PipelineLayoutDescriptor, RenderPipelineDescriptor, SamplerDescriptor,
|
||||
ShaderError, ShaderInput, ShaderModuleDescriptor, TextureDescriptor, TextureViewDescriptor,
|
||||
AccelerationStructureAABBs, AccelerationStructureBuildSizes, AccelerationStructureDescriptor,
|
||||
AccelerationStructureEntries, AccelerationStructureInstances,
|
||||
AccelerationStructureTriangleIndices, AccelerationStructureTriangleTransform,
|
||||
AccelerationStructureTriangles, Api, BindGroupDescriptor, BindGroupLayoutDescriptor,
|
||||
BufferDescriptor, BufferMapping, CommandEncoderDescriptor, ComputePipelineDescriptor, Device,
|
||||
DeviceError, FenceValue, GetAccelerationStructureBuildSizesDescriptor, Label, MemoryRange,
|
||||
PipelineCacheDescriptor, PipelineCacheError, PipelineError, PipelineLayoutDescriptor,
|
||||
RenderPipelineDescriptor, SamplerDescriptor, ShaderError, ShaderInput, ShaderModuleDescriptor,
|
||||
TextureDescriptor, TextureViewDescriptor,
|
||||
};
|
||||
|
||||
use super::{
|
||||
DynAccelerationStructure, DynBindGroup, DynBindGroupLayout, DynCommandEncoder,
|
||||
DynAccelerationStructure, DynBindGroup, DynBindGroupLayout, DynBuffer, DynCommandEncoder,
|
||||
DynComputePipeline, DynFence, DynPipelineCache, DynPipelineLayout, DynQuerySet, DynQueue,
|
||||
DynRenderPipeline, DynResourceExt as _, DynSampler, DynShaderModule, DynTexture,
|
||||
DynRenderPipeline, DynResource, DynResourceExt as _, DynSampler, DynShaderModule, DynTexture,
|
||||
DynTextureView,
|
||||
};
|
||||
|
||||
@@ -139,6 +143,23 @@ pub trait DynDevice: DynResource {
|
||||
unsafe fn stop_capture(&self);
|
||||
|
||||
unsafe fn pipeline_cache_get_data(&self, cache: &dyn DynPipelineCache) -> Option<Vec<u8>>;
|
||||
|
||||
unsafe fn create_acceleration_structure(
|
||||
&self,
|
||||
desc: &AccelerationStructureDescriptor,
|
||||
) -> Result<Box<dyn DynAccelerationStructure>, DeviceError>;
|
||||
unsafe fn get_acceleration_structure_build_sizes(
|
||||
&self,
|
||||
desc: &GetAccelerationStructureBuildSizesDescriptor<dyn DynBuffer>,
|
||||
) -> AccelerationStructureBuildSizes;
|
||||
unsafe fn get_acceleration_structure_device_address(
|
||||
&self,
|
||||
acceleration_structure: &dyn DynAccelerationStructure,
|
||||
) -> wgt::BufferAddress;
|
||||
unsafe fn destroy_acceleration_structure(
|
||||
&self,
|
||||
acceleration_structure: Box<dyn DynAccelerationStructure>,
|
||||
);
|
||||
}
|
||||
|
||||
impl<D: Device + DynResource> DynDevice for D {
|
||||
@@ -452,4 +473,89 @@ impl<D: Device + DynResource> DynDevice for D {
|
||||
let cache = cache.expect_downcast_ref();
|
||||
unsafe { D::pipeline_cache_get_data(self, cache) }
|
||||
}
|
||||
|
||||
unsafe fn create_acceleration_structure(
|
||||
&self,
|
||||
desc: &AccelerationStructureDescriptor,
|
||||
) -> Result<Box<dyn DynAccelerationStructure>, DeviceError> {
|
||||
unsafe { D::create_acceleration_structure(self, desc) }
|
||||
.map(|b| Box::new(b) as Box<dyn DynAccelerationStructure>)
|
||||
}
|
||||
|
||||
unsafe fn get_acceleration_structure_build_sizes(
|
||||
&self,
|
||||
desc: &GetAccelerationStructureBuildSizesDescriptor<dyn DynBuffer>,
|
||||
) -> AccelerationStructureBuildSizes {
|
||||
let entries = match &desc.entries {
|
||||
AccelerationStructureEntries::Instances(instances) => {
|
||||
AccelerationStructureEntries::Instances(AccelerationStructureInstances {
|
||||
buffer: instances.buffer.map(|b| b.expect_downcast_ref()),
|
||||
offset: instances.offset,
|
||||
count: instances.count,
|
||||
})
|
||||
}
|
||||
AccelerationStructureEntries::Triangles(triangles) => {
|
||||
AccelerationStructureEntries::Triangles(
|
||||
triangles
|
||||
.iter()
|
||||
.map(|t| AccelerationStructureTriangles {
|
||||
vertex_buffer: t.vertex_buffer.map(|b| b.expect_downcast_ref()),
|
||||
vertex_format: t.vertex_format,
|
||||
first_vertex: t.first_vertex,
|
||||
vertex_count: t.vertex_count,
|
||||
vertex_stride: t.vertex_stride,
|
||||
indices: t.indices.as_ref().map(|i| {
|
||||
AccelerationStructureTriangleIndices {
|
||||
buffer: i.buffer.map(|b| b.expect_downcast_ref()),
|
||||
format: i.format,
|
||||
offset: i.offset,
|
||||
count: i.count,
|
||||
}
|
||||
}),
|
||||
transform: t.transform.as_ref().map(|t| {
|
||||
AccelerationStructureTriangleTransform {
|
||||
buffer: t.buffer.expect_downcast_ref(),
|
||||
offset: t.offset,
|
||||
}
|
||||
}),
|
||||
flags: t.flags,
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
AccelerationStructureEntries::AABBs(entries) => AccelerationStructureEntries::AABBs(
|
||||
entries
|
||||
.iter()
|
||||
.map(|e| AccelerationStructureAABBs {
|
||||
buffer: e.buffer.map(|b| b.expect_downcast_ref()),
|
||||
offset: e.offset,
|
||||
count: e.count,
|
||||
stride: e.stride,
|
||||
flags: e.flags,
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
};
|
||||
|
||||
let desc = GetAccelerationStructureBuildSizesDescriptor {
|
||||
entries: &entries,
|
||||
flags: desc.flags,
|
||||
};
|
||||
unsafe { D::get_acceleration_structure_build_sizes(self, &desc) }
|
||||
}
|
||||
|
||||
unsafe fn get_acceleration_structure_device_address(
|
||||
&self,
|
||||
acceleration_structure: &dyn DynAccelerationStructure,
|
||||
) -> wgt::BufferAddress {
|
||||
let acceleration_structure = acceleration_structure.expect_downcast_ref();
|
||||
unsafe { D::get_acceleration_structure_device_address(self, acceleration_structure) }
|
||||
}
|
||||
|
||||
unsafe fn destroy_acceleration_structure(
|
||||
&self,
|
||||
acceleration_structure: Box<dyn DynAccelerationStructure>,
|
||||
) {
|
||||
unsafe { D::destroy_acceleration_structure(self, acceleration_structure.unbox()) }
|
||||
}
|
||||
}
|
||||
@@ -291,7 +291,7 @@ impl crate::Device for Context {
|
||||
}
|
||||
unsafe fn get_acceleration_structure_build_sizes<'a>(
|
||||
&self,
|
||||
_desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, Api>,
|
||||
_desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, Resource>,
|
||||
) -> crate::AccelerationStructureBuildSizes {
|
||||
Default::default()
|
||||
}
|
||||
@@ -494,7 +494,7 @@ impl crate::CommandEncoder for Encoder {
|
||||
descriptors: T,
|
||||
) where
|
||||
Api: 'a,
|
||||
T: IntoIterator<Item = crate::BuildAccelerationStructureDescriptor<'a, Api>>,
|
||||
T: IntoIterator<Item = crate::BuildAccelerationStructureDescriptor<'a, Resource, Resource>>,
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -1188,7 +1188,13 @@ impl crate::CommandEncoder for super::CommandEncoder {
|
||||
_descriptors: T,
|
||||
) where
|
||||
super::Api: 'a,
|
||||
T: IntoIterator<Item = crate::BuildAccelerationStructureDescriptor<'a, super::Api>>,
|
||||
T: IntoIterator<
|
||||
Item = crate::BuildAccelerationStructureDescriptor<
|
||||
'a,
|
||||
super::Buffer,
|
||||
super::AccelerationStructure,
|
||||
>,
|
||||
>,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
@@ -1608,7 +1608,7 @@ impl crate::Device for super::Device {
|
||||
}
|
||||
unsafe fn get_acceleration_structure_build_sizes<'a>(
|
||||
&self,
|
||||
_desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Api>,
|
||||
_desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Buffer>,
|
||||
) -> crate::AccelerationStructureBuildSizes {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
+35
-25
@@ -910,7 +910,7 @@ pub trait Device: WasmNotSendSync {
|
||||
) -> Result<<Self::A as Api>::AccelerationStructure, DeviceError>;
|
||||
unsafe fn get_acceleration_structure_build_sizes(
|
||||
&self,
|
||||
desc: &GetAccelerationStructureBuildSizesDescriptor<Self::A>,
|
||||
desc: &GetAccelerationStructureBuildSizesDescriptor<<Self::A as Api>::Buffer>,
|
||||
) -> AccelerationStructureBuildSizes;
|
||||
unsafe fn get_acceleration_structure_device_address(
|
||||
&self,
|
||||
@@ -1352,7 +1352,13 @@ pub trait CommandEncoder: WasmNotSendSync + fmt::Debug {
|
||||
descriptors: T,
|
||||
) where
|
||||
Self::A: 'a,
|
||||
T: IntoIterator<Item = BuildAccelerationStructureDescriptor<'a, Self::A>>;
|
||||
T: IntoIterator<
|
||||
Item = BuildAccelerationStructureDescriptor<
|
||||
'a,
|
||||
<Self::A as Api>::Buffer,
|
||||
<Self::A as Api>::AccelerationStructure,
|
||||
>,
|
||||
>;
|
||||
|
||||
unsafe fn place_acceleration_structure_barrier(
|
||||
&mut self,
|
||||
@@ -2192,13 +2198,17 @@ pub struct AccelerationStructureBuildSizes {
|
||||
/// Updates use source_acceleration_structure if present, else the update will be performed in place.
|
||||
/// For updates, only the data is allowed to change (not the meta data or sizes).
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct BuildAccelerationStructureDescriptor<'a, A: Api> {
|
||||
pub entries: &'a AccelerationStructureEntries<'a, A>,
|
||||
pub struct BuildAccelerationStructureDescriptor<
|
||||
'a,
|
||||
B: DynBuffer + ?Sized,
|
||||
A: DynAccelerationStructure + ?Sized,
|
||||
> {
|
||||
pub entries: &'a AccelerationStructureEntries<'a, B>,
|
||||
pub mode: AccelerationStructureBuildMode,
|
||||
pub flags: AccelerationStructureBuildFlags,
|
||||
pub source_acceleration_structure: Option<&'a A::AccelerationStructure>,
|
||||
pub destination_acceleration_structure: &'a A::AccelerationStructure,
|
||||
pub scratch_buffer: &'a A::Buffer,
|
||||
pub source_acceleration_structure: Option<&'a A>,
|
||||
pub destination_acceleration_structure: &'a A,
|
||||
pub scratch_buffer: &'a B,
|
||||
pub scratch_buffer_offset: wgt::BufferAddress,
|
||||
}
|
||||
|
||||
@@ -2208,8 +2218,8 @@ pub struct BuildAccelerationStructureDescriptor<'a, A: Api> {
|
||||
/// may result in reduced size requirements.
|
||||
/// - Any other change may result in a bigger or smaller size requirement.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GetAccelerationStructureBuildSizesDescriptor<'a, A: Api> {
|
||||
pub entries: &'a AccelerationStructureEntries<'a, A>,
|
||||
pub struct GetAccelerationStructureBuildSizesDescriptor<'a, B: DynBuffer + ?Sized> {
|
||||
pub entries: &'a AccelerationStructureEntries<'a, B>,
|
||||
pub flags: AccelerationStructureBuildFlags,
|
||||
}
|
||||
|
||||
@@ -2218,31 +2228,31 @@ pub struct GetAccelerationStructureBuildSizesDescriptor<'a, A: Api> {
|
||||
/// * `Triangles` - Multiple triangle meshes for a bottom level acceleration structure
|
||||
/// * `AABBs` - List of list of axis aligned bounding boxes for a bottom level acceleration structure
|
||||
#[derive(Debug)]
|
||||
pub enum AccelerationStructureEntries<'a, A: Api> {
|
||||
Instances(AccelerationStructureInstances<'a, A>),
|
||||
Triangles(Vec<AccelerationStructureTriangles<'a, A>>),
|
||||
AABBs(Vec<AccelerationStructureAABBs<'a, A>>),
|
||||
pub enum AccelerationStructureEntries<'a, B: DynBuffer + ?Sized> {
|
||||
Instances(AccelerationStructureInstances<'a, B>),
|
||||
Triangles(Vec<AccelerationStructureTriangles<'a, B>>),
|
||||
AABBs(Vec<AccelerationStructureAABBs<'a, B>>),
|
||||
}
|
||||
|
||||
/// * `first_vertex` - offset in the vertex buffer (as number of vertices)
|
||||
/// * `indices` - optional index buffer with attributes
|
||||
/// * `transform` - optional transform
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AccelerationStructureTriangles<'a, A: Api> {
|
||||
pub vertex_buffer: Option<&'a A::Buffer>,
|
||||
pub struct AccelerationStructureTriangles<'a, B: DynBuffer + ?Sized> {
|
||||
pub vertex_buffer: Option<&'a B>,
|
||||
pub vertex_format: wgt::VertexFormat,
|
||||
pub first_vertex: u32,
|
||||
pub vertex_count: u32,
|
||||
pub vertex_stride: wgt::BufferAddress,
|
||||
pub indices: Option<AccelerationStructureTriangleIndices<'a, A>>,
|
||||
pub transform: Option<AccelerationStructureTriangleTransform<'a, A>>,
|
||||
pub indices: Option<AccelerationStructureTriangleIndices<'a, B>>,
|
||||
pub transform: Option<AccelerationStructureTriangleTransform<'a, B>>,
|
||||
pub flags: AccelerationStructureGeometryFlags,
|
||||
}
|
||||
|
||||
/// * `offset` - offset in bytes
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AccelerationStructureAABBs<'a, A: Api> {
|
||||
pub buffer: Option<&'a A::Buffer>,
|
||||
pub struct AccelerationStructureAABBs<'a, B: DynBuffer + ?Sized> {
|
||||
pub buffer: Option<&'a B>,
|
||||
pub offset: u32,
|
||||
pub count: u32,
|
||||
pub stride: wgt::BufferAddress,
|
||||
@@ -2251,25 +2261,25 @@ pub struct AccelerationStructureAABBs<'a, A: Api> {
|
||||
|
||||
/// * `offset` - offset in bytes
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AccelerationStructureInstances<'a, A: Api> {
|
||||
pub buffer: Option<&'a A::Buffer>,
|
||||
pub struct AccelerationStructureInstances<'a, B: DynBuffer + ?Sized> {
|
||||
pub buffer: Option<&'a B>,
|
||||
pub offset: u32,
|
||||
pub count: u32,
|
||||
}
|
||||
|
||||
/// * `offset` - offset in bytes
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AccelerationStructureTriangleIndices<'a, A: Api> {
|
||||
pub struct AccelerationStructureTriangleIndices<'a, B: DynBuffer + ?Sized> {
|
||||
pub format: wgt::IndexFormat,
|
||||
pub buffer: Option<&'a A::Buffer>,
|
||||
pub buffer: Option<&'a B>,
|
||||
pub offset: u32,
|
||||
pub count: u32,
|
||||
}
|
||||
|
||||
/// * `offset` - offset in bytes
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AccelerationStructureTriangleTransform<'a, A: Api> {
|
||||
pub buffer: &'a A::Buffer,
|
||||
pub struct AccelerationStructureTriangleTransform<'a, B: DynBuffer + ?Sized> {
|
||||
pub buffer: &'a B,
|
||||
pub offset: u32,
|
||||
}
|
||||
|
||||
|
||||
@@ -1260,7 +1260,13 @@ impl crate::CommandEncoder for super::CommandEncoder {
|
||||
_descriptors: T,
|
||||
) where
|
||||
super::Api: 'a,
|
||||
T: IntoIterator<Item = crate::BuildAccelerationStructureDescriptor<'a, super::Api>>,
|
||||
T: IntoIterator<
|
||||
Item = crate::BuildAccelerationStructureDescriptor<
|
||||
'a,
|
||||
super::Buffer,
|
||||
super::AccelerationStructure,
|
||||
>,
|
||||
>,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
@@ -1394,7 +1394,7 @@ impl crate::Device for super::Device {
|
||||
|
||||
unsafe fn get_acceleration_structure_build_sizes(
|
||||
&self,
|
||||
_desc: &crate::GetAccelerationStructureBuildSizesDescriptor<super::Api>,
|
||||
_desc: &crate::GetAccelerationStructureBuildSizesDescriptor<super::Buffer>,
|
||||
) -> crate::AccelerationStructureBuildSizes {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
@@ -408,7 +408,13 @@ impl crate::CommandEncoder for super::CommandEncoder {
|
||||
unsafe fn build_acceleration_structures<'a, T>(&mut self, descriptor_count: u32, descriptors: T)
|
||||
where
|
||||
super::Api: 'a,
|
||||
T: IntoIterator<Item = crate::BuildAccelerationStructureDescriptor<'a, super::Api>>,
|
||||
T: IntoIterator<
|
||||
Item = crate::BuildAccelerationStructureDescriptor<
|
||||
'a,
|
||||
super::Buffer,
|
||||
super::AccelerationStructure,
|
||||
>,
|
||||
>,
|
||||
{
|
||||
const CAPACITY_OUTER: usize = 8;
|
||||
const CAPACITY_INNER: usize = 1;
|
||||
|
||||
@@ -2176,7 +2176,7 @@ impl crate::Device for super::Device {
|
||||
|
||||
unsafe fn get_acceleration_structure_build_sizes<'a>(
|
||||
&self,
|
||||
desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Api>,
|
||||
desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Buffer>,
|
||||
) -> crate::AccelerationStructureBuildSizes {
|
||||
const CAPACITY: usize = 8;
|
||||
|
||||
|
||||
Reference in new issue
Block a user