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::VK_PointSpriteBatch Class Reference

Reusable Vulkan point-list renderer for textured particle effects. More...

#include <mxvk/include/mxvk/mxvk_point_sprite_batch.hpp>

Public Member Functions

size_t capacity () const
void cleanup ()
 Destroy all owned resources and reset the batch to an unloaded state.
void load (VK_Window *window, const std::string &texture_path, const std::string &vertex_shader_path, const std::string &fragment_shader_path, size_t max_vertices)
 Create texture, vertex buffer, descriptors, and point-list pipeline.
bool loaded () const
VK_PointSpriteBatchoperator= (const VK_PointSpriteBatch &)=delete
VK_PointSpriteBatchoperator= (VK_PointSpriteBatch &&)=delete
void render (VkCommandBuffer cmd, uint32_t image_index)
 Record point-sprite draw commands into an active rendering scope.
void resize (VK_Window *window)
 Recreate swapchain-dependent resources after a window resize.
void set_additive_blending (bool enabled)
 Select additive or alpha-over color blending.
void set_depth_test_enabled (bool enabled)
 Enable or disable depth testing in the point-sprite pipeline.
void set_depth_write_enabled (bool enabled)
 Enable or disable depth writes in the point-sprite pipeline.
void update_mvp (uint32_t image_index, const glm::mat4 &mvp)
 Update the MVP uniform for one swapchain image.
void upload_vertices (const PointSpriteVertex *vertices, size_t count)
 Copy point vertices into the persistent mapped vertex buffer.
size_t vertex_count () const
 VK_PointSpriteBatch ()=default
 Construct an empty point-sprite batch.
 VK_PointSpriteBatch (const VK_PointSpriteBatch &)=delete
 VK_PointSpriteBatch (VK_PointSpriteBatch &&)=delete
 ~VK_PointSpriteBatch ()
 Destroy all owned Vulkan resources.

Detailed Description

Reusable Vulkan point-list renderer for textured particle effects.

VK_PointSpriteBatch owns a host-visible vertex buffer, a sampled point texture, per-swapchain MVP uniform buffers, descriptors, and a point-list graphics pipeline using MXVK's dynamic rendering path. It is intended for starfields, sparks, dust, and similar effects where each vertex becomes one shader-expanded point sprite via gl_PointSize/gl_PointCoord.

The caller owns simulation and fills PointSpriteVertex data each frame via upload_vertices(). The batch must be loaded only after VK_Window has created deferred render resources, usually from onRecordCustomRendering().

Definition at line 53 of file mxvk_point_sprite_batch.hpp.

Constructor & Destructor Documentation

◆ VK_PointSpriteBatch() [1/3]

mxvk::VK_PointSpriteBatch::VK_PointSpriteBatch ( )
default

Construct an empty point-sprite batch.

◆ ~VK_PointSpriteBatch()

mxvk::VK_PointSpriteBatch::~VK_PointSpriteBatch ( )

Destroy all owned Vulkan resources.

Definition at line 14 of file mxvk_point_sprite_batch.cpp.

14 {
15 cleanup();
16 }
void cleanup()
Destroy all owned resources and reset the batch to an unloaded state.

◆ VK_PointSpriteBatch() [2/3]

mxvk::VK_PointSpriteBatch::VK_PointSpriteBatch ( const VK_PointSpriteBatch & )
delete

◆ VK_PointSpriteBatch() [3/3]

mxvk::VK_PointSpriteBatch::VK_PointSpriteBatch ( VK_PointSpriteBatch && )
delete

Member Function Documentation

◆ capacity()

size_t mxvk::VK_PointSpriteBatch::capacity ( ) const
inlinenodiscard
Returns
Maximum number of vertices accepted by upload_vertices().

Definition at line 139 of file mxvk_point_sprite_batch.hpp.

139{ return max_vertices; }

◆ cleanup()

void mxvk::VK_PointSpriteBatch::cleanup ( )

Destroy all owned resources and reset the batch to an unloaded state.

Definition at line 78 of file mxvk_point_sprite_batch.cpp.

78 {
79 cleanup_swapchain_resources();
80 destroy_vertex_buffer();
81 destroy_texture(context.device, texture);
82 context = {};
83 pipeline_cache = VK_NULL_HANDLE;
84 color_attachment_format = VK_FORMAT_UNDEFINED;
85 depth_attachment_format = VK_FORMAT_UNDEFINED;
86 image_count = 0;
87 texture_path.clear();
88 vertex_shader_path.clear();
89 fragment_shader_path.clear();
90 max_vertices = 0;
91 active_vertices = 0;
92 batch_loaded = false;
93 }
void destroy_texture(VkDevice device, TextureResource &texture)
Destroy every Vulkan handle owned by a TextureResource.

