Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move special-case ground plane/picked/manipulator to common display lists code #2109

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions crates/lgn-animation/src/debug_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use crate::{
};
use lgn_ecs::prelude::{Query, Res};
use lgn_graphics_data::Color;
use lgn_graphics_renderer::{debug_display::DebugDisplay, resources::DefaultMeshType};
use lgn_graphics_renderer::{
debug_display::{DebugDisplay, DebugPrimitiveMaterial, DebugPrimitiveType},
resources::DefaultMeshType,
};

pub(crate) fn display_animation(
debug_display: Res<'_, DebugDisplay>,
Expand Down Expand Up @@ -42,8 +45,9 @@ pub(crate) fn display_animation(
let debug_color = Color::new(bone_depth * color_interval, 255, 52, 255);
builder.add_default_mesh(
&clip.poses[clip.current_key_frame_index].transforms[n_bone].global,
DefaultMeshType::Sphere,
DebugPrimitiveType::default_mesh(DefaultMeshType::Sphere),
debug_color,
DebugPrimitiveMaterial::WireDepth,
);
}
}
Expand Down
11 changes: 7 additions & 4 deletions crates/lgn-graphics-renderer/src/components/light_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
InsertRenderObjectCommand, PrimaryTableCommandBuilder, PrimaryTableView,
RemoveRenderObjectCommand, RenderObjectId, UpdateRenderObjectCommand,
},
debug_display::DebugDisplay,
debug_display::{DebugDisplay, DebugPrimitiveMaterial, DebugPrimitiveType},
lighting::RenderLight,
resources::DefaultMeshType,
};
Expand Down Expand Up @@ -170,8 +170,9 @@ pub(crate) fn tmp_debug_display_lights(
.with_translation(transform.translation)
.with_scale(Vec3::new(0.2, 0.2, 0.2)) // assumes the size of sphere 1.0. Needs to be scaled in order to match picking silhouette
.with_rotation(transform.rotation),
DefaultMeshType::Sphere,
DebugPrimitiveType::default_mesh(DefaultMeshType::Sphere),
Color::WHITE,
DebugPrimitiveMaterial::WireDepth,
);
match light.light_type {
LightType::Directional => {
Expand All @@ -182,8 +183,9 @@ pub(crate) fn tmp_debug_display_lights(
- transform.rotation.mul_vec3(Vec3::new(0.0, 0.0, 0.3)), // assumes arrow length to be 0.3
)
.with_rotation(transform.rotation),
DefaultMeshType::Arrow,
DebugPrimitiveType::default_mesh(DefaultMeshType::Arrow),
Color::WHITE,
DebugPrimitiveMaterial::WireDepth,
);
}
LightType::Spot => {
Expand All @@ -195,8 +197,9 @@ pub(crate) fn tmp_debug_display_lights(
)
.with_scale(Vec3::new(factor, factor, 1.0))
.with_rotation(transform.rotation),
DefaultMeshType::Cone,
DebugPrimitiveType::default_mesh(DefaultMeshType::Cone),
Color::WHITE,
DebugPrimitiveMaterial::WireDepth,
);
}
LightType::OmniDirectional => (),
Expand Down
148 changes: 144 additions & 4 deletions crates/lgn-graphics-renderer/src/components/render_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@ use std::collections::hash_map::{Values, ValuesMut};
use std::hash::Hash;
use std::{cmp::max, sync::Arc};

use lgn_ecs::prelude::{Query, ResMut};
use lgn_ecs::prelude::{Query, ResMut, With};
use lgn_ecs::system::Res;
use lgn_graphics_api::{
CmdCopyTextureParams, CommandBuffer, DeviceContext, Extents2D, Extents3D, Format, MemoryUsage,
Offset2D, Offset3D, PlaneSlice, ResourceFlags, ResourceState, ResourceUsage, Semaphore,
SemaphoreDef, Texture, TextureBarrier, TextureDef, TextureTiling, TextureView, TextureViewDef,
};
use lgn_graphics_data::Color;
use lgn_math::Vec3;
use lgn_transform::prelude::GlobalTransform;
use lgn_window::WindowId;
use parking_lot::RwLock;
use std::collections::HashMap;
use uuid::Uuid;

use crate::core::{
as_render_object, InsertRenderObjectCommand, PrimaryTableCommandBuilder, PrimaryTableView,
RemoveRenderObjectCommand, RenderObjectId, RenderViewport, RenderViewportRendererData,
UpdateRenderObjectCommand, Viewport, ViewportId,
RemoveRenderObjectCommand, RenderCamera, RenderObjectId, RenderObjects, RenderViewport,
RenderViewportRendererData, UpdateRenderObjectCommand, Viewport, ViewportId,
};
use crate::debug_display::{DebugDisplay, DebugPrimitiveMaterial, DebugPrimitiveType};
use crate::picking::ManipulatorManager;
use crate::render_pass::PickingRenderPass;
use crate::resources::{DefaultMeshType, MeshManager};
use crate::{RenderContext, Renderer};

