Add multi-threaded scene rendering with Job System

This commit is contained in:
Wojtek Figat
2022-11-06 15:35:43 +01:00
parent 183636289a
commit 9c7da16c1a
23 changed files with 564 additions and 160 deletions

View File

@@ -1,25 +1,14 @@
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
#define SCENE_RENDERING_USE_PROFILER 0
#define SCENE_RENDERING_USE_PROFILER_PER_ACTOR 0
#include "SceneRendering.h"
#include "Engine/Graphics/RenderTask.h"
#include "Engine/Graphics/RenderView.h"
#include "Engine/Renderer/RenderList.h"
#include "Engine/Threading/JobSystem.h"
#include "Engine/Threading/Threading.h"
#if SCENE_RENDERING_USE_PROFILER
#include "Engine/Profiler/ProfilerCPU.h"
#endif
FORCE_INLINE bool FrustumsListCull(const BoundingSphere& bounds, const BoundingFrustum* frustums, int32 frustumsCount)
{
for (int32 i = 0; i < frustumsCount; i++)
{
if (frustums[i].Intersects(bounds))
return true;
}
return false;
}
ISceneRenderingListener::~ISceneRenderingListener()
{
@@ -38,99 +27,65 @@ void ISceneRenderingListener::ListenSceneRendering(SceneRendering* scene)
}
}
FORCE_INLINE bool FrustumsListCull(const BoundingSphere& bounds, const Array<BoundingFrustum>& frustums)
{
const int32 count = frustums.Count();
const BoundingFrustum* data = frustums.Get();
for (int32 i = 0; i < count; i++)
{
if (data[i].Intersects(bounds))
return true;
}
return false;
}
void SceneRendering::Draw(RenderContextBatch& renderContextBatch, DrawCategory category)
{
ScopeLock lock(Locker);
auto& view = renderContextBatch.GetMainContext().View;
const Vector3 origin = view.Origin;
for (auto& renderContext : renderContextBatch.Contexts)
renderContext.List->Scenes.Add(this);
auto& list = Actors[(int32)category];
_drawListData = list.Get();
_drawListSize = list.Count();
_drawBatch = &renderContextBatch;
// Setup frustum data
Array<BoundingFrustum, RendererAllocation> frustumsData;
BoundingFrustum* frustums = &view.CullingFrustum;
int32 frustumsCount = renderContextBatch.Contexts.Count();
auto& frustumsData = _drawFrustumsData;
const int32 frustumsCount = renderContextBatch.Contexts.Count();
if (frustumsCount != 1)
{
frustumsData.Resize(frustumsCount);
frustums = frustumsData.Get();
for (int32 i = 0; i < frustumsCount; i++)
frustums[i] = renderContextBatch.Contexts[i].View.CullingFrustum;
frustumsData.Get()[i] = renderContextBatch.Contexts[i].View.CullingFrustum;
}
#define CHECK_ACTOR ((view.RenderLayersMask.Mask & e.LayerMask) && (e.NoCulling || FrustumsListCull(e.Bounds, frustums, frustumsCount)))
#define CHECK_ACTOR_SINGLE_FRUSTUM ((view.RenderLayersMask.Mask & e.LayerMask) && (e.NoCulling || frustums->Intersects(e.Bounds)))
#if SCENE_RENDERING_USE_PROFILER
#define DRAW_ACTOR(mode) PROFILE_CPU_ACTOR(e.Actor); e.Actor->Draw(mode)
#else
#define DRAW_ACTOR(mode) e.Actor->Draw(mode)
#endif
// Draw all visual components
if (view.IsOfflinePass)
_drawListIndex = -1;
if (_drawListSize >= 64 && category == SceneDrawAsync)
{
// Offline pass with additional static flags culling
for (int32 i = 0; i < list.Count(); i++)
{
auto e = list.Get()[i];
e.Bounds.Center -= origin;
if (CHECK_ACTOR && e.Actor->GetStaticFlags() & view.StaticFlagsMask)
{
DRAW_ACTOR(renderContextBatch);
}
}
}
else if (origin.IsZero() && frustumsCount == 1)
{
// Fast path for no origin shifting with a single context
RenderContext& renderContext = renderContextBatch.GetMainContext();
for (int32 i = 0; i < list.Count(); i++)
{
auto e = list.Get()[i];
if (CHECK_ACTOR_SINGLE_FRUSTUM)
{
DRAW_ACTOR(renderContext);
}
}
}
else if (origin.IsZero())
{
// Fast path for no origin shifting
for (int32 i = 0; i < list.Count(); i++)
{
auto e = list.Get()[i];
if (CHECK_ACTOR)
{
DRAW_ACTOR(renderContextBatch);
}
}
// Run in async via Job System
Function<void(int32)> func;
func.Bind<SceneRendering, &SceneRendering::DrawActorsJob>(this);
const uint64 waitLabel = JobSystem::Dispatch(func, JobSystem::GetThreadsCount());
renderContextBatch.WaitLabels.Add(waitLabel);
}
else
{
// Generic case
for (int32 i = 0; i < list.Count(); i++)
{
auto e = list.Get()[i];
e.Bounds.Center -= origin;
if (CHECK_ACTOR)
{
DRAW_ACTOR(renderContextBatch);
}
}
// Scene is small so draw on a main-thread
DrawActorsJob(0);
}
#undef CHECK_ACTOR
#undef DRAW_ACTOR
#if USE_EDITOR
if (view.Pass & DrawPass::GBuffer && category == SceneDraw)
{
// Draw physics shapes
if (view.Flags & ViewFlags::PhysicsDebug || view.Mode == ViewMode::PhysicsColliders)
{
const PhysicsDebugCallback* physicsDebugData = PhysicsDebug.Get();
for (int32 i = 0; i < PhysicsDebug.Count(); i++)
{
PhysicsDebug[i](view);
physicsDebugData[i](view);
}
}
}
@@ -144,7 +99,7 @@ void SceneRendering::CollectPostFxVolumes(RenderContext& renderContext)
#endif
for (int32 i = 0; i < PostFxProviders.Count(); i++)
{
PostFxProviders[i]->Collect(renderContext);
PostFxProviders.Get()[i]->Collect(renderContext);
}
}
@@ -174,7 +129,7 @@ void SceneRendering::AddActor(Actor* a, int32& key, DrawCategory category)
key = 0;
for (; key < list.Count(); key++)
{
if (list[key].Actor == nullptr)
if (list.Get()[key].Actor == nullptr)
break;
}
if (key == list.Count())
@@ -217,3 +172,65 @@ void SceneRendering::RemoveActor(Actor* a, int32& key, DrawCategory category)
}
key = -1;
}
#define FOR_EACH_BATCH_ACTOR const int64 count = _drawListSize; while (true) { const int64 index = Platform::InterlockedIncrement(&_drawListIndex); if (index >= count) break; auto e = _drawListData[index];
#define CHECK_ACTOR ((view.RenderLayersMask.Mask & e.LayerMask) && (e.NoCulling || FrustumsListCull(e.Bounds, _drawFrustumsData)))
#define CHECK_ACTOR_SINGLE_FRUSTUM ((view.RenderLayersMask.Mask & e.LayerMask) && (e.NoCulling || view.CullingFrustum.Intersects(e.Bounds)))
#if SCENE_RENDERING_USE_PROFILER_PER_ACTOR
#define DRAW_ACTOR(mode) PROFILE_CPU_ACTOR(e.Actor); e.Actor->Draw(mode)
#else
#define DRAW_ACTOR(mode) e.Actor->Draw(mode)
#endif
void SceneRendering::DrawActorsJob(int32 i)
{
PROFILE_CPU();
auto& mainContext = _drawBatch->GetMainContext();
const auto& view = mainContext.View;
if (view.IsOfflinePass)
{
// Offline pass with additional static flags culling
FOR_EACH_BATCH_ACTOR
e.Bounds.Center -= view.Origin;
if (CHECK_ACTOR && e.Actor->GetStaticFlags() & view.StaticFlagsMask)
{
DRAW_ACTOR(*_drawBatch);
}
}
}
else if (view.Origin.IsZero() && _drawFrustumsData.Count() == 1)
{
// Fast path for no origin shifting with a single context
FOR_EACH_BATCH_ACTOR
if (CHECK_ACTOR_SINGLE_FRUSTUM)
{
DRAW_ACTOR(mainContext);
}
}
}
else if (view.Origin.IsZero())
{
// Fast path for no origin shifting
FOR_EACH_BATCH_ACTOR
if (CHECK_ACTOR)
{
DRAW_ACTOR(*_drawBatch);
}
}
}
else
{
// Generic case
FOR_EACH_BATCH_ACTOR
e.Bounds.Center -= view.Origin;
if (CHECK_ACTOR)
{
DRAW_ACTOR(*_drawBatch);
}
}
}
}
#undef FOR_EACH_BATCH_ACTOR
#undef CHECK_ACTOR
#undef DRAW_ACTOR