Base class cleanup and restructuring

This commit is contained in:
Sascha Willems 2020-04-19 10:05:47 +02:00
parent e9b9332d04
commit cbe751d26e
2 changed files with 33 additions and 71 deletions

View file

@ -90,7 +90,7 @@ VkResult VulkanExampleBase::createInstance(bool enableValidation)
return vkCreateInstance(&instanceCreateInfo, nullptr, &instance); return vkCreateInstance(&instanceCreateInfo, nullptr, &instance);
} }
void VulkanExampleBase::drawFrame() void VulkanExampleBase::renderFrame()
{ {
VulkanExampleBase::prepareFrame(); VulkanExampleBase::prepareFrame();
submitInfo.commandBufferCount = 1; submitInfo.commandBufferCount = 1;
@ -236,7 +236,7 @@ VkPipelineShaderStageCreateInfo VulkanExampleBase::loadShader(std::string fileNa
return shaderStage; return shaderStage;
} }
void VulkanExampleBase::renderFrame() void VulkanExampleBase::nextFrame()
{ {
auto tStart = std::chrono::high_resolution_clock::now(); auto tStart = std::chrono::high_resolution_clock::now();
if (viewUpdated) if (viewUpdated)
@ -308,7 +308,7 @@ void VulkanExampleBase::renderLoop()
} }
} }
if (prepared && !IsIconic(window)) { if (prepared && !IsIconic(window)) {
renderFrame(); nextFrame();
} }
} }
#elif defined(VK_USE_PLATFORM_ANDROID_KHR) #elif defined(VK_USE_PLATFORM_ANDROID_KHR)

View file

