crystal_api/
traits.rs

1use std::{ops::Range, sync::Arc};
2
3use crate::{
4    GpuSamplerSet,
5    errors::GraphicsResult,
6    mesh::{Attribute, Mesh},
7    object::{MeshBuffer, Object},
8    shader::Shader,
9    vulkan,
10};
11
12/// ```GraphicsApi``` is the main trait used for creation of graphics resources and using this wrapper
13pub trait GraphicsApi: Sync + Send {
14    /// Executes all operations permitted with objects passed in this method
15    fn dispatch_any(&self, objects: &[Arc<Object>]) -> GraphicsResult<()>;
16    /// Executes all compute operations permitted with objects passed in this method
17    fn dispatch_compute(&self, objects: &[Arc<Object>]) -> GraphicsResult<()>;
18
19    /// Resizes resources
20    fn resize_resources(&self, width: u32, height: u32) -> GraphicsResult<()>;
21
22    /// Returns ```RenderTarget``` created on presentation init. Panics if API was initialized without presentation
23    fn get_presentation_render_target(&self) -> Arc<dyn RenderTarget>;
24    /// Creates shader layout
25    fn create_layout(
26        &self,
27        double_buffering: bool,
28        texture_num: usize,
29        sampler_num: usize,
30        uniform_num: usize,
31        storage_num: usize,
32    ) -> GraphicsResult<Arc<dyn Layout>>;
33    /// Creates GPU buffer
34    fn create_buffer(
35        &self,
36        size: u64,
37        uniform: bool,
38        transfer: bool,
39        enable_sync: bool,
40    ) -> GraphicsResult<Arc<dyn Buffer>>;
41    /// Creates a set of GPU buffers used for meshes
42    fn create_buffer_mesh(&self, mesh: Arc<Mesh>) -> GraphicsResult<Arc<MeshBuffer>>;
43    /// Creates sampler set
44    fn create_sampler_set(
45        &self,
46        textures: &[(u32, Arc<dyn Texture>)],
47    ) -> GraphicsResult<Arc<GpuSamplerSet>>;
48    /// Creates texture
49    fn create_texture(
50        &self,
51        buffer: Arc<dyn Buffer>,
52        data: [u32; 3],
53        anisotropy_texels: f32,
54    ) -> GraphicsResult<Arc<dyn Texture>>;
55
56    /// Returns the duration of previous frame
57    fn get_delta_time(&self) -> std::time::Duration;
58}
59
60/// Contains resources used to rendering in them
61pub trait RenderTarget: Sync + Send {
62    #[allow(missing_docs)]
63    fn as_vulkan(self: Arc<Self>) -> Option<Arc<vulkan::VulkanRenderTarget>> {
64        None
65    }
66}
67
68/// Contains resources used to mapping GPU memory in shaders
69pub trait Layout: Sync + Send {
70    #[allow(missing_docs)]
71    fn as_vulkan(self: Arc<Self>) -> Option<Arc<vulkan::VulkanLayout>> {
72        None
73    }
74
75    /// Creates graphics pipeline
76    fn create_graphics_pipeline(
77        self: Arc<Self>,
78        render_target: Arc<dyn RenderTarget>,
79        shaders: &[Shader],
80        attributes: &[Attribute],
81    ) -> GraphicsResult<Arc<dyn Pipeline>>;
82
83    /// Creates compute pipeline
84    fn create_compute_pipeline(
85        self: Arc<Self>,
86        shader: &Shader,
87    ) -> GraphicsResult<Arc<dyn Pipeline>>;
88
89    /// Registers samplers in layout for reusing them.
90    /// Samplers are removing automatically on zero hard references in ```Arc```
91    fn register_samplers(&self, samplers: &[Arc<GpuSamplerSet>]) -> GraphicsResult<()>;
92    /// Adds buffer
93    fn add_buffer(&self, binding: u32, buffer: Arc<dyn Buffer>) -> GraphicsResult<()>;
94}
95
96/// Used to store data about texture in GPU
97pub trait Texture: Sync + Send {
98    #[allow(missing_docs)]
99    fn as_vulkan(self: Arc<Self>) -> Option<Arc<vulkan::VulkanTexture>> {
100        None
101    }
102}
103
104/// Contains compiled shader stages and layout bindings info
105pub trait Pipeline: Sync + Send {
106    #[allow(missing_docs)]
107    fn as_vulkan(self: Arc<Self>) -> Option<Arc<vulkan::VulkanPipeline>> {
108        None
109    }
110}
111
112/// Used to store GPU data
113pub trait Buffer: Sync + Send {
114    #[allow(missing_docs)]
115    fn as_vulkan(self: Arc<Self>) -> Option<Arc<vulkan::BufferManager>> {
116        None
117    }
118
119    /// Returns a slice of this buffer in given range
120    fn get_memory(&self, range: Range<usize>) -> &mut [u8];
121    /// Returns a slice of this buffer
122    fn get_memory_full(&self) -> &mut [u8];
123}