Optimize PrevWorld updating during scene rendering

This commit is contained in:
Wojtek Figat
2022-11-03 00:08:30 +01:00
parent 949f16fba8
commit b2a9ee495a
4 changed files with 37 additions and 63 deletions

View File

@@ -85,7 +85,7 @@ void Foliage::AddToCluster(ChunkedArray<FoliageCluster, FOLIAGE_CLUSTER_CHUNKS_S
void Foliage::DrawInstance(RenderContext& renderContext, FoliageInstance& instance, FoliageType& type, Model* model, int32 lod, float lodDitherFactor, DrawCallsList* drawCallsLists, BatchedDrawCalls& result) const void Foliage::DrawInstance(RenderContext& renderContext, FoliageInstance& instance, FoliageType& type, Model* model, int32 lod, float lodDitherFactor, DrawCallsList* drawCallsLists, BatchedDrawCalls& result) const
{ {
const auto& meshes = model->LODs[lod].Meshes; const auto& meshes = model->LODs.Get()[lod].Meshes;
for (int32 meshIndex = 0; meshIndex < meshes.Count(); meshIndex++) for (int32 meshIndex = 0; meshIndex < meshes.Count(); meshIndex++)
{ {
auto& drawCall = drawCallsLists[lod][meshIndex]; auto& drawCall = drawCallsLists[lod][meshIndex];
@@ -94,7 +94,7 @@ void Foliage::DrawInstance(RenderContext& renderContext, FoliageInstance& instan
DrawKey key; DrawKey key;
key.Mat = drawCall.DrawCall.Material; key.Mat = drawCall.DrawCall.Material;
key.Geo = &meshes[meshIndex]; key.Geo = &meshes.Get()[meshIndex];
key.Lightmap = instance.Lightmap.TextureIndex; key.Lightmap = instance.Lightmap.TextureIndex;
auto* e = result.TryGet(key); auto* e = result.TryGet(key);
if (!e) if (!e)
@@ -108,7 +108,8 @@ void Foliage::DrawInstance(RenderContext& renderContext, FoliageInstance& instan
auto& instanceData = e->Instances.AddOne(); auto& instanceData = e->Instances.AddOne();
Matrix world; Matrix world;
const Transform transform = _transform.LocalToWorld(instance.Transform); const Transform transform = _transform.LocalToWorld(instance.Transform);
renderContext.View.GetWorldMatrix(transform, world); const Float3 translation = transform.Translation - renderContext.View.Origin;
Matrix::Transformation(transform.Scale, transform.Orientation, translation, world);
instanceData.InstanceOrigin = Float3(world.M41, world.M42, world.M43); instanceData.InstanceOrigin = Float3(world.M41, world.M42, world.M43);
instanceData.PerInstanceRandom = instance.Random; instanceData.PerInstanceRandom = instance.Random;
instanceData.InstanceTransform1 = Float3(world.M11, world.M12, world.M13); instanceData.InstanceTransform1 = Float3(world.M11, world.M12, world.M13);
@@ -154,7 +155,7 @@ void Foliage::DrawCluster(RenderContext& renderContext, FoliageCluster* cluster,
const auto model = type.Model.Get(); const auto model = type.Model.Get();
for (int32 i = 0; i < cluster->Instances.Count(); i++) for (int32 i = 0; i < cluster->Instances.Count(); i++)
{ {
auto& instance = *cluster->Instances[i]; auto& instance = *cluster->Instances.Get()[i];
BoundingSphere sphere = instance.Bounds; BoundingSphere sphere = instance.Bounds;
sphere.Center -= viewOrigin; sphere.Center -= viewOrigin;
if (Float3::Distance(renderContext.View.Position, sphere.Center) - (float)sphere.Radius < instance.CullDistance && if (Float3::Distance(renderContext.View.Position, sphere.Center) - (float)sphere.Radius < instance.CullDistance &&
@@ -294,7 +295,8 @@ void Foliage::DrawCluster(RenderContext& renderContext, FoliageCluster* cluster,
{ {
Matrix world; Matrix world;
const Transform transform = _transform.LocalToWorld(instance.Transform); const Transform transform = _transform.LocalToWorld(instance.Transform);
renderContext.View.GetWorldMatrix(transform, world); const Float3 translation = transform.Translation - renderContext.View.Origin;
Matrix::Transformation(transform.Scale, transform.Orientation, translation, world);
// Disable motion blur // Disable motion blur
instance.DrawState.PrevWorld = world; instance.DrawState.PrevWorld = world;

View File

@@ -699,45 +699,38 @@ void AnimatedModel::Update()
void AnimatedModel::Draw(RenderContext& renderContext) void AnimatedModel::Draw(RenderContext& renderContext)
{ {
if (!SkinnedModel || !SkinnedModel->IsLoaded())
return;
if (renderContext.View.Pass == DrawPass::GlobalSDF) if (renderContext.View.Pass == DrawPass::GlobalSDF)
return; // TODO: Animated Model rendering to Global SDF return; // TODO: Animated Model rendering to Global SDF
if (renderContext.View.Pass == DrawPass::GlobalSurfaceAtlas) if (renderContext.View.Pass == DrawPass::GlobalSurfaceAtlas)
return; // No supported return; // No supported
Matrix world; Matrix world;
renderContext.View.GetWorldMatrix(_transform, world); const Float3 translation = _transform.Translation - renderContext.View.Origin;
Matrix::Transformation(_transform.Scale, _transform.Orientation, translation, world);
GEOMETRY_DRAW_STATE_EVENT_BEGIN(_drawState, world); GEOMETRY_DRAW_STATE_EVENT_BEGIN(_drawState, world);
if (SkinnedModel && SkinnedModel->IsLoaded()) _lastMinDstSqr = Math::Min(_lastMinDstSqr, Vector3::DistanceSquared(_transform.Translation, renderContext.View.Position + renderContext.View.Origin));
if (_skinningData.IsReady())
{ {
_lastMinDstSqr = Math::Min(_lastMinDstSqr, Vector3::DistanceSquared(_transform.Translation, renderContext.View.Position + renderContext.View.Origin)); _skinningData.Flush(GPUDevice::Instance->GetMainContext());
if (_skinningData.IsReady()) SkinnedMesh::DrawInfo draw;
{ draw.Buffer = &Entries;
#if USE_EDITOR draw.Skinning = &_skinningData;
// Disable motion blur effects in editor without play mode enabled to hide minor artifacts on objects moving draw.BlendShapes = &_blendShapes;
if (!Editor::IsPlayMode) draw.World = &world;
_drawState.PrevWorld = world; draw.DrawState = &_drawState;
#endif PRAGMA_DISABLE_DEPRECATION_WARNINGS
draw.DrawModes = (DrawPass)(DrawModes & renderContext.View.GetShadowsDrawPassMask(ShadowsMode));
PRAGMA_ENABLE_DEPRECATION_WARNINGS
draw.Bounds = _sphere;
draw.Bounds.Center -= renderContext.View.Origin;
draw.PerInstanceRandom = GetPerInstanceRandom();
draw.LODBias = LODBias;
draw.ForcedLOD = ForcedLOD;
_skinningData.Flush(GPUDevice::Instance->GetMainContext()); SkinnedModel->Draw(renderContext, draw);
SkinnedMesh::DrawInfo draw;
draw.Buffer = &Entries;
draw.Skinning = &_skinningData;
draw.BlendShapes = &_blendShapes;
draw.World = &world;
draw.DrawState = &_drawState;
PRAGMA_DISABLE_DEPRECATION_WARNINGS
draw.DrawModes = (DrawPass)(DrawModes & renderContext.View.GetShadowsDrawPassMask(ShadowsMode));
PRAGMA_ENABLE_DEPRECATION_WARNINGS
draw.Bounds = _sphere;
draw.Bounds.Center -= renderContext.View.Origin;
draw.PerInstanceRandom = GetPerInstanceRandom();
draw.LODBias = LODBias;
draw.ForcedLOD = ForcedLOD;
SkinnedModel->Draw(renderContext, draw);
}
} }
GEOMETRY_DRAW_STATE_EVENT_END(_drawState, world); GEOMETRY_DRAW_STATE_EVENT_END(_drawState, world);
@@ -749,17 +742,13 @@ void AnimatedModel::Draw(RenderContextBatch& renderContextBatch)
return; return;
const RenderContext& renderContext = renderContextBatch.GetMainContext(); const RenderContext& renderContext = renderContextBatch.GetMainContext();
Matrix world; Matrix world;
renderContext.View.GetWorldMatrix(_transform, world); const Float3 translation = _transform.Translation - renderContext.View.Origin;
Matrix::Transformation(_transform.Scale, _transform.Orientation, translation, world);
GEOMETRY_DRAW_STATE_EVENT_BEGIN(_drawState, world); GEOMETRY_DRAW_STATE_EVENT_BEGIN(_drawState, world);
_lastMinDstSqr = Math::Min(_lastMinDstSqr, Vector3::DistanceSquared(_transform.Translation, renderContext.View.Position + renderContext.View.Origin)); _lastMinDstSqr = Math::Min(_lastMinDstSqr, Vector3::DistanceSquared(_transform.Translation, renderContext.View.Position + renderContext.View.Origin));
if (_skinningData.IsReady()) if (_skinningData.IsReady())
{ {
#if USE_EDITOR
// Disable motion blur effects in editor without play mode enabled to hide minor artifacts on objects moving
if (!Editor::IsPlayMode)
_drawState.PrevWorld = world;
#endif
_skinningData.Flush(GPUDevice::Instance->GetMainContext()); _skinningData.Flush(GPUDevice::Instance->GetMainContext());
SkinnedMesh::DrawInfo draw; SkinnedMesh::DrawInfo draw;
@@ -792,6 +781,7 @@ void AnimatedModel::Draw(RenderContextBatch& renderContextBatch)
} }
PRAGMA_ENABLE_DEPRECATION_WARNINGS PRAGMA_ENABLE_DEPRECATION_WARNINGS
} }
GEOMETRY_DRAW_STATE_EVENT_END(_drawState, world); GEOMETRY_DRAW_STATE_EVENT_END(_drawState, world);
} }

View File

@@ -269,18 +269,12 @@ void StaticModel::Draw(RenderContext& renderContext)
return; return;
} }
Matrix world; Matrix world;
renderContext.View.GetWorldMatrix(_transform, world); const Float3 translation = _transform.Translation - renderContext.View.Origin;
Matrix::Transformation(_transform.Scale, _transform.Orientation, translation, world);
GEOMETRY_DRAW_STATE_EVENT_BEGIN(_drawState, world); GEOMETRY_DRAW_STATE_EVENT_BEGIN(_drawState, world);
if (_vertexColorsDirty) if (_vertexColorsDirty)
FlushVertexColors(); FlushVertexColors();
#if USE_EDITOR
// Disable motion blur effects in editor without play mode enabled to hide minor artifacts on objects moving
if (!Editor::IsPlayMode)
_drawState.PrevWorld = world;
#endif
Mesh::DrawInfo draw; Mesh::DrawInfo draw;
draw.Buffer = &Entries; draw.Buffer = &Entries;
draw.World = &world; draw.World = &world;
@@ -307,18 +301,12 @@ void StaticModel::Draw(RenderContextBatch& renderContextBatch)
return; return;
const RenderContext& renderContext = renderContextBatch.GetMainContext(); const RenderContext& renderContext = renderContextBatch.GetMainContext();
Matrix world; Matrix world;
renderContext.View.GetWorldMatrix(_transform, world); const Float3 translation = _transform.Translation - renderContext.View.Origin;
Matrix::Transformation(_transform.Scale, _transform.Orientation, translation, world);
GEOMETRY_DRAW_STATE_EVENT_BEGIN(_drawState, world); GEOMETRY_DRAW_STATE_EVENT_BEGIN(_drawState, world);
if (_vertexColorsDirty) if (_vertexColorsDirty)
FlushVertexColors(); FlushVertexColors();
#if USE_EDITOR
// Disable motion blur effects in editor without play mode enabled to hide minor artifacts on objects moving
if (!Editor::IsPlayMode)
_drawState.PrevWorld = world;
#endif
Mesh::DrawInfo draw; Mesh::DrawInfo draw;
draw.Buffer = &Entries; draw.Buffer = &Entries;
draw.World = &world; draw.World = &world;

View File

@@ -352,12 +352,6 @@ void TextRender::Draw(RenderContext& renderContext)
const DrawPass drawModes = (DrawPass)(DrawModes & renderContext.View.Pass & (uint32)renderContext.View.GetShadowsDrawPassMask(ShadowsMode)); const DrawPass drawModes = (DrawPass)(DrawModes & renderContext.View.Pass & (uint32)renderContext.View.GetShadowsDrawPassMask(ShadowsMode));
if (_vb0.Data.Count() > 0 && drawModes != DrawPass::None) if (_vb0.Data.Count() > 0 && drawModes != DrawPass::None)
{ {
#if USE_EDITOR
// Disable motion blur effects in editor without play mode enabled to hide minor artifacts on objects moving
if (!Editor::IsPlayMode)
_drawState.PrevWorld = world;
#endif
// Flush buffers // Flush buffers
if (_buffersDirty) if (_buffersDirty)
{ {