Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
simbleau committed Aug 4, 2024
1 parent f21cdc2 commit 7c28ad6
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 128 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe
- There is now a `default_font` feature that uses the same `FiraMono-subset.ttf` font used in the bevy/default_font feature.
- There is now a `render_layers` example.
- There is now a `cube_3d` example.
- Added `VelloRenderer::from_device` to create a renderer manually.

### Changed

Expand All @@ -32,6 +33,7 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe
### Removed

- Removed `ZFunction`s from the render pipeline. Now ordering is based solely on the `Transform`'s z component. If you dependeded on this behavior, you'll need to adjust the transform Z in a system prior to render.
- Removed `VelloCanvasMaterial` from prelude, as it's not typically meant to be used under normal circumstances and could be a common footgun.

### Fixed

Expand Down
20 changes: 16 additions & 4 deletions examples/cube3d/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ use bevy::{
Render, RenderApp, RenderSet,
},
};
use bevy_vello::{prelude::*, render::VelloRenderer, VelloPlugin};
use bevy_vello::{
prelude::*,
render::{VelloRenderPlugin, VelloRenderer},
VelloPlugin,
};

#[derive(Component)]
pub struct VelloTarget(Handle<Image>);
Expand All @@ -33,7 +37,7 @@ fn main() {
let mut app = App::new();

app.add_plugins(DefaultPlugins)
.add_plugins(VelloPlugin::default())
.add_plugins(VelloRenderPlugin::default())
.add_systems(Startup, setup)
.add_systems(Update, cube_rotator_system)
.add_plugins(ExtractComponentPlugin::<VelloTarget>::default());
Expand Down Expand Up @@ -122,8 +126,16 @@ fn render_texture(
queue: Res<RenderQueue>,
time: Res<Time>,
) {
let renderer =
vello_renderer.get_or_insert_with(|| VelloRenderer::from_device(device.wgpu_device()));
let renderer = vello_renderer.get_or_insert_with(|| {
VelloRenderer::from_device(
device.wgpu_device(),
&VelloRenderSettings {
use_cpu: false,
antialiasing: vello::AaConfig::Msaa8,
..default()
},
)
});
let target = target.single();

let mut scene = VelloScene::default();
Expand Down
48 changes: 12 additions & 36 deletions examples/render_layers/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ use bevy_vello::{prelude::*, VelloPlugin};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(VelloPlugin::default())
.add_systems(
Startup,
(
setup_canvas,
setup_gizmos,
setup_animation,
setup_background,
),
)
.add_plugins(VelloPlugin {
canvas_render_layers: RenderLayers::layer(1).with(2),
..default()
})
.add_systems(Startup, (setup_gizmos, setup_scene))
.add_systems(Update, (animation, background, run_gizmos))
.run();
}
Expand All @@ -28,11 +23,6 @@ struct AnimationScene;
#[derive(Component)]
struct BackgroundScene;

fn setup_canvas(mut settings: ResMut<VelloRenderSettings>) {
// There's only 1 Vello canvas, so as long as you use a layer that has a camera, you're good!
settings.canvas_render_layers = RenderLayers::layer(3); // the gizmo camera layer
}

fn setup_gizmos(mut commands: Commands, mut config_store: ResMut<GizmoConfigStore>) {
// This camera can only see Gizmos.
commands.spawn((
Expand All @@ -50,42 +40,28 @@ fn setup_gizmos(mut commands: Commands, mut config_store: ResMut<GizmoConfigStor
config.render_layers = RenderLayers::layer(3);
}

fn setup_animation(mut commands: Commands) {
fn setup_scene(mut commands: Commands) {
commands.spawn((
Camera2dBundle {
camera: Camera {
// This camera will render AFTER the blue background camera!
order: 0,
// This camera will render first.
order: -1,
..default()
},
..default()
},
RenderLayers::layer(2),
RenderLayers::layer(1).with(2),
));

commands.spawn((
VelloSceneBundle::default(),
AnimationScene,
RenderLayers::layer(2),
));
}

fn setup_background(mut commands: Commands) {
commands.spawn((
Camera2dBundle {
camera: Camera {
// Render first
order: -1,
..default()
},
..default()
},
BackgroundScene,
RenderLayers::layer(1),
));
commands.spawn((
VelloSceneBundle::default(),
BackgroundScene,
RenderLayers::layer(1),
AnimationScene,
RenderLayers::layer(2),
));
}

Expand Down
18 changes: 2 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub mod prelude {
pub use crate::{
debug::DebugVisualizations,
integrations::{VectorFile, VelloAsset, VelloAssetAnchor},
render::{VelloCanvasMaterial, VelloRenderSettings},
render::{VelloCanvasSettings, VelloRenderSettings},
text::{VelloFont, VelloTextAnchor, VelloTextSection, VelloTextStyle},
CoordinateSpace, VelloAssetBundle, VelloScene, VelloSceneBundle, VelloTextBundle,
};
Expand Down Expand Up @@ -113,23 +113,9 @@ pub struct VelloTextBundle {
}

/// A simple newtype component wrapper for [`vello::Scene`] for rendering.
#[derive(Component, Default, Clone)]
#[derive(Component, Default, Clone, Deref, DerefMut)]
pub struct VelloScene(vello::Scene);

impl std::ops::Deref for VelloScene {
type Target = vello::Scene;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl std::ops::DerefMut for VelloScene {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl VelloScene {
pub fn new() -> Self {
Self::default()
Expand Down
49 changes: 38 additions & 11 deletions src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,51 @@
use crate::{
debug::DebugVisualizationsPlugin, render::VelloRenderPlugin, text::VelloFontLoader, VelloAsset,
VelloFont, VelloRenderSettings,
debug::DebugVisualizationsPlugin,
render::{VelloCanvasSettings, VelloRenderPlugin},
text::VelloFontLoader,
VelloAsset, VelloFont, VelloRenderSettings,
};
use bevy::{asset::load_internal_binary_asset, prelude::*, render::view::RenderLayers};
use vello::AaConfig;

#[derive(Resource, Default, Debug)]
#[derive(Clone)]
pub struct VelloPlugin {
/// The render layers that will be used for the Vello canvas mesh.
pub canvas_render_layers: RenderLayers,

/// Use CPU instead of GPU
pub use_cpu: bool,

/// Which antialiasing strategy to use
pub antialiasing: AaConfig,
}

impl Default for VelloPlugin {
fn default() -> Self {
let default_canvas_settings = VelloCanvasSettings::default();
let default_render_settings = VelloRenderSettings::default();
Self {
canvas_render_layers: default_canvas_settings.render_layers,
use_cpu: default_render_settings.use_cpu,
antialiasing: default_render_settings.antialiasing,
}
}
}

impl Plugin for VelloPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(VelloRenderPlugin)
.insert_resource(VelloRenderSettings {
canvas_render_layers: self.canvas_render_layers.clone(),
})
.add_plugins(DebugVisualizationsPlugin)
.init_asset::<VelloAsset>()
.init_asset::<VelloFont>()
.init_asset_loader::<VelloFontLoader>();
app.add_plugins(VelloRenderPlugin {
canvas_settings: VelloCanvasSettings {
render_layers: self.canvas_render_layers.clone(),
},
render_settings: VelloRenderSettings {
use_cpu: self.use_cpu,
antialiasing: self.antialiasing,
},
})
.add_plugins(DebugVisualizationsPlugin)
.init_asset::<VelloAsset>()
.init_asset::<VelloFont>()
.init_asset_loader::<VelloFontLoader>();
#[cfg(feature = "svg")]
app.add_plugins(crate::integrations::svg::SvgIntegrationPlugin);
#[cfg(feature = "lottie")]
Expand Down
39 changes: 35 additions & 4 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ use bevy::{
AsBindGroup, RenderPipelineDescriptor, ShaderRef, SpecializedMeshPipelineError,
VertexBufferLayout, VertexFormat, VertexStepMode,
},
view::RenderLayers,
},
sprite::{Material2d, Material2dKey},
};
use vello::{AaConfig, AaSupport};

mod extract;
mod plugin;
mod prepare;
mod systems;

pub use plugin::VelloRenderPlugin;
pub use plugin::VelloRenderSettings;

/// A handle to the screen space render target shader.
pub const SSRT_SHADER_HANDLE: Handle<Shader> = Handle::weak_from_u128(2314894693238056781);
Expand Down Expand Up @@ -64,13 +65,17 @@ impl Material2d for VelloCanvasMaterial {
pub struct VelloRenderer(vello::Renderer);

impl VelloRenderer {
pub fn from_device(device: &vello::wgpu::Device) -> Self {
pub fn from_device(device: &vello::wgpu::Device, settings: &VelloRenderSettings) -> Self {
let renderer = vello::Renderer::new(
device,
vello::RendererOptions {
surface_format: None,
use_cpu: false,
antialiasing_support: vello::AaSupport::area_only(),
use_cpu: settings.use_cpu,
antialiasing_support: AaSupport {
area: settings.antialiasing == AaConfig::Area,
msaa8: settings.antialiasing == AaConfig::Msaa8,
msaa16: settings.antialiasing == AaConfig::Msaa16,
},
num_init_threads: None,
},
)
Expand All @@ -83,3 +88,29 @@ impl VelloRenderer {
#[derive(Resource, Deref, DerefMut, Default)]
#[cfg(feature = "lottie")]
pub struct VelatoRenderer(velato::Renderer);

/// Render settings for Vello.
#[derive(Resource, Clone)]
pub struct VelloRenderSettings {
/// Use CPU instead of GPU
pub use_cpu: bool,

/// Which antialiasing strategy to use
pub antialiasing: AaConfig,
}

impl Default for VelloRenderSettings {
fn default() -> Self {
Self {
use_cpu: false,
antialiasing: AaConfig::Area,
}
}
}

/// Canvas settings for Vello.
#[derive(Resource, Clone, Debug, Default, PartialEq)]
pub struct VelloCanvasSettings {
/// The render layers that will be used for the Vello canvas mesh.
pub render_layers: RenderLayers,
}
Loading

0 comments on commit 7c28ad6

Please sign in to comment.