1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Environment submodule for shared environmental descriptor set data.
//! Fetches and sets projection set information for a flat pass.
use crate::{
    pod::ViewArgs,
    rendy::{command::RenderPassEncoder, factory::Factory},
    submodules::{gather::CameraGatherer, uniform::DynamicUniform},
    types::Backend,
};
use amethyst_core::ecs::World;

#[cfg(feature = "profiler")]
use thread_profiler::profile_scope;

/// Submodule for loading and binding descriptor sets for a flat, unlit environment.
/// This also abstracts away the need for handling multiple images in flight, as it provides
/// per-image submissions.
#[derive(Debug)]
pub struct FlatEnvironmentSub<B: Backend> {
    uniform: DynamicUniform<B, ViewArgs>,
}

impl<B: Backend> FlatEnvironmentSub<B> {
    /// Create and allocate a new `EnvironmentSub` with the provided rendy `Factory`
    pub fn new(factory: &Factory<B>) -> Result<Self, failure::Error> {
        Ok(Self {
            uniform: DynamicUniform::new(factory, rendy::hal::pso::ShaderStageFlags::VERTEX)?,
        })
    }

    /// Returns the raw `DescriptorSetLayout` for this environment
    pub fn raw_layout(&self) -> &B::DescriptorSetLayout {
        self.uniform.raw_layout()
    }

    /// Performs any re-allocation and GPU memory writing required for this environment set.
    pub fn process(&mut self, factory: &Factory<B>, index: usize, world: &World) {
        #[cfg(feature = "profiler")]
        profile_scope!("process");
        let projview = CameraGatherer::gather(world).projview;
        self.uniform.write(factory, index, projview);
    }

    /// Binds this environment set for all images.
    #[inline]
    pub fn bind(
        &self,
        index: usize,
        pipeline_layout: &B::PipelineLayout,
        set_id: u32,
        encoder: &mut RenderPassEncoder<'_, B>,
    ) {
        self.uniform.bind(index, pipeline_layout, set_id, encoder);
    }
}