1 // Copyright 2019 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef DAWNNATIVE_VULKAN_RESOURCEMEMORYALLOCATORVK_H_ 16 #define DAWNNATIVE_VULKAN_RESOURCEMEMORYALLOCATORVK_H_ 17 18 #include "common/SerialQueue.h" 19 #include "common/vulkan_platform.h" 20 #include "dawn_native/Error.h" 21 #include "dawn_native/IntegerTypes.h" 22 #include "dawn_native/PooledResourceMemoryAllocator.h" 23 #include "dawn_native/ResourceMemoryAllocation.h" 24 25 #include <memory> 26 #include <vector> 27 28 namespace dawn_native { namespace vulkan { 29 30 class Device; 31 32 // Various kinds of memory that influence the result of the allocation. For example, to take 33 // into account mappability and Vulkan's bufferImageGranularity. 34 enum class MemoryKind { 35 Linear, 36 LinearMappable, 37 Opaque, 38 }; 39 40 class ResourceMemoryAllocator { 41 public: 42 ResourceMemoryAllocator(Device* device); 43 ~ResourceMemoryAllocator(); 44 45 ResultOrError<ResourceMemoryAllocation> Allocate(const VkMemoryRequirements& requirements, 46 MemoryKind kind); 47 void Deallocate(ResourceMemoryAllocation* allocation); 48 49 void DestroyPool(); 50 51 void Tick(ExecutionSerial completedSerial); 52 53 int FindBestTypeIndex(VkMemoryRequirements requirements, MemoryKind kind); 54 55 private: 56 Device* mDevice; 57 58 class SingleTypeAllocator; 59 std::vector<std::unique_ptr<SingleTypeAllocator>> mAllocatorsPerType; 60 61 SerialQueue<ExecutionSerial, ResourceMemoryAllocation> mSubAllocationsToDelete; 62 }; 63 64 }} // namespace dawn_native::vulkan 65 66 #endif // DAWNNATIVE_VULKAN_RESOURCEMEMORYALLOCATORVK_H_ 67