-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improving descriptor sets and pools management (#41)
- Loading branch information
Showing
17 changed files
with
186 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
*.ilk | ||
*.pdb | ||
*.exe | ||
*vgcore | ||
.gdb_history | ||
.vs/ | ||
.xmake/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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; | ||
|
@@ -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; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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> | ||
|
@@ -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()); | ||
} | ||
|
@@ -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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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 | ||
|
@@ -138,6 +137,7 @@ namespace mlx | |
|
||
vkDeviceWaitIdle(_device()); | ||
|
||
_pool_manager.destroyAllPools(); | ||
_cmd_manager.destroy(); | ||
_allocator.destroy(); | ||
_device.destroy(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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" | ||
|
@@ -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> | ||
{ | ||
|
@@ -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; | ||
|
@@ -71,6 +76,7 @@ namespace mlx | |
ValidationLayers _layers; | ||
SingleTimeCmdManager _cmd_manager; | ||
Queues _queues; | ||
DescriptorPoolManager _pool_manager; | ||
Device _device; | ||
Instance _instance; | ||
GPUallocator _allocator; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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); | ||
|
@@ -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) | ||
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
} | ||
} |
Oops, something went wrong.