From e06f8f172de020307d307454b23f30279d1c8116 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Tue, 5 Mar 2019 02:50:30 +0100 Subject: [PATCH] - use the uniform buffer alignment as returned by the vulkan device --- .../vulkan/renderer/vk_renderstate.cpp | 17 ++++++----------- src/rendering/vulkan/shaders/vk_shader.h | 3 --- src/rendering/vulkan/system/vk_framebuffer.cpp | 6 ++++-- src/rendering/vulkan/system/vk_framebuffer.h | 3 +++ 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/rendering/vulkan/renderer/vk_renderstate.cpp b/src/rendering/vulkan/renderer/vk_renderstate.cpp index e15a1179e..5eb1d9528 100644 --- a/src/rendering/vulkan/renderer/vk_renderstate.cpp +++ b/src/rendering/vulkan/renderer/vk_renderstate.cpp @@ -479,16 +479,6 @@ void VkRenderState::ApplyPushConstants() mCommandBuffer->pushConstants(passManager->PipelineLayout.get(), VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, (uint32_t)sizeof(PushConstants), &mPushConstants); } -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)); - } -} - template static void BufferedSet(bool &modified, T &dst, const T &src) { @@ -533,7 +523,12 @@ void VkRenderState::ApplyMatrices() if (modified) { auto fb = GetVulkanFrameBuffer(); - CopyToBuffer(mMatricesOffset, mMatrices, fb->MatricesUBO); + + if (mMatricesOffset + (fb->UniformBufferAlignedSize() << 1) < fb->MatricesUBO->Size()) + { + mMatricesOffset += fb->UniformBufferAlignedSize(); + memcpy(static_cast(fb->MatricesUBO->Memory()) + mMatricesOffset, &mMatrices, sizeof(MatricesUBO)); + } } } diff --git a/src/rendering/vulkan/shaders/vk_shader.h b/src/rendering/vulkan/shaders/vk_shader.h index 174e6d115..5cff6fb2c 100644 --- a/src/rendering/vulkan/shaders/vk_shader.h +++ b/src/rendering/vulkan/shaders/vk_shader.h @@ -11,9 +11,6 @@ class VulkanDevice; class VulkanShader; -// To do: we need to read this from the card - or maybe merge ColorsUBO with GlowingWallsUBO since we have to use 256 bytes anyway -template int UniformBufferAlignment() { return (sizeof(T) + 255) / 256 * 256; } - struct MatricesUBO { VSMatrix ModelMatrix; diff --git a/src/rendering/vulkan/system/vk_framebuffer.cpp b/src/rendering/vulkan/system/vk_framebuffer.cpp index 0d8162534..b12c845b2 100644 --- a/src/rendering/vulkan/system/vk_framebuffer.cpp +++ b/src/rendering/vulkan/system/vk_framebuffer.cpp @@ -99,6 +99,8 @@ void VulkanFrameBuffer::InitializeState() gl_vendorstring = "Vulkan"; hwcaps = RFL_SHADER_STORAGE_BUFFER | RFL_BUFFER_STORAGE; + uniformblockalignment = (unsigned int)device->deviceProperties.limits.minUniformBufferOffsetAlignment; + maxuniformblock = device->deviceProperties.limits.maxUniformBufferRange; mUploadSemaphore.reset(new VulkanSemaphore(device)); mGraphicsCommandPool.reset(new VulkanCommandPool(device, device->graphicsFamily)); @@ -113,8 +115,8 @@ void VulkanFrameBuffer::InitializeState() // To do: move this to HW renderer interface maybe? MatricesUBO = (VKDataBuffer*)CreateDataBuffer(-1, false); StreamUBO = (VKDataBuffer*)CreateDataBuffer(-1, false); - MatricesUBO->SetData(UniformBufferAlignment<::MatricesUBO>() * 50000, nullptr, false); - StreamUBO->SetData(UniformBufferAlignment<::StreamUBO>() * 200, nullptr, false); + MatricesUBO->SetData(UniformBufferAlignedSize<::MatricesUBO>() * 50000, nullptr, false); + StreamUBO->SetData(UniformBufferAlignedSize<::StreamUBO>() * 200, nullptr, false); mShaderManager.reset(new VkShaderManager(device)); mSamplerManager.reset(new VkSamplerManager(device)); diff --git a/src/rendering/vulkan/system/vk_framebuffer.h b/src/rendering/vulkan/system/vk_framebuffer.h index 926e744f8..88ad3fc17 100644 --- a/src/rendering/vulkan/system/vk_framebuffer.h +++ b/src/rendering/vulkan/system/vk_framebuffer.h @@ -30,6 +30,9 @@ public: unsigned int GetLightBufferBlockSize() const; + template + int UniformBufferAlignedSize() const { return (sizeof(T) + uniformblockalignment - 1) / uniformblockalignment * uniformblockalignment; } + VKDataBuffer *ViewpointUBO = nullptr; VKDataBuffer *LightBufferSSO = nullptr; VKDataBuffer *MatricesUBO = nullptr;