From d73b0b314636fb9b5ef7d1983ce0a3d60ca124be Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Fri, 1 Mar 2019 20:15:56 +0100 Subject: [PATCH] - create helper function for copying --- .../vulkan/renderer/vk_renderstate.cpp | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/rendering/vulkan/renderer/vk_renderstate.cpp b/src/rendering/vulkan/renderer/vk_renderstate.cpp index 0ae93d61cc..38f64df95b 100644 --- a/src/rendering/vulkan/renderer/vk_renderstate.cpp +++ b/src/rendering/vulkan/renderer/vk_renderstate.cpp @@ -133,6 +133,16 @@ void VkRenderState::EnableLineSmooth(bool on) { } +template +static void CopyToBuffer(uint32_t &offset, const T &data, VKDataBuffer *buffer) +{ + if (offset + (UniformBufferAlignment() << 1) < buffer->Size()) + { + offset += UniformBufferAlignment(); + memcpy(static_cast(buffer->Memory()) + offset, &data, sizeof(T)); + } +} + void VkRenderState::Apply(int dt) { auto fb = GetVulkanFrameBuffer(); @@ -335,23 +345,9 @@ void VkRenderState::Apply(int dt) mPushConstants.uLightIndex = screen->mLights->BindUBO(mLightIndex); - if (mMatricesOffset + UniformBufferAlignment() < fb->MatricesUBO->Size()) - { - mMatricesOffset += UniformBufferAlignment(); - memcpy(static_cast(fb->MatricesUBO->Memory()) + mMatricesOffset, &mMatrices, sizeof(MatricesUBO)); - } - - if (mColorsOffset + UniformBufferAlignment() < fb->ColorsUBO->Size()) - { - mColorsOffset += UniformBufferAlignment(); - memcpy(static_cast(fb->ColorsUBO->Memory()) + mColorsOffset, &mColors, sizeof(ColorsUBO)); - } - - if (mColorsOffset + UniformBufferAlignment() < fb->GlowingWallsUBO->Size()) - { - mGlowingWallsOffset += UniformBufferAlignment(); - memcpy(static_cast(fb->GlowingWallsUBO->Memory()) + mGlowingWallsOffset, &mGlowingWalls, sizeof(GlowingWallsUBO)); - } + CopyToBuffer(mMatricesOffset, mMatrices, fb->MatricesUBO); + CopyToBuffer(mColorsOffset, mColors, fb->ColorsUBO); + CopyToBuffer(mGlowingWallsOffset, mGlowingWalls, fb->GlowingWallsUBO); mCommandBuffer->pushConstants(passManager->PipelineLayout.get(), VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, (uint32_t)sizeof(PushConstants), &mPushConstants);