ScreenLockDetector/src/vulkanrenderer.h

419 lines
11 KiB
C
Raw Normal View History

#ifndef VULKANRENDERER_H
#define VULKANRENDERER_H
#include <vector>
#include <string>
#include <map>
#include <memory>
#include <cstdint>
// 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<char>& 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<Vertex>& vertices,
VkBuffer& buffer, VkDeviceMemory& memory);
/**
* @brief
*/
bool createIndexBuffer(const std::vector<uint16_t>& 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);
2025-11-10 21:43:42 +08:00
/**
* @brief
*/
uint32_t getMaxUsableSampleCount();
/**
* @brief
*/
bool createMSAAResources();
/**
* @brief
*/
void cleanupMSAAResources();
/**
* @brief
*/
bool initializeTextRendering();
/**
* @brief
*/
bool createFontAtlas();
/**
* @brief
*/
void generateBackgroundQuad(std::vector<Vertex>& vertices,
std::vector<uint16_t>& indices);
/**
* @brief
*/
void generateRotatingCircles(std::vector<Vertex>& vertices,
std::vector<uint16_t>& indices,
double rotation);
/**
* @brief
*/
void generateWaveEffect(std::vector<Vertex>& vertices,
std::vector<uint16_t>& indices,
double wavePhase);
2025-11-10 21:01:35 +08:00
/**
* @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<Vertex>& vertices,
std::vector<uint16_t>& indices);
/**
* @brief
*/
void drawBackground(VkCommandBuffer commandBuffer, int frameCount);
/**
* @brief
*/
2025-11-10 17:01:06 +08:00
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<VkFramebuffer> m_framebuffers;
std::vector<VkImageView> 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<VkDescriptorSet> m_descriptorSets;
// Uniform Buffer
std::vector<VkBuffer> m_uniformBuffers;
std::vector<VkDeviceMemory> m_uniformBuffersMemory;
std::vector<void*> 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<char, CharInfo> m_charInfoMap;
// 文本缓存,避免每帧重建 text buffers
std::string m_lastRenderedText;
VkBuffer m_textVertexBuffer;
VkDeviceMemory m_textVertexMemory;
VkBuffer m_textIndexBuffer;
VkDeviceMemory m_textIndexMemory;
uint32_t m_textIndexCount;
2025-11-10 21:43:42 +08:00
// 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