GlistEngine
Loading...
Searching...
No Matches
gVKBuffer.h
Go to the documentation of this file.
1/*
2 * gVKBuffer.h
3 *
4 * Small allocation helpers shared by the Vulkan draw path: creating buffers with
5 * a matching memory type, and the batched upload path that carries transfer work
6 * (buffer copies, image layout transitions) to the GPU. Kept separate from the
7 * frame loop so both the vertex ring (gVKDraw) and textures (gVKTexture) reuse it.
8 */
9
10#pragma once
11
12#ifndef CORE_GVKBUFFER_H
13#define CORE_GVKBUFFER_H
14
15#include "gVKContext.h"
16
17#ifdef GVK_VULKAN
18
19// Picks a memory type index that is allowed by typeFilter and exposes all of the
20// requested property flags (e.g. host visible + coherent, or device local).
21uint32_t gvkFindMemoryType(gVKContext& ctx, uint32_t typeFilter, VkMemoryPropertyFlags properties);
22
23// Creates a buffer and backs it with freshly allocated memory. Returns false and
24// leaves the handles at VK_NULL_HANDLE on failure.
25//
26// preferred names memory the caller would rather have; when no memory type offers
27// it the allocation falls back to properties, which is what it must have. It exists
28// for buffers the CPU writes and the GPU reads every frame: on a device that
29// exposes device local memory as host visible those belong there rather than in
30// system RAM, which the GPU can only reach across the bus.
31bool gvkCreateBuffer(gVKContext& ctx, VkDeviceSize size, VkBufferUsageFlags usage,
32 VkMemoryPropertyFlags properties, VkBuffer& outBuffer, VkDeviceMemory& outMemory,
33 VkMemoryPropertyFlags preferred = 0);
34
35// Transfer work - staging copies into device local buffers and images, and the
36// layout transitions around them - is recorded into a shared batch rather than
37// submitted on its own. gvkBeginUpload returns a command buffer that is already
38// recording; record into it and hand it back with gvkEndUpload, naming the
39// staging buffer the recorded commands read. The batch owns that staging buffer
40// from then on and destroys it once the submission it went into has finished, so
41// a caller must never destroy it itself.
42//
43// Returns VK_NULL_HANDLE when no batch could be opened, in which case nothing was
44// recorded and gvkEndUpload must not be called.
45VkCommandBuffer gvkBeginUpload(gVKContext& ctx);
46void gvkEndUpload(gVKContext& ctx, VkBuffer stagingBuffer = VK_NULL_HANDLE,
47 VkDeviceMemory stagingMemory = VK_NULL_HANDLE, VkDeviceSize stagingSize = 0);
48
49// Copies size bytes into a device local buffer through staging the upload path
50// owns, and leaves a barrier making the result visible to vertex and index reads.
51bool gvkUploadBufferData(gVKContext& ctx, VkBuffer dst, const void* data, VkDeviceSize size);
52
53// Submits whatever is recorded. The frame loop calls this before submitting the
54// frame, which is what makes uploads recorded during that frame visible to it:
55// barriers apply to everything submitted later on the same queue, so no host wait
56// is involved.
57void gvkFlushUploads(gVKContext& ctx);
58
59// Frees the staging of batches the GPU has finished with. Cheap, and meant to run
60// once per frame.
61void gvkCollectUploads(gVKContext& ctx);
62
63// Submits and waits for every outstanding upload. Needed before destroying
64// anything a recorded-but-unsubmitted transfer names, which vkDeviceWaitIdle
65// cannot cover because that work has not reached the queue yet.
66void gvkWaitUploads(gVKContext& ctx);
67
68void gvkDestroyUploadContext(gVKContext& ctx);
69
70#endif /* GVK_VULKAN */
71
72#endif /* CORE_GVKBUFFER_H */