use super::CameraComponent;
use super::{CameraComponent, ManipulatorComponent, PickedComponent, VisualComponent};

pub trait Presenter: Send + Sync {
fn resize(&mut self, device_context: &DeviceContext, extents: RenderSurfaceExtents);
Expand Down Expand Up @@ -669,3 +676,136 @@ pub(crate) fn reflect_viewports(
}
}
}

#[allow(clippy::needless_pass_by_value)]
pub fn build_display_lists(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the right place for this function (system), but I don't know where to put it. In renderer/lib.rs? Open to suggestions.

renderer: Res<'_, Renderer>,
render_surfaces: ResMut<'_, RenderSurfaces>,
debug_display: ResMut<'_, DebugDisplay>,
picked_drawables: Query<'_, '_, (&VisualComponent, &GlobalTransform), With<PickedComponent>>,
manipulator_drawables: Query<'_, '_, (&GlobalTransform, &ManipulatorComponent)>,
) {
for render_surface in render_surfaces.iter() {
for viewport in render_surface.viewports() {
if viewport.options().ground_plane_enabled {
debug_display.create_display_list(|builder| {
builder.add_default_mesh(
&GlobalTransform::identity(),
DebugPrimitiveType::default_mesh(DefaultMeshType::GroundPlane),
Color::BLACK,
DebugPrimitiveMaterial::WireDepth,
);
});
}

if viewport.options().picked_enabled {
let mesh_manager = renderer.render_resources().get::<MeshManager>();
let mesh_reader = mesh_manager.read();
for (visual_component, transform) in &picked_drawables {
let render_model = visual_component.render_model_handle();
let render_model = render_model.get().unwrap();

for mesh in render_model.mesh_instances() {
let mesh_id = mesh.mesh_id;
let mesh = mesh_reader.get_render_mesh(mesh_id);

let mut min_bound = Vec3::new(f32::MAX, f32::MAX, f32::MAX);
let mut max_bound = Vec3::new(f32::MIN, f32::MIN, f32::MIN);

for position in &mesh.positions {
let world_pos = transform.mul_vec3(*position);

min_bound = min_bound.min(world_pos);
max_bound = max_bound.max(world_pos);
}

let delta = max_bound - min_bound;
let mid_point = min_bound + delta * 0.5;

let aabb_transform = GlobalTransform::identity()
.with_translation(mid_point)
.with_scale(delta);

debug_display.create_display_list(|builder| {
builder.add_default_mesh(
&aabb_transform,
DebugPrimitiveType::default_mesh(DefaultMeshType::WireframeCube),
Color::WHITE,
DebugPrimitiveMaterial::WireDepth,
);
builder.add_default_mesh(
transform,
DebugPrimitiveType::mesh(mesh_id),
Color::new(0, 127, 127, 127),
DebugPrimitiveMaterial::SolidDepth,
);
});
}
}
}

if viewport.options().manipulators_enabled {
let render_objects = renderer.render_resources().get::<RenderObjects>();
let viewport_primary_table = render_objects.primary_table::<RenderViewport>();
let camera_primary_table = render_objects.primary_table::<RenderCamera>();

// TODO(jsg) we need to make a display list specific to one viewport. Otherwise we will see the
// manipulators for all viewports in every viewport.
let render_viewport_id = viewport.render_object_id();
if render_viewport_id.is_none() {
continue;
}
let render_viewport_id = render_viewport_id.unwrap();
let render_viewport =
viewport_primary_table.try_get::<RenderViewport>(render_viewport_id);
if render_viewport.is_none() {
continue;
}
let render_viewport = render_viewport.unwrap();

let render_camera_id = render_viewport.camera_id();
if render_camera_id.is_none() {
continue;
}
let render_camera_id = render_camera_id.unwrap();
let render_camera = camera_primary_table.try_get::<RenderCamera>(render_camera_id);
if render_camera.is_none() {
continue;
}
let render_camera = render_camera.unwrap();

for (transform, manipulator) in &manipulator_drawables {
if manipulator.active {
let view_transform = render_camera.view_transform();
let projection = render_camera.build_projection(
render_viewport.extents().width as f32,
render_viewport.extents().height as f32,
);
let scaled_xform = ManipulatorManager::scale_manipulator_for_viewport(
transform,
&manipulator.local_transform,
projection,
&view_transform,
);

let mut color = if manipulator.selected {
Color::YELLOW
} else {
manipulator.color
};
color.a = if manipulator.transparent { 225 } else { 255 };

debug_display.create_display_list(|builder| {
builder.add_default_mesh(
&scaled_xform,
DebugPrimitiveType::default_mesh(manipulator.default_mesh_type),
color,
DebugPrimitiveMaterial::SolidNoDepth,
);
});
}
}
}
}
}
}
6 changes: 4 additions & 2 deletions crates/lgn-graphics-renderer/src/core/render_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,12 +526,14 @@ impl PrimaryTable {
let generation = id.generation;
assert!(
self.set.borrow().allocated.contains(index),
"RenderObject index {} not allocated.",
"RenderObject of type {} index {} not allocated.",
self.key.type_name,
index
);
assert!(
self.set.borrow().generations[index] == generation,
"RenderObject index {} generation mismatch (expected {} got {})",
"RenderObject of type {} index {} generation mismatch (expected {} got {})",
self.key.type_name,
index,
self.set.borrow().generations[index],
generation
Expand Down
24 changes: 24 additions & 0 deletions crates/lgn-graphics-renderer/src/core/services/viewports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use lgn_graphics_api::{
ResourceUsage, StoreOp, Texture, TextureBarrier, TextureDef, TextureTiling, TextureView,
TextureViewDef, ViewDimension,
};

use uuid::Uuid;

use crate::core::{RenderObjectId, SecondaryTableHandler};
Expand All @@ -17,11 +18,29 @@ impl ViewportId {
}
}

#[derive(Clone)]
pub struct ViewportOptions {
pub ground_plane_enabled: bool,
pub picked_enabled: bool,
pub manipulators_enabled: bool,
}

impl Default for ViewportOptions {
fn default() -> Self {
Self {
ground_plane_enabled: true,
picked_enabled: true,
manipulators_enabled: true,
}
}
}

#[derive(Clone)]
pub struct Viewport {
id: ViewportId,
offset: Offset2D,
extents: Extents2D,
options: ViewportOptions,
camera_id: Option<RenderObjectId>,
render_object_id: Option<RenderObjectId>,
}
Expand All @@ -32,6 +51,7 @@ impl Viewport {
id: ViewportId::new(),
offset,
extents,
options: ViewportOptions::default(),
camera_id: None,
render_object_id: None,
}
Expand Down Expand Up @@ -71,6 +91,10 @@ impl Viewport {
pub fn set_render_object_id(&mut self, render_object_id: RenderObjectId) {
self.render_object_id = Some(render_object_id);
}

pub fn options(&self) -> &ViewportOptions {
&self.options
}
}

#[derive(Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use lgn_tracing::span_fn;
use lgn_transform::components::GlobalTransform;
use std::sync::Mutex;

use crate::resources::DefaultMeshType;
use crate::resources::{DefaultMeshType, RenderMeshId};

pub struct DebugDisplay {
display_lists: Mutex<Vec<DisplayList>>,
Expand Down Expand Up @@ -61,13 +61,15 @@ impl<'system> DisplayListBuilder<'system> {
pub fn add_default_mesh(
&mut self,
transform: &GlobalTransform,
default_mesh_type: DefaultMeshType,
primitive_type: DebugPrimitiveType,
color: Color,
material: DebugPrimitiveMaterial,
) {
let primitive = DebugPrimitive {
primitive_type: DebugPrimitiveType::DefaultMesh { default_mesh_type },
primitive_type,
transform: *transform,
color,
material,
};
self.display_list.primitives.push(primitive);
}
Expand All @@ -78,14 +80,31 @@ pub struct DisplayList {
}

pub enum DebugPrimitiveType {
// TODO(vdbdd): add those new types
// DisplayList
// Mesh
Mesh { mesh_id: RenderMeshId },
DefaultMesh { default_mesh_type: DefaultMeshType },
}

impl DebugPrimitiveType {
pub fn mesh(mesh_id: RenderMeshId) -> Self {
Self::Mesh { mesh_id }
}

pub fn default_mesh(default_mesh_type: DefaultMeshType) -> Self {
Self::DefaultMesh { default_mesh_type }
}
}

#[allow(clippy::enum_variant_names)]
pub enum DebugPrimitiveMaterial {
WireDepth,
SolidDepth,
WireNoDepth,
SolidNoDepth,
}

pub struct DebugPrimitive {
pub primitive_type: DebugPrimitiveType,
pub transform: GlobalTransform,
pub color: Color,
pub material: DebugPrimitiveMaterial,
}
8 changes: 6 additions & 2 deletions crates/lgn-graphics-renderer/src/debug_display/stress_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use lgn_graphics_data::Color;
use lgn_tracing::span_fn;
use lgn_transform::components::GlobalTransform;

use crate::resources::DefaultMeshType;
use crate::{
debug_display::{DebugPrimitiveMaterial, DebugPrimitiveType},
resources::DefaultMeshType,
};

use super::DebugDisplay;

Expand All @@ -22,8 +25,9 @@ pub fn add_debug_things(debug_display: Res<'_, DebugDisplay>) {
for _i in 1..1000 {
builder.add_default_mesh(
&GlobalTransform::identity(),
DefaultMeshType::Sphere,
DebugPrimitiveType::default_mesh(DefaultMeshType::Sphere),
Color::BLACK,
DebugPrimitiveMaterial::WireDepth,
);
}
});
Expand Down
Loading