From 6427ac197c1d67a20b68210810354fbcf6a00584 Mon Sep 17 00:00:00 2001 From: Paris DOUADY Date: Fri, 29 Dec 2023 19:03:13 +0100 Subject: [PATCH] frustrum is managed by gfx now --- engine/src/drawables/terrain.rs | 22 +++++++--------- engine/src/gfx.rs | 2 +- engine_demo/src/helmet.rs | 7 ++--- engine_demo/src/main.rs | 26 +++---------------- engine_demo/src/spheres.rs | 7 ++--- engine_demo/src/terrain.rs | 9 ++++--- native_app/src/game_loop.rs | 3 +-- native_app/src/rendering/map_rendering/mod.rs | 7 +++-- .../src/rendering/map_rendering/terrain.rs | 6 ++--- .../src/rendering/map_rendering/trees.rs | 18 ++++--------- native_app/src/rendering/orbit_camera.rs | 4 +-- 11 files changed, 41 insertions(+), 70 deletions(-) diff --git a/engine/src/drawables/terrain.rs b/engine/src/drawables/terrain.rs index 6927d182..0a91750d 100644 --- a/engine/src/drawables/terrain.rs +++ b/engine/src/drawables/terrain.rs @@ -1,9 +1,5 @@ -use crate::{ - bg_layout_litmesh, pbuffer::PBuffer, CompiledModule, Drawable, FrameContext, GfxContext, - IndexType, PipelineBuilder, RenderParams, Texture, TextureBuilder, Uniform, TL, -}; -use geom::{vec2, vec3, Camera, InfiniteFrustrum, Intersect3, Matrix4, Vec2, AABB3}; use std::sync::Arc; + use wgpu::{ BindGroupDescriptor, BindGroupLayoutDescriptor, BufferUsages, CommandEncoder, CommandEncoderDescriptor, Extent3d, FilterMode, ImageCopyTexture, ImageDataLayout, IndexFormat, @@ -11,6 +7,13 @@ use wgpu::{ VertexAttribute, VertexBufferLayout, }; +use geom::{vec2, vec3, Camera, Intersect3, Matrix4, Vec2, AABB3}; + +use crate::{ + bg_layout_litmesh, pbuffer::PBuffer, CompiledModule, Drawable, FrameContext, GfxContext, + IndexType, PipelineBuilder, RenderParams, Texture, TextureBuilder, Uniform, TL, +}; + const LOD: usize = 5; const LOD_MIN_DIST_LOG2: f32 = 9.0; // 2^9 = 512, meaning until 1048m away, we use the highest lod const MAX_HEIGHT: f32 = 2008.0; @@ -203,12 +206,7 @@ impl TerrainRender, - ) { + pub fn draw_terrain(&mut self, cam: &Camera, fctx: &mut FrameContext<'_>) { profiling::scope!("terrain::draw_terrain"); let eye = cam.eye(); @@ -224,7 +222,7 @@ impl TerrainRender, pub(crate) projection: Uniform, - pub(crate) frustrum: InfiniteFrustrum, + pub frustrum: InfiniteFrustrum, pub(crate) sun_projection: [Uniform; N_CASCADES], pub render_params: Uniform, pub(crate) texture_cache_paths: FastMap>, diff --git a/engine_demo/src/helmet.rs b/engine_demo/src/helmet.rs index ead66cc5..b708a517 100644 --- a/engine_demo/src/helmet.rs +++ b/engine_demo/src/helmet.rs @@ -1,7 +1,8 @@ -use crate::DemoElement; use engine::meshload::load_mesh; use engine::{Context, FrameContext, InstancedMesh, InstancedMeshBuilder, MeshInstance}; -use geom::{vec3, Camera, InfiniteFrustrum, LinearColor, Vec3}; +use geom::{vec3, Camera, LinearColor, Vec3}; + +use crate::DemoElement; pub struct Helmet { mesh: Option, @@ -31,7 +32,7 @@ impl DemoElement for Helmet { fn update(&mut self, _ctx: &mut Context, _cam: &Camera) {} - fn render(&mut self, fc: &mut FrameContext, _cam: &Camera, _frustrum: &InfiniteFrustrum) { + fn render(&mut self, fc: &mut FrameContext, _cam: &Camera) { fc.draw(self.mesh.clone()); } } diff --git a/engine_demo/src/main.rs b/engine_demo/src/main.rs index b1471070..ebb1ef13 100644 --- a/engine_demo/src/main.rs +++ b/engine_demo/src/main.rs @@ -20,7 +20,7 @@ trait DemoElement { where Self: Sized; fn update(&mut self, ctx: &mut Context, cam: &Camera); - fn render(&mut self, fc: &mut FrameContext, cam: &Camera, frustrum: &InfiniteFrustrum); + fn render(&mut self, fc: &mut FrameContext, cam: &Camera); fn render_gui(&mut self, _ui: &mut egui::Ui) {} } @@ -31,10 +31,6 @@ struct State { camera: Camera, camera_speed: f32, - frustrum: InfiniteFrustrum, - last_cam: Camera, - - freeze_cam: bool, delta: f32, play_queue: Vec<&'static str>, @@ -84,9 +80,6 @@ impl engine::framework::State for State { delta: 0.0, play_queue: vec![], camera_speed: 100.0, - frustrum: InfiniteFrustrum::new([Plane::X; 5]), - last_cam: camera, - freeze_cam: false, ms_hist: History::new(128), gfx_settings, sun_angle: Degrees(0.0), @@ -115,10 +108,8 @@ impl engine::framework::State for State { params.sun_col = 4.0 * sun.z.max(0.0).sqrt().sqrt() * LinearColor::new(1.0, 0.95 + sun.z * 0.05, 0.95 + sun.z * 0.05, 1.0); - if !self.freeze_cam { - params.cam_pos = self.camera.eye(); - params.cam_dir = self.camera.dir(); - } + params.cam_pos = self.camera.eye(); + params.cam_dir = self.camera.dir(); params.sun = sun; params.viewport = Vec2::new(gfx.size.0 as f32, gfx.size.1 as f32); self.camera.dist = 300.0; @@ -146,19 +137,11 @@ impl engine::framework::State for State { } fn render(&mut self, fc: &mut FrameContext) { - if !self.freeze_cam { - self.frustrum = InfiniteFrustrum::from_reversez_invviewproj( - self.camera.eye(), - fc.gfx.render_params.value().inv_proj, - ); - self.last_cam = self.camera; - } - for (de, enabled) in &mut self.demo_elements { if !*enabled { continue; } - de.render(fc, &self.last_cam, &self.frustrum); + de.render(fc, &self.camera); } } @@ -193,7 +176,6 @@ impl engine::framework::State for State { )); ui.add(egui::Slider::new(&mut self.camera_speed, 1.0..=100.0).text("Camera speed")); - ui.checkbox(&mut self.freeze_cam, "Freeze camera"); ui.checkbox(&mut self.gfx_settings.fullscreen, "Fullscreen"); ui.checkbox(&mut self.gfx_settings.vsync, "VSync"); diff --git a/engine_demo/src/spheres.rs b/engine_demo/src/spheres.rs index 01087638..21c11552 100644 --- a/engine_demo/src/spheres.rs +++ b/engine_demo/src/spheres.rs @@ -1,10 +1,11 @@ -use crate::DemoElement; use engine::meshload::load_mesh; use engine::{ Context, FrameContext, InstancedMesh, InstancedMeshBuilder, Material, MeshInstance, MetallicRoughness, }; -use geom::{vec3, Camera, InfiniteFrustrum, LinearColor, Vec3}; +use geom::{vec3, Camera, LinearColor, Vec3}; + +use crate::DemoElement; pub struct Spheres { meshes: Vec, @@ -56,7 +57,7 @@ impl DemoElement for Spheres { fn update(&mut self, _ctx: &mut Context, _cam: &Camera) {} - fn render(&mut self, fc: &mut FrameContext, _cam: &Camera, _frustrum: &InfiniteFrustrum) { + fn render(&mut self, fc: &mut FrameContext, _cam: &Camera) { fc.draw(self.meshes.clone()); } } diff --git a/engine_demo/src/terrain.rs b/engine_demo/src/terrain.rs index 486af0f3..9e0fada2 100644 --- a/engine_demo/src/terrain.rs +++ b/engine_demo/src/terrain.rs @@ -1,8 +1,9 @@ -use crate::DemoElement; use engine::meshload::load_mesh; use engine::terrain::TerrainRender as EngineTerrainRender; use engine::{Context, FrameContext, InstancedMeshBuilder, MeshInstance}; -use geom::{vec2, Camera, Heightmap, HeightmapChunk, InfiniteFrustrum, LinearColor, Vec3}; +use geom::{vec2, Camera, Heightmap, HeightmapChunk, LinearColor, Vec3}; + +use crate::DemoElement; const CSIZE: usize = 512; const CRESO: usize = 16; @@ -95,8 +96,8 @@ impl DemoElement for Terrain { } } - fn render(&mut self, fc: &mut FrameContext, cam: &Camera, frustrum: &InfiniteFrustrum) { - self.terrain.draw_terrain(cam, frustrum, fc); + fn render(&mut self, fc: &mut FrameContext, cam: &Camera) { + self.terrain.draw_terrain(cam, fc); self.hitmesh.instances.clear(); if let Some(pos) = self.last_hitpos { diff --git a/native_app/src/game_loop.rs b/native_app/src/game_loop.rs index f803d43e..8e31373c 100644 --- a/native_app/src/game_loop.rs +++ b/native_app/src/game_loop.rs @@ -179,7 +179,6 @@ impl engine::framework::State for State { &sim.map(), time.seconds, &camera.camera, - &camera.frustrum, MapRenderOptions { show_arrows: self.uiw.read::().show_arrows(), show_lots: self.uiw.read::().show_lots(), @@ -261,7 +260,7 @@ impl State { .build_sun_shadowmap_matrix( sun, params.shadow_mapping_resolution as f32, - &camera.frustrum, + &ctx.gfx.frustrum, ) .try_into() .unwrap(); diff --git a/native_app/src/rendering/map_rendering/mod.rs b/native_app/src/rendering/map_rendering/mod.rs index c8f190f6..9b6326cb 100644 --- a/native_app/src/rendering/map_rendering/mod.rs +++ b/native_app/src/rendering/map_rendering/mod.rs @@ -52,19 +52,18 @@ impl MapRenderer { map: &Map, time: u32, cam: &Camera, - frustrum: &InfiniteFrustrum, options: MapRenderOptions, draw: &mut ImmediateDraw, ctx: &mut FrameContext<'_>, ) { profiling::scope!("render map renderer"); - self.terrain.draw(cam, frustrum, ctx); + self.terrain.draw(cam, ctx); - self.trees.draw(map, cam, frustrum, ctx); + self.trees.draw(map, cam, ctx); self.meshb.latest_mesh(map, options, ctx); - Self::signals_render(map, time, cam, frustrum, draw); + Self::signals_render(map, time, cam, &ctx.gfx.frustrum, draw); ctx.draw(self.water.clone()); } diff --git a/native_app/src/rendering/map_rendering/terrain.rs b/native_app/src/rendering/map_rendering/terrain.rs index 98ed8dd9..c6c545b5 100644 --- a/native_app/src/rendering/map_rendering/terrain.rs +++ b/native_app/src/rendering/map_rendering/terrain.rs @@ -1,6 +1,6 @@ use engine::terrain::TerrainRender as EngineTerrainRender; use engine::{Context, FrameContext, GfxContext}; -use geom::{Camera, InfiniteFrustrum}; +use geom::Camera; use simulation::map::{Map, MapSubscriber, UpdateType}; use simulation::Simulation; @@ -24,8 +24,8 @@ impl TerrainRender { } } - pub fn draw(&mut self, cam: &Camera, frustrum: &InfiniteFrustrum, fctx: &mut FrameContext<'_>) { - self.terrain.draw_terrain(cam, frustrum, fctx); + pub fn draw(&mut self, cam: &Camera, fctx: &mut FrameContext<'_>) { + self.terrain.draw_terrain(cam, fctx); } pub fn update(&mut self, ctx: &mut Context, map: &Map) { diff --git a/native_app/src/rendering/map_rendering/trees.rs b/native_app/src/rendering/map_rendering/trees.rs index 5b756c7d..a1899250 100644 --- a/native_app/src/rendering/map_rendering/trees.rs +++ b/native_app/src/rendering/map_rendering/trees.rs @@ -1,16 +1,14 @@ +use std::ops::Mul; + use common::FastMap; use engine::meshload::load_mesh; use engine::wgpu::RenderPass; use engine::{ Drawable, FrameContext, GfxContext, InstancedMesh, InstancedMeshBuilder, MeshInstance, }; -use geom::{ - vec3, vec4, Camera, HeightmapChunk, InfiniteFrustrum, Intersect3, LinearColor, Matrix4, Vec3, - AABB3, -}; +use geom::{vec3, vec4, Camera, HeightmapChunk, Intersect3, LinearColor, Matrix4, Vec3, AABB3}; use simulation::config; use simulation::map::{Map, MapSubscriber, SubscriberChunkID, UpdateType}; -use std::ops::Mul; pub struct TreesRender { tree_builder: InstancedMeshBuilder, @@ -56,13 +54,7 @@ impl TreesRender { } } - pub fn draw( - &mut self, - map: &Map, - cam: &Camera, - frustrum: &InfiniteFrustrum, - ctx: &mut FrameContext<'_>, - ) { + pub fn draw(&mut self, map: &Map, cam: &Camera, ctx: &mut FrameContext<'_>) { self.build(map, ctx); if config().disable_trees { @@ -113,7 +105,7 @@ impl TreesRender { .map(HeightmapChunk::max_height) .fold(0.0, f32::max); - if !frustrum.intersects(&AABB3::new_size( + if !ctx.gfx.frustrum.intersects(&AABB3::new_size( cid.corner().z(-40.0), vec3( 5.0 + SubscriberChunkID::SIZE_F32, diff --git a/native_app/src/rendering/orbit_camera.rs b/native_app/src/rendering/orbit_camera.rs index 5bdf038e..1d02e33e 100644 --- a/native_app/src/rendering/orbit_camera.rs +++ b/native_app/src/rendering/orbit_camera.rs @@ -2,7 +2,7 @@ use common::saveload::Encoder; use engine::{Context, Tesselator}; -use geom::{Camera, InfiniteFrustrum, Plane, Radians, Vec2, Vec3, AABB}; +use geom::{Camera, Plane, Radians, Vec2, Vec3, AABB}; use simulation::map::pathfinding_crate::num_traits::Pow; use crate::gui::windows::settings::Settings; @@ -18,7 +18,6 @@ pub struct OrbitCamera { pub targetyaw: Radians, pub targetpitch: Radians, pub targetdist: f32, - pub frustrum: InfiniteFrustrum, } impl OrbitCamera { @@ -82,7 +81,6 @@ impl OrbitCamera { targetyaw: camera.yaw, targetpitch: camera.pitch, targetdist: camera.dist, - frustrum: InfiniteFrustrum::new([Plane::new(Vec3::ZERO, 0.0); 5]), } }