MXVK Vulkan Framework 0.33.1
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_Textoperator= (const VK_Text &)=delete
VK_Textoperator= (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 71 of file mxvk_text.cpp.

73 : device(dev), physicalDevice(physDev), graphicsQueue(gQueue), commandPool(cmdPool) {
74
75 if (!TTF_Init()) {
76 throw mxvk::Exception("Failed to initialize SDL_ttf: " + std::string(SDL_GetError()));
77 }
78
79 initFont(fontPath, fontSize);
80 }

◆ ~VK_Text()

mxvk::VK_Text::~VK_Text ( )

Destructor – destroys all Vulkan and SDL_ttf resources.

Definition at line 82 of file mxvk_text.cpp.

82 {
83 vkDeviceWaitIdle(device);
84 textQuads.clear();
85 clearCache();
86
87 if (descriptorPool != VK_NULL_HANDLE) {
88 std::cout << "vk: destroying text descriptor pool\n";
89 vkDestroyDescriptorPool(device, descriptorPool, nullptr);
90 }
91
92 if (fontSampler != VK_NULL_HANDLE) {
93 std::cout << "vk: destroying text font sampler\n";
94 vkDestroySampler(device, fontSampler, nullptr);
95 }
96
97 if (font) {
98 std::cout << "mxvk: closing text font\n";
99 TTF_CloseFont(font);
100 }
101 TTF_Quit();
102 }
VkSampler fontSampler
Sampler used for all text textures.
void clearCache()
Destroy all cached text textures, freeing GPU memory.

◆ 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 104 of file mxvk_text.cpp.

104 {
105 for (auto &[key, cached] : textureCache) {
106 destroyCachedTexture(cached);
107 }
108 textureCache.clear();
109 }

◆ clearQueue()

void mxvk::VK_Text::clearQueue ( )

Discard all pending text quads without rendering them.

Definition at line 528 of file mxvk_text.cpp.

528 {
529 if (textQuads.empty()) {
530 return;
531 }
532 if (device == VK_NULL_HANDLE) {
533 textQuads.clear();
534 return;
535 }
536
537 // Text quads own vertex/index buffers referenced by submitted command buffers.
538 // Wait for graphics work to finish before destroying these resources.
539 const VkResult queue_idle_result = vkQueueWaitIdle(graphicsQueue);
540 if (queue_idle_result == VK_ERROR_DEVICE_LOST) {
541 std::cerr << "mxvk: Device lost while clearing queue; skipping text resource reset\n";
542 textQuads.clear();
543 return;
544 }
545 if (queue_idle_result != VK_SUCCESS) {
546 throw mxvk::Exception(std::format(
547 "Fatal : VkResult is \"{}\" in {} at line {}",
548 static_cast<int>(queue_idle_result),
549 __FILE__,
550 __LINE__));
551 }
552
553 textQuads.clear();
554 if (descriptorPool != VK_NULL_HANDLE) {
555 VK_CHECK_RESULT(vkResetDescriptorPool(device, descriptorPool, 0));
556 }
557 pruneCache();
558 }
#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 604 of file mxvk_text.cpp.

604 {
605 return getTextDimensions(text, width, height, font);
606 }
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 619 of file mxvk_text.cpp.

619 {
620 return getTextDimensions(text, width, height, textFont.get());
621 }

◆ getTextDimensions() [3/3]

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

Definition at line 608 of file mxvk_text.cpp.

608 {
609 if (text.empty() || !textFont) {
610 width = 0;
611 height = 0;
612 return false;
613 }
614
615 // SDL3_ttf: TTF_GetStringSize replaces TTF_SizeText; length=0 for null-terminated
616 return TTF_GetStringSize(textFont, text.c_str(), 0, &width, &height);
617 }

◆ 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 298 of file mxvk_text.cpp.

298 {
299 printTextG_SolidWithFont(text, x, y, col, font);
300 }

◆ 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 306 of file mxvk_text.cpp.

306 {
307 printTextG_SolidWithFont(text, x, y, col, textFont.get());
308 }

◆ 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 302 of file mxvk_text.cpp.

302 {
303 printTextG_SolidWithFont(text, x, y, col, textFont);
304 }

◆ 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 505 of file mxvk_text.cpp.

506 {
507
508 struct TextPushConstants {
509 float screenWidth;
510 float screenHeight;
511 float alpha;
512 float padding;
513 } pc{static_cast<float>(screenWidth), static_cast<float>(screenHeight), 1.0f, 0.0f};
514
515 for (auto &quad : textQuads) {
516 pc.alpha = quad.alpha;
517 vkCmdPushConstants(cmdBuffer, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(TextPushConstants), &pc);
518 VkBuffer vertexBuffers[] = {quad.vertexBuffer};
519 VkDeviceSize offsets[] = {0};
520 vkCmdBindVertexBuffers(cmdBuffer, 0, 1, vertexBuffers, offsets);
521 vkCmdBindIndexBuffer(cmdBuffer, quad.indexBuffer, 0, VK_INDEX_TYPE_UINT16);
522 vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &quad.descriptorSet, 0, nullptr);
523
524 vkCmdDrawIndexed(cmdBuffer, quad.indexCount, 1, 0, 0, 0);
525 }
526 }

◆ setDescriptorSetLayout()

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

Assign an external descriptor-set layout.

Definition at line 161 of file mxvk_text.hpp.

161{ 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 260 of file mxvk_text.cpp.

260 {
261 vkDeviceWaitIdle(device);
262 clearQueue();
263 clearCache();
264 if (font) {
265 TTF_CloseFont(font);
266 font = nullptr;
267 }
268 if (fontSampler != VK_NULL_HANDLE) {
269 vkDestroySampler(device, fontSampler, nullptr);
270 fontSampler = VK_NULL_HANDLE;
271 }
272 initFont(fontPath, fontSize);
273 }
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 184 of file mxvk_text.hpp.


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