Refactor SceneRendering to simplify actors impl of drawing flow at high level

This commit is contained in:
Wojciech Figat
2022-02-22 17:06:19 +01:00
parent 8c075c78cb
commit 3fe1e2c763
43 changed files with 191 additions and 617 deletions

View File

@@ -60,7 +60,6 @@ NavMeshBoundsVolume* SceneNavigation::FindNavigationBoundsOverlap(const Bounding
Scene::Scene(const SpawnParams& params)
: Actor(params)
, Rendering(this)
, LightmapsData(this)
, CSGData(this)
{

View File

@@ -1,369 +1,53 @@
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
#define SCENE_RENDERING_USE_PROFILER 0
#include "SceneRendering.h"
#include "Scene.h"
#include "Engine/Graphics/RenderTask.h"
#include "Engine/Graphics/RenderView.h"
#define SCENE_RENDERING_USE_PROFILER 0
#define SCENE_RENDERING_USE_SIMD 0
#include "Engine/Renderer/RenderList.h"
#if SCENE_RENDERING_USE_PROFILER
#include "Engine/Profiler/ProfilerCPU.h"
#endif
#if SCENE_RENDERING_USE_SIMD
#include "Engine/Core/SIMD.h"
ALIGN_BEGIN(16) struct CullDataSIMD
{
float xs[8];
float ys[8];
float zs[8];
float ds[8];
} ALIGN_END(16);
#endif
int32 SceneRendering::DrawEntries::Add(Actor* obj)
{
int32 key = 0;
for (; key < List.Count(); key++)
{
if (List[key].Actor == nullptr)
break;
}
if (key == List.Count())
List.AddOne();
auto& e = List[key];
e.Actor = obj;
e.LayerMask = obj->GetLayerMask();
e.Bounds = obj->GetSphere();
return key;
}
void SceneRendering::DrawEntries::Update(Actor* obj, int32 key)
{
if (List.IsEmpty())
return;
auto& e = List[key];
ASSERT_LOW_LAYER(obj == e.Actor);
e.LayerMask = obj->GetLayerMask();
e.Bounds = obj->GetSphere();
}
void SceneRendering::DrawEntries::Remove(Actor* obj, int32 key)
{
if (List.IsEmpty())
return;
auto& e = List[key];
ASSERT_LOW_LAYER(obj == e.Actor);
e.Actor = nullptr;
e.LayerMask = 0;
}
void SceneRendering::DrawEntries::Clear()
{
List.Clear();
}
void SceneRendering::DrawEntries::CullAndDraw(RenderContext& renderContext)
{
auto& view = renderContext.View;
const BoundingFrustum frustum = view.CullingFrustum;
#if SCENE_RENDERING_USE_SIMD
CullDataSIMD cullData;
{
// Near
auto plane = view.Frustum.GetNear();
cullData.xs[0] = plane.Normal.X;
cullData.ys[0] = plane.Normal.Y;
cullData.zs[0] = plane.Normal.Z;
cullData.ds[0] = plane.D;
// Far
plane = view.Frustum.GetFar();
cullData.xs[1] = plane.Normal.X;
cullData.ys[1] = plane.Normal.Y;
cullData.zs[1] = plane.Normal.Z;
cullData.ds[1] = plane.D;
// Left
plane = view.Frustum.GetLeft();
cullData.xs[2] = plane.Normal.X;
cullData.ys[2] = plane.Normal.Y;
cullData.zs[2] = plane.Normal.Z;
cullData.ds[2] = plane.D;
// Right
plane = view.Frustum.GetRight();
cullData.xs[3] = plane.Normal.X;
cullData.ys[3] = plane.Normal.Y;
cullData.zs[3] = plane.Normal.Z;
cullData.ds[3] = plane.D;
// Top
plane = view.Frustum.GetTop();
cullData.xs[4] = plane.Normal.X;
cullData.ys[4] = plane.Normal.Y;
cullData.zs[4] = plane.Normal.Z;
cullData.ds[4] = plane.D;
// Bottom
plane = view.Frustum.GetBottom();
cullData.xs[5] = plane.Normal.X;
cullData.ys[5] = plane.Normal.Y;
cullData.zs[5] = plane.Normal.Z;
cullData.ds[5] = plane.D;
// Extra 0
cullData.xs[6] = 0;
cullData.ys[6] = 0;
cullData.zs[6] = 0;
cullData.ds[6] = 0;
// Extra 1
cullData.xs[7] = 0;
cullData.ys[7] = 0;
cullData.zs[7] = 0;
cullData.ds[7] = 0;
}
SimdVector4 px = SIMD::Load(cullData.xs);
SimdVector4 py = SIMD::Load(cullData.ys);
SimdVector4 pz = SIMD::Load(cullData.zs);
SimdVector4 pd = SIMD::Load(cullData.ds);
SimdVector4 px2 = SIMD::Load(&cullData.xs[4]);
SimdVector4 py2 = SIMD::Load(&cullData.ys[4]);
SimdVector4 pz2 = SIMD::Load(&cullData.zs[4]);
SimdVector4 pd2 = SIMD::Load(&cullData.ds[4]);
for (int32 i = 0; i < List.Count(); i++)
{
auto e = List[i];
SimdVector4 cx = SIMD::Splat(e.Bounds.Center.X);
SimdVector4 cy = SIMD::Splat(e.Bounds.Center.Y);
SimdVector4 cz = SIMD::Splat(e.Bounds.Center.Z);
SimdVector4 r = SIMD::Splat(-e.Bounds.Radius);
SimdVector4 t = SIMD::Mul(cx, px);
t = SIMD::Add(t, SIMD::Mul(cy, py));
t = SIMD::Add(t, SIMD::Mul(cz, pz));
t = SIMD::Add(t, pd);
t = SIMD::Sub(t, r);
if (SIMD::MoveMask(t))
continue;
t = SIMD::Mul(cx, px2);
t = SIMD::Add(t, SIMD::Mul(cy, py2));
t = SIMD::Add(t, SIMD::Mul(cz, pz2));
t = SIMD::Add(t, pd2);
t = SIMD::Sub(t, r);
if (SIMD::MoveMask(t))
continue;
if (view.RenderLayersMask.Mask & e.LayerMask)
{
#if SCENE_RENDERING_USE_PROFILER
PROFILE_CPU();
#if TRACY_ENABLE
___tracy_scoped_zone.Name(*e.Actor->GetName(), e.Actor->GetName().Length());
#endif
#endif
e.Actor->Draw(renderContext);
}
}
#else
for (int32 i = 0; i < List.Count(); i++)
{
auto e = List[i];
if (view.RenderLayersMask.Mask & e.LayerMask && frustum.Intersects(e.Bounds))
{
#if SCENE_RENDERING_USE_PROFILER
PROFILE_CPU();
#if TRACY_ENABLE
___tracy_scoped_zone.Name(*e.Actor->GetName(), e.Actor->GetName().Length());
#endif
#endif
e.Actor->Draw(renderContext);
}
}
#endif
}
void SceneRendering::DrawEntries::CullAndDrawOffline(RenderContext& renderContext)
{
auto& view = renderContext.View;
const BoundingFrustum frustum = view.CullingFrustum;
#if SCENE_RENDERING_USE_SIMD
CullDataSIMD cullData;
{
// Near
auto plane = view.Frustum.GetNear();
cullData.xs[0] = plane.Normal.X;
cullData.ys[0] = plane.Normal.Y;
cullData.zs[0] = plane.Normal.Z;
cullData.ds[0] = plane.D;
// Far
plane = view.Frustum.GetFar();
cullData.xs[1] = plane.Normal.X;
cullData.ys[1] = plane.Normal.Y;
cullData.zs[1] = plane.Normal.Z;
cullData.ds[1] = plane.D;
// Left
plane = view.Frustum.GetLeft();
cullData.xs[2] = plane.Normal.X;
cullData.ys[2] = plane.Normal.Y;
cullData.zs[2] = plane.Normal.Z;
cullData.ds[2] = plane.D;
// Right
plane = view.Frustum.GetRight();
cullData.xs[3] = plane.Normal.X;
cullData.ys[3] = plane.Normal.Y;
cullData.zs[3] = plane.Normal.Z;
cullData.ds[3] = plane.D;
// Top
plane = view.Frustum.GetTop();
cullData.xs[4] = plane.Normal.X;
cullData.ys[4] = plane.Normal.Y;
cullData.zs[4] = plane.Normal.Z;
cullData.ds[4] = plane.D;
// Bottom
plane = view.Frustum.GetBottom();
cullData.xs[5] = plane.Normal.X;
cullData.ys[5] = plane.Normal.Y;
cullData.zs[5] = plane.Normal.Z;
cullData.ds[5] = plane.D;
// Extra 0
cullData.xs[6] = 0;
cullData.ys[6] = 0;
cullData.zs[6] = 0;
cullData.ds[6] = 0;
// Extra 1
cullData.xs[7] = 0;
cullData.ys[7] = 0;
cullData.zs[7] = 0;
cullData.ds[7] = 0;
}
SimdVector4 px = SIMD::Load(cullData.xs);
SimdVector4 py = SIMD::Load(cullData.ys);
SimdVector4 pz = SIMD::Load(cullData.zs);
SimdVector4 pd = SIMD::Load(cullData.ds);
SimdVector4 px2 = SIMD::Load(&cullData.xs[4]);
SimdVector4 py2 = SIMD::Load(&cullData.ys[4]);
SimdVector4 pz2 = SIMD::Load(&cullData.zs[4]);
SimdVector4 pd2 = SIMD::Load(&cullData.ds[4]);
for (int32 i = 0; i < List.Count(); i++)
{
auto e = List[i];
SimdVector4 cx = SIMD::Splat(e.Bounds.Center.X);
SimdVector4 cy = SIMD::Splat(e.Bounds.Center.Y);
SimdVector4 cz = SIMD::Splat(e.Bounds.Center.Z);
SimdVector4 r = SIMD::Splat(-e.Bounds.Radius);
SimdVector4 t = SIMD::Mul(cx, px);
t = SIMD::Add(t, SIMD::Mul(cy, py));
t = SIMD::Add(t, SIMD::Mul(cz, pz));
t = SIMD::Add(t, pd);
t = SIMD::Sub(t, r);
if (SIMD::MoveMask(t))
continue;
t = SIMD::Mul(cx, px2);
t = SIMD::Add(t, SIMD::Mul(cy, py2));
t = SIMD::Add(t, SIMD::Mul(cz, pz2));
t = SIMD::Add(t, pd2);
t = SIMD::Sub(t, r);
if (SIMD::MoveMask(t))
continue;
if (view.RenderLayersMask.Mask & e.LayerMask && e.Actor->GetStaticFlags() & renderContext.View.StaticFlagsMask)
{
#if SCENE_RENDERING_USE_PROFILER
PROFILE_CPU();
#if TRACY_ENABLE
___tracy_scoped_zone.Name(*e.Actor->GetName(), e.Actor->GetName().Length());
#endif
#endif
e.Actor->Draw(renderContext);
}
}
#else
for (int32 i = 0; i < List.Count(); i++)
{
auto e = List[i];
if (view.RenderLayersMask.Mask & e.LayerMask && frustum.Intersects(e.Bounds) && e.Actor->GetStaticFlags() & view.StaticFlagsMask)
{
#if SCENE_RENDERING_USE_PROFILER
PROFILE_CPU();
#if TRACY_ENABLE
___tracy_scoped_zone.Name(*e.Actor->GetName(), e.Actor->GetName().Length());
#endif
#endif
e.Actor->Draw(renderContext);
}
}
#endif
}
SceneRendering::SceneRendering(::Scene* scene)
: Scene(scene)
{
}
void SceneRendering::Draw(RenderContext& renderContext)
{
// Skip if disabled
if (!Scene->GetIsActive())
return;
auto& view = renderContext.View;
const BoundingFrustum frustum = view.CullingFrustum;
renderContext.List->Scenes.Add(this);
// Draw all visual components
if (view.IsOfflinePass)
{
Geometry.CullAndDrawOffline(renderContext);
if (view.Pass & DrawPass::GBuffer)
for (int32 i = 0; i < Actors.Count(); i++)
{
Common.CullAndDrawOffline(renderContext);
for (int32 i = 0; i < CommonNoCulling.Count(); i++)
auto& e = Actors[i];
if (view.RenderLayersMask.Mask & e.LayerMask && (e.NoCulling || frustum.Intersects(e.Bounds)) && e.Actor->GetStaticFlags() & view.StaticFlagsMask)
{
auto actor = CommonNoCulling[i];
if (actor->GetStaticFlags() & view.StaticFlagsMask && view.RenderLayersMask.HasLayer(actor->GetLayer()))
actor->Draw(renderContext);
#if SCENE_RENDERING_USE_PROFILER
PROFILE_CPU();
#if TRACY_ENABLE
___tracy_scoped_zone.Name(*e.Actor->GetName(), e.Actor->GetName().Length());
#endif
#endif
e.Actor->Draw(renderContext);
}
}
}
else
{
Geometry.CullAndDraw(renderContext);
if (view.Pass & DrawPass::GBuffer)
for (int32 i = 0; i < Actors.Count(); i++)
{
Common.CullAndDraw(renderContext);
for (int32 i = 0; i < CommonNoCulling.Count(); i++)
auto& e = Actors[i];
if (view.RenderLayersMask.Mask & e.LayerMask && (e.NoCulling || frustum.Intersects(e.Bounds)))
{
auto actor = CommonNoCulling[i];
if (view.RenderLayersMask.HasLayer(actor->GetLayer()))
{
#if SCENE_RENDERING_USE_PROFILER
PROFILE_CPU();
PROFILE_CPU();
#if TRACY_ENABLE
___tracy_scoped_zone.Name(*actor->GetName(), actor->GetName().Length());
___tracy_scoped_zone.Name(*e.Actor->GetName(), e.Actor->GetName().Length());
#endif
#endif
actor->Draw(renderContext);
}
e.Actor->Draw(renderContext);
}
}
}
@@ -395,10 +79,47 @@ void SceneRendering::CollectPostFxVolumes(RenderContext& renderContext)
void SceneRendering::Clear()
{
Geometry.Clear();
Common.Clear();
CommonNoCulling.Clear();
Actors.Clear();
#if USE_EDITOR
PhysicsDebug.Clear();
#endif
}
int32 SceneRendering::AddActor(Actor* a)
{
int32 key = 0;
for (; key < Actors.Count(); key++)
{
if (Actors[key].Actor == nullptr)
break;
}
if (key == Actors.Count())
Actors.AddOne();
auto& e = Actors[key];
e.Actor = a;
e.LayerMask = a->GetLayerMask();
e.Bounds = a->GetSphere();
e.NoCulling = a->_drawNoCulling;
return key;
}
void SceneRendering::UpdateActor(Actor* a, int32 key)
{
if (Actors.IsEmpty())
return;
auto& e = Actors[key];
ASSERT_LOW_LAYER(a == e.Actor);
e.LayerMask = a->GetLayerMask();
e.Bounds = a->GetSphere();
}
void SceneRendering::RemoveActor(Actor* a, int32& key)
{
if (Actors.IsEmpty())
return;
auto& e = Actors[key];
ASSERT_LOW_LAYER(a == e.Actor);
e.Actor = nullptr;
e.LayerMask = 0;
key = -1;
}

View File

@@ -6,7 +6,6 @@
#include "Engine/Core/Collections/Array.h"
#include "Engine/Core/Math/BoundingSphere.h"
#include "Engine/Level/Actor.h"
#include "Engine/Level/Types.h"
class SceneRenderTask;
struct PostProcessSettings;
@@ -19,7 +18,6 @@ struct RenderView;
class FLAXENGINE_API IPostFxSettingsProvider
{
public:
/// <summary>
/// Collects the settings for rendering of the specified task.
/// </summary>
@@ -39,46 +37,29 @@ public:
/// </summary>
class FLAXENGINE_API SceneRendering
{
friend Scene;
#if USE_EDITOR
typedef Function<void(RenderView&)> PhysicsDebugCallback;
friend class ViewportIconsRendererService;
#endif
struct DrawEntry
public:
struct DrawActor
{
Actor* Actor;
uint32 LayerMask;
int8 NoCulling : 1;
BoundingSphere Bounds;
};
struct DrawEntries
{
Array<DrawEntry> List;
int32 Add(Actor* obj);
void Update(Actor* obj, int32 key);
void Remove(Actor* obj, int32 key);
void Clear();
void CullAndDraw(RenderContext& renderContext);
void CullAndDrawOffline(RenderContext& renderContext);
};
Array<DrawActor> Actors;
Array<IPostFxSettingsProvider*> PostFxProviders;
private:
Scene* Scene;
DrawEntries Geometry;
DrawEntries Common;
Array<Actor*> CommonNoCulling;
Array<IPostFxSettingsProvider*> PostFxProviders;
#if USE_EDITOR
Array<PhysicsDebugCallback> PhysicsDebug;
Array<Actor*> ViewportIcons;
#endif
explicit SceneRendering(::Scene* scene);
public:
/// <summary>
/// Draws the scene. Performs the optimized actors culling and draw calls submission for the current render pass (defined by the render view).
/// </summary>
@@ -97,48 +78,9 @@ public:
void Clear();
public:
FORCE_INLINE int32 AddGeometry(Actor* obj)
{
return Geometry.Add(obj);
}
FORCE_INLINE void UpdateGeometry(Actor* obj, int32 key)
{
Geometry.Update(obj, key);
}
FORCE_INLINE void RemoveGeometry(Actor* obj, int32& key)
{
Geometry.Remove(obj, key);
key = -1;
}
FORCE_INLINE int32 AddCommon(Actor* obj)
{
return Common.Add(obj);
}
FORCE_INLINE void UpdateCommon(Actor* obj, int32 key)
{
Common.Update(obj, key);
}
FORCE_INLINE void RemoveCommon(Actor* obj, int32& key)
{
Common.Remove(obj, key);
key = -1;
}
FORCE_INLINE void AddCommonNoCulling(Actor* obj)
{
CommonNoCulling.Add(obj);
}
FORCE_INLINE void RemoveCommonNoCulling(Actor* obj)
{
CommonNoCulling.Remove(obj);
}
int32 AddActor(Actor* a);
void UpdateActor(Actor* a, int32 key);
void RemoveActor(Actor* a, int32& key);
FORCE_INLINE void AddPostFxProvider(IPostFxSettingsProvider* obj)
{
@@ -151,7 +93,6 @@ public:
}
#if USE_EDITOR
template<class T, void(T::*Method)(RenderView&)>
FORCE_INLINE void AddPhysicsDebug(T* obj)
{
@@ -177,6 +118,5 @@ public:
{
ViewportIcons.Remove(obj);
}
#endif
};