Skip to main content

blade_graphics/
traits.rs

1use std::{fmt::Debug, hash::Hash};
2
3pub trait ResourceDevice {
4    type Buffer: Send + Sync + Clone + Copy + Debug + Hash + PartialEq;
5    type Texture: Send + Sync + Clone + Copy + Debug + Hash + PartialEq;
6    type TextureView: Send + Sync + Clone + Copy + Debug + Hash + PartialEq;
7    type Sampler: Send + Sync + Clone + Copy + Debug + Hash + PartialEq;
8    type AccelerationStructure: Send + Sync + Clone + Copy + Debug + Hash + PartialEq;
9
10    fn create_buffer(&self, desc: super::BufferDesc) -> Self::Buffer;
11    fn sync_buffer(&self, buffer: Self::Buffer);
12    fn destroy_buffer(&self, buffer: Self::Buffer);
13    fn create_texture(&self, desc: super::TextureDesc) -> Self::Texture;
14    fn destroy_texture(&self, texture: Self::Texture);
15    fn create_texture_view(
16        &self,
17        texture: Self::Texture,
18        desc: super::TextureViewDesc,
19    ) -> Self::TextureView;
20    fn destroy_texture_view(&self, view: Self::TextureView);
21    fn create_sampler(&self, desc: super::SamplerDesc) -> Self::Sampler;
22    fn destroy_sampler(&self, sampler: Self::Sampler);
23    fn create_acceleration_structure(
24        &self,
25        desc: super::AccelerationStructureDesc,
26    ) -> Self::AccelerationStructure;
27    fn destroy_acceleration_structure(&self, acceleration_structure: Self::AccelerationStructure);
28}
29
30pub trait ComputePipelineBase {
31    fn get_workgroup_size(&self) -> [u32; 3];
32}
33
34pub trait ShaderDevice {
35    type ComputePipeline: Send + Sync + ComputePipelineBase;
36    type RenderPipeline: Send + Sync;
37
38    fn create_compute_pipeline(&self, desc: super::ComputePipelineDesc) -> Self::ComputePipeline;
39    fn destroy_compute_pipeline(&self, pipeline: &mut Self::ComputePipeline);
40    fn create_render_pipeline(&self, desc: super::RenderPipelineDesc) -> Self::RenderPipeline;
41    fn destroy_render_pipeline(&self, pipeline: &mut Self::RenderPipeline);
42}
43
44pub trait CommandDevice {
45    type CommandEncoder;
46    type SyncPoint: Clone + Debug;
47
48    fn create_command_encoder(&self, desc: super::CommandEncoderDesc) -> Self::CommandEncoder;
49    fn destroy_command_encoder(&self, encoder: &mut Self::CommandEncoder);
50    fn submit(&self, encoder: &mut Self::CommandEncoder) -> Self::SyncPoint;
51    fn wait_for(&self, sp: &Self::SyncPoint, timeout_ms: u32) -> Result<bool, super::DeviceError>;
52}
53
54pub trait CommandEncoder {
55    type Texture: Send + Sync + Clone + Copy + Debug;
56    type Frame: Send + Sync + Debug;
57    fn start(&mut self);
58    fn init_texture(&mut self, texture: Self::Texture);
59    fn present(&mut self, frame: Self::Frame);
60    fn timings(&self) -> &super::Timings;
61}
62
63pub trait TransferEncoder {
64    type BufferPiece: Send + Sync + Clone + Copy + Debug;
65    type TexturePiece: Send + Sync + Clone + Copy + Debug;
66
67    fn fill_buffer(&mut self, dst: Self::BufferPiece, size: u64, value: u8);
68    fn copy_buffer_to_buffer(&mut self, src: Self::BufferPiece, dst: Self::BufferPiece, size: u64);
69    fn copy_texture_to_texture(
70        &mut self,
71        src: Self::TexturePiece,
72        dst: Self::TexturePiece,
73        size: super::Extent,
74    );
75
76    fn copy_buffer_to_texture(
77        &mut self,
78        src: Self::BufferPiece,
79        bytes_per_row: u32,
80        dst: Self::TexturePiece,
81        size: super::Extent,
82    );
83
84    fn copy_texture_to_buffer(
85        &mut self,
86        src: Self::TexturePiece,
87        dst: Self::BufferPiece,
88        bytes_per_row: u32,
89        size: super::Extent,
90    );
91}
92
93pub trait AccelerationStructureEncoder {
94    type AccelerationStructure: Send + Sync + Clone + Debug;
95    type AccelerationStructureMesh: Send + Sync + Clone + Debug;
96    type BufferPiece: Send + Sync + Clone + Copy + Debug;
97
98    fn build_bottom_level(
99        &mut self,
100        acceleration_structure: Self::AccelerationStructure,
101        meshes: &[Self::AccelerationStructureMesh],
102        scratch_data: Self::BufferPiece,
103    );
104
105    fn build_top_level(
106        &mut self,
107        acceleration_structure: Self::AccelerationStructure,
108        bottom_level: &[Self::AccelerationStructure],
109        instance_count: u32,
110        instance_data: Self::BufferPiece,
111        scratch_data: Self::BufferPiece,
112    );
113}
114
115pub trait RenderEncoder {
116    fn set_scissor_rect(&mut self, rect: &super::ScissorRect);
117    fn set_viewport(&mut self, viewport: &super::Viewport);
118    fn set_stencil_reference(&mut self, reference: u32);
119}
120
121pub trait PipelineEncoder {
122    fn bind<D: super::ShaderData>(&mut self, group: u32, data: &D);
123}
124
125pub trait ComputePipelineEncoder: PipelineEncoder {
126    type BufferPiece: Send + Sync + Clone + Copy + Debug;
127
128    fn dispatch(&mut self, groups: [u32; 3]);
129    fn dispatch_indirect(&mut self, indirect_buf: Self::BufferPiece);
130}
131
132pub trait RenderPipelineEncoder: PipelineEncoder + RenderEncoder {
133    type BufferPiece: Send + Sync + Clone + Copy + Debug;
134
135    //Note: does this need to be available outside of the pipeline?
136    fn bind_vertex(&mut self, index: u32, vertex_buf: Self::BufferPiece);
137    fn draw(
138        &mut self,
139        first_vertex: u32,
140        vertex_count: u32,
141        first_instance: u32,
142        instance_count: u32,
143    );
144    fn draw_indexed(
145        &mut self,
146        index_buf: Self::BufferPiece,
147        index_type: super::IndexType,
148        index_count: u32,
149        base_vertex: i32,
150        start_instance: u32,
151        instance_count: u32,
152    );
153    fn draw_indirect(&mut self, indirect_buf: Self::BufferPiece);
154    fn draw_indexed_indirect(
155        &mut self,
156        index_buf: Self::BufferPiece,
157        index_type: crate::IndexType,
158        indirect_buf: Self::BufferPiece,
159    );
160}