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_Text Class Reference

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

Public Member Functions

void clearCache ()
 Destroy all cached text textures, freeing GPU memory.
void clearQueue ()
 Discard all pending text quads without rendering them.
bool getTextDimensions (const std::string &text, int &width, int &height)
 Measure the pixel dimensions of a string with the current font.
bool getTextDimensions (const std::string &text, int &width, int &height, const Font &textFont)
bool getTextDimensions (const std::string &text, int &width, int &height, TTF_Font *textFont)
VK_Text & operator= (const VK_Text &)=delete
VK_Text & operator= (VK_Text &&)=delete
void printTextG_Solid (const std::string &text, int x, int y, const SDL_Color &col)
 Queue a text string for solid (opaque) rendering.
void printTextG_Solid (const std::string &text, int x, int y, const SDL_Color &col, const Font &textFont)
void printTextG_Solid (const std::string &text, int x, int y, const SDL_Color &col, TTF_Font *textFont)
void renderText (VkCommandBuffer cmdBuffer, VkPipelineLayout pipelineLayout, uint32_t screenWidth, uint32_t screenHeight)
 Record all queued text quads into a command buffer.
void setDescriptorSetLayout (VkDescriptorSetLayout layout)
 Assign an external descriptor-set layout.
void setFont (const std::string &fontPath, int fontSize)
 Change the active font.
 VK_Text (const VK_Text &)=delete
 VK_Text (VK_Text &&)=delete
 VK_Text (VkDevice device, VkPhysicalDevice physicalDevice, VkQueue graphicsQueue, VkCommandPool commandPool, const std::string &fontPath, int fontSize=24)
 Construct VKText and load the font.
 ~VK_Text ()
 Destructor – destroys all Vulkan and SDL_ttf resources.

Public Attributes

VkSampler fontSampler = VK_NULL_HANDLE
 Sampler used for all text textures.

Detailed Description

Definition at line 114 of file mxvk_text.hpp.

Constructor & Destructor Documentation

◆ VK_Text() [1/3]

mxvk::VK_Text::VK_Text ( VkDevice device,
VkPhysicalDevice physicalDevice,
VkQueue graphicsQueue,
VkCommandPool commandPool,
const std::string & fontPath,
int fontSize = 24 )

Construct VKText and load the font.

Parameters
deviceLogical device.
physicalDevicePhysical device.
graphicsQueueGraphics queue.
commandPoolCommand pool for staging.
fontPathPath to the TTF font file.
fontSizePoint size.

Definition at line 63 of file mxvk_text.cpp.

63 : device(dev), physicalDevice(physDev), graphicsQueue(gQueue), commandPool(cmdPool) {
64
65 if (!TTF_Init()) {
66 throw mxvk::Exception("Failed to initialize SDL_ttf: " + std::string(SDL_GetError()));
67 }
68
69 initFont(fontPath, fontSize);
70 }

◆ ~VK_Text()

mxvk::VK_Text::~VK_Text ( )

Destructor – destroys all Vulkan and SDL_ttf resources.

Definition at line 72 of file mxvk_text.cpp.

72 {
73 vkDeviceWaitIdle(device);
74 textQuads.clear();
75 clearCache();
76
77 if (descriptorPool != VK_NULL_HANDLE) {
78 std::cout << "vk: destroying text descriptor pool\n";
79 vkDestroyDescriptorPool(device, descriptorPool, nullptr);
80 }
81
82 if (fontSampler != VK_NULL_HANDLE) {
83 std::cout << "vk: destroying text font sampler\n";
84 vkDestroySampler(device, fontSampler, nullptr);
85 }
86
87 if (font) {
88 std::cout << "mxvk: closing text font\n";
89 TTF_CloseFont(font);
90 }
91 TTF_Quit();
92 }
VkSampler fontSampler
Sampler used for all text textures.
void clearCache()
Destroy all cached text textures, freeing GPU memory.
Definition mxvk_text.cpp:94

◆ VK_Text() [2/3]

mxvk::VK_Text::VK_Text ( const VK_Text & )
delete

◆ VK_Text() [3/3]

mxvk::VK_Text::VK_Text ( VK_Text && )
delete

Member Function Documentation

◆ clearCache()

void mxvk::VK_Text::clearCache ( )

Destroy all cached text textures, freeing GPU memory.

Definition at line 94 of file mxvk_text.cpp.

94 {
95 for (auto &[key, cached] : textureCache) {
96 destroyCachedTexture(cached);
97 }
98 textureCache.clear();
99 }

◆ clearQueue()

void mxvk::VK_Text::clearQueue ( )

Discard all pending text quads without rendering them.

Definition at line 494 of file mxvk_text.cpp.

494 {
495 if (textQuads.empty()) {
496 return;
497 }
498 if (device == VK_NULL_HANDLE) {
499 textQuads.clear();
500 return;
501 }
502
503 // Text quads own vertex/index buffers referenced by submitted command buffers.
504 // Wait for graphics work to finish before destroying these resources.
505 const VkResult queue_idle_result = vkQueueWaitIdle(graphicsQueue);
506 if (queue_idle_result == VK_ERROR_DEVICE_LOST) {
507 std::cerr << "mxvk: Device lost while clearing queue; skipping text resource reset\n";
508 textQuads.clear();
509 return;
510 }
511 if (queue_idle_result != VK_SUCCESS) {
512 throw mxvk::Exception(std::format("Fatal : VkResult is \"{}\" in {} at line {}", static_cast<int>(queue_idle_result), __FILE__, __LINE__));
513 }
514
515 textQuads.clear();
516 if (descriptorPool != VK_NULL_HANDLE) {
517 VK_CHECK_RESULT(vkResetDescriptorPool(device, descriptorPool, 0));
518 }
519 pruneCache();
520 }
#define VK_CHECK_RESULT(f)

