Skip to content

Commit

Permalink
Merge pull request #83 from TheRawMeatball/split-renderer
Browse files Browse the repository at this point in the history
Split the renderer
  • Loading branch information
StarArawn committed Feb 19, 2022
2 parents 137c421 + 1bcca74 commit 4b45882
Show file tree
Hide file tree
Showing 28 changed files with 415 additions and 276 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions bevy_kayak_renderer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "bevy_kayak_renderer"
version = "0.0.1"
edition = "2021"

[dependencies]
bytemuck = "1.7.2"
bevy = { version = "0.6.0" }
kayak_font = { path = "../kayak_font" }
serde = "1.0"
serde_json = "1.0"
serde_path_to_error = "0.1"
File renamed without changes.
File renamed without changes.
File renamed without changes.
59 changes: 59 additions & 0 deletions bevy_kayak_renderer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use bevy::{
prelude::*,
window::{WindowCreated, WindowResized},
};

pub mod camera;
pub mod render;

pub use camera::*;

#[derive(Default)]
pub struct BevyKayakRendererPlugin;

impl Plugin for BevyKayakRendererPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.add_system(update_window_size)
.init_resource::<WindowSize>()
.add_plugin(render::BevyKayakUIRenderPlugin)
.add_plugin(camera::KayakUICameraPlugin);
}
}

/// Tracks the bevy window size.
#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct WindowSize(pub f32, pub f32);

fn update_window_size(
mut window_resized_events: EventReader<WindowResized>,
mut window_created_events: EventReader<WindowCreated>,
) {
let mut changed_window_ids = Vec::new();
// handle resize events. latest events are handled first because we only want to resize each
// window once
for event in window_resized_events.iter().rev() {
if changed_window_ids.contains(&event.id) {
continue;
}

changed_window_ids.push(event.id);
}

// handle resize events. latest events are handled first because we only want to resize each
// window once
for event in window_created_events.iter().rev() {
if changed_window_ids.contains(&event.id) {
continue;
}

changed_window_ids.push(event.id);
}
}

#[derive(Debug, Clone, Copy, Default)]
pub struct Corner<T> {
pub top_left: T,
pub top_right: T,
pub bottom_left: T,
pub bottom_right: T,
}
93 changes: 93 additions & 0 deletions bevy_kayak_renderer/src/render/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use bevy::{
core_pipeline::node::MAIN_PASS_DRIVER,
prelude::{Commands, Plugin, Res},
render::{
camera::ActiveCameras,
render_graph::{EmptyNode, RenderGraph, SlotInfo, SlotType},
render_phase::{DrawFunctions, RenderPhase},
RenderApp, RenderStage,
},
};

use crate::{
render::{
ui_pass::MainPassUINode, ui_pass_driver::UIPassDriverNode, unified::UnifiedRenderPlugin,
},
UICameraBundle,
};

use self::ui_pass::TransparentUI;

mod ui_pass;
mod ui_pass_driver;
pub mod unified;

pub mod node {
pub const UI_PASS_DEPENDENCIES: &str = "kayak_ui_pass_dependencies";
pub const UI_PASS_DRIVER: &str = "kayak_ui_pass_driver";
}

pub mod draw_ui_graph {
pub const NAME: &str = "kayak_draw_ui";
pub mod input {
pub const VIEW_ENTITY: &str = "kayak_view_entity";
}
pub mod node {
pub const MAIN_PASS: &str = "kayak_ui_pass";
}
}

pub struct BevyKayakUIRenderPlugin;

impl Plugin for BevyKayakUIRenderPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
let render_app = app.sub_app_mut(RenderApp);
render_app
.init_resource::<DrawFunctions<TransparentUI>>()
.add_system_to_stage(RenderStage::Extract, extract_core_pipeline_camera_phases);
// .add_system_to_stage(RenderStage::PhaseSort, sort_phase_system::<TransparentUI>);

let pass_node_ui = MainPassUINode::new(&mut render_app.world);
let mut graph = render_app.world.get_resource_mut::<RenderGraph>().unwrap();

