Skip to content

Commit

Permalink
add render bundle renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
melody-rs committed Apr 18, 2024
1 parent 02459da commit 2593559
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 116 deletions.
19 changes: 12 additions & 7 deletions crates/components/src/map_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
// You should have received a copy of the GNU General Public License
// along with Luminol. If not, see <http://www.gnu.org/licenses/>.

use std::sync::Arc;

use itertools::Itertools;

pub struct MapView {
Expand All @@ -30,8 +28,9 @@ pub struct MapView {

/// The first sprite is for drawing on the tilemap,
/// and the second sprite is for the hover preview.
pub preview_events: luminol_data::OptionVec<Arc<luminol_graphics::Event>>, // TODO only add events when they are being displayed as a preview
pub map_view: luminol_graphics::MapView,
pub preview_events:
luminol_data::OptionVec<luminol_graphics::ArcRenderer<luminol_graphics::Event>>, // TODO only add events when they are being displayed as a preview
pub map_view: luminol_graphics::BundleRenderer<luminol_graphics::MapView>,

pub selected_layer: SelectedLayer,
pub selected_event_id: Option<usize>,
Expand Down Expand Up @@ -87,13 +86,13 @@ impl MapView {
|x, y, passage| passages[(x, y)] = passage,
);

let map_view = luminol_graphics::MapView::new(
let map_view = luminol_graphics::BundleRenderer::new(luminol_graphics::MapView::new(
&update_state.graphics,
update_state.filesystem,
&map,
tileset,
&passages,
)?;
)?);

let preview_events = map
.events
Expand All @@ -105,7 +104,7 @@ impl MapView {
e,
map_view.atlas(),
)
.map(|opt_e| opt_e.map(|e| (id, Arc::new(e))))
.map(|opt_e| opt_e.map(|e| (id, luminol_graphics::ArcRenderer::new(e))))
})
.flatten_ok()
.try_collect()?;
Expand Down Expand Up @@ -378,9 +377,15 @@ impl MapView {
}
}

let time = ui.input(|i| i.time);
self.map_view
.increment_animation_time(&graphics_state, time);
self.map_view
.paint(&graphics_state, ui.painter(), canvas_rect);

ui.ctx()
.request_repaint_after(std::time::Duration::from_secs_f32(1.0 / 16.0));

ui.painter().rect_stroke(
map_rect,
5.,
Expand Down
31 changes: 4 additions & 27 deletions crates/graphics/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,37 +119,14 @@ impl Event {
pub fn set_proj(&self, render_state: &luminol_egui_wgpu::RenderState, proj: glam::Mat4) {
self.viewport.set_proj(render_state, proj);
}
}

pub fn draw<'rpass>(
impl crate::Renderable for Event {
fn draw<'rpass>(
&'rpass self,
graphics_state: &'rpass GraphicsState,
render_pass: &mut impl wgpu::util::RenderEncoder<'rpass>,
) {
self.sprite.draw(graphics_state, render_pass);
}

pub fn paint(
self: Arc<Self>,
graphics_state: Arc<GraphicsState>,
painter: &egui::Painter,
rect: egui::Rect,
) {
struct Callback(Arc<Event>, Arc<GraphicsState>);

impl luminol_egui_wgpu::CallbackTrait for Callback {
fn paint<'a>(
&'a self,
_info: egui::PaintCallbackInfo,
render_pass: &mut wgpu::RenderPass<'a>,
_callback_resources: &'a luminol_egui_wgpu::CallbackResources,
) {
self.0.draw(&self.1, render_pass);
}
}

painter.add(luminol_egui_wgpu::Callback::new_paint_callback(
rect,
Callback(self, graphics_state),
));
self.sprite.draw(graphics_state, render_pass)
}
}
3 changes: 3 additions & 0 deletions crates/graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub mod event;
pub mod map;
pub mod plane;

pub mod renderers;
pub use renderers::{ArcRenderer, BundleRenderer, Renderable};

pub use event::Event;
pub use map::MapView;
pub use plane::Plane;
Expand Down
100 changes: 21 additions & 79 deletions crates/graphics/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use color_eyre::eyre::Context;
use itertools::Itertools;

use std::sync::Arc;
use std::time::Duration;

use crate::{Collision, Event, GraphicsState, Grid, Plane, Tiles, Viewport};

Expand All @@ -32,15 +31,13 @@ pub struct MapView {
pub events: luminol_data::OptionVec<Event>,
viewport: Arc<Viewport>,

ani_time: Option<f64>,
ani_time: f64,

pub fog_enabled: bool,
pub pano_enabled: bool,
pub coll_enabled: bool,
pub grid_enabled: bool,
pub enabled_layers: Vec<bool>,

render_bundle: Option<Arc<wgpu::RenderBundle>>,
}

