From 8b09bb40dce15c991453ab5a56e4392d7f8b8e36 Mon Sep 17 00:00:00 2001 From: Speak2Erase Date: Thu, 18 Apr 2024 08:43:26 -0700 Subject: [PATCH] Reorganize graphics crate --- crates/components/src/map_view.rs | 2 +- crates/components/src/tilepicker.rs | 21 ++-- crates/graphics/src/data/mod.rs | 32 ++++++ crates/graphics/src/{ => data}/quad.rs | 0 crates/graphics/src/{ => data}/vertex.rs | 0 crates/graphics/src/{ => data}/viewport.rs | 26 ++--- crates/graphics/src/event.rs | 2 +- crates/graphics/src/lib.rs | 100 ++++-------------- .../src/{atlas_loader.rs => loaders/atlas.rs} | 2 +- crates/graphics/src/loaders/mod.rs | 26 +++++ .../{texture_loader.rs => loaders/texture.rs} | 0 crates/graphics/src/map.rs | 6 +- crates/graphics/src/plane.rs | 2 +- .../{ => primitives}/collision/instance.rs | 0 .../src/{ => primitives}/collision/mod.rs | 7 +- .../src/{ => primitives}/collision/shader.rs | 3 +- .../src/{ => primitives}/collision/vertex.rs | 0 .../src/{ => primitives}/grid/display.rs | 0 .../src/{ => primitives}/grid/instance.rs | 0 .../graphics/src/{ => primitives}/grid/mod.rs | 7 +- .../src/{ => primitives}/grid/shader.rs | 3 +- .../src/{ => primitives}/grid/vertex.rs | 0 crates/graphics/src/primitives/mod.rs | 89 ++++++++++++++++ .../{ => primitives}/shaders/collision.wgsl | 0 .../src/{ => primitives}/shaders/gamma.wgsl | 0 .../src/{ => primitives}/shaders/grid.wgsl | 0 .../src/{ => primitives}/shaders/hue.wgsl | 0 .../src/{ => primitives}/shaders/sprite.wgsl | 0 .../src/{ => primitives}/shaders/tilemap.wgsl | 0 .../src/{ => primitives}/sprite/graphic.rs | 0 .../src/{ => primitives}/sprite/mod.rs | 8 +- .../src/{ => primitives}/sprite/shader.rs | 2 +- .../src/{ => primitives}/sprite/vertices.rs | 2 +- .../src/{ => primitives}/tiles/atlas.rs | 2 +- .../{ => primitives}/tiles/autotile_ids.rs | 0 .../src/{ => primitives}/tiles/autotiles.rs | 0 .../src/{ => primitives}/tiles/display.rs | 0 .../src/{ => primitives}/tiles/instance.rs | 2 +- .../src/{ => primitives}/tiles/mod.rs | 7 +- .../src/{ => primitives}/tiles/shader.rs | 2 +- crates/ui/src/tabs/map/mod.rs | 4 +- 41 files changed, 213 insertions(+), 144 deletions(-) create mode 100644 crates/graphics/src/data/mod.rs rename crates/graphics/src/{ => data}/quad.rs (100%) rename crates/graphics/src/{ => data}/vertex.rs (100%) rename crates/graphics/src/{ => data}/viewport.rs (85%) rename crates/graphics/src/{atlas_loader.rs => loaders/atlas.rs} (97%) create mode 100644 crates/graphics/src/loaders/mod.rs rename crates/graphics/src/{texture_loader.rs => loaders/texture.rs} (100%) rename crates/graphics/src/{ => primitives}/collision/instance.rs (100%) rename crates/graphics/src/{ => primitives}/collision/mod.rs (97%) rename crates/graphics/src/{ => primitives}/collision/shader.rs (97%) rename crates/graphics/src/{ => primitives}/collision/vertex.rs (100%) rename crates/graphics/src/{ => primitives}/grid/display.rs (100%) rename crates/graphics/src/{ => primitives}/grid/instance.rs (100%) rename crates/graphics/src/{ => primitives}/grid/mod.rs (94%) rename crates/graphics/src/{ => primitives}/grid/shader.rs (97%) rename crates/graphics/src/{ => primitives}/grid/vertex.rs (100%) create mode 100644 crates/graphics/src/primitives/mod.rs rename crates/graphics/src/{ => primitives}/shaders/collision.wgsl (100%) rename crates/graphics/src/{ => primitives}/shaders/gamma.wgsl (100%) rename crates/graphics/src/{ => primitives}/shaders/grid.wgsl (100%) rename crates/graphics/src/{ => primitives}/shaders/hue.wgsl (100%) rename crates/graphics/src/{ => primitives}/shaders/sprite.wgsl (100%) rename crates/graphics/src/{ => primitives}/shaders/tilemap.wgsl (100%) rename crates/graphics/src/{ => primitives}/sprite/graphic.rs (100%) rename crates/graphics/src/{ => primitives}/sprite/mod.rs (95%) rename crates/graphics/src/{ => primitives}/sprite/shader.rs (98%) rename crates/graphics/src/{ => primitives}/sprite/vertices.rs (98%) rename crates/graphics/src/{ => primitives}/tiles/atlas.rs (99%) rename crates/graphics/src/{ => primitives}/tiles/autotile_ids.rs (100%) rename crates/graphics/src/{ => primitives}/tiles/autotiles.rs (100%) rename crates/graphics/src/{ => primitives}/tiles/display.rs (100%) rename crates/graphics/src/{ => primitives}/tiles/instance.rs (99%) rename crates/graphics/src/{ => primitives}/tiles/mod.rs (96%) rename crates/graphics/src/{ => primitives}/tiles/shader.rs (98%) diff --git a/crates/components/src/map_view.rs b/crates/components/src/map_view.rs index 392fd652..5b7ef58b 100644 --- a/crates/components/src/map_view.rs +++ b/crates/components/src/map_view.rs @@ -78,7 +78,7 @@ impl MapView { let tileset = &tilesets.data[map.tileset_id]; let mut passages = luminol_data::Table2::new(map.data.xsize(), map.data.ysize()); - luminol_graphics::collision::calculate_passages( + luminol_graphics::primitives::collision::calculate_passages( &tileset.passages, &tileset.priorities, &map.data, diff --git a/crates/components/src/tilepicker.rs b/crates/components/src/tilepicker.rs index 5ade8961..937df0a4 100644 --- a/crates/components/src/tilepicker.rs +++ b/crates/components/src/tilepicker.rs @@ -32,14 +32,14 @@ pub struct Tilepicker { drag_origin: Option, resources: Arc, - viewport: Arc, + viewport: Arc, ani_time: Option, } struct Resources { - tiles: luminol_graphics::tiles::Tiles, - collision: luminol_graphics::collision::Collision, - grid: luminol_graphics::grid::Grid, + tiles: luminol_graphics::Tiles, + collision: luminol_graphics::Collision, + grid: luminol_graphics::Grid, } // wgpu types are not Send + Sync on webassembly, so we use fragile to make sure we never access any wgpu resources across thread boundaries @@ -130,20 +130,20 @@ impl Tilepicker { tilepicker_data, ); - let viewport = Arc::new(luminol_graphics::viewport::Viewport::new( + let viewport = Arc::new(luminol_graphics::Viewport::new( &update_state.graphics, 256., atlas.tileset_height as f32 + 32., )); - let tiles = luminol_graphics::tiles::Tiles::new( + let tiles = luminol_graphics::Tiles::new( &update_state.graphics, viewport.clone(), atlas, &tilepicker_data, ); - let grid = luminol_graphics::grid::Grid::new( + let grid = luminol_graphics::Grid::new( &update_state.graphics, viewport.clone(), tilepicker_data.xsize() as u32, @@ -166,11 +166,8 @@ impl Tilepicker { (passages.len().saturating_sub(8)).min(tileset.passages.len().saturating_sub(384)); passages.as_mut_slice()[8..8 + length] .copy_from_slice(&tileset.passages.as_slice()[384..384 + length]); - let collision = luminol_graphics::collision::Collision::new( - &update_state.graphics, - viewport.clone(), - &passages, - ); + let collision = + luminol_graphics::Collision::new(&update_state.graphics, viewport.clone(), &passages); Ok(Self { resources: Arc::new(Resources { diff --git a/crates/graphics/src/data/mod.rs b/crates/graphics/src/data/mod.rs new file mode 100644 index 00000000..4e8c829e --- /dev/null +++ b/crates/graphics/src/data/mod.rs @@ -0,0 +1,32 @@ +// Copyright (C) 2024 Lily Lyons +// +// This file is part of Luminol. +// +// Luminol is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Luminol is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Luminol. If not, see . +// +// Additional permission under GNU GPL version 3 section 7 +// +// If you modify this Program, or any covered work, by linking or combining +// it with Steamworks API by Valve Corporation, containing parts covered by +// terms of the Steamworks API by Valve Corporation, the licensors of this +// Program grant you additional permission to convey the resulting work. + +mod quad; +pub use quad::Quad; + +mod vertex; +pub use vertex::Vertex; + +mod viewport; +pub use viewport::Viewport; diff --git a/crates/graphics/src/quad.rs b/crates/graphics/src/data/quad.rs similarity index 100% rename from crates/graphics/src/quad.rs rename to crates/graphics/src/data/quad.rs diff --git a/crates/graphics/src/vertex.rs b/crates/graphics/src/data/vertex.rs similarity index 100% rename from crates/graphics/src/vertex.rs rename to crates/graphics/src/data/vertex.rs diff --git a/crates/graphics/src/viewport.rs b/crates/graphics/src/data/viewport.rs similarity index 85% rename from crates/graphics/src/viewport.rs rename to crates/graphics/src/data/viewport.rs index f056ccda..91aa94b8 100644 --- a/crates/graphics/src/viewport.rs +++ b/crates/graphics/src/data/viewport.rs @@ -82,18 +82,18 @@ impl Viewport { .queue .write_buffer(self.as_buffer(), 0, bytemuck::bytes_of(&self.data.load())); } -} -pub fn add_to_bind_group_layout( - layout_builder: &mut BindGroupLayoutBuilder, -) -> &mut BindGroupLayoutBuilder { - layout_builder.append( - wgpu::ShaderStages::VERTEX_FRAGMENT, - wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, - has_dynamic_offset: false, - min_binding_size: None, - }, - None, - ) + pub fn add_to_bind_group_layout( + layout_builder: &mut BindGroupLayoutBuilder, + ) -> &mut BindGroupLayoutBuilder { + layout_builder.append( + wgpu::ShaderStages::VERTEX_FRAGMENT, + wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + None, + ) + } } diff --git a/crates/graphics/src/event.rs b/crates/graphics/src/event.rs index bd1d70ca..cd49ea73 100644 --- a/crates/graphics/src/event.rs +++ b/crates/graphics/src/event.rs @@ -22,7 +22,7 @@ use wgpu::util::DeviceExt; use std::sync::Arc; -use crate::{quad::Quad, sprite::Sprite, tiles::Atlas, viewport::Viewport, GraphicsState}; +use crate::{Atlas, GraphicsState, Quad, Sprite, Viewport}; pub struct Event { sprite: Sprite, diff --git a/crates/graphics/src/lib.rs b/crates/graphics/src/lib.rs index cd03418a..90bbba08 100644 --- a/crates/graphics/src/lib.rs +++ b/crates/graphics/src/lib.rs @@ -19,64 +19,47 @@ pub mod binding_helpers; pub use binding_helpers::{BindGroupBuilder, BindGroupLayoutBuilder}; -pub mod collision; -pub mod grid; -pub mod quad; -pub mod sprite; -pub mod tiles; -pub mod vertex; -pub mod viewport; +pub mod loaders; +pub use loaders::texture::Texture; + +// Building blocks that make up more complex parts (i.e. the map view, or events) +pub mod primitives; +pub use primitives::{ + collision::Collision, grid::Grid, sprite::Sprite, tiles::Atlas, tiles::Tiles, +}; + +pub mod data; +pub use data::*; pub mod event; pub mod map; pub mod plane; -pub mod atlas_loader; - -pub mod texture_loader; - pub use event::Event; pub use map::MapView; pub use plane::Plane; -pub use texture_loader::Texture; - pub struct GraphicsState { - pub texture_loader: texture_loader::Loader, - pub atlas_loader: atlas_loader::Loader, + pub texture_loader: loaders::texture::Loader, + pub atlas_loader: loaders::atlas::Loader, pub render_state: luminol_egui_wgpu::RenderState, pub nearest_sampler: wgpu::Sampler, - pipelines: Pipelines, - bind_group_layouts: BindGroupLayouts, + pipelines: primitives::Pipelines, + bind_group_layouts: primitives::BindGroupLayouts, texture_error_tx: crossbeam::channel::Sender, texture_error_rx: crossbeam::channel::Receiver, } -pub struct BindGroupLayouts { - sprite: wgpu::BindGroupLayout, - tiles: wgpu::BindGroupLayout, - collision: wgpu::BindGroupLayout, - grid: wgpu::BindGroupLayout, -} - -pub struct Pipelines { - sprites: std::collections::HashMap, - tiles: wgpu::RenderPipeline, - collision: wgpu::RenderPipeline, - grid: wgpu::RenderPipeline, -} - impl GraphicsState { pub fn new(render_state: luminol_egui_wgpu::RenderState) -> Self { - let bind_group_layouts = initialize_bind_group_layouts(&render_state); - - let pipelines = initialize_render_pipelines(&render_state, &bind_group_layouts); + let bind_group_layouts = primitives::BindGroupLayouts::new(&render_state); + let pipelines = primitives::Pipelines::new(&render_state, &bind_group_layouts); - let texture_loader = texture_loader::Loader::new(render_state.clone()); - let atlas_cache = atlas_loader::Loader::default(); + let texture_loader = loaders::texture::Loader::new(render_state.clone()); + let atlas_cache = loaders::atlas::Loader::default(); let nearest_sampler = render_state .device @@ -122,48 +105,3 @@ impl GraphicsState { .to_rgba8() } } - -fn initialize_bind_group_layouts( - render_state: &luminol_egui_wgpu::RenderState, -) -> BindGroupLayouts { - BindGroupLayouts { - sprite: sprite::create_bind_group_layout(render_state), - tiles: tiles::create_bind_group_layout(render_state), - collision: collision::create_bind_group_layout(render_state), - grid: grid::create_bind_group_layout(render_state), - } -} - -macro_rules! create_pipelines { - ( - $render_state:ident, $bind_group_layouts:ident, - $($name:ident: $fun:path),* - ) => {{ - let mut composer = naga_oil::compose::Composer::default(); - $( - let $name = match $fun(&mut composer, $render_state, $bind_group_layouts) { - Ok(p) => p, - Err(err) => { - let err = err.emit_to_string(&composer); - panic!("Error creating {} render pipeline:\n{err}", stringify!($name)) - } - }; - )* - Pipelines { - $($name,)* - } - }}; -} - -fn initialize_render_pipelines( - render_state: &luminol_egui_wgpu::RenderState, - bind_group_layouts: &BindGroupLayouts, -) -> Pipelines { - create_pipelines! { - render_state, bind_group_layouts, - sprites: sprite::shader::create_sprite_shaders, - tiles: tiles::shader::create_render_pipeline, - collision: collision::shader::create_render_pipeline, - grid: grid::shader::create_render_pipeline - } -} diff --git a/crates/graphics/src/atlas_loader.rs b/crates/graphics/src/loaders/atlas.rs similarity index 97% rename from crates/graphics/src/atlas_loader.rs rename to crates/graphics/src/loaders/atlas.rs index da22da04..7507b1b6 100644 --- a/crates/graphics/src/atlas_loader.rs +++ b/crates/graphics/src/loaders/atlas.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, GraphicsState}; +use crate::{Atlas, GraphicsState}; #[derive(Default)] pub struct Loader { diff --git a/crates/graphics/src/loaders/mod.rs b/crates/graphics/src/loaders/mod.rs new file mode 100644 index 00000000..f959b64b --- /dev/null +++ b/crates/graphics/src/loaders/mod.rs @@ -0,0 +1,26 @@ +// Copyright (C) 2024 Lily Lyons +// +// This file is part of Luminol. +// +// Luminol is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Luminol is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Luminol. If not, see . +// +// Additional permission under GNU GPL version 3 section 7 +// +// If you modify this Program, or any covered work, by linking or combining +// it with Steamworks API by Valve Corporation, containing parts covered by +// terms of the Steamworks API by Valve Corporation, the licensors of this +// Program grant you additional permission to convey the resulting work. + +pub mod atlas; +pub mod texture; diff --git a/crates/graphics/src/texture_loader.rs b/crates/graphics/src/loaders/texture.rs similarity index 100% rename from crates/graphics/src/texture_loader.rs rename to crates/graphics/src/loaders/texture.rs diff --git a/crates/graphics/src/map.rs b/crates/graphics/src/map.rs index 91e46d9e..837a2f4c 100644 --- a/crates/graphics/src/map.rs +++ b/crates/graphics/src/map.rs @@ -23,9 +23,7 @@ use wgpu::util::DeviceExt; use std::sync::Arc; use std::time::Duration; -use crate::{ - collision::Collision, grid::Grid, tiles::Tiles, viewport::Viewport, Event, GraphicsState, Plane, -}; +use crate::{Collision, Event, GraphicsState, Grid, Plane, Tiles, Viewport}; pub struct MapView { pub tiles: Tiles, @@ -238,7 +236,7 @@ impl MapView { }) } - pub fn atlas(&self) -> &crate::tiles::Atlas { + pub fn atlas(&self) -> &crate::Atlas { &self.tiles.atlas } diff --git a/crates/graphics/src/plane.rs b/crates/graphics/src/plane.rs index d09de445..b90a638e 100644 --- a/crates/graphics/src/plane.rs +++ b/crates/graphics/src/plane.rs @@ -15,7 +15,7 @@ // You should have received a copy of the GNU General Public License // along with Luminol. If not, see . -use crate::{quad::Quad, sprite::Sprite, viewport::Viewport, GraphicsState, Texture}; +use crate::{GraphicsState, Quad, Sprite, Texture, Viewport}; use std::sync::Arc; pub struct Plane { diff --git a/crates/graphics/src/collision/instance.rs b/crates/graphics/src/primitives/collision/instance.rs similarity index 100% rename from crates/graphics/src/collision/instance.rs rename to crates/graphics/src/primitives/collision/instance.rs diff --git a/crates/graphics/src/collision/mod.rs b/crates/graphics/src/primitives/collision/mod.rs similarity index 97% rename from crates/graphics/src/collision/mod.rs rename to crates/graphics/src/primitives/collision/mod.rs index 9d0a8b30..484b9a21 100644 --- a/crates/graphics/src/collision/mod.rs +++ b/crates/graphics/src/primitives/collision/mod.rs @@ -17,10 +17,7 @@ use std::sync::Arc; -use crate::{ - viewport::{self, Viewport}, - BindGroupBuilder, BindGroupLayoutBuilder, GraphicsState, -}; +use crate::{BindGroupBuilder, BindGroupLayoutBuilder, GraphicsState, Viewport}; use instance::Instances; use itertools::Itertools; @@ -194,7 +191,7 @@ pub fn create_bind_group_layout( ) -> wgpu::BindGroupLayout { let mut builder = BindGroupLayoutBuilder::new(); - viewport::add_to_bind_group_layout(&mut builder); + Viewport::add_to_bind_group_layout(&mut builder); builder.build(&render_state.device, Some("collision bind group layout")) } diff --git a/crates/graphics/src/collision/shader.rs b/crates/graphics/src/primitives/collision/shader.rs similarity index 97% rename from crates/graphics/src/collision/shader.rs rename to crates/graphics/src/primitives/collision/shader.rs index 884e1373..5739f428 100644 --- a/crates/graphics/src/collision/shader.rs +++ b/crates/graphics/src/primitives/collision/shader.rs @@ -17,11 +17,12 @@ use super::instance::Instances; use super::Vertex; +use crate::primitives::BindGroupLayouts; pub fn create_render_pipeline( composer: &mut naga_oil::compose::Composer, render_state: &luminol_egui_wgpu::RenderState, - bind_group_layouts: &crate::BindGroupLayouts, + bind_group_layouts: &BindGroupLayouts, ) -> Result { let module = composer.make_naga_module(naga_oil::compose::NagaModuleDescriptor { source: include_str!("../shaders/collision.wgsl"), diff --git a/crates/graphics/src/collision/vertex.rs b/crates/graphics/src/primitives/collision/vertex.rs similarity index 100% rename from crates/graphics/src/collision/vertex.rs rename to crates/graphics/src/primitives/collision/vertex.rs diff --git a/crates/graphics/src/grid/display.rs b/crates/graphics/src/primitives/grid/display.rs similarity index 100% rename from crates/graphics/src/grid/display.rs rename to crates/graphics/src/primitives/grid/display.rs diff --git a/crates/graphics/src/grid/instance.rs b/crates/graphics/src/primitives/grid/instance.rs similarity index 100% rename from crates/graphics/src/grid/instance.rs rename to crates/graphics/src/primitives/grid/instance.rs diff --git a/crates/graphics/src/grid/mod.rs b/crates/graphics/src/primitives/grid/mod.rs similarity index 94% rename from crates/graphics/src/grid/mod.rs rename to crates/graphics/src/primitives/grid/mod.rs index c3b3b020..363771e0 100644 --- a/crates/graphics/src/grid/mod.rs +++ b/crates/graphics/src/primitives/grid/mod.rs @@ -17,10 +17,7 @@ use std::sync::Arc; -use crate::{ - viewport::{self, Viewport}, - BindGroupBuilder, BindGroupLayoutBuilder, GraphicsState, -}; +use crate::{BindGroupBuilder, BindGroupLayoutBuilder, GraphicsState, Viewport}; use display::Display; use instance::Instances; @@ -96,7 +93,7 @@ pub fn create_bind_group_layout( ) -> wgpu::BindGroupLayout { let mut builder = BindGroupLayoutBuilder::new(); - viewport::add_to_bind_group_layout(&mut builder); + Viewport::add_to_bind_group_layout(&mut builder); display::add_to_bind_group_layout(&mut builder); builder.build(&render_state.device, Some("grid bind group layout")) diff --git a/crates/graphics/src/grid/shader.rs b/crates/graphics/src/primitives/grid/shader.rs similarity index 97% rename from crates/graphics/src/grid/shader.rs rename to crates/graphics/src/primitives/grid/shader.rs index 1bc9ebc3..b2e181ba 100644 --- a/crates/graphics/src/grid/shader.rs +++ b/crates/graphics/src/primitives/grid/shader.rs @@ -16,11 +16,12 @@ // along with Luminol. If not, see . use super::Vertex; +use crate::primitives::BindGroupLayouts; pub fn create_render_pipeline( composer: &mut naga_oil::compose::Composer, render_state: &luminol_egui_wgpu::RenderState, - bind_group_layouts: &crate::BindGroupLayouts, + bind_group_layouts: &BindGroupLayouts, ) -> Result { let module = composer.make_naga_module(naga_oil::compose::NagaModuleDescriptor { source: include_str!("../shaders/grid.wgsl"), diff --git a/crates/graphics/src/grid/vertex.rs b/crates/graphics/src/primitives/grid/vertex.rs similarity index 100% rename from crates/graphics/src/grid/vertex.rs rename to crates/graphics/src/primitives/grid/vertex.rs diff --git a/crates/graphics/src/primitives/mod.rs b/crates/graphics/src/primitives/mod.rs new file mode 100644 index 00000000..48758c72 --- /dev/null +++ b/crates/graphics/src/primitives/mod.rs @@ -0,0 +1,89 @@ +// Copyright (C) 2024 Lily Lyons +// +// This file is part of Luminol. +// +// Luminol is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Luminol is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Luminol. If not, see . +// +// Additional permission under GNU GPL version 3 section 7 +// +// If you modify this Program, or any covered work, by linking or combining +// it with Steamworks API by Valve Corporation, containing parts covered by +// terms of the Steamworks API by Valve Corporation, the licensors of this +// Program grant you additional permission to convey the resulting work. + +pub mod collision; +pub mod grid; +pub mod sprite; +pub mod tiles; + +pub struct BindGroupLayouts { + sprite: wgpu::BindGroupLayout, + tiles: wgpu::BindGroupLayout, + collision: wgpu::BindGroupLayout, + grid: wgpu::BindGroupLayout, +} + +pub struct Pipelines { + sprites: std::collections::HashMap, + tiles: wgpu::RenderPipeline, + collision: wgpu::RenderPipeline, + grid: wgpu::RenderPipeline, +} + +impl BindGroupLayouts { + pub fn new(render_state: &luminol_egui_wgpu::RenderState) -> Self { + Self { + sprite: sprite::create_bind_group_layout(render_state), + tiles: tiles::create_bind_group_layout(render_state), + collision: collision::create_bind_group_layout(render_state), + grid: grid::create_bind_group_layout(render_state), + } + } +} + +macro_rules! create_pipelines { + ( + $render_state:ident, $bind_group_layouts:ident, + $($name:ident: $fun:path),* + ) => {{ + let mut composer = naga_oil::compose::Composer::default(); + $( + let $name = match $fun(&mut composer, $render_state, $bind_group_layouts) { + Ok(p) => p, + Err(err) => { + let err = err.emit_to_string(&composer); + panic!("Error creating {} render pipeline:\n{err}", stringify!($name)) + } + }; + )* + Pipelines { + $($name,)* + } + }}; +} + +impl Pipelines { + pub fn new( + render_state: &luminol_egui_wgpu::RenderState, + bind_group_layouts: &BindGroupLayouts, + ) -> Self { + create_pipelines! { + render_state, bind_group_layouts, + sprites: sprite::shader::create_sprite_shaders, + tiles: tiles::shader::create_render_pipeline, + collision: collision::shader::create_render_pipeline, + grid: grid::shader::create_render_pipeline + } + } +} diff --git a/crates/graphics/src/shaders/collision.wgsl b/crates/graphics/src/primitives/shaders/collision.wgsl similarity index 100% rename from crates/graphics/src/shaders/collision.wgsl rename to crates/graphics/src/primitives/shaders/collision.wgsl diff --git a/crates/graphics/src/shaders/gamma.wgsl b/crates/graphics/src/primitives/shaders/gamma.wgsl similarity index 100% rename from crates/graphics/src/shaders/gamma.wgsl rename to crates/graphics/src/primitives/shaders/gamma.wgsl diff --git a/crates/graphics/src/shaders/grid.wgsl b/crates/graphics/src/primitives/shaders/grid.wgsl similarity index 100% rename from crates/graphics/src/shaders/grid.wgsl rename to crates/graphics/src/primitives/shaders/grid.wgsl diff --git a/crates/graphics/src/shaders/hue.wgsl b/crates/graphics/src/primitives/shaders/hue.wgsl similarity index 100% rename from crates/graphics/src/shaders/hue.wgsl rename to crates/graphics/src/primitives/shaders/hue.wgsl diff --git a/crates/graphics/src/shaders/sprite.wgsl b/crates/graphics/src/primitives/shaders/sprite.wgsl similarity index 100% rename from crates/graphics/src/shaders/sprite.wgsl rename to crates/graphics/src/primitives/shaders/sprite.wgsl diff --git a/crates/graphics/src/shaders/tilemap.wgsl b/crates/graphics/src/primitives/shaders/tilemap.wgsl similarity index 100% rename from crates/graphics/src/shaders/tilemap.wgsl rename to crates/graphics/src/primitives/shaders/tilemap.wgsl diff --git a/crates/graphics/src/sprite/graphic.rs b/crates/graphics/src/primitives/sprite/graphic.rs similarity index 100% rename from crates/graphics/src/sprite/graphic.rs rename to crates/graphics/src/primitives/sprite/graphic.rs diff --git a/crates/graphics/src/sprite/mod.rs b/crates/graphics/src/primitives/sprite/mod.rs similarity index 95% rename from crates/graphics/src/sprite/mod.rs rename to crates/graphics/src/primitives/sprite/mod.rs index ca6f9836..b7ddb209 100644 --- a/crates/graphics/src/sprite/mod.rs +++ b/crates/graphics/src/primitives/sprite/mod.rs @@ -16,11 +16,7 @@ // along with Luminol. If not, see . use std::sync::Arc; -use crate::{ - quad::Quad, - viewport::{self, Viewport}, - BindGroupBuilder, BindGroupLayoutBuilder, GraphicsState, Texture, -}; +use crate::{BindGroupBuilder, BindGroupLayoutBuilder, GraphicsState, Quad, Texture, Viewport}; pub(crate) mod graphic; pub(crate) mod shader; @@ -114,7 +110,7 @@ pub fn create_bind_group_layout( None, ); - viewport::add_to_bind_group_layout(&mut builder); + Viewport::add_to_bind_group_layout(&mut builder); graphic::add_to_bind_group_layout(&mut builder); builder.build(&render_state.device, Some("sprite bind group layout")) diff --git a/crates/graphics/src/sprite/shader.rs b/crates/graphics/src/primitives/sprite/shader.rs similarity index 98% rename from crates/graphics/src/sprite/shader.rs rename to crates/graphics/src/primitives/sprite/shader.rs index b8422a1f..62b02c78 100644 --- a/crates/graphics/src/sprite/shader.rs +++ b/crates/graphics/src/primitives/sprite/shader.rs @@ -19,7 +19,7 @@ use std::collections::HashMap; use naga_oil::compose::ComposerError; -use crate::{vertex::Vertex, BindGroupLayouts}; +use crate::{primitives::BindGroupLayouts, Vertex}; fn create_shader( composer: &mut naga_oil::compose::Composer, diff --git a/crates/graphics/src/sprite/vertices.rs b/crates/graphics/src/primitives/sprite/vertices.rs similarity index 98% rename from crates/graphics/src/sprite/vertices.rs rename to crates/graphics/src/primitives/sprite/vertices.rs index dc430971..87ffc07f 100644 --- a/crates/graphics/src/sprite/vertices.rs +++ b/crates/graphics/src/primitives/sprite/vertices.rs @@ -15,7 +15,7 @@ // You should have received a copy of the GNU General Public License // along with Luminol. If not, see . -use crate::quad::Quad; +use crate::Quad; #[derive(Debug)] pub struct Vertices { diff --git a/crates/graphics/src/tiles/atlas.rs b/crates/graphics/src/primitives/tiles/atlas.rs similarity index 99% rename from crates/graphics/src/tiles/atlas.rs rename to crates/graphics/src/primitives/tiles/atlas.rs index fcaa2ce2..7a5cf8b3 100644 --- a/crates/graphics/src/tiles/atlas.rs +++ b/crates/graphics/src/primitives/tiles/atlas.rs @@ -21,7 +21,7 @@ use itertools::Itertools; use wgpu::util::DeviceExt; use super::autotile_ids::AUTOTILES; -use crate::{quad::Quad, GraphicsState, Texture}; +use crate::{GraphicsState, Quad, 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/autotile_ids.rs b/crates/graphics/src/primitives/tiles/autotile_ids.rs similarity index 100% rename from crates/graphics/src/tiles/autotile_ids.rs rename to crates/graphics/src/primitives/tiles/autotile_ids.rs diff --git a/crates/graphics/src/tiles/autotiles.rs b/crates/graphics/src/primitives/tiles/autotiles.rs similarity index 100% rename from crates/graphics/src/tiles/autotiles.rs rename to crates/graphics/src/primitives/tiles/autotiles.rs diff --git a/crates/graphics/src/tiles/display.rs b/crates/graphics/src/primitives/tiles/display.rs similarity index 100% rename from crates/graphics/src/tiles/display.rs rename to crates/graphics/src/primitives/tiles/display.rs diff --git a/crates/graphics/src/tiles/instance.rs b/crates/graphics/src/primitives/tiles/instance.rs similarity index 99% rename from crates/graphics/src/tiles/instance.rs rename to crates/graphics/src/primitives/tiles/instance.rs index cd3a9e9f..be21d433 100644 --- a/crates/graphics/src/tiles/instance.rs +++ b/crates/graphics/src/primitives/tiles/instance.rs @@ -18,7 +18,7 @@ use itertools::Itertools; use wgpu::util::DeviceExt; -use crate::quad::Quad; +use crate::Quad; #[derive(Debug)] pub struct Instances { diff --git a/crates/graphics/src/tiles/mod.rs b/crates/graphics/src/primitives/tiles/mod.rs similarity index 96% rename from crates/graphics/src/tiles/mod.rs rename to crates/graphics/src/primitives/tiles/mod.rs index af88ac90..100d94f9 100644 --- a/crates/graphics/src/tiles/mod.rs +++ b/crates/graphics/src/primitives/tiles/mod.rs @@ -17,10 +17,7 @@ use std::sync::Arc; -use crate::{ - viewport::{self, Viewport}, - BindGroupBuilder, BindGroupLayoutBuilder, GraphicsState, -}; +use crate::{BindGroupBuilder, BindGroupLayoutBuilder, GraphicsState, Viewport}; pub use atlas::Atlas; @@ -166,7 +163,7 @@ pub fn create_bind_group_layout( None, ); - viewport::add_to_bind_group_layout(&mut builder); + Viewport::add_to_bind_group_layout(&mut builder); autotiles::add_to_bind_group_layout(&mut builder); display::add_to_bind_group_layout(&mut builder); diff --git a/crates/graphics/src/tiles/shader.rs b/crates/graphics/src/primitives/tiles/shader.rs similarity index 98% rename from crates/graphics/src/tiles/shader.rs rename to crates/graphics/src/primitives/tiles/shader.rs index 009c3581..c94528cc 100644 --- a/crates/graphics/src/tiles/shader.rs +++ b/crates/graphics/src/primitives/tiles/shader.rs @@ -16,7 +16,7 @@ // along with Luminol. If not, see . use super::instance::Instances; -use crate::{vertex::Vertex, BindGroupLayouts}; +use crate::{primitives::BindGroupLayouts, Vertex}; pub fn create_render_pipeline( composer: &mut naga_oil::compose::Composer, diff --git a/crates/ui/src/tabs/map/mod.rs b/crates/ui/src/tabs/map/mod.rs index 2e0275ae..0172766a 100644 --- a/crates/ui/src/tabs/map/mod.rs +++ b/crates/ui/src/tabs/map/mod.rs @@ -124,7 +124,7 @@ impl Tab { let tileset = &tilesets.data[map.tileset_id]; let mut passages = luminol_data::Table2::new(map.data.xsize(), map.data.ysize()); - luminol_graphics::collision::calculate_passages( + luminol_graphics::primitives::collision::calculate_passages( &tileset.passages, &tileset.priorities, &map.data, @@ -621,7 +621,7 @@ impl luminol_core::Tab for Tab { } // Update the collision preview - luminol_graphics::collision::calculate_passages( + luminol_graphics::primitives::collision::calculate_passages( &tileset.passages, &tileset.priorities, &map.data,