Skip to content

Commit

Permalink
Reorganize graphics crate
Browse files Browse the repository at this point in the history
  • Loading branch information
melody-rs committed Apr 18, 2024
1 parent bbe0a94 commit 8b09bb4
Show file tree
Hide file tree
Showing 41 changed files with 213 additions and 144 deletions.
2 changes: 1 addition & 1 deletion crates/components/src/map_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 9 additions & 12 deletions crates/components/src/tilepicker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ pub struct Tilepicker {
drag_origin: Option<egui::Pos2>,

resources: Arc<Resources>,
viewport: Arc<luminol_graphics::viewport::Viewport>,
viewport: Arc<luminol_graphics::Viewport>,
ani_time: Option<f64>,
}

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
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions crates/graphics/src/data/mod.rs
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
//
// 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;
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
}
2 changes: 1 addition & 1 deletion crates/graphics/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
100 changes: 19 additions & 81 deletions crates/graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<color_eyre::Report>,
texture_error_rx: crossbeam::channel::Receiver<color_eyre::Report>,
}

pub struct BindGroupLayouts {
sprite: wgpu::BindGroupLayout,
tiles: wgpu::BindGroupLayout,
collision: wgpu::BindGroupLayout,
grid: wgpu::BindGroupLayout,
}

pub struct Pipelines {
sprites: std::collections::HashMap<luminol_data::BlendMode, wgpu::RenderPipeline>,
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
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//
// You should have received a copy of the GNU General Public License
// along with Luminol. If not, see <http://www.gnu.org/licenses/>.
use crate::{tiles::Atlas, GraphicsState};
use crate::{Atlas, GraphicsState};

#[derive(Default)]
pub struct Loader {
Expand Down
26 changes: 26 additions & 0 deletions crates/graphics/src/loaders/mod.rs
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
//
// 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;
File renamed without changes.
6 changes: 2 additions & 4 deletions crates/graphics/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -238,7 +236,7 @@ impl MapView {
})
}

pub fn atlas(&self) -> &crate::tiles::Atlas {
pub fn atlas(&self) -> &crate::Atlas {
&self.tiles.atlas
}

Expand Down
2 changes: 1 addition & 1 deletion crates/graphics/src/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with Luminol. If not, see <http://www.gnu.org/licenses/>.

use crate::{quad::Quad, sprite::Sprite, viewport::Viewport, GraphicsState, Texture};
use crate::{GraphicsState, Quad, Sprite, Texture, Viewport};
use std::sync::Arc;

pub struct Plane {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<wgpu::RenderPipeline, naga_oil::compose::ComposerError> {
let module = composer.make_naga_module(naga_oil::compose::NagaModuleDescriptor {
source: include_str!("../shaders/collision.wgsl"),
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"))
Expand Down
Loading

0 comments on commit 8b09bb4

Please sign in to comment.