From c30edaa21a0459c631dc1000af4ede2d864f8446 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Sat, 2 Mar 2019 23:47:56 +0100 Subject: [PATCH] - only update matrices if they change --- .../vulkan/renderer/vk_renderstate.cpp | 39 +++++++++++++++---- .../vulkan/renderer/vk_renderstate.h | 2 + 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/rendering/vulkan/renderer/vk_renderstate.cpp b/src/rendering/vulkan/renderer/vk_renderstate.cpp index 3a3b3c997..7b8775573 100644 --- a/src/rendering/vulkan/renderer/vk_renderstate.cpp +++ b/src/rendering/vulkan/renderer/vk_renderstate.cpp @@ -18,6 +18,7 @@ VkRenderState::VkRenderState() { + mIdentityMatrix.loadIdentity(); Reset(); } @@ -427,30 +428,52 @@ static void CopyToBuffer(uint32_t &offset, const T &data, VKDataBuffer *buffer) } } +template +static void BufferedSet(bool &modified, T &dst, const T &src) +{ + 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; +} + void VkRenderState::ApplyMatrices() { + bool modified = (mMatricesOffset == 0); // always modified first call if (mTextureMatrixEnabled) { - mMatrices.TextureMatrix = mTextureMatrix; + BufferedSet(modified, mMatrices.TextureMatrix, mTextureMatrix); } else { - mMatrices.TextureMatrix.loadIdentity(); + BufferedSet(modified, mMatrices.TextureMatrix, mIdentityMatrix); } if (mModelMatrixEnabled) { - mMatrices.ModelMatrix = mModelMatrix; - mMatrices.NormalModelMatrix.computeNormalMatrix(mModelMatrix); + BufferedSet(modified, mMatrices.ModelMatrix, mModelMatrix); + if (modified) + mMatrices.NormalModelMatrix.computeNormalMatrix(mModelMatrix); } else { - mMatrices.ModelMatrix.loadIdentity(); - mMatrices.NormalModelMatrix.loadIdentity(); + BufferedSet(modified, mMatrices.ModelMatrix, mIdentityMatrix); + BufferedSet(modified, mMatrices.NormalModelMatrix, mIdentityMatrix); } - auto fb = GetVulkanFrameBuffer(); - CopyToBuffer(mMatricesOffset, mMatrices, fb->MatricesUBO); + if (modified) + { + auto fb = GetVulkanFrameBuffer(); + CopyToBuffer(mMatricesOffset, mMatrices, fb->MatricesUBO); + } } void VkRenderState::ApplyVertexBuffers() diff --git a/src/rendering/vulkan/renderer/vk_renderstate.h b/src/rendering/vulkan/renderer/vk_renderstate.h index b2a8ac027..99db9aebe 100644 --- a/src/rendering/vulkan/renderer/vk_renderstate.h +++ b/src/rendering/vulkan/renderer/vk_renderstate.h @@ -80,4 +80,6 @@ private: uint32_t mMatricesOffset = 0; uint32_t mDataIndex = -1; uint32_t mStreamDataOffset = 0; + + VSMatrix mIdentityMatrix; };