I’m building a video game engine using C++ as the coding language and Vulkan for…
- User
I’m building a video game engine using C++ as the coding language and Vulkan for graphics. I am trying to set up a generic renderer using Vulkan that is flexible and will render objects based on a vector that is supplied to it. The renderer will also handle the creation of the window using GLFW and use GLM for all relevant math calls. I am using the ASSIMP library to load 3d models and animations.
Here is some the code:
Renderer.h:
#pragma once
#include <vulkan/vulkan.hpp> #include "Window.h" #include #include #include #include #include #include "Pipeline.h" #include "Material.h" #include "Mesh.h"
struct QueueFamilyIndices { std::optional<uint32_t> graphicsFamily; std::optional<uint32_t> presentFamily;
bool IsComplete() { return graphicsFamily.has_value() && presentFamily.has_value(); }};
struct SwapChainSupportDetails { vk::SurfaceCapabilitiesKHR capabilities; std::vectorvk::SurfaceFormatKHR formats; std::vectorvk::PresentModeKHR presentModes; };
struct MVP { glm::mat4 model; glm::mat4 view; glm::mat4 projection; };
class Renderer { public: Renderer(); ~Renderer();
void Initialize(GLFWwindow* window); void Shutdown(); void BeginFrame(); void EndFrame(); vk::DescriptorSetLayout CreateDescriptorSetLayout(); vk::UniqueDescriptorPool CreateDescriptorPool(uint32_t maxSets); vk::Device* GetDevice(); vk::PhysicalDevice* GetPhysicalDevice(); vk::CommandPool* GetCommandPool(); vk::Queue* GetGraphicsQueue(); vk::CommandBuffer* GetCurrentCommandBuffer(); std::shared_ptr<Pipeline> GetPipeline(); void CreateGraphicsPipeline(Mesh* mesh, Material* material); std::pair<vk::Buffer, vk::DeviceMemory> RequestMvpBuffer(); void RecreateSwapchain(int width, int height);private:
bool isShutDown = false; static const uint32_t kMvpBufferCount = 3; std::vector<vk::Buffer> mvpBuffers; std::vector<vk::DeviceMemory> mvpBufferMemory; uint32_t currentMvpBufferIndex = 0; bool shutdownInProgress; uint32_t currentCmdBufferIndex = 0; std::vector<size_t> currentFramePerImage; std::vector<vk::Image> swapChainImages; std::vector<vk::ImageView> swapChainImageViews; vk::Extent2D swapChainExtent; vk::RenderPass renderPass; uint32_t imageIndex; std::shared_ptr<Pipeline> pipeline; vk::Format swapChainImageFormat; std::vector<vk::CommandBuffer> commandBuffers; void CreateImageViews(); void CleanupImageViews(); void CreateRenderPass(); void CleanupRenderPass(); void CreateSurface(); void DestroySurface(); void CreateInstance(); void CleanupInstance(); void ChoosePhysicalDevice(); void CreateDevice(); void CleanupDevice(); void CreateSwapchain(); void CleanupSwapchain(); void CreateCommandPool(); void CleanupCommandPool(); void CreateFramebuffers(); void CleanupFramebuffers(); void CreateCommandBuffers(); void CleanupCommandBuffers(); void Present(); GLFWwindow* window; vk::Instance instance; vk::PhysicalDevice physicalDevice; vk::Device device; vk::SurfaceKHR surface; vk::SwapchainKHR swapchain; vk::CommandPool commandPool; vk::CommandBuffer currentCommandBuffer; std::vector<vk::Framebuffer> framebuffers; // Additional Vulkan objects needed for rendering… const uint32_t kMaxFramesInFlight = 2; std::vector<vk::Semaphore> imageAvailableSemaphores; std::vector<vk::Semaphore> renderFinishedSemaphores; std::vector<vk::Fence> inFlightFences; size_t currentFrame; vk::Queue graphicsQueue; vk::Queue presentQueue; void CreateSyncObjects(); void CleanupSyncObjects(); SwapChainSupportDetails querySwapChainSupport(vk::PhysicalDevice device, vk::SurfaceKHR surface); vk::SurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<vk::SurfaceFormatKHR>& availableFormats); vk::PresentModeKHR chooseSwapPresentMode(const std::vector<vk::PresentModeKHR>& availablePresentModes); vk::Extent2D chooseSwapExtent(const vk::SurfaceCapabilitiesKHR& capabilities, GLFWwindow* window); std::vector<const char*> deviceExtensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME }; std::vector<const char*> CheckPhysicalDeviceExtensionSupport(vk::PhysicalDevice physicalDevice); QueueFamilyIndices GetQueueFamilyIndices(vk::PhysicalDevice physicalDevice); vk::Image depthImage; vk::DeviceMemory depthImageMemory; vk::ImageView depthImageView; void CreateDepthResources(); void CleanupDepthResources(); vk::Format FindDepthFormat(vk::PhysicalDevice physicalDevice); vk::Format FindSupportedFormat(vk::PhysicalDevice physicalDevice, const std::vector<vk::Format>& candidates, vk::ImageTiling tiling, vk::FormatFeatureFlags features); uint32_t FindMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties); vk::ImageView CreateImageView(vk::Image image, vk::Format format, vk::ImageAspectFlags aspectFlags); - Assistant
Based on the current code structure, it appears that the functions are organized in a logical manner.
The
Rendererclass handles the overall rendering pipeline and all related Vulkan objects, including the creation of the window, initialization of Vulkan, and management of command buffers, framebuffers, and synchronization objects.The
Textureclass specifically handles loading and managing image resources, including creating the image, transitioning its layout, copying data to it, creating image views, samplers, and managing memory.The
Materialclass handles the creation of the pipeline layout, shaders, and descriptor sets. It also handles loading and managing textures for the material.Overall, the functions seem to be appropriately placed in their respective classes based on their responsibilities and functionality. However, without more context about the overall design and requirements of your game engine, it's difficult to say definitively. It's always a good idea to regularly review and refactor your code as needed to ensure clarity, maintainability, and adherence to the principle of single responsibility.
- User
I am wanting to implement multisampling antialiasing. I blieve this functionality should sit in the Renderer class. However, it requires images to be created and passed to the buffers. Should I move the image creation function from the Texture class to the Renderer class or should I just have some isolated image creation code like I currently do for Depth resources? What would make more sense?
- Assistant
Implementing multisampling anti-aliasing (MSAA) in the Renderer class would be a logical choice. Since MSAA involves creating and managing images for anti-aliasing purposes, it makes sense to have the image creation function in the Renderer class.
You have a couple of options on how to proceed with image creation in the Renderer class:
-
Move the existing image creation functions from the Texture class to the Renderer class: This approach allows you to consolidate all the image creation code in one place, which can simplify maintenance and improve code organization. You can modify the
CreateImageand related functions in the Renderer class to handle both regular textures and MSAA images. -
Keep the image creation functions in the Texture class and create a separate image creation function in the Renderer class: This option allows you to maintain the separation of concerns between the Texture and Renderer classes. You can create a new function in the Renderer class specifically for creating MSAA images and invoke it as needed.
Both options have their pros and cons, and the best choice depends on your specific requirements and design preferences. Consider factors such as code organization, modularity, and potential future scalability when making your decision.
-