DynDevice create/destroy bind group

bindgroup fixup
This commit is contained in:
Andreas Reich 2024-07-27 13:12:47 +02:00
parent 33f57e2305
commit 276753f963
13 changed files with 158 additions and 32 deletions

View File

@ -2072,7 +2072,7 @@ impl<A: HalApi> Device<A> {
used: &mut BindGroupStates<A>,
used_texture_ranges: &mut Vec<TextureInitTrackerAction<A>>,
snatch_guard: &'a SnatchGuard<'a>,
) -> Result<hal::TextureBinding<'a, A>, binding_model::CreateBindGroupError> {
) -> Result<hal::TextureBinding<'a, A::TextureView>, binding_model::CreateBindGroupError> {
view.same_device(self)?;
let (pub_usage, internal_use) = self.texture_use_parameters(

View File

@ -1104,7 +1104,13 @@ impl crate::Device for super::Device {
unsafe fn create_bind_group(
&self,
desc: &crate::BindGroupDescriptor<super::Api>,
desc: &crate::BindGroupDescriptor<
super::BindGroupLayout,
super::Buffer,
super::Sampler,
super::TextureView,
super::AccelerationStructure,
>,
) -> Result<super::BindGroup, DeviceError> {
let mut cpu_views = desc
.layout

View File

@ -88,6 +88,7 @@ impl crate::Api for Api {
}
crate::impl_dyn_resource!(
AccelerationStructure,
BindGroup,
BindGroupLayout,
Buffer,
@ -671,6 +672,8 @@ unsafe impl Sync for ComputePipeline {}
#[derive(Debug)]
pub struct AccelerationStructure {}
impl crate::DynAccelerationStructure for AccelerationStructure {}
impl SwapChain {
unsafe fn release_resources(self) -> d3d12::ComPtr<dxgi1_4::IDXGISwapChain3> {
self.raw

View File

@ -2,14 +2,14 @@
#![allow(trivial_casts)]
use crate::{
Api, BindGroupLayoutDescriptor, BufferDescriptor, BufferMapping, CommandEncoderDescriptor,
Device, DeviceError, DynBuffer, DynResource, MemoryRange, PipelineLayoutDescriptor,
SamplerDescriptor, TextureDescriptor, TextureViewDescriptor,
Api, BindGroupDescriptor, BindGroupLayoutDescriptor, BufferDescriptor, BufferMapping,
CommandEncoderDescriptor, Device, DeviceError, DynBuffer, DynResource, MemoryRange,
PipelineLayoutDescriptor, SamplerDescriptor, TextureDescriptor, TextureViewDescriptor,
};
use super::{
DynBindGroupLayout, DynCommandEncoder, DynPipelineLayout, DynQueue, DynResourceExt as _,
DynSampler, DynTexture, DynTextureView,
DynAccelerationStructure, DynBindGroup, DynBindGroupLayout, DynCommandEncoder,
DynPipelineLayout, DynQueue, DynResourceExt as _, DynSampler, DynTexture, DynTextureView,
};
pub trait DynDevice: DynResource {
@ -65,6 +65,18 @@ pub trait DynDevice: DynResource {
desc: &PipelineLayoutDescriptor<dyn DynBindGroupLayout>,
) -> Result<Box<dyn DynPipelineLayout>, DeviceError>;
unsafe fn destroy_pipeline_layout(&self, pipeline_layout: Box<dyn DynPipelineLayout>);
unsafe fn create_bind_group(
&self,
desc: &BindGroupDescriptor<
dyn DynBindGroupLayout,
dyn DynBuffer,
dyn DynSampler,
dyn DynTextureView,
dyn DynAccelerationStructure,
>,
) -> Result<Box<dyn DynBindGroup>, DeviceError>;
unsafe fn destroy_bind_group(&self, group: Box<dyn DynBindGroup>);
}
impl<D: Device + DynResource> DynDevice for D {
@ -201,4 +213,52 @@ impl<D: Device + DynResource> DynDevice for D {
unsafe fn destroy_pipeline_layout(&self, pipeline_layout: Box<dyn DynPipelineLayout>) {
unsafe { D::destroy_pipeline_layout(self, pipeline_layout.unbox()) };
}
unsafe fn create_bind_group(
&self,
desc: &BindGroupDescriptor<
dyn DynBindGroupLayout,
dyn DynBuffer,
dyn DynSampler,
dyn DynTextureView,
dyn DynAccelerationStructure,
>,
) -> Result<Box<dyn DynBindGroup>, DeviceError> {
let buffers: Vec<_> = desc
.buffers
.iter()
.map(|b| b.clone().expect_downcast())
.collect();
let samplers: Vec<_> = desc
.samplers
.iter()
.map(|s| s.expect_downcast_ref())
.collect();
let textures: Vec<_> = desc
.textures
.iter()
.map(|t| t.clone().expect_downcast())
.collect();
let acceleration_structures: Vec<_> = desc
.acceleration_structures
.iter()
.map(|a| a.expect_downcast_ref())
.collect();
let desc = BindGroupDescriptor {
label: desc.label.to_owned(),
layout: desc.layout.expect_downcast_ref(),
buffers: &buffers,
samplers: &samplers,
textures: &textures,
entries: desc.entries,
acceleration_structures: &acceleration_structures,
};
unsafe { D::create_bind_group(self, &desc) }.map(|b| Box::new(b) as Box<dyn DynBindGroup>)
}
unsafe fn destroy_bind_group(&self, group: Box<dyn DynBindGroup>) {
unsafe { D::destroy_bind_group(self, group.unbox()) };
}
}

View File

@ -12,7 +12,7 @@ use std::any::Any;
use wgt::WasmNotSendSync;
use crate::BufferBinding;
use crate::{BufferBinding, TextureBinding};
/// Base trait for all resources, allows downcasting via [`Any`].
pub trait DynResource: Any + WasmNotSendSync + 'static {
@ -121,3 +121,12 @@ impl<'a> BufferBinding<'a, dyn DynBuffer> {
}
}
}
impl<'a> TextureBinding<'a, dyn DynTextureView> {
pub fn expect_downcast<T: DynTextureView>(self) -> TextureBinding<'a, T> {
TextureBinding {
view: self.view.expect_downcast_ref(),
usage: self.usage,
}
}
}

View File

@ -42,6 +42,7 @@ impl crate::Api for Api {
crate::impl_dyn_resource!(Context, Encoder, Resource);
impl crate::DynAccelerationStructure for Resource {}
impl crate::DynBindGroup for Resource {}
impl crate::DynBindGroupLayout for Resource {}
impl crate::DynBuffer for Resource {}
@ -218,7 +219,7 @@ impl crate::Device for Context {
unsafe fn destroy_pipeline_layout(&self, pipeline_layout: Resource) {}
unsafe fn create_bind_group(
&self,
desc: &crate::BindGroupDescriptor<Api>,
desc: &crate::BindGroupDescriptor<Resource, Resource, Resource, Resource, Resource>,
) -> DeviceResult<Resource> {
Ok(Resource)
}

View File

@ -1232,7 +1232,13 @@ impl crate::Device for super::Device {
unsafe fn create_bind_group(
&self,
desc: &crate::BindGroupDescriptor<super::Api>,
desc: &crate::BindGroupDescriptor<
super::BindGroupLayout,
super::Buffer,
super::Sampler,
super::TextureView,
super::AccelerationStructure,
>,
) -> Result<super::BindGroup, crate::DeviceError> {
let mut contents = Vec::new();
@ -1589,7 +1595,7 @@ impl crate::Device for super::Device {
unsafe fn create_acceleration_structure(
&self,
_desc: &crate::AccelerationStructureDescriptor,
) -> Result<(), crate::DeviceError> {
) -> Result<super::AccelerationStructure, crate::DeviceError> {
unimplemented!()
}
unsafe fn get_acceleration_structure_build_sizes<'a>(
@ -1600,11 +1606,15 @@ impl crate::Device for super::Device {
}
unsafe fn get_acceleration_structure_device_address(
&self,
_acceleration_structure: &(),
_acceleration_structure: &super::AccelerationStructure,
) -> wgt::BufferAddress {
unimplemented!()
}
unsafe fn destroy_acceleration_structure(&self, _acceleration_structure: ()) {}
unsafe fn destroy_acceleration_structure(
&self,
_acceleration_structure: super::AccelerationStructure,
) {
}
fn get_internal_counters(&self) -> wgt::HalCounters {
self.counters.clone()

View File

@ -153,7 +153,7 @@ impl crate::Api for Api {
type Sampler = Sampler;
type QuerySet = QuerySet;
type Fence = Fence;
type AccelerationStructure = ();
type AccelerationStructure = AccelerationStructure;
type PipelineCache = ();
type BindGroupLayout = BindGroupLayout;
@ -165,6 +165,7 @@ impl crate::Api for Api {
}
crate::impl_dyn_resource!(
AccelerationStructure,
BindGroup,
BindGroupLayout,
Buffer,
@ -750,6 +751,11 @@ impl Fence {
}
}
#[derive(Debug)]
pub struct AccelerationStructure;
impl crate::DynAccelerationStructure for AccelerationStructure {}
#[derive(Clone, Debug, PartialEq)]
struct StencilOps {
pass: u32,

View File

@ -443,7 +443,7 @@ pub trait Api: Clone + fmt::Debug + Sized {
type ComputePipeline: DynComputePipeline;
type PipelineCache: fmt::Debug + WasmNotSendSync;
type AccelerationStructure: fmt::Debug + WasmNotSendSync + 'static;
type AccelerationStructure: DynAccelerationStructure + fmt::Debug + 'static;
}
pub trait Instance: Sized + WasmNotSendSync {
@ -804,9 +804,17 @@ pub trait Device: WasmNotSendSync {
desc: &PipelineLayoutDescriptor<<Self::A as Api>::BindGroupLayout>,
) -> Result<<Self::A as Api>::PipelineLayout, DeviceError>;
unsafe fn destroy_pipeline_layout(&self, pipeline_layout: <Self::A as Api>::PipelineLayout);
#[allow(clippy::type_complexity)]
unsafe fn create_bind_group(
&self,
desc: &BindGroupDescriptor<Self::A>,
desc: &BindGroupDescriptor<
<Self::A as Api>::BindGroupLayout,
<Self::A as Api>::Buffer,
<Self::A as Api>::Sampler,
<Self::A as Api>::TextureView,
<Self::A as Api>::AccelerationStructure,
>,
) -> Result<<Self::A as Api>::BindGroup, DeviceError>;
unsafe fn destroy_bind_group(&self, group: <Self::A as Api>::BindGroup);
@ -1776,10 +1784,9 @@ pub struct BufferBinding<'a, B: DynBuffer + ?Sized> {
pub size: Option<wgt::BufferSize>,
}
// Rust gets confused about the impl requirements for `A`
impl<B: DynBuffer> Clone for BufferBinding<'_, B> {
impl<'a, T: DynBuffer + ?Sized> Clone for BufferBinding<'a, T> {
fn clone(&self) -> Self {
Self {
BufferBinding {
buffer: self.buffer,
offset: self.offset,
size: self.size,
@ -1788,15 +1795,14 @@ impl<B: DynBuffer> Clone for BufferBinding<'_, B> {
}
#[derive(Debug)]
pub struct TextureBinding<'a, A: Api> {
pub view: &'a A::TextureView,
pub struct TextureBinding<'a, T: DynTextureView + ?Sized> {
pub view: &'a T,
pub usage: TextureUses,
}
// Rust gets confused about the impl requirements for `A`
impl<A: Api> Clone for TextureBinding<'_, A> {
impl<'a, T: DynTextureView + ?Sized> Clone for TextureBinding<'a, T> {
fn clone(&self) -> Self {
Self {
TextureBinding {
view: self.view,
usage: self.usage,
}
@ -1820,14 +1826,21 @@ pub struct BindGroupEntry {
/// of the corresponding resource array, selected by the relevant
/// `BindGroupLayoutEntry`.
#[derive(Clone, Debug)]
pub struct BindGroupDescriptor<'a, A: Api> {
pub struct BindGroupDescriptor<
'a,
Bgl: DynBindGroupLayout + ?Sized,
B: DynBuffer + ?Sized,
S: DynSampler + ?Sized,
T: DynTextureView + ?Sized,
A: DynAccelerationStructure + ?Sized,
> {
pub label: Label<'a>,
pub layout: &'a A::BindGroupLayout,
pub buffers: &'a [BufferBinding<'a, A::Buffer>],
pub samplers: &'a [&'a A::Sampler],
pub textures: &'a [TextureBinding<'a, A>],
pub layout: &'a Bgl,
pub buffers: &'a [BufferBinding<'a, B>],
pub samplers: &'a [&'a S],
pub textures: &'a [TextureBinding<'a, T>],
pub entries: &'a [BindGroupEntry],
pub acceleration_structures: &'a [&'a A::AccelerationStructure],
pub acceleration_structures: &'a [&'a A],
}
#[derive(Clone, Debug)]

View File

@ -776,7 +776,13 @@ impl crate::Device for super::Device {
unsafe fn create_bind_group(
&self,
desc: &crate::BindGroupDescriptor<super::Api>,
desc: &crate::BindGroupDescriptor<
super::BindGroupLayout,
super::Buffer,
super::Sampler,
super::TextureView,
super::AccelerationStructure,
>,
) -> DeviceResult<super::BindGroup> {
let mut bg = super::BindGroup::default();
for (&stage, counter) in super::NAGA_STAGES.iter().zip(bg.counters.iter_mut()) {

View File

@ -72,6 +72,7 @@ impl crate::Api for Api {
}
crate::impl_dyn_resource!(
AccelerationStructure,
BindGroup,
BindGroupLayout,
Buffer,
@ -931,3 +932,5 @@ unsafe impl Sync for CommandBuffer {}
#[derive(Debug)]
pub struct AccelerationStructure;
impl crate::DynAccelerationStructure for AccelerationStructure {}

View File

@ -1453,7 +1453,13 @@ impl crate::Device for super::Device {
unsafe fn create_bind_group(
&self,
desc: &crate::BindGroupDescriptor<super::Api>,
desc: &crate::BindGroupDescriptor<
super::BindGroupLayout,
super::Buffer,
super::Sampler,
super::TextureView,
super::AccelerationStructure,
>,
) -> Result<super::BindGroup, crate::DeviceError> {
let mut vk_sets = unsafe {
self.desc_allocator.lock().allocate(

View File

@ -79,6 +79,7 @@ impl crate::Api for Api {
}
crate::impl_dyn_resource!(
AccelerationStructure,
BindGroup,
BindGroupLayout,
Buffer,
@ -670,6 +671,8 @@ pub struct AccelerationStructure {
block: Mutex<gpu_alloc::MemoryBlock<vk::DeviceMemory>>,
}
impl crate::DynAccelerationStructure for AccelerationStructure {}
#[derive(Debug)]
pub struct Texture {
raw: vk::Image,