16#include<vulkan/vulkan.h>
22#include <SDL_vulkan.h>
30#include <unordered_map>
32#ifndef VK_CHECK_RESULT
33#define VK_CHECK_RESULT(f) { \
35 if (res != VK_SUCCESS) { \
36 throw mx::Exception("Fatal : VkResult is \"" + std::to_string(res) + "\" in " + __FILE__ + " at line " + std::to_string(__LINE__)); \
58 return text == other.
text &&
r == other.
r &&
g == other.
g &&
b == other.
b &&
a == other.
a;
65 size_t h = std::hash<std::string>{}(k.
text);
66 h ^= std::hash<uint8_t>{}(k.
r) + 0x9e3779b9 + (h << 6) + (h >> 2);
67 h ^= std::hash<uint8_t>{}(k.
g) + 0x9e3779b9 + (h << 6) + (h >> 2);
68 h ^= std::hash<uint8_t>{}(k.
b) + 0x9e3779b9 + (h << 6) + (h >> 2);
69 h ^= std::hash<uint8_t>{}(k.
a) + 0x9e3779b9 + (h << 6) + (h >> 2);
95 VkCommandPool
commandPool,
const std::string &fontPath,
int fontSize = 24);
112 void printTextG_Solid(
const std::string &text,
int x,
int y,
const SDL_Color &col);
121 void renderText(VkCommandBuffer cmdBuffer, VkPipelineLayout pipelineLayout,
122 uint32_t screenWidth, uint32_t screenHeight);
135 void setFont(
const std::string &fontPath,
int fontSize);
179 *
this = std::move(other);
182 if (
this != &other) {
183 text = std::move(other.text);
189 vertices = std::move(other.vertices);
190 indices = std::move(other.indices);
203 other.ownsTexture =
false;
204 other.vertexBuffer = VK_NULL_HANDLE;
205 other.vertexBufferMemory = VK_NULL_HANDLE;
206 other.indexBuffer = VK_NULL_HANDLE;
207 other.indexBufferMemory = VK_NULL_HANDLE;
208 other.textImage = VK_NULL_HANDLE;
209 other.textImageMemory = VK_NULL_HANDLE;
210 other.textImageView = VK_NULL_HANDLE;
211 other.descriptorSet = VK_NULL_HANDLE;
212 other.indexCount = 0;
213 other.device = VK_NULL_HANDLE;
219 if (
device != VK_NULL_HANDLE) {
251 std::unordered_map<CacheKey, CachedTexture, CacheKeyHash>
textureCache;
253 void initFont(
const std::string &fontPath,
int fontSize);
258 void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage,
259 VkMemoryPropertyFlags properties, VkBuffer& buffer,
260 VkDeviceMemory& bufferMemory);
261 uint32_t
findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
262 void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height);
266 void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling,
267 VkImageUsageFlags usage, VkMemoryPropertyFlags properties,
268 VkImage& image, VkDeviceMemory& imageMemory);
VkPhysicalDevice physicalDevice
void printTextG_Solid(const std::string &text, int x, int y, const SDL_Color &col)
Queue a text string for solid (opaque) rendering.
VkDescriptorSetLayout descriptorSetLayout
VkDescriptorSet createDescriptorSet(VkImageView imageView)
void initFont(const std::string &fontPath, int fontSize)
void growDescriptorPool()
VkCommandBuffer beginSingleTimeCommands()
VKText(const VKText &)=delete
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties)
VkDescriptorPool descriptorPool
VKText & operator=(const VKText &)=delete
VkSampler fontSampler
Sampler used for all text textures.
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory)
void renderText(VkCommandBuffer cmdBuffer, VkPipelineLayout pipelineLayout, uint32_t screenWidth, uint32_t screenHeight)
Record all queued text quads into a command buffer.
void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage &image, VkDeviceMemory &imageMemory)
~VKText()
Destructor – destroys all Vulkan and SDL_ttf resources.
VKText & operator=(VKText &&)=delete
void endSingleTimeCommands(VkCommandBuffer commandBuffer)
std::unordered_map< CacheKey, CachedTexture, CacheKeyHash > textureCache
SDL_Surface * convertToRGBA(SDL_Surface *surface)
void setDescriptorSetLayout(VkDescriptorSetLayout layout)
Assign an external descriptor-set layout.
void createDescriptorPool()
void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height)
void setFont(const std::string &fontPath, int fontSize)
Change the active font.
void clearCache()
Destroy all cached text textures, freeing GPU memory.
VkImageView createImageView(VkImage image, VkFormat format)
VkCommandPool commandPool
void transitionImageLayout(VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout)
bool getTextDimensions(const std::string &text, int &width, int &height)
Measure the pixel dimensions of a string with the current font.
VKText(VkDevice device, VkPhysicalDevice physicalDevice, VkQueue graphicsQueue, VkCommandPool commandPool, const std::string &fontPath, int fontSize=24)
Construct VKText and load the font.
void clearQueue()
Discard all pending text quads without rendering them.
std::vector< TextQuad > textQuads
Exception type used throughout libmx.
Hash functor for CacheKey.
size_t operator()(const CacheKey &k) const
Cache key combining text content and colour.
bool operator==(const CacheKey &other) const
GPU texture resources cached for a rendered text string.
VkDeviceMemory imageMemory
bool ownsTexture
false when texture is owned by the cache.
VkDescriptorSet descriptorSet
VkDeviceMemory vertexBufferMemory
TextQuad & operator=(const TextQuad &)=delete
VkDeviceMemory indexBufferMemory
TextQuad & operator=(TextQuad &&other) noexcept
VkImageView textImageView
TextQuad(TextQuad &&other) noexcept
VkDeviceMemory textImageMemory
std::vector< TextVertex > vertices
TextQuad(const TextQuad &)=delete
std::vector< uint16_t > indices