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

@ -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;