1+ #pragma once
2+ #include " reactive/common.hpp"
3+
4+ namespace rv {
5+
6+ // Forward declarations
7+ class Context ;
8+
9+ // メモリ割り当て情報
10+ struct BufferAllocation {
11+ vk::Buffer buffer;
12+ VmaAllocation allocation;
13+ VmaAllocationInfo info;
14+
15+ BufferAllocation () = default ;
16+ BufferAllocation (vk::Buffer buf, VmaAllocation alloc, const VmaAllocationInfo& allocInfo)
17+ : buffer(buf), allocation(alloc), info(allocInfo) {}
18+
19+ // データマッピング用ヘルパー
20+ void * getMappedData () const { return info.pMappedData ; }
21+ bool isMapped () const { return info.pMappedData != nullptr ; }
22+ };
23+
24+ struct ImageAllocation {
25+ vk::Image image;
26+ VmaAllocation allocation;
27+ VmaAllocationInfo info;
28+
29+ ImageAllocation () = default ;
30+ ImageAllocation (vk::Image img, VmaAllocation alloc, const VmaAllocationInfo& allocInfo)
31+ : image(img), allocation(alloc), info(allocInfo) {}
32+ };
33+
34+ // メモリ使用方法を指定
35+ enum class MemoryUsage {
36+ GpuOnly, // GPU専用(Device Local)
37+ CpuOnly, // CPU専用(Host Visible)
38+ CpuToGpu, // CPU→GPU転送用(Host Visible + Host Coherent)
39+ GpuToCpu, // GPU→CPU読み取り用(Host Visible + Host Cached)
40+ CpuCopy, // CPU側コピー用(Host Visible + Host Coherent + Host Cached)
41+ GpuLazilyAllocated // 遅延割り当て(Lazily Allocated)
42+ };
43+
44+ // バッファー作成情報
45+ struct BufferCreateInfo {
46+ vk::DeviceSize size;
47+ vk::BufferUsageFlags usage;
48+ MemoryUsage memoryUsage = MemoryUsage::GpuOnly;
49+ bool preferredFlags = false ; // 推奨フラグを優先するか
50+ std::string debugName;
51+ };
52+
53+ // イメージ作成情報
54+ struct ImageCreateInfo {
55+ vk::ImageType imageType = vk::ImageType::e2D;
56+ vk::Format format;
57+ vk::Extent3D extent;
58+ uint32_t mipLevels = 1 ;
59+ uint32_t arrayLayers = 1 ;
60+ vk::SampleCountFlagBits samples = vk::SampleCountFlagBits::e1 ;
61+ vk::ImageTiling tiling = vk::ImageTiling::eOptimal;
62+ vk::ImageUsageFlags usage;
63+ vk::ImageLayout initialLayout = vk::ImageLayout::eUndefined;
64+ MemoryUsage memoryUsage = MemoryUsage::GpuOnly;
65+ std::string debugName;
66+ };
67+
68+ /* *
69+ * VMA (Vulkan Memory Allocator) を使用したメモリ管理クラス
70+ *
71+ * このクラスは以下の問題を解決します:
72+ * 1. 個別VkDeviceMemory割り当てによる制限到達
73+ * 2. メモリフラグメンテーション
74+ * 3. メモリ使用量の最適化
75+ * 4. 自動的なメモリタイプ選択
76+ */
77+ class MemoryManager {
78+ public:
79+ explicit MemoryManager (const Context* context);
80+ ~MemoryManager ();
81+
82+ // コピー・ムーブ禁止
83+ MemoryManager (const MemoryManager&) = delete ;
84+ MemoryManager& operator =(const MemoryManager&) = delete ;
85+ MemoryManager (MemoryManager&&) = delete ;
86+ MemoryManager& operator =(MemoryManager&&) = delete ;
87+
88+ // バッファー作成・破棄
89+ BufferAllocation createBuffer (const BufferCreateInfo& createInfo);
90+ void destroyBuffer (const BufferAllocation& allocation);
91+
92+ // イメージ作成・破棄
93+ ImageAllocation createImage (const ImageCreateInfo& createInfo);
94+ void destroyImage (const ImageAllocation& allocation);
95+
96+ // メモリマッピング
97+ void * mapMemory (const BufferAllocation& allocation);
98+ void unmapMemory (const BufferAllocation& allocation);
99+
100+ // メモリ統計情報
101+ void getMemoryStatistics (VmaTotalStatistics* stats) const ;
102+ void getMemoryBudget (VmaBudget* budget) const ;
103+
104+ // デフラグメンテーション
105+ void defragment ();
106+
107+ // デバッグ情報
108+ void dumpMemoryToJson (const std::string& filePath) const ;
109+
110+ private:
111+ const Context* m_context;
112+ VmaAllocator m_allocator;
113+
114+ // VMA usage フラグ変換
115+ VmaMemoryUsage convertMemoryUsage (MemoryUsage usage) const ;
116+ VmaAllocationCreateFlags getMemoryFlags (MemoryUsage usage) const ;
117+
118+ // デバッグ名設定
119+ void setDebugName (vk::Buffer buffer, const std::string& name) const ;
120+ void setDebugName (vk::Image image, const std::string& name) const ;
121+ };
122+
123+ } // namespace rv
0 commit comments