Report the maximum buffer offset alignment requirement The buffer usage flags can contain multiple bits, so we must use the maximum of the required offset alignments. While they're all 256 currently, this refactoring ensures the logic remains correct if we modify them or handle other usage bits. Bug: b/238916823 Change-Id: I189c8e25edae48266925c2ae1a70bfb13081bead Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/67088 Reviewed-by: Sean Risser <srisser@google.com> Kokoro-Result: kokoro <noreply+kokoro@google.com> Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Vulkan/VkBuffer.cpp b/src/Vulkan/VkBuffer.cpp index daa37d9..7912c52 100644 --- a/src/Vulkan/VkBuffer.cpp +++ b/src/Vulkan/VkBuffer.cpp
@@ -17,6 +17,7 @@ #include "VkConfig.hpp" #include "VkDeviceMemory.hpp" +#include <algorithm> #include <cstring> #include <limits> @@ -66,22 +67,21 @@ VkMemoryRequirements memoryRequirements = {}; memoryRequirements.size = size; + memoryRequirements.alignment = REQUIRED_MEMORY_ALIGNMENT; if(usage & (VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) { - memoryRequirements.alignment = vk::MIN_TEXEL_BUFFER_OFFSET_ALIGNMENT; + memoryRequirements.alignment = std::max(memoryRequirements.alignment, vk::MIN_TEXEL_BUFFER_OFFSET_ALIGNMENT); } - else if(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) + + if(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) { - memoryRequirements.alignment = vk::MIN_STORAGE_BUFFER_OFFSET_ALIGNMENT; + memoryRequirements.alignment = std::max(memoryRequirements.alignment, vk::MIN_STORAGE_BUFFER_OFFSET_ALIGNMENT); } - else if(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) + + if(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) { - memoryRequirements.alignment = vk::MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT; - } - else - { - memoryRequirements.alignment = REQUIRED_MEMORY_ALIGNMENT; + memoryRequirements.alignment = std::max(memoryRequirements.alignment, vk::MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT); } memoryRequirements.memoryTypeBits = vk::MEMORY_TYPE_GENERIC_BIT;