Introduce DynComputePipeline & DynRenderPipeline

This commit is contained in:
Andreas Reich 2024-07-15 23:34:17 +02:00
parent 50a1811229
commit fc764b4b17
8 changed files with 238 additions and 6 deletions

View File

@ -629,6 +629,8 @@ pub struct RenderPipeline {
vertex_strides: [Option<NonZeroU32>; crate::MAX_VERTEX_BUFFERS], vertex_strides: [Option<NonZeroU32>; crate::MAX_VERTEX_BUFFERS],
} }
impl crate::DynRenderPipeline for RenderPipeline {}
unsafe impl Send for RenderPipeline {} unsafe impl Send for RenderPipeline {}
unsafe impl Sync for RenderPipeline {} unsafe impl Sync for RenderPipeline {}
@ -638,6 +640,8 @@ pub struct ComputePipeline {
layout: PipelineLayoutShared, layout: PipelineLayoutShared,
} }
impl crate::DynComputePipeline for ComputePipeline {}
unsafe impl Send for ComputePipeline {} unsafe impl Send for ComputePipeline {}
unsafe impl Sync for ComputePipeline {} unsafe impl Sync for ComputePipeline {}

View File

@ -1,11 +1,13 @@
use std::ops::Range; use std::ops::Range;
use crate::{ use crate::{
BufferBarrier, BufferBinding, BufferCopy, CommandEncoder, DeviceError, DynBindGroup, BufferBarrier, BufferBinding, BufferCopy, CommandEncoder, DeviceError, Label, MemoryRange, Rect,
DynPipelineLayout, DynQuerySet, Label, MemoryRange,
}; };
use super::{DynBuffer, DynResourceExt as _}; use super::{
DynBindGroup, DynBuffer, DynComputePipeline, DynPipelineLayout, DynQuerySet, DynRenderPipeline,
DynResourceExt as _,
};
pub trait DynCommandEncoder: std::fmt::Debug { pub trait DynCommandEncoder: std::fmt::Debug {
unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError>; unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError>;
@ -56,6 +58,8 @@ pub trait DynCommandEncoder: std::fmt::Debug {
stride: wgt::BufferSize, stride: wgt::BufferSize,
); );
unsafe fn set_render_pipeline(&mut self, pipeline: &dyn DynRenderPipeline);
unsafe fn set_index_buffer<'a>( unsafe fn set_index_buffer<'a>(
&mut self, &mut self,
binding: BufferBinding<'a, dyn DynBuffer>, binding: BufferBinding<'a, dyn DynBuffer>,
@ -67,6 +71,75 @@ pub trait DynCommandEncoder: std::fmt::Debug {
index: u32, index: u32,
binding: BufferBinding<'a, dyn DynBuffer>, binding: BufferBinding<'a, dyn DynBuffer>,
); );
unsafe fn set_viewport(&mut self, rect: &Rect<f32>, depth_range: Range<f32>);
unsafe fn set_scissor_rect(&mut self, rect: &Rect<u32>);
unsafe fn set_stencil_reference(&mut self, value: u32);
unsafe fn set_blend_constants(&mut self, color: &[f32; 4]);
unsafe fn draw(
&mut self,
first_vertex: u32,
vertex_count: u32,
first_instance: u32,
instance_count: u32,
);
unsafe fn draw_indexed(
&mut self,
first_index: u32,
index_count: u32,
base_vertex: i32,
first_instance: u32,
instance_count: u32,
);
unsafe fn draw_indirect(
&mut self,
buffer: &dyn DynBuffer,
offset: wgt::BufferAddress,
draw_count: u32,
);
unsafe fn draw_indexed_indirect(
&mut self,
buffer: &dyn DynBuffer,
offset: wgt::BufferAddress,
draw_count: u32,
);
unsafe fn draw_indirect_count(
&mut self,
buffer: &dyn DynBuffer,
offset: wgt::BufferAddress,
count_buffer: &dyn DynBuffer,
count_offset: wgt::BufferAddress,
max_count: u32,
);
unsafe fn draw_indexed_indirect_count(
&mut self,
buffer: &dyn DynBuffer,
offset: wgt::BufferAddress,
count_buffer: &dyn DynBuffer,
count_offset: wgt::BufferAddress,
max_count: u32,
);
// unsafe fn begin_compute_pass(&mut self, desc: &ComputePassDescriptor<Self::A>);
// unsafe fn end_compute_pass(&mut self);
unsafe fn set_compute_pipeline(&mut self, pipeline: &dyn DynComputePipeline);
unsafe fn dispatch(&mut self, count: [u32; 3]);
unsafe fn dispatch_indirect(&mut self, buffer: &dyn DynBuffer, offset: wgt::BufferAddress);
// unsafe fn build_acceleration_structures<'a, T>(
// &mut self,
// descriptor_count: u32,
// descriptors: T,
// ) where
// Self::A: 'a,
// T: IntoIterator<Item = BuildAccelerationStructureDescriptor<'a, Self::A>>;
// unsafe fn place_acceleration_structure_barrier(
// &mut self,
// barrier: AccelerationStructureBarrier,
// );
} }
impl<C: CommandEncoder> DynCommandEncoder for C { impl<C: CommandEncoder> DynCommandEncoder for C {
@ -178,6 +251,142 @@ impl<C: CommandEncoder> DynCommandEncoder for C {
unsafe { C::copy_query_results(self, set, range, buffer, offset, stride) }; unsafe { C::copy_query_results(self, set, range, buffer, offset, stride) };
} }
unsafe fn set_viewport(&mut self, rect: &Rect<f32>, depth_range: Range<f32>) {
unsafe {
C::set_viewport(self, rect, depth_range);
}
}
unsafe fn set_scissor_rect(&mut self, rect: &Rect<u32>) {
unsafe {
C::set_scissor_rect(self, rect);
}
}
unsafe fn set_stencil_reference(&mut self, value: u32) {
unsafe {
C::set_stencil_reference(self, value);
}
}
unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) {
unsafe { C::set_blend_constants(self, color) };
}
unsafe fn draw(
&mut self,
first_vertex: u32,
vertex_count: u32,
first_instance: u32,
instance_count: u32,
) {
unsafe {
C::draw(
self,
first_vertex,
vertex_count,
first_instance,
instance_count,
)
};
}
unsafe fn draw_indexed(
&mut self,
first_index: u32,
index_count: u32,
base_vertex: i32,
first_instance: u32,
instance_count: u32,
) {
unsafe {
C::draw_indexed(
self,
first_index,
index_count,
base_vertex,
first_instance,
instance_count,
)
};
}
unsafe fn draw_indirect(
&mut self,
buffer: &dyn DynBuffer,
offset: wgt::BufferAddress,
draw_count: u32,
) {
let buffer = buffer.expect_downcast_ref();
unsafe { C::draw_indirect(self, buffer, offset, draw_count) };
}
unsafe fn draw_indexed_indirect(
&mut self,
buffer: &dyn DynBuffer,
offset: wgt::BufferAddress,
draw_count: u32,
) {
let buffer = buffer.expect_downcast_ref();
unsafe { C::draw_indexed_indirect(self, buffer, offset, draw_count) };
}
unsafe fn draw_indirect_count(
&mut self,
buffer: &dyn DynBuffer,
offset: wgt::BufferAddress,
count_buffer: &dyn DynBuffer,
count_offset: wgt::BufferAddress,
max_count: u32,
) {
let buffer = buffer.expect_downcast_ref();
let count_buffer = count_buffer.expect_downcast_ref();
unsafe {
C::draw_indirect_count(self, buffer, offset, count_buffer, count_offset, max_count)
};
}
unsafe fn draw_indexed_indirect_count(
&mut self,
buffer: &dyn DynBuffer,
offset: wgt::BufferAddress,
count_buffer: &dyn DynBuffer,
count_offset: wgt::BufferAddress,
max_count: u32,
) {
let buffer = buffer.expect_downcast_ref();
let count_buffer = count_buffer.expect_downcast_ref();
unsafe {
C::draw_indexed_indirect_count(
self,
buffer,
offset,
count_buffer,
count_offset,
max_count,
)
};
}
unsafe fn set_compute_pipeline(&mut self, pipeline: &dyn DynComputePipeline) {
let pipeline = pipeline.expect_downcast_ref();
unsafe { C::set_compute_pipeline(self, pipeline) };
}
unsafe fn dispatch(&mut self, count: [u32; 3]) {
unsafe { C::dispatch(self, count) };
}
unsafe fn dispatch_indirect(&mut self, buffer: &dyn DynBuffer, offset: wgt::BufferAddress) {
let buffer = buffer.expect_downcast_ref();
unsafe { C::dispatch_indirect(self, buffer, offset) };
}
unsafe fn set_render_pipeline(&mut self, pipeline: &dyn DynRenderPipeline) {
let pipeline = pipeline.expect_downcast_ref();
unsafe { C::set_render_pipeline(self, pipeline) };
}
unsafe fn set_index_buffer<'a>( unsafe fn set_index_buffer<'a>(
&mut self, &mut self,
binding: BufferBinding<'a, dyn DynBuffer>, binding: BufferBinding<'a, dyn DynBuffer>,

View File

@ -90,6 +90,8 @@ pub trait DynBindGroup: DynResource + std::fmt::Debug {}
pub trait DynBuffer: DynResource + std::fmt::Debug {} pub trait DynBuffer: DynResource + std::fmt::Debug {}
pub trait DynPipelineLayout: DynResource + std::fmt::Debug {} pub trait DynPipelineLayout: DynResource + std::fmt::Debug {}
pub trait DynQuerySet: DynResource + std::fmt::Debug {} pub trait DynQuerySet: DynResource + std::fmt::Debug {}
pub trait DynRenderPipeline: DynResource + std::fmt::Debug {}
pub trait DynComputePipeline: DynResource + std::fmt::Debug {}
impl<'a> BufferBinding<'a, dyn DynBuffer> { impl<'a> BufferBinding<'a, dyn DynBuffer> {
pub fn expect_downcast<B: DynBuffer>(self) -> BufferBinding<'a, B> { pub fn expect_downcast<B: DynBuffer>(self) -> BufferBinding<'a, B> {

View File

@ -46,6 +46,8 @@ impl crate::DynBindGroup for Resource {}
impl crate::DynBuffer for Resource {} impl crate::DynBuffer for Resource {}
impl crate::DynPipelineLayout for Resource {} impl crate::DynPipelineLayout for Resource {}
impl crate::DynQuerySet for Resource {} impl crate::DynQuerySet for Resource {}
impl crate::DynRenderPipeline for Resource {}
impl crate::DynComputePipeline for Resource {}
impl crate::Instance for Context { impl crate::Instance for Context {
type A = Api; type A = Api;

View File

@ -653,6 +653,8 @@ pub struct RenderPipeline {
alpha_to_coverage_enabled: bool, alpha_to_coverage_enabled: bool,
} }
impl crate::DynRenderPipeline for RenderPipeline {}
#[cfg(send_sync)] #[cfg(send_sync)]
unsafe impl Sync for RenderPipeline {} unsafe impl Sync for RenderPipeline {}
#[cfg(send_sync)] #[cfg(send_sync)]
@ -663,6 +665,8 @@ pub struct ComputePipeline {
inner: Arc<PipelineInner>, inner: Arc<PipelineInner>,
} }
impl crate::DynComputePipeline for ComputePipeline {}
#[cfg(send_sync)] #[cfg(send_sync)]
unsafe impl Sync for ComputePipeline {} unsafe impl Sync for ComputePipeline {}
#[cfg(send_sync)] #[cfg(send_sync)]

View File

@ -265,7 +265,10 @@ pub mod api {
mod dynamic; mod dynamic;
pub(crate) use dynamic::{impl_dyn_resource, DynResource}; pub(crate) use dynamic::{impl_dyn_resource, DynResource};
pub use dynamic::{DynBindGroup, DynBuffer, DynCommandEncoder, DynPipelineLayout, DynQuerySet}; pub use dynamic::{
DynBindGroup, DynBuffer, DynCommandEncoder, DynComputePipeline, DynPipelineLayout, DynQuerySet,
DynRenderPipeline,
};
use std::{ use std::{
borrow::{Borrow, Cow}, borrow::{Borrow, Cow},
@ -434,8 +437,8 @@ pub trait Api: Clone + fmt::Debug + Sized {
type BindGroup: DynBindGroup; type BindGroup: DynBindGroup;
type PipelineLayout: DynPipelineLayout; type PipelineLayout: DynPipelineLayout;
type ShaderModule: fmt::Debug + WasmNotSendSync; type ShaderModule: fmt::Debug + WasmNotSendSync;
type RenderPipeline: fmt::Debug + WasmNotSendSync; type RenderPipeline: DynRenderPipeline;
type ComputePipeline: fmt::Debug + WasmNotSendSync; type ComputePipeline: DynComputePipeline;
type PipelineCache: fmt::Debug + WasmNotSendSync; type PipelineCache: fmt::Debug + WasmNotSendSync;
type AccelerationStructure: fmt::Debug + WasmNotSendSync + 'static; type AccelerationStructure: fmt::Debug + WasmNotSendSync + 'static;

View File

@ -767,6 +767,8 @@ pub struct RenderPipeline {
unsafe impl Send for RenderPipeline {} unsafe impl Send for RenderPipeline {}
unsafe impl Sync for RenderPipeline {} unsafe impl Sync for RenderPipeline {}
impl crate::DynRenderPipeline for RenderPipeline {}
#[derive(Debug)] #[derive(Debug)]
pub struct ComputePipeline { pub struct ComputePipeline {
raw: metal::ComputePipelineState, raw: metal::ComputePipelineState,
@ -780,6 +782,8 @@ pub struct ComputePipeline {
unsafe impl Send for ComputePipeline {} unsafe impl Send for ComputePipeline {}
unsafe impl Sync for ComputePipeline {} unsafe impl Sync for ComputePipeline {}
impl crate::DynComputePipeline for ComputePipeline {}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct QuerySet { pub struct QuerySet {
raw_buffer: metal::Buffer, raw_buffer: metal::Buffer,

View File

@ -822,11 +822,15 @@ pub struct RenderPipeline {
raw: vk::Pipeline, raw: vk::Pipeline,
} }
impl crate::DynRenderPipeline for RenderPipeline {}
#[derive(Debug)] #[derive(Debug)]
pub struct ComputePipeline { pub struct ComputePipeline {
raw: vk::Pipeline, raw: vk::Pipeline,
} }
impl crate::DynComputePipeline for ComputePipeline {}
#[derive(Debug)] #[derive(Debug)]
pub struct PipelineCache { pub struct PipelineCache {
raw: vk::PipelineCache, raw: vk::PipelineCache,