Files
SDL3CPlusPlus/src/services/impl/gui_renderer.hpp
johndoe6345789 5e18571856 feat: Enhance GUI rendering and configuration management
- Added support for FreeType font rendering in GuiRenderer, including glyph loading and text rendering capabilities.
- Introduced GuiFontConfig structure to manage font settings through configuration.
- Updated JsonConfigService to parse and provide GUI font settings from JSON configuration.
- Implemented MaterialX shader generation capabilities with a new MaterialXShaderGenerator class.
- Enhanced ShaderScriptService to utilize MaterialX for shader generation based on configuration.
- Added NullGuiService as a placeholder implementation for GUI service.
- Extended IConfigService interface to include methods for retrieving graphics backend, MaterialX, and GUI font configurations.
- Updated RuntimeConfig structure to include graphics backend and MaterialX configurations.
- Added tests to ensure proper integration of new configuration settings and shader generation functionality.
2026-01-06 14:38:42 +00:00

127 lines
4.2 KiB
C++

#ifndef SDL3CPP_SERVICES_GUI_RENDERER_HPP
#define SDL3CPP_SERVICES_GUI_RENDERER_HPP
#include <filesystem>
#include <memory>
#include <unordered_map>
#include <vector>
#include <cstdint>
#include <vulkan/vulkan.h>
#include "services/interfaces/config_types.hpp"
#include "services/interfaces/gui_types.hpp"
#include "services/interfaces/i_buffer_service.hpp"
#include "services/interfaces/i_logger.hpp"
namespace sdl3cpp::services::impl {
struct GuiVertex {
float x, y, z;
float r, g, b, a;
};
struct SvgCircle {
float cx = 0.0f;
float cy = 0.0f;
float r = 0.0f;
GuiColor color{1.0f, 1.0f, 1.0f, 1.0f};
};
struct ParsedSvg {
float viewWidth = 1.0f;
float viewHeight = 1.0f;
std::vector<SvgCircle> circles;
};
class GuiRenderer {
public:
GuiRenderer(VkDevice device, VkPhysicalDevice physicalDevice, VkFormat swapchainFormat,
VkRenderPass renderPass, const std::filesystem::path& scriptDirectory,
const GuiFontConfig& fontConfig,
std::shared_ptr<IBufferService> bufferService, std::shared_ptr<ILogger> logger);
~GuiRenderer();
GuiRenderer(const GuiRenderer&) = delete;
GuiRenderer& operator=(const GuiRenderer&) = delete;
void Prepare(const std::vector<GuiCommand>& commands, uint32_t width,
uint32_t height);
void RenderToSwapchain(VkCommandBuffer commandBuffer, VkRenderPass renderPass);
void Resize(uint32_t width, uint32_t height, VkFormat format, VkRenderPass renderPass);
bool IsReady() const;
private:
struct GlyphBitmap {
int width = 0;
int height = 0;
int pitch = 0;
int bearingX = 0;
int bearingY = 0;
int advance = 0;
std::vector<uint8_t> pixels;
};
const ParsedSvg* LoadSvg(const std::string& relativePath);
bool EnsureFreeTypeReady();
std::filesystem::path ResolveFontPath() const;
const GlyphBitmap* LoadGlyph(char c, int pixelSize);
void RenderFreeTypeText(const GuiCommand& cmd,
const GuiCommand::RectData& activeClip,
const GuiCommand::RectData& bounds);
void AddQuad(const GuiCommand::RectData& rect,
const GuiColor& color,
const GuiCommand::RectData& clipRect);
void CreatePipeline(VkRenderPass renderPass, VkExtent2D extent);
void CreateVertexAndIndexBuffers(size_t vertexCount, size_t indexCount);
void CleanupPipeline();
void CleanupBuffers();
void UpdateFormat(VkFormat format);
void GenerateGuiGeometry(const std::vector<GuiCommand>& commands, uint32_t width, uint32_t height);
const std::vector<uint8_t>& LoadShaderBytes(const std::filesystem::path& path,
VkShaderStageFlagBits stage);
const std::vector<uint8_t>& LoadShaderBytes(const std::string& cacheKey,
const std::string& source,
VkShaderStageFlagBits stage);
VkDevice device_;
VkPhysicalDevice physicalDevice_;
VkFormat swapchainFormat_;
VkRenderPass renderPass_;
std::filesystem::path scriptDirectory_;
GuiFontConfig fontConfig_;
// Pipeline resources
VkPipeline pipeline_ = VK_NULL_HANDLE;
VkPipelineLayout pipelineLayout_ = VK_NULL_HANDLE;
VkShaderModule vertShaderModule_ = VK_NULL_HANDLE;
VkShaderModule fragShaderModule_ = VK_NULL_HANDLE;
// Vertex/index buffers
VkBuffer vertexBuffer_ = VK_NULL_HANDLE;
VkDeviceMemory vertexMemory_ = VK_NULL_HANDLE;
VkBuffer indexBuffer_ = VK_NULL_HANDLE;
VkDeviceMemory indexMemory_ = VK_NULL_HANDLE;
// Geometry data
std::vector<GuiVertex> vertices_;
std::vector<uint32_t> indices_;
uint32_t viewportWidth_ = 0;
uint32_t viewportHeight_ = 0;
std::unordered_map<std::string, ParsedSvg> svgCache_;
std::unordered_map<std::string, std::vector<uint8_t>> shaderSpirvCache_;
std::unordered_map<uint64_t, GlyphBitmap> glyphCache_;
void* ftLibrary_ = nullptr;
void* ftFace_ = nullptr;
int currentFontSize_ = 0;
bool freetypeReady_ = false;
std::shared_ptr<IBufferService> bufferService_;
std::shared_ptr<ILogger> logger_;
};
} // namespace sdl3cpp::services::impl
#endif // SDL3CPP_SERVICES_GUI_RENDERER_HPP