let mut draw_ui_graph = RenderGraph::default();
draw_ui_graph.add_node(draw_ui_graph::node::MAIN_PASS, pass_node_ui);
let input_node_id = draw_ui_graph.set_input(vec![SlotInfo::new(
draw_ui_graph::input::VIEW_ENTITY,
SlotType::Entity,
)]);
draw_ui_graph
.add_slot_edge(
input_node_id,
draw_ui_graph::input::VIEW_ENTITY,
draw_ui_graph::node::MAIN_PASS,
MainPassUINode::IN_VIEW,
)
.unwrap();
graph.add_sub_graph(draw_ui_graph::NAME, draw_ui_graph);

graph.add_node(node::UI_PASS_DEPENDENCIES, EmptyNode);
graph.add_node(node::UI_PASS_DRIVER, UIPassDriverNode);
graph
.add_node_edge(node::UI_PASS_DEPENDENCIES, node::UI_PASS_DRIVER)
.unwrap();
graph
.add_node_edge(MAIN_PASS_DRIVER, node::UI_PASS_DRIVER)
.unwrap();

app.add_plugin(UnifiedRenderPlugin);
}
}

pub fn extract_core_pipeline_camera_phases(
mut commands: Commands,
active_cameras: Res<ActiveCameras>,
) {
if let Some(camera_2d) = active_cameras.get(UICameraBundle::UI_CAMERA) {
if let Some(entity) = camera_2d.entity {
commands
.get_or_spawn(entity)
.insert(RenderPhase::<TransparentUI>::default());
}
}
}
File renamed without changes.
File renamed without changes.
69 changes: 69 additions & 0 deletions bevy_kayak_renderer/src/render/unified/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use bevy::{
prelude::{Assets, Commands, HandleUntyped, Plugin, Res},
reflect::TypeUuid,
render::{render_phase::DrawFunctions, render_resource::Shader, RenderApp, RenderStage},
window::Windows,
};

use crate::{
render::{
ui_pass::TransparentUI,
unified::pipeline::{DrawUI, QuadMeta, UnifiedPipeline},
},
WindowSize,
};

use self::pipeline::ImageBindGroups;

pub mod pipeline;
mod text;

pub const UNIFIED_SHADER_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 7604018236855288450);

pub struct UnifiedRenderPlugin;

impl Plugin for UnifiedRenderPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
let mut shaders = app.world.get_resource_mut::<Assets<Shader>>().unwrap();
let unified_shader = Shader::from_wgsl(include_str!("shader.wgsl"));
shaders.set_untracked(UNIFIED_SHADER_HANDLE, unified_shader);

app.add_plugin(text::TextRendererPlugin);

let render_app = app.sub_app_mut(RenderApp);
render_app
.init_resource::<ImageBindGroups>()
.init_resource::<UnifiedPipeline>()
.init_resource::<QuadMeta>()
.add_system_to_stage(RenderStage::Extract, extract_baseline)
.add_system_to_stage(RenderStage::Prepare, pipeline::prepare_quads)
.add_system_to_stage(RenderStage::Queue, pipeline::queue_quads);

let draw_quad = DrawUI::new(&mut render_app.world);

render_app
.world
.get_resource::<DrawFunctions<TransparentUI>>()
.unwrap()
.write()
.add(draw_quad);
}
}

pub struct Dpi(f32);

