Optimize LightmapUVs to be stored already as Half4 to avoid runtime conversions during rendering
This commit is contained in:
@@ -57,6 +57,15 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Rectangle"/> struct.
|
||||
/// </summary>
|
||||
/// <param name="packed">The location and size packed.</param>
|
||||
Rectangle(const Float4& packed)
|
||||
{
|
||||
Platform::MemoryCopy(this, &packed, sizeof(*this));
|
||||
}
|
||||
|
||||
public:
|
||||
String ToString() const;
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "FoliageCluster.h"
|
||||
#include "Engine/Core/Log.h"
|
||||
#include "Engine/Core/Random.h"
|
||||
#include "Engine/Core/Math/Rectangle.h"
|
||||
#include "Engine/Core/Collections/BitArray.h"
|
||||
#include "Engine/Engine/Engine.h"
|
||||
#include "Engine/Graphics/Graphics.h"
|
||||
@@ -17,6 +18,7 @@
|
||||
#endif
|
||||
#include "Engine/Level/SceneQuery.h"
|
||||
#include "Engine/Profiler/ProfilerCPU.h"
|
||||
#include "Engine/Profiler/ProfilerMemory.h"
|
||||
#include "Engine/Renderer/RenderList.h"
|
||||
#include "Engine/Renderer/GlobalSignDistanceFieldPass.h"
|
||||
#include "Engine/Renderer/GI/GlobalSurfaceAtlasPass.h"
|
||||
@@ -140,7 +142,7 @@ void Foliage::DrawInstance(DrawContext& context, FoliageInstance& instance, Mode
|
||||
DrawKey key;
|
||||
key.Mat = drawCall.Material;
|
||||
key.Geo = &meshes.Get()[meshIndex];
|
||||
key.Lightmap = instance.Lightmap.TextureIndex;
|
||||
key.Lightmap = instance.LightmapTextureIndex;
|
||||
auto* e = result.TryGet(key);
|
||||
if (!e)
|
||||
{
|
||||
@@ -158,7 +160,7 @@ void Foliage::DrawInstance(DrawContext& context, FoliageInstance& instance, Mode
|
||||
const Float3 translation = transform.Translation - context.ViewOrigin;
|
||||
Matrix::Transformation(transform.Scale, transform.Orientation, translation, world);
|
||||
constexpr float worldDeterminantSign = 1.0f;
|
||||
instanceData.Store(world, world, instance.Lightmap.UVsArea, drawCall.Surface.GeometrySize, instance.Random, worldDeterminantSign, lodDitherFactor);
|
||||
instanceData.Store(world, world, instance.LightmapUVsArea, drawCall.Surface.GeometrySize, instance.Random, worldDeterminantSign, lodDitherFactor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -351,7 +353,7 @@ void Foliage::DrawCluster(DrawContext& context, FoliageCluster* cluster, Mesh::D
|
||||
instance.DrawState.PrevWorld = world;
|
||||
|
||||
// Draw model
|
||||
draw.Lightmap = _scene->LightmapsData.GetReadyLightmap(instance.Lightmap.TextureIndex);
|
||||
draw.Lightmap = _scene->LightmapsData.GetReadyLightmap(instance.LightmapTextureIndex);
|
||||
draw.LightmapUVs = &instance.Lightmap.UVsArea;
|
||||
draw.Buffer = &type.Entries;
|
||||
draw.World = &world;
|
||||
@@ -1267,8 +1269,8 @@ void Foliage::Draw(RenderContext& renderContext)
|
||||
draw.ForcedLOD = -1;
|
||||
draw.SortOrder = 0;
|
||||
draw.VertexColors = nullptr;
|
||||
draw.Lightmap = _scene ? _scene->LightmapsData.GetReadyLightmap(instance.Lightmap.TextureIndex) : nullptr;
|
||||
draw.LightmapUVs = &instance.Lightmap.UVsArea;
|
||||
draw.Lightmap = _scene ? _scene->LightmapsData.GetReadyLightmap(instance.LightmapTextureIndex) : nullptr;
|
||||
draw.LightmapUVs = &instance.LightmapUVsArea;
|
||||
draw.Buffer = &type.Entries;
|
||||
draw.World = &world;
|
||||
draw.DrawState = &instance.DrawState;
|
||||
@@ -1367,7 +1369,8 @@ struct InstanceEncoded2
|
||||
Float3 Translation;
|
||||
Quaternion Orientation;
|
||||
Float3 Scale;
|
||||
LightmapEntry Lightmap;
|
||||
int32 LightmapTextureIndex;
|
||||
Rectangle LightmapUVsArea;
|
||||
|
||||
static const int32 Size = 68;
|
||||
static const int32 Base64Size = GetInstanceBase64Size(Size);
|
||||
@@ -1435,7 +1438,10 @@ struct FoliageWriter
|
||||
|
||||
// Lightmap data (if used)
|
||||
if (data.HasLightmap)
|
||||
Memory.Write(instance.Lightmap);
|
||||
{
|
||||
Memory.Write(instance.LightmapTextureIndex);
|
||||
Memory.Write(instance.LightmapUVsArea);
|
||||
}
|
||||
|
||||
// Move to the next instance
|
||||
InstanceCounter++;
|
||||
@@ -1519,7 +1525,10 @@ struct FoliageReader
|
||||
q.Normalize();
|
||||
instance.Transform.Orientation = q;
|
||||
if (data.HasLightmap)
|
||||
memory.Read(instance.Lightmap);
|
||||
{
|
||||
memory.Read(instance.LightmapTextureIndex);
|
||||
memory.Read(instance.LightmapUVsArea);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1656,7 +1665,7 @@ void Foliage::Deserialize(DeserializeStream& stream, ISerializeModifier* modifie
|
||||
instance.Transform.Translation = enc.Translation;
|
||||
instance.Transform.Orientation = enc.Orientation;
|
||||
instance.Transform.Scale = enc.Scale;
|
||||
instance.Lightmap = LightmapEntry();
|
||||
instance.LightmapTextureIndex = -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -1680,7 +1689,8 @@ void Foliage::Deserialize(DeserializeStream& stream, ISerializeModifier* modifie
|
||||
instance.Transform.Translation = enc.Translation;
|
||||
instance.Transform.Orientation = enc.Orientation;
|
||||
instance.Transform.Scale = enc.Scale;
|
||||
instance.Lightmap = enc.Lightmap;
|
||||
instance.LightmapTextureIndex = enc.LightmapTextureIndex;
|
||||
instance.LightmapUVsArea = Half4(enc.LightmapUVsArea);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "Engine/Core/Math/Transform.h"
|
||||
#include "Engine/Core/Math/BoundingSphere.h"
|
||||
#include "Engine/Core/Math/Half.h"
|
||||
#include "Engine/Renderer/DrawCall.h"
|
||||
#include "Engine/Level/Scene/Lightmap.h"
|
||||
|
||||
@@ -29,6 +30,11 @@ API_STRUCT(NoPod) struct FLAXENGINE_API FoliageInstance
|
||||
/// </summary>
|
||||
API_FIELD() int32 Type;
|
||||
|
||||
/// <summary>
|
||||
/// The lightmap index for the foliage instance. -1 if unused.
|
||||
/// </summary>
|
||||
int8 LightmapTextureIndex;
|
||||
|
||||
/// <summary>
|
||||
/// The per-instance random value from range [0;1].
|
||||
/// </summary>
|
||||
@@ -45,9 +51,9 @@ API_STRUCT(NoPod) struct FLAXENGINE_API FoliageInstance
|
||||
API_FIELD() BoundingSphere Bounds;
|
||||
|
||||
/// <summary>
|
||||
/// The lightmap entry for the foliage instance.
|
||||
/// Lightmap UVs area that entry occupies (packed Rectangle into Half4).
|
||||
/// </summary>
|
||||
LightmapEntry Lightmap;
|
||||
Half4 LightmapUVsArea;
|
||||
|
||||
public:
|
||||
bool operator==(const FoliageInstance& v) const
|
||||
@@ -60,7 +66,7 @@ public:
|
||||
/// </summary>
|
||||
FORCE_INLINE bool HasLightmap() const
|
||||
{
|
||||
return Lightmap.TextureIndex != INVALID_INDEX;
|
||||
return LightmapTextureIndex != INVALID_INDEX;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -68,6 +74,6 @@ public:
|
||||
/// </summary>
|
||||
FORCE_INLINE void RemoveLightmap()
|
||||
{
|
||||
Lightmap.TextureIndex = INVALID_INDEX;
|
||||
LightmapTextureIndex = INVALID_INDEX;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -305,7 +305,7 @@ void Mesh::Draw(const RenderContext& renderContext, const DrawInfo& info, float
|
||||
drawCall.Surface.GeometrySize = _box.GetSize();
|
||||
drawCall.Surface.PrevWorld = info.DrawState->PrevWorld;
|
||||
drawCall.Surface.Lightmap = (info.Flags & StaticFlags::Lightmap) != StaticFlags::None ? info.Lightmap : nullptr;
|
||||
drawCall.Surface.LightmapUVsArea = info.LightmapUVs ? *info.LightmapUVs : Rectangle::Empty;
|
||||
drawCall.Surface.LightmapUVsArea = info.LightmapUVs ? *info.LightmapUVs : Half4::Zero;
|
||||
drawCall.Surface.LODDitherFactor = lodDitherFactor;
|
||||
drawCall.PerInstanceRandom = info.PerInstanceRandom;
|
||||
drawCall.StencilValue = info.StencilValue;
|
||||
@@ -368,7 +368,7 @@ void Mesh::Draw(const RenderContextBatch& renderContextBatch, const DrawInfo& in
|
||||
drawCall.Surface.GeometrySize = _box.GetSize();
|
||||
drawCall.Surface.PrevWorld = info.DrawState->PrevWorld;
|
||||
drawCall.Surface.Lightmap = (info.Flags & StaticFlags::Lightmap) != StaticFlags::None ? info.Lightmap : nullptr;
|
||||
drawCall.Surface.LightmapUVsArea = info.LightmapUVs ? *info.LightmapUVs : Rectangle::Empty;
|
||||
drawCall.Surface.LightmapUVsArea = info.LightmapUVs ? *info.LightmapUVs : Half4::Zero;
|
||||
drawCall.Surface.LODDitherFactor = lodDitherFactor;
|
||||
drawCall.PerInstanceRandom = info.PerInstanceRandom;
|
||||
drawCall.StencilValue = info.StencilValue;
|
||||
|
||||
@@ -375,7 +375,7 @@ public:
|
||||
/// <summary>
|
||||
/// The lightmap UVs.
|
||||
/// </summary>
|
||||
const Rectangle* LightmapUVs;
|
||||
const Half4* LightmapUVs;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright (c) Wojciech Figat. All rights reserved.
|
||||
|
||||
#include "StaticModel.h"
|
||||
#include "Engine/Core/Math/Rectangle.h"
|
||||
#include "Engine/Engine/Engine.h"
|
||||
#include "Engine/Content/Deprecated.h"
|
||||
#include "Engine/Graphics/GPUBuffer.h"
|
||||
@@ -20,6 +21,31 @@
|
||||
#include "Editor/Editor.h"
|
||||
#endif
|
||||
|
||||
void LightmapEntry::Serialize(ISerializable::SerializeStream& stream)
|
||||
{
|
||||
stream.JKEY("LightmapIndex");
|
||||
stream.Int(TextureIndex);
|
||||
|
||||
stream.JKEY("LightmapArea");
|
||||
stream.Rectangle(UVsArea.ToFloat4());
|
||||
}
|
||||
|
||||
void LightmapEntry::Deserialize(ISerializable::DeserializeStream& stream)
|
||||
{
|
||||
ISerializeModifier* modifier = nullptr;
|
||||
|
||||
DESERIALIZE_MEMBER(LightmapIndex, TextureIndex);
|
||||
{
|
||||
const auto e = SERIALIZE_FIND_MEMBER(stream, "LightmapArea");
|
||||
if (e != stream.MemberEnd())
|
||||
{
|
||||
Rectangle rect;
|
||||
Serialization::Deserialize(e->value, rect, nullptr);
|
||||
UVsArea = Half4(rect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StaticModel::StaticModel(const SpawnParams& params)
|
||||
: ModelInstanceActor(params)
|
||||
, _scaleInLightmap(1.0f)
|
||||
@@ -457,11 +483,7 @@ void StaticModel::Serialize(SerializeStream& stream, const void* otherObj)
|
||||
#endif
|
||||
)
|
||||
{
|
||||
stream.JKEY("LightmapIndex");
|
||||
stream.Int(Lightmap.TextureIndex);
|
||||
|
||||
stream.JKEY("LightmapArea");
|
||||
stream.Rectangle(Lightmap.UVsArea);
|
||||
Lightmap.Serialize(stream);
|
||||
}
|
||||
|
||||
stream.JKEY("Buffer");
|
||||
@@ -502,9 +524,7 @@ void StaticModel::Deserialize(DeserializeStream& stream, ISerializeModifier* mod
|
||||
DESERIALIZE_MEMBER(ForcedLOD, _forcedLod);
|
||||
DESERIALIZE_MEMBER(SortOrder, _sortOrder);
|
||||
DESERIALIZE_MEMBER(DrawModes, _drawModes);
|
||||
DESERIALIZE_MEMBER(LightmapIndex, Lightmap.TextureIndex);
|
||||
DESERIALIZE_MEMBER(LightmapArea, Lightmap.UVsArea);
|
||||
|
||||
Lightmap.Deserialize(stream);
|
||||
Entries.DeserializeIfExists(stream, "Buffer", modifier);
|
||||
|
||||
{
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Config.h"
|
||||
#include "Engine/Core/Math/Rectangle.h"
|
||||
#include "Engine/Core/Math/Half.h"
|
||||
#include "Engine/Core/Math/Color.h"
|
||||
|
||||
struct RenderView;
|
||||
@@ -183,13 +183,13 @@ struct DrawCall
|
||||
struct
|
||||
{
|
||||
const Lightmap* Lightmap;
|
||||
Rectangle LightmapUVsArea;
|
||||
Half4 LightmapUVsArea;
|
||||
} Features;
|
||||
|
||||
struct
|
||||
{
|
||||
const Lightmap* Lightmap;
|
||||
Rectangle LightmapUVsArea;
|
||||
Half4 LightmapUVsArea;
|
||||
SkinnedMeshDrawData* Skinning;
|
||||
Float3 GeometrySize; // Object geometry size in the world (unscaled).
|
||||
float LODDitherFactor; // The model LOD transition dither progress.
|
||||
@@ -199,7 +199,7 @@ struct DrawCall
|
||||
struct
|
||||
{
|
||||
const Lightmap* Lightmap;
|
||||
Rectangle LightmapUVsArea;
|
||||
Half4 LightmapUVsArea;
|
||||
Float4 HeightmapUVScaleBias;
|
||||
Float4 NeighborLOD;
|
||||
Float2 OffsetUV;
|
||||
|
||||
@@ -102,7 +102,7 @@ void LightmapUVsDensityMaterialShader::Bind(BindParameters& params)
|
||||
scaleZ > 0.00001f ? 1.0f / scaleZ : 0.0f);
|
||||
data.LightmapTexelsPerWorldUnit = ShadowsOfMordor::LightmapTexelsPerWorldUnit;
|
||||
data.LightmapSize = 1024.0f;
|
||||
data.LightmapArea = drawCall.Surface.LightmapUVsArea;
|
||||
data.LightmapArea = drawCall.Surface.LightmapUVsArea.ToFloat4();
|
||||
const ModelLOD* drawCallModelLod;
|
||||
float scaleInLightmap = drawCall.Surface.LODDitherFactor; // Reuse field
|
||||
if (scaleInLightmap < 0.0f)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Engine/Core/Types/Guid.h"
|
||||
#include "Engine/Core/Math/Rectangle.h"
|
||||
#include "Engine/Core/Math/Half.h"
|
||||
#include "Engine/Core/ISerializable.h"
|
||||
|
||||
#if USE_EDITOR
|
||||
@@ -42,21 +42,24 @@ struct LightmapEntry
|
||||
/// <summary>
|
||||
/// Index of the lightmap
|
||||
/// </summary>
|
||||
int32 TextureIndex;
|
||||
int16 TextureIndex;
|
||||
|
||||
/// <summary>
|
||||
/// Lightmap UVs area that entry occupies
|
||||
/// </summary>
|
||||
Rectangle UVsArea;
|
||||
Half4 UVsArea;
|
||||
|
||||
/// <summary>
|
||||
/// Init
|
||||
/// </summary>
|
||||
LightmapEntry()
|
||||
: TextureIndex(INVALID_INDEX)
|
||||
, UVsArea(Rectangle::Empty)
|
||||
, UVsArea(Half4::Zero)
|
||||
{
|
||||
}
|
||||
|
||||
void Serialize(ISerializable::SerializeStream& stream);
|
||||
void Deserialize(ISerializable::DeserializeStream& stream);
|
||||
};
|
||||
|
||||
template<>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright (c) Wojciech Figat. All rights reserved.
|
||||
|
||||
#include "RenderList.h"
|
||||
#include "Engine/Core/Log.h"
|
||||
#include "Engine/Core/Math/Half.h"
|
||||
#include "Engine/Core/Collections/Sorting.h"
|
||||
#include "Engine/Graphics/Materials/IMaterial.h"
|
||||
#include "Engine/Graphics/Materials/MaterialShader.h"
|
||||
@@ -12,11 +14,9 @@
|
||||
#include "Engine/Graphics/RenderTools.h"
|
||||
#include "Engine/Graphics/Graphics.h"
|
||||
#include "Engine/Graphics/PostProcessEffect.h"
|
||||
#include "Engine/Graphics/Shaders/GPUVertexLayout.h"
|
||||
#include "Engine/Profiler/Profiler.h"
|
||||
#include "Engine/Content/Assets/CubeTexture.h"
|
||||
#include "Engine/Core/Log.h"
|
||||
#include "Engine/Core/Math/Half.h"
|
||||
#include "Engine/Graphics/Shaders/GPUVertexLayout.h"
|
||||
#include "Engine/Level/Scene/Lightmap.h"
|
||||
#include "Engine/Level/Actors/PostFxVolume.h"
|
||||
|
||||
@@ -47,10 +47,9 @@ namespace
|
||||
}
|
||||
}
|
||||
|
||||
void ShaderObjectData::Store(const Matrix& worldMatrix, const Matrix& prevWorldMatrix, const Rectangle& lightmapUVsArea, const Float3& geometrySize, float perInstanceRandom, float worldDeterminantSign, float lodDitherFactor)
|
||||
void ShaderObjectData::Store(const Matrix& worldMatrix, const Matrix& prevWorldMatrix, const Half4& lightmapUVsArea, const Float3& geometrySize, float perInstanceRandom, float worldDeterminantSign, float lodDitherFactor)
|
||||
{
|
||||
Half4 lightmapUVsAreaPacked(*(Float4*)&lightmapUVsArea);
|
||||
Float2 lightmapUVsAreaPackedAliased = *(Float2*)&lightmapUVsAreaPacked;
|
||||
Float2 lightmapUVsAreaPackedAliased = *(Float2*)&lightmapUVsArea;
|
||||
Raw[0] = Float4(worldMatrix.M11, worldMatrix.M12, worldMatrix.M13, worldMatrix.M41);
|
||||
Raw[1] = Float4(worldMatrix.M21, worldMatrix.M22, worldMatrix.M23, worldMatrix.M42);
|
||||
Raw[2] = Float4(worldMatrix.M31, worldMatrix.M32, worldMatrix.M33, worldMatrix.M43);
|
||||
@@ -62,7 +61,7 @@ void ShaderObjectData::Store(const Matrix& worldMatrix, const Matrix& prevWorldM
|
||||
// TODO: pack WorldDeterminantSign and LODDitherFactor
|
||||
}
|
||||
|
||||
void ShaderObjectData::Load(Matrix& worldMatrix, Matrix& prevWorldMatrix, Rectangle& lightmapUVsArea, Float3& geometrySize, float& perInstanceRandom, float& worldDeterminantSign, float& lodDitherFactor) const
|
||||
void ShaderObjectData::Load(Matrix& worldMatrix, Matrix& prevWorldMatrix, Half4& lightmapUVsArea, Float3& geometrySize, float& perInstanceRandom, float& worldDeterminantSign, float& lodDitherFactor) const
|
||||
{
|
||||
worldMatrix.SetRow1(Float4(Float3(Raw[0]), 0.0f));
|
||||
worldMatrix.SetRow2(Float4(Float3(Raw[1]), 0.0f));
|
||||
@@ -77,8 +76,7 @@ void ShaderObjectData::Load(Matrix& worldMatrix, Matrix& prevWorldMatrix, Rectan
|
||||
worldDeterminantSign = Raw[7].X;
|
||||
lodDitherFactor = Raw[7].Y;
|
||||
Float2 lightmapUVsAreaPackedAliased(Raw[7].Z, Raw[7].W);
|
||||
Half4 lightmapUVsAreaPacked(*(Half4*)&lightmapUVsAreaPackedAliased);
|
||||
*(Float4*)&lightmapUVsArea = lightmapUVsAreaPacked.ToFloat4();
|
||||
lightmapUVsArea = *(Half4*)&lightmapUVsAreaPackedAliased;
|
||||
}
|
||||
|
||||
bool RenderLightData::CanRenderShadow(const RenderView& view) const
|
||||
|
||||
@@ -22,6 +22,7 @@ class IPostFxSettingsProvider;
|
||||
class CubeTexture;
|
||||
struct RenderContext;
|
||||
struct RenderContextBatch;
|
||||
struct Half4;
|
||||
|
||||
struct RenderLightData
|
||||
{
|
||||
@@ -683,8 +684,8 @@ GPU_CB_STRUCT(ShaderObjectData
|
||||
{
|
||||
Float4 Raw[8];
|
||||
|
||||
void FLAXENGINE_API Store(const Matrix& worldMatrix, const Matrix& prevWorldMatrix, const Rectangle& lightmapUVsArea, const Float3& geometrySize, float perInstanceRandom = 0.0f, float worldDeterminantSign = 1.0f, float lodDitherFactor = 0.0f);
|
||||
void FLAXENGINE_API Load(Matrix& worldMatrix, Matrix& prevWorldMatrix, Rectangle& lightmapUVsArea, Float3& geometrySize, float& perInstanceRandom, float& worldDeterminantSign, float& lodDitherFactor) const;
|
||||
void FLAXENGINE_API Store(const Matrix& worldMatrix, const Matrix& prevWorldMatrix, const Half4& lightmapUVsAreaPacked, const Float3& geometrySize, float perInstanceRandom = 0.0f, float worldDeterminantSign = 1.0f, float lodDitherFactor = 0.0f);
|
||||
void FLAXENGINE_API Load(Matrix& worldMatrix, Matrix& prevWorldMatrix, Half4& lightmapUVsArea, Float3& geometrySize, float& perInstanceRandom, float& worldDeterminantSign, float& lodDitherFactor) const;
|
||||
|
||||
FORCE_INLINE void Store(const DrawCall& drawCall)
|
||||
{
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace ShadowsOfMordor
|
||||
{
|
||||
Chart = chart;
|
||||
const float invSize = 1.0f / (int32)settings->AtlasSize;
|
||||
chart->Result.UVsArea = Rectangle(X * invSize, Y * invSize, chart->Width * invSize, chart->Height * invSize);
|
||||
chart->Result.UVsArea = Half4(X * invSize, Y * invSize, chart->Width * invSize, chart->Height * invSize);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -172,28 +172,24 @@ void ShadowsOfMordor::Builder::updateEntries()
|
||||
if (e.ChartIndex == INVALID_INDEX)
|
||||
{
|
||||
// Removed previously baked lightmap info
|
||||
LightmapEntry emptyEntry;
|
||||
switch (e.Type)
|
||||
{
|
||||
case GeometryType::StaticModel:
|
||||
{
|
||||
auto staticModel = e.AsStaticModel.Actor;
|
||||
if (staticModel)
|
||||
staticModel->Lightmap = emptyEntry;
|
||||
if (auto staticModel = e.AsStaticModel.Actor)
|
||||
staticModel->RemoveLightmap();
|
||||
}
|
||||
break;
|
||||
case GeometryType::Terrain:
|
||||
{
|
||||
auto terrain = e.AsTerrain.Actor;
|
||||
if (terrain)
|
||||
terrain->GetPatch(e.AsTerrain.PatchIndex)->Chunks[e.AsTerrain.ChunkIndex].Lightmap = emptyEntry;
|
||||
if (auto terrain = e.AsTerrain.Actor)
|
||||
terrain->GetPatch(e.AsTerrain.PatchIndex)->Chunks[e.AsTerrain.ChunkIndex].RemoveLightmap();
|
||||
}
|
||||
break;
|
||||
case GeometryType::Foliage:
|
||||
{
|
||||
auto foliage = e.AsFoliage.Actor;
|
||||
if (foliage)
|
||||
foliage->Instances[e.AsFoliage.InstanceIndex].Lightmap = emptyEntry;
|
||||
if (auto foliage = e.AsFoliage.Actor)
|
||||
foliage->Instances[e.AsFoliage.InstanceIndex].RemoveLightmap();
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -202,8 +198,10 @@ void ShadowsOfMordor::Builder::updateEntries()
|
||||
auto& chart = scene->Charts[e.ChartIndex];
|
||||
|
||||
// Update result uvs by taking into account lightmap uvs box
|
||||
chart.Result.UVsArea.Size /= e.UVsBox.Size;
|
||||
chart.Result.UVsArea.Location += e.UVsBox.Location * chart.Result.UVsArea.Size;
|
||||
Rectangle rect = chart.Result.UVsArea.ToFloat4();
|
||||
rect.Size /= e.UVsBox.Size;
|
||||
rect.Location += e.UVsBox.Location * rect.Size;
|
||||
chart.Result.UVsArea = Half4(rect);
|
||||
|
||||
switch (e.Type)
|
||||
{
|
||||
@@ -243,7 +241,9 @@ void ShadowsOfMordor::Builder::updateEntries()
|
||||
if (foliage)
|
||||
{
|
||||
// Update data
|
||||
foliage->Instances[e.AsFoliage.InstanceIndex].Lightmap = chart.Result;
|
||||
auto& instance = foliage->Instances[e.AsFoliage.InstanceIndex];
|
||||
instance.LightmapTextureIndex = (int8)chart.Result.TextureIndex;
|
||||
instance.LightmapUVsArea = chart.Result.UVsArea;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -127,7 +127,7 @@ void ShadowsOfMordor::Builder::onJobRender(GPUContext* context)
|
||||
Matrix worldMatrix;
|
||||
staticModel->GetLocalToWorldMatrix(worldMatrix);
|
||||
Matrix::Transpose(worldMatrix, shaderData.WorldMatrix);
|
||||
shaderData.LightmapArea = staticModel->Lightmap.UVsArea;
|
||||
shaderData.LightmapArea = staticModel->Lightmap.UVsArea.ToFloat4();
|
||||
|
||||
context->UpdateCB(cb, &shaderData);
|
||||
context->BindCB(0, cb);
|
||||
@@ -163,7 +163,7 @@ void ShadowsOfMordor::Builder::onJobRender(GPUContext* context)
|
||||
Matrix world;
|
||||
chunk->GetTransform().GetWorld(world);
|
||||
Matrix::Transpose(world, shaderData.WorldMatrix);
|
||||
shaderData.LightmapArea = chunk->Lightmap.UVsArea;
|
||||
shaderData.LightmapArea = chunk->Lightmap.UVsArea.ToFloat4();
|
||||
shaderData.TerrainChunkSizeLOD0 = TERRAIN_UNITS_PER_VERTEX * chunkSize;
|
||||
shaderData.HeightmapUVScaleBias = chunk->GetHeightmapUVScaleBias();
|
||||
|
||||
@@ -199,7 +199,7 @@ void ShadowsOfMordor::Builder::onJobRender(GPUContext* context)
|
||||
Matrix world;
|
||||
foliage->GetTransform().LocalToWorld(instance.Transform).GetWorld(world);
|
||||
Matrix::Transpose(world, shaderData.WorldMatrix);
|
||||
shaderData.LightmapArea = instance.Lightmap.UVsArea;
|
||||
shaderData.LightmapArea = instance.LightmapUVsArea.ToFloat4();
|
||||
|
||||
context->UpdateCB(cb, &shaderData);
|
||||
context->BindCB(0, cb);
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "Engine/Content/Assets/Model.h"
|
||||
#include "Engine/Content/Assets/Shader.h"
|
||||
#include "Engine/Core/Types/DateTime.h"
|
||||
#include "Engine/Core/Math/Rectangle.h"
|
||||
#include "Engine/CSG/CSGMesh.h"
|
||||
#include "Builder.Config.h"
|
||||
|
||||
|
||||
@@ -120,7 +120,7 @@ void TerrainChunk::Draw(const RenderContext& renderContext) const
|
||||
else
|
||||
{
|
||||
drawCall.Terrain.Lightmap = nullptr;
|
||||
drawCall.Terrain.LightmapUVsArea = Rectangle::Empty;
|
||||
drawCall.Terrain.LightmapUVsArea = Half4::Zero;
|
||||
}
|
||||
drawCall.PerInstanceRandom = _perInstanceRandom;
|
||||
drawCall.SetStencilValue(_patch->_terrain->GetLayer());
|
||||
@@ -181,7 +181,7 @@ void TerrainChunk::Draw(const RenderContext& renderContext, MaterialBase* materi
|
||||
else
|
||||
{
|
||||
drawCall.Terrain.Lightmap = nullptr;
|
||||
drawCall.Terrain.LightmapUVsArea = Rectangle::Empty;
|
||||
drawCall.Terrain.LightmapUVsArea = Half4::Zero;
|
||||
}
|
||||
drawCall.PerInstanceRandom = _perInstanceRandom;
|
||||
drawCall.SetStencilValue(_patch->_terrain->GetLayer());
|
||||
@@ -311,11 +311,7 @@ void TerrainChunk::Serialize(SerializeStream& stream, const void* otherObj)
|
||||
#endif
|
||||
)
|
||||
{
|
||||
stream.JKEY("LightmapIndex");
|
||||
stream.Int(Lightmap.TextureIndex);
|
||||
|
||||
stream.JKEY("LightmapArea");
|
||||
stream.Rectangle(Lightmap.UVsArea);
|
||||
Lightmap.Serialize(stream);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,6 +320,5 @@ void TerrainChunk::Deserialize(DeserializeStream& stream, ISerializeModifier* mo
|
||||
DESERIALIZE_MEMBER(Offset, _yOffset);
|
||||
DESERIALIZE_MEMBER(Height, _yHeight);
|
||||
DESERIALIZE_MEMBER(Material, OverrideMaterial);
|
||||
DESERIALIZE_MEMBER(LightmapIndex, Lightmap.TextureIndex);
|
||||
DESERIALIZE_MEMBER(LightmapArea, Lightmap.UVsArea);
|
||||
Lightmap.Deserialize(stream);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user