nngn
Loading...
Searching...
No Matches
staging.h
Go to the documentation of this file.
1#ifndef NNGN_GRAPHICS_VULKAN_STAGING_H
2#define NNGN_GRAPHICS_VULKAN_STAGING_H
3
4#include <limits>
5
6#include "graphics/stats.h"
7#include "utils/log.h"
8
9#include "instance.h"
10#include "resource.h"
11#include "vulkan.h"
12
13namespace nngn {
14
21public:
24 StagingBuffer(void) = default;
26 ~StagingBuffer(void);
27 void init(
28 const Instance &inst, VkDevice dev, DeviceMemory *dev_mem,
29 VkDeviceSize default_size);
30 Stats stats(std::size_t i) const;
32 void resize(std::size_t n);
46 bool write(std::size_t i, VkDeviceSize n, VkDeviceSize size, auto &&f);
48 void destroy(std::size_t i);
49private:
51 struct FreeBuffer : DedicatedBuffer { u8 age = {}; };
53 struct Frame {
54 void release(VkDevice dev, std::vector<FreeBuffer> *free);
55 void destroy(VkDevice dev, DeviceMemory *dev_mem);
56 std::vector<DedicatedBuffer> blocks = {};
57 void *mapped = {};
60 };
62 struct Allocation {
64 VkDeviceSize offset = {}, n = {};
65 };
67 struct MappedRegion {
69 VkDeviceSize offset = {}, n = {};
70 };
71 const DedicatedBuffer *cur_block(std::size_t i) const;
72 DedicatedBuffer *cur_block(std::size_t i);
84 Allocation alloc(std::size_t i, VkDeviceSize n, VkDeviceSize size);
91 MappedRegion map(std::size_t i, VkDeviceSize n, VkDeviceSize size);
98 void retire_free();
99 std::vector<Frame> frames = {};
100 std::vector<FreeBuffer> free = {};
101 const Instance *inst = {};
102 VkDevice dev = {};
104 VkDeviceSize default_size = {};
105};
106
108 const Instance &inst_, VkDevice dev_, DeviceMemory *dev_mem_,
109 VkDeviceSize default_size_
110) {
111 this->inst = &inst_;
112 this->dev = dev_;
113 this->dev_mem = dev_mem_;
114 this->default_size = default_size_;
115}
116
117inline void StagingBuffer::resize(std::size_t n) {
118 for(std::size_t i = n, old = this->frames.size(); i < old; ++i)
119 this->destroy(i);
120 this->frames.resize(n);
121}
122
124 std::size_t i
125) const {
126 assert(i < this->frames.size());
127 auto &f = this->frames[i];
128 return f.blocks.empty() ? nullptr : &f.blocks.back();
129}
130
132 return const_cast<DedicatedBuffer*>(
133 static_cast<const StagingBuffer*>(this)->cur_block(i));
134}
135
137 std::size_t i, VkDeviceSize n, VkDeviceSize size, auto &&f
138) {
140 for(std::size_t bi = 0; n;) {
141 auto [mem, off, nw] = this->map(i, n, size);
142 if(!mem)
143 return false;
144 void *const p = static_cast<char*>(this->frames[i].mapped) + off;
145 if(!f(this->cur_block(i)->id(), p, off, bi, nw))
146 return false;
147 bi += static_cast<std::size_t>(nw);
148 n -= static_cast<std::size_t>(nw);
149 }
150 return true;
151}
152
153}
154
155#endif
Definition: resource.h:41
Manages device memory queries, allocations, and lifetime.
Definition: memory.h:20
Owning wrapper for a Vulkan instance.
Definition: instance.h:19
Manages staging buffers for a group of frames.
Definition: staging.h:20
const DedicatedBuffer * cur_block(std::size_t i) const
Definition: staging.h:123
VkDeviceSize default_size
Definition: staging.h:104
void destroy(std::size_t i)
Releases resources for frame i.
Definition: staging.cpp:42
std::vector< Frame > frames
Definition: staging.h:99
Allocation alloc(std::size_t i, VkDeviceSize n, VkDeviceSize size)
Allocates at most n blocks of size bytes for frame i.
Definition: staging.cpp:78
void resize(std::size_t n)
Resizes to accommodate n frames.
Definition: staging.h:117
VkDevice dev
Definition: staging.h:102
const Instance * inst
Definition: staging.h:101
bool write(std::size_t i, VkDeviceSize n, VkDeviceSize size, auto &&f)
Writes data for a frame, allocating extra memory as necessary.
Definition: staging.h:136
DeviceMemory * dev_mem
Definition: staging.h:103
void retire_free()
Removes old unused buffers from the free list.
Definition: staging.cpp:48
std::vector< FreeBuffer > free
Definition: staging.h:100
void init(const Instance &inst, VkDevice dev, DeviceMemory *dev_mem, VkDeviceSize default_size)
Definition: staging.h:107
assert
Definition: debug.lua:3
#define NNGN_LOG_CONTEXT_CF(c)
Definition: log.h:11
Definition: debug.h:45
std::uint64_t u64
Definition: def.h:15
std::uint8_t u8
Definition: def.h:12
std::uint32_t u32
Definition: def.h:14
COMMAND_BUFFER DESCRIPTOR_POOL DESCRIPTOR_SET_LAYOUT FENCE IMAGE PIPELINE PIPELINE_CACHE RENDER_PASS SEMAPHORE BUFFER COMMAND_POOL DESCRIPTOR_SET VkDeviceMemory
Definition: types.h:77
Definition: debug.h:13
release
Definition: input.lua:33
function f()) end
map
Definition: utils.lua:45
Definition: stats.h:11
Result of a block allocation.
Definition: staging.h:62
DedicatedBuffer * block
Definition: staging.h:63
VkDeviceSize n
Definition: staging.h:64
VkDeviceSize offset
Definition: staging.h:64
Data for a single frame.
Definition: staging.h:53
u32 n_reused
Definition: staging.h:58
u32 n_alloc
Definition: staging.h:58
std::vector< DedicatedBuffer > blocks
Definition: staging.h:56
void destroy(VkDevice dev, DeviceMemory *dev_mem)
Definition: staging.cpp:26
u32 n_req_alloc
Definition: staging.h:58
void * mapped
Definition: staging.h:57
u64 req_mem
Definition: staging.h:59
Buffer in the free list, ready to be reused.
Definition: staging.h:51
Region mapped for writting.
Definition: staging.h:67
VkDeviceSize n
Definition: staging.h:69
VkDeviceMemory mem
Definition: staging.h:68
VkDeviceSize offset
Definition: staging.h:69
stats
Definition: timeline.lua:5
#define NNGN_MOVE_ONLY(x)
Definition: utils.h:39