refactor
This commit is contained in:
@@ -1,11 +1,19 @@
|
||||
pub mod billboard;
|
||||
mod bind_group;
|
||||
mod debug_overlay;
|
||||
mod global;
|
||||
mod pipeline;
|
||||
mod shadow;
|
||||
mod types;
|
||||
|
||||
pub mod billboard;
|
||||
pub mod snow;
|
||||
pub mod snow_light;
|
||||
|
||||
pub use billboard::{BillboardDrawCall, BillboardPipeline};
|
||||
pub use global::{
|
||||
aspect_ratio, init, init_snow_light_accumulation, render, set_selected_entity, set_snow_depth,
|
||||
set_terrain_data, update_spotlights, with_device, with_queue, with_surface_format,
|
||||
};
|
||||
pub use types::{DrawCall, Pipeline, Spotlight, SpotlightRaw, Uniforms, MAX_SPOTLIGHTS};
|
||||
|
||||
use crate::entity::EntityHandle;
|
||||
@@ -18,7 +26,6 @@ use pipeline::{
|
||||
create_debug_lines_pipeline, create_main_pipeline, create_snow_clipmap_pipeline,
|
||||
create_wireframe_pipeline,
|
||||
};
|
||||
use std::cell::RefCell;
|
||||
use std::num::NonZeroU64;
|
||||
|
||||
const MAX_DRAW_CALLS: usize = 64;
|
||||
@@ -71,7 +78,7 @@ pub struct Renderer
|
||||
dummy_snow_light_view: wgpu::TextureView,
|
||||
dummy_snow_light_sampler: wgpu::Sampler,
|
||||
|
||||
snow_light_accumulation: Option<crate::snow_light::SnowLightAccumulation>,
|
||||
snow_light_accumulation: Option<snow_light::SnowLightAccumulation>,
|
||||
snow_light_bound: bool,
|
||||
|
||||
pub selected_entity: Option<EntityHandle>,
|
||||
@@ -1037,12 +1044,8 @@ impl Renderer
|
||||
|
||||
pub fn init_snow_light_accumulation(&mut self, terrain_min: glam::Vec2, terrain_max: glam::Vec2)
|
||||
{
|
||||
let snow_light_accumulation = crate::snow_light::SnowLightAccumulation::new(
|
||||
&self.device,
|
||||
terrain_min,
|
||||
terrain_max,
|
||||
512,
|
||||
);
|
||||
let snow_light_accumulation =
|
||||
snow_light::SnowLightAccumulation::new(&self.device, terrain_min, terrain_max, 512);
|
||||
|
||||
self.snow_light_accumulation = Some(snow_light_accumulation);
|
||||
}
|
||||
@@ -1093,137 +1096,3 @@ impl Renderer
|
||||
self.config.width as f32 / self.config.height as f32
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static GLOBAL_RENDERER: RefCell<Option<Renderer>> = RefCell::new(None);
|
||||
}
|
||||
|
||||
pub fn init(renderer: Renderer)
|
||||
{
|
||||
GLOBAL_RENDERER.with(|r| *r.borrow_mut() = Some(renderer));
|
||||
}
|
||||
|
||||
pub fn with_device<F, R>(f: F) -> R
|
||||
where
|
||||
F: FnOnce(&wgpu::Device) -> R,
|
||||
{
|
||||
GLOBAL_RENDERER.with(|r| {
|
||||
let renderer = r.borrow();
|
||||
let renderer = renderer.as_ref().expect("Renderer not set");
|
||||
f(&renderer.device)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn with_queue<F, R>(f: F) -> R
|
||||
where
|
||||
F: FnOnce(&wgpu::Queue) -> R,
|
||||
{
|
||||
GLOBAL_RENDERER.with(|r| {
|
||||
let renderer = r.borrow();
|
||||
let renderer = renderer.as_ref().expect("Renderer not set");
|
||||
f(&renderer.queue)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set_terrain_data()
|
||||
{
|
||||
GLOBAL_RENDERER.with(|r| {
|
||||
let mut renderer = r.borrow_mut();
|
||||
let renderer = renderer.as_mut().expect("Renderer not set");
|
||||
renderer.set_terrain_data();
|
||||
});
|
||||
}
|
||||
|
||||
pub fn init_snow_light_accumulation(terrain_min: glam::Vec2, terrain_max: glam::Vec2)
|
||||
{
|
||||
GLOBAL_RENDERER.with(|r| {
|
||||
let mut renderer = r.borrow_mut();
|
||||
let renderer = renderer.as_mut().expect("Renderer not set");
|
||||
renderer.init_snow_light_accumulation(terrain_min, terrain_max);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn set_snow_depth(snow_depth_view: &wgpu::TextureView)
|
||||
{
|
||||
GLOBAL_RENDERER.with(|r| {
|
||||
let mut renderer = r.borrow_mut();
|
||||
let renderer = renderer.as_mut().expect("Renderer not set");
|
||||
renderer.set_snow_depth(snow_depth_view);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn aspect_ratio() -> f32
|
||||
{
|
||||
GLOBAL_RENDERER.with(|r| {
|
||||
let renderer = r.borrow();
|
||||
let renderer = renderer.as_ref().expect("Renderer not set");
|
||||
renderer.aspect_ratio()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn render(
|
||||
view: &glam::Mat4,
|
||||
projection: &glam::Mat4,
|
||||
camera_position: glam::Vec3,
|
||||
player_position: glam::Vec3,
|
||||
draw_calls: &[DrawCall],
|
||||
billboard_calls: &[BillboardDrawCall],
|
||||
time: f32,
|
||||
delta_time: f32,
|
||||
debug_mode: DebugMode,
|
||||
) -> wgpu::SurfaceTexture
|
||||
{
|
||||
GLOBAL_RENDERER.with(|r| {
|
||||
let mut renderer = r.borrow_mut();
|
||||
let renderer = renderer.as_mut().expect("Renderer not set");
|
||||
renderer.render(
|
||||
view,
|
||||
projection,
|
||||
camera_position,
|
||||
player_position,
|
||||
draw_calls,
|
||||
billboard_calls,
|
||||
time,
|
||||
delta_time,
|
||||
debug_mode,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn with_surface_format<F, R>(f: F) -> R
|
||||
where
|
||||
F: FnOnce(wgpu::TextureFormat) -> R,
|
||||
{
|
||||
GLOBAL_RENDERER.with(|r| {
|
||||
let renderer = r.borrow();
|
||||
let renderer = renderer.as_ref().expect("Renderer not set");
|
||||
f(renderer.config.format)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set_shadow_bias(bias: f32)
|
||||
{
|
||||
GLOBAL_RENDERER.with(|r| {
|
||||
let mut renderer = r.borrow_mut();
|
||||
let renderer = renderer.as_mut().expect("Renderer not set");
|
||||
renderer.shadow_bias = bias;
|
||||
});
|
||||
}
|
||||
|
||||
pub fn update_spotlights(spotlights: Vec<Spotlight>)
|
||||
{
|
||||
GLOBAL_RENDERER.with(|r| {
|
||||
let mut renderer = r.borrow_mut();
|
||||
let renderer = renderer.as_mut().expect("Renderer not set");
|
||||
renderer.spotlights = spotlights;
|
||||
});
|
||||
}
|
||||
|
||||
pub fn set_selected_entity(entity: Option<EntityHandle>)
|
||||
{
|
||||
GLOBAL_RENDERER.with(|r| {
|
||||
let mut renderer = r.borrow_mut();
|
||||
let renderer = renderer.as_mut().expect("Renderer not set");
|
||||
renderer.selected_entity = entity;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user