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