◆ load()

void mxvk::VK_PointSpriteBatch::load ( VK_Window * window,
const std::string & texture_path,
const std::string & vertex_shader_path,
const std::string & fragment_shader_path,
size_t max_vertices )

Create texture, vertex buffer, descriptors, and point-list pipeline.

Parameters
windowActive MXVK window with ready swapchain/render resources.
texture_pathPNG texture sampled by the fragment shader.
vertex_shader_pathSPIR-V vertex shader path.
fragment_shader_pathSPIR-V fragment shader path.
max_verticesMaximum number of point vertices the batch can draw.
Exceptions
mxvk::Exceptionon invalid input or Vulkan resource failure.

Definition at line 18 of file mxvk_point_sprite_batch.cpp.

22 {
23 if (window == nullptr) {
24 throw mxvk::Exception("VK_PointSpriteBatch::load called with null window");
25 }
26 if (max_vertex_count == 0) {
27 throw mxvk::Exception("VK_PointSpriteBatch::load requires non-zero vertex capacity");
28 }
29
30 cleanup();
31 context = {
32 .device = window->getDevice(),
33 .physical_device = window->getPhysicalDevice(),
34 .graphics_queue = window->getGraphicsQueue(),
35 .command_pool = window->getCommandPool(),
36 };
37 pipeline_cache = window->getPipelineCache();
38 color_attachment_format = window->getSwapchainFormat();
39 depth_attachment_format = window->getDepthFormat();
40 image_count = window->getSwapchainImageCount();
41 texture_path = texture_path_value;
42 vertex_shader_path = vertex_shader_path_value;
43 fragment_shader_path = fragment_shader_path_value;
44 max_vertices = max_vertex_count;
45
46 if (context.device == VK_NULL_HANDLE || context.physical_device == VK_NULL_HANDLE ||
47 context.graphics_queue == VK_NULL_HANDLE || context.command_pool == VK_NULL_HANDLE) {
48 throw mxvk::Exception("Cannot create point-sprite batch before Vulkan render resources are available");
49 }
50 if (color_attachment_format == VK_FORMAT_UNDEFINED || image_count == 0) {
51 throw mxvk::Exception("Cannot create point-sprite batch before swapchain resources are available");
52 }
53
54 create_texture_from_png(context, texture_path, texture);
55 create_vertex_buffer();
56 create_swapchain_resources();
57 batch_loaded = true;
58 }
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.

◆ loaded()

bool mxvk::VK_PointSpriteBatch::loaded ( ) const
inlinenodiscard
Returns
true when load() has completed successfully.

Definition at line 137 of file mxvk_point_sprite_batch.hpp.

137{ return batch_loaded; }

◆ operator=() [1/2]

VK_PointSpriteBatch & mxvk::VK_PointSpriteBatch::operator= ( const VK_PointSpriteBatch & )
delete

◆ operator=() [2/2]

VK_PointSpriteBatch & mxvk::VK_PointSpriteBatch::operator= ( VK_PointSpriteBatch && )
delete

◆ render()

void mxvk::VK_PointSpriteBatch::render ( VkCommandBuffer cmd,
uint32_t image_index )

Record point-sprite draw commands into an active rendering scope.

Parameters
cmdCommand buffer in recording state inside MXVK dynamic rendering.
image_indexCurrent swapchain image index.

Definition at line 118 of file mxvk_point_sprite_batch.cpp.

118 {
119 if (!batch_loaded || active_vertices == 0 || pipeline == VK_NULL_HANDLE || image_index >= descriptor_sets.size()) {
120 return;
121 }
122
123 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
124 VkBuffer buffers[] = {vertex_buffer.buffer};
125 VkDeviceSize offsets[] = {0};
126 vkCmdBindVertexBuffers(cmd, 0, 1, buffers, offsets);
127 vkCmdBindDescriptorSets(
128 cmd,
129 VK_PIPELINE_BIND_POINT_GRAPHICS,
130 pipeline_layout,
131 0,
132 1,
133 &descriptor_sets[image_index],
134 0,
135 nullptr);
136 vkCmdDraw(cmd, static_cast<uint32_t>(active_vertices), 1, 0, 0);
137 }

