Refactor scene rendering to use batched culling for main view and shadow projections

This commit is contained in:
Wojtek Figat
2022-10-28 09:13:28 +02:00
parent e217d5e79b
commit eb52d333ae
44 changed files with 1064 additions and 514 deletions

View File

@@ -31,11 +31,11 @@
#define STREAM_TASK_BASE MainThreadTask
#endif
#define CHECK_INVALID_BUFFER(buffer) \
if (buffer->IsValidFor(this) == false) \
#define CHECK_INVALID_BUFFER(model, buffer) \
if (buffer->IsValidFor(model) == false) \
{ \
LOG(Warning, "Invalid Model Instance Buffer size {0} for Model {1}. It should be {2}. Manual update to proper size.", buffer->Count(), ToString(), MaterialSlots.Count()); \
buffer->Setup(this); \
LOG(Warning, "Invalid Model Instance Buffer size {0} for Model {1}. It should be {2}. Manual update to proper size.", buffer->Count(), model->ToString(), model->MaterialSlots.Count()); \
buffer->Setup(model); \
}
REGISTER_BINARY_ASSET_ABSTRACT(ModelBase, "FlaxEngine.ModelBase");
@@ -206,14 +206,15 @@ void Model::Draw(const RenderContext& renderContext, MaterialBase* material, con
LODs[lodIndex].Draw(renderContext, material, world, flags, receiveDecals);
}
void Model::Draw(const RenderContext& renderContext, const Mesh::DrawInfo& info)
template<typename ContextType>
FORCE_INLINE void ModelDraw(Model* model, const RenderContext& renderContext, const ContextType& context, const Mesh::DrawInfo& info)
{
ASSERT(info.Buffer);
if (!CanBeRendered())
if (!model->CanBeRendered())
return;
const auto frame = Engine::FrameCount;
const auto modelFrame = info.DrawState->PrevFrame + 1;
CHECK_INVALID_BUFFER(info.Buffer);
CHECK_INVALID_BUFFER(model, info.Buffer);
// Select a proper LOD index (model may be culled)
int32 lodIndex;
@@ -223,7 +224,7 @@ void Model::Draw(const RenderContext& renderContext, const Mesh::DrawInfo& info)
}
else
{
lodIndex = RenderTools::ComputeModelLOD(this, info.Bounds.Center, (float)info.Bounds.Radius, renderContext);
lodIndex = RenderTools::ComputeModelLOD(model, info.Bounds.Center, (float)info.Bounds.Radius, renderContext);
if (lodIndex == -1)
{
// Handling model fade-out transition
@@ -244,9 +245,9 @@ void Model::Draw(const RenderContext& renderContext, const Mesh::DrawInfo& info)
}
else
{
const auto prevLOD = ClampLODIndex(info.DrawState->PrevLOD);
const auto prevLOD = model->ClampLODIndex(info.DrawState->PrevLOD);
const float normalizedProgress = static_cast<float>(info.DrawState->LODTransition) * (1.0f / 255.0f);
LODs[prevLOD].Draw(renderContext, info, normalizedProgress);
model->LODs.Get()[prevLOD].Draw(renderContext, info, normalizedProgress);
}
}
@@ -254,7 +255,7 @@ void Model::Draw(const RenderContext& renderContext, const Mesh::DrawInfo& info)
}
}
lodIndex += info.LODBias + renderContext.View.ModelLODBias;
lodIndex = ClampLODIndex(lodIndex);
lodIndex = model->ClampLODIndex(lodIndex);
if (renderContext.View.IsSingleFrame)
{
@@ -287,22 +288,32 @@ void Model::Draw(const RenderContext& renderContext, const Mesh::DrawInfo& info)
// Draw
if (info.DrawState->PrevLOD == lodIndex || renderContext.View.IsSingleFrame)
{
LODs[lodIndex].Draw(renderContext, info, 0.0f);
model->LODs.Get()[lodIndex].Draw(context, info, 0.0f);
}
else if (info.DrawState->PrevLOD == -1)
{
const float normalizedProgress = static_cast<float>(info.DrawState->LODTransition) * (1.0f / 255.0f);
LODs[lodIndex].Draw(renderContext, info, 1.0f - normalizedProgress);
model->LODs.Get()[lodIndex].Draw(context, info, 1.0f - normalizedProgress);
}
else
{
const auto prevLOD = ClampLODIndex(info.DrawState->PrevLOD);
const auto prevLOD = model->ClampLODIndex(info.DrawState->PrevLOD);
const float normalizedProgress = static_cast<float>(info.DrawState->LODTransition) * (1.0f / 255.0f);
LODs[prevLOD].Draw(renderContext, info, normalizedProgress);
LODs[lodIndex].Draw(renderContext, info, normalizedProgress - 1.0f);
model->LODs.Get()[prevLOD].Draw(context, info, normalizedProgress);
model->LODs.Get()[lodIndex].Draw(context, info, normalizedProgress - 1.0f);
}
}
void Model::Draw(const RenderContext& renderContext, const Mesh::DrawInfo& info)
{
ModelDraw(this, renderContext, renderContext, info);
}
void Model::Draw(const RenderContextBatch& renderContextBatch, const Mesh::DrawInfo& info)
{
ModelDraw(this, renderContextBatch.GetMainContext(), renderContextBatch, info);
}
bool Model::SetupLODs(const Span<int32>& meshesCountPerLod)
{
ScopeLock lock(Locker);