PeriDyno 1.0.0
Loading...
Searching...
No Matches
VkApp.h
Go to the documentation of this file.
1/*
2* Vulkan Example base class
3*
4* Copyright (C) by Sascha Willems - www.saschawillems.de
5*
6* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
7*/
8
9#pragma once
10
11#ifdef _WIN32
12#pragma comment(linker, "/subsystem:windows")
13#include <windows.h>
14#include <fcntl.h>
15#include <io.h>
16#include <ShellScalingAPI.h>
17#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
18#include <android/native_activity.h>
19#include <android/asset_manager.h>
20#include <android_native_app_glue.h>
21#include <sys/system_properties.h>
22#include "VulkanAndroid.h"
23#endif
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <assert.h>
29#include <vector>
30#include <array>
31#include <numeric>
32#include <ctime>
33#include <iostream>
34#include <chrono>
35#include <random>
36#include <algorithm>
37#include <sys/stat.h>
38
39#define GLM_FORCE_RADIANS
40#define GLM_FORCE_DEPTH_ZERO_TO_ONE
41#define GLM_ENABLE_EXPERIMENTAL
42#include <glm/glm.hpp>
43#include <glm/gtc/matrix_transform.hpp>
44#include <glm/gtc/matrix_inverse.hpp>
45#include <glm/gtc/type_ptr.hpp>
46#include <string>
47#include <numeric>
48#include <array>
49
50#include "vulkan/vulkan.h"
51
52#include "Keys.h"
53#include "VulkanTools.h"
54#include "VulkanDebug.h"
55#include "VulkanUIOverlay.h"
56#include "VulkanSwapChain.h"
57#include "VulkanBuffer.h"
58#include "VulkanTexture.h"
59
61#include "Camera.h"
62
63#include "VkContext.h"
64
65namespace dyno
66{
67 class SceneGraph;
68}
69
70
71class VkApp
72{
73private:
74 std::string getWindowTitle();
75 bool viewUpdated = false;
76 uint32_t destWidth;
77 uint32_t destHeight;
78 bool resizing = false;
79 void windowResize();
80 void handleMouseMove(int32_t x, int32_t y);
81 void nextFrame();
82 void updateOverlay();
83 void createCommandPool();
85 void initSwapchain();
86 void setupSwapChain();
89 std::string shaderDir = "glsl";
90protected:
91 // Returns the path to the root of the glsl or hlsl shader directory.
92 std::string getShadersPath() const;
93
94 // Frame counter to display fps
95 uint32_t frameCounter = 0;
96 uint32_t lastFPS = 0;
97 std::chrono::time_point<std::chrono::high_resolution_clock> lastTimestamp;
98 // Vulkan instance, stores all per-application states
99
101 std::vector<const char*> enabledDeviceExtensions;
102 std::vector<const char*> enabledInstanceExtensions;
104 void* deviceCreatepNextChain = nullptr;
106 //VkDevice device;
107 // Handle to the device graphics queue that command buffers are submitted to
108 //VkQueue queue;
109 // Depth buffer format (selected during Vulkan initialization)
110 VkFormat depthFormat;
111 // Command buffer pool
112 VkCommandPool cmdPool;
114 VkPipelineStageFlags submitPipelineStages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
115 // Contains command buffers and semaphores to be presented to the queue
116 VkSubmitInfo submitInfo;
117 // Command buffers used for rendering
118 std::vector<VkCommandBuffer> drawCmdBuffers;
119 // Global render pass for frame buffer writes
120 VkRenderPass renderPass;
121 // List of available frame buffers (same as number of swap chain images)
122 std::vector<VkFramebuffer>frameBuffers;
123 // Active frame buffer index
124 uint32_t currentBuffer = 0;
125 // Descriptor set pool
126 VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
127 // List of shader modules created (stored for cleanup)
128 std::vector<VkShaderModule> shaderModules;
129// // Pipeline cache object
130// VkPipelineCache pipelineCache;
131 // Wraps the swap chain to present images (framebuffers) to the windowing system
133 // Synchronization semaphores
134 struct {
135 // Swap chain image presentation
136 VkSemaphore presentComplete;
137 // Command buffer submission and execution
138 VkSemaphore renderComplete;
140 std::vector<VkFence> waitFences;
141public:
142 bool swapChainCleaned = false;
143 bool prepared = false;
144 bool resized = false;
145 uint32_t width = 1280;
146 uint32_t height = 720;
147
149
151 float frameTimer = 1.0f;
152
154
156 struct Settings {
158 bool validation = false;
160 bool fullscreen = false;
162 bool vsync = false;
164 bool overlay = false;
166
167 VkClearColorValue defaultClearColor = { { 0.025f, 0.025f, 0.025f, 1.0f } };
168
169 static std::vector<const char*> args;
170
171 // Defines a frame rate independent timer value clamped from -1.0...1.0
172 // For use in animations, rotations, etc.
173 float timer = 0.0f;
174 // Multiplier for speeding up (or slowing down) the global timer
175 float timerSpeed = 0.25f;
176 bool paused = false;
177
179 glm::vec2 mousePos;
180
181 std::string title = "Vulkan Example";
182 std::string name = "vulkanExample";
183 uint32_t apiVersion = VK_API_VERSION_1_0;
184
185 struct {
186 VkImage image;
187 VkDeviceMemory mem;
188 VkImageView view;
190
191 struct {
192 glm::vec2 axisLeft = glm::vec2(0.0f);
193 glm::vec2 axisRight = glm::vec2(0.0f);
195
196 struct {
197 bool left = false;
198 bool right = false;
199 bool middle = false;
201
202 // OS specific
203#if defined(_WIN32)
204 HWND window;
205 HINSTANCE windowInstance;
206#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
207 // true if application has focused, false if moved to background
208 bool focused = false;
209 struct TouchPos {
210 int32_t x;
211 int32_t y;
212 } touchPos;
213 bool touchDown = false;
214 double touchTimer = 0.0;
215 int64_t lastTapTime = 0;
216#endif
217
218 VkApp(bool enableValidation = false);
219 virtual ~VkApp();
221 bool initVulkan();
222
223#if defined(_WIN32)
224 void setupConsole(std::string title);
225 void setupDPIAwareness();
226 HWND setupWindow(HINSTANCE hinstance, WNDPROC wndproc);
227 void handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
228#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
229 static int32_t handleAppInput(struct android_app* app, AInputEvent* event);
230 static void handleAppCommand(android_app* app, int32_t cmd);
231#endif
233 virtual void render();
235 virtual void viewChanged();
237 virtual void keyPressed(uint32_t);
239 virtual void mouseMoved(double x, double y, bool &handled);
241 virtual void windowResized();
243 virtual void buildCommandBuffers();
245 virtual void setupDepthStencil();
247 virtual void setupFrameBuffer();
249 virtual void setupRenderPass();
251 virtual void getEnabledFeatures();
252
254 virtual void prepare();
255
257 VkPipelineShaderStageCreateInfo loadShader(std::string fileName, VkShaderStageFlagBits stage);
258
260 void renderLoop();
261
263 void drawUI(const VkCommandBuffer commandBuffer);
264
266 void prepareFrame();
268 void submitFrame();
270 virtual void renderFrame();
271
273 virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay);
274
275
276 void buildCustomCommandBuffer(VkCommandBuffer commandBuffer);
277
278 void prepare(VkRenderPass renderPass);
279
280 void setWindowTitle(std::string name);
281
282 void setSceneGraph(std::shared_ptr<dyno::SceneGraph> scn);
283};
284
void prepareFrame()
Definition VkApp.cpp:357
void drawUI(const VkCommandBuffer commandBuffer)
Adds the drawing commands for the ImGui overlay to the given command buffer.
Definition VkApp.cpp:345
uint32_t apiVersion
Definition VkApp.h:183
dyno::VkContext * ctx
Definition VkApp.h:153
uint32_t destHeight
Definition VkApp.h:77
std::string getWindowTitle()
Definition VkApp.cpp:24
uint32_t lastFPS
Definition VkApp.h:96
VkDescriptorPool descriptorPool
Definition VkApp.h:126
VkSubmitInfo submitInfo
Definition VkApp.h:116
bool left
Definition VkApp.h:197
void nextFrame()
Definition VkApp.cpp:111
bool prepared
Definition VkApp.h:143
bool resized
Definition VkApp.h:144
void destroyCommandBuffers()
Definition VkApp.cpp:49
void buildCustomCommandBuffer(VkCommandBuffer commandBuffer)
Definition VkApp.cpp:1342
bool middle
Definition VkApp.h:199
float timer
Definition VkApp.h:173
virtual void mouseMoved(double x, double y, bool &handled)
(Virtual) Called after the mouse cursor moved and before internal events (like camera rotation) is ha...
Definition VkApp.cpp:1032
virtual void buildCommandBuffers()
(Virtual) Called when resources have been recreated that require a rebuild of the command buffers (e....
Definition VkApp.cpp:1034
virtual void getEnabledFeatures()
(Virtual) Called after the physical device features have been read, can be used to set features to en...
Definition VkApp.cpp:1237
std::vector< const char * > enabledDeviceExtensions
Set of device extensions to be enabled for this example (must be set in the derived constructor)
Definition VkApp.h:101
virtual void setupFrameBuffer()
(Virtual) Setup default framebuffers for all requested swapchain images
Definition VkApp.cpp:1139
VkImageView view
Definition VkApp.h:188
glm::vec2 mousePos
Definition VkApp.h:179
void initSwapchain()
Definition VkApp.cpp:1326
std::string getShadersPath() const
Definition VkApp.cpp:54
virtual ~VkApp()
Definition VkApp.cpp:426
std::vector< const char * > enabledInstanceExtensions
Definition VkApp.h:102
vks::UIOverlay UIOverlay
Definition VkApp.h:148
VkPipelineShaderStageCreateInfo loadShader(std::string fileName, VkShaderStageFlagBits stage)
Loads a SPIR-V shader file for the given shader stage.
Definition VkApp.cpp:99
void * deviceCreatepNextChain
Optional pNext structure for passing extension structures to device creation.
Definition VkApp.h:104
bool paused
Definition VkApp.h:176
virtual void renderFrame()
(Virtual) Default image acquire + submission and command buffer submission function
Definition VkApp.cpp:15
virtual void viewChanged()
(Virtual) Called when the camera view has changed
Definition VkApp.cpp:1013
void setWindowTitle(std::string name)
Definition VkApp.cpp:1379
virtual void keyPressed(uint32_t)
(Virtual) Called after a key was pressed, can be used to do custom key handling
Definition VkApp.cpp:1030
float frameTimer
Last frame time measured using a high performance timer (if available)
Definition VkApp.h:151
void submitFrame()
Presents the current image to the swap chain.
Definition VkApp.cpp:370
void windowResize()
Definition VkApp.cpp:1239
VkClearColorValue defaultClearColor
Definition VkApp.h:167
void handleMouseMove(int32_t x, int32_t y)
Definition VkApp.cpp:1291
VkPipelineStageFlags submitPipelineStages
Pipeline stages used to wait at for graphics queue submissions.
Definition VkApp.h:114
VkDeviceMemory mem
Definition VkApp.h:187
virtual void setupDepthStencil()
(Virtual) Setup default depth and stencil views
Definition VkApp.cpp:1098
void setupSwapChain()
Definition VkApp.cpp:1335
void renderLoop()
Entry point for the main render loop.
Definition VkApp.cpp:156
uint32_t currentBuffer
Definition VkApp.h:124
std::vector< VkFence > waitFences
Definition VkApp.h:140
void createSynchronizationPrimitives()
Definition VkApp.cpp:1079
VkApp(bool enableValidation=false)
Definition VkApp.cpp:385
virtual void windowResized()
(Virtual) Called when the window has been resized, can be used by the sample application to recreate ...
Definition VkApp.cpp:1324
struct VkApp::@035006017256177105240376270267042317070052055111 depthStencil
std::chrono::time_point< std::chrono::high_resolution_clock > lastTimestamp
Definition VkApp.h:97
virtual void prepare()
Prepares all Vulkan resources and functions required to run the sample.
Definition VkApp.cpp:59
bool viewUpdated
Definition VkApp.h:75
void createCommandPool()
Definition VkApp.cpp:1089
uint32_t width
Definition VkApp.h:145
VkFormat depthFormat
Logical device, application's view of the physical device (GPU)
Definition VkApp.h:110
bool swapChainCleaned
Definition VkApp.h:142
std::vector< VkFramebuffer > frameBuffers
Definition VkApp.h:122
Camera camera
Definition VkApp.h:178
static std::vector< const char * > args
Definition VkApp.h:169
std::vector< VkCommandBuffer > drawCmdBuffers
Definition VkApp.h:118
float timerSpeed
Definition VkApp.h:175
std::string name
Definition VkApp.h:182
virtual void render()
(Pure virtual) Render function to be implemented by the sample application
Definition VkApp.cpp:990
VkSemaphore renderComplete
Definition VkApp.h:138
glm::vec2 axisLeft
Definition VkApp.h:192
VulkanSwapChain swapChain
Definition VkApp.h:132
uint32_t height
Definition VkApp.h:146
uint32_t frameCounter
Definition VkApp.h:95
uint32_t destWidth
Definition VkApp.h:76
VkCommandPool cmdPool
Definition VkApp.h:112
VkImage image
Definition VkApp.h:186
void updateOverlay()
Definition VkApp.cpp:295
std::string title
Definition VkApp.h:181
void setSceneGraph(std::shared_ptr< dyno::SceneGraph > scn)
Definition VkApp.cpp:1384
struct VkApp::@024362050273107367271176263277065044127044370222 semaphores
VkRenderPass renderPass
Definition VkApp.h:120
bool resizing
Definition VkApp.h:78
struct VkApp::@236151146162240273010117313013267214200120303363 gamePadState
std::string shaderDir
Definition VkApp.h:89
bool right
Definition VkApp.h:198
void createCommandBuffers()
Definition VkApp.cpp:35
glm::vec2 axisRight
Definition VkApp.h:193
virtual void setupRenderPass()
(Virtual) Setup a default renderpass
Definition VkApp.cpp:1165
VkSemaphore presentComplete
Definition VkApp.h:136
struct VkApp::Settings settings
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)
(Virtual) Called when the UI overlay is updating, can be used to add custom elements to the overlay
Definition VkApp.cpp:1340
bool initVulkan()
Setup the vulkan instance, enable required extensions and connect to the physical device (GPU)
Definition VkApp.cpp:468
struct VkApp::@033204162100256106143271256032327056257013175042 mouseButtons
std::vector< VkShaderModule > shaderModules
Definition VkApp.h:128
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Example settings that can be changed e.g. by command line arguments.
Definition VkApp.h:156
bool fullscreen
Set to true if fullscreen mode has been requested via command line.
Definition VkApp.h:160
bool overlay
Enable UI overlay.
Definition VkApp.h:164
bool validation
Activates validation layers (and message output) when set to true.
Definition VkApp.h:158
bool vsync
Set to true if v-sync will be forced for the swapchain.
Definition VkApp.h:162