#pragma once #include "renderer.hpp" #include "vulkan_util.hpp" namespace gz::vk { struct UniformBufferObject { alignas(16) glm::mat4 model; alignas(16) glm::mat4 view; alignas(16) glm::mat4 projection; }; class Renderer3D : public Renderer { public: /** * @brief Create a 3D 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 createUniformBuffers() * -# call createDescriptorResources() * -# call initSwapChainDependantResources() */ Renderer3D(VulkanInstance& instance, TextureManager& textureManager); /** * @name Rendering */ /// @{ void drawFrame(uint32_t imageIndex); /// @} private: void recordCommandBuffer(uint32_t imageIndex, uint32_t currentFrame); std::vector uniformBuffers; std::vector uniformBuffersMemory; void createUniformBuffers(); void updateUniformBuffer(); /** * @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 images; std::vector imageMemory; std::vector 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_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 framebuffers; // PIPELINE PipelineContainer pipelines; /** * @name Desciptors * @details These functions create a desciptor with bindings for * -# UniformBufferObject (DESCRIPTOR_TYPE_UNIFORM_BUFFER) * -# Combined Image Sampler (DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) */ /// @{ VkDescriptorPool descriptorPool; VkDescriptorSetLayout descriptorSetLayout; std::vector descriptorSets; /** * @brief Create a descriptor layout binding for the MVP uniform buffer object * @details Create a desciptor set layout with bindings for * -# UniformBufferObject (DESCRIPTOR_TYPE_UNIFORM_BUFFER) * -# Combined Image Sampler (DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) */ void createDescriptorPool(); /* * @bried Create one descriptor set with layout descriptorSetLayout for each frame */ void createDescriptorSets(); const uint32_t bindingUniformBuffer = 0; const uint32_t bindingCombinedImageSampler = 1; /* const uint32_t bindingCombinedImageSampler = 1; */ void createDescriptorResources(); /** * @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: * - images, imageMemory, imageViews * - framebuffers * - render pass * - pipeline */ void initSwapChainDependantResources(); /** * @brief Recreates swap chain dependant resources * @details * Calls: * -# cleanupSwapChainDependantResources() * -# initSwapChainDependantResources() */ void swapChainRecreateCallback(); /// @} /* * @brief Destroy all vulkan objects owned by this object * @details: * Does: * -# destroy uniform buffers * -# call cleanupSwapChainDependantResources() * -# call Renderer::cleanup_() */ void cleanup(); void loadModel(); VerticesAndIndices model; Log rLog; }; }