4
社区成员
发帖
与我相关
我的任务
分享#define GLFW_INCLUDE_VULKAN // 让GLFW自动包含Vulkan头文件
#include <GLFW/glfw3.h> // GLFW窗口库
#define GLM_FORCE_DEPTH_ZERO_TO_ONE // 让GLM使用Vulkan的深度范围[0,1]
#define GLM_ENABLE_EXPERIMENTAL // 启用GLM实验性功能(gtx/hash.hpp需要)
#include <glm/glm.hpp> // GLM数学库核心
#include <glm/gtc/matrix_transform.hpp> // GLM矩阵变换函数
#include <glm/gtx/hash.hpp> // GLM哈希支持
#define STB_IMAGE_IMPLEMENTATION // 生成stb_image实现代码
#include <stb_image.h> // stb_image图像加载库
#define TINYOBJLOADER_IMPLEMENTATION // 生成tinyobjloader实现代码
#include <tiny_obj_loader.h> // tinyobjloader模型加载库
#include <iostream> // 标准输入输出流
#include <fstream> // 文件读写
#include <stdexcept> // 标准异常类
#include <algorithm> // std::clamp/std::max等
#include <chrono> // 时间库
#include <vector> // 动态数组
#include <cstring> // memcpy/strcmp
#include <cstdlib> // EXIT_SUCCESS/EXIT_FAILURE
#include <cstdint> // 固定宽度整型
#include <limits> // std::numeric_limits
#include <array> // std::array
#include <optional> // std::optional
#include <set> // std::set
#include <unordered_map> // 哈希表
#include <cmath> // std::floor/std::log2
const uint32_t WIDTH = 800; // 窗口宽度
const uint32_t HEIGHT = 600; // 窗口高度
const std::string MODEL_PATH = "models/viking_room.obj"; // 模型路径
const std::string TEXTURE_PATH = "textures/viking_room.png"; // 纹理路径
const int MAX_FRAMES_IN_FLIGHT = 2; // 同时在途的帧数
const std::vector<const char*> validationLayers = { // 校验层列表
"VK_LAYER_KHRONOS_validation" // Khronos官方校验层
};
const std::vector<const char*> deviceExtensions = { // 需要的设备扩展
VK_KHR_SWAPCHAIN_EXTENSION_NAME // 交换链扩展
};
#ifdef NDEBUG
const bool enableValidationLayers = false; // Release关闭校验层
#else
const bool enableValidationLayers = true; // Debug开启校验层
#endif
VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugUtilsMessengerEXT* pDebugMessenger) { // 动态加载vkCreateDebugUtilsMessengerEXT
auto func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT"); // 获取函数指针
if (func != nullptr) { // 若存在
return func(instance, pCreateInfo, pAllocator, pDebugMessenger); // 调用
}
else {
return VK_ERROR_EXTENSION_NOT_PRESENT; // 返回错误
}
}
void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, const VkAllocationCallbacks* pAllocator) { // 动态加载vkDestroyDebugUtilsMessengerEXT
auto func = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT"); // 获取函数指针
if (func != nullptr) { // 若存在
func(instance, debugMessenger, pAllocator); // 调用
}
}
struct QueueFamilyIndices { // 队列族索引
std::optional<uint32_t> graphicsFamily; // 图形队列族
std::optional<uint32_t> presentFamily; // 呈现队列族
bool isComplete() { // 是否都已找到
return graphicsFamily.has_value() && presentFamily.has_value();
}
};
struct SwapChainSupportDetails { // 交换链支持详情
VkSurfaceCapabilitiesKHR capabilities; // 表面能力
std::vector<VkSurfaceFormatKHR> formats; // 支持的格式
std::vector<VkPresentModeKHR> presentModes; // 支持的呈现模式
};
struct Vertex { // 顶点结构体
glm::vec3 pos; // 位置
glm::vec3 color; // 颜色
glm::vec2 texCoord; // 纹理坐标
static VkVertexInputBindingDescription getBindingDescription() { // 顶点绑定描述
VkVertexInputBindingDescription bindingDescription{}; // 值初始化
bindingDescription.binding = 0; // 绑定索引0
bindingDescription.stride = sizeof(Vertex); // 跨距
bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; // 逐顶点
return bindingDescription;
}
static std::array<VkVertexInputAttributeDescription, 3> getAttributeDescriptions() { // 顶点属性描述
std::array<VkVertexInputAttributeDescription, 3> attributeDescriptions{};
attributeDescriptions[0].binding = 0; // 绑定0
attributeDescriptions[0].location = 0; // location 0
attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT; // vec3
attributeDescriptions[0].offset = offsetof(Vertex, pos); // pos偏移
attributeDescriptions[1].binding = 0; // 绑定0
attributeDescriptions[1].location = 1; // location 1
attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT; // vec3
attributeDescriptions[1].offset = offsetof(Vertex, color); // color偏移
attributeDescriptions[2].binding = 0; // 绑定0
attributeDescriptions[2].location = 2; // location 2
attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT; // vec2
attributeDescriptions[2].offset = offsetof(Vertex, texCoord); // texCoord偏移
return attributeDescriptions;
}
bool operator==(const Vertex& other) const { // 相等运算符,用于哈希去重
return pos == other.pos && color == other.color && texCoord == other.texCoord;
}
};
namespace std {
template<> struct hash<Vertex> { // 为Vertex特化std::hash
size_t operator()(Vertex const& vertex) const { // 哈希函数
return ((hash<glm::vec3>()(vertex.pos) ^ (hash<glm::vec3>()(vertex.color) << 1)) >> 1) ^ (hash<glm::vec2>()(vertex.texCoord) << 1); // 组合各字段哈希
}
};
}
struct UniformBufferObject { // Uniform缓冲对象
alignas(16) glm::mat4 model; // 模型矩阵
alignas(16) glm::mat4 view; // 视图矩阵
alignas(16) glm::mat4 proj; // 投影矩阵
};
class HelloTriangleApplication { // 应用主类
public:
void run() { // 入口
initWindow(); // 创建窗口
initVulkan(); // 初始化Vulkan
mainLoop(); // 主循环
cleanup(); // 清理
}
private:
GLFWwindow* window; // GLFW窗口
VkInstance instance; // Vulkan实例
VkDebugUtilsMessengerEXT debugMessenger; // 调试信使
VkSurfaceKHR surface; // 窗口表面
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; // 物理设备
VkSampleCountFlagBits msaaSamples = VK_SAMPLE_COUNT_1_BIT; // MSAA采样数(默认1,后续从设备属性中选取)
VkDevice device; // 逻辑设备
VkQueue graphicsQueue; // 图形队列
VkQueue presentQueue; // 呈现队列
VkSwapchainKHR swapChain; // 交换链
std::vector<VkImage> swapChainImages; // 交换链图像
VkFormat swapChainImageFormat; // 交换链图像格式
VkExtent2D swapChainExtent; // 交换链图像尺寸
std::vector<VkImageView> swapChainImageViews; // 交换链图像视图
std::vector<VkFramebuffer> swapChainFramebuffers; // 帧缓冲
VkRenderPass renderPass; // 渲染通道
VkDescriptorSetLayout descriptorSetLayout; // 描述符集布局
VkPipelineLayout pipelineLayout; // 管线布局
VkPipeline graphicsPipeline; // 图形管线
VkCommandPool commandPool; // 命令池
VkImage colorImage; // MSAA颜色图像(多重采样)
VkDeviceMemory colorImageMemory; // MSAA颜色图像内存
VkImageView colorImageView; // MSAA颜色图像视图
VkImage depthImage; // 深度图像
VkDeviceMemory depthImageMemory; // 深度图像内存
VkImageView depthImageView; // 深度图像视图
uint32_t mipLevels; // 纹理mipmap层数
VkImage textureImage; // 纹理图像
VkDeviceMemory textureImageMemory; // 纹理图像内存
VkImageView textureImageView; // 纹理图像视图
VkSampler textureSampler; // 纹理采样器
std::vector<Vertex> vertices; // 顶点数据
std::vector<uint32_t> indices; // 索引数据
VkBuffer vertexBuffer; // 顶点缓冲
VkDeviceMemory vertexBufferMemory; // 顶点缓冲内存
VkBuffer indexBuffer; // 索引缓冲
VkDeviceMemory indexBufferMemory; // 索引缓冲内存
std::vector<VkBuffer> uniformBuffers; // Uniform缓冲
std::vector<VkDeviceMemory> uniformBuffersMemory; // Uniform缓冲内存
std::vector<void*> uniformBuffersMapped; // 映射后的指针
VkDescriptorPool descriptorPool; // 描述符池
std::vector<VkDescriptorSet> descriptorSets; // 描述符集
std::vector<VkCommandBuffer> commandBuffers; // 命令缓冲
std::vector<VkSemaphore> imageAvailableSemaphores; // 图像可用信号量
std::vector<VkSemaphore> renderFinishedSemaphores; // 渲染完成信号量
std::vector<VkFence> inFlightFences; // 在途围栏
uint32_t currentFrame = 0; // 当前帧
bool framebufferResized = false; // 帧缓冲是否改变大小
void initWindow() { // 初始化窗口
glfwInit(); // 初始化GLFW
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); // 不创建OpenGL上下文
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); // 创建窗口
glfwSetWindowUserPointer(window, this); // 保存this指针
glfwSetFramebufferSizeCallback(window, framebufferResizeCallback); // 注册回调
}
static void framebufferResizeCallback(GLFWwindow* window, int width, int height) { // 窗口大小变化回调
auto app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window)); // 取回应用
app->framebufferResized = true; // 标记需重建
}
void initVulkan() { // 初始化Vulkan
createInstance(); // 创建实例
setupDebugMessenger(); // 设置调试信使
createSurface(); // 创建表面
pickPhysicalDevice(); // 选择物理设备(其中确定msaaSamples)
createLogicalDevice(); // 创建逻辑设备
createSwapChain(); // 创建交换链
createImageViews(); // 创建图像视图
createRenderPass(); // 创建渲染通道(含MSAA解析附件)
createDescriptorSetLayout(); // 创建描述符集布局
createGraphicsPipeline(); // 创建图形管线(多重采样数用msaaSamples)
createCommandPool(); // 创建命令池
createColorResources(); // 创建MSAA颜色资源(新增)
createDepthResources(); // 创建深度资源(多重采样)
createFramebuffers(); // 创建帧缓冲(3个附件)
createTextureImage(); // 创建纹理图像
createTextureImageView(); // 创建纹理图像视图
createTextureSampler(); // 创建纹理采样器
loadModel(); // 加载模型
createVertexBuffer(); // 创建顶点缓冲
createIndexBuffer(); // 创建索引缓冲
createUniformBuffers(); // 创建Uniform缓冲
createDescriptorPool(); // 创建描述符池
createDescriptorSets(); // 分配描述符集
createCommandBuffers(); // 创建命令缓冲
createSyncObjects(); // 创建同步对象
}
void mainLoop() { // 主循环
while (!glfwWindowShouldClose(window)) { // 窗口未关闭
glfwPollEvents(); // 处理事件
drawFrame(); // 绘制
}
vkDeviceWaitIdle(device); // 等待设备空闲
}
void cleanupSwapChain() { // 清理交换链资源
vkDestroyImageView(device, depthImageView, nullptr); // 销毁深度视图
vkDestroyImage(device, depthImage, nullptr); // 销毁深度图像
vkFreeMemory(device, depthImageMemory, nullptr); // 释放深度内存
vkDestroyImageView(device, colorImageView, nullptr); // 销毁MSAA颜色视图
vkDestroyImage(device, colorImage, nullptr); // 销毁MSAA颜色图像
vkFreeMemory(device, colorImageMemory, nullptr); // 释放MSAA颜色内存
for (auto framebuffer : swapChainFramebuffers) { // 遍历帧缓冲
vkDestroyFramebuffer(device, framebuffer, nullptr); // 销毁
}
for (auto imageView : swapChainImageViews) { // 遍历图像视图
vkDestroyImageView(device, imageView, nullptr); // 销毁
}
vkDestroySwapchainKHR(device, swapChain, nullptr); // 销毁交换链
}
void cleanup() { // 清理所有资源
cleanupSwapChain(); // 清理交换链
vkDestroyPipeline(device, graphicsPipeline, nullptr); // 销毁管线
vkDestroyPipelineLayout(device, pipelineLayout, nullptr); // 销毁管线布局
vkDestroyRenderPass(device, renderPass, nullptr); // 销毁渲染通道
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { // 遍历每帧
vkDestroyBuffer(device, uniformBuffers[i], nullptr); // 销毁Uniform缓冲
vkFreeMemory(device, uniformBuffersMemory[i], nullptr); // 释放内存
}
vkDestroyDescriptorPool(device, descriptorPool, nullptr); // 销毁描述符池
vkDestroySampler(device, textureSampler, nullptr); // 销毁采样器
vkDestroyImageView(device, textureImageView, nullptr); // 销毁纹理视图
vkDestroyImage(device, textureImage, nullptr); // 销毁纹理图像
vkFreeMemory(device, textureImageMemory, nullptr); // 释放纹理内存
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); // 销毁描述符集布局
vkDestroyBuffer(device, indexBuffer, nullptr); // 销毁索引缓冲
vkFreeMemory(device, indexBufferMemory, nullptr); // 释放内存
vkDestroyBuffer(device, vertexBuffer, nullptr); // 销毁顶点缓冲
vkFreeMemory(device, vertexBufferMemory, nullptr); // 释放内存
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { // 遍历每帧
vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr); // 销毁渲染完成信号量
vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr); // 销毁图像可用信号量
vkDestroyFence(device, inFlightFences[i], nullptr); // 销毁围栏
}
vkDestroyCommandPool(device, commandPool, nullptr); // 销毁命令池
vkDestroyDevice(device, nullptr); // 销毁设备
if (enableValidationLayers) { // 若启用校验层
DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr); // 销毁调试信使
}
vkDestroySurfaceKHR(instance, surface, nullptr); // 销毁表面
vkDestroyInstance(instance, nullptr); // 销毁实例
glfwDestroyWindow(window); // 销毁窗口
glfwTerminate(); // 终止GLFW
}
void recreateSwapChain() { // 重建交换链
int width = 0, height = 0; // 尺寸
glfwGetFramebufferSize(window, &width, &height); // 获取尺寸
while (width == 0 || height == 0) { // 最小化时等待
glfwGetFramebufferSize(window, &width, &height);
glfwWaitEvents();
}
vkDeviceWaitIdle(device); // 等待空闲
cleanupSwapChain(); // 清理旧资源
createSwapChain(); // 重建交换链
createImageViews(); // 重建图像视图
createColorResources(); // 重建MSAA颜色资源
createDepthResources(); // 重建深度资源
createFramebuffers(); // 重建帧缓冲
}
void createInstance() { // 创建实例
if (enableValidationLayers && !checkValidationLayerSupport()) { // 校验层不可用
throw std::runtime_error("validation layers requested, but not available!");
}
VkApplicationInfo appInfo{}; // 应用信息
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "Hello Triangle"; // 应用名
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0); // 应用版本
appInfo.pEngineName = "No Engine"; // 引擎名
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0); // 引擎版本
appInfo.apiVersion = VK_API_VERSION_1_0; // API版本
VkInstanceCreateInfo createInfo{}; // 实例创建信息
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
auto extensions = getRequiredExtensions(); // 所需扩展
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size()); // 数量
createInfo.ppEnabledExtensionNames = extensions.data(); // 扩展名
VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo{}; // 调试信使信息
if (enableValidationLayers) { // 启用校验层
createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size()); // 层数量
createInfo.ppEnabledLayerNames = validationLayers.data(); // 层名
populateDebugMessengerCreateInfo(debugCreateInfo); // 填充
createInfo.pNext = (VkDebugUtilsMessengerCreateInfoEXT*)&debugCreateInfo; // 挂到pNext
}
else {
createInfo.enabledLayerCount = 0; // 不启用
createInfo.pNext = nullptr;
}
if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) { // 创建实例
throw std::runtime_error("failed to create instance!");
}
}
void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) { // 填充调试信使信息
createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; // 严重性
createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT; // 类型
createInfo.pfnUserCallback = debugCallback; // 回调
}
void setupDebugMessenger() { // 设置调试信使
if (!enableValidationLayers) return; // 未启用则返回
VkDebugUtilsMessengerCreateInfoEXT createInfo;
populateDebugMessengerCreateInfo(createInfo); // 填充
if (CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) { // 创建
throw std::runtime_error("failed to set up debug messenger!");
}
}
void createSurface() { // 创建表面
if (glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS) { // GLFW创建
throw std::runtime_error("failed to create window surface!");
}
}
void pickPhysicalDevice() { // 选择物理设备
uint32_t deviceCount = 0; // 数量
vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr); // 查询
if (deviceCount == 0) { // 无GPU
throw std::runtime_error("failed to find GPUs with Vulkan support!");
}
std::vector<VkPhysicalDevice> devices(deviceCount); // 设备列表
vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data()); // 获取
for (const auto& device : devices) { // 遍历
if (isDeviceSuitable(device)) { // 合适
physicalDevice = device; // 选中
msaaSamples = getMaxUsableSampleCount(); // 选择最大可用MSAA采样数(新增)
break;
}
}
if (physicalDevice == VK_NULL_HANDLE) { // 未找到
throw std::runtime_error("failed to find a suitable GPU!");
}
}
void createLogicalDevice() { // 创建逻辑设备
QueueFamilyIndices indices = findQueueFamilies(physicalDevice); // 队列族
std::vector<VkDeviceQueueCreateInfo> queueCreateInfos; // 队列创建信息
std::set<uint32_t> uniqueQueueFamilies = { indices.graphicsFamily.value(), indices.presentFamily.value() }; // 去重
float queuePriority = 1.0f; // 优先级
for (uint32_t queueFamily : uniqueQueueFamilies) { // 遍历
VkDeviceQueueCreateInfo queueCreateInfo{};
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueCreateInfo.queueFamilyIndex = queueFamily; // 队列族
queueCreateInfo.queueCount = 1; // 数量
queueCreateInfo.pQueuePriorities = &queuePriority; // 优先级
queueCreateInfos.push_back(queueCreateInfo);
}
VkPhysicalDeviceFeatures deviceFeatures{}; // 设备特性
deviceFeatures.samplerAnisotropy = VK_TRUE; // 各向异性
VkDeviceCreateInfo createInfo{}; // 设备创建信息
createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size()); // 队列数量
createInfo.pQueueCreateInfos = queueCreateInfos.data(); // 队列信息
createInfo.pEnabledFeatures = &deviceFeatures; // 特性
createInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size()); // 扩展数量
createInfo.ppEnabledExtensionNames = deviceExtensions.data(); // 扩展名
if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) { // 创建
throw std::runtime_error("failed to create logical device!");
}
vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue); // 获取图形队列
vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue); // 获取呈现队列
}
void createSwapChain() { // 创建交换链
SwapChainSupportDetails swapChainSupport = querySwapChainSupport(physicalDevice); // 查询支持
VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats); // 选格式
VkPresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.presentModes); // 选模式
VkExtent2D extent = chooseSwapExtent(swapChainSupport.capabilities); // 选尺寸
uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1; // 至少+1
if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) { // 不超过最大
imageCount = swapChainSupport.capabilities.maxImageCount;
}
VkSwapchainCreateInfoKHR createInfo{}; // 创建信息
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
createInfo.surface = surface; // 表面
createInfo.minImageCount = imageCount; // 图像数量
createInfo.imageFormat = surfaceFormat.format; // 格式
createInfo.imageColorSpace = surfaceFormat.colorSpace; // 颜色空间
createInfo.imageExtent = extent; // 尺寸
createInfo.imageArrayLayers = 1; // 层数
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; // 用途
QueueFamilyIndices indices = findQueueFamilies(physicalDevice); // 队列族
uint32_t queueFamilyIndices[] = { indices.graphicsFamily.value(), indices.presentFamily.value() }; // 两族
if (indices.graphicsFamily != indices.presentFamily) { // 不同族
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT; // 并发
createInfo.queueFamilyIndexCount = 2;
createInfo.pQueueFamilyIndices = queueFamilyIndices;
}
else {
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; // 独占
}
createInfo.preTransform = swapChainSupport.capabilities.currentTransform; // 变换
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; // 不透明
createInfo.presentMode = presentMode; // 呈现模式
createInfo.clipped = VK_TRUE; // 裁剪
if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) { // 创建
throw std::runtime_error("failed to create swap chain!");
}
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr); // 查询数量
swapChainImages.resize(imageCount); // 调整大小
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data()); // 获取图像
swapChainImageFormat = surfaceFormat.format; // 保存格式
swapChainExtent = extent; // 保存尺寸
}
void createImageViews() { // 创建图像视图
swapChainImageViews.resize(swapChainImages.size()); // 调整
for (uint32_t i = 0; i < swapChainImages.size(); i++) { // 遍历
swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1); // 创建(mip=1)
}
}
void createRenderPass() { // 创建渲染通道
VkAttachmentDescription colorAttachment{}; // 颜色附件(多重采样)
colorAttachment.format = swapChainImageFormat; // 格式
colorAttachment.samples = msaaSamples; // 采样数:MSAA采样数(新增)
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; // 清空
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; // 存储(但resolve后即可,此处对MSAA附件实际无意义)
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; // 模板不关心
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; // 初始布局
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; // 最终布局:颜色附件(用于resolve)
VkAttachmentDescription depthAttachment{}; // 深度附件
depthAttachment.format = findDepthFormat(); // 格式
depthAttachment.samples = msaaSamples; // 采样数:MSAA采样数(新增)
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; // 清空
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; // 不存储
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; // 深度模板附件布局
VkAttachmentDescription colorAttachmentResolve{}; // 解析附件(新增):MSAA结果解析到单采样图像
colorAttachmentResolve.format = swapChainImageFormat; // 格式与交换链一致
colorAttachmentResolve.samples = VK_SAMPLE_COUNT_1_BIT; // 单采样(新增)
colorAttachmentResolve.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; // 不加载(会被resolve结果覆盖)
colorAttachmentResolve.storeOp = VK_ATTACHMENT_STORE_OP_STORE; // 存储
colorAttachmentResolve.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachmentResolve.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachmentResolve.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; // 初始布局
colorAttachmentResolve.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; // 最终布局:呈现源
VkAttachmentReference colorAttachmentRef{}; // 颜色附件引用
colorAttachmentRef.attachment = 0; // 索引0
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; // 布局
VkAttachmentReference depthAttachmentRef{}; // 深度附件引用
depthAttachmentRef.attachment = 1; // 索引1
depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; // 布局
VkAttachmentReference colorAttachmentResolveRef{}; // 解析附件引用(新增)
colorAttachmentResolveRef.attachment = 2; // 索引2
colorAttachmentResolveRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; // 布局
VkSubpassDescription subpass{}; // 子通道
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; // 图形
subpass.colorAttachmentCount = 1; // 颜色附件数
subpass.pColorAttachments = &colorAttachmentRef; // 颜色附件
subpass.pDepthStencilAttachment = &depthAttachmentRef; // 深度附件
subpass.pResolveAttachments = &colorAttachmentResolveRef; // 解析附件(新增):MSAA -> 单采样
VkSubpassDependency dependency{}; // 依赖
dependency.srcSubpass = VK_SUBPASS_EXTERNAL; // 外部
dependency.dstSubpass = 0; // 子通道0
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; // 源阶段
dependency.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; // 源访问
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT; // 目标阶段
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; // 目标访问
std::array<VkAttachmentDescription, 3> attachments = { colorAttachment, depthAttachment, colorAttachmentResolve }; // 3个附件(新增解析附件)
VkRenderPassCreateInfo renderPassInfo{}; // 创建信息
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size()); // 数量
renderPassInfo.pAttachments = attachments.data(); // 附件
renderPassInfo.subpassCount = 1; // 子通道数
renderPassInfo.pSubpasses = &subpass; // 子通道
renderPassInfo.dependencyCount = 1; // 依赖数
renderPassInfo.pDependencies = &dependency; // 依赖
if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) { // 创建
throw std::runtime_error("failed to create render pass!");
}
}
void createDescriptorSetLayout() { // 创建描述符集布局
VkDescriptorSetLayoutBinding uboLayoutBinding{}; // UBO绑定
uboLayoutBinding.binding = 0; // 索引0
uboLayoutBinding.descriptorCount = 1; // 数量
uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; // 类型
uboLayoutBinding.pImmutableSamplers = nullptr; // 无
uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; // 顶点阶段
VkDescriptorSetLayoutBinding samplerLayoutBinding{}; // 采样器绑定
samplerLayoutBinding.binding = 1; // 索引1
samplerLayoutBinding.descriptorCount = 1;
samplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; // 组合采样器
samplerLayoutBinding.pImmutableSamplers = nullptr;
samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; // 片段阶段
std::array<VkDescriptorSetLayoutBinding, 2> bindings = { uboLayoutBinding, samplerLayoutBinding }; // 两个绑定
VkDescriptorSetLayoutCreateInfo layoutInfo{}; // 创建信息
layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size()); // 数量
layoutInfo.pBindings = bindings.data(); // 绑定
if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS) { // 创建
throw std::runtime_error("failed to create descriptor set layout!");
}
}
void createGraphicsPipeline() { // 创建图形管线
auto vertShaderCode = readFile("shaders/27_shader_depth.vert.spv"); // 读顶点SPIR-V
auto fragShaderCode = readFile("shaders/27_shader_depth.frag.spv"); // 读片段SPIR-V
VkShaderModule vertShaderModule = createShaderModule(vertShaderCode); // 顶点模块
VkShaderModule fragShaderModule = createShaderModule(fragShaderCode); // 片段模块
VkPipelineShaderStageCreateInfo vertShaderStageInfo{}; // 顶点阶段
vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT; // 顶点
vertShaderStageInfo.module = vertShaderModule; // 模块
vertShaderStageInfo.pName = "main"; // 入口
VkPipelineShaderStageCreateInfo fragShaderStageInfo{}; // 片段阶段
fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT; // 片段
fragShaderStageInfo.module = fragShaderModule;
fragShaderStageInfo.pName = "main";
VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo }; // 阶段数组
VkPipelineVertexInputStateCreateInfo vertexInputInfo{}; // 顶点输入
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
auto bindingDescription = Vertex::getBindingDescription(); // 绑定描述
auto attributeDescriptions = Vertex::getAttributeDescriptions(); // 属性描述
vertexInputInfo.vertexBindingDescriptionCount = 1; // 绑定数
vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size()); // 属性数
vertexInputInfo.pVertexBindingDescriptions = &bindingDescription; // 绑定
vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data(); // 属性
VkPipelineInputAssemblyStateCreateInfo inputAssembly{}; // 输入装配
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; // 三角形列表
inputAssembly.primitiveRestartEnable = VK_FALSE; // 不重启
VkPipelineViewportStateCreateInfo viewportState{}; // 视口状态
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState.viewportCount = 1; // 视口数
viewportState.scissorCount = 1; // 剪裁数
VkPipelineRasterizationStateCreateInfo rasterizer{}; // 光栅化
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rasterizer.depthClampEnable = VK_FALSE; // 不钳制
rasterizer.rasterizerDiscardEnable = VK_FALSE; // 不丢弃
rasterizer.polygonMode = VK_POLYGON_MODE_FILL; // 填充
rasterizer.lineWidth = 1.0f; // 线宽
rasterizer.cullMode = VK_CULL_MODE_BACK_BIT; // 背面剔除
rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; // 逆时针正面
rasterizer.depthBiasEnable = VK_FALSE; // 不偏移
VkPipelineMultisampleStateCreateInfo multisampling{}; // 多重采样状态
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampling.sampleShadingEnable = VK_FALSE; // 不启用采样着色
multisampling.rasterizationSamples = msaaSamples; // 采样数:使用msaaSamples(新增)
VkPipelineDepthStencilStateCreateInfo depthStencil{}; // 深度模板
depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
depthStencil.depthTestEnable = VK_TRUE; // 深度测试
depthStencil.depthWriteEnable = VK_TRUE; // 深度写
depthStencil.depthCompareOp = VK_COMPARE_OP_LESS; // 小于通过
depthStencil.depthBoundsTestEnable = VK_FALSE; // 不启用
depthStencil.stencilTestEnable = VK_FALSE; // 不启用
VkPipelineColorBlendAttachmentState colorBlendAttachment{}; // 颜色混合附件
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; // 写RGBA
colorBlendAttachment.blendEnable = VK_FALSE; // 不混合
VkPipelineColorBlendStateCreateInfo colorBlending{}; // 颜色混合
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlending.logicOpEnable = VK_FALSE; // 不启用
colorBlending.logicOp = VK_LOGIC_OP_COPY; // 逻辑操作
colorBlending.attachmentCount = 1; // 附件数
colorBlending.pAttachments = &colorBlendAttachment; // 附件
colorBlending.blendConstants[0] = 0.0f; // 常量
colorBlending.blendConstants[1] = 0.0f;
colorBlending.blendConstants[2] = 0.0f;
colorBlending.blendConstants[3] = 0.0f;
std::vector<VkDynamicState> dynamicStates = { // 动态状态
VK_DYNAMIC_STATE_VIEWPORT, // 视口
VK_DYNAMIC_STATE_SCISSOR // 剪裁
};
VkPipelineDynamicStateCreateInfo dynamicState{}; // 动态状态
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
dynamicState.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size()); // 数量
dynamicState.pDynamicStates = dynamicStates.data(); // 数组
VkPipelineLayoutCreateInfo pipelineLayoutInfo{}; // 管线布局
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = 1; // 布局数
pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout; // 布局
if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) { // 创建
throw std::runtime_error("failed to create pipeline layout!");
}
VkGraphicsPipelineCreateInfo pipelineInfo{}; // 管线信息
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.stageCount = 2; // 阶段数
pipelineInfo.pStages = shaderStages; // 阶段
pipelineInfo.pVertexInputState = &vertexInputInfo; // 顶点输入
pipelineInfo.pInputAssemblyState = &inputAssembly; // 输入装配
pipelineInfo.pViewportState = &viewportState; // 视口
pipelineInfo.pRasterizationState = &rasterizer; // 光栅化
pipelineInfo.pMultisampleState = &multisampling; // 多重采样
pipelineInfo.pDepthStencilState = &depthStencil; // 深度模板
pipelineInfo.pColorBlendState = &colorBlending; // 颜色混合
pipelineInfo.pDynamicState = &dynamicState; // 动态
pipelineInfo.layout = pipelineLayout; // 布局
pipelineInfo.renderPass = renderPass; // 渲染通道
pipelineInfo.subpass = 0; // 子通道
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; // 无基础管线
if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) { // 创建
throw std::runtime_error("failed to create graphics pipeline!");
}
vkDestroyShaderModule(device, fragShaderModule, nullptr); // 销毁片段模块
vkDestroyShaderModule(device, vertShaderModule, nullptr); // 销毁顶点模块
}
void createFramebuffers() { // 创建帧缓冲
swapChainFramebuffers.resize(swapChainImageViews.size()); // 调整
for (size_t i = 0; i < swapChainImageViews.size(); i++) { // 遍历
std::array<VkImageView, 3> attachments = { // 3个附件(新增解析附件)
colorImageView, // MSAA颜色附件
depthImageView, // 深度附件
swapChainImageViews[i] // 解析目标(交换链图像)
};
VkFramebufferCreateInfo framebufferInfo{}; // 创建信息
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
framebufferInfo.renderPass = renderPass; // 渲染通道
framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size()); // 数量
framebufferInfo.pAttachments = attachments.data(); // 附件
framebufferInfo.width = swapChainExtent.width; // 宽
framebufferInfo.height = swapChainExtent.height; // 高
framebufferInfo.layers = 1; // 层
if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) { // 创建
throw std::runtime_error("failed to create framebuffer!");
}
}
}
void createCommandPool() { // 创建命令池
QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice); // 队列族
VkCommandPoolCreateInfo poolInfo{}; // 创建信息
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; // 允许重置
poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value(); // 队列族
if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) { // 创建
throw std::runtime_error("failed to create graphics command pool!");
}
}
void createColorResources() { // 创建MSAA颜色资源(新增)
VkFormat colorFormat = swapChainImageFormat; // 颜色格式与交换链一致
createImage(swapChainExtent.width, swapChainExtent.height, 1, msaaSamples, colorFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, colorImage, colorImageMemory); // 创建MSAA颜色图像(瞬态附件+颜色附件)
colorImageView = createImageView(colorImage, colorFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1); // 创建视图
}
void createDepthResources() { // 创建深度资源
VkFormat depthFormat = findDepthFormat(); // 深度格式
createImage(swapChainExtent.width, swapChainExtent.height, 1, msaaSamples, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImage, depthImageMemory); // 创建深度图像(MSAA采样)
depthImageView = createImageView(depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1); // 创建视图
}
VkFormat findSupportedFormat(const std::vector<VkFormat>& candidates, VkImageTiling tiling, VkFormatFeatureFlags features) { // 查找支持格式
for (VkFormat format : candidates) { // 遍历
VkFormatProperties props; // 属性
vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &props); // 查询
if (tiling == VK_IMAGE_TILING_LINEAR && (props.linearTilingFeatures & features) == features) { // 线性支持
return format;
}
else if (tiling == VK_IMAGE_TILING_OPTIMAL && (props.optimalTilingFeatures & features) == features) { // 最优支持
return format;
}
}
throw std::runtime_error("failed to find supported format!");
}
VkFormat findDepthFormat() { // 查找深度格式
return findSupportedFormat(
{ VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT }, // 候选
VK_IMAGE_TILING_OPTIMAL, // 最优
VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT // 用途
);
}
bool hasStencilComponent(VkFormat format) { // 是否含模板
return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;
}
void createTextureImage() { // 创建纹理图像
int texWidth, texHeight, texChannels; // 宽高通道
stbi_uc* pixels = stbi_load(TEXTURE_PATH.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); // 加载
VkDeviceSize imageSize = texWidth * texHeight * 4; // 大小
mipLevels = static_cast<uint32_t>(std::floor(std::log2(std::max(texWidth, texHeight)))) + 1; // mip层数
if (!pixels) { // 失败
throw std::runtime_error("failed to load texture image!");
}
VkBuffer stagingBuffer; // 暂存缓冲
VkDeviceMemory stagingBufferMemory; // 暂存内存
createBuffer(imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory); // 创建
void* data; // 映射指针
vkMapMemory(device, stagingBufferMemory, 0, imageSize, 0, &data); // 映射
memcpy(data, pixels, static_cast<size_t>(imageSize)); // 拷贝
vkUnmapMemory(device, stagingBufferMemory); // 取消映射
stbi_image_free(pixels); // 释放
createImage(texWidth, texHeight, mipLevels, VK_SAMPLE_COUNT_1_BIT, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, textureImage, textureImageMemory); // 创建纹理图像(单采样)
transitionImageLayout(textureImage, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, mipLevels); // 转布局
copyBufferToImage(stagingBuffer, textureImage, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight)); // 拷贝
// 之后在generateMipmaps中转成SHADER_READ_ONLY_OPTIMAL
vkDestroyBuffer(device, stagingBuffer, nullptr); // 销毁暂存
vkFreeMemory(device, stagingBufferMemory, nullptr); // 释放
generateMipmaps(textureImage, VK_FORMAT_R8G8B8A8_SRGB, texWidth, texHeight, mipLevels); // 生成mipmap
}
void generateMipmaps(VkImage image, VkFormat imageFormat, int32_t texWidth, int32_t texHeight, uint32_t mipLevels) { // 生成mipmap
// 检查格式是否支持线性blit
VkFormatProperties formatProperties;
vkGetPhysicalDeviceFormatProperties(physicalDevice, imageFormat, &formatProperties);
if (!(formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT)) {
throw std::runtime_error("texture image format does not support linear blitting!");
}
VkCommandBuffer commandBuffer = beginSingleTimeCommands(); // 一次性命令
VkImageMemoryBarrier barrier{}; // 屏障
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.image = image; // 图像
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; // 忽略
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; // 忽略
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; // 颜色
barrier.subresourceRange.baseArrayLayer = 0;
barrier.subresourceRange.layerCount = 1;
barrier.subresourceRange.levelCount = 1; // 逐层处理
int32_t mipWidth = texWidth; // 当前mip宽
int32_t mipHeight = texHeight; // 当前mip高
for (uint32_t i = 1; i < mipLevels; i++) { // 从第1层开始
barrier.subresourceRange.baseMipLevel = i - 1; // 上一层
barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; // 旧:传输目标
barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; // 新:传输源
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; // 传输写
barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; // 传输读
vkCmdPipelineBarrier(commandBuffer,
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0,
0, nullptr,
0, nullptr,
1, &barrier);
VkImageBlit blit{}; // blit区域
blit.srcOffsets[0] = { 0, 0, 0 }; // 源起点
blit.srcOffsets[1] = { mipWidth, mipHeight, 1 }; // 源终点
blit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
blit.srcSubresource.mipLevel = i - 1; // 源mip
blit.srcSubresource.baseArrayLayer = 0;
blit.srcSubresource.layerCount = 1;
blit.dstOffsets[0] = { 0, 0, 0 }; // 目标起点
blit.dstOffsets[1] = { mipWidth > 1 ? mipWidth / 2 : 1, mipHeight > 1 ? mipHeight / 2 : 1, 1 }; // 目标终点(减半)
blit.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
blit.dstSubresource.mipLevel = i; // 目标mip
blit.dstSubresource.baseArrayLayer = 0;
blit.dstSubresource.layerCount = 1;
vkCmdBlitImage(commandBuffer,
image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // 源
image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // 目标
1, &blit,
VK_FILTER_LINEAR); // 线性过滤
barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; // 旧:传输源
barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; // 新:着色器只读
barrier.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT; // 传输读
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; // 着色器读
vkCmdPipelineBarrier(commandBuffer,
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0,
0, nullptr,
0, nullptr,
1, &barrier);
if (mipWidth > 1) mipWidth /= 2; // 下一层宽度减半
if (mipHeight > 1) mipHeight /= 2; // 下一层高度减半
}
barrier.subresourceRange.baseMipLevel = mipLevels - 1; // 最后一层
barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
vkCmdPipelineBarrier(commandBuffer,
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0,
0, nullptr,
0, nullptr,
1, &barrier);
endSingleTimeCommands(commandBuffer); // 结束
}
VkSampleCountFlagBits getMaxUsableSampleCount() { // 获取最大可用MSAA采样数(新增)
VkPhysicalDeviceProperties physicalDeviceProperties; // 设备属性
vkGetPhysicalDeviceProperties(physicalDevice, &physicalDeviceProperties); // 查询
VkSampleCountFlags counts = physicalDeviceProperties.limits.framebufferColorSampleCounts & physicalDeviceProperties.limits.framebufferDepthSampleCounts; // 颜色与深度采样数的交集
if (counts & VK_SAMPLE_COUNT_64_BIT) { return VK_SAMPLE_COUNT_64_BIT; } // 64x
if (counts & VK_SAMPLE_COUNT_32_BIT) { return VK_SAMPLE_COUNT_32_BIT; } // 32x
if (counts & VK_SAMPLE_COUNT_16_BIT) { return VK_SAMPLE_COUNT_16_BIT; } // 16x
if (counts & VK_SAMPLE_COUNT_8_BIT) { return VK_SAMPLE_COUNT_8_BIT; } // 8x
if (counts & VK_SAMPLE_COUNT_4_BIT) { return VK_SAMPLE_COUNT_4_BIT; } // 4x
if (counts & VK_SAMPLE_COUNT_2_BIT) { return VK_SAMPLE_COUNT_2_BIT; } // 2x
return VK_SAMPLE_COUNT_1_BIT; // 默认1x(无MSAA)
}
void createTextureImageView() { // 创建纹理视图
textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT, mipLevels); // 包含所有mip层
}
void createTextureSampler() { // 创建纹理采样器
VkPhysicalDeviceProperties properties{}; // 设备属性
vkGetPhysicalDeviceProperties(physicalDevice, &properties); // 查询
VkSamplerCreateInfo samplerInfo{}; // 采样器信息
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
samplerInfo.magFilter = VK_FILTER_LINEAR; // 放大线性
samplerInfo.minFilter = VK_FILTER_LINEAR; // 缩小线性
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT; // U重复
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT; // V重复
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT; // W重复
samplerInfo.anisotropyEnable = VK_TRUE; // 各向异性
samplerInfo.maxAnisotropy = properties.limits.maxSamplerAnisotropy; // 最大值
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK; // 边界色
samplerInfo.unnormalizedCoordinates = VK_FALSE; // 归一化坐标
samplerInfo.compareEnable = VK_FALSE; // 不比较
samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS; // 比较操作
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR; // mipmap线性
samplerInfo.minLod = 0.0f; // 最小LOD
samplerInfo.maxLod = VK_LOD_CLAMP_NONE; // 最大LOD:不限制
samplerInfo.mipLodBias = 0.0f; // LOD偏置
if (vkCreateSampler(device, &samplerInfo, nullptr, &textureSampler) != VK_SUCCESS) { // 创建
throw std::runtime_error("failed to create texture sampler!");
}
}
VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags, uint32_t mipLevels) { // 创建图像视图
VkImageViewCreateInfo viewInfo{}; // 视图信息
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.image = image; // 图像
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; // 2D
viewInfo.format = format; // 格式
viewInfo.subresourceRange.aspectMask = aspectFlags; // 方面
viewInfo.subresourceRange.baseMipLevel = 0; // 基础mip
viewInfo.subresourceRange.levelCount = mipLevels; // mip数
viewInfo.subresourceRange.baseArrayLayer = 0; // 基础层
viewInfo.subresourceRange.layerCount = 1; // 层数
VkImageView imageView; // 视图句柄
if (vkCreateImageView(device, &viewInfo, nullptr, &imageView) != VK_SUCCESS) { // 创建
throw std::runtime_error("failed to create image view!");
}
return imageView;
}
void createImage(uint32_t width, uint32_t height, uint32_t mipLevels, VkSampleCountFlagBits numSamples, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory) { // 创建图像(新增numSamples)
VkImageCreateInfo imageInfo{}; // 图像信息
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageInfo.imageType = VK_IMAGE_TYPE_2D; // 2D
imageInfo.extent.width = width; // 宽
imageInfo.extent.height = height; // 高
imageInfo.extent.depth = 1; // 深
imageInfo.mipLevels = mipLevels; // mip层数
imageInfo.arrayLayers = 1; // 数组层
imageInfo.format = format; // 格式
imageInfo.tiling = tiling; // 平铺
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; // 初始布局
imageInfo.usage = usage; // 用途
imageInfo.samples = numSamples; // 采样数(新增)
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; // 独占
if (vkCreateImage(device, &imageInfo, nullptr, &image) != VK_SUCCESS) { // 创建
throw std::runtime_error("failed to create image!");
}
VkMemoryRequirements memRequirements; // 内存需求
vkGetImageMemoryRequirements(device, image, &memRequirements); // 查询
VkMemoryAllocateInfo allocInfo{}; // 分配信息
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size; // 大小
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties); // 类型
if (vkAllocateMemory(device, &allocInfo, nullptr, &imageMemory) != VK_SUCCESS) { // 分配
throw std::runtime_error("failed to allocate image memory!");
}
vkBindImageMemory(device, image, imageMemory, 0); // 绑定
}
void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout, uint32_t mipLevels) { // 转换布局
VkCommandBuffer commandBuffer = beginSingleTimeCommands(); // 一次性命令
VkImageMemoryBarrier barrier{}; // 屏障
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.oldLayout = oldLayout; // 旧布局
barrier.newLayout = newLayout; // 新布局
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; // 忽略
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; // 忽略
barrier.image = image; // 图像
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; // 颜色
barrier.subresourceRange.baseMipLevel = 0;
barrier.subresourceRange.levelCount = mipLevels; // 所有mip层
barrier.subresourceRange.baseArrayLayer = 0;
barrier.subresourceRange.layerCount = 1;
VkPipelineStageFlags sourceStage; // 源阶段
VkPipelineStageFlags destinationStage; // 目标阶段
if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) { // 未定义 -> 传输目标
barrier.srcAccessMask = 0; // 无需等待
barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; // 传输写
sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; // 顶部
destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT; // 传输
}
else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) { // 传输目标 -> 着色器只读
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; // 传输写
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; // 着色器读
sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT; // 传输
destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; // 片段
}
else {
throw std::invalid_argument("unsupported layout transition!"); // 不支持
}
vkCmdPipelineBarrier( // 记录屏障
commandBuffer,
sourceStage, destinationStage,
0,
0, nullptr,
0, nullptr,
1, &barrier
);
endSingleTimeCommands(commandBuffer); // 结束
}
void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height) { // 缓冲拷贝到图像
VkCommandBuffer commandBuffer = beginSingleTimeCommands(); // 一次性命令
VkBufferImageCopy region{}; // 拷贝区域
region.bufferOffset = 0; // 缓冲偏移
region.bufferRowLength = 0; // 行长度
region.bufferImageHeight = 0; // 图像高度
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; // 颜色
region.imageSubresource.mipLevel = 0; // mip0
region.imageSubresource.baseArrayLayer = 0;
region.imageSubresource.layerCount = 1;
region.imageOffset = { 0, 0, 0 }; // 偏移
region.imageExtent = {
width,
height,
1
};
vkCmdCopyBufferToImage(commandBuffer, buffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion); // 记录
endSingleTimeCommands(commandBuffer); // 结束
}
void loadModel() { // 加载模型
tinyobj::attrib_t attrib; // 属性
std::vector<tinyobj::shape_t> shapes; // 形状
std::vector<tinyobj::material_t> materials; // 材质
std::string warn, err; // 警告/错误
if (!tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, MODEL_PATH.c_str())) { // 加载
throw std::runtime_error(warn + err); // 失败抛错
}
std::unordered_map<Vertex, uint32_t> uniqueVertices{}; // 去重表
for (const auto& shape : shapes) { // 遍历形状
for (const auto& index : shape.mesh.indices) { // 遍历索引
Vertex vertex{}; // 顶点
vertex.pos = { // 位置
attrib.vertices[3 * index.vertex_index + 0],
attrib.vertices[3 * index.vertex_index + 1],
attrib.vertices[3 * index.vertex_index + 2]
};
vertex.texCoord = { // 纹理坐标(V翻转)
attrib.texcoords[2 * index.texcoord_index + 0],
1.0f - attrib.texcoords[2 * index.texcoord_index + 1]
};
vertex.color = { 1.0f, 1.0f, 1.0f }; // 白色
if (uniqueVertices.count(vertex) == 0) { // 新顶点
uniqueVertices[vertex] = static_cast<uint32_t>(vertices.size()); // 记录
vertices.push_back(vertex); // 添加
}
indices.push_back(uniqueVertices[vertex]); // 添加索引
}
}
}
void createVertexBuffer() { // 创建顶点缓冲
VkDeviceSize bufferSize = sizeof(vertices[0]) * vertices.size(); // 大小
VkBuffer stagingBuffer; // 暂存
VkDeviceMemory stagingBufferMemory;
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory); // 创建
void* data;
vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data); // 映射
memcpy(data, vertices.data(), (size_t)bufferSize); // 拷贝
vkUnmapMemory(device, stagingBufferMemory); // 取消
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vertexBuffer, vertexBufferMemory); // 创建
copyBuffer(stagingBuffer, vertexBuffer, bufferSize); // 拷贝
vkDestroyBuffer(device, stagingBuffer, nullptr); // 销毁
vkFreeMemory(device, stagingBufferMemory, nullptr); // 释放
}
void createIndexBuffer() { // 创建索引缓冲
VkDeviceSize bufferSize = sizeof(indices[0]) * indices.size(); // 大小
VkBuffer stagingBuffer; // 暂存
VkDeviceMemory stagingBufferMemory;
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory); // 创建
void* data;
vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
memcpy(data, indices.data(), (size_t)bufferSize); // 拷贝
vkUnmapMemory(device, stagingBufferMemory);
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexBuffer, indexBufferMemory); // 创建
copyBuffer(stagingBuffer, indexBuffer, bufferSize); // 拷贝
vkDestroyBuffer(device, stagingBuffer, nullptr);
vkFreeMemory(device, stagingBufferMemory, nullptr);
}
void createUniformBuffers() { // 创建Uniform缓冲
VkDeviceSize bufferSize = sizeof(UniformBufferObject); // 大小
uniformBuffers.resize(MAX_FRAMES_IN_FLIGHT); // 每帧
uniformBuffersMemory.resize(MAX_FRAMES_IN_FLIGHT);
uniformBuffersMapped.resize(MAX_FRAMES_IN_FLIGHT);
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { // 遍历
createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, uniformBuffers[i], uniformBuffersMemory[i]); // 创建
vkMapMemory(device, uniformBuffersMemory[i], 0, bufferSize, 0, &uniformBuffersMapped[i]); // 持久映射
}
}
void createDescriptorPool() { // 创建描述符池
std::array<VkDescriptorPoolSize, 2> poolSizes{}; // 池大小
poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; // UBO
poolSizes[0].descriptorCount = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT); // 每帧
poolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; // 采样器
poolSizes[1].descriptorCount = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT);
VkDescriptorPoolCreateInfo poolInfo{}; // 创建信息
poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size()); // 数量
poolInfo.pPoolSizes = poolSizes.data(); // 大小
poolInfo.maxSets = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT); // 最大集合
if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &descriptorPool) != VK_SUCCESS) { // 创建
throw std::runtime_error("failed to create descriptor pool!");
}
}
void createDescriptorSets() { // 创建描述符集
std::vector<VkDescriptorSetLayout> layouts(MAX_FRAMES_IN_FLIGHT, descriptorSetLayout); // 布局
VkDescriptorSetAllocateInfo allocInfo{}; // 分配信息
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool; // 池
allocInfo.descriptorSetCount = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT); // 数量
allocInfo.pSetLayouts = layouts.data(); // 布局
descriptorSets.resize(MAX_FRAMES_IN_FLIGHT); // 调整
if (vkAllocateDescriptorSets(device, &allocInfo, descriptorSets.data()) != VK_SUCCESS) { // 分配
throw std::runtime_error("failed to allocate descriptor sets!");
}
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { // 遍历
VkDescriptorBufferInfo bufferInfo{}; // 缓冲信息
bufferInfo.buffer = uniformBuffers[i]; // 缓冲
bufferInfo.offset = 0; // 偏移
bufferInfo.range = sizeof(UniformBufferObject); // 范围
VkDescriptorImageInfo imageInfo{}; // 图像信息
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; // 布局
imageInfo.imageView = textureImageView; // 视图
imageInfo.sampler = textureSampler; // 采样器
std::array<VkWriteDescriptorSet, 2> descriptorWrites{}; // 写入
descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[0].dstSet = descriptorSets[i]; // 目标
descriptorWrites[0].dstBinding = 0; // 绑定0
descriptorWrites[0].dstArrayElement = 0;
descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptorWrites[0].descriptorCount = 1;
descriptorWrites[0].pBufferInfo = &bufferInfo; // 缓冲
descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[1].dstSet = descriptorSets[i];
descriptorWrites[1].dstBinding = 1; // 绑定1
descriptorWrites[1].dstArrayElement = 0;
descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptorWrites[1].descriptorCount = 1;
descriptorWrites[1].pImageInfo = &imageInfo; // 图像
vkUpdateDescriptorSets(device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr); // 更新
}
}
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory) { // 创建缓冲
VkBufferCreateInfo bufferInfo{}; // 创建信息
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = size; // 大小
bufferInfo.usage = usage; // 用途
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; // 独占
if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) { // 创建
throw std::runtime_error("failed to create buffer!");
}
VkMemoryRequirements memRequirements; // 内存需求
vkGetBufferMemoryRequirements(device, buffer, &memRequirements); // 查询
VkMemoryAllocateInfo allocInfo{}; // 分配信息
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size; // 大小
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties); // 类型
if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) { // 分配
throw std::runtime_error("failed to allocate buffer memory!");
}
vkBindBufferMemory(device, buffer, bufferMemory, 0); // 绑定
}
VkCommandBuffer beginSingleTimeCommands() { // 开始一次性命令
VkCommandBufferAllocateInfo allocInfo{}; // 分配信息
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; // 主
allocInfo.commandPool = commandPool; // 池
allocInfo.commandBufferCount = 1; // 数量
VkCommandBuffer commandBuffer; // 命令缓冲
vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer); // 分配
VkCommandBufferBeginInfo beginInfo{}; // 开始信息
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; // 一次提交
vkBeginCommandBuffer(commandBuffer, &beginInfo); // 开始
return commandBuffer;
}
void endSingleTimeCommands(VkCommandBuffer commandBuffer) { // 结束一次性命令
vkEndCommandBuffer(commandBuffer); // 结束
VkSubmitInfo submitInfo{}; // 提交信息
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffer;
vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); // 提交
vkQueueWaitIdle(graphicsQueue); // 等待
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer); // 释放
}
void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) { // 拷贝缓冲
VkCommandBuffer commandBuffer = beginSingleTimeCommands(); // 开始
VkBufferCopy copyRegion{}; // 拷贝区域
copyRegion.size = size; // 大小
vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, ©Region); // 记录
endSingleTimeCommands(commandBuffer); // 结束
}
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) { // 查找内存类型
VkPhysicalDeviceMemoryProperties memProperties; // 内存属性
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties); // 查询
for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) { // 遍历
if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) { // 匹配
return i;
}
}
throw std::runtime_error("failed to find suitable memory type!");
}
void createCommandBuffers() { // 创建命令缓冲
commandBuffers.resize(MAX_FRAMES_IN_FLIGHT); // 每帧
VkCommandBufferAllocateInfo allocInfo{}; // 分配信息
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.commandPool = commandPool; // 池
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; // 主
allocInfo.commandBufferCount = (uint32_t)commandBuffers.size(); // 数量
if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) { // 分配
throw std::runtime_error("failed to allocate command buffers!");
}
}
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) { // 记录命令
VkCommandBufferBeginInfo beginInfo{}; // 开始信息
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
if (vkBeginCommandBuffer(commandBuffer, &beginInfo) != VK_SUCCESS) { // 开始
throw std::runtime_error("failed to begin recording command buffer!");
}
VkRenderPassBeginInfo renderPassInfo{}; // 渲染通道开始信息
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = renderPass; // 渲染通道
renderPassInfo.framebuffer = swapChainFramebuffers[imageIndex]; // 帧缓冲
renderPassInfo.renderArea.offset = { 0, 0 }; // 偏移
renderPassInfo.renderArea.extent = swapChainExtent; // 尺寸
std::array<VkClearValue, 2> clearValues{}; // 清空值
clearValues[0].color = { {0.0f, 0.0f, 0.0f, 1.0f} }; // 黑色
clearValues[1].depthStencil = { 1.0f, 0 }; // 深度1.0
renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size()); // 数量
renderPassInfo.pClearValues = clearValues.data(); // 值
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); // 开始
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline); // 绑定管线
VkViewport viewport{}; // 视口
viewport.x = 0.0f; // x
viewport.y = 0.0f; // y
viewport.width = (float)swapChainExtent.width; // 宽
viewport.height = (float)swapChainExtent.height; // 高
viewport.minDepth = 0.0f; // 最小深度
viewport.maxDepth = 1.0f; // 最大深度
vkCmdSetViewport(commandBuffer, 0, 1, &viewport); // 设置
VkRect2D scissor{}; // 剪裁
scissor.offset = { 0, 0 }; // 偏移
scissor.extent = swapChainExtent; // 尺寸
vkCmdSetScissor(commandBuffer, 0, 1, &scissor); // 设置
VkBuffer vertexBuffers[] = { vertexBuffer }; // 顶点缓冲
VkDeviceSize offsets[] = { 0 }; // 偏移
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets); // 绑定
vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT32); // 绑定索引
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets[currentFrame], 0, nullptr); // 绑定描述符集
vkCmdDrawIndexed(commandBuffer, static_cast<uint32_t>(indices.size()), 1, 0, 0, 0); // 绘制
vkCmdEndRenderPass(commandBuffer); // 结束
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) { // 结束记录
throw std::runtime_error("failed to record command buffer!");
}
}
void createSyncObjects() { // 创建同步对象
imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT); // 图像可用
renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT); // 渲染完成
inFlightFences.resize(MAX_FRAMES_IN_FLIGHT); // 在途围栏
VkSemaphoreCreateInfo semaphoreInfo{}; // 信号量信息
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
VkFenceCreateInfo fenceInfo{}; // 围栏信息
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; // 初始已触发
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { // 遍历
if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS || // 创建
vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS ||
vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {
throw std::runtime_error("failed to create synchronization objects for a frame!");
}
}
}
void updateUniformBuffer(uint32_t currentImage) { // 更新Uniform
static auto startTime = std::chrono::high_resolution_clock::now(); // 起始时间
auto currentTime = std::chrono::high_resolution_clock::now(); // 当前
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count(); // 秒数
UniformBufferObject ubo{}; // UBO
ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f)); // 旋转
ubo.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)); // 视图
ubo.proj = glm::perspective(glm::radians(45.0f), swapChainExtent.width / (float)swapChainExtent.height, 0.1f, 10.0f); // 投影
ubo.proj[1][1] *= -1; // 翻转Y
memcpy(uniformBuffersMapped[currentImage], &ubo, sizeof(ubo)); // 拷贝
}
void drawFrame() { // 绘制一帧
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX); // 等待围栏
uint32_t imageIndex; // 图像索引
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex); // 获取图像
if (result == VK_ERROR_OUT_OF_DATE_KHR) { // 交换链过期
recreateSwapChain(); // 重建
return;
}
else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) { // 错误
throw std::runtime_error("failed to acquire swap chain image!");
}
updateUniformBuffer(currentFrame); // 更新UBO
vkResetFences(device, 1, &inFlightFences[currentFrame]); // 重置围栏
vkResetCommandBuffer(commandBuffers[currentFrame], /*VkCommandBufferResetFlagBits*/ 0); // 重置命令
recordCommandBuffer(commandBuffers[currentFrame], imageIndex); // 记录
VkSubmitInfo submitInfo{}; // 提交信息
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] }; // 等待
VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT }; // 阶段
submitInfo.waitSemaphoreCount = 1; // 数量
submitInfo.pWaitSemaphores = waitSemaphores; // 等待
submitInfo.pWaitDstStageMask = waitStages; // 阶段
submitInfo.commandBufferCount = 1; // 命令数
submitInfo.pCommandBuffers = &commandBuffers[currentFrame]; // 命令
VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] }; // 信号
submitInfo.signalSemaphoreCount = 1; // 数量
submitInfo.pSignalSemaphores = signalSemaphores; // 信号
if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) { // 提交
throw std::runtime_error("failed to submit draw command buffer!");
}
VkPresentInfoKHR presentInfo{}; // 呈现信息
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1; // 数量
presentInfo.pWaitSemaphores = signalSemaphores; // 等待
VkSwapchainKHR swapChains[] = { swapChain }; // 交换链
presentInfo.swapchainCount = 1; // 数量
presentInfo.pSwapchains = swapChains; // 交换链
presentInfo.pImageIndices = &imageIndex; // 索引
result = vkQueuePresentKHR(presentQueue, &presentInfo); // 呈现
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized) { // 需重建
framebufferResized = false;
recreateSwapChain();
}
else if (result != VK_SUCCESS) { // 错误
throw std::runtime_error("failed to present swap chain image!");
}
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; // 下一帧
}
VkShaderModule createShaderModule(const std::vector<char>& code) { // 创建着色器模块
VkShaderModuleCreateInfo createInfo{}; // 信息
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.codeSize = code.size(); // 大小
createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data()); // 代码
VkShaderModule shaderModule; // 模块
if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) { // 创建
throw std::runtime_error("failed to create shader module!");
}
return shaderModule;
}
VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats) { // 选表面格式
for (const auto& availableFormat : availableFormats) { // 遍历
if (availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { // 优选
return availableFormat;
}
}
return availableFormats[0]; // 否则第一个
}
VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes) { // 选呈现模式
for (const auto& availablePresentMode : availablePresentModes) { // 遍历
if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) { // 优选Mailbox
return availablePresentMode;
}
}
return VK_PRESENT_MODE_FIFO_KHR; // 否则FIFO
}
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) { // 选交换范围
if (capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max()) { // 已指定
return capabilities.currentExtent;
}
else {
int width, height;
glfwGetFramebufferSize(window, &width, &height); // 尺寸
VkExtent2D actualExtent = {
static_cast<uint32_t>(width),
static_cast<uint32_t>(height)
};
actualExtent.width = std::clamp(actualExtent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width); // 钳制
actualExtent.height = std::clamp(actualExtent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
return actualExtent;
}
}
SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device) { // 查询交换链支持
SwapChainSupportDetails details; // 详情
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities); // 能力
uint32_t formatCount;
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr); // 格式数
if (formatCount != 0) {
details.formats.resize(formatCount);
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data()); // 获取
}
uint32_t presentModeCount;
vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr); // 模式数
if (presentModeCount != 0) {
details.presentModes.resize(presentModeCount);
vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data()); // 获取
}
return details;
}
bool isDeviceSuitable(VkPhysicalDevice device) { // 设备是否合适
QueueFamilyIndices indices = findQueueFamilies(device); // 队列族
bool extensionsSupported = checkDeviceExtensionSupport(device); // 扩展
bool swapChainAdequate = false;
if (extensionsSupported) {
SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device); // 查询
swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty(); // 非空
}
VkPhysicalDeviceFeatures supportedFeatures;
vkGetPhysicalDeviceFeatures(device, &supportedFeatures); // 特性
return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy; // 全部满足
}
bool checkDeviceExtensionSupport(VkPhysicalDevice device) { // 检查扩展
uint32_t extensionCount;
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr); // 数量
std::vector<VkExtensionProperties> availableExtensions(extensionCount);
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data()); // 获取
std::set<std::string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end()); // 需要的
for (const auto& extension : availableExtensions) { // 遍历
requiredExtensions.erase(extension.extensionName); // 移除已满足
}
return requiredExtensions.empty(); // 全部满足
}
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) { // 查找队列族
QueueFamilyIndices indices; // 索引
uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr); // 数量
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data()); // 获取
int i = 0;
for (const auto& queueFamily : queueFamilies) { // 遍历
if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) { // 图形
indices.graphicsFamily = i;
}
VkBool32 presentSupport = false;
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport); // 呈现
if (presentSupport) { // 支持
indices.presentFamily = i;
}
if (indices.isComplete()) { // 都找到
break;
}
i++;
}
return indices;
}
std::vector<const char*> getRequiredExtensions() { // 所需扩展
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions;
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); // GLFW需要
std::vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount); // 拷贝
if (enableValidationLayers) { // 启用校验层
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); // 添加调试扩展
}
return extensions;
}
bool checkValidationLayerSupport() { // 检查校验层
uint32_t layerCount;
vkEnumerateInstanceLayerProperties(&layerCount, nullptr); // 数量
std::vector<VkLayerProperties> availableLayers(layerCount);
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data()); // 获取
for (const char* layerName : validationLayers) { // 遍历
bool layerFound = false;
for (const auto& layerProperties : availableLayers) { // 遍历
if (strcmp(layerName, layerProperties.layerName) == 0) { // 匹配
layerFound = true;
break;
}
}
if (!layerFound) { // 未找到
return false;
}
}
return true;
}
static std::vector<char> readFile(const std::string& filename) { // 读取文件
std::ifstream file(filename, std::ios::ate | std::ios::binary); // 打开
if (!file.is_open()) { // 失败
throw std::runtime_error("failed to open file!");
}
size_t fileSize = (size_t)file.tellg(); // 大小
std::vector<char> buffer(fileSize); // 缓冲
file.seekg(0); // 回到开头
file.read(buffer.data(), fileSize); // 读取
file.close(); // 关闭
return buffer;
}
static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageType, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* pUserData) { // 调试回调
std::cerr << "validation layer: " << pCallbackData->pMessage << std::endl; // 打印
return VK_FALSE; // 不中止
}
};
int main() { // 入口
HelloTriangleApplication app; // 应用
try {
app.run(); // 运行
}
catch (const std::exception& e) { // 捕获
std::cerr << e.what() << std::endl; // 打印
return EXIT_FAILURE; // 失败
}
return EXIT_SUCCESS; // 成功
}