impl MapView {
Expand Down Expand Up @@ -142,15 +139,13 @@ impl MapView {

viewport,

ani_time: None,
ani_time: 0.0,

fog_enabled: true,
pano_enabled: true,
coll_enabled: false,
grid_enabled: true,
enabled_layers: vec![true; map.data.zsize()],

render_bundle: None,
})
}

Expand Down Expand Up @@ -180,101 +175,48 @@ impl MapView {
self.viewport.set_proj(render_state, proj);
}

pub fn paint(
&mut self,
graphics_state: &GraphicsState,
painter: &egui::Painter,
rect: egui::Rect,
) {
struct Callback(Arc<wgpu::RenderBundle>);

impl luminol_egui_wgpu::CallbackTrait for Callback {
fn paint<'a>(
&'a self,
_: egui::PaintCallbackInfo,
render_pass: &mut wgpu::RenderPass<'a>,
_: &'a luminol_egui_wgpu::CallbackResources,
) {
render_pass.execute_bundles(std::iter::once(self.0.as_ref()));
}
pub fn increment_animation_time(&mut self, graphics_state: &GraphicsState, time: f64) {
if time - self.ani_time >= 16. / 60. {
self.ani_time = time;
self.tiles
.autotiles
.inc_ani_index(&graphics_state.render_state);
}

let time = painter.ctx().input(|i| i.time);
if let Some(ani_time) = self.ani_time {
if time - ani_time >= 16. / 60. {
self.ani_time = Some(time);
self.tiles
.autotiles
.inc_ani_index(&graphics_state.render_state);
}
} else {
self.ani_time = Some(time);
}

painter
.ctx()
.request_repaint_after(Duration::from_secs_f64(16. / 60.));

let bundle = match self.render_bundle.clone() {
Some(bundle) => bundle,
None => {
let render_bundle = self.make_render_bundle(graphics_state);
self.render_bundle.insert(render_bundle).clone()
}
};

painter.add(luminol_egui_wgpu::Callback::new_paint_callback(
rect,
Callback(bundle),
));
}
}

fn make_render_bundle(&self, graphics_state: &GraphicsState) -> Arc<wgpu::RenderBundle> {
let mut render_bundle_encoder = graphics_state
.render_state
.device
.create_render_bundle_encoder(&wgpu::RenderBundleEncoderDescriptor {
label: Some("luminol map view render bundle encoder"),
color_formats: &[Some(graphics_state.render_state.target_format)],
sample_count: 1,
..Default::default()
});

impl crate::Renderable for MapView {
fn draw<'rpass>(
&'rpass self,
graphics_state: &'rpass GraphicsState,
render_pass: &mut impl wgpu::util::RenderEncoder<'rpass>,
) {
if self.pano_enabled {
if let Some(panorama) = &self.panorama {
panorama.draw(graphics_state, &mut render_bundle_encoder);
panorama.draw(graphics_state, render_pass);
}
}

self.tiles.draw(
graphics_state,
&self.enabled_layers,
&mut render_bundle_encoder,
);
self.tiles
.draw(graphics_state, &self.enabled_layers, render_pass);

for (_, event) in self.events.iter() {
event.draw(graphics_state, &mut render_bundle_encoder);
event.draw(graphics_state, render_pass);
}

if self.fog_enabled {
if let Some(fog) = &self.fog {
fog.draw(graphics_state, &mut render_bundle_encoder);
fog.draw(graphics_state, render_pass);
}
}

if self.coll_enabled {
self.collision
.draw(graphics_state, &mut render_bundle_encoder);
self.collision.draw(graphics_state, render_pass);
}

if self.grid_enabled {
// self.grid
// .draw(graphics_state, info, &mut render_bundle_encoder);
}

let bundle = render_bundle_encoder.finish(&wgpu::RenderBundleDescriptor {
label: Some("luminol map view render bundle"),
});
Arc::new(bundle)
}
}
6 changes: 4 additions & 2 deletions crates/graphics/src/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ impl Plane {

Self { sprite }
}
}

pub fn draw<'rpass>(
impl crate::Renderable for Plane {
fn draw<'rpass>(
&'rpass self,
graphics_state: &'rpass GraphicsState,
render_pass: &mut impl wgpu::util::RenderEncoder<'rpass>,
) {
self.sprite.draw(graphics_state, render_pass);
self.sprite.draw(graphics_state, render_pass)
}
}
4 changes: 3 additions & 1 deletion crates/graphics/src/primitives/sprite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ impl Sprite {
bytemuck::cast_slice(&vertices),
);
}
}

pub fn draw<'rpass>(
impl crate::Renderable for Sprite {
fn draw<'rpass>(
&'rpass self,
graphics_state: &'rpass GraphicsState,
render_pass: &mut impl wgpu::util::RenderEncoder<'rpass>,
Expand Down
Loading

0 comments on commit 2593559

Please sign in to comment.