载入中...
搜索中...
未找到
FrameArena.cpp
浏览该文件的文档.
2
3namespace eve::graphics::vulkan {
4
6 // GenericBuffer releases its memory; unmap the persistent mapping first.
7 if (mapped_ && device_.instance) {
8 device_->unmapMemory(buf_.memory);
9 }
10 mapped_ = nullptr;
11}
12
14 if (this != &o) {
15 if (mapped_ && device_.instance) device_->unmapMemory(buf_.memory);
16 mapped_ = o.mapped_;
17 o.mapped_ = nullptr;
18 device_ = o.device_;
19 buf_ = std::move(o.buf_);
20 capacity_ = o.capacity_;
21 head_ = o.head_;
22 o.capacity_ = 0;
23 o.head_ = 0;
24 }
25 return *this;
26}
27
28bool FrameArena::ensure(vkb::Device &device, vk::DeviceSize bytes, vk::BufferUsageFlags usage) {
29 if (capacity_ >= bytes) return true;
30 if (!device.instance) return false;
31
32 const vk::DeviceSize grow = (capacity_ > 0) ? capacity_ : 1;
33 vk::DeviceSize target = bytes;
34 while (target < bytes) target += grow; // grow at least by current capacity
35 target = std::max<vk::DeviceSize>(target, 1 << 20);
36
37 if (mapped_ && device_.instance) device_->unmapMemory(buf_.memory);
38 mapped_ = nullptr;
39 vkb::GenericBuffer next(device, usage, target,
40 vk::MemoryPropertyFlagBits::eHostVisible |
41 vk::MemoryPropertyFlagBits::eHostCoherent);
42 buf_ = std::move(next);
43 device_ = device;
44 capacity_ = target;
45 head_ = 0;
46 mapped_ = device->mapMemory(buf_.memory, 0, capacity_);
47 return true;
48}
49
50FrameArena::Alloc FrameArena::alloc(vk::DeviceSize bytes, vk::DeviceSize align) {
51 Alloc out{};
52 if (bytes == 0) return out;
53 const vk::DeviceSize aligned = ((head_ + align - 1) / align) * align;
54 if (aligned + bytes > capacity_) return out; // overflow: caller truncates + counts
55 head_ = aligned + bytes;
56 out.offset = aligned;
57 out.size = bytes;
58 out.mapped = mapped_ ? static_cast<char *>(mapped_) + aligned : nullptr;
59 return out;
60}
61
63 head_ = 0;
64}
65
66vk::Buffer FrameArena::buffer() const {
67 return buf_.buffer;
68}
69
70} // namespace eve::graphics::vulkan
vkb::Device & device
Per-frame GPU allocation arena (host-visible coherent).
Definition FrameArena.h:18
bool ensure(vkb::Device &device, vk::DeviceSize bytes, vk::BufferUsageFlags usage)
Allocate or grow (before any recording) so bytes fit.
Alloc alloc(vk::DeviceSize bytes, vk::DeviceSize align=16)
Allocate bytes (aligned). Returns empty Alloc on capacity overflow.
FrameArena & operator=(const FrameArena &)=delete