@ -38,7 +38,6 @@
#define GLM_ENABLE_EXPERIMENTAL #define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <string> #include <string>
#include <array>
#include <numeric> #include <numeric>
#include "vulkan/vulkan.h" #include "vulkan/vulkan.h"
@ -57,17 +56,20 @@
class VulkanExampleBase class VulkanExampleBase
{ {
private: private:
// Get window title with example name, device, et.
std::string getWindowTitle(); std::string getWindowTitle();
/** brief Indicates that the view (position, rotation) has changed and buffers containing camera matrices need to be updated */
bool viewUpdated = false; bool viewUpdated = false;
// Destination dimensions for resizing the window
uint32_t destWidth; uint32_t destWidth;
uint32_t destHeight; uint32_t destHeight;
bool resizing = false; bool resizing = false;
// Called if the window is resized and some resources have to be recreatesd
void windowResize(); void windowResize();
void handleMouseMove(int32_t x, int32_t y); void handleMouseMove(int32_t x, int32_t y);
void nextFrame();
void updateOverlay();
void createPipelineCache();
void createCommandPool();
void createSynchronizationPrimitives();
void initSwapchain();
void setupSwapChain();
protected: protected:
// Frame counter to display fps // Frame counter to display fps
uint32_t frameCounter = 0; uint32_t frameCounter = 0;
@ -241,13 +243,9 @@ public:
xcb_intern_atom_reply_t *atom_wm_delete_window; xcb_intern_atom_reply_t *atom_wm_delete_window;
#endif #endif
// Default ctor
VulkanExampleBase(bool enableValidation = false); VulkanExampleBase(bool enableValidation = false);
// dtor
virtual ~VulkanExampleBase(); virtual ~VulkanExampleBase();
/** @brief Setup the vulkan instance, enable required extensions and connect to the physical device (GPU) */
// Setup the vulkan instance, enable required extensions and connect to the physical device (GPU)
bool initVulkan(); bool initVulkan();
#if defined(_WIN32) #if defined(_WIN32)
@ -310,95 +308,59 @@ public:
void initxcbConnection(); void initxcbConnection();
void handleEvent(const xcb_generic_event_t *event); void handleEvent(const xcb_generic_event_t *event);
#endif #endif
/** /** @brief (Virtual) Creates the application wide Vulkan instance */
* Create the application wide Vulkan instance
*
* @note Virtual, can be overriden by derived example class for custom instance creation
*/
virtual VkResult createInstance(bool enableValidation); virtual VkResult createInstance(bool enableValidation);
/** @brief (Pure virtual) Render function to be implemented by the sample application */
// Pure virtual render function (override in derived class)
virtual void render() = 0; virtual void render() = 0;
/** @brief (Virtual) Default image acquire and command buffer submission function */ /** @brief (Virtual) Called when the camera view has changed */
virtual void drawFrame();
// Called when view change occurs
// Can be overriden in derived class to e.g. update uniform buffers
// Containing view dependant matrices
virtual void viewChanged(); virtual void viewChanged();
/** @brief (Virtual) Called after a key was pressed, can be used to do custom key handling */ /** @brief (Virtual) Called after a key was pressed, can be used to do custom key handling */
virtual void keyPressed(uint32_t); virtual void keyPressed(uint32_t);
/** @brief (Virtual) Called after th mouse cursor moved and before internal events (like camera rotation) is handled */ /** @brief (Virtual) Called after the mouse cursor moved and before internal events (like camera rotation) is handled */
virtual void mouseMoved(double x, double y, bool &handled); virtual void mouseMoved(double x, double y, bool &handled);
// Called when the window has been resized /** @brief (Virtual) Called when the window has been resized, can be used by the sample application to recreate resources */
// Can be overriden in derived class to recreate or rebuild resources attached to the frame buffer / swapchain
virtual void windowResized(); virtual void windowResized();
// Pure virtual function to be overriden by the dervice class /** @brief (Virtual) Called when resources have been recreated that require a rebuild of the command buffers (e.g. frame buffer), to be implemente by the sample application */
// 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(); virtual void buildCommandBuffers();
/** @brief (Virtual) Setup default depth and stencil views */
void createSynchronizationPrimitives();
// Creates a new (graphics) command pool object storing command buffers
void createCommandPool();
// Setup default depth and stencil views
virtual void setupDepthStencil(); virtual void setupDepthStencil();
// Create framebuffers for all requested swap chain images /** @brief (Virtual) Setup default framebuffers for all requested swapchain images */
// Can be overriden in derived class to setup a custom framebuffer (e.g. for MSAA)
virtual void setupFrameBuffer(); virtual void setupFrameBuffer();
// Setup a default render pass /** @brief (Virtual) Setup a default renderpass */
// Can be overriden in derived class to setup a custom render pass (e.g. for MSAA)
virtual void setupRenderPass(); 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 */ /** @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(); virtual void getEnabledFeatures();
// Connect and prepare the swap chain /** @brief Checks if command buffers are valid (!= VK_NULL_HANDLE) */
void initSwapchain();
// Create swap chain images
void setupSwapChain();
// Check if command buffers are valid (!= VK_NULL_HANDLE)
bool checkCommandBuffers(); bool checkCommandBuffers();
// Create command buffers for drawing commands /** @brief Creates the per-frame command buffers */
void createCommandBuffers(); void createCommandBuffers();
// Destroy all command buffers and set their handles to VK_NULL_HANDLE /** @brief Destroy all command buffers and set their handles to VK_NULL_HANDLE */
// May be necessary during runtime if options are toggled
void destroyCommandBuffers(); void destroyCommandBuffers();
// Command buffer creation /** @brief Creates and returns a new command buffer */
// Creates and returns a new command buffer
VkCommandBuffer createCommandBuffer(VkCommandBufferLevel level, bool begin); VkCommandBuffer createCommandBuffer(VkCommandBufferLevel level, bool begin);
// End the command buffer, submit it to the queue and free (if requested) /** @brief 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); void flushCommandBuffer(VkCommandBuffer commandBuffer, VkQueue queue, bool free);
// Create a cache pool for rendering pipelines /** @brief Prepares all Vulkan resources and functions required to run the sample */
void createPipelineCache();
// Prepare commonly used Vulkan functions
virtual void prepare(); virtual void prepare();
// Load a SPIR-V shader /** @brief Loads a SPIR-V shader file for the given shader stage */
VkPipelineShaderStageCreateInfo loadShader(std::string fileName, VkShaderStageFlagBits stage); VkPipelineShaderStageCreateInfo loadShader(std::string fileName, VkShaderStageFlagBits stage);
// Start the main render loop /** @brief Entry point for the main render loop */
void renderLoop(); void renderLoop();
// Render one frame of a render loop on platforms that sync rendering /** @brief Adds the drawing commands for the ImGui overlay to the given command buffer */
void renderFrame();
void updateOverlay();
void drawUI(const VkCommandBuffer commandBuffer); void drawUI(const VkCommandBuffer commandBuffer);
// Prepare the frame for workload submission /** Prepare the next frame for workload sumbission by acquiring the next swap chain image */
// - Acquires the next image from the swap chain
// - Sets the default wait and signal semaphores
void prepareFrame(); void prepareFrame();
/** @brief Presents the current image to the swap chain */
// Submit the frames' workload
void submitFrame(); void submitFrame();
/** @brief (Virtual) Default image acquire + submission and command buffer submission function */
virtual void renderFrame();
/** @brief (Virtual) Called when the UI overlay is updating, can be used to add custom elements to the overlay */ /** @brief (Virtual) Called when the UI overlay is updating, can be used to add custom elements to the overlay */
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay); virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay);