~sircmpwn/xrgears

ref: f53b7eaffce0ac98c6541802803df215df383f11 xrgears/vks/vksApplication.hpp -rw-r--r-- 8.5 KiB
f53b7eaf — Lubosz Sarnecki make vkc and vks classes inherit from abstact vik classes. 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* 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 <sys/stat.h>

#include <vulkan/vulkan.h>

#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>

#include <iostream>
#include <chrono>
#include <string>
#include <array>
#include <vector>

#include "vksTools.hpp"
#include "vksDebug.hpp"
#include "vksInitializers.hpp"
#include "vksDevice.hpp"
#include "vksSwapChain.hpp"
#include "vksTextOverlay.hpp"
#include "vksCamera.hpp"
#include "vksRenderer.hpp"
#include "vksFrameCounter.hpp"
#include "vksSettings.hpp"

#include "../vitamin-k/vikApplication.hpp"

namespace vks {
class Window;

class Application : public vik::Application {

 public:
  vks::Renderer *renderer;

  vks::Timer timer;

  // fps timer (one second interval)

  // Get window title with example name, device, et.
  std::string getWindowTitle();
  /** brief Indicates that the view (position, rotation) has changed and */
  bool viewUpdated = false;
  // Destination dimensions for resizing the window
  uint32_t destWidth;
  uint32_t destHeight;
  bool resizing = false;
  // Called if the window is resized and some resources have to be recreatesd
  void windowResize();

 public:
  // Frame counter to display fps


  // Physical device (GPU) that Vulkan will ise
  VkPhysicalDevice physicalDevice;
  // Stores physical device properties (for e.g. checking device limits)
  VkPhysicalDeviceProperties deviceProperties;
  // Stores the features available on the selected physical device (for e.g. checking if a feature is available)
  VkPhysicalDeviceFeatures deviceFeatures;
  // Stores all available memory (type) properties for the physical device
  VkPhysicalDeviceMemoryProperties deviceMemoryProperties;
  /**
  * Set of physical device features to be enabled for this example (must be set in the derived constructor)
  *
  * @note By default no phyiscal device features are enabled
  */
  VkPhysicalDeviceFeatures enabledFeatures{};
  /** @brief Set of device extensions to be enabled for this example (must be set in the derived constructor) */
  std::vector<const char*> enabledExtensions;
  /** @brief Logical device, application's view of the physical device (GPU) */
  // todo: getter? should always point to VulkanDevice->device
  VkDevice device;
  // Handle to the device graphics queue that command buffers are submitted to
  VkQueue queue;
  // Depth buffer format (selected during Vulkan initialization)
  VkFormat depthFormat;
  // Command buffer pool
  VkCommandPool cmdPool;
  /** @brief Pipeline stages used to wait at for graphics queue submissions */
  VkPipelineStageFlags submitPipelineStages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  // Contains command buffers and semaphores to be presented to the queue
  VkSubmitInfo submitInfo;
  // Command buffers used for rendering
  std::vector<VkCommandBuffer> drawCmdBuffers;
  // Global render pass for frame buffer writes
  VkRenderPass renderPass;
  // List of available frame buffers (same as number of swap chain images)
  std::vector<VkFramebuffer>frameBuffers;
  // Active frame buffer index
  uint32_t currentBuffer = 0;
  // Descriptor set pool
  VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
  // List of shader modules created (stored for cleanup)
  std::vector<VkShaderModule> shaderModules;
  // Pipeline cache object
  VkPipelineCache pipelineCache;
  // Wraps the swap chain to present images (framebuffers) to the windowing system
  SwapChain swapChain;
  // Synchronization semaphores

  struct {
    // Swap chain image presentation
    VkSemaphore presentComplete;
    // Command buffer submission and execution
    VkSemaphore renderComplete;
    // Text overlay submission and execution
    VkSemaphore textOverlayComplete;
  } semaphores;

 public:
  bool prepared = false;
  /*
  uint32_t width = 1080*2;
  uint32_t height = 1200;
  */
  /*
  uint32_t width = 1920;
  uint32_t height = 1200;
*/
  uint32_t width = 2560;
  uint32_t height = 1440;



  /*
  uint32_t width = 1366;
  uint32_t height = 768;
  */

  /** @brief Encapsulated physical and logical vulkan device */
  vks::Device *vksDevice;

