From e76f3b3acff04f10c45a60aa3d258cdf8744d989 Mon Sep 17 00:00:00 2001 From: Lily Lyons Date: Wed, 29 Nov 2023 23:47:14 -0800 Subject: [PATCH] refactor(tilemap): :recycle: don't pass viewports around constantly --- crates/components/src/tilepicker.rs | 37 ++++++++------------- crates/graphics/src/atlas_cache.rs | 6 ++-- crates/graphics/src/collision/mod.rs | 23 +++++-------- crates/graphics/src/event.rs | 46 +++++++++++--------------- crates/graphics/src/map.rs | 39 ++++++++++------------ crates/graphics/src/plane.rs | 7 ++-- crates/graphics/src/sprite/graphic.rs | 8 +++-- crates/graphics/src/sprite/mod.rs | 22 ++++++------ crates/graphics/src/sprite/shader.rs | 8 +++-- crates/graphics/src/sprite/vertices.rs | 6 ++-- crates/graphics/src/tiles/atlas.rs | 3 +- crates/graphics/src/tiles/autotiles.rs | 8 +++-- crates/graphics/src/tiles/instance.rs | 7 ++-- crates/graphics/src/tiles/mod.rs | 11 +++--- crates/graphics/src/tiles/opacity.rs | 8 +++-- crates/graphics/src/tiles/shader.rs | 4 +-- crates/graphics/src/viewport.rs | 10 +++--- 17 files changed, 123 insertions(+), 130 deletions(-) diff --git a/crates/components/src/tilepicker.rs b/crates/components/src/tilepicker.rs index f377e94f..2adbd2fb 100644 --- a/crates/components/src/tilepicker.rs +++ b/crates/components/src/tilepicker.rs @@ -29,6 +29,7 @@ pub struct Tilepicker { drag_origin: Option, resources: Arc, + viewport: Arc, ani_time: Option, } @@ -36,7 +37,6 @@ pub struct Tilepicker { struct Resources { tiles: luminol_graphics::tiles::Tiles, collision: luminol_graphics::collision::Collision, - viewport: luminol_graphics::viewport::Viewport, } struct Callback { @@ -61,20 +61,14 @@ impl egui_wgpu::CallbackTrait for Callback { render_pass: &mut wgpu::RenderPass<'a>, _callback_resources: &'a egui_wgpu::CallbackResources, ) { - self.resources.tiles.draw( - &self.graphics_state, - &self.resources.viewport, - &[true], - None, - render_pass, - ); + self.resources + .tiles + .draw(&self.graphics_state, &[true], None, render_pass); if self.coll_enabled { - self.resources.collision.draw( - &self.graphics_state, - &self.resources.viewport, - render_pass, - ); + self.resources + .collision + .draw(&self.graphics_state, render_pass); } } } @@ -136,15 +130,15 @@ impl Tilepicker { tilepicker_data, ); - let viewport = luminol_graphics::viewport::Viewport::new( + let viewport = Arc::new(luminol_graphics::viewport::Viewport::new( &update_state.graphics, 256., atlas.tileset_height as f32 + 32., - ); + )); let tiles = luminol_graphics::tiles::Tiles::new( &update_state.graphics, - &viewport, + viewport.clone(), atlas, &tilepicker_data, ); @@ -167,16 +161,13 @@ impl Tilepicker { .copy_from_slice(&tileset.passages.as_slice()[384..384 + length]); let collision = luminol_graphics::collision::Collision::new( &update_state.graphics, - &viewport, + viewport.clone(), &passages, ); Ok(Self { - resources: Arc::new(Resources { - tiles, - collision, - viewport, - }), + resources: Arc::new(Resources { tiles, collision }), + viewport, ani_time: None, selected_tiles_left: 0, selected_tiles_top: 0, @@ -233,7 +224,7 @@ impl Tilepicker { .intersect(scroll_rect.translate(canvas_rect.min.to_vec2())); let scroll_rect = absolute_scroll_rect.translate(-canvas_rect.min.to_vec2()); - self.resources.viewport.set_proj( + self.viewport.set_proj( &graphics_state.render_state, glam::Mat4::orthographic_rh( scroll_rect.left(), diff --git a/crates/graphics/src/atlas_cache.rs b/crates/graphics/src/atlas_cache.rs index fc87a80e..5617df05 100644 --- a/crates/graphics/src/atlas_cache.rs +++ b/crates/graphics/src/atlas_cache.rs @@ -14,7 +14,7 @@ // // You should have received a copy of the GNU General Public License // along with Luminol. If not, see . -use crate::tiles::Atlas; +use crate::{tiles::Atlas, GraphicsState}; #[derive(Default, Debug)] pub struct Cache { @@ -24,7 +24,7 @@ pub struct Cache { impl Cache { pub fn load_atlas( &self, - graphics_state: &crate::GraphicsState, + graphics_state: &GraphicsState, filesystem: &impl luminol_filesystem::FileSystem, tileset: &luminol_data::rpg::Tileset, ) -> anyhow::Result { @@ -37,7 +37,7 @@ impl Cache { pub fn reload_atlas( &self, - graphics_state: &crate::GraphicsState, + graphics_state: &GraphicsState, filesystem: &impl luminol_filesystem::FileSystem, tileset: &luminol_data::rpg::Tileset, ) -> anyhow::Result { diff --git a/crates/graphics/src/collision/mod.rs b/crates/graphics/src/collision/mod.rs index db8b5a82..9ea885b4 100644 --- a/crates/graphics/src/collision/mod.rs +++ b/crates/graphics/src/collision/mod.rs @@ -15,9 +15,11 @@ // You should have received a copy of the GNU General Public License // along with Luminol. If not, see . +use std::sync::Arc; + use crate::{ viewport::{self, Viewport}, - BindGroupBuilder, BindGroupLayoutBuilder, + BindGroupBuilder, BindGroupLayoutBuilder, GraphicsState, }; use instance::Instances; @@ -31,6 +33,7 @@ mod vertex; #[derive(Debug)] pub struct Collision { pub instances: Instances, + pub viewport: Arc, pub bind_group: wgpu::BindGroup, } @@ -145,8 +148,8 @@ pub fn calculate_passage(layers: impl Iterator impl Collision { pub fn new( - graphics_state: &crate::GraphicsState, - viewport: &Viewport, + graphics_state: &GraphicsState, + viewport: Arc, passages: &luminol_data::Table2, ) -> Self { let instances = Instances::new(&graphics_state.render_state, passages); @@ -163,6 +166,7 @@ impl Collision { Self { instances, + viewport, bind_group, } } @@ -178,25 +182,16 @@ impl Collision { pub fn draw<'rpass>( &'rpass self, - graphics_state: &'rpass crate::GraphicsState, - viewport: &crate::viewport::Viewport, + graphics_state: &'rpass GraphicsState, render_pass: &mut wgpu::RenderPass<'rpass>, ) { - #[repr(C)] - #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] - struct VertexPushConstant { - viewport: [u8; 64], - } - render_pass.push_debug_group("tilemap collision renderer"); render_pass.set_pipeline(&graphics_state.pipelines.collision); if graphics_state.push_constants_supported() { render_pass.set_push_constants( wgpu::ShaderStages::VERTEX, 0, - bytemuck::bytes_of(&VertexPushConstant { - viewport: viewport.as_bytes(), - }), + &self.viewport.as_bytes(), ); } self.instances.draw(render_pass); diff --git a/crates/graphics/src/event.rs b/crates/graphics/src/event.rs index fdec02d3..677e8c45 100644 --- a/crates/graphics/src/event.rs +++ b/crates/graphics/src/event.rs @@ -17,22 +17,17 @@ use std::sync::Arc; -use crate::{sprite::Sprite, viewport::Viewport, GraphicsState}; +use crate::{quad::Quad, sprite::Sprite, tiles::Atlas, viewport::Viewport, GraphicsState}; #[derive(Debug)] pub struct Event { - resources: Arc, + sprite: Arc, + viewport: Arc, pub sprite_size: egui::Vec2, } -#[derive(Debug)] -struct Resources { - sprite: Sprite, - viewport: Viewport, -} - struct Callback { - resources: Arc, + sprite: Arc, graphics_state: Arc, } @@ -51,19 +46,17 @@ impl egui_wgpu::CallbackTrait for Callback { render_pass: &mut wgpu::RenderPass<'a>, _callback_resources: &'a egui_wgpu::CallbackResources, ) { - self.resources - .sprite - .draw(&self.graphics_state, &self.resources.viewport, render_pass); + self.sprite.draw(&self.graphics_state, render_pass); } } impl Event { // code smell, fix pub fn new( - graphics_state: &crate::GraphicsState, + graphics_state: &GraphicsState, filesystem: &impl luminol_filesystem::FileSystem, event: &luminol_data::rpg::Event, - atlas: &crate::tiles::Atlas, + atlas: &Atlas, ) -> anyhow::Result> { let Some(page) = event.pages.first() else { anyhow::bail!("event does not have first page"); @@ -85,7 +78,7 @@ impl Event { // Why does this have to be + 1? let quad = atlas.calc_quad((id + 1) as i16); - let viewport = Viewport::new(graphics_state, 32., 32.); + let viewport = Arc::new(Viewport::new(graphics_state, 32., 32.)); (quad, viewport, egui::vec2(32., 32.)) } else { @@ -107,47 +100,48 @@ impl Event { ), egui::vec2(cw - 0.02, ch - 0.02), ); - let quad = crate::quad::Quad::new(pos, tex_coords, 0.0); + let quad = Quad::new(pos, tex_coords, 0.0); - let viewport = Viewport::new(graphics_state, cw, ch); + let viewport = Arc::new(Viewport::new(graphics_state, cw, ch)); (quad, viewport, egui::vec2(cw, ch)) }; - let sprite = Sprite::new( + let sprite = Arc::new(Sprite::new( graphics_state, - &viewport, + viewport.clone(), quads, texture, page.graphic.blend_type, page.graphic.character_hue, page.graphic.opacity, - ); + )); Ok(Some(Self { - resources: Arc::new(Resources { sprite, viewport }), + sprite, + viewport, sprite_size, })) } - pub fn sprite(&self) -> &crate::sprite::Sprite { - &self.resources.sprite + pub fn sprite(&self) -> &Sprite { + &self.sprite } pub fn set_proj(&self, render_state: &egui_wgpu::RenderState, proj: glam::Mat4) { - self.resources.viewport.set_proj(render_state, proj); + self.viewport.set_proj(render_state, proj); } pub fn paint( &self, - graphics_state: Arc, + graphics_state: Arc, painter: &egui::Painter, rect: egui::Rect, ) { painter.add(egui_wgpu::Callback::new_paint_callback( rect, Callback { - resources: self.resources.clone(), + sprite: self.sprite.clone(), graphics_state, }, )); diff --git a/crates/graphics/src/map.rs b/crates/graphics/src/map.rs index 2cd67aed..fda93d3d 100644 --- a/crates/graphics/src/map.rs +++ b/crates/graphics/src/map.rs @@ -24,6 +24,7 @@ use crate::{collision::Collision, tiles::Tiles, viewport::Viewport, GraphicsStat #[derive(Debug)] pub struct Map { resources: Arc, + viewport: Arc, ani_time: Option, pub fog_enabled: bool, @@ -35,7 +36,6 @@ pub struct Map { #[derive(Debug)] struct Resources { tiles: Tiles, - viewport: Viewport, panorama: Option, fog: Option, collision: Collision, @@ -52,7 +52,7 @@ struct Callback { struct OverlayCallback { resources: Arc, - graphics_state: Arc, + graphics_state: Arc, fog_enabled: bool, coll_enabled: bool, @@ -79,13 +79,12 @@ impl egui_wgpu::CallbackTrait for Callback { ) { if self.pano_enabled { if let Some(panorama) = &self.resources.panorama { - panorama.draw(&self.graphics_state, &self.resources.viewport, render_pass); + panorama.draw(&self.graphics_state, render_pass); } } self.resources.tiles.draw( &self.graphics_state, - &self.resources.viewport, &self.enabled_layers, self.selected_layer, render_pass, @@ -102,23 +101,21 @@ impl egui_wgpu::CallbackTrait for OverlayCallback { ) { if self.fog_enabled { if let Some(fog) = &self.resources.fog { - fog.draw(&self.graphics_state, &self.resources.viewport, render_pass); + fog.draw(&self.graphics_state, render_pass); } } if self.coll_enabled { - self.resources.collision.draw( - &self.graphics_state, - &self.resources.viewport, - render_pass, - ); + self.resources + .collision + .draw(&self.graphics_state, render_pass); } } } impl Map { pub fn new( - graphics_state: &crate::GraphicsState, + graphics_state: &GraphicsState, filesystem: &impl luminol_filesystem::FileSystem, map: &luminol_data::rpg::Map, tileset: &luminol_data::rpg::Tileset, @@ -128,19 +125,19 @@ impl Map { .atlas_cache .load_atlas(graphics_state, filesystem, tileset)?; - let viewport = crate::viewport::Viewport::new( + let viewport = Arc::new(Viewport::new( graphics_state, map.width as f32 * 32., map.height as f32 * 32., - ); + )); - let tiles = Tiles::new(graphics_state, &viewport, atlas, &map.data); - let collision = Collision::new(graphics_state, &viewport, passages); + let tiles = Tiles::new(graphics_state, viewport.clone(), atlas, &map.data); + let collision = Collision::new(graphics_state, viewport.clone(), passages); let panorama = if let Some(ref panorama_name) = tileset.panorama_name { Some(Plane::new( graphics_state, - &viewport, + viewport.clone(), graphics_state.texture_loader.load_now_dir( filesystem, "Graphics/Panoramas", @@ -159,7 +156,7 @@ impl Map { let fog = if let Some(ref fog_name) = tileset.fog_name { Some(Plane::new( graphics_state, - &viewport, + viewport.clone(), graphics_state.texture_loader.load_now_dir( filesystem, "Graphics/Fogs", @@ -179,11 +176,11 @@ impl Map { Ok(Self { resources: std::sync::Arc::new(Resources { tiles, - viewport, panorama, fog, collision, }), + viewport, ani_time: None, @@ -217,12 +214,12 @@ impl Map { } pub fn set_proj(&self, render_state: &egui_wgpu::RenderState, proj: glam::Mat4) { - self.resources.viewport.set_proj(render_state, proj); + self.viewport.set_proj(render_state, proj); } pub fn paint( &mut self, - graphics_state: Arc, + graphics_state: Arc, painter: &egui::Painter, selected_layer: Option, rect: egui::Rect, @@ -259,7 +256,7 @@ impl Map { pub fn paint_overlay( &mut self, - graphics_state: Arc, + graphics_state: Arc, painter: &egui::Painter, rect: egui::Rect, ) { diff --git a/crates/graphics/src/plane.rs b/crates/graphics/src/plane.rs index 7cc52355..4836a879 100644 --- a/crates/graphics/src/plane.rs +++ b/crates/graphics/src/plane.rs @@ -28,7 +28,7 @@ impl Plane { #[allow(clippy::too_many_arguments)] pub fn new( graphics_state: &GraphicsState, - viewport: &Viewport, + viewport: Arc, texture: Arc, hue: i32, zoom: i32, @@ -67,10 +67,9 @@ impl Plane { pub fn draw<'rpass>( &'rpass self, - graphics_state: &'rpass crate::GraphicsState, - viewport: &crate::viewport::Viewport, + graphics_state: &'rpass GraphicsState, render_pass: &mut wgpu::RenderPass<'rpass>, ) { - self.sprite.draw(graphics_state, viewport, render_pass); + self.sprite.draw(graphics_state, render_pass); } } diff --git a/crates/graphics/src/sprite/graphic.rs b/crates/graphics/src/sprite/graphic.rs index 3542d3b2..015bea1f 100644 --- a/crates/graphics/src/sprite/graphic.rs +++ b/crates/graphics/src/sprite/graphic.rs @@ -18,6 +18,8 @@ use crossbeam::atomic::AtomicCell; use wgpu::util::DeviceExt; +use crate::{BindGroupLayoutBuilder, GraphicsState}; + #[derive(Debug)] pub struct Graphic { data: AtomicCell, @@ -34,7 +36,7 @@ struct Data { } impl Graphic { - pub fn new(graphics_state: &crate::GraphicsState, hue: i32, opacity: i32) -> Self { + pub fn new(graphics_state: &GraphicsState, hue: i32, opacity: i32) -> Self { let hue = (hue % 360) as f32 / 360.0; let opacity = opacity as f32 / 255.; let data = Data { @@ -126,8 +128,8 @@ impl Graphic { } pub fn add_to_bind_group_layout( - layout_builder: &mut crate::BindGroupLayoutBuilder, -) -> &mut crate::BindGroupLayoutBuilder { + layout_builder: &mut BindGroupLayoutBuilder, +) -> &mut BindGroupLayoutBuilder { layout_builder.append( wgpu::ShaderStages::VERTEX_FRAGMENT, wgpu::BindingType::Buffer { diff --git a/crates/graphics/src/sprite/mod.rs b/crates/graphics/src/sprite/mod.rs index e44e9a26..270dec36 100644 --- a/crates/graphics/src/sprite/mod.rs +++ b/crates/graphics/src/sprite/mod.rs @@ -32,6 +32,7 @@ pub struct Sprite { pub graphic: graphic::Graphic, pub vertices: vertices::Vertices, pub blend_mode: luminol_data::BlendMode, + pub viewport: Arc, pub bind_group: wgpu::BindGroup, } @@ -39,7 +40,7 @@ pub struct Sprite { impl Sprite { pub fn new( graphics_state: &GraphicsState, - viewport: &Viewport, + viewport: Arc, quad: Quad, texture: Arc, blend_mode: luminol_data::BlendMode, @@ -70,16 +71,14 @@ impl Sprite { graphic, vertices, blend_mode, + viewport, + bind_group, } } - pub fn reupload_verts( - &self, - render_state: &egui_wgpu::RenderState, - quads: &[crate::quad::Quad], - ) { - let vertices = crate::quad::Quad::into_vertices(quads, self.texture.size()); + pub fn reupload_verts(&self, render_state: &egui_wgpu::RenderState, quads: &[Quad]) { + let vertices = Quad::into_vertices(quads, self.texture.size()); render_state.queue.write_buffer( &self.vertices.vertex_buffer, 0, @@ -89,15 +88,18 @@ impl Sprite { pub fn draw<'rpass>( &'rpass self, - graphics_state: &'rpass crate::GraphicsState, - viewport: &crate::viewport::Viewport, + graphics_state: &'rpass GraphicsState, render_pass: &mut wgpu::RenderPass<'rpass>, ) { render_pass.set_pipeline(&graphics_state.pipelines.sprites[&self.blend_mode]); render_pass.set_bind_group(0, &self.bind_group, &[]); if graphics_state.push_constants_supported() { - render_pass.set_push_constants(wgpu::ShaderStages::VERTEX, 0, &viewport.as_bytes()); + render_pass.set_push_constants( + wgpu::ShaderStages::VERTEX, + 0, + &self.viewport.as_bytes(), + ); render_pass.set_push_constants( wgpu::ShaderStages::FRAGMENT, 64, diff --git a/crates/graphics/src/sprite/shader.rs b/crates/graphics/src/sprite/shader.rs index 911b0681..d3c72d72 100644 --- a/crates/graphics/src/sprite/shader.rs +++ b/crates/graphics/src/sprite/shader.rs @@ -15,9 +15,11 @@ // You should have received a copy of the GNU General Public License // along with Luminol. If not, see . +use crate::{vertex::Vertex, BindGroupLayouts}; + fn create_shader( render_state: &egui_wgpu::RenderState, - bind_group_layouts: &crate::BindGroupLayouts, + bind_group_layouts: &BindGroupLayouts, target: wgpu::BlendState, ) -> wgpu::RenderPipeline { let push_constants_supported = crate::push_constants_supported(render_state); @@ -86,7 +88,7 @@ fn create_shader( vertex: wgpu::VertexState { module: &shader_module, entry_point: "vs_main", - buffers: &[crate::vertex::Vertex::desc()], + buffers: &[Vertex::desc()], }, fragment: Some(wgpu::FragmentState { module: &shader_module, @@ -108,7 +110,7 @@ fn create_shader( pub fn create_sprite_shaders( render_state: &egui_wgpu::RenderState, - bind_group_layouts: &crate::BindGroupLayouts, + bind_group_layouts: &BindGroupLayouts, ) -> std::collections::HashMap { [ ( diff --git a/crates/graphics/src/sprite/vertices.rs b/crates/graphics/src/sprite/vertices.rs index 43cb1e7c..87fedbed 100644 --- a/crates/graphics/src/sprite/vertices.rs +++ b/crates/graphics/src/sprite/vertices.rs @@ -15,6 +15,8 @@ // You should have received a copy of the GNU General Public License // along with Luminol. If not, see . +use crate::quad::Quad; + #[derive(Debug)] pub struct Vertices { pub vertex_buffer: wgpu::Buffer, @@ -23,10 +25,10 @@ pub struct Vertices { impl Vertices { pub fn from_quads( render_state: &egui_wgpu::RenderState, - quads: &[crate::quad::Quad], + quads: &[Quad], extents: wgpu::Extent3d, ) -> Self { - let (vertex_buffer, _) = crate::quad::Quad::into_buffer(render_state, quads, extents); + let (vertex_buffer, _) = Quad::into_buffer(render_state, quads, extents); Self { vertex_buffer } } diff --git a/crates/graphics/src/tiles/atlas.rs b/crates/graphics/src/tiles/atlas.rs index 4aa5b155..9a959478 100644 --- a/crates/graphics/src/tiles/atlas.rs +++ b/crates/graphics/src/tiles/atlas.rs @@ -18,8 +18,7 @@ use anyhow::Context; use itertools::Itertools; use super::autotile_ids::AUTOTILES; -use crate::quad::Quad; -use crate::{GraphicsState, Texture}; +use crate::{quad::Quad, GraphicsState, Texture}; pub const MAX_SIZE: u32 = 8192; // Max texture size in one dimension pub const TILE_SIZE: u32 = 32; // Tiles are 32x32 diff --git a/crates/graphics/src/tiles/autotiles.rs b/crates/graphics/src/tiles/autotiles.rs index dddd5c36..c184b66f 100644 --- a/crates/graphics/src/tiles/autotiles.rs +++ b/crates/graphics/src/tiles/autotiles.rs @@ -18,6 +18,8 @@ use crossbeam::atomic::AtomicCell; use wgpu::util::DeviceExt; +use crate::{BindGroupLayoutBuilder, GraphicsState}; + #[derive(Debug)] pub struct Autotiles { data: AtomicCell, @@ -35,7 +37,7 @@ struct Data { } impl Autotiles { - pub fn new(graphics_state: &crate::GraphicsState, atlas: &super::Atlas) -> Self { + pub fn new(graphics_state: &GraphicsState, atlas: &super::Atlas) -> Self { let autotiles = Data { autotile_frames: atlas.autotile_frames, autotile_region_width: atlas.autotile_width, @@ -87,8 +89,8 @@ impl Autotiles { } pub fn add_to_bind_group_layout( - layout_builder: &mut crate::BindGroupLayoutBuilder, -) -> &mut crate::BindGroupLayoutBuilder { + layout_builder: &mut BindGroupLayoutBuilder, +) -> &mut BindGroupLayoutBuilder { layout_builder.append( wgpu::ShaderStages::VERTEX_FRAGMENT, wgpu::BindingType::Buffer { diff --git a/crates/graphics/src/tiles/instance.rs b/crates/graphics/src/tiles/instance.rs index 24277178..c2336e19 100644 --- a/crates/graphics/src/tiles/instance.rs +++ b/crates/graphics/src/tiles/instance.rs @@ -18,6 +18,8 @@ use itertools::Itertools; use wgpu::util::DeviceExt; +use crate::quad::Quad; + #[derive(Debug)] pub struct Instances { instance_buffer: wgpu::Buffer, @@ -35,7 +37,7 @@ struct Instance { layer: u32, } -const TILE_QUAD: crate::quad::Quad = crate::quad::Quad::new( +const TILE_QUAD: Quad = Quad::new( egui::Rect::from_min_max(egui::pos2(0., 0.), egui::pos2(32., 32.0)), // slightly smaller than 32x32 to reduce bleeding from adjacent pixels in the atlas egui::Rect::from_min_max(egui::pos2(0.01, 0.01), egui::pos2(31.99, 31.99)), @@ -58,8 +60,7 @@ impl Instances { usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, }); - let (vertex_buffer, _) = - crate::quad::Quad::into_buffer(render_state, &[TILE_QUAD], atlas_size); + let (vertex_buffer, _) = Quad::into_buffer(render_state, &[TILE_QUAD], atlas_size); Self { instance_buffer, diff --git a/crates/graphics/src/tiles/mod.rs b/crates/graphics/src/tiles/mod.rs index 8ec7207b..c9dcf038 100644 --- a/crates/graphics/src/tiles/mod.rs +++ b/crates/graphics/src/tiles/mod.rs @@ -15,6 +15,8 @@ // You should have received a copy of the GNU General Public License // along with Luminol. If not, see . +use std::sync::Arc; + use crate::{ viewport::{self, Viewport}, BindGroupBuilder, BindGroupLayoutBuilder, GraphicsState, @@ -39,6 +41,7 @@ pub struct Tiles { pub atlas: Atlas, pub instances: Instances, pub opacity: Opacity, + pub viewport: Arc, pub bind_group: wgpu::BindGroup, } @@ -46,7 +49,7 @@ pub struct Tiles { impl Tiles { pub fn new( graphics_state: &GraphicsState, - viewport: &Viewport, + viewport: Arc, atlas: Atlas, tiles: &luminol_data::Table3, ) -> Self { @@ -81,6 +84,7 @@ impl Tiles { opacity, bind_group, + viewport, } } @@ -95,8 +99,7 @@ impl Tiles { pub fn draw<'rpass>( &'rpass self, - graphics_state: &'rpass crate::GraphicsState, - viewport: &crate::viewport::Viewport, + graphics_state: &'rpass GraphicsState, enabled_layers: &[bool], selected_layer: Option, render_pass: &mut wgpu::RenderPass<'rpass>, @@ -117,7 +120,7 @@ impl Tiles { wgpu::ShaderStages::VERTEX, 0, bytemuck::bytes_of(&VertexPushConstant { - viewport: viewport.as_bytes(), + viewport: self.viewport.as_bytes(), autotiles: self.autotiles.as_bytes(), }), ); diff --git a/crates/graphics/src/tiles/opacity.rs b/crates/graphics/src/tiles/opacity.rs index c638b55f..991ed544 100644 --- a/crates/graphics/src/tiles/opacity.rs +++ b/crates/graphics/src/tiles/opacity.rs @@ -18,6 +18,8 @@ use crossbeam::atomic::AtomicCell; use wgpu::util::DeviceExt; +use crate::{BindGroupLayoutBuilder, GraphicsState}; + #[derive(Debug)] pub struct Opacity { data: AtomicCell<[f32; 4]>, // length has to be a multiple of 4 @@ -25,7 +27,7 @@ pub struct Opacity { } impl Opacity { - pub fn new(graphics_state: &crate::GraphicsState) -> Self { + pub fn new(graphics_state: &GraphicsState) -> Self { let opacity = [1.; 4]; let uniform = (!graphics_state.push_constants_supported()).then(|| { @@ -71,8 +73,8 @@ impl Opacity { } pub fn add_to_bind_group_layout( - layout_builder: &mut crate::BindGroupLayoutBuilder, -) -> &mut crate::BindGroupLayoutBuilder { + layout_builder: &mut BindGroupLayoutBuilder, +) -> &mut BindGroupLayoutBuilder { layout_builder.append( wgpu::ShaderStages::VERTEX_FRAGMENT, wgpu::BindingType::Buffer { diff --git a/crates/graphics/src/tiles/shader.rs b/crates/graphics/src/tiles/shader.rs index 00b33b34..19aa45bf 100644 --- a/crates/graphics/src/tiles/shader.rs +++ b/crates/graphics/src/tiles/shader.rs @@ -16,11 +16,11 @@ // along with Luminol. If not, see . use super::instance::Instances; -use crate::vertex::Vertex; +use crate::{vertex::Vertex, BindGroupLayouts}; pub fn create_render_pipeline( render_state: &egui_wgpu::RenderState, - bind_group_layouts: &crate::BindGroupLayouts, + bind_group_layouts: &BindGroupLayouts, ) -> wgpu::RenderPipeline { let push_constants_supported = crate::push_constants_supported(render_state); diff --git a/crates/graphics/src/viewport.rs b/crates/graphics/src/viewport.rs index 466a466d..1beffe36 100644 --- a/crates/graphics/src/viewport.rs +++ b/crates/graphics/src/viewport.rs @@ -18,6 +18,8 @@ use crossbeam::atomic::AtomicCell; use wgpu::util::DeviceExt; +use crate::{BindGroupLayoutBuilder, GraphicsState}; + #[derive(Debug)] pub struct Viewport { data: AtomicCell, @@ -25,14 +27,14 @@ pub struct Viewport { } impl Viewport { - pub fn new(graphics_state: &crate::GraphicsState, width: f32, height: f32) -> Self { + pub fn new(graphics_state: &GraphicsState, width: f32, height: f32) -> Self { Self::new_proj( graphics_state, glam::Mat4::orthographic_rh(0.0, width, height, 0.0, -1.0, 1.0), ) } - pub fn new_proj(graphics_state: &crate::GraphicsState, proj: glam::Mat4) -> Self { + pub fn new_proj(graphics_state: &GraphicsState, proj: glam::Mat4) -> Self { let uniform = (!graphics_state.push_constants_supported()).then(|| { graphics_state.render_state.device.create_buffer_init( &wgpu::util::BufferInitDescriptor { @@ -82,8 +84,8 @@ impl Viewport { } pub fn add_to_bind_group_layout( - layout_builder: &mut crate::BindGroupLayoutBuilder, -) -> &mut crate::BindGroupLayoutBuilder { + layout_builder: &mut BindGroupLayoutBuilder, +) -> &mut BindGroupLayoutBuilder { layout_builder.append( wgpu::ShaderStages::VERTEX_FRAGMENT, wgpu::BindingType::Buffer {