GlistEngine
Loading...
Searching...
No Matches
gVKMeshBuffer.h
Go to the documentation of this file.
1/*
2 * gVKMeshBuffer.h
3 *
4 * Static vertex and index buffers of the Vulkan backend, and the mapping that
5 * makes them reachable through the GLuint handles the engine passes around.
6 *
7 * gVbo is written against OpenGL's model: createVAO() hands out a vertex array,
8 * genBuffers() hands out buffer names, and the data lands through
9 * setVertexBufferData / setIndexBufferData while a vertex array is bound. Vulkan
10 * has no vertex array object and no notion of a buffer "name", so the backend keeps
11 * that bookkeeping itself: every id maps to a real VkBuffer here, and every vertex
12 * array remembers which two ids were bound to it, which is exactly what a draw call
13 * needs in order to bind the right buffers.
14 *
15 * Kept apart from gVKDraw's per-frame ring on purpose. That ring is host visible
16 * and rewritten every frame for 2D geometry; these buffers are device local, filled
17 * once through a staging copy, and live as long as the mesh does.
18 *
19 * Created by: Veysel Burak Eroglu.
20 */
21
22#pragma once
23
24#ifndef CORE_GVKMESHBUFFER_H
25#define CORE_GVKMESHBUFFER_H
26
27#include "gVKContext.h"
28
29#ifdef GVK_VULKAN
30
31#include <cstdint>
32#include <utility>
33#include <vector>
34
35/*
36 * One buffer, either vertex or index data. size is the number of bytes actually
37 * uploaded, which the draw path uses to sanity check a draw against the data
38 * behind it.
39 *
40 * There are two shapes here, and which one a buffer takes is decided by how it is
41 * used rather than by anything the caller says:
42 *
43 * - Uploaded once: a device local buffer filled through a staging copy. This is
44 * the overwhelming majority - a mesh loads and is then drawn unchanged - and
45 * device local is the right memory for data the GPU reads every frame.
46 *
47 * - Updated while frames are being recorded, or after it has already been drawn:
48 * the buffer is rebuilt as one host visible copy per frame in flight, and every
49 * later upload is a plain memcpy into the copy belonging to the frame being
50 * recorded. This is what a CPU-skinned mesh does. Repeated setup uploads remain
51 * static, because mesh builders often fill one buffer in several steps.
52 *
53 * The per-frame copies are what makes writing safe without any stall: the frame
54 * loop waits on frame f's fence before recording frame f, so by the time the CPU
55 * writes slot f, every submission that read slot f has finished.
56 */
57struct gVKMeshBuffer {
58 // The buffer a draw should bind. For a static buffer this is the only one; for
59 // a dynamic one gvkResolveMeshBuffer keeps it pointed at the current frame's
60 // slot, so every call site can go on reading this field.
61 VkBuffer buffer = VK_NULL_HANDLE;
62 VkDeviceMemory memory = VK_NULL_HANDLE;
63 // How many bytes the mesh currently holds, and how many its slots were built to
64 // take. They differ because the slots are allocated with room to spare: a mesh
65 // whose vertex count wobbles between draws - a rectangle drawn filled and then
66 // outlined shares one buffer and one index count is longer than the other - would
67 // otherwise reallocate on every draw.
68 VkDeviceSize size = 0;
69 VkDeviceSize capacity = 0;
70 // Which usage flag the buffer was created with. A GLuint name carries no type in
71 // OpenGL, the first upload decides it here.
72 bool isindex = false;
73 // Small buffers live as CPU bytes and take a slice from the shared per-frame
74 // mesh arena when drawn. This avoids one VkDeviceMemory allocation for every
75 // tiny transient primitive; desktop drivers commonly cap allocation count long
76 // before either VRAM or system memory is full.
77 bool isarenaresident = false;
78 std::vector<unsigned char> arenadata;
79 // Everything below is unused until a second upload promotes the buffer.
80 bool isdynamic = false;
81 VkBuffer slotbuffers[GVK_MAX_FRAMES_IN_FLIGHT] = {};
82 VkDeviceMemory slotmemories[GVK_MAX_FRAMES_IN_FLIGHT] = {};
83 void* slotmapped[GVK_MAX_FRAMES_IN_FLIGHT] = {};
84 // Which version of the data each slot currently holds, counted against version,
85 // which every upload advances. A frame that draws the mesh without a fresh
86 // upload finds its slot behind and copies the freshest slot over it, so no frame
87 // draws a pose two frames old.
88 uint64_t slotversion[GVK_MAX_FRAMES_IN_FLIGHT] = {};
89 uint64_t version = 0;
90
91 // Where this mesh's newest data sits inside the per-frame arena, and which
92 // frame that slice belongs to. A mesh drawn without a fresh upload keeps
93 // using its slice for the rest of that frame; once the arena has been rewound
94 // the generation no longer matches and the data is pushed again.
95 VkBuffer arenabuffer = VK_NULL_HANDLE;
96 VkDeviceSize arenaoffset = 0;
97 uint64_t arenageneration = 0;
98 // The frame of this buffer's last upload. A second upload inside the same
99 // frame is what makes an arena slice necessary; a mesh uploaded once a frame
100 // is served by its own per-frame copy, which costs nothing to keep.
101 uint64_t lastuploadgeneration = ~0ull;
102};
103
104/*
105 * What a vertex array id stands for lives in gVKRenderEngine.h as gVKVertexArray:
106 * it holds nothing but two GLuint ids, so it does not belong behind this header's
107 * Vulkan guard.
108 *
109 * ---------------------------------------------------------------------------
110 *
111 * Uploads data into buf, creating or resizing its VkBuffer as needed. The copy goes
112 * through a host visible staging buffer, so the destination can stay device local:
113 * mesh data is written once and read by the GPU every frame, which is the case that
114 * memory type is for.
115 *
116 * Returns false and leaves buf empty if the buffer could not be created. Passing
117 * size 0 or a null pointer frees whatever the id held and returns true.
118 */
119bool gvkUploadMeshBuffer(gVKContext& ctx, gVKMeshBuffer& buf, const void* data,
120 VkDeviceSize size, bool isIndex);
121
122/*
123 * The VkBuffer a draw recorded right now should bind, and the only supported way
124 * to get one: a dynamic buffer has a copy per frame in flight, and this picks the
125 * one belonging to the frame being recorded, refreshing it from the newest data
126 * first if that frame has not seen the latest upload.
127 *
128 * outOffset is the byte offset to bind the vertex buffer from - zero for a
129 * static mesh, and the mesh's slice inside the arena for a dynamic one.
130 *
131 * A static buffer is returned as it is. Returns VK_NULL_HANDLE if there is nothing
132 * to draw from, which callers already check for.
133 */
134VkBuffer gvkResolveMeshBuffer(gVKContext& ctx, gVKMeshBuffer& buf, VkDeviceSize& outOffset);
135
136/*
137 * Frees the device local buffers that promotions replaced. They are held rather
138 * than destroyed at promotion time because the frame being recorded may already
139 * reference them; call this once at shutdown, after the device is idle.
140 */
141void gvkDestroyRetiredMeshBuffers(gVKContext& ctx);
142
143/*
144 * Frees the retired buffers that are old enough to be certain no command buffer
145 * still names them. Called once at the top of a frame; anything younger stays.
146 */
147void gvkCollectRetiredMeshBuffers(gVKContext& ctx);
148
149void gvkDestroyMeshBuffer(gVKContext& ctx, gVKMeshBuffer& buf);
150
151#endif /* GVK_VULKAN */
152
153#endif /* CORE_GVKMESHBUFFER_H */