Fix: Vulkan: Misalignment sub-buffers immediate drawing

Since recently we bind immediate sub buffers as storage buffers to
extend the geometry for line drawing. We didn't add alignment checks and
that raised warnings. This change will fix those warnings.

Pull Request: https://projects.blender.org/blender/blender/pulls/131103
This commit is contained in:
Jeroen Bakker
2024-11-28 16:31:58 +01:00
parent e52cb5e24b
commit 8e4502258a
2 changed files with 23 additions and 10 deletions

View File

@@ -46,13 +46,22 @@ void VKImmediate::deinit(VKDevice &device)
uchar *VKImmediate::begin()
{
const VKWorkarounds &workarounds = VKBackend::get().device.workarounds_get();
const VKDevice &device = VKBackend::get().device;
const VKWorkarounds &workarounds = device.workarounds_get();
vertex_format_converter.init(&vertex_format, workarounds);
const size_t bytes_needed = vertex_buffer_size(&vertex_format_converter.device_format_get(),
vertex_len);
VKBuffer &buffer = ensure_space(bytes_needed);
current_subbuffer_len_ = bytes_needed;
VkDeviceSize offset_alignment =
device.physical_device_properties_get().limits.minStorageBufferOffsetAlignment;
VKBuffer &buffer = ensure_space(bytes_needed, offset_alignment);
/* Apply alignment when allocating new sub buffer, to reduce signed/unsigned data conversion
* later on. */
buffer_offset_ += offset_alignment - 1;
buffer_offset_ &= ~(offset_alignment - 1);
BLI_assert((buffer_offset_ & (offset_alignment - 1)) == 0);
current_subbuffer_len_ = bytes_needed;
uchar *data = static_cast<uchar *>(buffer.mapped_memory_get());
return data + buffer_offset_;
}
@@ -124,20 +133,23 @@ VkDeviceSize VKImmediate::buffer_bytes_free()
return active_buffers_.last()->size_in_bytes() - buffer_offset_;
}
static VkDeviceSize new_buffer_size(size_t sub_buffer_size)
static VkDeviceSize new_buffer_size(VkDeviceSize sub_buffer_size)
{
return max_ulul(sub_buffer_size, DEFAULT_INTERNAL_BUFFER_SIZE);
}
VKBuffer &VKImmediate::ensure_space(size_t bytes_needed)
VKBuffer &VKImmediate::ensure_space(VkDeviceSize bytes_needed, VkDeviceSize offset_alignment)
{
VkDeviceSize bytes_required = bytes_needed + offset_alignment;
/* Last used buffer still has space. */
if (!active_buffers_.is_empty() && buffer_bytes_free() >= bytes_needed) {
if (!active_buffers_.is_empty() && buffer_bytes_free() >= bytes_required) {
return *active_buffers_.last();
}
/* Recycle a previous buffer. */
if (!recycling_buffers_.is_empty() && recycling_buffers_.last()->size_in_bytes() >= bytes_needed)
if (!recycling_buffers_.is_empty() &&
recycling_buffers_.last()->size_in_bytes() >= bytes_required)
{
CLOG_INFO(&LOG, 2, "Activating recycled buffer");
buffer_offset_ = 0;
@@ -145,12 +157,13 @@ VKBuffer &VKImmediate::ensure_space(size_t bytes_needed)
return *active_buffers_.last();
}
size_t alloc_size = new_buffer_size(bytes_needed);
/* Offset alignment isn't needed when creating buffers as it is managed by VMA. */
VkDeviceSize alloc_size = new_buffer_size(bytes_needed);
CLOG_INFO(&LOG, 2, "Allocate buffer (size=%d)", int(alloc_size));
buffer_offset_ = 0;
active_buffers_.append(std::make_unique<VKBuffer>());
VKBuffer &result = *active_buffers_.last();
result.create(new_buffer_size(bytes_needed),
result.create(alloc_size,
GPU_USAGE_DYNAMIC,
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT |
VK_BUFFER_USAGE_TRANSFER_DST_BIT);

View File

@@ -55,7 +55,7 @@ class VKImmediate : public Immediate {
VKBufferWithOffset active_buffer() const;
VkDeviceSize buffer_bytes_free();
VKBuffer &ensure_space(size_t bytes_needed);
VKBuffer &ensure_space(VkDeviceSize bytes_needed, VkDeviceSize offset_allignment);
};
} // namespace blender::gpu