MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxvk_resource.hpp
Go to the documentation of this file.
1/**
2 * @file mxvk_resource.hpp
3 * @brief Reusable Vulkan buffer, image, upload, and one-shot command helpers.
4 */
5#pragma once
6
7#include "mxvk_context.hpp"
8
9#include <volk/volk.h>
10
11#include <SDL3/SDL.h>
12
13#include <cstddef>
14#include <string>
15
16namespace mxvk {
17
18 /**
19 * @brief Owned Vulkan buffer allocation with optional persistent host mapping.
20 *
21 * The helper functions in this file treat this as a small RAII-like aggregate:
22 * create_buffer() initializes the handles, map_buffer() stores the mapped
23 * pointer, and destroy_buffer() unmaps and releases any live resources.
24 */
26 /** @brief Vulkan buffer handle. */
27 VkBuffer buffer = VK_NULL_HANDLE;
28 /** @brief Device memory bound to @ref buffer. */
29 VkDeviceMemory memory = VK_NULL_HANDLE;
30 /** @brief Requested buffer size in bytes. */
31 VkDeviceSize size = 0;
32 /** @brief Host pointer returned by vkMapMemory, or nullptr when unmapped. */
33 void *mapped = nullptr;
34 };
35
36 /**
37 * @brief Owned sampled 2D texture resources.
38 *
39 * Texture upload helpers create the image, memory, image view, and sampler.
40 * destroy_texture() releases every live member and resets the dimensions.
41 */
43 /** @brief Optimal-tiled image containing the texture pixels. */
44 VkImage image = VK_NULL_HANDLE;
45 /** @brief Device-local memory bound to @ref image. */
46 VkDeviceMemory memory = VK_NULL_HANDLE;
47 /** @brief 2D color image view used by descriptors. */
48 VkImageView view = VK_NULL_HANDLE;
49 /** @brief Sampler configured for linear filtering and clamp-to-edge addressing. */
50 VkSampler sampler = VK_NULL_HANDLE;
51 /** @brief Texture width in pixels. */
52 uint32_t width = 0;
53 /** @brief Texture height in pixels. */
54 uint32_t height = 0;
55 };
56
57 /**
58 * @brief Find a memory type satisfying a Vulkan memory type mask and property set.
59 * @param physical_device Physical device to query.
60 * @param type_filter Memory type bitmask from VkMemoryRequirements.
61 * @param properties Required VkMemoryPropertyFlagBits.
62 * @return Matching memory type index.
63 * @throws mxvk::Exception if no matching memory type is available.
64 */
65 [[nodiscard]] uint32_t find_memory_type(VkPhysicalDevice physical_device,
66 uint32_t type_filter,
67 VkMemoryPropertyFlags properties);
68
69 /**
70 * @brief Create and bind a Vulkan buffer allocation.
71 * @param context Valid Vulkan device and physical device handles.
72 * @param size Buffer size in bytes.
73 * @param usage VkBufferUsageFlagBits describing intended buffer use.
74 * @param properties Required memory properties.
75 * @param buffer Output buffer resource; any existing contents are destroyed first.
76 * @throws mxvk::Exception on invalid input or Vulkan allocation failure.
77 */
78 void create_buffer(const VulkanContext &context,
79 VkDeviceSize size,
80 VkBufferUsageFlags usage,
81 VkMemoryPropertyFlags properties,
82 BufferResource &buffer);
83
84 /**
85 * @brief Unmap and destroy a BufferResource.
86 * @param device Logical device that owns the resource.
87 * @param buffer Resource to release and reset.
88 */
89 void destroy_buffer(VkDevice device, BufferResource &buffer);
90
91 /**
92 * @brief Persistently map a host-visible buffer allocation.
93 * @param device Logical device that owns the buffer memory.
94 * @param buffer Buffer resource to map.
95 * @throws mxvk::Exception if the buffer is invalid or mapping fails.
96 */
97 void map_buffer(VkDevice device, BufferResource &buffer);
98
99 /**
100 * @brief Unmap a BufferResource if it is currently mapped.
101 * @param device Logical device that owns the buffer memory.
102 * @param buffer Buffer resource to unmap.
103 */
104 void unmap_buffer(VkDevice device, BufferResource &buffer);
105
106 /**
107 * @brief Create a 2D VkImage and allocate/bind its memory.
108 * @param context Valid Vulkan device and physical device handles.
109 * @param width Image width in pixels.
110 * @param height Image height in pixels.
111 * @param format Image format.
112 * @param tiling Image tiling mode.
113 * @param usage VkImageUsageFlagBits describing intended image use.
114 * @param properties Required memory properties.
115 * @param image Output image handle.
116 * @param memory Output memory handle bound to @p image.
117 * @throws mxvk::Exception on invalid dimensions or Vulkan allocation failure.
118 */
119 void create_image(const VulkanContext &context,
120 uint32_t width,
121 uint32_t height,
122 VkFormat format,
123 VkImageTiling tiling,
124 VkImageUsageFlags usage,
125 VkMemoryPropertyFlags properties,
126 VkImage &image,
127 VkDeviceMemory &memory);
128
129 /**
130 * @brief Create a 2D image view for an image.
131 * @param device Logical device.
132 * @param image Image to view.
133 * @param format View format.
134 * @param aspect Image aspect mask, usually VK_IMAGE_ASPECT_COLOR_BIT.
135 * @return Created image view handle.
136 * @throws mxvk::Exception if view creation fails.
137 */
138 [[nodiscard]] VkImageView create_image_view(VkDevice device,
139 VkImage image,
140 VkFormat format,
141 VkImageAspectFlags aspect);
142
143 /**
144 * @brief Allocate and begin a primary one-time command buffer.
145 * @param context Valid upload context with device, queue, and command pool.
146 * @return Recording command buffer.
147 * @throws mxvk::Exception if allocation or begin fails.
148 */
149 [[nodiscard]] VkCommandBuffer begin_one_time_commands(const VulkanContext &context);
150
151 /**
152 * @brief End, submit with vkQueueSubmit2, wait idle, and free a one-time command buffer.
153 * @param context Valid upload context with device, queue, and command pool.
154 * @param command_buffer Command buffer returned by begin_one_time_commands().
155 * @throws mxvk::Exception if end, submit, or queue wait fails.
156 */
157 void end_one_time_commands(const VulkanContext &context, VkCommandBuffer command_buffer);
158
159 /**
160 * @brief Copy one buffer into another with a one-shot Vulkan 1.3 copy command.
161 * @param context Valid upload context.
162 * @param source Source buffer.
163 * @param destination Destination buffer.
164 * @param size Bytes to copy.
165 * @throws mxvk::Exception if command allocation, submission, or completion fails.
166 */
167 void copy_buffer(const VulkanContext &context,
168 VkBuffer source,
169 VkBuffer destination,
170 VkDeviceSize size);
171
172 /**
173 * @brief Record a Vulkan 1.3 synchronization2 image layout transition.
174 * @param command_buffer Command buffer in recording state.
175 * @param image Image to transition.
176 * @param old_layout Current image layout.
177 * @param new_layout Desired image layout.
178 * @param aspect Image aspect mask.
179 *
180 * Supported transitions are:
181 * - VK_IMAGE_LAYOUT_UNDEFINED to VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
182 * - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL to VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
183 *
184 * @throws mxvk::Exception for unsupported transitions.
185 */
186 void transition_image_layout(VkCommandBuffer command_buffer,
187 VkImage image,
188 VkImageLayout old_layout,
189 VkImageLayout new_layout,
190 VkImageAspectFlags aspect = VK_IMAGE_ASPECT_COLOR_BIT);
191
192 /**
193 * @brief Record a Vulkan 1.3 vkCmdCopyBufferToImage2 copy for a full 2D image.
194 * @param command_buffer Command buffer in recording state.
195 * @param buffer Source buffer containing tightly packed RGBA pixels.
196 * @param image Destination image in VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL.
197 * @param width Copy width in pixels.
198 * @param height Copy height in pixels.
199 */
200 void copy_buffer_to_image(VkCommandBuffer command_buffer,
201 VkBuffer buffer,
202 VkImage image,
203 uint32_t width,
204 uint32_t height);
205
206 /**
207 * @brief Upload an SDL surface into a sampled 2D texture.
208 * @param context Valid upload context.
209 * @param surface Source surface. Pixels are copied immediately.
210 * @param texture Output texture resource; any existing contents are destroyed first.
211 * @param format Destination image format. Defaults to VK_FORMAT_R8G8B8A8_UNORM.
212 *
213 * The source surface is copied row-by-row into a tight staging buffer to
214 * tolerate SDL pitch padding. The surface is expected to contain 4 bytes per pixel,
215 * as produced by MXVK's PNG loader.
216 *
217 * @throws mxvk::Exception on invalid input, allocation failure, or upload failure.
218 */
219 void create_texture_from_surface(const VulkanContext &context,
220 SDL_Surface *surface,
221 TextureResource &texture,
222 VkFormat format = VK_FORMAT_R8G8B8A8_UNORM);
223
224 /**
225 * @brief Load a PNG and upload it into a sampled 2D texture.
226 * @param context Valid upload context.
227 * @param path PNG path.
228 * @param texture Output texture resource; any existing contents are destroyed first.
229 * @param format Destination image format. Defaults to VK_FORMAT_R8G8B8A8_UNORM.
230 * @throws mxvk::Exception if loading or upload fails.
231 */
232 void create_texture_from_png(const VulkanContext &context,
233 const std::string &path,
234 TextureResource &texture,
235 VkFormat format = VK_FORMAT_R8G8B8A8_UNORM);
236
237 /**
238 * @brief Destroy every Vulkan handle owned by a TextureResource.
239 * @param device Logical device that owns the texture.
240 * @param texture Texture resource to release and reset.
241 */
242 void destroy_texture(VkDevice device, TextureResource &texture);
243
244} // namespace mxvk
Minimal Vulkan handles shared across MXVK helpers.
Utilities for loading and saving PNG images.
Definition mxvk.hpp:30
void transition_image_layout(VkCommandBuffer command_buffer, VkImage image, VkImageLayout old_layout, VkImageLayout new_layout, VkImageAspectFlags aspect=VK_IMAGE_ASPECT_COLOR_BIT)
Record a Vulkan 1.3 synchronization2 image layout transition.
void create_texture_from_png(const VulkanContext &context, const std::string &path, TextureResource &texture, VkFormat format=VK_FORMAT_R8G8B8A8_UNORM)
Load a PNG and upload it into a sampled 2D texture.
void create_texture_from_surface(const VulkanContext &context, SDL_Surface *surface, TextureResource &texture, VkFormat format=VK_FORMAT_R8G8B8A8_UNORM)
Upload an SDL surface into a sampled 2D texture.
void copy_buffer(const VulkanContext &context, VkBuffer source, VkBuffer destination, VkDeviceSize size)
Copy one buffer into another with a one-shot Vulkan 1.3 copy command.
void create_image(const VulkanContext &context, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage &image, VkDeviceMemory &memory)
Create a 2D VkImage and allocate/bind its memory.
VkImageView create_image_view(VkDevice device, VkImage image, VkFormat format, VkImageAspectFlags aspect)
Create a 2D image view for an image.
uint32_t find_memory_type(VkPhysicalDevice physical_device, uint32_t type_filter, VkMemoryPropertyFlags properties)
Find a memory type satisfying a Vulkan memory type mask and property set.
void unmap_buffer(VkDevice device, BufferResource &buffer)
Unmap a BufferResource if it is currently mapped.
void map_buffer(VkDevice device, BufferResource &buffer)
Persistently map a host-visible buffer allocation.
void create_buffer(const VulkanContext &context, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, BufferResource &buffer)
Create and bind a Vulkan buffer allocation.
VkCommandBuffer begin_one_time_commands(const VulkanContext &context)
Allocate and begin a primary one-time command buffer.
void end_one_time_commands(const VulkanContext &context, VkCommandBuffer command_buffer)
End, submit with vkQueueSubmit2, wait idle, and free a one-time command buffer.
void destroy_buffer(VkDevice device, BufferResource &buffer)
Unmap and destroy a BufferResource.
void copy_buffer_to_image(VkCommandBuffer command_buffer, VkBuffer buffer, VkImage image, uint32_t width, uint32_t height)
Record a Vulkan 1.3 vkCmdCopyBufferToImage2 copy for a full 2D image.
void destroy_texture(VkDevice device, TextureResource &texture)
Destroy every Vulkan handle owned by a TextureResource.
Owned Vulkan buffer allocation with optional persistent host mapping.
VkDeviceMemory memory
Device memory bound to buffer.
VkBuffer buffer
Vulkan buffer handle.
VkDeviceSize size
Requested buffer size in bytes.
void * mapped
Host pointer returned by vkMapMemory, or nullptr when unmapped.
Owned sampled 2D texture resources.
uint32_t width
Texture width in pixels.
VkImage image
Optimal-tiled image containing the texture pixels.
VkImageView view
2D color image view used by descriptors.
uint32_t height
Texture height in pixels.
VkSampler sampler
Sampler configured for linear filtering and clamp-to-edge addressing.
VkDeviceMemory memory
Device-local memory bound to image.
Minimal Vulkan handles required by MXVK resource helpers.