• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef skgpu_VulkanAMDMemoryAllocator_DEFINED
9 #define skgpu_VulkanAMDMemoryAllocator_DEFINED
10 
11 #include "include/gpu/vk/VulkanMemoryAllocator.h"
12 
13 namespace skgpu {
14 
15 class VulkanExtensions;
16 struct VulkanInterface;
17 
18 #ifndef SK_USE_VMA
19 class VulkanAMDMemoryAllocator {
20 public:
21     static sk_sp<VulkanMemoryAllocator> Make(VkInstance instance,
22                                              VkPhysicalDevice physicalDevice,
23                                              VkDevice device,
24                                              uint32_t physicalDeviceVersion,
25                                              const VulkanExtensions* extensions,
26                                              sk_sp<const VulkanInterface> interface,
27                                              bool mustUseCoherentHostVisibleMemory,
28                                              bool threadSafe);
29 };
30 
31 #else
32 
33 #include "GrVulkanMemoryAllocator.h"
34 
35 class VulkanAMDMemoryAllocator : public VulkanMemoryAllocator {
36 public:
37     static sk_sp<VulkanMemoryAllocator> Make(VkInstance instance,
38                                              VkPhysicalDevice physicalDevice,
39                                              VkDevice device,
40                                              uint32_t physicalDeviceVersion,
41                                              const VulkanExtensions* extensions,
42                                              sk_sp<const VulkanInterface> interface,
43                                              bool mustUseCoherentHostVisibleMemory,
44                                              bool threadSafe);
45 
46     ~VulkanAMDMemoryAllocator() override;
47 
48     VkResult allocateImageMemory(VkImage image, uint32_t allocationPropertyFlags,
49                                  skgpu::VulkanBackendMemory*) override;
50 
51     VkResult allocateBufferMemory(VkBuffer buffer,
52                                   BufferUsage usage,
53                                   uint32_t allocationPropertyFlags,
54                                   skgpu::VulkanBackendMemory*) override;
55 
56     void freeMemory(const VulkanBackendMemory&) override;
57 
58     void getAllocInfo(const VulkanBackendMemory&, VulkanAlloc*) const override;
59 
60     VkResult mapMemory(const VulkanBackendMemory&, void** data) override;
61     void unmapMemory(const VulkanBackendMemory&) override;
62 
63     VkResult flushMemory(const VulkanBackendMemory&, VkDeviceSize offset,
64                          VkDeviceSize size) override;
65     VkResult invalidateMemory(const VulkanBackendMemory&, VkDeviceSize offset,
66                               VkDeviceSize size) override;
67 
68     std::pair<uint64_t, uint64_t> totalAllocatedAndUsedMemory() const override;
69 
70 private:
71     VulkanAMDMemoryAllocator(VmaAllocator allocator, sk_sp<const VulkanInterface> interface,
72                              bool mustUseCoherentHostVisibleMemory);
73 
74     VmaAllocator fAllocator;
75 
76     // If a future version of the AMD allocator has helper functions for flushing and invalidating
77     // memory, then we won't need to save the VulkanInterface here since we won't need to
78     // make direct vulkan calls.
79     sk_sp<const VulkanInterface> fInterface;
80 
81     // For host visible allocations do we require they are coherent or not. All devices are required
82     // to support a host visible and coherent memory type. This is used to work around bugs for
83     // devices that don't handle non coherent memory correctly.
84     bool fMustUseCoherentHostVisibleMemory;
85 };
86 
87 #endif // SK_USE_VMA
88 
89 } // namespace skgpu
90 
91 #endif // skgpu_VulkanAMDMemoryAllocator_DEFINED
92