Skip to content

Commit

Permalink
improving descriptor sets and pools management (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 authored Jan 20, 2024
2 parents 6866f22 + 0536290 commit fcf4d3d
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*.ilk
*.pdb
*.exe
*vgcore
.gdb_history
.vs/
.xmake/
Expand Down
17 changes: 10 additions & 7 deletions example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */
/* Updated: 2024/01/18 15:23:35 by maldavid ### ########.fr */
/* Updated: 2024/01/19 05:34:23 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -28,14 +28,21 @@ int update(void* param)
static int i = 0;
mlx_t* mlx = (mlx_t*)param;

mlx_set_font_scale(mlx->mlx, mlx->win, "default", 6.f);
if(i == 200)
mlx_clear_window(mlx->mlx, mlx->win);

if(i >= 250)
mlx_set_font_scale(mlx->mlx, mlx->win, "default", 16.f);
else
mlx_set_font_scale(mlx->mlx, mlx->win, "default", 6.f);
mlx_string_put(mlx->mlx, mlx->win, 160, 120, 0xFFFF2066, "this text should be hidden");

mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_png, 100, 100);
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_jpg, 210, 150);
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_bmp, 220, 40);
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->img, 150, 60);

mlx_set_font(mlx->mlx, mlx->win, "default");
mlx_string_put(mlx->mlx, mlx->win, 20, 50, 0xFFFFFFFF, "that's a text");

int color = 0;
Expand All @@ -46,11 +53,7 @@ int update(void* param)
color += (color < 255);
}

if(++i == 5000)
mlx_clear_window(mlx->mlx, mlx->win);
if(i == 7000)
mlx_set_font_scale(mlx->mlx, mlx->win, "default", 16.f);

i++;
return 0;
}

Expand Down
19 changes: 15 additions & 4 deletions src/core/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */
/* Updated: 2024/01/18 15:19:58 by maldavid ### ########.fr */
/* Updated: 2024/01/20 08:21:37 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,7 +16,6 @@
#include <SDL2/SDL.h>
#include <renderer/images/texture.h>
#include <renderer/core/render_core.h>
#include <array>
#include <core/errors.h>
#include <mlx_profile.h>
#include <core/memory.h>
Expand All @@ -31,6 +30,10 @@ namespace mlx::core
if(__drop_sdl_responsability) // is case the mlx is running in a sandbox like MacroUnitTester where SDL is already init
return;
SDL_SetMemoryFunctions(MemManager::malloc, MemManager::calloc, MemManager::realloc, MemManager::free);

/* Remove this comment if you want to prioritise Wayland over X11/XWayland, at your own risks */
//SDL_SetHint(SDL_HINT_VIDEODRIVER, "wayland,x11");

if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0)
error::report(e_kind::fatal_error, "SDL error : unable to init all subsystems : %s", SDL_GetError());
}
Expand Down Expand Up @@ -80,9 +83,17 @@ namespace mlx::core
void Application::destroyTexture(void* ptr)
{
MLX_PROFILE_FUNCTION();
vkDeviceWaitIdle(Render_Core::get().getDevice().get()); // TODO : synchronize with another method than stopping all the GPU process
vkDeviceWaitIdle(Render_Core::get().getDevice().get()); // TODO : synchronize with another method than waiting for GPU to be idle
if(ptr == nullptr)
{
core::error::report(e_kind::error, "wrong texture (NULL)");
return;
}
Texture* texture = static_cast<Texture*>(ptr);
texture->destroy();
if(!texture->isInit())
core::error::report(e_kind::error, "trying to destroy a texture");
else
texture->destroy();
}

