plumageRender/base/vulkanexamplebase.h

167 lines
3.8 KiB
C++

/*
* Vulkan Example base class
*
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#pragma once
#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>
#include <sstream>
#include <array>
#include <numeric>
#include "vulkan/vulkan.h"
#include "VulkanTools.h"
#include "camera.hpp"
#include "keycodes.hpp"
#include "VulkanDevice.hpp"
#include "VulkanSwapChain.hpp"
#include "imgui/imgui.h"
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;
struct MultisampleTarget {
struct {
VkImage image;
VkImageView view;
VkDeviceMemory memory;
} color;
struct {
VkImage image;
VkImageView view;
VkDeviceMemory memory;
} depth;
} multisampleTarget;
protected:
VkInstance instance;
VkPhysicalDevice physicalDevice;
VkPhysicalDeviceProperties deviceProperties;
VkPhysicalDeviceFeatures deviceFeatures;
VkPhysicalDeviceMemoryProperties deviceMemoryProperties;
VkDevice device;
vks::VulkanDevice *vulkanDevice;
VkQueue queue;
VkFormat depthFormat;
VkCommandPool cmdPool;
VkRenderPass renderPass;
std::vector<VkFramebuffer>frameBuffers;
uint32_t currentBuffer = 0;
VkDescriptorPool descriptorPool;
VkPipelineCache pipelineCache;
VulkanSwapChain swapChain;
std::string title = "Vulkan Example";
std::string name = "vulkanExample";
//void windowResize();
public:
static std::vector<const char*> args;
bool prepared = false;
uint32_t width = 1920;
uint32_t height = 1080;
float frameTimer = 1.0f;
Camera camera;
glm::vec2 mousePos;
bool paused = false;
uint32_t lastFPS = 0;
VkFormat colorFormat = VK_FORMAT_R8G8B8A8_SRGB;
uint32_t queueFamilyIndex;
uint32_t renderingFrameIndex = 3;
struct Signal
{
bool imageSequenceOutputComplete = false;
bool imageSequenceToVideoComplete = false;
}signal;
struct Settings {
uint32_t selectedPhysicalDeviceIndex = 7;
bool validation = false; // 校验层开关
bool fullscreen = false; // 全屏开关
bool vsync = false; // 垂直同步开关
bool multiSampling = false; // 多重采样
bool rotateModel = true; // 模型自旋转(暂时失效)
bool headless = false; // 无头开关
bool outputPNGimage = false;
bool enableSaveToImageSequeue = true; // 图片序列开关(暂时弃用)
uint32_t outputFrameCount = 10; // 图片序列结束帧
bool takeScreenShot = false; // 截屏(暂时弃用)
uint32_t startFrameCount = 1; // 图片序列开始帧
uint32_t videoFrameRate = 25;
VkSampleCountFlagBits sampleCount = VK_SAMPLE_COUNT_4_BIT; // 多重采样倍率
} settings;
struct DepthStencil {
VkImage image;
VkDeviceMemory mem;
VkImageView view;
} depthStencil;
struct ColorAttachment {
VkImage image;
VkDeviceMemory memory;
VkImageView view;
} colorAttachment;
struct GamePadState {
glm::vec2 axisLeft = glm::vec2(0.0f);
glm::vec2 axisRight = glm::vec2(0.0f);
} gamePadState;
struct MouseButtons {
bool left = false;
bool right = false;
bool middle = false;
} mouseButtons;
// OS specific
VulkanExampleBase();
virtual ~VulkanExampleBase();
void initVulkan();
virtual VkResult createInstance(bool enableValidation);
virtual void render() = 0;
//virtual void windowResized();
virtual void setupFrameBuffer();
virtual void prepare();
virtual void fileDropped(std::string filename);
void initSwapchain();
void setupSwapChain();
void renderLoop();
void renderFrame();
};