[Impeller] check if memory type is host coherent, avoid flush. (flutter/engine#56350)

Fixes https://github.com/flutter/flutter/issues/137454

Host coherent memory does not need to be flushed. Almost all mobile devices will have host coherent memory for us to write to, but we still need check because of swiftshader and/or desktop at some point.
This commit is contained in:
Jonah Williams
2024-11-04 13:13:37 -08:00
committed by GitHub
parent 2571cfc398
commit 574bb3b0f4
4 changed files with 39 additions and 8 deletions

View File

@@ -506,7 +506,6 @@ std::shared_ptr<DeviceBuffer> AllocatorVK::OnCreateBuffer(
!desc.readback) {
allocation_info.pool = staging_buffer_pool_.get().pool;
}
VkBuffer buffer = {};
VmaAllocation buffer_allocation = {};
VmaAllocationInfo buffer_allocation_info = {};
@@ -518,6 +517,10 @@ std::shared_ptr<DeviceBuffer> AllocatorVK::OnCreateBuffer(
&buffer_allocation_info //
)};
auto type = memory_properties_.memoryTypes[buffer_allocation_info.memoryType];
bool is_host_coherent =
!!(type.propertyFlags & vk::MemoryPropertyFlagBits::eHostCoherent);
if (result != vk::Result::eSuccess) {
VALIDATION_LOG << "Unable to allocate a device buffer: "
<< vk::to_string(result);
@@ -530,8 +533,8 @@ std::shared_ptr<DeviceBuffer> AllocatorVK::OnCreateBuffer(
UniqueBufferVMA{BufferVMA{allocator_.get(), //
buffer_allocation, //
vk::Buffer{buffer}}}, //
buffer_allocation_info //
);
buffer_allocation_info, //
is_host_coherent);
}
Bytes AllocatorVK::DebugGetHeapUsage() const {

View File

@@ -5,9 +5,11 @@
#include "flutter/testing/testing.h" // IWYU pragma: keep
#include "gtest/gtest.h"
#include "impeller/base/allocation_size.h"
#include "impeller/core/device_buffer.h"
#include "impeller/core/device_buffer_descriptor.h"
#include "impeller/core/formats.h"
#include "impeller/renderer/backend/vulkan/allocator_vk.h"
#include "impeller/renderer/backend/vulkan/device_buffer_vk.h"
#include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
#include "vulkan/vulkan_enums.hpp"
@@ -71,6 +73,19 @@ TEST(AllocatorVKTest, MemoryTypeSelectionTwoHeap) {
EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(4, properties), -1);
}
TEST(AllocatorVKTest, DeviceBufferCoherency) {
auto const context = MockVulkanContextBuilder().Build();
auto allocator = context->GetResourceAllocator();
std::shared_ptr<DeviceBuffer> buffer =
allocator->CreateBuffer(DeviceBufferDescriptor{
.storage_mode = StorageMode::kHostVisible,
.size = 1024,
});
EXPECT_TRUE(DeviceBufferVK::Cast(*buffer).IsHostCoherent());
}
#ifdef IMPELLER_DEBUG
TEST(AllocatorVKTest, RecreateSwapchainWhenSizeChanges) {

View File

@@ -5,23 +5,23 @@
#include "impeller/renderer/backend/vulkan/device_buffer_vk.h"
#include "flutter/flutter_vma/flutter_vma.h"
#include "flutter/fml/trace_event.h"
#include "impeller/renderer/backend/vulkan/context_vk.h"
#include "vulkan/vulkan_core.h"
namespace impeller {
DeviceBufferVK::DeviceBufferVK(DeviceBufferDescriptor desc,
std::weak_ptr<Context> context,
UniqueBufferVMA buffer,
VmaAllocationInfo info)
VmaAllocationInfo info,
bool is_host_coherent)
: DeviceBuffer(desc),
context_(std::move(context)),
resource_(ContextVK::Cast(*context_.lock().get()).GetResourceManager(),
BufferResource{
std::move(buffer), //
info //
}) {}
}),
is_host_coherent_(is_host_coherent) {}
DeviceBufferVK::~DeviceBufferVK() = default;
@@ -69,12 +69,20 @@ bool DeviceBufferVK::SetLabel(std::string_view label) {
}
void DeviceBufferVK::Flush(std::optional<Range> range) const {
if (is_host_coherent_) {
return;
}
auto flush_range = range.value_or(Range{0, GetDeviceBufferDescriptor().size});
::vmaFlushAllocation(resource_->buffer.get().allocator,
resource_->buffer.get().allocation, flush_range.offset,
flush_range.length);
}
// Visible for testing.
bool DeviceBufferVK::IsHostCoherent() const {
return is_host_coherent_;
}
void DeviceBufferVK::Invalidate(std::optional<Range> range) const {
auto flush_range = range.value_or(Range{0, GetDeviceBufferDescriptor().size});
::vmaInvalidateAllocation(resource_->buffer.get().allocator,

View File

@@ -20,13 +20,17 @@ class DeviceBufferVK final : public DeviceBuffer,
DeviceBufferVK(DeviceBufferDescriptor desc,
std::weak_ptr<Context> context,
UniqueBufferVMA buffer,
VmaAllocationInfo info);
VmaAllocationInfo info,
bool is_host_coherent);
// |DeviceBuffer|
~DeviceBufferVK() override;
vk::Buffer GetBuffer() const;
// Visible for testing.
bool IsHostCoherent() const;
private:
friend class AllocatorVK;
@@ -51,6 +55,7 @@ class DeviceBufferVK final : public DeviceBuffer,
std::weak_ptr<Context> context_;
UniqueResourceVKT<BufferResource> resource_;
bool is_host_coherent_ = false;
// |DeviceBuffer|
uint8_t* OnGetContents() const override;