plumageRender/base/vulkanexamplebase.h

213 lines
5.9 KiB
C
Raw 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
2024-04-18 18:26:07 +08:00
//#define GLM_FORCE_LEFT_HANDED
2023-05-17 14:49:05 +08:00
#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>
2024-04-18 18:26:07 +08:00
#include <filesystem>
2023-05-17 14:49:05 +08:00
2024-04-17 11:26:38 +08:00
#include "renderConfig.h"
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"
2024-04-16 16:59:10 +08:00
//#include "VulkanSwapChain.hpp"
2023-05-17 14:49:05 +08:00
2024-04-16 16:59:10 +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;
2024-04-16 16:59:10 +08:00
//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;
2024-04-16 16:59:10 +08:00
//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 11:26:38 +08:00
// temporary
PlumageConfig::PlumageConfiguration setter;
PlumageConfig::PlumageConfiguration::Settings settings = setter.settings;
PlumageConfig::PlumageConfiguration::FilePath filePath = setter.filePath;
2024-04-19 18:08:16 +08:00
2024-03-22 18:05:18 +08:00
uint32_t selectedPhysicalDeviceIndex = 0;
2023-05-17 14:49:05 +08:00
bool prepared = false;
2024-04-23 10:42:15 +08:00
uint32_t currentFrame = 0;
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;
2024-04-17 11:26:38 +08:00
/*
2024-04-16 18:04:26 +08:00
struct FilePath
{ //model path
std::string glTFModelFilePath = getAssetPath() + "models/DamagedHelmet/DamagedHelmet.gltf";
std::string modelVertShaderPath = getAssetPath() + "buster_drone/shaders/glsl/mesh.vert.spv";
std::string modelFragShaderPath = getAssetPath() + "buster_drone/shaders/glsl/mesh.frag.spv";
//ui
std::string uiVertShaderPath = getAssetPath() + "shaders/ui.vert.spv";
std::string uiFragShaderPath = getAssetPath() + "shaders/ui.frag.spv";
// skybox path
std::string skyboxModleFilePath = getAssetPath() + "models/cube.gltf";
std::string skyboxVertShaderPath = getAssetPath() + "shaders/skybox.vert.spv";
std::string skyboxFragShaderPath = getAssetPath() + "shaders/skybox.frag.spv";
std::string iblTexturesFilePath = getAssetPath() + "textures/hdr/gcanyon_cube.ktx";
//tonemapping
std::string tonemappingVertShaderPath = getAssetPath() + "buster_drone/shaders/glsl/genbrdflut.vert.spv";
std::string tonemappingEnableFragShaderPath = getAssetPath() + "buster_drone/shaders/glsl/tonemapping_enable.frag.spv";
std::string tonemappingDisableFragShaderPath = getAssetPath() + "buster_drone/shaders/glsl/tonemapping_disable.frag.spv";
// cube map
std::string irradianceFragShaderPath = getAssetPath() + "shaders/irradiancecube.frag.spv";
std::string filterVertShaderPath = getAssetPath() + "shaders/filtercube.vert.spv";
std::string prefilterEnvmapFragShaderPath = getAssetPath() + "shaders/prefilterenvmap.frag.spv";
//brdf cube map
std::string brdfVertShaderPath = getAssetPath() + "shaders/genbrdflut.vert.spv";
std::string brdfFragShaderPath = getAssetPath() + "shaders/genbrdflut.frag.spv";
// environment map texture
std::string envMapFilePath = getAssetPath() + "environments/metro_noord_4k_hdr16f_cube.ktx";
std::string emptyEnvmapFilePath = getAssetPath() + "textures/empty.ktx";
// pbr shader
std::string pbrVertShaderPath = getAssetPath() + "shaders/pbr.vert.spv";
std::string pbrFragShaderPath = getAssetPath() + "shaders/pbr_khr.frag.spv";
//ttf file path
std::string ttfFilePath = getAssetPath() + "/data/Roboto-Medium.ttf";
// output file path
std::string imageOutputPath = getAssetPath() + "output/imageSequence";
std::string videoOutputPath = getAssetPath() + "output/video";
std::string totalImageOutputPath;
std::string deviceSpecFilePath;
// script file path
std::string image2videoBatFilePath = getAssetPath() + "script/image2video.bat";
std::string image2videoShFilePath = getAssetPath() + "script/image2video.sh";
} filePath;
2024-04-17 11:26:38 +08:00
*/
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
};