  /** @brief Example settings that can be changed e.g. by command line arguments */
  Settings settings;

  VkClearColorValue defaultClearColor = { { 0.025f, 0.025f, 0.025f, 1.0f } };

  float zoom = 0;

  bool enableTextOverlay = true;
  TextOverlay *textOverlay;

  // Use to adjust mouse rotation speed
  float rotationSpeed = 1.0f;
  // Use to adjust mouse zoom speed
  float zoomSpeed = 1.0f;

  Camera camera;

  glm::vec3 rotation = glm::vec3();
  glm::vec3 cameraPos = glm::vec3();
  glm::vec2 mousePos;

  std::string title = "Vulkan Example";
  std::string name = "vulkanExample";

  bool quit = false;
  struct {
    bool left = false;
    bool right = false;
    bool middle = false;
  } mouseButtons;

  struct {
    VkImage image;
    VkDeviceMemory mem;
    VkImageView view;
  } depthStencil;

  // Gamepad state (only one pad supported)
  struct {
    glm::vec2 axisLeft = glm::vec2(0.0f);
    glm::vec2 axisRight = glm::vec2(0.0f);
  } gamePadState;

  // Default ctor
  explicit Application();

  // dtor
  virtual ~Application();

  // Setup the vulkan instance, enable required extensions and connect to the physical device (GPU)
  void initVulkan(Window *window);


  //virtual VkResult createInstance(bool enableValidation, VikWindow *window);

  // Pure virtual render function (override in derived class)
  virtual void render() = 0;
  // Called when view change occurs
  // Can be overriden in derived class to e.g. update uniform buffers
  // Containing view dependant matrices
  virtual void viewChanged();
  // Called if a key is pressed
  /** @brief (Virtual) Called after a key was pressed, can be used to do custom key handling */
  virtual void keyPressed(uint32_t);
  // Called when the window has been resized
  // Can be overriden in derived class to recreate or rebuild resources attached to the frame buffer / swapchain
  //virtual void windowResized();
  // Pure virtual function to be overriden by the dervice class
  // Called in case of an event where e.g. the framebuffer has to be rebuild and thus
  // all command buffers that may reference this
  virtual void buildCommandBuffers();

  // Creates a new (graphics) command pool object storing command buffers
  void createCommandPool();
  // Setup default depth and stencil views
  virtual void setupDepthStencil();
  // Create framebuffers for all requested swap chain images
  // Can be overriden in derived class to setup a custom framebuffer (e.g. for MSAA)
  virtual void setupFrameBuffer();
  // Setup a default render pass
  // Can be overriden in derived class to setup a custom render pass (e.g. for MSAA)
  virtual void setupRenderPass();

  /** @brief (Virtual) Called after the physical device features have been read, can be used to set features to enable on the device */
  virtual void getEnabledFeatures();


  // Check if command buffers are valid (!= VK_NULL_HANDLE)
  bool checkCommandBuffers();
  // Create command buffers for drawing commands
  void createCommandBuffers();
  // Destroy all command buffers and set their handles to VK_NULL_HANDLE
  // May be necessary during runtime if options are toggled
  void destroyCommandBuffers();

  // Command buffer creation
  // Creates and returns a new command buffer
  VkCommandBuffer createCommandBuffer(VkCommandBufferLevel level, bool begin);
  // End the command buffer, submit it to the queue and free (if requested)
  // Note : Waits for the queue to become idle
  void flushCommandBuffer(VkCommandBuffer commandBuffer, VkQueue queue, bool free);

  // Create a cache pool for rendering pipelines
  void createPipelineCache();

  // Prepare commonly used Vulkan functions
  virtual void prepare();

  void check_view_update();

  // Start the main render loop
  void loop(Window *window);

  void updateTextOverlay();

  // Prepare the frame for workload submission
  // - Acquires the next image from the swap chain
  // - Sets the default wait and signal semaphores
  void prepareFrame();

  // Submit the frames' workload
  // - Submits the text overlay (if enabled)
  void submitFrame();

  void update_camera(float frame_time);
  void check_tick_finnished(Window *window);

  void submit_text_overlay();

  void init_physical_device();

  void init_debugging();

  void get_physical_device_properties();

  void init_semaphores();

  void parse_arguments(const int argc, const char *argv[]);

  void list_gpus();
};
}