◆ resize()

void mxvk::VK_PointSpriteBatch::resize ( VK_Window * window)

Recreate swapchain-dependent resources after a window resize.

Parameters
windowActive MXVK window with recreated swapchain resources.

Definition at line 60 of file mxvk_point_sprite_batch.cpp.

60 {
61 if (!batch_loaded || window == nullptr) {
62 return;
63 }
64
65 context.device = window->getDevice();
66 context.physical_device = window->getPhysicalDevice();
67 context.graphics_queue = window->getGraphicsQueue();
68 context.command_pool = window->getCommandPool();
69 pipeline_cache = window->getPipelineCache();
70 color_attachment_format = window->getSwapchainFormat();
71 depth_attachment_format = window->getDepthFormat();
72 image_count = window->getSwapchainImageCount();
73
74 cleanup_swapchain_resources();
75 create_swapchain_resources();
76 }

◆ set_additive_blending()

void mxvk::VK_PointSpriteBatch::set_additive_blending ( bool enabled)

Select additive or alpha-over color blending.

Parameters
enabledtrue for additive blending, false for alpha blending.

Definition at line 139 of file mxvk_point_sprite_batch.cpp.

139 {
140 if (additive_blending == enabled) {
141 return;
142 }
143 additive_blending = enabled;
144 if (batch_loaded) {
145 destroy_pipeline();
146 create_pipeline();
147 }
148 }

◆ set_depth_test_enabled()

void mxvk::VK_PointSpriteBatch::set_depth_test_enabled ( bool enabled)

Enable or disable depth testing in the point-sprite pipeline.

Parameters
enabledtrue to test against the depth attachment.

Definition at line 150 of file mxvk_point_sprite_batch.cpp.

150 {
151 if (depth_test_enabled == enabled) {
152 return;
153 }
154 depth_test_enabled = enabled;
155 if (batch_loaded) {
156 destroy_pipeline();
157 create_pipeline();
158 }
159 }

◆ set_depth_write_enabled()

void mxvk::VK_PointSpriteBatch::set_depth_write_enabled ( bool enabled)

Enable or disable depth writes in the point-sprite pipeline.

Parameters
enabledtrue to write point depth values.

Definition at line 161 of file mxvk_point_sprite_batch.cpp.

161 {
162 if (depth_write_enabled == enabled) {
163 return;
164 }
165 depth_write_enabled = enabled;
166 if (batch_loaded) {
167 destroy_pipeline();
168 create_pipeline();
169 }
170 }

◆ update_mvp()

void mxvk::VK_PointSpriteBatch::update_mvp ( uint32_t image_index,
const glm::mat4 & mvp )

Update the MVP uniform for one swapchain image.

Parameters
image_indexCurrent swapchain image index.
mvpModel-view-projection transform consumed by the vertex shader.

Definition at line 108 of file mxvk_point_sprite_batch.cpp.

108 {
109 if (image_index >= uniform_buffers.size() || uniform_buffers[image_index].mapped == nullptr) {
110 return;
111 }
112
113 UniformBufferObject ubo{};
114 ubo.mvp = mvp;
115 std::memcpy(uniform_buffers[image_index].mapped, &ubo, sizeof(ubo));
116 }

◆ upload_vertices()

void mxvk::VK_PointSpriteBatch::upload_vertices ( const PointSpriteVertex * vertices,
size_t count )

Copy point vertices into the persistent mapped vertex buffer.

Parameters
verticesPointer to count vertices. May be nullptr only when count is zero.
countNumber of vertices to upload and draw.
Exceptions
mxvk::Exceptionif count exceeds capacity().

Definition at line 95 of file mxvk_point_sprite_batch.cpp.

95 {
96 if (!batch_loaded || vertex_buffer.mapped == nullptr || vertices == nullptr || count == 0) {
97 active_vertices = 0;
98 return;
99 }
100 if (count > max_vertices) {
101 throw mxvk::Exception("VK_PointSpriteBatch::upload_vertices exceeds batch capacity");
102 }
103
104 std::memcpy(vertex_buffer.mapped, vertices, count * sizeof(PointSpriteVertex));
105 active_vertices = count;
106 }

◆ vertex_count()

size_t mxvk::VK_PointSpriteBatch::vertex_count ( ) const
inlinenodiscard
Returns
Number of vertices that will be drawn by render().

Definition at line 141 of file mxvk_point_sprite_batch.hpp.

141{ return active_vertices; }

The documentation for this class was generated from the following files: