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
20class StagingBuffer {
21public:
24 StagingBuffer(void) = default;
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 = {};
58 u32 n_reused = 0, n_alloc = 0, n_req_alloc = 0;
59 u64 req_mem = 0;
60 };
62 struct Allocation {
63 DedicatedBuffer *block = {};
64 VkDeviceSize offset = {}, n = {};
65 };
67 struct MappedRegion {
69 VkDeviceSize offset = {}, n = {};
70 };
71 const DedicatedBuffer *cur_block(std::size_t i) const;
84 Allocation alloc(std::size_t i, VkDeviceSize n, VkDeviceSize size);
91 MappedRegion map(std::size_t i, VkDeviceSize n, VkDeviceSize size);
99 std::vector<Frame> frames = {};
100 std::vector<FreeBuffer> free = {};
101 const Instance *inst = {};
102 VkDevice dev = {};
103 DeviceMemory *dev_mem = {};
104 VkDeviceSize default_size = {};
105};
106
107inline void StagingBuffer::init(
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
123inline const DedicatedBuffer *StagingBuffer::cur_block(
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
131inline DedicatedBuffer *StagingBuffer::cur_block(std::size_t i) {
132 return const_cast<DedicatedBuffer*>(
133 static_cast<const StagingBuffer*>(this)->cur_block(i));
134}
135
136inline bool StagingBuffer::write(
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
std::vector< FreeBuffer > free
Definition: staging.h:100
const DedicatedBuffer * cur_block(std::size_t i) const
MappedRegion map(std::size_t i, VkDeviceSize n, VkDeviceSize size)
Maps a writeable region, allocating if necessary.
std::vector< Frame > frames
Definition: staging.h:99
VkDeviceSize default_size
Definition: staging.h:104
void destroy(std::size_t i)
Releases resources for frame i.
Definition: staging.cpp:42
DeviceMemory * dev_mem
Definition: staging.h:103
DedicatedBuffer * cur_block(std::size_t i)
void resize(std::size_t n)
Resizes to accommodate n frames.
Definition: staging.h:117
VkDevice dev
Definition: staging.h:102
Allocation alloc(std::size_t i, VkDeviceSize n, VkDeviceSize size)
Allocates at most n blocks of size bytes for frame i.
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
void retire_free()
Removes old unused buffers from the free list.
StagingBuffer(void)=default
void init(const Instance &inst, VkDevice dev, DeviceMemory *dev_mem, VkDeviceSize default_size)
Definition: staging.h:107
init
Definition: img_common.lua:34
map
Definition: demo1.lua:4
for i
Definition: font.lua:5
assert
Definition: debug.lua:3
n
Definition: dump_lights.lua:5
p
Definition: input.lua:29
function f()) end
stats
Definition: timeline.lua:5
#define NNGN_LOG_CONTEXT_CF(c)
Definition: log.h:11
#define NNGN_MOVE_ONLY(x)
Definition: utils.h:39
Definition: audio.cpp:7
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
std::uint64_t u64
Definition: def.h:15
std::uint8_t u8
Definition: def.h:12
Definition: debug.h:13
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
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)
u32 n_req_alloc
Definition: staging.h:58
void release(VkDevice dev, std::vector< FreeBuffer > *free)
u64 req_mem
Definition: staging.h:59
void * mapped
Definition: staging.h:57
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