pub fn extract_baseline(
mut commands: Commands,
windows: Res<Windows>,
window_size: Res<WindowSize>,
) {
let dpi = if let Some(window) = windows.get_primary() {
window.scale_factor() as f32
} else {
1.0
};

commands.insert_resource(*window_size);
commands.insert_resource(Dpi(dpi));
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ use kayak_font::{
};

use super::{Dpi, UNIFIED_SHADER_HANDLE};
use crate::{render::ui_pass::TransparentUI, WindowSize};
use kayak_core::styles::Corner;
use crate::render::ui_pass::TransparentUI;
use crate::{Corner, WindowSize};

pub struct UnifiedPipeline {
view_layout: BindGroupLayout,
Expand Down Expand Up @@ -300,7 +300,7 @@ impl FromWorld for UnifiedPipeline {

#[derive(Debug, Bundle)]
pub struct ExtractQuadBundle {
pub(crate) extracted_quad: ExtractedQuad,
pub extracted_quad: ExtractedQuad,
}

#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,53 +1,27 @@
use bevy::{
prelude::{Assets, Plugin, Res, ResMut},
prelude::{Plugin, Res, ResMut},
render::{
render_asset::RenderAssets,
renderer::{RenderDevice, RenderQueue},
texture::Image,
RenderApp, RenderStage,
},
};
use kayak_font::{
bevy::{FontTextureCache, KayakFontPlugin},
KayakFont,
};

mod extract;
mod font_mapping;

use crate::BevyContext;
use kayak_font::bevy::{FontTextureCache, KayakFontPlugin};

use super::pipeline::UnifiedPipeline;
pub use extract::extract_texts;
pub use font_mapping::*;

#[derive(Default)]
pub struct TextRendererPlugin;

impl Plugin for TextRendererPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.add_plugin(KayakFontPlugin)
.init_resource::<FontMapping>()
.add_system(process_loaded_fonts);
app.add_plugin(KayakFontPlugin);

let render_app = app.sub_app_mut(RenderApp);
render_app.add_system_to_stage(RenderStage::Queue, create_and_update_font_cache_texture);
}
}

fn process_loaded_fonts(
mut font_mapping: ResMut<FontMapping>,
fonts: Res<Assets<KayakFont>>,
bevy_context: Option<Res<BevyContext>>,
) {
if let Some(context) = bevy_context {
if context.is_added() {
font_mapping.mark_all_as_new();
}
font_mapping.add_loaded_to_kayak(&fonts, &context);
}
}

fn create_and_update_font_cache_texture(
device: Res<RenderDevice>,
queue: Res<RenderQueue>,
Expand Down
1 change: 1 addition & 0 deletions bevy_kayak_ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ bytemuck = "1.7.2"
bevy = { version = "0.6.0" }
kayak_core = { path = "../kayak_core" }
kayak_font = { path = "../kayak_font" }
bevy_kayak_renderer = { path = "../bevy_kayak_renderer" }
serde = "1.0"
serde_json = "1.0"
serde_path_to_error = "0.1"
11 changes: 5 additions & 6 deletions bevy_kayak_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@ use bevy::{
};

mod bevy_context;
mod camera;
mod cursor;
mod key;
mod render;

use crate::cursor::convert_cursor_icon;
pub use bevy_context::BevyContext;
pub use camera::*;
pub use bevy_kayak_renderer::camera::*;
use kayak_core::{bind, Binding, InputEvent, MutableBound};
pub use render::unified::font::FontMapping;
pub use render::unified::image::ImageManager;
pub use render::font::FontMapping;
pub use render::image::ImageManager;

#[derive(Default)]
pub struct BevyKayakUIPlugin;

impl Plugin for BevyKayakUIPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.insert_resource(bind(WindowSize::default()))
.add_plugin(render::BevyKayakUIRenderPlugin)
.add_plugin(camera::KayakUICameraPlugin)
.add_plugin(bevy_kayak_renderer::BevyKayakRendererPlugin)
.add_plugin(render::BevyKayakUIExtractPlugin)
.add_system(update_window_size)
.add_system(process_events.exclusive_system())
.add_system(update.exclusive_system());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use bevy::{
sprite::Rect,
};
use kayak_core::render_primitive::RenderPrimitive;
use kayak_core::styles::Corner;
use kayak_font::{Alignment, CoordinateSystem, KayakFont};

use crate::{
use crate::to_bevy_color;
use bevy_kayak_renderer::{
render::unified::pipeline::{ExtractQuadBundle, ExtractedQuad, UIQuadType},
to_bevy_color,
Corner,
};

use super::font_mapping::FontMapping;
Expand Down

0 comments on commit 4b45882

Please sign in to comment.