crystal_api/
layout.rs

1use std::sync::Arc;
2
3use crate::{
4    GpuSamplerSet, Shader,
5    buffer::Buffer,
6    debug::log,
7    errors::GraphicsResult,
8    mesh::AttributeDescriptor,
9    pipeline::{ComputeDescriptor, Pipeline},
10    proxies::LayoutProxy,
11    render_target::RenderTarget,
12};
13
14/// Used to specify input and output shader data
15pub struct Layout {
16    pub(crate) inner: Arc<dyn LayoutProxy>,
17}
18
19impl Layout {
20    pub(crate) fn new(proxy: Arc<dyn LayoutProxy>) -> Self {
21        Self { inner: proxy }
22    }
23
24    /// Creates graphics pipeline
25    pub fn create_graphics_pipeline<V: AttributeDescriptor>(
26        &self,
27        render_target: &RenderTarget,
28        shaders: &[Shader],
29    ) -> GraphicsResult<Pipeline<V>> {
30        log!("creating graphics pipeline");
31
32        let attributes = V::get_attributes();
33
34        Ok(Pipeline::new(self.inner.clone().create_graphics_pipeline(
35            render_target.inner.clone(),
36            shaders,
37            attributes,
38        )?))
39    }
40
41    /// Creates compute pipeline
42    pub fn create_compute_pipeline(
43        &self,
44        shader: &Shader,
45    ) -> GraphicsResult<Pipeline<ComputeDescriptor>> {
46        log!("creating compute pipeline");
47
48        Ok(Pipeline::new(
49            self.inner.clone().create_compute_pipeline(shader)?,
50        ))
51    }
52
53    /// Registers samplers in layout for reusing them
54    pub fn register_samplers(&self, samplers: &[Arc<GpuSamplerSet>]) -> GraphicsResult<()> {
55        self.inner.register_samplers(samplers)
56    }
57
58    /// Adds buffer
59    pub fn add_buffer<T>(&self, binding: u32, buffer: &Buffer<T>) -> GraphicsResult<()> {
60        self.inner.add_buffer(binding, buffer.inner.clone())
61    }
62}