plumageRender/base/vulkanexamplebase.h

167 lines
3.8 KiB
C
Raw Permalink Normal View History

2023-05-17 14:49:05 +08:00
/*
* Vulkan Example base class
*
2023-06-06 15:52:39 +08:00
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
2023-05-17 14:49:05 +08:00
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#pragma once
2024-04-16 16:43:47 +08:00
2023-05-17 14:49:05 +08:00
#include <iostream>
#include <chrono>
#include <sys/stat.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <string>
2023-06-06 15:52:39 +08:00
#include <sstream>
2023-05-17 14:49:05 +08:00
#include <array>
2023-06-06 15:52:39 +08:00
#include <numeric>
2023-05-17 14:49:05 +08:00
#include "vulkan/vulkan.h"
#include "VulkanTools.h"
2023-06-06 15:52:39 +08:00
#include "camera.hpp"
2023-05-17 14:49:05 +08:00
#include "keycodes.hpp"
2023-06-06 15:52:39 +08:00
2023-06-06 11:40:59 +08:00
#include "VulkanDevice.hpp"
2023-06-06 15:52:39 +08:00
#include "VulkanSwapChain.hpp"
2023-05-17 14:49:05 +08:00
2023-06-06 15:52:39 +08:00
#include "imgui/imgui.h"
2023-05-17 14:49:05 +08:00
class VulkanExampleBase
{
private:
float fpsTimer = 0.0f;
uint32_t frameCounter = 0;
uint32_t destWidth;
uint32_t destHeight;
bool resizing = false;
void handleMouseMove(int32_t x, int32_t y);
PFN_vkCreateDebugReportCallbackEXT vkCreateDebugReportCallback;
PFN_vkDestroyDebugReportCallbackEXT vkDestroyDebugReportCallback;
VkDebugReportCallbackEXT debugReportCallback;
2023-06-06 15:52:39 +08:00
struct MultisampleTarget {
struct {
VkImage image;
VkImageView view;
VkDeviceMemory memory;
} color;
struct {
VkImage image;
VkImageView view;
VkDeviceMemory memory;
} depth;
} multisampleTarget;
2023-05-17 14:49:05 +08:00
protected:
VkInstance instance;
VkPhysicalDevice physicalDevice;
VkPhysicalDeviceProperties deviceProperties;
VkPhysicalDeviceFeatures deviceFeatures;
VkPhysicalDeviceMemoryProperties deviceMemoryProperties;
VkDevice device;
2023-06-06 15:52:39 +08:00
vks::VulkanDevice *vulkanDevice;
2023-05-17 14:49:05 +08:00
VkQueue queue;
VkFormat depthFormat;
VkCommandPool cmdPool;
2023-06-06 15:52:39 +08:00
VkRenderPass renderPass;
2023-05-17 14:49:05 +08:00
std::vector<VkFramebuffer>frameBuffers;
uint32_t currentBuffer = 0;
2023-06-06 15:52:39 +08:00
VkDescriptorPool descriptorPool;
2023-05-17 14:49:05 +08:00
VkPipelineCache pipelineCache;
VulkanSwapChain swapChain;
2023-06-06 15:52:39 +08:00
std::string title = "Vulkan Example";
std::string name = "vulkanExample";
2024-04-16 16:43:47 +08:00
//void windowResize();
2023-06-06 15:52:39 +08:00
public:
static std::vector<const char*> args;
2024-04-17 13:39:27 +08:00
2023-05-17 14:49:05 +08:00
bool prepared = false;
uint32_t width = 1920;
uint32_t height = 1080;
float frameTimer = 1.0f;
2023-06-06 15:52:39 +08:00
Camera camera;
glm::vec2 mousePos;
bool paused = false;
uint32_t lastFPS = 0;
2023-05-17 14:49:05 +08:00
2024-04-16 13:23:41 +08:00
VkFormat colorFormat = VK_FORMAT_R8G8B8A8_SRGB;
2024-04-16 15:12:42 +08:00
uint32_t queueFamilyIndex;
2024-04-16 16:15:39 +08:00
uint32_t renderingFrameIndex = 3;
2024-04-16 11:38:12 +08:00
struct Signal
{
bool imageSequenceOutputComplete = false;
bool imageSequenceToVideoComplete = false;
}signal;
2023-05-17 14:49:05 +08:00
struct Settings {
2024-04-17 13:39:27 +08:00
uint32_t selectedPhysicalDeviceIndex = 7;
bool validation = false; // 校验层开关
bool fullscreen = false; // 全屏开关
bool vsync = false; // 垂直同步开关
2024-04-16 11:38:12 +08:00
bool multiSampling = false; // 多重采样
bool rotateModel = true; // 模型自旋转(暂时失效)
bool headless = false; // 无头开关
2024-03-29 15:17:09 +08:00
bool outputPNGimage = false;
bool enableSaveToImageSequeue = true; // 图片序列开关(暂时弃用)
2024-04-18 10:04:21 +08:00
uint32_t outputFrameCount = 10; // 图片序列结束帧
bool takeScreenShot = false; // 截屏(暂时弃用)
uint32_t startFrameCount = 1; // 图片序列开始帧
uint32_t videoFrameRate = 25;
VkSampleCountFlagBits sampleCount = VK_SAMPLE_COUNT_4_BIT; // 多重采样倍率
2023-05-17 14:49:05 +08:00
} settings;
struct DepthStencil {
2023-05-17 14:49:05 +08:00
VkImage image;
VkDeviceMemory mem;
2023-05-17 14:49:05 +08:00
VkImageView view;
} depthStencil;
2023-05-17 14:49:05 +08:00
2024-04-16 11:38:12 +08:00
struct ColorAttachment {
VkImage image;
VkDeviceMemory memory;
VkImageView view;
} colorAttachment;
2023-06-06 15:52:39 +08:00
struct GamePadState {
2023-05-17 14:49:05 +08:00
glm::vec2 axisLeft = glm::vec2(0.0f);
glm::vec2 axisRight = glm::vec2(0.0f);
} gamePadState;
2023-06-06 15:52:39 +08:00
struct MouseButtons {
2023-05-17 14:49:05 +08:00
bool left = false;
bool right = false;
bool middle = false;
} mouseButtons;
2023-06-06 15:52:39 +08:00
// OS specific
2023-05-17 14:49:05 +08:00
2023-06-06 15:52:39 +08:00
VulkanExampleBase();
virtual ~VulkanExampleBase();
void initVulkan();
2023-05-17 14:49:05 +08:00
virtual VkResult createInstance(bool enableValidation);
virtual void render() = 0;
2024-04-16 16:43:47 +08:00
//virtual void windowResized();
2023-05-17 14:49:05 +08:00
virtual void setupFrameBuffer();
virtual void prepare();
2023-06-06 15:52:39 +08:00
virtual void fileDropped(std::string filename);
2023-05-17 14:49:05 +08:00
2023-06-06 15:52:39 +08:00
void initSwapchain();
void setupSwapChain();
2023-05-17 14:49:05 +08:00
void renderLoop();
2023-06-06 15:52:39 +08:00
void renderFrame();
2023-05-17 14:49:05 +08:00
};