Application::~Application()
Expand Down
7 changes: 5 additions & 2 deletions src/core/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
/* Updated: 2024/01/18 15:02:06 by maldavid ### ########.fr */
/* Updated: 2024/01/19 05:35:38 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -228,7 +228,10 @@ extern "C"
mlx::core::error::report(e_kind::error, "TTF loader : not a truetype font file '%s'", filepath);
return;
}
static_cast<mlx::core::Application*>(mlx)->loadFont(win, file, 16.f);
if(std::strcmp(filepath, "default") == 0)
static_cast<mlx::core::Application*>(mlx)->loadFont(win, file, 6.f);
else
static_cast<mlx::core::Application*>(mlx)->loadFont(win, file, 16.f);
}

void mlx_set_font_scale(void* mlx, void* win, char* filepath, float scale)
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/core/render_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/17 23:33:34 by maldavid #+# #+# */
/* Updated: 2024/01/07 01:29:31 by maldavid ### ########.fr */
/* Updated: 2024/01/20 08:20:07 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,7 +15,6 @@
#include <mlx_profile.h>
#include <renderer/core/render_core.h>
#include <renderer/command/vk_cmd_buffer.h>
#include <mutex>

#ifdef DEBUG
#ifdef MLX_COMPILER_MSVC
Expand Down Expand Up @@ -138,6 +137,7 @@ namespace mlx

vkDeviceWaitIdle(_device());

_pool_manager.destroyAllPools();
_cmd_manager.destroy();
_allocator.destroy();
_device.destroy();
Expand Down
8 changes: 7 additions & 1 deletion src/renderer/core/render_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:16:32 by maldavid #+# #+# */
/* Updated: 2024/01/11 05:14:03 by maldavid ### ########.fr */
/* Updated: 2024/01/20 08:17:58 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,6 +18,8 @@
#include <optional>

#include <renderer/command/single_time_cmd_manager.h>
#include <renderer/descriptors/descriptor_pool_manager.h>
#include <renderer/descriptors/vk_descriptor_pool.h>
#include "vk_queues.h"
#include "vk_device.h"
#include "vk_instance.h"
Expand Down Expand Up @@ -45,6 +47,8 @@ namespace mlx
const std::vector<const char*> validationLayers = { "VK_LAYER_KHRONOS_validation" };

constexpr const int MAX_FRAMES_IN_FLIGHT = 3;
constexpr const int MAX_SETS_PER_POOL = 512;
constexpr const int NUMBER_OF_UNIFORM_BUFFERS = 1; // change this if for wathever reason more than one uniform buffer is needed

class Render_Core : public Singleton<Render_Core>
{
Expand All @@ -62,6 +66,7 @@ namespace mlx
inline ValidationLayers& getLayers() noexcept { return _layers; }
inline CmdBuffer& getSingleTimeCmdBuffer() noexcept { return _cmd_manager.getCmdBuffer(); }
inline SingleTimeCmdManager& getSingleTimeCmdManager() noexcept { return _cmd_manager; }
inline DescriptorPool& getDescriptorPool() { return _pool_manager.getAvailablePool(); }

private:
Render_Core() = default;
Expand All @@ -71,6 +76,7 @@ namespace mlx
ValidationLayers _layers;
SingleTimeCmdManager _cmd_manager;
Queues _queues;
DescriptorPoolManager _pool_manager;
Device _device;
Instance _instance;
GPUallocator _allocator;
Expand Down
33 changes: 18 additions & 15 deletions src/renderer/core/vk_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
/* By: maldavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:14:29 by maldavid #+# #+# */
/* Updated: 2024/01/10 21:54:17 by maldavid ### ########.fr */
/* Updated: 2024/01/20 05:34:15 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

#include "render_core.h"
#include <iterator>
#include <map>
#include <vector>
#include <set>
#include <SDL2/SDL.h>
Expand Down Expand Up @@ -84,24 +85,19 @@ namespace mlx
if(SDL_Vulkan_CreateSurface(window, Render_Core::get().getInstance().get(), &surface) != SDL_TRUE)
core::error::report(e_kind::fatal_error, "Vulkan : failed to create a surface to pick physical device");

std::vector<std::pair<int, VkPhysicalDevice>> devices_score;
std::multimap<int, VkPhysicalDevice> devices_score;

std::transform(devices.cbegin(), devices.cend(), std::back_inserter(devices_score), [&](VkPhysicalDevice device)
for(const auto& device : devices)
{
return std::make_pair(deviceScore(device, surface), device);
});

using device_pair = std::pair<int, VkPhysicalDevice>;
std::sort(devices_score.begin(), devices_score.end(), [](const device_pair& a, const device_pair& b)
{
return a.first > b.first;
});

if(devices_score.front().first > 0)
_physicalDevice = devices_score.front().second;
int score = deviceScore(device, surface);
devices_score.insert(std::make_pair(score, device));
}

if(_physicalDevice == VK_NULL_HANDLE)
if(devices_score.rbegin()->first > 0)
_physicalDevice = devices_score.rbegin()->second;
else
core::error::report(e_kind::fatal_error, "Vulkan : failed to find a suitable GPU");

#ifdef DEBUG
VkPhysicalDeviceProperties props;
vkGetPhysicalDeviceProperties(_physicalDevice, &props);
Expand All @@ -126,6 +122,9 @@ namespace mlx
if(!indices.isComplete() || !extensionsSupported || formatCount == 0)
return -1;

VkPhysicalDeviceFeatures features;
vkGetPhysicalDeviceFeatures(device, &features);

int score = 0;
#ifndef FORCE_INTEGRATED_GPU
if(props.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
Expand All @@ -134,6 +133,10 @@ namespace mlx
if(props.deviceType != VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU)
return -1;
#endif

if(!features.geometryShader)
return -1;

score += props.limits.maxImageDimension2D;
score += props.limits.maxBoundDescriptorSets;
return score;
Expand Down
39 changes: 39 additions & 0 deletions src/renderer/descriptors/descriptor_pool_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* descriptor_pool_manager.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 06:51:47 by maldavid #+# #+# */
/* Updated: 2024/01/20 08:18:27 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

#include <renderer/core/render_core.h>
#include <renderer/descriptors/descriptor_pool_manager.h>

namespace mlx
{
DescriptorPool& DescriptorPoolManager::getAvailablePool()
{
for(auto& pool : _pools)
{
if(pool.getNumberOfSetsAllocated() < MAX_SETS_PER_POOL)
return pool;
}
VkDescriptorPoolSize pool_sizes[] = {
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, (MAX_FRAMES_IN_FLIGHT * NUMBER_OF_UNIFORM_BUFFERS) },
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, MAX_SETS_PER_POOL - (MAX_FRAMES_IN_FLIGHT * NUMBER_OF_UNIFORM_BUFFERS) }
};
_pools.emplace_front().init((sizeof(pool_sizes) / sizeof(VkDescriptorPoolSize)), pool_sizes);
return _pools.front();
}

void DescriptorPoolManager::destroyAllPools()
{
for(auto& pool : _pools)
pool.destroy();
_pools.clear();
}
}
36 changes: 36 additions & 0 deletions src/renderer/descriptors/descriptor_pool_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* descriptor_pool_manager.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 06:26:26 by maldavid #+# #+# */
/* Updated: 2024/01/20 08:23:04 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef __MLX_DESCRIPTOR_POOL_MANAGER__
#define __MLX_DESCRIPTOR_POOL_MANAGER__

#include <renderer/descriptors/vk_descriptor_pool.h>
#include <list>

namespace mlx
{
class DescriptorPoolManager
{
public:
DescriptorPoolManager() = default;

DescriptorPool& getAvailablePool(); // assumes the pool is for only one set allocation, may cause some issues if this is for more than one
void destroyAllPools();

~DescriptorPoolManager() = default;

private:
std::list<DescriptorPool> _pools;
};
}

#endif
26 changes: 22 additions & 4 deletions src/renderer/descriptors/vk_descriptor_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
/* By: maldavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/23 18:34:23 by maldavid #+# #+# */
/* Updated: 2024/01/18 10:19:55 by maldavid ### ########.fr */
/* Updated: 2024/01/20 07:40:40 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

#include "vk_descriptor_pool.h"
#include <renderer/descriptors/vk_descriptor_set.h>
#include <renderer/core/render_core.h>

namespace mlx
Expand All @@ -21,17 +22,34 @@ namespace mlx
poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
poolInfo.poolSizeCount = n;
poolInfo.pPoolSizes = size;
poolInfo.maxSets = 8192;
poolInfo.maxSets = MAX_SETS_PER_POOL;
poolInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;

VkResult res = vkCreateDescriptorPool(Render_Core::get().getDevice().get(), &poolInfo, nullptr, &_pool);
if(res != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan : failed to create descriptor pool, %s", RCore::verbaliseResultVk(res));
_allocated_sets++;
#ifdef DEBUG
core::error::report(e_kind::message, "Vulkan : created new descriptor pool");
#endif
}

void DescriptorPool::destroy() noexcept
void DescriptorPool::freeDescriptor(const DescriptorSet& set)
{
vkDestroyDescriptorPool(Render_Core::get().getDevice().get(), _pool, nullptr);
if(!isInit())
return;
const auto& sets = set.getAllFramesDescriptorSets();
vkFreeDescriptorSets(Render_Core::get().getDevice().get(), _pool, sets.size(), sets.data());
_allocated_sets--; // if this goes in underflow I quit
}

void DescriptorPool::destroy() noexcept
{
if(_pool != VK_NULL_HANDLE)
vkDestroyDescriptorPool(Render_Core::get().getDevice().get(), _pool, nullptr);
_pool = VK_NULL_HANDLE;
#ifdef DEBUG
core::error::report(e_kind::message, "Vulkan : destroyed a descriptor pool");
#endif
}
}
Loading

0 comments on commit fcf4d3d

Please sign in to comment.