| Flutter Impeller |

allocator_vk_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "flutter/testing/testing.h" // IWYU pragma: keep
6 #include "gtest/gtest.h"
7 #include "impeller/base/allocation_size.h"
8 #include "impeller/core/allocator.h"
9 #include "impeller/core/device_buffer.h"
10 #include "impeller/core/device_buffer_descriptor.h"
11 #include "impeller/core/formats.h"
12 #include "impeller/renderer/backend/vulkan/allocator_vk.h"
13 #include "impeller/renderer/backend/vulkan/device_buffer_vk.h"
14 #include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
15 #include "vulkan/vulkan_enums.hpp"
16
17 namespace impeller {
18 namespace testing {
19
20 TEST(AllocatorVKTest, ToVKImageUsageFlags) {
21 EXPECT_EQ(AllocatorVK::ToVKImageUsageFlags(
22 PixelFormat::kR8G8B8A8UNormInt,
23 static_cast<TextureUsageMask>(TextureUsage::kRenderTarget),
24 StorageMode::kDeviceTransient,
25 /*supports_memoryless_textures=*/true),
26 vk::ImageUsageFlagBits::eInputAttachment |
27 vk::ImageUsageFlagBits::eColorAttachment |
28 vk::ImageUsageFlagBits::eTransientAttachment);
29
30 EXPECT_EQ(AllocatorVK::ToVKImageUsageFlags(
31 PixelFormat::kD24UnormS8Uint,
32 static_cast<TextureUsageMask>(TextureUsage::kRenderTarget),
33 StorageMode::kDeviceTransient,
34 /*supports_memoryless_textures=*/true),
35 vk::ImageUsageFlagBits::eDepthStencilAttachment |
36 vk::ImageUsageFlagBits::eTransientAttachment);
37 }
38
39 TEST(AllocatorVKTest, MemoryTypeSelectionSingleHeap) {
40 vk::PhysicalDeviceMemoryProperties properties;
41 properties.memoryTypeCount = 1;
42 properties.memoryHeapCount = 1;
43 properties.memoryTypes[0].heapIndex = 0;
44 properties.memoryTypes[0].propertyFlags =
45 vk::MemoryPropertyFlagBits::eDeviceLocal;
46 properties.memoryHeaps[0].size = 1024 * 1024 * 1024;
47 properties.memoryHeaps[0].flags = vk::MemoryHeapFlagBits::eDeviceLocal;
48
49 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(1, properties), 0);
50 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(2, properties), -1);
51 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(3, properties), 0);
52 }
53
54 TEST(AllocatorVKTest, MemoryTypeSelectionTwoHeap) {
55 vk::PhysicalDeviceMemoryProperties properties;
56 properties.memoryTypeCount = 2;
57 properties.memoryHeapCount = 2;
58 properties.memoryTypes[0].heapIndex = 0;
59 properties.memoryTypes[0].propertyFlags =
60 vk::MemoryPropertyFlagBits::eHostVisible;
61 properties.memoryHeaps[0].size = 1024 * 1024 * 1024;
62 properties.memoryHeaps[0].flags = vk::MemoryHeapFlagBits::eDeviceLocal;
63
64 properties.memoryTypes[1].heapIndex = 1;
65 properties.memoryTypes[1].propertyFlags =
66 vk::MemoryPropertyFlagBits::eDeviceLocal;
67 properties.memoryHeaps[1].size = 1024 * 1024 * 1024;
68 properties.memoryHeaps[1].flags = vk::MemoryHeapFlagBits::eDeviceLocal;
69
70 // First fails because this only looks for eDeviceLocal.
71 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(1, properties), -1);
72 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(2, properties), 1);
73 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(3, properties), 1);
74 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(4, properties), -1);
75 }
76
77 TEST(AllocatorVKTest, ImageResourceKeepsVulkanDeviceAlive) {
78 std::shared_ptr<Texture> texture;
79 std::weak_ptr<Allocator> weak_allocator;
80 {
81 auto const context = MockVulkanContextBuilder().Build();
82 weak_allocator = context->GetResourceAllocator();
83 auto allocator = context->GetResourceAllocator();
84
85 texture = allocator->CreateTexture(TextureDescriptor{
86 .storage_mode = StorageMode::kDevicePrivate,
87 .format = PixelFormat::kR8G8B8A8UNormInt,
88 .size = {1, 1},
89 });
90 context->Shutdown();
91 }
92
93 ASSERT_TRUE(weak_allocator.lock());
94 }
95
96 #ifdef IMPELLER_DEBUG
97
98 TEST(AllocatorVKTest, RecreateSwapchainWhenSizeChanges) {
99 auto const context = MockVulkanContextBuilder().Build();
100 auto allocator = context->GetResourceAllocator();
101
102 EXPECT_EQ(reinterpret_cast<AllocatorVK*>(allocator.get())
103 ->DebugGetHeapUsage()
104 .GetByteSize(),
105 0u);
106
107 allocator->CreateBuffer(DeviceBufferDescriptor{
108 .storage_mode = StorageMode::kDevicePrivate,
109 .size = 1024,
110 });
111
112 // Usage increases beyond the size of the allocated buffer since VMA will
113 // first allocate large blocks of memory and then suballocate small memory
114 // allocations.
115 EXPECT_EQ(reinterpret_cast<AllocatorVK*>(allocator.get())
116 ->DebugGetHeapUsage()
117 .ConvertTo<MebiBytes>()
118 .GetSize(),
119 16u);
120 }
121
122 #endif // IMPELLER_DEBUG
123
124 } // namespace testing
125 } // namespace impeller
allocation_size.h
allocator.h
allocator_vk.h
impeller::AllocationSize::GetByteSize
constexpr uint64_t GetByteSize() const
Definition: allocation_size.h:71
impeller::AllocatorVK
Definition: allocator_vk.h:19
impeller::AllocatorVK::FindMemoryTypeIndex
static int32_t FindMemoryTypeIndex(uint32_t memory_type_bits_requirement, vk::PhysicalDeviceMemoryProperties &memory_properties)
Select a matching memory type for the given [memory_type_bits_requirement], or -1 if none is found.
Definition: allocator_vk.cc:180
impeller::AllocatorVK::ToVKImageUsageFlags
static vk::ImageUsageFlags ToVKImageUsageFlags(PixelFormat format, TextureUsageMask usage, StorageMode mode, bool supports_memoryless_textures)
Definition: allocator_vk.cc:206
impeller::AllocatorVK::DebugGetHeapUsage
Bytes DebugGetHeapUsage() const override
Definition: allocator_vk.cc:552
formats.h
device_buffer.h
device_buffer_descriptor.h
device_buffer_vk.h
impeller::testing::TEST
TEST(AllocationSizeTest, CanCreateTypedAllocations)
Definition: allocation_size_unittests.cc:10
impeller
Definition: allocation.cc:12
impeller::MebiBytes
AllocationSize< 1 '024u *1 '024u > MebiBytes
Definition: allocation_size.h:158
impeller::StorageMode::kDevicePrivate
@ kDevicePrivate
impeller::StorageMode::kDeviceTransient
@ kDeviceTransient
impeller::PixelFormat::kD24UnormS8Uint
@ kD24UnormS8Uint
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
impeller::TextureUsage::kRenderTarget
@ kRenderTarget
impeller::DeviceBufferDescriptor
Definition: device_buffer_descriptor.h:14
impeller::DeviceBufferDescriptor::storage_mode
StorageMode storage_mode
Definition: device_buffer_descriptor.h:15
impeller::Mask< TextureUsage >
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:38
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:39