Skip to content

Commit 43c00cf

Browse files
Jonah Williamskjlubick
authored andcommitted
[Impeller] Allocate fewer textures in dedicated memory and adjust buffer flags. (flutter#43313)
Following some thresholds from ANGLE, lets try allocating fewer of our resources into dedicated memory to see if that improves allocator performance. flutter/flutter#129737
1 parent b4014bb commit 43c00cf

9 files changed

Lines changed: 37 additions & 29 deletions

File tree

ci/licenses_golden/licenses_flutter

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4207,6 +4207,7 @@ FILE: ../../../flutter/impeller/renderer/backend/vulkan/fence_waiter_vk.cc
42074207
FILE: ../../../flutter/impeller/renderer/backend/vulkan/fence_waiter_vk.h
42084208
FILE: ../../../flutter/impeller/renderer/backend/vulkan/formats_vk.cc
42094209
FILE: ../../../flutter/impeller/renderer/backend/vulkan/formats_vk.h
4210+
FILE: ../../../flutter/impeller/renderer/backend/vulkan/limits_vk.h
42104211
FILE: ../../../flutter/impeller/renderer/backend/vulkan/pass_bindings_cache.cc
42114212
FILE: ../../../flutter/impeller/renderer/backend/vulkan/pass_bindings_cache.h
42124213
FILE: ../../../flutter/impeller/renderer/backend/vulkan/pipeline_cache_vk.cc

impeller/core/device_buffer.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ std::shared_ptr<Texture> DeviceBuffer::AsTexture(
3939
return texture;
4040
}
4141

42-
void DeviceBuffer::Flush() {}
43-
4442
const DeviceBufferDescriptor& DeviceBuffer::GetDeviceBufferDescriptor() const {
4543
return desc_;
4644
}

impeller/core/device_buffer.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ class DeviceBuffer : public Buffer,
3232

3333
BufferView AsBufferView() const;
3434

35-
virtual void Flush();
36-
3735
virtual std::shared_ptr<Texture> AsTexture(
3836
Allocator& allocator,
3937
const TextureDescriptor& descriptor,

impeller/renderer/backend/vulkan/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impeller_component("vulkan") {
5252
"fence_waiter_vk.h",
5353
"formats_vk.cc",
5454
"formats_vk.h",
55+
"limits_vk.h",
5556
"pass_bindings_cache.cc",
5657
"pass_bindings_cache.h",
5758
"pipeline_cache_vk.cc",

impeller/renderer/backend/vulkan/allocator_vk.cc

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "impeller/core/formats.h"
1212
#include "impeller/renderer/backend/vulkan/device_buffer_vk.h"
1313
#include "impeller/renderer/backend/vulkan/formats_vk.h"
14+
#include "impeller/renderer/backend/vulkan/limits_vk.h"
1415
#include "impeller/renderer/backend/vulkan/texture_vk.h"
1516

1617
namespace impeller {
@@ -198,9 +199,9 @@ static constexpr VkMemoryPropertyFlags ToVKBufferMemoryPropertyFlags(
198199
StorageMode mode) {
199200
switch (mode) {
200201
case StorageMode::kHostVisible:
201-
// See https://github.com/flutter/flutter/issues/128556 . Some devices do
202-
// not have support for coherent host memory so we don't request it here.
203-
return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
202+
return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
203+
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
204+
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
204205
case StorageMode::kDevicePrivate:
205206
return VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
206207
case StorageMode::kDeviceTransient:
@@ -210,18 +211,27 @@ static constexpr VkMemoryPropertyFlags ToVKBufferMemoryPropertyFlags(
210211
}
211212

212213
static VmaAllocationCreateFlags ToVmaAllocationCreateFlags(StorageMode mode,
213-
bool is_texture) {
214+
bool is_texture,
215+
size_t size) {
214216
VmaAllocationCreateFlags flags = 0;
215217
switch (mode) {
216218
case StorageMode::kHostVisible:
217219
if (is_texture) {
218-
flags |= {};
220+
if (size >= kImageSizeThresholdForDedicatedMemoryAllocation) {
221+
flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
222+
} else {
223+
flags |= {};
224+
}
219225
} else {
220-
flags |= VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT;
226+
flags |= VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
221227
flags |= VMA_ALLOCATION_CREATE_MAPPED_BIT;
222228
}
223229
return flags;
224230
case StorageMode::kDevicePrivate:
231+
if (is_texture &&
232+
size >= kImageSizeThresholdForDedicatedMemoryAllocation) {
233+
flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
234+
}
225235
return flags;
226236
case StorageMode::kDeviceTransient:
227237
return flags;
@@ -261,7 +271,9 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
261271
alloc_nfo.usage = ToVMAMemoryUsage();
262272
alloc_nfo.preferredFlags = ToVKTextureMemoryPropertyFlags(
263273
desc.storage_mode, supports_memoryless_textures);
264-
alloc_nfo.flags = ToVmaAllocationCreateFlags(desc.storage_mode, true);
274+
alloc_nfo.flags =
275+
ToVmaAllocationCreateFlags(desc.storage_mode, /*is_texture=*/true,
276+
desc.GetByteSizeOfBaseMipLevel());
265277

266278
auto create_info_native =
267279
static_cast<vk::ImageCreateInfo::NativeType>(image_info);
@@ -396,7 +408,8 @@ std::shared_ptr<DeviceBuffer> AllocatorVK::OnCreateBuffer(
396408
allocation_info.usage = ToVMAMemoryUsage();
397409
allocation_info.preferredFlags =
398410
ToVKBufferMemoryPropertyFlags(desc.storage_mode);
399-
allocation_info.flags = ToVmaAllocationCreateFlags(desc.storage_mode, false);
411+
allocation_info.flags = ToVmaAllocationCreateFlags(
412+
desc.storage_mode, /*is_texture=*/false, desc.size);
400413

401414
VkBuffer buffer = {};
402415
VmaAllocation buffer_allocation = {};

impeller/renderer/backend/vulkan/device_buffer_vk.cc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,9 @@ bool DeviceBufferVK::OnCopyHostBuffer(const uint8_t* source,
5050
::memmove(dest + offset, source + source_range.offset, source_range.length);
5151
}
5252

53-
// Some devices do not have support for coherent host memory and require an
54-
// explicit flush.
55-
::vmaFlushAllocation(allocator_, allocation_, offset, source_range.length);
56-
5753
return true;
5854
}
5955

60-
void DeviceBufferVK::Flush() {
61-
TRACE_EVENT0("impeller", "FlushDeviceBuffer");
62-
::vmaFlushAllocation(allocator_, allocation_, 0, VK_WHOLE_SIZE);
63-
}
64-
6556
bool DeviceBufferVK::SetLabel(const std::string& label) {
6657
auto context = context_.lock();
6758
if (!context || !buffer_) {

impeller/renderer/backend/vulkan/device_buffer_vk.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ class DeviceBufferVK final : public DeviceBuffer,
2828

2929
vk::Buffer GetBuffer() const;
3030

31-
// If the contents of this buffer have been written to with
32-
// `OnGetContents`, then calling flush may be necessary if the memory is
33-
// non-coherent.
34-
void Flush() override;
35-
3631
private:
3732
friend class AllocatorVK;
3833

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
5+
namespace impeller {
6+
7+
// Maximum size to use VMA image suballocation. Any allocation greater than or
8+
// equal to this value will use a dedicated VkDeviceMemory.
9+
//
10+
// This value was taken from ANGLE.
11+
constexpr size_t kImageSizeThresholdForDedicatedMemoryAllocation =
12+
4 * 1024 * 1024;
13+
14+
} // namespace impeller

lib/ui/painting/image_decoder_impeller.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,6 @@ ImpellerAllocator::ImpellerAllocator(
510510

511511
std::shared_ptr<impeller::DeviceBuffer> ImpellerAllocator::GetDeviceBuffer()
512512
const {
513-
if (buffer_) {
514-
buffer_->Flush();
515-
}
516513
return buffer_;
517514
}
518515

0 commit comments

Comments
 (0)