vulkan-project/renderer2D.hpp
2022-10-14 20:58:20 +02:00

133 lines
4.7 KiB
C++

#pragma once
#include "renderer.hpp"
#include "shape.hpp"
#include "vulkan_util.hpp"
namespace gz::vk {
class Renderer2D : public Renderer {
public:
/**
* @brief Create a 2D renderer
* @details
* -# @ref VulkanInstance::registerCleanupCallback "register" @ref cleanup() "cleanup callback"
* -# @ref VulkanInstance::registerSwapChainRecreateCallback "register" @ref swapChainRecreateCallback "swapChain recreation callback"
* -# create command buffers
* -# create vertex & index buffers
* -# call initSwapChainDependantResources
*/
Renderer2D(VulkanInstance& instance, TextureManager& textureManager);
/**
* @name Rendering
*/
/// @{
void drawShape(Shape* shape);
/**
* @brief Copies the vertices from shapes into the vertex buffer, using a staging buffer
*/
void fillVertexBufferWithShapes();
void fillIndexBufferWithShapes();
void drawFrame(uint32_t imageIndex);
/// @}
private:
/**
* @brief Destroy all vulkan objects owned by this object
* @details:
* Does:
* -# call cleanupSwapChainDependantResources()
* -# call Renderer::cleanup_()
*/
void cleanup();
std::vector<Shape> shapes;
size_t shapesVerticesCount = 0;
size_t shapesIndicesCount = 0;
/**
* @brief Render everything
* @details
* As of now, this does (in the same command buffer from graphicsPool)
* -# begin render pass
* - image layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
* - clear image
* -# bind 2d pipeline, vertex and index buffer
* -# bind texture sampler
* -# draw indexed: draw the shapes from shapes vector
* -# end render pass
* - image layout: VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
* -# copy image to swapChain image with same imageIndex
*/
void recordCommandBuffer(uint32_t imageIndex, uint32_t currentFrame);
// IMAGE
/**
* @name Image (render targets)
* @details
* The images are used as render targets. After rendering, the current image gets blitted onto the current swap chain image.
* @{
*/
std::vector<VkImage> images;
std::vector<VkDeviceMemory> imageMemory;
std::vector<VkImageView> imageViews;
/**
* @brief Creates the images (on imageMemory) and imageViews with the format of the VulkanInstance::swapChain images
*/
void createImages();
/// @}
/**
* @name Render pass
* @details
* Attachments:
* - color blend:
* - loadOp = VK_ATTACHMENT_LOAD_OP_LOAD (not clear!)
* - storeOp = VK_ATTACHMENT_STORE_OP_STORE
* - initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
* - finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
* - stencil load/store = dont care
*/
/// @{
/**
* @brief Create a render pass
*/
void createRenderPass();
VkRenderPass renderPass;
/// @}
std::vector<VkFramebuffer> framebuffers;
// PIPELINE
PipelineContainer pipelines;
// SWAPCHAIN RECREATION
/**
* @brief Swapchain recreation
*/
/// @{
/**
* @brief Cleans up resources that were initialized by initSwapChainDependantResources
*/
void cleanupSwapChainDependantResources();
/**
* @brief Sets up resources that depend on the swap chain or its attributes
* @details
* Initializes up:
* - render pass
* - images, imageMemory, imageViews
* - framebuffers
* - pipeline
*/
void initSwapChainDependantResources();
/**
* @brief Recreates swap chain dependant resources
* @details
* Calls:
* -# cleanupSwapChainDependantResources
* -# initSwapChainDependantResources
*/
void swapChainRecreateCallback();
/// @}
Log rLog;
};
} // namespace gz::vk