vulkan初体验之三色三角形
文章目录
vulkan与OpenGL
都说vulkan是OpenGLnext,相对于OpenGL,vulkan在效率上有着巨量的提升。Vulkan在效率上的提升主要是它天然支持多线程。而且拥有异步数据交换,使用OpenGL时,如果把数据交互放到另一个独立线程中完成,将会引起冲突,这个原因是上传资源和进行绘制时都需要改变上下文,Vulkan则没有这个问题。Vulkan可以并行创建Command Buffer并行绘制,但由于Command Buffer 提交后就都是GPU驱动怎么执行的事了,执行的过程没必要也没可能用多线程加速。 但随之而来的是巨量的代码量的提升,写一个三色三角形需要1500+行的代码,我也不知道我是怎么回事要对这个麻烦的东西产生兴趣,估计不久我就回去继续看OpenGL了…
先来个效果图
这是一个绕x轴旋转的三色三角形

接下来着重介绍一下VulkanManager类
核心类——VulkanManager类
该类完成了Vulkan图形应用程序所需的各种基础工作(实例创建、销毁以及绘制)。本质上,该类是Vulkan项目的核心管理者。
#ifndef VULKANEXBASE_MYVULKANMANAGER_H
#define VULKANEXBASE_MYVULKANMANAGER_H
#include <vector>
#include <vulkan/vulkan.h>
#include <vector>
#include "../util/DrawableObjectCommon.h"
#include "ShaderQueueSuit_Common.h"
#define FENCE_TIMEOUT 100000000
class MyVulkanManager
{
public:
//窗口辅助结构体
static struct WindowInfo info;
//vulkan绘制的循环标志
static bool loopDrawFlag;
static std::vector<const char *> instanceExtensionNames;//需要使用的实例扩展名称列表
static VkInstance instance;//Vulkan实例
static uint32_t gpuCount;//物理设备数量
static std::vector<VkPhysicalDevice> gpus; //物理设备列表
static uint32_t queueFamilyCount;//物理设备对应的队列家族数量
static std::vector<VkQueueFamilyProperties> queueFamilyprops;//物理设备对应的队列家族属性列表
static uint32_t queueGraphicsFamilyIndex;//支持图形工作的队列家族索引
static VkQueue queueGraphics;//支持图形工作的队列
static uint32_t queuePresentFamilyIndex;//支持显示工作的队列家族索引
static std::vector<const char *> deviceExtensionNames; //所需的设备扩展名称列表
static VkDevice device; //逻辑设备
static VkCommandPool cmdPool;//命令池
static VkCommandBuffer cmdBuffer;//命令缓冲
static VkCommandBufferBeginInfo cmd_buf_info;//命令缓冲启动信息
static VkCommandBuffer cmd_bufs[1]; //供提交执行的命令缓冲数组
static VkSubmitInfo submit_info[1]; //命令缓冲提交执行信息数组
static uint32_t screenWidth;//屏幕宽度
static uint32_t screenHeight;//屏幕高度
static VkSurfaceKHR surface;//KHR表面
static std::vector<VkFormat> formats;//KHR表面支持的格式
static VkSurfaceCapabilitiesKHR surfCapabilities;//表面的能力
static uint32_t presentModeCount;//显示模式数量
static std::vector<VkPresentModeKHR> presentModes;//显示模式列表
static VkExtent2D swapchainExtent;//交换链尺寸
static VkSwapchainKHR swapChain;//交换链
static uint32_t swapchainImageCount;//交换链中的图像数量
static std::vector<VkImage> swapchainImages;//交换链中的图像列表
static std::vector<VkImageView> swapchainImageViews;//交换链对应的的图像视图列表
static VkFormat depthFormat;//深度图像格式
static VkFormatProperties depthFormatProps; //物理设备支持的深度格式属性
static VkImage depthImage;//深度缓冲图像
static VkPhysicalDeviceMemoryProperties memoryroperties;//物理设备内存属性
static VkDeviceMemory memDepth; //深度缓冲图像对应的内存
static VkImageView depthImageView;//深度缓冲图像视图
static VkSemaphore imageAcquiredSemaphore;//渲染目标图像获取完成信号量
static uint32_t currentBuffer;//从交换链中获取的当前渲染用图像对应的缓冲编号
static VkRenderPass renderPass;//渲染通道
static VkClearValue clear_values[2];//渲染通道用清除帧缓冲深度、颜色附件的数据
static VkRenderPassBeginInfo rp_begin;//渲染通道启动信息
static VkFence taskFinishFence;//等待任务完毕的栅栏
static VkPresentInfoKHR present;//呈现信息
static VkFramebuffer* framebuffers;//帧缓冲序列首指针
static ShaderQueueSuit_Common* sqsCL;//着色器管线指针
static DrawableObjectCommonLight* triForDraw;//绘制用三色三角形物体对象指针
//三角形旋转角度
static float xAngle;
static float yAngle;
static float zAngle;
static void init_vulkan_instance();//创建Vulkan实例
static void enumerate_vulkan_phy_devices();//初始化物理设备
static void create_vulkan_devices();//创建逻辑设备
static void create_vulkan_CommandBuffer();//创建命令缓冲
static void create_vulkan_swapChain();//初始化交换链
static void create_vulkan_DepthBuffer();//创建深度缓冲相关
static void create_render_pass();//创建渲染通道
static void init_queue();//获取设备中支持图形工作的队列
static void create_frame_buffer();//创建帧缓冲
static void createDrawableObject();//创建绘制用物体
static void drawObject();//执行场景中的物体绘制
static void doVulkan();//启动线程执行Vulkan任务
static void initPipeline();//初始化管线
static void createFence();//创建栅栏
static void initPresentInfo();//初始化显示信息
static void initMatrix();//初始化矩阵
static void flushUniformBuffer();//将一致变量数据送入缓冲
static void flushTexToDesSet();//将纹理等数据与描述集关联
static void destroyFence();//销毁栅栏
static void destroyPipeline();//销毁管线
static void destroyDrawableObject();//销毁绘制用物体
static void destroy_frame_buffer();//销毁帧缓冲
static void destroy_render_pass();//销毁渲染通道
static void destroy_vulkan_DepthBuffer();//销毁深度缓冲相关
static void destroy_vulkan_swapChain(

1137

被折叠的 条评论
为什么被折叠?



