#ifndef VULKANRENDERER_H #define VULKANRENDERER_H #include #include #include #include #include // Forward declare Vulkan types typedef struct VkDevice_T* VkDevice; typedef struct VkPhysicalDevice_T* VkPhysicalDevice; typedef struct VkCommandBuffer_T* VkCommandBuffer; typedef struct VkRenderPass_T* VkRenderPass; typedef struct VkFramebuffer_T* VkFramebuffer; typedef struct VkPipeline_T* VkPipeline; typedef struct VkPipelineLayout_T* VkPipelineLayout; typedef struct VkDescriptorSetLayout_T* VkDescriptorSetLayout; typedef struct VkDescriptorPool_T* VkDescriptorPool; typedef struct VkDescriptorSet_T* VkDescriptorSet; typedef struct VkBuffer_T* VkBuffer; typedef struct VkDeviceMemory_T* VkDeviceMemory; typedef struct VkImage_T* VkImage; typedef struct VkImageView_T* VkImageView; typedef struct VkSampler_T* VkSampler; typedef struct VkShaderModule_T* VkShaderModule; typedef struct VkQueue_T* VkQueue; typedef struct VkCommandPool_T* VkCommandPool; /** * @brief 顶点数据结构 */ struct Vertex { float pos[2]; // 位置 (x, y) float color[4]; // 颜色 (r, g, b, a) float texCoord[2]; // 纹理坐标 (u, v) }; /** * @brief Uniform Buffer 数据结构 */ struct UniformBufferObject { float time; // 当前时间/帧数 float resolution[2]; // 分辨率 (width, height) float rotation; // 旋转角度 float wavePhase; // 波浪相位 float padding[2]; // 对齐到 16 字节边界 }; /** * @brief 字符信息结构 */ struct CharInfo { float texCoords[4]; // x, y, width, height in texture atlas float size[2]; // width, height in pixels float bearing[2]; // bearing x, y float advance; // advance for next character }; /** * @brief Vulkan渲染器类 * * 负责所有Vulkan渲染操作,包括几何体、文字、特效等 */ class VulkanRenderer { public: VulkanRenderer(); ~VulkanRenderer(); /** * @brief 初始化渲染器 * @param device Vulkan逻辑设备 * @param physicalDevice Vulkan物理设备 * @param graphicsQueue 图形队列 * @param queueFamilyIndex 队列族索引 * @param swapchainFormat 交换链图像格式 * @param width 渲染目标宽度 * @param height 渲染目标高度 * @return true表示成功 */ bool initialize(VkDevice device, VkPhysicalDevice physicalDevice, VkQueue graphicsQueue, uint32_t queueFamilyIndex, uint32_t swapchainFormat, uint32_t width, uint32_t height); /** * @brief 清理渲染器资源 */ void cleanup(); /** * @brief 调整大小 * @param width 新宽度 * @param height 新高度 * @return true表示成功 */ bool resize(uint32_t width, uint32_t height); /** * @brief 录制渲染命令 * @param commandBuffer 命令缓冲区 * @param imageIndex 交换链图像索引 * @param imageView 交换链图像视图 * @param frameCount 当前帧数 * @param rotationAngle 旋转角度 * @param wavePhase 波浪相位 * @param paintingEnabled 是否启用绘制 * @param lockInfo 锁屏信息 */ void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex, VkImageView imageView, int frameCount, double rotationAngle, double wavePhase, bool paintingEnabled, const std::string& lockInfo); /** * @brief 更新动画参数 * @param frameCount 帧数 * @param rotation 旋转角度 * @param wavePhase 波浪相位 */ void updateAnimationParams(int frameCount, double rotation, double wavePhase); /** * @brief 获取渲染通道 */ VkRenderPass getRenderPass() const { return m_renderPass; } /** * @brief 获取帧缓冲 */ VkFramebuffer getFramebuffer(uint32_t index) const; /** * @brief 设置错误回调 */ void setErrorCallback(void (*callback)(const char*)) { m_errorCallback = callback; } private: /** * @brief 创建渲染通道 */ bool createRenderPass(); /** * @brief 创建帧缓冲 */ bool createFramebuffers(); /** * @brief 创建着色器模块 */ VkShaderModule createShaderModule(const std::vector& code); /** * @brief 创建图形管线(背景渐变) */ bool createBackgroundPipeline(); /** * @brief 创建简单背景管线(fallback) */ bool createBackgroundPipelineSimple(); /** * @brief 创建图形管线(几何体 - 圆圈、波浪) */ bool createGeometryPipeline(); /** * @brief 创建线条渲染管线(用于波浪线) */ bool createLinePipeline(); /** * @brief 创建图形管线(文本渲染) */ bool createTextPipeline(); /** * @brief 创建描述符集布局 */ bool createDescriptorSetLayout(); /** * @brief 创建描述符池 */ bool createDescriptorPool(); /** * @brief 创建描述符集 */ bool createDescriptorSets(); /** * @brief 创建Uniform Buffer */ bool createUniformBuffers(); /** * @brief 更新Uniform Buffer */ void updateUniformBuffer(uint32_t currentImage); /** * @brief 创建顶点缓冲区 */ bool createVertexBuffer(const std::vector& vertices, VkBuffer& buffer, VkDeviceMemory& memory); /** * @brief 创建索引缓冲区 */ bool createIndexBuffer(const std::vector& indices, VkBuffer& buffer, VkDeviceMemory& memory); /** * @brief 创建缓冲区 */ bool createBuffer(uint64_t size, uint32_t usage, uint32_t properties, VkBuffer& buffer, VkDeviceMemory& memory); /** * @brief 复制缓冲区 */ bool copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, uint64_t size); /** * @brief 创建 host-visible 动态顶点缓冲区(可直接映射更新) */ bool createDynamicVertexBuffer(uint64_t size, VkBuffer& buffer, VkDeviceMemory& memory); /** * @brief 创建 host-visible 动态索引缓冲区(可直接映射更新) */ bool createDynamicIndexBuffer(uint64_t size, VkBuffer& buffer, VkDeviceMemory& memory); /** * @brief 更新动态缓冲区内容(通过内存映射) */ bool updateDynamicBuffer(VkDeviceMemory memory, const void* data, uint64_t size); /** * @brief 查找内存类型 */ uint32_t findMemoryType(uint32_t typeFilter, uint32_t properties); /** * @brief 获取最大可用的采样数 */ uint32_t getMaxUsableSampleCount(); /** * @brief 创建多重采样图像资源 */ bool createMSAAResources(); /** * @brief 清理多重采样资源 */ void cleanupMSAAResources(); /** * @brief 初始化文本渲染系统 */ bool initializeTextRendering(); /** * @brief 创建字体纹理图集 */ bool createFontAtlas(); /** * @brief 生成背景四边形顶点 */ void generateBackgroundQuad(std::vector& vertices, std::vector& indices); /** * @brief 生成旋转圆圈顶点 */ void generateRotatingCircles(std::vector& vertices, std::vector& indices, double rotation); /** * @brief 生成波浪效果顶点 */ void generateWaveEffect(std::vector& vertices, std::vector& indices, double wavePhase); /** * @brief 计算文本宽度(像素) */ float calculateTextWidth(const std::string& text, float scale); /** * @brief 生成文本四边形顶点 */ void generateTextQuads(const std::string& text, float x, float y, float scale, const float color[4], std::vector& vertices, std::vector& indices); /** * @brief 绘制背景 */ void drawBackground(VkCommandBuffer commandBuffer, int frameCount); /** * @brief 绘制几何体(圆圈和波浪) */ void drawGeometry(VkCommandBuffer commandBuffer, int frameCount, double rotation, double wavePhase); /** * @brief 绘制文本 */ void drawText(VkCommandBuffer commandBuffer, int frameCount, bool paintingEnabled, const std::string& lockInfo); /** * @brief 记录错误 */ void logError(const char* message); private: // Vulkan 对象 VkDevice m_device; VkPhysicalDevice m_physicalDevice; VkQueue m_graphicsQueue; uint32_t m_queueFamilyIndex; VkCommandPool m_transferCommandPool; VkRenderPass m_renderPass; std::vector m_framebuffers; std::vector m_imageViews; // 管线对象 VkPipeline m_backgroundPipeline; VkPipelineLayout m_backgroundPipelineLayout; VkPipeline m_geometryPipeline; VkPipelineLayout m_geometryPipelineLayout; VkPipeline m_linePipeline; VkPipelineLayout m_linePipelineLayout; VkPipeline m_textPipeline; VkPipelineLayout m_textPipelineLayout; // 描述符对象 VkDescriptorSetLayout m_descriptorSetLayout; VkDescriptorPool m_descriptorPool; std::vector m_descriptorSets; // Uniform Buffer std::vector m_uniformBuffers; std::vector m_uniformBuffersMemory; std::vector m_uniformBuffersMapped; // 几何体缓冲区 VkBuffer m_backgroundVertexBuffer; VkDeviceMemory m_backgroundVertexMemory; VkBuffer m_backgroundIndexBuffer; VkDeviceMemory m_backgroundIndexMemory; uint32_t m_backgroundIndexCount; VkBuffer m_circleVertexBuffer; VkDeviceMemory m_circleVertexMemory; VkBuffer m_circleIndexBuffer; VkDeviceMemory m_circleIndexMemory; uint32_t m_circleIndexCount; VkBuffer m_waveVertexBuffer; VkDeviceMemory m_waveVertexMemory; VkBuffer m_waveIndexBuffer; VkDeviceMemory m_waveIndexMemory; uint32_t m_waveIndexCount; // 文本渲染资源 VkImage m_fontTexture; VkDeviceMemory m_fontTextureMemory; VkImageView m_fontTextureView; VkSampler m_fontSampler; std::map m_charInfoMap; // 文本缓存,避免每帧重建 text buffers std::string m_lastRenderedText; VkBuffer m_textVertexBuffer; VkDeviceMemory m_textVertexMemory; VkBuffer m_textIndexBuffer; VkDeviceMemory m_textIndexMemory; uint32_t m_textIndexCount; // MSAA 资源 VkImage m_msaaImage; VkDeviceMemory m_msaaImageMemory; VkImageView m_msaaImageView; uint32_t m_msaaSamples; // VkSampleCountFlagBits // 渲染目标尺寸 uint32_t m_width; uint32_t m_height; uint32_t m_swapchainFormat; // 动画参数 UniformBufferObject m_ubo; // 初始化状态 bool m_initialized; // 错误回调 void (*m_errorCallback)(const char*); // 最大帧数(支持三缓冲) static const int MAX_FRAMES_IN_FLIGHT = 3; }; #endif // VULKANRENDERER_H