inexorgame / vulkan-renderer

A new 3D game engine for Linux and Windows using C++20 and Vulkan API 1.3, in very early but ongoing development
https://inexor.org
MIT License
757 stars 33 forks source link

gpu_info.cpp doesn't print any info #548

Closed IAmNotHanni closed 1 year ago

IAmNotHanni commented 1 year ago

Describe The Bug

The gpu_info file contains several functions which could be useful if there's an issue with someone using our code. It can print the available gpus and all kind of data associated with them. The problem is that it doesn't print anything anymore. The reason for this is that during my refactoring, I used the reserve method instead of the resize method (or calling the std::vector with a number of elements in its constructor, which essentially constructs the elements, being equivalent to .resize()).

So this code is wrong:

void print_all_physical_devices(const VkInstance instance, const VkSurfaceKHR surface) {
    assert(instance);
    assert(surface);

    std::uint32_t gpu_count{0};

    // Query how many graphics cards are available.
    if (const auto result = vkEnumeratePhysicalDevices(instance, &gpu_count, nullptr); result != VK_SUCCESS) {
        spdlog::error("Error: vkEnumeratePhysicalDevices returned {}!", vk_tools::as_string(result));
        return;
    }

    spdlog::trace("Number of available gpus: {}", gpu_count);

    if (gpu_count == 0) {
        return;
    }

    std::vector<VkPhysicalDevice> available_gpus;
    available_gpus.reserve(gpu_count); // THIS IS WRONG!!!

    // Store all available graphics cards.
    if (const auto result = vkEnumeratePhysicalDevices(instance, &gpu_count, available_gpus.data());
        result != VK_SUCCESS) {
        spdlog::error("Error: vkEnumeratePhysicalDevices returned {}!", vk_tools::as_string(result));
        return;
    }
   /// more going on below from here...
}

It should really be std::vector<VkPhysicalDevice> available_gpus(gpu_count); or change the .reserve to .resize.

Since this is a very common mistake always remember: reserve doesn't create any objects and doesn't call any constructors!

Steps To Reproduce

Just run the current main branch and add a debug break in print_all_physical_devices.

Expected Behavior

It should print all the available info

Affected Code

gpu_info.cpp

Operating System

All

Application Version

4f7b8377e5ed91e85db9f040527f33127d247c31

Additional Context

None

Relevant Log Output

None
IAmNotHanni commented 1 year ago

This seems to be an issue in other functions in gpu_info.cpp as well, so I will need to fix all of them.

IAmNotHanni commented 1 year ago

As already stated in the pr, we will delete this entirely rather than to fix it.