Merge all sectors into one mesh

This commit is contained in:
Magnus Norddahl 2023-01-06 02:58:10 +01:00
commit 71a78f2baf
6 changed files with 215 additions and 194 deletions

View file

@ -33,23 +33,23 @@ void Mesh::Draw(FRenderState& renderstate)
#endif
MeshApplyState origState;
origState.RenderStyle = renderstate.mRenderStyle;
origState.SpecialEffect = renderstate.mSpecialEffect;
origState.TextureEnabled = renderstate.mTextureEnabled;
origState.AlphaThreshold = renderstate.mAlphaThreshold;
origState.applyData.RenderStyle = renderstate.mRenderStyle;
origState.applyData.SpecialEffect = renderstate.mSpecialEffect;
origState.applyData.TextureEnabled = renderstate.mTextureEnabled;
origState.applyData.AlphaThreshold = renderstate.mAlphaThreshold;
origState.applyData.FogEnabled = renderstate.mFogEnabled;
origState.applyData.BrightmapEnabled = renderstate.mBrightmapEnabled;
origState.applyData.TextureClamp = renderstate.mTextureClamp;
origState.applyData.TextureMode = renderstate.mTextureMode;
origState.applyData.TextureModeFlags = renderstate.mTextureModeFlags;
origState.applyData.uLightDist = renderstate.mLightParms[0];
origState.applyData.uLightFactor = renderstate.mLightParms[1];
origState.applyData.uFogDensity = renderstate.mLightParms[2];
origState.applyData.uClipSplit[0] = renderstate.mClipSplit[0];
origState.applyData.uClipSplit[1] = renderstate.mClipSplit[1];
origState.uLightLevel = renderstate.mLightParms[3];
origState.streamData = renderstate.mStreamData;
origState.material = renderstate.mMaterial;
origState.FogEnabled = renderstate.mFogEnabled;
origState.BrightmapEnabled = renderstate.mBrightmapEnabled;
origState.TextureClamp = renderstate.mTextureClamp;
origState.TextureMode = renderstate.mTextureMode;
origState.TextureModeFlags = renderstate.mTextureModeFlags;
origState.uLightDist = renderstate.mLightParms[0];
origState.uLightFactor = renderstate.mLightParms[1];
origState.uFogDensity = renderstate.mLightParms[2];
origState.uLightLevel = renderstate.mLightParms[3];
origState.uClipSplit[0] = renderstate.mClipSplit[0];
origState.uClipSplit[1] = renderstate.mClipSplit[1];
int applyIndex = -1;
int depthFunc = -1;
@ -58,7 +58,7 @@ void Mesh::Draw(FRenderState& renderstate)
bool apply = applyIndex != cmd.ApplyIndex;
if (apply)
{
int newDepthFunc = mApplys[cmd.ApplyIndex].DepthFunc;
int newDepthFunc = mApplys[cmd.ApplyIndex].applyData.DepthFunc;
if (depthFunc != newDepthFunc)
{
depthFunc = newDepthFunc;
@ -86,7 +86,7 @@ void Mesh::Draw(FRenderState& renderstate)
bool apply = applyIndex != cmd.ApplyIndex;
if (apply)
{
int newDepthFunc = mApplys[cmd.ApplyIndex].DepthFunc;
int newDepthFunc = mApplys[cmd.ApplyIndex].applyData.DepthFunc;
if (depthFunc != newDepthFunc)
{
depthFunc = newDepthFunc;
@ -104,21 +104,21 @@ void Mesh::Draw(FRenderState& renderstate)
void Mesh::Apply(FRenderState& renderstate, const MeshApplyState& state)
{
renderstate.mRenderStyle = state.RenderStyle;
renderstate.mSpecialEffect = state.SpecialEffect;
renderstate.mTextureEnabled = state.TextureEnabled;
renderstate.mAlphaThreshold = state.AlphaThreshold;
renderstate.mRenderStyle = state.applyData.RenderStyle;
renderstate.mSpecialEffect = state.applyData.SpecialEffect;
renderstate.mTextureEnabled = state.applyData.TextureEnabled;
renderstate.mAlphaThreshold = state.applyData.AlphaThreshold;
renderstate.mFogEnabled = state.applyData.FogEnabled;
renderstate.mBrightmapEnabled = state.applyData.BrightmapEnabled;
renderstate.mTextureClamp = state.applyData.TextureClamp;
renderstate.mTextureMode = state.applyData.TextureMode;
renderstate.mTextureModeFlags = state.applyData.TextureModeFlags;
renderstate.mLightParms[0] = state.applyData.uLightDist;
renderstate.mLightParms[1] = state.applyData.uLightFactor;
renderstate.mLightParms[2] = state.applyData.uFogDensity;
renderstate.mClipSplit[0] = state.applyData.uClipSplit[0];
renderstate.mClipSplit[1] = state.applyData.uClipSplit[1];
renderstate.mLightParms[3] = state.uLightLevel;
renderstate.mStreamData = state.streamData;
renderstate.mMaterial = state.material;
renderstate.mFogEnabled = state.FogEnabled;
renderstate.mBrightmapEnabled = state.BrightmapEnabled;
renderstate.mTextureClamp = state.TextureClamp;
renderstate.mTextureMode = state.TextureMode;
renderstate.mTextureModeFlags = state.TextureModeFlags;
renderstate.mLightParms[0] = state.uLightDist;
renderstate.mLightParms[1] = state.uLightFactor;
renderstate.mLightParms[2] = state.uFogDensity;
renderstate.mLightParms[3] = state.uLightLevel;
renderstate.mClipSplit[0] = state.uClipSplit[0];
renderstate.mClipSplit[1] = state.uClipSplit[1];
}

View file

@ -27,8 +27,8 @@ void MeshBuilder::Draw(int dt, int index, int count, bool apply)
command.DrawType = dt;
command.Start = index;
command.Count = count;
command.ApplyIndex = mApplys.Size() - 1;
mDraws.Push(command);
command.ApplyIndex = -1;
mDrawLists->mDraws.Push(command);
}
void MeshBuilder::DrawIndexed(int dt, int index, int count, bool apply)
@ -40,8 +40,8 @@ void MeshBuilder::DrawIndexed(int dt, int index, int count, bool apply)
command.DrawType = dt;
command.Start = index;
command.Count = count;
command.ApplyIndex = mApplys.Size() - 1;
mIndexedDraws.Push(command);
command.ApplyIndex = -1;
mDrawLists->mIndexedDraws.Push(command);
}
void MeshBuilder::SetDepthFunc(int func)
@ -53,45 +53,54 @@ void MeshBuilder::Apply()
{
MeshApplyState state;
state.RenderStyle = mRenderStyle;
state.SpecialEffect = mSpecialEffect;
state.TextureEnabled = mTextureEnabled;
state.AlphaThreshold = mAlphaThreshold;
state.DepthFunc = mDepthFunc;
state.applyData.RenderStyle = mRenderStyle;
state.applyData.SpecialEffect = mSpecialEffect;
state.applyData.TextureEnabled = mTextureEnabled;
state.applyData.AlphaThreshold = mAlphaThreshold;
state.applyData.DepthFunc = mDepthFunc;
state.applyData.FogEnabled = mFogEnabled;
state.applyData.BrightmapEnabled = mBrightmapEnabled;
state.applyData.TextureClamp = mTextureClamp;
state.applyData.TextureMode = mTextureMode;
state.applyData.TextureModeFlags = mTextureModeFlags;
state.applyData.uLightDist = mLightParms[0];
state.applyData.uLightFactor = mLightParms[1];
state.applyData.uFogDensity = mLightParms[2];
state.applyData.uClipSplit = { mClipSplit[0], mClipSplit[1] };
state.uLightLevel = mLightParms[3];
state.streamData = mStreamData;
state.material = mMaterial;
state.FogEnabled = mFogEnabled;
state.BrightmapEnabled = mBrightmapEnabled;
state.TextureClamp = mTextureClamp;
state.TextureMode = mTextureMode;
state.TextureModeFlags = mTextureModeFlags;
state.streamData.uVertexNormal = FVector4(0.0f, 0.0f, 0.0f, 0.0f); // Grr, this should be part of the vertex!!
state.uLightDist = mLightParms[0];
state.uLightFactor = mLightParms[1];
state.uFogDensity = mLightParms[2];
state.uLightLevel = mLightParms[3];
state.uClipSplit = { mClipSplit[0], mClipSplit[1] };
mApplys.Push(state);
mDrawLists = &mSortedLists[state];
}
std::unique_ptr<Mesh> MeshBuilder::Create()
{
if (mDraws.Size() == 0 && mIndexedDraws.Size() == 0)
if (mSortedLists.empty())
return {};
auto mesh = std::make_unique<Mesh>();
mesh->mApplys = std::move(mApplys);
mesh->mDraws = std::move(mDraws);
mesh->mIndexedDraws = std::move(mIndexedDraws);
mesh->mVertices = std::move(mVertices);
mApplys.Clear();
mDraws.Clear();
mIndexedDraws.Clear();
int applyIndex = 0;
for (auto& it : mSortedLists)
{
mesh->mApplys.Push(it.first);
for (MeshDrawCommand& command : it.second.mDraws)
{
command.ApplyIndex = applyIndex;
mesh->mDraws.Push(command);
}
for (MeshDrawCommand& command : it.second.mIndexedDraws)
{
command.ApplyIndex = applyIndex;
mesh->mIndexedDraws.Push(command);
}
applyIndex++;
}
mesh->mVertices = std::move(mVertices);
mVertices.Clear();
return mesh;

View file

@ -3,33 +3,55 @@
#include "hw_renderstate.h"
#include "hw_material.h"
#include "flatvertices.h"
#include <map>
class Mesh;
class MeshApplyState
{
public:
FRenderStyle RenderStyle;
int SpecialEffect;
bool TextureEnabled;
float AlphaThreshold;
int DepthFunc;
struct ApplyData
{
FRenderStyle RenderStyle;
int SpecialEffect;
int TextureEnabled;
float AlphaThreshold;
int DepthFunc;
int FogEnabled;
int BrightmapEnabled;
int TextureClamp;
int TextureMode;
int TextureModeFlags;
float uFogDensity;
float uLightFactor;
float uLightDist;
FVector2 uClipSplit;
};
float uLightLevel;
ApplyData applyData;
StreamData streamData;
FMaterialState material;
uint8_t FogEnabled;
uint8_t BrightmapEnabled;
int TextureClamp;
int TextureMode;
int TextureModeFlags;
bool operator<(const MeshApplyState& other) const
{
if (material.mMaterial != other.material.mMaterial)
return material.mMaterial < other.material.mMaterial;
if (material.mClampMode != other.material.mClampMode)
return material.mClampMode < other.material.mClampMode;
if (material.mTranslation != other.material.mTranslation)
return material.mTranslation < other.material.mTranslation;
if (material.mOverrideShader != other.material.mOverrideShader)
return material.mOverrideShader < other.material.mOverrideShader;
float uLightLevel;
float uFogDensity;
float uLightFactor;
float uLightDist;
int result = memcmp(&applyData, &other.applyData, sizeof(ApplyData));
if (result != 0)
return result < 0;
FVector2 uClipSplit;
result = memcmp(&streamData, &other.streamData, sizeof(StreamData));
return result < 0;
}
};
class MeshDrawCommand
@ -79,9 +101,14 @@ public:
private:
void Apply();
TArray<MeshApplyState> mApplys;
TArray<MeshDrawCommand> mDraws;
TArray<MeshDrawCommand> mIndexedDraws;
struct DrawLists
{
TArray<MeshDrawCommand> mDraws;
TArray<MeshDrawCommand> mIndexedDraws;
};
std::map<MeshApplyState, DrawLists> mSortedLists;
DrawLists* mDrawLists = nullptr;
TArray<FFlatVertex> mVertices;
int mDepthFunc = 0;
};