Change the matrix interface on RenderState so the backend doesn't have to try detect changes on each Apply

Include the texture matrix in the mesh builder
This commit is contained in:
Magnus Norddahl 2023-05-06 00:34:42 +02:00
commit eba0bee9f6
17 changed files with 126 additions and 136 deletions

View file

@ -42,6 +42,7 @@ void Mesh::Draw(FRenderState& renderstate)
origState.applyData.TextureModeFlags = renderstate.mTextureModeFlags;
origState.streamData = renderstate.mStreamData;
origState.material = renderstate.mMaterial;
origState.textureMatrix.loadIdentity();
int applyIndex = -1;
int depthFunc = -1;
@ -106,4 +107,5 @@ void Mesh::Apply(FRenderState& renderstate, const MeshApplyState& state)
renderstate.mTextureModeFlags = state.applyData.TextureModeFlags;
renderstate.mStreamData = state.streamData;
renderstate.mMaterial = state.material;
renderstate.SetTextureMatrix(state.textureMatrix);
}

View file

@ -69,6 +69,7 @@ void MeshBuilder::Apply()
state.applyData.TextureModeFlags = mTextureModeFlags;
state.streamData = mStreamData;
state.material = mMaterial;
state.textureMatrix = mTextureMatrix;
state.streamData.uVertexNormal = FVector4(0.0f, 0.0f, 0.0f, 0.0f); // Grr, this should be part of the vertex!!

View file

@ -27,6 +27,7 @@ public:
ApplyData applyData;
StreamData streamData;
FMaterialState material;
VSMatrix textureMatrix;
bool operator<(const MeshApplyState& other) const
{
@ -43,6 +44,10 @@ public:
if (result != 0)
return result < 0;
result = memcmp(&textureMatrix, &other.textureMatrix, sizeof(VSMatrix));
if (result != 0)
return result < 0;
result = memcmp(&streamData, &other.streamData, sizeof(StreamData));
return result < 0;
}
@ -68,6 +73,8 @@ public:
// Buffers
int SetViewpoint(const HWViewpointUniforms& vp) override { return 0; }
void SetViewpoint(int index) override { }
void SetModelMatrix(const VSMatrix& matrix, const VSMatrix& normalMatrix) override { }
void SetTextureMatrix(const VSMatrix& matrix) override { mTextureMatrix = matrix; }
int UploadLights(const FDynLightData& lightdata) override { return -1; }
int UploadBones(const TArray<VSMatrix>& bones) override { return -1; }
@ -109,4 +116,6 @@ private:
TArray<FFlatVertex> mVertices;
int mDepthFunc = 0;
VSMatrix mTextureMatrix = VSMatrix::identity();
};

View file

