Skip to content

Commit

Permalink
refactor(graphics): ♻️ remove pervasive passing of push constant support
Browse files Browse the repository at this point in the history
  • Loading branch information
melody-rs committed Nov 30, 2023
1 parent e003788 commit 479e14c
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 93 deletions.
3 changes: 0 additions & 3 deletions crates/components/src/map_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,12 @@ impl MapView {
update_state.filesystem,
e,
&atlas,
update_state.graphics.push_constants_supported(),
);
let preview_sprite = luminol_graphics::Event::new(
&update_state.graphics,
update_state.filesystem,
e,
&atlas,
update_state.graphics.push_constants_supported(),
);
let Ok(sprite) = sprite else {
return Err(sprite.unwrap_err());
Expand All @@ -127,7 +125,6 @@ impl MapView {
&map,
tileset,
&passages,
update_state.graphics.push_constants_supported(),
)?;

Ok(Self {
Expand Down
13 changes: 2 additions & 11 deletions crates/components/src/tilepicker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,15 @@ impl Tilepicker {

let viewport = luminol_graphics::viewport::Viewport::new(
&update_state.graphics,
glam::Mat4::orthographic_rh(
0.0,
256.,
atlas.tileset_height as f32 + 32.,
0.0,
-1.0,
1.0,
),
update_state.graphics.push_constants_supported(),
256.,
atlas.tileset_height as f32 + 32.,
);

let tiles = luminol_graphics::tiles::Tiles::new(
&update_state.graphics,
&viewport,
atlas,
&tilepicker_data,
update_state.graphics.push_constants_supported(),
);

let mut passages =
Expand All @@ -177,7 +169,6 @@ impl Tilepicker {
&update_state.graphics,
&viewport,
&passages,
update_state.graphics.push_constants_supported(),
);

Ok(Self {
Expand Down
7 changes: 2 additions & 5 deletions crates/graphics/src/collision/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ mod vertex;
pub struct Collision {
pub instances: Instances,
pub bind_group: wgpu::BindGroup,
pub use_push_constants: bool,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -149,12 +148,11 @@ impl Collision {
graphics_state: &crate::GraphicsState,
viewport: &Viewport,
passages: &luminol_data::Table2,
use_push_constants: bool,
) -> Self {
let instances = Instances::new(&graphics_state.render_state, passages);

let mut bind_group_builder = BindGroupBuilder::new();
if use_push_constants {
if graphics_state.push_constants_supported() {
bind_group_builder.append_buffer(viewport.as_buffer().unwrap());
}
let bind_group = bind_group_builder.build(
Expand All @@ -166,7 +164,6 @@ impl Collision {
Self {
instances,
bind_group,
use_push_constants,
}
}

Expand All @@ -193,7 +190,7 @@ impl Collision {

render_pass.push_debug_group("tilemap collision renderer");
render_pass.set_pipeline(&graphics_state.pipelines.collision);
if self.use_push_constants {
if graphics_state.push_constants_supported() {
render_pass.set_push_constants(
wgpu::ShaderStages::VERTEX,
0,
Expand Down
16 changes: 3 additions & 13 deletions crates/graphics/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ impl Event {
filesystem: &impl luminol_filesystem::FileSystem,
event: &luminol_data::rpg::Event,
atlas: &crate::tiles::Atlas,
use_push_constants: bool,
) -> anyhow::Result<Option<Self>> {
let Some(page) = event.pages.first() else {
anyhow::bail!("event does not have first page");
Expand All @@ -86,11 +85,7 @@ impl Event {
// Why does this have to be + 1?
let quad = atlas.calc_quad((id + 1) as i16);

let viewport = crate::viewport::Viewport::new(
graphics_state,
glam::Mat4::orthographic_rh(0.0, 32., 32., 0., -1., 1.),
use_push_constants,
);
let viewport = Viewport::new(graphics_state, 32., 32.);

(quad, viewport, egui::vec2(32., 32.))
} else {
Expand All @@ -114,24 +109,19 @@ impl Event {
);
let quad = crate::quad::Quad::new(pos, tex_coords, 0.0);

let viewport = crate::viewport::Viewport::new(
graphics_state,
glam::Mat4::orthographic_rh(0.0, cw, ch, 0., -1., 1.),
use_push_constants,
);
let viewport = Viewport::new(graphics_state, cw, ch);

(quad, viewport, egui::vec2(cw, ch))
};

let sprite = crate::sprite::Sprite::new(
let sprite = Sprite::new(
graphics_state,
&viewport,
quads,
texture,
page.graphic.blend_type,
page.graphic.character_hue,
page.graphic.opacity,
use_push_constants,
);

Ok(Some(Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl GraphicsState {
let nearest_sampler = render_state
.device
.create_sampler(&wgpu::SamplerDescriptor {
label: Some("luminol tileset atlas sampler"),
label: Some("luminol nearest texture sampler"),
mag_filter: wgpu::FilterMode::Nearest,
min_filter: wgpu::FilterMode::Nearest,
..Default::default()
Expand Down
29 changes: 4 additions & 25 deletions crates/graphics/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,38 +123,19 @@ impl Map {
map: &luminol_data::rpg::Map,
tileset: &luminol_data::rpg::Tileset,
passages: &luminol_data::Table2,
use_push_constants: bool,
) -> anyhow::Result<Self> {
let atlas = graphics_state
.atlas_cache
.load_atlas(graphics_state, filesystem, tileset)?;

let viewport = crate::viewport::Viewport::new(
graphics_state,
glam::Mat4::orthographic_rh(
0.0,
map.width as f32 * 32.,
map.height as f32 * 32.,
0.0,
-1.0,
1.0,
),
use_push_constants,
map.width as f32 * 32.,
map.height as f32 * 32.,
);

let tiles = Tiles::new(
graphics_state,
&viewport,
atlas,
&map.data,
use_push_constants,
);
let collision = crate::collision::Collision::new(
graphics_state,
&viewport,
passages,
use_push_constants,
);
let tiles = Tiles::new(graphics_state, &viewport, atlas, &map.data);
let collision = Collision::new(graphics_state, &viewport, passages);

let panorama = if let Some(ref panorama_name) = tileset.panorama_name {
Some(Plane::new(
Expand All @@ -171,7 +152,6 @@ impl Map {
255,
map.width,
map.height,
use_push_constants,
))
} else {
None
Expand All @@ -191,7 +171,6 @@ impl Map {
tileset.fog_opacity,
map.width,
map.height,
use_push_constants,
))
} else {
None
Expand Down
2 changes: 0 additions & 2 deletions crates/graphics/src/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ impl Plane {
opacity: i32,
map_width: usize,
map_height: usize,
use_push_constants: bool,
) -> Self {
let zoom = zoom as f32 / 100.;
let map_width = map_width as f32 * 32.;
Expand All @@ -61,7 +60,6 @@ impl Plane {
blend_mode,
hue,
opacity,
use_push_constants,
);

Self { sprite }
Expand Down
9 changes: 2 additions & 7 deletions crates/graphics/src/sprite/graphic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ struct Data {
}

impl Graphic {
pub fn new(
graphics_state: &crate::GraphicsState,
hue: i32,
opacity: i32,
use_push_constants: bool,
) -> Self {
pub fn new(graphics_state: &crate::GraphicsState, hue: i32, opacity: i32) -> Self {
let hue = (hue % 360) as f32 / 360.0;
let opacity = opacity as f32 / 255.;
let data = Data {
Expand All @@ -49,7 +44,7 @@ impl Graphic {
_padding: 0,
};

let uniform = (!use_push_constants).then(|| {
let uniform = (!graphics_state.push_constants_supported()).then(|| {
graphics_state.render_state.device.create_buffer_init(
&wgpu::util::BufferInitDescriptor {
label: Some("tilemap sprite graphic buffer"),
Expand Down
7 changes: 2 additions & 5 deletions crates/graphics/src/sprite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub struct Sprite {
pub graphic: graphic::Graphic,
pub vertices: vertices::Vertices,
pub blend_mode: luminol_data::BlendMode,
pub use_push_constants: bool,

pub bind_group: wgpu::BindGroup,
}
Expand All @@ -46,11 +45,10 @@ impl Sprite {
blend_mode: luminol_data::BlendMode,
hue: i32,
opacity: i32,
use_push_constants: bool,
) -> Self {
let vertices =
vertices::Vertices::from_quads(&graphics_state.render_state, &[quad], texture.size());
let graphic = graphic::Graphic::new(graphics_state, hue, opacity, use_push_constants);
let graphic = graphic::Graphic::new(graphics_state, hue, opacity);

let mut bind_group_builder = BindGroupBuilder::new();
bind_group_builder
Expand All @@ -72,7 +70,6 @@ impl Sprite {
graphic,
vertices,
blend_mode,
use_push_constants,
bind_group,
}
}
Expand All @@ -99,7 +96,7 @@ impl Sprite {
render_pass.set_pipeline(&graphics_state.pipelines.sprites[&self.blend_mode]);
render_pass.set_bind_group(0, &self.bind_group, &[]);

if self.use_push_constants {
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::FRAGMENT,
Expand Down
8 changes: 2 additions & 6 deletions crates/graphics/src/tiles/autotiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ struct Data {
}

impl Autotiles {
pub fn new(
graphics_state: &crate::GraphicsState,
atlas: &super::Atlas,
use_push_constants: bool,
) -> Self {
pub fn new(graphics_state: &crate::GraphicsState, atlas: &super::Atlas) -> Self {
let autotiles = Data {
autotile_frames: atlas.autotile_frames,
autotile_region_width: atlas.autotile_width,
Expand All @@ -48,7 +44,7 @@ impl Autotiles {
_end_padding: 0,
};

let uniform = (!use_push_constants).then(|| {
let uniform = (!graphics_state.push_constants_supported()).then(|| {
graphics_state.render_state.device.create_buffer_init(
&wgpu::util::BufferInitDescriptor {
label: Some("tilemap autotile buffer"),
Expand Down
11 changes: 4 additions & 7 deletions crates/graphics/src/tiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub struct Tiles {
pub atlas: Atlas,
pub instances: Instances,
pub opacity: Opacity,
pub use_push_constants: bool,

pub bind_group: wgpu::BindGroup,
}
Expand All @@ -50,15 +49,14 @@ impl Tiles {
viewport: &Viewport,
atlas: Atlas,
tiles: &luminol_data::Table3,
use_push_constants: bool,
) -> Self {
let autotiles = Autotiles::new(graphics_state, &atlas, use_push_constants);
let autotiles = Autotiles::new(graphics_state, &atlas);
let instances = Instances::new(
&graphics_state.render_state,
tiles,
atlas.atlas_texture.size(),
);
let opacity = Opacity::new(graphics_state, use_push_constants);
let opacity = Opacity::new(graphics_state);

let mut bind_group_builder = BindGroupBuilder::new();
bind_group_builder
Expand All @@ -81,7 +79,6 @@ impl Tiles {
atlas,
instances,
opacity,
use_push_constants,

bind_group,
}
Expand Down Expand Up @@ -115,7 +112,7 @@ impl Tiles {
render_pass.set_pipeline(&graphics_state.pipelines.tiles);
render_pass.set_bind_group(0, &self.bind_group, &[]);

if self.use_push_constants {
if graphics_state.push_constants_supported() {
render_pass.set_push_constants(
wgpu::ShaderStages::VERTEX,
0,
Expand All @@ -135,7 +132,7 @@ impl Tiles {
if enabled {
self.opacity
.set_opacity(&graphics_state.render_state, layer, opacity);
if self.use_push_constants {
if graphics_state.push_constants_supported() {
render_pass.set_push_constants(
wgpu::ShaderStages::FRAGMENT,
64 + 48,
Expand Down
4 changes: 2 additions & 2 deletions crates/graphics/src/tiles/opacity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub struct Opacity {
}

impl Opacity {
pub fn new(graphics_state: &crate::GraphicsState, use_push_constants: bool) -> Self {
pub fn new(graphics_state: &crate::GraphicsState) -> Self {
let opacity = [1.; 4];

let uniform = (!use_push_constants).then(|| {
let uniform = (!graphics_state.push_constants_supported()).then(|| {
graphics_state.render_state.device.create_buffer_init(
&wgpu::util::BufferInitDescriptor {
label: Some("tilemap opacity buffer"),
Expand Down
Loading

0 comments on commit 479e14c

Please sign in to comment.