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_and_present(&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
23    fn get_presentation_render_target(&self) -> Option<Arc<dyn crate::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        layouts: &[Arc<dyn Layout>],
48    ) -> GraphicsResult<Arc<GpuSamplerSet>>;
49    /// Creates texture from buffers
50    fn create_texture(
51        &self,
52        buffer: Arc<dyn Buffer>,
53        extent: [u32; 2],
54        anisotropy_texels: f32,
55    ) -> GraphicsResult<Arc<dyn Texture>>;
56
57    /// Returns the duration of previous frame
58    fn get_delta_time(&self) -> std::time::Duration;
59}
60
61/// Contains resources used to rendering in them
62pub trait RenderTarget: Sync + Send {
63    #[allow(missing_docs)]
64    fn as_vulkan(self: Arc<Self>) -> Option<Arc<vulkan::VulkanRenderTarget>> {
65        None
66    }
67
68    /// Creates child render target from textures
69    fn create_render_target(
70        &self,
71        extent: [u32; 2],
72        anisotropy_texels: f32,
73        msaa_samples: u8,
74    ) -> GraphicsResult<(Arc<dyn RenderTarget>, Arc<dyn Texture>)>;
75}
76
77/// Contains resources used to mapping GPU memory in shaders
78pub trait Layout: Sync + Send {
79    #[allow(missing_docs)]
80    fn as_vulkan(self: Arc<Self>) -> Option<Arc<vulkan::VulkanLayout>> {
81        None
82    }
83
84    /// Creates graphics pipeline
85    fn create_graphics_pipeline(
86        self: Arc<Self>,
87        render_target: Arc<dyn RenderTarget>,
88        shaders: &[Shader],
89        attributes: &[Attribute],
90    ) -> GraphicsResult<Arc<dyn Pipeline>>;
91
92    /// Creates compute pipeline
93    fn create_compute_pipeline(
94        self: Arc<Self>,
95        shader: &Shader,
96    ) -> GraphicsResult<Arc<dyn Pipeline>>;
97
98    /// Registers samplers in layout for reusing them.
99    /// Samplers are removing automatically on zero hard references in ```Arc```
100    fn register_samplers(&self, samplers: &[Arc<GpuSamplerSet>]) -> GraphicsResult<()>;
101    /// Adds buffer
102    fn add_buffer(&self, binding: u32, buffer: Arc<dyn Buffer>) -> GraphicsResult<()>;
103}
104
105/// Used to store data about texture in GPU
106pub trait Texture: Sync + Send {
107    #[allow(missing_docs)]
108    fn as_vulkan(self: Arc<Self>) -> Option<Arc<vulkan::VulkanTexture>> {
109        None
110    }
111}
112
113/// Contains compiled shader stages and layout bindings info
114pub trait Pipeline: Sync + Send {
115    #[allow(missing_docs)]
116    fn as_vulkan(self: Arc<Self>) -> Option<Arc<vulkan::VulkanPipeline>> {
117        None
118    }
119}
120
121/// Used to store GPU data
122pub trait Buffer: Sync + Send {
123    #[allow(missing_docs)]
124    fn as_vulkan(self: Arc<Self>) -> Option<Arc<vulkan::BufferManager>> {
125        None
126    }
127
128    /// Returns a slice of this buffer in given range
129    fn get_memory(&self, range: Range<usize>) -> &mut [u8];
130    /// Returns a slice of this buffer
131    fn get_memory_full(&self) -> &mut [u8];
132}