@ -228,8 +228,6 @@ protected:
uint8_t mTextureEnabled:1;
uint8_t mGlowEnabled : 1;
uint8_t mGradientEnabled : 1;
uint8_t mModelMatrixEnabled : 1;
uint8_t mTextureMatrixEnabled : 1;
uint8_t mSplitEnabled : 1;
uint8_t mBrightmapEnabled : 1;
@ -262,10 +260,6 @@ protected:
public:
uint64_t firstFrame = 0;
VSMatrix mModelMatrix;
VSMatrix mTextureMatrix;
public:
void Reset()
{
@ -278,8 +272,6 @@ public:
mTextureModeFlags = 0;
mStreamData.uDesaturationFactor = 0.0f;
mStreamData.uAlphaThreshold = 0.5f;
mModelMatrixEnabled = false;
mTextureMatrixEnabled = false;
mSplitEnabled = false;
mStreamData.uAddColor = 0;
mStreamData.uObjectColor = 0xffffffff;
@ -322,8 +314,6 @@ public:
#ifdef NPOT_EMULATION
mStreamData.uNpotEmulation = { 0,0,0,0 };
#endif
mModelMatrix.loadIdentity();
mTextureMatrix.loadIdentity();
ClearClipSplit();
}
@ -448,16 +438,6 @@ public:
mSplitEnabled = on;
}
void EnableModelMatrix(bool on)
{
mModelMatrixEnabled = on;
}
void EnableTextureMatrix(bool on)
{
mTextureMatrixEnabled = on;
}
void SetGlowParams(float *t, float *b)
{
mStreamData.uGlowTopColor = { t[0], t[1], t[2], t[3] };
@ -759,6 +739,8 @@ public:
// Buffers
virtual int SetViewpoint(const HWViewpointUniforms& vp) = 0;
virtual void SetViewpoint(int index) = 0;
virtual void SetModelMatrix(const VSMatrix& matrix, const VSMatrix& normalMatrix) = 0;
virtual void SetTextureMatrix(const VSMatrix& matrix) = 0;
virtual int UploadLights(const FDynLightData& lightdata) = 0;
virtual int UploadBones(const TArray<VSMatrix>& bones) = 0;

View file

@ -384,11 +384,12 @@ void FSkyVertexBuffer::CreateDome()
//
//-----------------------------------------------------------------------------
void FSkyVertexBuffer::SetupMatrices(FGameTexture *tex, float x_offset, float y_offset, bool mirror, int mode, VSMatrix &modelMatrix, VSMatrix &textureMatrix, bool tiled, float xscale, float yscale)
void FSkyVertexBuffer::SetupMatrices(FRenderState& state, FGameTexture *tex, float x_offset, float y_offset, bool mirror, int mode, bool tiled, float xscale, float yscale)
{
float texw = tex->GetDisplayWidth();
float texh = tex->GetDisplayHeight();
VSMatrix modelMatrix;
modelMatrix.loadIdentity();
modelMatrix.rotate(-180.0f + x_offset, 0.f, 1.f, 0.f);
@ -433,9 +434,16 @@ void FSkyVertexBuffer::SetupMatrices(FGameTexture *tex, float x_offset, float y_
modelMatrix.translate(0.f, (-40 + texskyoffset) * skyoffsetfactor, 0.f);
modelMatrix.scale(1.f, 0.8f * 1.17f, 1.f);
}
VSMatrix normalModelMatrix;
normalModelMatrix.computeNormalMatrix(modelMatrix);
state.SetModelMatrix(modelMatrix, normalModelMatrix);
VSMatrix textureMatrix;
textureMatrix.loadIdentity();
textureMatrix.scale(mirror ? -xscale : xscale, yscale, 1.f);
textureMatrix.translate(1.f, y_offset / texh, 1.f);
state.SetTextureMatrix(textureMatrix);
}
//-----------------------------------------------------------------------------
@ -461,8 +469,6 @@ void FSkyVertexBuffer::DoRenderDome(FRenderState& state, FGameTexture* tex, int
if (tex && tex->isValid())
{
state.SetMaterial(tex, UF_Texture, 0, CLAMP_NONE, 0, -1);
state.EnableModelMatrix(true);
state.EnableTextureMatrix(true);
}
int rc = mRows + 1;
@ -494,8 +500,8 @@ void FSkyVertexBuffer::DoRenderDome(FRenderState& state, FGameTexture* tex, int
RenderRow(state, DT_TriangleStrip, rc + i, primStart, false);
}
state.EnableTextureMatrix(false);
state.EnableModelMatrix(false);
state.SetTextureMatrix(VSMatrix::identity());
state.SetModelMatrix(VSMatrix::identity(), VSMatrix::identity());
}
@ -509,7 +515,7 @@ void FSkyVertexBuffer::RenderDome(FRenderState& state, FGameTexture* tex, float
{
if (tex)
{
SetupMatrices(tex, x_offset, y_offset, mirror, mode, state.mModelMatrix, state.mTextureMatrix, tiled, xscale, yscale);
SetupMatrices(state, tex, x_offset, y_offset, mirror, mode, tiled, xscale, yscale);
}
DoRenderDome(state, tex, mode, false, color);
}
@ -526,14 +532,19 @@ void FSkyVertexBuffer::RenderBox(FRenderState& state, FSkyBox* tex, float x_offs
int faces;
state.SetObjectColor(color);
state.EnableModelMatrix(true);
state.mModelMatrix.loadIdentity();
state.mModelMatrix.scale(1, 1 / stretch, 1); // Undo the map's vertical scaling as skyboxes are true cubes.
VSMatrix modelMatrix;
modelMatrix.loadIdentity();
modelMatrix.scale(1, 1 / stretch, 1); // Undo the map's vertical scaling as skyboxes are true cubes.
if (!sky2)
state.mModelMatrix.rotate(-180.0f + x_offset, skyrotatevector.X, skyrotatevector.Z, skyrotatevector.Y);
modelMatrix.rotate(-180.0f + x_offset, skyrotatevector.X, skyrotatevector.Z, skyrotatevector.Y);
else
state.mModelMatrix.rotate(-180.0f + x_offset, skyrotatevector2.X, skyrotatevector2.Z, skyrotatevector2.Y);
modelMatrix.rotate(-180.0f + x_offset, skyrotatevector2.X, skyrotatevector2.Z, skyrotatevector2.Y);
VSMatrix normalModelMatrix;
normalModelMatrix.computeNormalMatrix(modelMatrix);
state.SetModelMatrix(modelMatrix, normalModelMatrix);
if (tex->GetSkyFace(5))
{
@ -570,7 +581,7 @@ void FSkyVertexBuffer::RenderBox(FRenderState& state, FSkyBox* tex, float x_offs
state.SetMaterial(tex->GetSkyFace(faces + 1), UF_Texture, 0, CLAMP_XY, 0, -1);
state.Draw(DT_TriangleStrip, FaceStart(4), 4);
state.EnableModelMatrix(false);
state.SetModelMatrix(VSMatrix::identity(), VSMatrix::identity());
state.SetObjectColor(0xffffffff);
}

View file

@ -85,7 +85,7 @@ public:
FSkyVertexBuffer(DFrameBuffer* fb);
~FSkyVertexBuffer();
void SetupMatrices(FGameTexture *tex, float x_offset, float y_offset, bool mirror, int mode, VSMatrix &modelmatrix, VSMatrix &textureMatrix, bool tiled, float xscale = 0, float vertscale = 0);
void SetupMatrices(FRenderState& state, FGameTexture *tex, float x_offset, float y_offset, bool mirror, int mode, bool tiled, float xscale = 0, float vertscale = 0);
std::pair<IBuffer*, IBuffer*> GetBufferObjects() const
{
return std::make_pair(mVertexBuffer, nullptr);

View file

@ -169,8 +169,13 @@ void Draw2D(F2DDrawer* drawer, FRenderState& state, int x, int y, int width, int
{
m[4 * 3 + i] = (FLOATTYPE) cmd.transform.Cells[i][2];
}
state.mModelMatrix.loadMatrix(m);
state.EnableModelMatrix(true);
VSMatrix modelMatrix;
modelMatrix.loadMatrix(m);
VSMatrix normalModelMatrix;
normalModelMatrix.computeNormalMatrix(modelMatrix);
state.SetModelMatrix(modelMatrix, normalModelMatrix);
}
if (cmd.mTexture != nullptr && cmd.mTexture->isValid())
@ -184,10 +189,11 @@ void Draw2D(F2DDrawer* drawer, FRenderState& state, int x, int y, int width, int
// Canvas textures are stored upside down
if (cmd.mTexture->isHardwareCanvas())
{
state.mTextureMatrix.loadIdentity();
state.mTextureMatrix.scale(1.f, -1.f, 1.f);
state.mTextureMatrix.translate(0.f, 1.f, 0.0f);
state.EnableTextureMatrix(true);
VSMatrix textureMatrix;
textureMatrix.loadIdentity();
textureMatrix.scale(1.f, -1.f, 1.f);
textureMatrix.translate(0.f, 1.f, 0.0f);
state.SetTextureMatrix(textureMatrix);
}
if (cmd.mFlags & F2DDrawer::DTF_Burn)
{
@ -238,8 +244,10 @@ void Draw2D(F2DDrawer* drawer, FRenderState& state, int x, int y, int width, int
state.SetObjectColor(0xffffffff);
state.SetObjectColor2(0);
state.SetAddColor(0);
state.EnableTextureMatrix(false);
state.EnableModelMatrix(false);
if (cmd.mTexture != nullptr && cmd.mTexture->isValid() && cmd.mTexture->isHardwareCanvas())
state.SetTextureMatrix(VSMatrix::identity());
if (cmd.useTransform)
state.SetModelMatrix(VSMatrix::identity(), VSMatrix::identity());
state.SetEffect(EFF_NONE);
}

View file

@ -58,61 +58,16 @@ void VkStreamBufferWriter::Reset()
VkMatrixBufferWriter::VkMatrixBufferWriter(VulkanRenderDevice* fb)
{
mBuffer = fb->GetBufferManager()->MatrixBuffer.get();
mIdentityMatrix.loadIdentity();
}
template<typename T>
static void BufferedSet(bool& modified, T& dst, const T& src)
bool VkMatrixBufferWriter::Write(const MatricesUBO& matrices)
{
if (dst == src)
return;
dst = src;
modified = true;
}
static void BufferedSet(bool& modified, VSMatrix& dst, const VSMatrix& src)
{
if (memcmp(dst.get(), src.get(), sizeof(FLOATTYPE) * 16) == 0)
return;
dst = src;
modified = true;
}
bool VkMatrixBufferWriter::Write(const VSMatrix& modelMatrix, bool modelMatrixEnabled, const VSMatrix& textureMatrix, bool textureMatrixEnabled)
{
bool modified = (mOffset == 0); // always modified first call
if (modelMatrixEnabled)
{
BufferedSet(modified, mMatrices.ModelMatrix, modelMatrix);
if (modified)
mMatrices.NormalModelMatrix.computeNormalMatrix(modelMatrix);
}
else
{
BufferedSet(modified, mMatrices.ModelMatrix, mIdentityMatrix);
BufferedSet(modified, mMatrices.NormalModelMatrix, mIdentityMatrix);
}
if (textureMatrixEnabled)
{
BufferedSet(modified, mMatrices.TextureMatrix, textureMatrix);
}
else
{
BufferedSet(modified, mMatrices.TextureMatrix, mIdentityMatrix);
}
if (modified)
{
mOffset = mBuffer->NextStreamDataBlock();
if (mOffset == 0xffffffff)
return false;
uint8_t* ptr = (uint8_t*)mBuffer->Data;
memcpy(ptr + mOffset, &mMatrices, sizeof(MatricesUBO));
}
mOffset = mBuffer->NextStreamDataBlock();
if (mOffset == 0xffffffff)
return false;
uint8_t* ptr = (uint8_t*)mBuffer->Data;
memcpy(ptr + mOffset, &matrices, sizeof(MatricesUBO));
return true;
}

View file

@ -5,7 +5,6 @@
#include "vulkan/shaders/vk_shader.h"
class VkStreamBuffer;
class VkMatrixBuffer;
class VkStreamBufferWriter
{
@ -29,14 +28,12 @@ class VkMatrixBufferWriter
public:
VkMatrixBufferWriter(VulkanRenderDevice* fb);
bool Write(const VSMatrix& modelMatrix, bool modelMatrixEnabled, const VSMatrix& textureMatrix, bool textureMatrixEnabled);
bool Write(const MatricesUBO& matrices);
void Reset();
uint32_t Offset() const { return mOffset; }
private:
VkStreamBuffer* mBuffer;
MatricesUBO mMatrices = {};
VSMatrix mIdentityMatrix;
uint32_t mOffset = 0;
};

View file

@ -42,6 +42,10 @@ EXTERN_CVAR(Bool, r_skipmats)
VkRenderState::VkRenderState(VulkanRenderDevice* fb) : fb(fb), mStreamBufferWriter(fb), mMatrixBufferWriter(fb)
{
mMatrices.ModelMatrix.loadIdentity();
mMatrices.NormalModelMatrix.loadIdentity();
mMatrices.TextureMatrix.loadIdentity();
Reset();
}
@ -411,10 +415,14 @@ void VkRenderState::ApplyPushConstants()
void VkRenderState::ApplyMatrices()
{
if (!mMatrixBufferWriter.Write(mModelMatrix, mModelMatrixEnabled, mTextureMatrix, mTextureMatrixEnabled))
if (mMatricesChanged)
{
WaitForStreamBuffers();
mMatrixBufferWriter.Write(mModelMatrix, mModelMatrixEnabled, mTextureMatrix, mTextureMatrixEnabled);
if (!mMatrixBufferWriter.Write(mMatrices))
{
WaitForStreamBuffers();
mMatrixBufferWriter.Write(mMatrices);
}
mMatricesChanged = false;
}
}
@ -503,6 +511,21 @@ void VkRenderState::SetViewpoint(int index)
mNeedApply = true;
}
void VkRenderState::SetModelMatrix(const VSMatrix& matrix, const VSMatrix& normalMatrix)
{
mMatrices.ModelMatrix = matrix;
mMatrices.NormalModelMatrix = normalMatrix;
mMatricesChanged = true;
mNeedApply = true;
}
void VkRenderState::SetTextureMatrix(const VSMatrix& matrix)
{
mMatrices.TextureMatrix = matrix;
mMatricesChanged = true;
mNeedApply = true;
}
int VkRenderState::UploadLights(const FDynLightData& data)
{
auto buffers = fb->GetBufferManager();
@ -598,8 +621,6 @@ void VkRenderState::EndRenderPass()
mLastViewpointOffset = 0xffffffff;
mLastVertexBuffer = nullptr;
mLastIndexBuffer = nullptr;
mLastModelMatrixEnabled = true;
mLastTextureMatrixEnabled = true;
}
}

View file

@ -50,6 +50,8 @@ public:
// Buffers
int SetViewpoint(const HWViewpointUniforms& vp) override;
void SetViewpoint(int index) override;
void SetModelMatrix(const VSMatrix& matrix, const VSMatrix& normalMatrix) override;
void SetTextureMatrix(const VSMatrix& matrix) override;
int UploadLights(const FDynLightData& lightdata) override;
int UploadBones(const TArray<VSMatrix>& bones) override;
@ -110,8 +112,8 @@ protected:
IBuffer* mLastVertexBuffer = nullptr;
IBuffer* mLastIndexBuffer = nullptr;
bool mLastModelMatrixEnabled = true;
bool mLastTextureMatrixEnabled = true;
MatricesUBO mMatrices = {};
bool mMatricesChanged = true;
int mApplyCount = 0;