From d7e1903369330c5438892c5ddfdbc7c1ef179633 Mon Sep 17 00:00:00 2001 From: Silas Bartha Date: Wed, 24 Jul 2024 11:46:40 -0400 Subject: Renamed + Updated to Bevy 0.14 + Added 0BSD Option --- src/components.rs | 20 ++++++++++---------- src/lib.rs | 33 ++++++++++++++++----------------- src/nodes.rs | 16 ++++++++-------- src/render_assets.rs | 35 ++++++++++++++++------------------- src/systems.rs | 14 +++++++------- 5 files changed, 57 insertions(+), 61 deletions(-) (limited to 'src') diff --git a/src/components.rs b/src/components.rs index f9fe912..8e25530 100644 --- a/src/components.rs +++ b/src/components.rs @@ -2,18 +2,18 @@ use std::sync::{Arc, Mutex}; use bevy::{ecs::query::QueryItem, prelude::*, render::extract_component::ExtractComponent}; -use crate::render_assets::FramebufferExtractSource; +use crate::render_assets::HeadlessRenderSource; -/// Framebuffer extraction destination. Contains the image which the framebuffer is extracted to. +/// Headless render destination. Contains the image which the rendered frame is copied to. #[derive(Component, Default, Clone)] -pub struct FramebufferExtractDestination(pub Arc>); +pub struct HeadlessRenderDestination(pub Arc>); -impl ExtractComponent for FramebufferExtractDestination { - type QueryData = (&'static Self, &'static Handle); +impl ExtractComponent for HeadlessRenderDestination { + type QueryData = (&'static Self, &'static Handle); type QueryFilter = (); - type Out = (Self, Handle); + type Out = (Self, Handle); fn extract_component( (destination, source_handle): QueryItem<'_, Self::QueryData>, @@ -22,11 +22,11 @@ impl ExtractComponent for FramebufferExtractDestination { } } -/// Bundle containing both a source and destination for framebuffer extraction. +/// Bundle containing both a source and destination for headless rendering. #[derive(Bundle)] -pub struct ExtractFramebufferBundle { +pub struct HeadlessRenderBundle { /// Source - pub source: Handle, + pub source: Handle, /// Destination - pub dest: FramebufferExtractDestination, + pub dest: HeadlessRenderDestination, } diff --git a/src/lib.rs b/src/lib.rs index 29a5896..9fadebe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,6 @@ #![warn(missing_docs)] -//! Plugin for the Bevy game engine which provides the ability to extract the frambuffer image after rendering -//! to use for whatever you want. +//! Plugin for the Bevy game engine which provides the ability to render to an image headlessly. use bevy::{ prelude::*, @@ -10,9 +9,9 @@ use bevy::{ render_asset::RenderAssetPlugin, render_graph::RenderGraph, Render, RenderApp, RenderSet, }, }; -use components::FramebufferExtractDestination; -use nodes::{FramebufferExtractLabel, FramebufferExtractNode}; -use render_assets::FramebufferExtractSource; +use components::HeadlessRenderDestination; +use nodes::{HeadlessRenderCopyLabel, HeadlessRenderCopyNode}; +use render_assets::{HeadlessRenderSource, GpuHeadlessRenderSource}; /// Components used by this plugin. pub mod components; @@ -22,28 +21,28 @@ pub mod render_assets; mod nodes; mod systems; -/// Plugin which handles framebuffer extraction. -pub struct FramebufferExtractPlugin; +/// Plugin which handles headless rendering. +pub struct HeadlessRenderPlugin; -impl Plugin for FramebufferExtractPlugin { +impl Plugin for HeadlessRenderPlugin { fn build(&self, app: &mut App) { - app.register_type::() - .init_asset::() - .register_asset_reflect::() + app.register_type::() + .init_asset::() + .register_asset_reflect::() .add_plugins(( - RenderAssetPlugin::::default(), - ExtractComponentPlugin::::default(), + RenderAssetPlugin::::default(), + ExtractComponentPlugin::::default(), )); let render_app = app.sub_app_mut(RenderApp); render_app.add_systems( Render, - systems::extract_framebuffers + systems::copy_buffers .after(RenderSet::Render) .before(RenderSet::Cleanup), ); - let mut graph = render_app.world.resource_mut::(); - graph.add_node(FramebufferExtractLabel, FramebufferExtractNode); - graph.add_node_edge(CameraDriverLabel, FramebufferExtractLabel); + let mut graph = render_app.world_mut().resource_mut::(); + graph.add_node(HeadlessRenderCopyLabel, HeadlessRenderCopyNode); + graph.add_node_edge(CameraDriverLabel, HeadlessRenderCopyLabel); } } diff --git a/src/nodes.rs b/src/nodes.rs index 9791ad1..4e2ddd3 100644 --- a/src/nodes.rs +++ b/src/nodes.rs @@ -4,19 +4,19 @@ use bevy::{ render_asset::RenderAssets, render_graph::{Node, NodeRunError, RenderGraphContext, RenderLabel}, render_resource::{ImageCopyBuffer, ImageDataLayout}, - renderer::RenderContext, + renderer::RenderContext, texture::GpuImage, }, }; -use crate::render_assets::FramebufferExtractSource; +use crate::render_assets::GpuHeadlessRenderSource; #[derive(RenderLabel, Clone, PartialEq, Eq, Debug, Hash)] -pub struct FramebufferExtractLabel; +pub struct HeadlessRenderCopyLabel; #[derive(Default)] -pub struct FramebufferExtractNode; +pub struct HeadlessRenderCopyNode; -impl Node for FramebufferExtractNode { +impl Node for HeadlessRenderCopyNode { fn run( &self, _graph: &mut RenderGraphContext, @@ -24,12 +24,12 @@ impl Node for FramebufferExtractNode { world: &World, ) -> Result<(), NodeRunError> { for (_, source) in world - .resource::>() + .resource::>() .iter() { let Some(gpu_image) = world - .resource::>() - .get(&source.source_handle) + .resource::>() + .get(source.source_handle.id()) else { return Ok(()); }; diff --git a/src/render_assets.rs b/src/render_assets.rs index e5277d7..48efdd0 100644 --- a/src/render_assets.rs +++ b/src/render_assets.rs @@ -2,14 +2,14 @@ use bevy::{ ecs::system::{lifetimeless::SRes, SystemParamItem}, prelude::*, render::{ - render_asset::{PrepareAssetError, RenderAsset, RenderAssetUsages, RenderAssets}, + render_asset::{PrepareAssetError, RenderAsset, RenderAssets}, render_resource::{Buffer, BufferDescriptor, BufferUsages, Extent3d, TextureFormat}, - renderer::RenderDevice, + renderer::RenderDevice, texture::GpuImage, }, }; -/// Render-world version of FramebufferExtractSource -pub struct GpuFramebufferExtractSource { +/// Render-world version of HeadlessRenderSource +pub struct GpuHeadlessRenderSource { pub(crate) buffer: Buffer, pub(crate) source_handle: Handle, pub(crate) source_size: Extent3d, @@ -18,26 +18,22 @@ pub struct GpuFramebufferExtractSource { pub(crate) format: TextureFormat, } -/// Framebuffer extraction source. Contains a handle to the render texture which will be extracted +/// Headless render source. Contains a handle to the render texture which will be copied /// from. #[derive(Asset, Reflect, Clone, Default)] -pub struct FramebufferExtractSource(pub Handle); +pub struct HeadlessRenderSource(pub Handle); -impl RenderAsset for FramebufferExtractSource { - type PreparedAsset = GpuFramebufferExtractSource; - type Param = (SRes, SRes>); - - fn asset_usage(&self) -> RenderAssetUsages { - RenderAssetUsages::default() - } +impl RenderAsset for GpuHeadlessRenderSource { + type SourceAsset = HeadlessRenderSource; + type Param = (SRes, SRes>); fn prepare_asset( - self, + source_asset: Self::SourceAsset, (device, images): &mut SystemParamItem, - ) -> Result> { - let Some(gpu_image) = images.get(&self.0) else { + ) -> Result> { + let Some(gpu_image) = images.get(source_asset.0.id()) else { warn!("Failed to get GPU image"); - return Err(PrepareAssetError::RetryNextUpdate(self)); + return Err(PrepareAssetError::RetryNextUpdate(source_asset)); }; let size = gpu_image.texture.size(); @@ -47,18 +43,19 @@ impl RenderAsset for FramebufferExtractSource { let padded_bytes_per_row = RenderDevice::align_copy_bytes_per_row(bytes_per_row as usize) as u32; - Ok(GpuFramebufferExtractSource { + Ok(GpuHeadlessRenderSource { buffer: device.create_buffer(&BufferDescriptor { label: Some("framebuffer_extract_buffer"), size: (size.height * padded_bytes_per_row) as u64, usage: BufferUsages::COPY_DST | BufferUsages::MAP_READ, mapped_at_creation: false, }), - source_handle: self.0.clone(), + source_handle: source_asset.0.clone(), source_size: size, bytes_per_row, padded_bytes_per_row, format, }) } + } diff --git a/src/systems.rs b/src/systems.rs index 74f8dc8..bb0efc7 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -9,17 +9,17 @@ use bevy::{ use pollster::FutureExt; -use crate::{components::FramebufferExtractDestination, render_assets::FramebufferExtractSource}; +use crate::{components::HeadlessRenderDestination, render_assets::{HeadlessRenderSource, GpuHeadlessRenderSource}}; -pub fn extract_framebuffers( - mut extract_bundles: Query<( - &Handle, - &mut FramebufferExtractDestination, +pub fn copy_buffers( + mut headless_render_query: Query<( + &Handle, + &mut HeadlessRenderDestination, )>, - sources: Res>, + sources: Res>, device: Res, ) { - for (source_handle, destination_handle) in extract_bundles.iter_mut() { + for (source_handle, destination_handle) in headless_render_query.iter_mut() { let Some(gpu_source) = sources.get(source_handle) else { continue; }; -- cgit v1.2.3