Files
face_sdk/vulkan/Application.h
T
2025-12-18 22:05:10 +08:00

108 lines
4.5 KiB
C++

#pragma once
#include "AppBase.h"
#include <thread>
#include <mutex>
struct Texture
{
VkSampler sampler;
VkImage image;
VkImageLayout image_layout;
VkDeviceMemory device_memory;
VkImageView view;
uint32_t width, height;
uint32_t mip_levels;
VkBuffer stagingBuffer = VK_NULL_HANDLE;
VkDeviceMemory stagingBufferMemory = VK_NULL_HANDLE;
VkDevice device;
void reset() {
sampler = VK_NULL_HANDLE;
image = VK_NULL_HANDLE;
device_memory = VK_NULL_HANDLE;
view = VK_NULL_HANDLE;
stagingBuffer = VK_NULL_HANDLE;
stagingBufferMemory = VK_NULL_HANDLE;
}
};
class Application : public AppBase
{
public:
virtual void initVulkan();
void initWindow();
void createSurface();
void mainLoop();
virtual void cleanup();
void createImageViews();
void createFramebuffers();
void createCommandPool();
void createCommandBuffer();
void createLogicalDevice();
void createSwapChain();
void createPipelineLayout();
void createGraphicsPipeline();
void createRenderPass();
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex, long long frameTime);
void createSyncObjects();
virtual void drawFrame(long long frameTime);
virtual void render(VkCommandBuffer commandBuffer, long long frameTime);
virtual bool isInited() { return inited; }
void processWithVulkan(uint8_t* data, int width, int height, int rowStride, size_t dataSize, Texture& out_texture, bool srgb, VkCommandPool pool);
protected:
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; // 物理设备
VkDevice device; // 逻辑设备
VkQueue graphicsQueue; // 图形队列
VkSurfaceKHR surface; // 窗口表面
VkSwapchainKHR swapChain; // 交换链
std::vector<VkImage> swapChainImages; // 交换链图像
VkFormat swapChainImageFormat; // 交换链图像格式
VkExtent2D swapChainExtent; // 交换链图像分辨率
std::vector<VkImageView> swapChainImageViews; // 交换链图像视图
VkRenderPass renderPass; // 渲染流程
VkPipelineLayout pipelineLayout; // 管线布局
VkPipeline graphicsPipeline; // 图形管线
std::vector<VkFramebuffer> swapChainFramebuffers; // 帧缓冲区
VkQueue presentQueue; // 呈现队列
public:
VkCommandPool commandPool;
VkCommandPool commandPool_ex;
std::vector<VkCommandBuffer> commandBuffers;
std::vector<VkSemaphore> imageAvailableSemaphores; // 每个交换链图像一个
std::vector<VkSemaphore> renderFinishedSemaphores; // 每个交换链图像一个
std::vector<VkFence> inFlightFences; // 每个帧在飞行一个
std::vector<VkFence> imagesInFlight; // 跟踪每个图像的使用状态
int currentFrame = 0;
int MAX_FRAMES_IN_FLIGHT = 2;
bool inited = false;
protected:
void loadTexture(std::string path, Texture& tex, bool srgb, VkCommandPool pool);
void loadTexture(std::vector<unsigned char>& image_data, size_t image_size, int w, int h, Texture& tex, bool srgb, VkCommandPool pool);
#ifdef _WIN32
void loadTextureExample(std::wstring path, Texture& tex, bool srgb, VkCommandPool pool);
#endif
uint32_t findMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter,
VkMemoryPropertyFlags properties);
void createTexture(VkDevice device, VkPhysicalDevice physicalDevice, int width, int height, Texture& texture, bool srgb);
void updateTexture(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue queue, uint8_t* data, int width, int height, int rowStride, size_t dataSize, Texture& texture);
VkCommandBuffer beginSingleTimeCommands(VkDevice device, VkCommandPool commandPool);
void endSingleTimeCommands(VkDevice device, VkCommandPool commandPool, VkQueue queue, VkCommandBuffer commandBuffer);
void transitionImageLayout(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout);
void destroy_texture(Texture texture);
std::mutex mtx;
long long _lastDrawFrameTime;
std::mutex changeMotionMtx;
};