Skip to main content

bevy_core_pipeline/fullscreen_vertex_shader/
mod.rs

1use bevy_asset::{AssetServer, Handle};
2use bevy_ecs::{resource::Resource, world::FromWorld};
3use bevy_render::render_resource::VertexState;
4use bevy_shader::Shader;
5
6/// A shader that renders to the whole screen. Useful for post-processing.
7#[derive(Resource, Clone)]
8pub struct FullscreenShader(Handle<Shader>);
9
10impl FromWorld for FullscreenShader {
11    fn from_world(world: &mut bevy_ecs::world::World) -> Self {
12        Self(
13            world
14                .resource::<AssetServer>()
15                .load("embedded://bevy_core_pipeline/fullscreen_vertex_shader.wesl"),
16        )
17    }
18}
19
20impl FullscreenShader {
21    /// Gets the raw shader handle.
22    pub fn shader(&self) -> Handle<Shader> {
23        self.0.clone()
24    }
25
26    /// Creates a [`VertexState`] that uses the [`FullscreenShader`] to output a
27    /// ```wgsl
28    /// struct FullscreenVertexOutput {
29    ///     @builtin(position)
30    ///     position: vec4<f32>;
31    ///     @location(0)
32    ///     uv: vec2<f32>;
33    /// };
34    /// ```
35    /// from the vertex shader.
36    /// The draw call should render one triangle: `render_pass.draw(0..3, 0..1);`
37    pub fn to_vertex_state(&self) -> VertexState {
38        VertexState {
39            shader: self.0.clone(),
40            shader_defs: Vec::new(),
41            entry_point: Some("fullscreen_vertex_shader".into()),
42            buffers: Vec::new(),
43            constants: Vec::new(),
44        }
45    }
46}