Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

I’m building a video game engine using C++ as the coding language and Vulkan for…

  1. 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 a portion of the code:

    Engine.h:

    #pragma once

    #include "Window.h" #include "Renderer.h" #include "Scene.h"

    class Engine { public: Engine(); ~Engine();

    void Run();
    void Shutdown();
    

    private:

    void Initialize();
    void MainLoop();
    void Update(float deltaTime);
    void Render();
    
    Window window;
    Renderer renderer;
    Scene scene;
    

    };

    Engine.cpp:

    #include "Engine.h" #include "Terrain.h" #include

    Engine::Engine() { Initialize(); }

    Engine::~Engine() { Shutdown(); }

    void Engine::Run() { MainLoop(); }

    void Engine::Initialize() { // Initialize window, renderer, and scene window.Initialize(); renderer.Initialize(window.GetWindow()); scene.Initialize();

    VkDescriptorSetLayout descriptorSetLayout = renderer.CreateDescriptorSetLayout();
    VkDescriptorPool descriptorPool = renderer.CreateDescriptorPool(1); // Assuming only one terrain object
    
    Terrain terrain(0,10,1,renderer.GetDevice(), renderer.GetPhysicalDevice(), renderer.GetCommandPool(), renderer.GetGraphicsQueue());
    terrain.GenerateTerrain(descriptorSetLayout, descriptorPool);
    
    scene.AddGameObject(terrain.GetTerrainObject());
    float deltaTime = window.GetDeltaTime();
    

    }

    void Engine::MainLoop() { while (!window.ShouldClose()) { window.PollEvents();

    	float deltaTime = window.GetDeltaTime();
    	Update(deltaTime);
    	Render();
    }
    

    }

    void Engine::Update(float deltaTime) { scene.Update(deltaTime); }

    void Engine::Render() { renderer.BeginFrame(); scene.Render(renderer); renderer.EndFrame(); }

    void Engine::Shutdown() { // Clean up resources in reverse order scene.Shutdown(); renderer.Shutdown(); window.Shutdown(); }

    Can you please check the code for any potential memory leaks? If you find any, please tell me the changes required to fix them.

  2. Assistant

    It seems that most of the memory management is handled correctly in the provided code. However, there are no comments or documentation for the included libraries and some of the Vulkan helper functions. I will point out a potential memory leak with the descriptor set and pool creation, and recommend some changes to clean up.

    Potential memory leak:

    In the Initialize() function of your Engine class, you create a descriptor set layout and a descriptor pool. However, in the Shutdown() function, you don't destroy them. To fix the memory leaks and ensure proper cleanup, you should store the generated VkDescriptorSetLayout and VkDescriptorPool as class members in the Engine class and destroy them in the Engine::Shutdown() function.

    Changes required:

    1. In your Engine.h file, add the following private member variables:
    	VkDescriptorSetLayout descriptorSetLayout;
    	VkDescriptorPool descriptorPool;
    
    1. In your Engine.cpp file, modify the Initialize() method to remove the local variable declarations for descriptorSetLayout and descriptorPool, and assign values directly to the class member variables:
    void Engine::Initialize()
    {
    	// (Your existing code here ...)
    	descriptorSetLayout = renderer.CreateDescriptorSetLayout();
    	descriptorPool = renderer.CreateDescriptorPool(1);
    
    	// (The rest of the code ...)
    }
    
    1. In your Engine.cpp file, add the code to destroy the descriptor set layout and pool in the Shutdown() method:
    void Engine::Shutdown()
    {
    	// Clean up resources in reverse order
    	scene.Shutdown();
    	renderer.Shutdown();
    
    	// Destroy descriptor set layout and pool
    	vkDestroyDescriptorPool(renderer.GetDevice(), descriptorPool, nullptr);
    	vkDestroyDescriptorSetLayout(renderer.GetDevice(), descriptorSetLayout, nullptr);
    
    	window.Shutdown();
    }
    

    With these changes, your Engine class should properly manage the memory for descriptor set layout and descriptor pool.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.