Skip to content

Commit

Permalink
Added WindowBundle to RenderBaseAppExt.
Browse files Browse the repository at this point in the history
  • Loading branch information
azriel91 committed Jun 1, 2019
1 parent 3822efe commit 525d2d3
Showing 1 changed file with 52 additions and 22 deletions.
74 changes: 52 additions & 22 deletions amethyst_test/src/render_app_ext.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::{ops::Deref, sync::Arc};

use amethyst::{
assets::Processor,
core::TransformBundle,
ecs::Resources,
ecs::{ReadExpect, Resources, SystemData},
renderer::{
pass::{DrawFlat2DDesc, DrawFlat2DTransparentDesc},
rendy::{
factory::Factory,
graph::{
present::PresentNode,
render::{RenderGroupDesc, SubpassBuilder},
GraphBuilder,
},
Expand All @@ -17,11 +20,11 @@ use amethyst::{
},
},
sprite::SpriteSheet,
sprite_visibility::SpriteVisibilitySortingSystem,
types::DefaultBackend,
GraphCreator, RenderingSystem,
},
ui::DrawUiDesc,
window::ScreenDimensions,
window::{DisplayConfig, ScreenDimensions, Window, WindowBundle},
GameData, StateEvent, StateEventReader,
};

Expand All @@ -37,14 +40,24 @@ impl RenderBaseAppExt
for AmethystApplication<GameData<'static, 'static>, StateEvent, StateEventReader>
{
fn render_base() -> Self {
let mut display_config = DisplayConfig::default();
display_config.dimensions = Some((SCREEN_WIDTH, SCREEN_HEIGHT));
display_config.visibility = false;

AmethystApplication::blank()
.with_rendering_system()
.with_bundle(TransformBundle::new())
.with_bundle(WindowBundle::from_config(display_config))
.with_rendering_system()
.with_system(
Processor::<SpriteSheet>::new(),
"sprite_sheet_processor",
&[],
)
.with_system(
SpriteVisibilitySortingSystem::new(),
"sprite_visibility_system",
&["transform_system"],
)
.with_resource(ScreenDimensions::new(SCREEN_WIDTH, SCREEN_HEIGHT, HIDPI))
}
}
Expand All @@ -62,27 +75,46 @@ where
{
fn with_rendering_system(self) -> Self {
self.with_thread_local(RenderingSystem::<DefaultBackend, _>::new(
RenderGraphEmpty::default(),
RenderGraph::default(),
))
}
}

/// Empty render graph in case the `RenderingSystem` is only needed to load textures and meshes.
/// Default render graph in case the `RenderingSystem` is only needed to load textures and meshes.
#[derive(Default)]
pub struct RenderGraphEmpty;
pub struct RenderGraph {
dimensions: Option<ScreenDimensions>,
surface_format: Option<Format>,
dirty: bool,
}

impl GraphCreator<DefaultBackend> for RenderGraphEmpty {
fn rebuild(&mut self, _res: &Resources) -> bool {
false
impl GraphCreator<DefaultBackend> for RenderGraph {
fn rebuild(&mut self, res: &Resources) -> bool {
// Rebuild when dimensions change, but wait until at least two frames have the same.
let new_dimensions = res.try_fetch::<ScreenDimensions>();
if self.dimensions.as_ref() != new_dimensions.as_ref().map(|d| d.deref()) {
self.dirty = true;
self.dimensions = new_dimensions.map(|d| d.clone());
return false;
}
return self.dirty;
}

fn builder(
&mut self,
_factory: &mut Factory<DefaultBackend>,
_res: &Resources,
factory: &mut Factory<DefaultBackend>,
res: &Resources,
) -> GraphBuilder<DefaultBackend, Resources> {
let window_kind = Kind::D2(SCREEN_WIDTH, SCREEN_HEIGHT, 1, 1);
let surface_format = Format::Rgba32Sfloat; // Normally extracted from the `Window`.
self.dirty = false;

let window = <ReadExpect<'_, Arc<Window>>>::fetch(res);
let surface = factory.create_surface(&window);
// cache surface format to speed things up
let surface_format = *self
.surface_format
.get_or_insert_with(|| factory.get_surface_format(&surface));
let dimensions = self.dimensions.as_ref().unwrap();
let window_kind = Kind::D2(dimensions.width() as u32, dimensions.height() as u32, 1, 1);

let mut graph_builder = GraphBuilder::new();
let colour = graph_builder.create_image(
Expand All @@ -100,26 +132,24 @@ impl GraphCreator<DefaultBackend> for RenderGraphEmpty {
Some(ClearValue::DepthStencil(ClearDepthStencil(1., 0))),
);

let _sprite = graph_builder.add_node(
let sprite = graph_builder.add_node(
SubpassBuilder::new()
.with_group(DrawFlat2DDesc::new().builder())
.with_color(colour)
.with_depth_stencil(depth)
.into_pass(),
);
let _sprite_trans = graph_builder.add_node(
let sprite_trans = graph_builder.add_node(
SubpassBuilder::new()
.with_group(DrawFlat2DTransparentDesc::new().builder())
.with_color(colour)
.with_depth_stencil(depth)
.into_pass(),
);
let _ui = graph_builder.add_node(
SubpassBuilder::new()
.with_group(DrawUiDesc::new().builder())
.with_color(colour)
.with_depth_stencil(depth)
.into_pass(),
let _present = graph_builder.add_node(
PresentNode::builder(factory, surface, colour)
.with_dependency(sprite_trans)
.with_dependency(sprite),
);

graph_builder
Expand Down

0 comments on commit 525d2d3

Please sign in to comment.