◆ getTextDimensions() [1/3]

bool mxvk::VK_Text::getTextDimensions ( const std::string & text,
int & width,
int & height )

Measure the pixel dimensions of a string with the current font.

Parameters
textString to measure.
widthOutput: pixel width.
heightOutput: pixel height.
Returns
true if measurement succeeded.

Definition at line 564 of file mxvk_text.cpp.

564{ return getTextDimensions(text, width, height, font); }
bool getTextDimensions(const std::string &text, int &width, int &height)
Measure the pixel dimensions of a string with the current font.

◆ getTextDimensions() [2/3]

bool mxvk::VK_Text::getTextDimensions ( const std::string & text,
int & width,
int & height,
const Font & textFont )

Definition at line 577 of file mxvk_text.cpp.

577{ return getTextDimensions(text, width, height, textFont.get()); }

◆ getTextDimensions() [3/3]

bool mxvk::VK_Text::getTextDimensions ( const std::string & text,
int & width,
int & height,
TTF_Font * textFont )

Definition at line 566 of file mxvk_text.cpp.

566 {
567 if (text.empty() || !textFont) {
568 width = 0;
569 height = 0;
570 return false;
571 }
572
573 // SDL3_ttf: TTF_GetStringSize replaces TTF_SizeText; length=0 for null-terminated
574 return TTF_GetStringSize(textFont, text.c_str(), 0, &width, &height);
575 }

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ printTextG_Solid() [1/3]

void mxvk::VK_Text::printTextG_Solid ( const std::string & text,
int x,
int y,
const SDL_Color & col )

Queue a text string for solid (opaque) rendering.

Parameters
textString to draw.
xDestination X in pixels.
yDestination Y in pixels.
colText colour.

Definition at line 280 of file mxvk_text.cpp.

280{ printTextG_SolidWithFont(text, x, y, col, font); }

◆ printTextG_Solid() [2/3]

void mxvk::VK_Text::printTextG_Solid ( const std::string & text,
int x,
int y,
const SDL_Color & col,
const Font & textFont )

Definition at line 284 of file mxvk_text.cpp.

284{ printTextG_SolidWithFont(text, x, y, col, textFont.get()); }

◆ printTextG_Solid() [3/3]

void mxvk::VK_Text::printTextG_Solid ( const std::string & text,
int x,
int y,
const SDL_Color & col,
TTF_Font * textFont )

Definition at line 282 of file mxvk_text.cpp.

282{ printTextG_SolidWithFont(text, x, y, col, textFont); }

◆ renderText()

void mxvk::VK_Text::renderText ( VkCommandBuffer cmdBuffer,
VkPipelineLayout pipelineLayout,
uint32_t screenWidth,
uint32_t screenHeight )

Record all queued text quads into a command buffer.

Parameters
cmdBufferActive Vulkan command buffer.
pipelineLayoutPipeline layout for push constants.
screenWidthViewport width.
screenHeightViewport height.

Definition at line 472 of file mxvk_text.cpp.

472 {
473
474 struct TextPushConstants {
475 float screenWidth;
476 float screenHeight;
477 float alpha;
478 float padding;
479 } pc{static_cast<float>(screenWidth), static_cast<float>(screenHeight), 1.0f, 0.0f};
480
481 for (auto &quad : textQuads) {
482 pc.alpha = quad.alpha;
483 vkCmdPushConstants(cmdBuffer, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(TextPushConstants), &pc);
484 VkBuffer vertexBuffers[] = {quad.vertexBuffer};
485 VkDeviceSize offsets[] = {0};
486 vkCmdBindVertexBuffers(cmdBuffer, 0, 1, vertexBuffers, offsets);
487 vkCmdBindIndexBuffer(cmdBuffer, quad.indexBuffer, 0, VK_INDEX_TYPE_UINT16);
488 vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &quad.descriptorSet, 0, nullptr);
489
490 vkCmdDrawIndexed(cmdBuffer, quad.indexCount, 1, 0, 0, 0);
491 }
492 }

◆ setDescriptorSetLayout()

void mxvk::VK_Text::setDescriptorSetLayout ( VkDescriptorSetLayout layout)
inline

Assign an external descriptor-set layout.

Definition at line 159 of file mxvk_text.hpp.

159{ descriptorSetLayout = layout; }

◆ setFont()

void mxvk::VK_Text::setFont ( const std::string & fontPath,
int fontSize )

Change the active font.

Parameters
fontPathNew font file path.
fontSizeNew point size.

Definition at line 242 of file mxvk_text.cpp.

242 {
243 vkDeviceWaitIdle(device);
244 clearQueue();
245 clearCache();
246 if (font) {
247 TTF_CloseFont(font);
248 font = nullptr;
249 }
250 if (fontSampler != VK_NULL_HANDLE) {
251 vkDestroySampler(device, fontSampler, nullptr);
252 fontSampler = VK_NULL_HANDLE;
253 }
254 initFont(fontPath, fontSize);
255 }
void clearQueue()
Discard all pending text quads without rendering them.

Member Data Documentation

◆ fontSampler

VkSampler mxvk::VK_Text::fontSampler = VK_NULL_HANDLE

Sampler used for all text textures.

Definition at line 182 of file mxvk_text.hpp.


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