| 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/device_buffer_descriptor.h"
9 #include "impeller/core/formats.h"
10 #include "impeller/renderer/backend/vulkan/allocator_vk.h"
11 #include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
12 #include "vulkan/vulkan_enums.hpp"
13
14 namespace impeller {
15 namespace testing {
16
17 TEST(AllocatorVKTest, ToVKImageUsageFlags) {
18 EXPECT_EQ(AllocatorVK::ToVKImageUsageFlags(
19 PixelFormat::kR8G8B8A8UNormInt,
20 static_cast<TextureUsageMask>(TextureUsage::kRenderTarget),
21 StorageMode::kDeviceTransient,
22 /*supports_memoryless_textures=*/true),
23 vk::ImageUsageFlagBits::eInputAttachment |
24 vk::ImageUsageFlagBits::eColorAttachment |
25 vk::ImageUsageFlagBits::eTransientAttachment);
26
27 EXPECT_EQ(AllocatorVK::ToVKImageUsageFlags(
28 PixelFormat::kD24UnormS8Uint,
29 static_cast<TextureUsageMask>(TextureUsage::kRenderTarget),
30 StorageMode::kDeviceTransient,
31 /*supports_memoryless_textures=*/true),
32 vk::ImageUsageFlagBits::eDepthStencilAttachment |
33 vk::ImageUsageFlagBits::eTransientAttachment);
34 }
35
36 TEST(AllocatorVKTest, MemoryTypeSelectionSingleHeap) {
37 vk::PhysicalDeviceMemoryProperties properties;
38 properties.memoryTypeCount = 1;
39 properties.memoryHeapCount = 1;
40 properties.memoryTypes[0].heapIndex = 0;
41 properties.memoryTypes[0].propertyFlags =
42 vk::MemoryPropertyFlagBits::eDeviceLocal;
43 properties.memoryHeaps[0].size = 1024 * 1024 * 1024;
44 properties.memoryHeaps[0].flags = vk::MemoryHeapFlagBits::eDeviceLocal;
45
46 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(1, properties), 0);
47 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(2, properties), -1);
48 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(3, properties), 0);
49 }
50
51 TEST(AllocatorVKTest, MemoryTypeSelectionTwoHeap) {
52 vk::PhysicalDeviceMemoryProperties properties;
53 properties.memoryTypeCount = 2;
54 properties.memoryHeapCount = 2;
55 properties.memoryTypes[0].heapIndex = 0;
56 properties.memoryTypes[0].propertyFlags =
57 vk::MemoryPropertyFlagBits::eHostVisible;
58 properties.memoryHeaps[0].size = 1024 * 1024 * 1024;
59 properties.memoryHeaps[0].flags = vk::MemoryHeapFlagBits::eDeviceLocal;
60
61 properties.memoryTypes[1].heapIndex = 1;
62 properties.memoryTypes[1].propertyFlags =
63 vk::MemoryPropertyFlagBits::eDeviceLocal;
64 properties.memoryHeaps[1].size = 1024 * 1024 * 1024;
65 properties.memoryHeaps[1].flags = vk::MemoryHeapFlagBits::eDeviceLocal;
66
67 // First fails because this only looks for eDeviceLocal.
68 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(1, properties), -1);
69 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(2, properties), 1);
70 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(3, properties), 1);
71 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(4, properties), -1);
72 }
73
74 #ifdef IMPELLER_DEBUG
75
76 TEST(AllocatorVKTest, RecreateSwapchainWhenSizeChanges) {
77 auto const context = MockVulkanContextBuilder().Build();
78 auto allocator = context->GetResourceAllocator();
79
80 EXPECT_EQ(reinterpret_cast<AllocatorVK*>(allocator.get())
81 ->DebugGetHeapUsage()
82 .GetByteSize(),
83 0u);
84
85 allocator->CreateBuffer(DeviceBufferDescriptor{
86 .storage_mode = StorageMode::kDevicePrivate,
87 .size = 1024,
88 });
89
90 // Usage increases beyond the size of the allocated buffer since VMA will
91 // first allocate large blocks of memory and then suballocate small memory
92 // allocations.
93 EXPECT_EQ(reinterpret_cast<AllocatorVK*>(allocator.get())
94 ->DebugGetHeapUsage()
95 .ConvertTo<MebiBytes>()
96 .GetSize(),
97 16u);
98 }
99
100 #endif // IMPELLER_DEBUG
101
102 } // namespace testing
103 } // namespace impeller
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:175
allocator_vk.h
impeller::DeviceBufferDescriptor
Definition: device_buffer_descriptor.h:14
impeller::MebiBytes
AllocationSize< 1 '024u *1 '024u > MebiBytes
Definition: allocation_size.h:158
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
formats.h
impeller::TextureUsage::kRenderTarget
@ kRenderTarget
impeller::AllocatorVK::DebugGetHeapUsage
Bytes DebugGetHeapUsage() const override
Definition: allocator_vk.cc:537
impeller::AllocationSize::GetByteSize
constexpr uint64_t GetByteSize() const
Definition: allocation_size.h:71
impeller::AllocatorVK::ToVKImageUsageFlags
static vk::ImageUsageFlags ToVKImageUsageFlags(PixelFormat format, TextureUsageMask usage, StorageMode mode, bool supports_memoryless_textures)
Definition: allocator_vk.cc:201
impeller::Mask< TextureUsage >
impeller::StorageMode::kDeviceTransient
@ kDeviceTransient
impeller::AllocatorVK
Definition: allocator_vk.h:19
impeller::StorageMode::kDevicePrivate
@ kDevicePrivate
impeller::DeviceBufferDescriptor::storage_mode
StorageMode storage_mode
Definition: device_buffer_descriptor.h:15
impeller::PixelFormat::kD24UnormS8Uint
@ kD24UnormS8Uint
device_buffer_descriptor.h
allocation_size.h
impeller
Definition: allocation.cc:12
impeller::testing::TEST
TEST(AllocationSizeTest, CanCreateTypedAllocations)
Definition: allocation_size_unittests.cc:10