- use the uniform buffer alignment as returned by the vulkan device

This commit is contained in:
Magnus Norddahl 2019-03-05 02:50:30 +01:00
parent 95116e8580
commit e06f8f172d
4 changed files with 13 additions and 16 deletions

View file

@ -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); mCommandBuffer->pushConstants(passManager->PipelineLayout.get(), VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, (uint32_t)sizeof(PushConstants), &mPushConstants);
} }
template<typename T>
static void CopyToBuffer(uint32_t &offset, const T &data, VKDataBuffer *buffer)
{
if (offset + (UniformBufferAlignment<T>() << 1) < buffer->Size())
{
offset += UniformBufferAlignment<T>();
memcpy(static_cast<uint8_t*>(buffer->Memory()) + offset, &data, sizeof(T));
}
}
template<typename T> template<typename T>
static void BufferedSet(bool &modified, T &dst, const T &src) static void BufferedSet(bool &modified, T &dst, const T &src)
{ {
@ -533,7 +523,12 @@ void VkRenderState::ApplyMatrices()
if (modified) if (modified)
{ {
auto fb = GetVulkanFrameBuffer(); auto fb = GetVulkanFrameBuffer();
CopyToBuffer(mMatricesOffset, mMatrices, fb->MatricesUBO);
if (mMatricesOffset + (fb->UniformBufferAlignedSize<MatricesUBO>() << 1) < fb->MatricesUBO->Size())
{
mMatricesOffset += fb->UniformBufferAlignedSize<MatricesUBO>();
memcpy(static_cast<uint8_t*>(fb->MatricesUBO->Memory()) + mMatricesOffset, &mMatrices, sizeof(MatricesUBO));
}
} }
} }

View file

@ -11,9 +11,6 @@
class VulkanDevice; class VulkanDevice;
class VulkanShader; 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<typename T> int UniformBufferAlignment() { return (sizeof(T) + 255) / 256 * 256; }
struct MatricesUBO struct MatricesUBO
{ {
VSMatrix ModelMatrix; VSMatrix ModelMatrix;

View file

@ -99,6 +99,8 @@ void VulkanFrameBuffer::InitializeState()
gl_vendorstring = "Vulkan"; gl_vendorstring = "Vulkan";
hwcaps = RFL_SHADER_STORAGE_BUFFER | RFL_BUFFER_STORAGE; 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)); mUploadSemaphore.reset(new VulkanSemaphore(device));
mGraphicsCommandPool.reset(new VulkanCommandPool(device, device->graphicsFamily)); mGraphicsCommandPool.reset(new VulkanCommandPool(device, device->graphicsFamily));
@ -113,8 +115,8 @@ void VulkanFrameBuffer::InitializeState()
// To do: move this to HW renderer interface maybe? // To do: move this to HW renderer interface maybe?
MatricesUBO = (VKDataBuffer*)CreateDataBuffer(-1, false); MatricesUBO = (VKDataBuffer*)CreateDataBuffer(-1, false);
StreamUBO = (VKDataBuffer*)CreateDataBuffer(-1, false); StreamUBO = (VKDataBuffer*)CreateDataBuffer(-1, false);
MatricesUBO->SetData(UniformBufferAlignment<::MatricesUBO>() * 50000, nullptr, false); MatricesUBO->SetData(UniformBufferAlignedSize<::MatricesUBO>() * 50000, nullptr, false);
StreamUBO->SetData(UniformBufferAlignment<::StreamUBO>() * 200, nullptr, false); StreamUBO->SetData(UniformBufferAlignedSize<::StreamUBO>() * 200, nullptr, false);
mShaderManager.reset(new VkShaderManager(device)); mShaderManager.reset(new VkShaderManager(device));
mSamplerManager.reset(new VkSamplerManager(device)); mSamplerManager.reset(new VkSamplerManager(device));

View file

@ -30,6 +30,9 @@ public:
unsigned int GetLightBufferBlockSize() const; unsigned int GetLightBufferBlockSize() const;
template<typename T>
int UniformBufferAlignedSize() const { return (sizeof(T) + uniformblockalignment - 1) / uniformblockalignment * uniformblockalignment; }
VKDataBuffer *ViewpointUBO = nullptr; VKDataBuffer *ViewpointUBO = nullptr;
VKDataBuffer *LightBufferSSO = nullptr; VKDataBuffer *LightBufferSSO = nullptr;
VKDataBuffer *MatricesUBO = nullptr; VKDataBuffer *MatricesUBO = nullptr;