MXVK Vulkan Framework 0.35.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_PointSpriteBatch & operator= (const VK_PointSpriteBatch &)=delete
VK_PointSpriteBatch & operator= (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{ cleanup(); }
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 135 of file mxvk_point_sprite_batch.hpp.

135{ return max_vertices; }

◆ cleanup()

void mxvk::VK_PointSpriteBatch::cleanup ( )

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

Definition at line 71 of file mxvk_point_sprite_batch.cpp.

71 {
72 cleanup_swapchain_resources();
73 destroy_vertex_buffer();
74 destroy_texture(context.device, texture);
75 context = {};
76 pipeline_cache = VK_NULL_HANDLE;
77 color_attachment_format = VK_FORMAT_UNDEFINED;
78 depth_attachment_format = VK_FORMAT_UNDEFINED;
79 image_count = 0;
80 texture_path.clear();
81 vertex_shader_path.clear();
82 fragment_shader_path.clear();
83 max_vertices = 0;
84 active_vertices = 0;
85 batch_loaded = false;
86 }
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 16 of file mxvk_point_sprite_batch.cpp.

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

133{ 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 111 of file mxvk_point_sprite_batch.cpp.

111 {
112 if (!batch_loaded || active_vertices == 0 || pipeline == VK_NULL_HANDLE || image_index >= descriptor_sets.size()) {
113 return;
114 }
115
116 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
117 VkBuffer buffers[] = {vertex_buffer.buffer};
118 VkDeviceSize offsets[] = {0};
119 vkCmdBindVertexBuffers(cmd, 0, 1, buffers, offsets);
120 vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout, 0, 1, &descriptor_sets[image_index], 0, nullptr);
121 vkCmdDraw(cmd, static_cast<uint32_t>(active_vertices), 1, 0, 0);
122 }

◆ 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 53 of file mxvk_point_sprite_batch.cpp.

53 {
54 if (!batch_loaded || window == nullptr) {
55 return;
56 }
57
58 context.device = window->getDevice();
59 context.physical_device = window->getPhysicalDevice();
60 context.graphics_queue = window->getGraphicsQueue();
61 context.command_pool = window->getCommandPool();
62 pipeline_cache = window->getPipelineCache();
63 color_attachment_format = window->getSwapchainFormat();
64 depth_attachment_format = window->getDepthFormat();
65 image_count = window->getSwapchainImageCount();
66
67 cleanup_swapchain_resources();
68 create_swapchain_resources();
69 }

◆ 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 124 of file mxvk_point_sprite_batch.cpp.

124 {
125 if (additive_blending == enabled) {
126 return;
127 }
128 additive_blending = enabled;
129 if (batch_loaded) {
130 destroy_pipeline();
131 create_pipeline();
132 }
133 }

◆ 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 135 of file mxvk_point_sprite_batch.cpp.

135 {
136 if (depth_test_enabled == enabled) {
137 return;
138 }
139 depth_test_enabled = enabled;
140 if (batch_loaded) {
141 destroy_pipeline();
142 create_pipeline();
143 }
144 }

◆ 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 146 of file mxvk_point_sprite_batch.cpp.

146 {
147 if (depth_write_enabled == enabled) {
148 return;
149 }
150 depth_write_enabled = enabled;
151 if (batch_loaded) {
152 destroy_pipeline();
153 create_pipeline();
154 }
155 }

◆ 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 101 of file mxvk_point_sprite_batch.cpp.

101 {
102 if (image_index >= uniform_buffers.size() || uniform_buffers[image_index].mapped == nullptr) {
103 return;
104 }
105
106 UniformBufferObject ubo{};
107 ubo.mvp = mvp;
108 std::memcpy(uniform_buffers[image_index].mapped, &ubo, sizeof(ubo));
109 }

◆ 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 88 of file mxvk_point_sprite_batch.cpp.

88 {
89 if (!batch_loaded || vertex_buffer.mapped == nullptr || vertices == nullptr || count == 0) {
90 active_vertices = 0;
91 return;
92 }
93 if (count > max_vertices) {
94 throw mxvk::Exception("VK_PointSpriteBatch::upload_vertices exceeds batch capacity");
95 }
96
97 std::memcpy(vertex_buffer.mapped, vertices, count * sizeof(PointSpriteVertex));
98 active_vertices = count;
99 }

◆ vertex_count()

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

Definition at line 137 of file mxvk_point_sprite_batch.hpp.

137{ return active_vertices; }

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