aboutsummaryrefslogtreecommitdiff
path: root/src/nodes.rs
blob: 9791ad162a6520f78ea33f4652631efaaf6f1b3e (plain)
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
use bevy::{
    prelude::*,
    render::{
        render_asset::RenderAssets,
        render_graph::{Node, NodeRunError, RenderGraphContext, RenderLabel},
        render_resource::{ImageCopyBuffer, ImageDataLayout},
        renderer::RenderContext,
    },
};

use crate::render_assets::FramebufferExtractSource;

#[derive(RenderLabel, Clone, PartialEq, Eq, Debug, Hash)]
pub struct FramebufferExtractLabel;

#[derive(Default)]
pub struct FramebufferExtractNode;

impl Node for FramebufferExtractNode {
    fn run(
        &self,
        _graph: &mut RenderGraphContext,
        render_context: &mut RenderContext,
        world: &World,
    ) -> Result<(), NodeRunError> {
        for (_, source) in world
            .resource::<RenderAssets<FramebufferExtractSource>>()
            .iter()
        {
            let Some(gpu_image) = world
                .resource::<RenderAssets<Image>>()
                .get(&source.source_handle)
            else {
                return Ok(());
            };

            render_context.command_encoder().copy_texture_to_buffer(
                gpu_image.texture.as_image_copy(),
                ImageCopyBuffer {
                    buffer: &source.buffer,
                    layout: ImageDataLayout {
                        offset: 0,
                        bytes_per_row: Some(source.padded_bytes_per_row),
                        rows_per_image: None,
                    },
                },
                source.source_size,
            );
        }
        Ok(())
    }
}