fixing performance issues

This commit is contained in:
2023-08-02 12:50:50 +02:00
parent c497645ee9
commit a70bec4740
36 changed files with 629 additions and 431 deletions

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */
/* Updated: 2023/04/12 11:03:57 by maldavid ### ########.fr */
/* Updated: 2023/04/25 15:12:57 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -36,36 +36,20 @@ namespace mlx::core
void* Application::newTexture(int w, int h)
{
std::shared_ptr<Texture> texture = std::make_shared<Texture>();
texture->create(nullptr, w, h, VK_FORMAT_R8G8B8A8_UNORM);
TextureID id = _texture_lib.addTextureToLibrary(texture);
_texture_ids.push_back(id);
return &_texture_ids.back();
_textures.emplace_front().create(nullptr, w, h, VK_FORMAT_R8G8B8A8_UNORM);
return &_textures.front();
}
void* Application::newStbTexture(char* file, int* w, int* h)
{
std::shared_ptr<Texture> texture = std::make_shared<Texture>(stbTextureLoad(file, w, h));
TextureID id = _texture_lib.addTextureToLibrary(texture);
_texture_ids.push_back(id);
return &_texture_ids.back();
}
char* Application::mapTexture(void* img, int* bits_per_pixel, int* size_line, int* endian)
{
TextureID id = *static_cast<TextureID*>(img);
std::shared_ptr<Texture> texture = _texture_lib.getTexture(id);
char* map = static_cast<char*>(texture->openCPUmap());
*bits_per_pixel = sizeof(uint32_t) * 8;
*size_line = texture->getWidth();
*endian = isSystemBigEndian();
return map;
_textures.emplace_front(stbTextureLoad(file, w, h));
return &_textures.front();
}
void Application::destroyTexture(void* ptr)
{
vkDeviceWaitIdle(Render_Core::get().getDevice().get());
TextureID id = *static_cast<TextureID*>(ptr);
_texture_lib.removeTextureFromLibrary(id);
Texture* texture = static_cast<Texture*>(ptr);
texture->destroy();
}
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */
/* Updated: 2023/04/13 10:56:19 by maldavid ### ########.fr */
/* Updated: 2023/04/25 15:23:31 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -25,8 +25,6 @@
#include <core/graphics.h>
#include <platform/inputs.h>
#include <renderer/texture_library.h>
namespace mlx::core
{
class Application
@@ -39,22 +37,20 @@ namespace mlx::core
inline void onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept;
inline void enableAutoRepeat() noexcept;
inline void disableAutoRepeat() noexcept;
inline void getScreenSize(int* w, int* h) noexcept;
inline void* newGraphicsSuport(std::size_t w, std::size_t h, std::string title);
inline void clearGraphicsSupport(void* win);
inline void destroyGraphicsSupport(void* win);
inline void pixelPut(void* win, int x, int y, int color) const noexcept;
inline void pixelPut(void* win, int x, int y, uint32_t color) const noexcept;
inline void stringPut(void* win, int x, int y, int color, char* str);
void* newTexture(int w, int h);
void* newStbTexture(char* file, int* w, int* h); // stb textures are format managed by stb image (png, jpg, bpm, ...)
char* mapTexture(void* img, int* bits_per_pixel, int* size_line, int* endian);
inline void texturePut(void* win, void* img, int x, int y);
inline int getTexturePixel(void* img, int x, int y);
inline void setTexturePixel(void* img, int x, int y, uint32_t color);
void destroyTexture(void* ptr);
inline void loopHook(int (*f)(void*), void* param);
@@ -65,8 +61,7 @@ namespace mlx::core
~Application() = default;
private:
TextureLibrary _texture_lib;
std::list<TextureID> _texture_ids;
std::list<Texture> _textures;
std::vector<std::unique_ptr<GraphicsSupport>> _graphics;
std::function<int(void*)> _loop_hook;
std::unique_ptr<Input> _in;

View File

@@ -32,16 +32,6 @@ namespace mlx::core
_in->onEvent(_graphics[*static_cast<int*>(win)]->getWindow()->getID(), event, funct_ptr, param);
}
void Application::enableAutoRepeat() noexcept
{
_in->enableAutoRepeat();
}
void Application::disableAutoRepeat() noexcept
{
_in->disableAutoRepeat();
}
void Application::getScreenSize(int* w, int* h) noexcept
{
SDL_DisplayMode DM;
@@ -67,7 +57,7 @@ namespace mlx::core
_graphics[*static_cast<int*>(win)].reset();
}
void Application::pixelPut(void* win, int x, int y, int color) const noexcept
void Application::pixelPut(void* win, int x, int y, uint32_t color) const noexcept
{
_graphics[*static_cast<int*>(win)]->pixelPut(x, y, color);
}
@@ -79,11 +69,22 @@ namespace mlx::core
void Application::texturePut(void* win, void* img, int x, int y)
{
TextureID id = *static_cast<TextureID*>(img);
std::shared_ptr<Texture> texture = _texture_lib.getTexture(id);
Texture* texture = static_cast<Texture*>(img);
_graphics[*static_cast<int*>(win)]->texturePut(texture, x, y);
}
int Application::getTexturePixel(void* img, int x, int y)
{
Texture* texture = static_cast<Texture*>(img);
return texture->getPixel(x, y);
}
void Application::setTexturePixel(void* img, int x, int y, uint32_t color)
{
Texture* texture = static_cast<Texture*>(img);
texture->setPixel(x, y, color);
}
void Application::loopHook(int (*f)(void*), void* param)
{
_loop_hook = f;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
/* Updated: 2023/04/12 19:34:22 by maldavid ### ########.fr */
/* Updated: 2023/04/25 15:23:05 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -24,26 +24,26 @@ extern "C"
return new mlx::core::Application();
}
void* mlx_new_window(void* mlx, int w, int h, const char* title)
void* mlx_new_window(mlx::core::Application* mlx, int w, int h, const char* title)
{
return static_cast<mlx::core::Application*>(mlx)->newGraphicsSuport(w, h, title);
return mlx->newGraphicsSuport(w, h, title);
}
int mlx_loop_hook(void* mlx, int (*f)(void*), void* param)
int mlx_loop_hook(mlx::core::Application* mlx, int (*f)(void*), void* param)
{
static_cast<mlx::core::Application*>(mlx)->loopHook(f, param);
mlx->loopHook(f, param);
return 0;
}
int mlx_loop(void* mlx)
int mlx_loop(mlx::core::Application* mlx)
{
static_cast<mlx::core::Application*>(mlx)->run();
mlx->run();
return 0;
}
int mlx_loop_end(void* mlx)
int mlx_loop_end(mlx::core::Application* mlx)
{
static_cast<mlx::core::Application*>(mlx)->loopEnd();
mlx->loopEnd();
return 0;
}
@@ -57,59 +57,57 @@ extern "C"
return SDL_ShowCursor(SDL_DISABLE);
}
int mlx_mouse_move(void* mlx, void* win, int x, int y)
int mlx_mouse_move(mlx::core::Application* mlx, void* win, int x, int y)
{
static_cast<mlx::core::Application*>(mlx)->mouseMove(win, x, y);
mlx->mouseMove(win, x, y);
return 0;
}
int mlx_mouse_get_pos(void* mlx, int* x, int* y)
int mlx_mouse_get_pos(mlx::core::Application* mlx, int* x, int* y)
{
static_cast<mlx::core::Application*>(mlx)->getMousePos(x, y);
mlx->getMousePos(x, y);
return 0;
}
int mlx_on_event(void* mlx, void* win, int event, int (*funct_ptr)(int, void*), void* param)
int mlx_on_event(mlx::core::Application* mlx, void* win, int event, int (*funct_ptr)(int, void*), void* param)
{
static_cast<mlx::core::Application*>(mlx)->onEvent(win, event, funct_ptr, param);
mlx->onEvent(win, event, funct_ptr, param);
return 0;
}
int mlx_do_key_autorepeaton(void* mlx)
void* mlx_new_image(mlx::core::Application* mlx, int width, int height)
{
static_cast<mlx::core::Application*>(mlx)->enableAutoRepeat();
return mlx->newTexture(width, height);
}
int mlx_get_image_pixel(mlx::core::Application* mlx, void* img, int x, int y)
{
return mlx->getTexturePixel(img, x, y);
}
void mlx_set_image_pixel(mlx::core::Application* mlx, void* img, int x, int y, int color)
{
unsigned char color_bits[4];
color_bits[0] = (color & 0x00FF0000) >> 16;
color_bits[1] = (color & 0x0000FF00) >> 8;
color_bits[2] = color & 0x000000FF;
color_bits[3] = 0xFF;
mlx->setTexturePixel(img, x, y, *reinterpret_cast<unsigned int*>(color_bits));
}
int mlx_put_image_to_window(mlx::core::Application* mlx, void* win, void* img, int x, int y)
{
mlx->texturePut(win, img, x, y);
return 0;
}
int mlx_do_key_autorepeatoff(void* mlx)
int mlx_destroy_image(mlx::core::Application* mlx, void* img)
{
static_cast<mlx::core::Application*>(mlx)->disableAutoRepeat();
mlx->destroyTexture(img);
return 0;
}
void* mlx_new_image(void* mlx, int width, int height)
{
return static_cast<mlx::core::Application*>(mlx)->newTexture(width, height);
}
char* mlx_get_data_addr(void* mlx, void* img, int* bits_per_pixel, int* size_line, int* endian)
{
return static_cast<mlx::core::Application*>(mlx)->mapTexture(img, bits_per_pixel, size_line, endian);
}
int mlx_put_image_to_window(void* mlx, void* win, void* img, int x, int y)
{
static_cast<mlx::core::Application*>(mlx)->texturePut(win, img, x, y);
return 0;
}
int mlx_destroy_image(void* mlx, void* img)
{
static_cast<mlx::core::Application*>(mlx)->destroyTexture(img);
return 0;
}
void* mlx_png_file_to_image(void* mlx, char* filename, int* width, int* height)
void* mlx_png_file_to_image(mlx::core::Application* mlx, char* filename, int* width, int* height)
{
std::filesystem::path file(filename);
if(file.extension() != ".png")
@@ -117,10 +115,10 @@ extern "C"
mlx::core::error::report(e_kind::error, "PNG loader : not a png file '%s'", filename);
return nullptr;
}
return static_cast<mlx::core::Application*>(mlx)->newStbTexture(filename, width, height);
return mlx->newStbTexture(filename, width, height);
}
void* mlx_jpg_file_to_image(void* mlx, char* filename, int* width, int* height)
void* mlx_jpg_file_to_image(mlx::core::Application* mlx, char* filename, int* width, int* height)
{
std::filesystem::path file(filename);
if(file.extension() != ".jpg" && file.extension() != ".jpeg")
@@ -128,10 +126,10 @@ extern "C"
mlx::core::error::report(e_kind::error, "PNG loader : not a jpg file '%s'", filename);
return nullptr;
}
return static_cast<mlx::core::Application*>(mlx)->newStbTexture(filename, width, height);
return mlx->newStbTexture(filename, width, height);
}
void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height)
void* mlx_bmp_file_to_image(mlx::core::Application* mlx, char* filename, int* width, int* height)
{
std::filesystem::path file(filename);
if(file.extension() != ".bmp" && file.extension() != ".dib")
@@ -139,43 +137,53 @@ extern "C"
mlx::core::error::report(e_kind::error, "PNG loader : not a jpg file '%s'", filename);
return nullptr;
}
return static_cast<mlx::core::Application*>(mlx)->newStbTexture(filename, width, height);
return mlx->newStbTexture(filename, width, height);
}
int mlx_pixel_put(void* mlx, void* win, int x, int y, int color)
int mlx_pixel_put(mlx::core::Application* mlx, void* win, int x, int y, int color)
{
static_cast<mlx::core::Application*>(mlx)->pixelPut(win, x, y, color);
unsigned char color_bits[4];
color_bits[0] = (color & 0x00FF0000) >> 16;
color_bits[1] = (color & 0x0000FF00) >> 8;
color_bits[2] = color & 0x000000FF;
color_bits[3] = 0xFF;
mlx->pixelPut(win, x, y, *reinterpret_cast<unsigned int*>(color_bits));
return 0;
}
int mlx_string_put(void* mlx, void* win, int x, int y, int color, char* str)
int mlx_string_put(mlx::core::Application* mlx, void* win, int x, int y, int color, char* str)
{
static_cast<mlx::core::Application*>(mlx)->stringPut(win, x, y, color, str);
unsigned char color_bits[4];
color_bits[0] = (color & 0x00FF0000) >> 16;
color_bits[1] = (color & 0x0000FF00) >> 8;
color_bits[2] = color & 0x000000FF;
color_bits[3] = 0xFF;
mlx->stringPut(win, x, y, *reinterpret_cast<unsigned int*>(color_bits), str);
return 0;
}
int mlx_clear_window(void* mlx, void* win)
int mlx_clear_window(mlx::core::Application* mlx, void* win)
{
static_cast<mlx::core::Application*>(mlx)->clearGraphicsSupport(win);
mlx->clearGraphicsSupport(win);
return 0;
}
int mlx_destroy_window(void* mlx, void* win)
int mlx_destroy_window(mlx::core::Application* mlx, void* win)
{
static_cast<mlx::core::Application*>(mlx)->destroyGraphicsSupport(win);
mlx->destroyGraphicsSupport(win);
return 0;
}
int mlx_destroy_display(void* mlx)
int mlx_destroy_display(mlx::core::Application* mlx)
{
delete static_cast<mlx::core::Application*>(mlx);
delete mlx;
mlx::Render_Core::get().destroy();
return 0;
}
int mlx_get_screens_size(void* mlx, int* w, int* h)
int mlx_get_screens_size(mlx::core::Application* mlx, int* w, int* h)
{
static_cast<mlx::core::Application*>(mlx)->getScreenSize(w, h);
mlx->getScreenSize(w, h);
return 0;
}
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:48:06 by maldavid #+# #+# */
/* Updated: 2022/12/18 17:47:51 by maldavid ### ########.fr */
/* Updated: 2023/04/23 13:07:56 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,11 +30,11 @@ namespace mlx::core::error
switch(kind)
{
case e_kind::message: std::cout << "[MacroLibX] Message : " << buffer << std::endl; break;
case e_kind::warning: std::cout << "[MacroLibX] Warning : " << buffer << std::endl; break;
case e_kind::error: std::cerr << "[MacroLibX] Error : " << buffer << std::endl; break;
case e_kind::message: std::cout << "\033[1;34m[MacroLibX] Message : \033[1;0m" << buffer << std::endl; break;
case e_kind::warning: std::cout << "\033[1;35m[MacroLibX] Warning : \033[1;0m" << buffer << std::endl; break;
case e_kind::error: std::cerr << "\033[1;31m[MacroLibX] Error : \033[1;0m" << buffer << std::endl; break;
case e_kind::fatal_error:
std::cerr << "[MacroLibX] Fatal Error : " << buffer << std::endl;
std::cerr << "\033[1;31m[MacroLibX] Fatal Error : \033[1;0m" << buffer << std::endl;
std::exit(EXIT_FAILURE);
break;
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */
/* Updated: 2023/04/11 18:37:11 by maldavid ### ########.fr */
/* Updated: 2023/04/21 18:43:38 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -40,9 +40,9 @@ namespace mlx
inline void beginRender() noexcept;
inline void clearRenderData() noexcept;
inline void pixelPut(int x, int y, int color) noexcept;
inline void pixelPut(int x, int y, uint32_t color) noexcept;
inline void stringPut(int x, int y, int color, std::string str);
inline void texturePut(std::shared_ptr<Texture> texture, int x, int y);
inline void texturePut(Texture* texture, int x, int y);
void endRender() noexcept;

View File

@@ -32,7 +32,7 @@ namespace mlx
_text_put_pipeline->clear();
}
void GraphicsSupport::pixelPut(int x, int y, int color) noexcept
void GraphicsSupport::pixelPut(int x, int y, uint32_t color) noexcept
{
_pixel_put_pipeline.setPixel(x, y, color);
}
@@ -42,7 +42,7 @@ namespace mlx
_text_put_pipeline->put(x, y, color, str);
}
void GraphicsSupport::texturePut(std::shared_ptr<Texture> texture, int x, int y)
void GraphicsSupport::texturePut(Texture* texture, int x, int y)
{
_textures_to_render.emplace(texture, x, y);
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */
/* Updated: 2023/04/12 19:54:20 by maldavid ### ########.fr */
/* Updated: 2023/04/19 12:14:38 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,9 +26,6 @@ namespace mlx
_xRel = 0;
_yRel = 0;
if(!_auto_repeat)
std::memset(_keys.data(), 0, SDL_NUM_SCANCODES);
while(SDL_PollEvent(&_event))
{
if(_event.type == SDL_MOUSEMOTION)

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */
/* Updated: 2023/04/13 10:55:46 by maldavid ### ########.fr */
/* Updated: 2023/04/19 12:14:43 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -51,9 +51,6 @@ namespace mlx
inline bool is_running() const noexcept { return !_end; }
inline constexpr void finish() noexcept { _end = true; }
inline constexpr void enableAutoRepeat() noexcept { _auto_repeat = true; }
inline constexpr void disableAutoRepeat() noexcept { _auto_repeat = false; }
inline void addWindow(std::shared_ptr<MLX_Window> window)
{
_windows[window->getID()] = window;
@@ -81,6 +78,5 @@ namespace mlx
int _yRel = 0;
bool _end = false;
bool _auto_repeat = true;
};
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 18:55:57 by maldavid #+# #+# */
/* Updated: 2023/01/25 15:24:40 by maldavid ### ########.fr */
/* Updated: 2023/04/23 12:37:32 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -81,7 +81,7 @@ namespace mlx
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = RCore::findMemoryType(memRequirements.memoryTypeBits, properties);
allocInfo.memoryTypeIndex = *RCore::findMemoryType(memRequirements.memoryTypeBits, properties);
if(vkAllocateMemory(device, &allocInfo, nullptr, &_memory) != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan : failed to allocate buffer memory");

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 23:18:52 by maldavid #+# #+# */
/* Updated: 2023/03/31 15:29:01 by maldavid ### ########.fr */
/* Updated: 2023/04/22 19:51:47 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/25 15:05:05 by maldavid #+# #+# */
/* Updated: 2023/01/25 15:36:41 by maldavid ### ########.fr */
/* Updated: 2023/04/22 19:51:54 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:45:52 by maldavid #+# #+# */
/* Updated: 2023/01/23 18:54:12 by maldavid ### ########.fr */
/* Updated: 2023/04/22 19:51:56 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:45:29 by maldavid #+# #+# */
/* Updated: 2023/01/25 15:37:09 by maldavid ### ########.fr */
/* Updated: 2023/04/22 19:51:59 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:27:38 by maldavid #+# #+# */
/* Updated: 2023/01/25 15:37:00 by maldavid ### ########.fr */
/* Updated: 2023/04/22 19:52:05 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:26:06 by maldavid #+# #+# */
/* Updated: 2023/04/02 18:13:38 by maldavid ### ########.fr */
/* Updated: 2023/04/23 15:19:08 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,11 +19,16 @@ namespace mlx
{
void CmdBuffer::init(CmdManager* manager)
{
_manager = manager;
init(&manager->getCmdPool());
}
void CmdBuffer::init(CmdPool* pool)
{
_pool = pool;
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.commandPool = manager->getCmdPool().get();
allocInfo.commandPool = pool->get();
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandBufferCount = 1;
@@ -57,6 +62,26 @@ namespace mlx
_is_recording = false;
}
void CmdBuffer::submitIdle() noexcept
{
auto device = Render_Core::get().getDevice().get();
VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &_cmd_buffer;
VkFenceCreateInfo fenceCreateInfo = {};
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
VkFence fence;
vkCreateFence(device, &fenceCreateInfo, nullptr, &fence);
vkResetFences(device, 1, &fence);
vkQueueSubmit(Render_Core::get().getQueue().getGraphic(), 1, &submitInfo, fence);
vkWaitForFences(device, 1, &fence, VK_TRUE, UINT64_MAX);
vkDestroyFence(device, fence, nullptr);
}
void CmdBuffer::submit(Semaphore& semaphores) noexcept
{
VkSemaphore signalSemaphores[] = { semaphores.getRenderImageSemaphore() };
@@ -79,7 +104,7 @@ namespace mlx
void CmdBuffer::destroy() noexcept
{
vkFreeCommandBuffers(Render_Core::get().getDevice().get(), _manager->getCmdPool().get(), 1, &_cmd_buffer);
_fence.destroy();
_cmd_buffer = VK_NULL_HANDLE;
}
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:25:42 by maldavid #+# #+# */
/* Updated: 2023/04/02 17:57:26 by maldavid ### ########.fr */
/* Updated: 2023/04/21 13:20:49 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,23 +22,27 @@ namespace mlx
{
public:
void init(class CmdManager* manager);
void init(class CmdPool* pool);
void destroy() noexcept;
void beginRecord(VkCommandBufferUsageFlags usage = 0);
void submit(class Semaphore& semaphores) noexcept;
void submitIdle() noexcept;
inline void waitForExecution() noexcept { _fence.waitAndReset(); }
inline void reset() noexcept { vkResetCommandBuffer(_cmd_buffer, 0); }
void endRecord();
inline bool isRecording() const noexcept { return _is_recording; }
inline bool isInit() const noexcept { return _cmd_buffer != VK_NULL_HANDLE; }
inline VkCommandBuffer& operator()() noexcept { return _cmd_buffer; }
inline VkCommandBuffer& get() noexcept { return _cmd_buffer; }
inline Fence& getFence() noexcept { return _fence; }
private:
Fence _fence;
VkCommandBuffer _cmd_buffer = VK_NULL_HANDLE;
class CmdManager* _manager = nullptr;
class CmdPool* _pool = nullptr;
bool _is_recording = false;
};
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/17 23:33:34 by maldavid #+# #+# */
/* Updated: 2023/03/31 12:22:34 by maldavid ### ########.fr */
/* Updated: 2023/04/23 19:09:21 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,13 +23,15 @@
#include "render_core.h"
#include <mutex>
#ifdef DEBUG
#warning "MLX is being compiled in debug mode, this activates Vulkan's validation layers and debug messages and may impact rendering performances"
#endif
namespace mlx
{
std::mutex mutex;
namespace RCore
{
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties)
std::optional<uint32_t> findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties, bool error)
{
VkPhysicalDeviceMemoryProperties memProperties;
vkGetPhysicalDeviceMemoryProperties(Render_Core::get().getDevice().getPhysicalDevice(), &memProperties);
@@ -37,11 +39,11 @@ namespace mlx
for(uint32_t i = 0; i < memProperties.memoryTypeCount; i++)
{
if((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties)
return i;
return i;
}
core::error::report(e_kind::fatal_error, "Vulkan : failed to find suitable memory type");
return -1; // just to avoid warning
if(error)
core::error::report(e_kind::fatal_error, "Vulkan : failed to find suitable memory type");
return std::nullopt;
}
}
@@ -60,12 +62,10 @@ namespace mlx
void Render_Core::destroy()
{
std::unique_lock<std::mutex> watchdog(mutex, std::try_to_lock);
if(!_is_init)
return;
vkDeviceWaitIdle(_device());
vkDeviceWaitIdle(_device());
_device.destroy();
_layers.destroy();

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:16:32 by maldavid #+# #+# */
/* Updated: 2023/01/25 15:23:19 by maldavid ### ########.fr */
/* Updated: 2023/04/23 12:31:42 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,6 +14,7 @@
#define __MLX_RENDER_CORE__
#include <volk.h>
#include <optional>
#include "vk_queues.h"
#include "vk_device.h"
@@ -27,10 +28,10 @@ namespace mlx
{
namespace RCore
{
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
std::optional<uint32_t> findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties, bool error = true);
}
#ifndef NDEBUG
#ifdef DEBUG
constexpr const bool enableValidationLayers = true;
#else
constexpr const bool enableValidationLayers = false;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/23 18:40:44 by maldavid #+# #+# */
/* Updated: 2023/03/31 17:54:38 by maldavid ### ########.fr */
/* Updated: 2023/04/22 19:52:08 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/31 18:03:35 by maldavid #+# #+# */
/* Updated: 2023/04/08 18:41:54 by maldavid ### ########.fr */
/* Updated: 2023/08/02 12:34:25 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,19 +14,20 @@
#include <renderer/images/texture.h>
#include <renderer/buffers/vk_buffer.h>
#include <renderer/renderer.h>
#include <iostream>
#include <cstring>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#include <iostream>
namespace mlx
{
void Texture::create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format)
{
Image::create(width, height, format,
VK_IMAGE_TILING_OPTIMAL,
Image::create(width, height, format, VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
{ VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT }
);
Image::createImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT);
@@ -52,29 +53,58 @@ namespace mlx
Image::copyFromBuffer(staging_buffer);
staging_buffer.destroy();
}
_cpu_map = nullptr;
_cpu_map_adress = nullptr;
}
void* Texture::openCPUmap()
void Texture::setPixel(int x, int y, uint32_t color) noexcept
{
if(_cpu_map == nullptr)
{
_cpu_map = std::make_shared<Buffer>();
_cpu_map->create(Buffer::kind::dynamic, sizeof(uint32_t) * (getWidth() * getHeight()), VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
Image::copyToBuffer(*_cpu_map);
}
if(!_cpu_map->isMapped())
_cpu_map->mapMem(&_cpu_map_adress);
if(_cpu_map_adress == nullptr)
core::error::report(e_kind::fatal_error, "Texture : CPU memory mapping failed");
return _cpu_map_adress;
if(x < 0 || y < 0 || x > getWidth() || y > getHeight())
return;
if(_map == nullptr)
openCPUmap();
_cpu_map[(y * getWidth()) + x] = color;
_has_been_modified = true;
}
int Texture::getPixel(int x, int y) noexcept
{
if(x < 0 || y < 0 || x > getWidth() || y > getHeight())
return 0;
if(_map == nullptr)
openCPUmap();
uint32_t color = _cpu_map[(y * getWidth()) + x];
color >>= 8;
return (color);
}
void Texture::openCPUmap()
{
if(_map != nullptr)
return;
#ifdef DEBUG
core::error::report(e_kind::message, "Texture : enabling CPU mapping");
#endif
std::size_t size = getWidth() * getHeight() * formatSize(getFormat());
_buf_map.emplace();
_buf_map->create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
Image::copyToBuffer(*_buf_map);
_buf_map->mapMem(&_map);
_cpu_map = std::vector<uint32_t>(getWidth() * getHeight(), 0);
std::memcpy(_cpu_map.data(), _map, size);
#ifdef DEBUG
core::error::report(e_kind::message, "Texture : mapped CPU memory using staging buffer");
#endif
}
void Texture::render(Renderer& renderer, int x, int y)
{
if(_cpu_map_adress != nullptr)
Image::copyFromBuffer(*_cpu_map);
if(_has_been_modified)
{
std::memcpy(_map, _cpu_map.data(), _cpu_map.size() * formatSize(getFormat()));
Image::copyFromBuffer(*_buf_map);
_has_been_modified = false;
}
auto cmd = renderer.getActiveCmdBuffer().get();
_vbo.bind(renderer);
_ibo.bind(renderer);
@@ -86,8 +116,8 @@ namespace mlx
void Texture::destroy() noexcept
{
Image::destroy();
if(_cpu_map != nullptr)
_cpu_map->destroy();
if(_buf_map.has_value())
_buf_map->destroy();
_vbo.destroy();
_ibo.destroy();
}
@@ -96,23 +126,15 @@ namespace mlx
{
Texture texture;
int channels;
VkFormat format;
uint8_t* data = nullptr;
std::string filename = file.string();
if(!std::filesystem::exists(std::move(file)))
core::error::report(e_kind::fatal_error, "Image : file not found '%s'", filename.c_str());
if(stbi_is_hdr(filename.c_str()))
{
data = (uint8_t*)stbi_loadf(filename.c_str(), w, h, &channels, 4);
format = VK_FORMAT_R32G32B32A32_SFLOAT;
}
else
{
data = stbi_load(filename.c_str(), w, h, &channels, 4);
format = VK_FORMAT_R8G8B8A8_UNORM;
}
texture.create(data, *w, *h, format);
core::error::report(e_kind::fatal_error, "Texture : unsupported image format '%s'", filename.c_str());
data = stbi_load(filename.c_str(), w, h, &channels, 4);
texture.create(data, *w, *h, VK_FORMAT_R8G8B8A8_UNORM);
stbi_image_free(data);
return texture;
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 02:24:58 by maldavid #+# #+# */
/* Updated: 2023/04/07 16:39:35 by maldavid ### ########.fr */
/* Updated: 2023/08/02 12:32:27 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -33,7 +33,8 @@ namespace mlx
void render(class Renderer& renderer, int x, int y);
void destroy() noexcept override;
void* openCPUmap();
void setPixel(int x, int y, uint32_t color) noexcept;
int getPixel(int x, int y) noexcept;
inline void setDescriptor(DescriptorSet set) noexcept { _set = std::move(set); }
inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; }
@@ -43,12 +44,17 @@ namespace mlx
~Texture() = default;
private:
void openCPUmap();
private:
C_VBO _vbo;
C_IBO _ibo;
DescriptorSet _set;
std::shared_ptr<Buffer> _cpu_map;
void* _cpu_map_adress;
std::vector<uint32_t> _cpu_map;
std::optional<Buffer> _buf_map = std::nullopt;
void* _map = nullptr;
bool _has_been_modified = false;
bool _has_been_updated = false;
};
@@ -56,11 +62,11 @@ namespace mlx
struct TextureRenderData
{
std::shared_ptr<Texture> texture;
Texture* texture;
int x;
int y;
TextureRenderData(std::shared_ptr<Texture> _texture, int _x, int _y) : texture(_texture), x(_x), y(_y) {}
TextureRenderData(Texture* _texture, int _x, int _y) : texture(_texture), x(_x), y(_y) {}
bool operator==(const TextureRenderData& rhs) const { return texture == rhs.texture && x == rhs.x && y == rhs.y; }
};
}
@@ -72,7 +78,7 @@ namespace std
{
size_t operator()(const mlx::TextureRenderData& td) const noexcept
{
return std::hash<std::shared_ptr<mlx::Texture>>()(td.texture) + std::hash<int>()(td.x) + std::hash<int>()(td.y);
return std::hash<mlx::Texture*>()(td.texture) + std::hash<int>()(td.x) + std::hash<int>()(td.y);
}
};
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/07 16:40:09 by maldavid #+# #+# */
/* Updated: 2023/04/11 12:30:28 by maldavid ### ########.fr */
/* Updated: 2023/04/23 12:55:22 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,7 +19,7 @@ namespace mlx
Image::create(width, height, format,
VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
{ VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT }
);
Image::createImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT);

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/25 11:59:07 by maldavid #+# #+# */
/* Updated: 2023/04/13 15:07:01 by maldavid ### ########.fr */
/* Updated: 2023/04/23 14:59:41 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,14 +14,16 @@
#include <renderer/core/render_core.h>
#include <renderer/buffers/vk_buffer.h>
#include <renderer/command/vk_cmd_pool.h>
#include <renderer/core/vk_fence.h>
namespace mlx
{
void Image::create(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties)
void Image::create(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, std::vector<VkMemoryPropertyFlags> properties)
{
_width = width;
_height = height;
_format = format;
_tiling = tiling;
VkImageCreateInfo imageInfo{};
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
@@ -43,16 +45,27 @@ namespace mlx
VkMemoryRequirements memRequirements;
vkGetImageMemoryRequirements(Render_Core::get().getDevice().get(), _image, &memRequirements);
std::optional<uint32_t> memTypeIndex;
for(auto prop : properties)
{
memTypeIndex = RCore::findMemoryType(memRequirements.memoryTypeBits, prop, false);
if(memTypeIndex.has_value())
break;
}
if(!memTypeIndex.has_value())
core::error::report(e_kind::fatal_error, "Vulkan : failed to find suitable memory type for an image");
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = RCore::findMemoryType(memRequirements.memoryTypeBits, properties);
allocInfo.memoryTypeIndex = *memTypeIndex;
if(vkAllocateMemory(Render_Core::get().getDevice().get(), &allocInfo, nullptr, &_memory) != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan : failed to allocate memory for an image");
vkBindImageMemory(Render_Core::get().getDevice().get(), _image, _memory, 0);
_pool.init();
}
void Image::createImageView(VkImageViewType type, VkImageAspectFlags aspectFlags) noexcept
@@ -93,24 +106,11 @@ namespace mlx
void Image::copyFromBuffer(Buffer& buffer)
{
CmdPool cmdpool;
cmdpool.init();
auto device = Render_Core::get().getDevice().get();
if(!_transfer_cmd.isInit())
_transfer_cmd.init(&_pool);
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandPool = cmdpool.get();
allocInfo.commandBufferCount = 1;
VkCommandBuffer cmdBuffer;
vkAllocateCommandBuffers(device, &allocInfo, &cmdBuffer);
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
vkBeginCommandBuffer(cmdBuffer, &beginInfo);
_transfer_cmd.reset();
_transfer_cmd.beginRecord();
VkImageMemoryBarrier copy_barrier = {};
copy_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
@@ -123,7 +123,7 @@ namespace mlx
copy_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
copy_barrier.subresourceRange.levelCount = 1;
copy_barrier.subresourceRange.layerCount = 1;
vkCmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &copy_barrier);
vkCmdPipelineBarrier(_transfer_cmd.get(), VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &copy_barrier);
VkBufferImageCopy region = {};
region.bufferOffset = 0;
@@ -133,13 +133,10 @@ namespace mlx
region.imageSubresource.mipLevel = 0;
region.imageSubresource.baseArrayLayer = 0;
region.imageSubresource.layerCount = 1;
region.imageOffset = {0, 0, 0};
region.imageExtent = {
_width,
_height,
1
};
vkCmdCopyBufferToImage(cmdBuffer, buffer.get(), _image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
region.imageOffset = { 0, 0, 0 };
region.imageExtent = { _width, _height, 1 };
vkCmdCopyBufferToImage(_transfer_cmd.get(), buffer.get(), _image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
VkImageMemoryBarrier use_barrier = {};
use_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
@@ -153,43 +150,19 @@ namespace mlx
use_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
use_barrier.subresourceRange.levelCount = 1;
use_barrier.subresourceRange.layerCount = 1;
vkCmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &use_barrier);
vkCmdPipelineBarrier(_transfer_cmd.get(), VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &use_barrier);
vkEndCommandBuffer(cmdBuffer);
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &cmdBuffer;
auto graphicsQueue = Render_Core::get().getQueue().getGraphic();
vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
vkQueueWaitIdle(graphicsQueue);
cmdpool.destroy();
_transfer_cmd.endRecord();
_transfer_cmd.submitIdle();
}
void Image::copyToBuffer(Buffer& buffer)
{
CmdPool cmdpool;
cmdpool.init();
auto device = Render_Core::get().getDevice().get();
if(!_transfer_cmd.isInit())
_transfer_cmd.init(&_pool);
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandPool = cmdpool.get();
allocInfo.commandBufferCount = 1;
VkCommandBuffer cmdBuffer;
vkAllocateCommandBuffers(device, &allocInfo, &cmdBuffer);
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
vkBeginCommandBuffer(cmdBuffer, &beginInfo);
_transfer_cmd.reset();
_transfer_cmd.beginRecord();
VkImageMemoryBarrier copy_barrier = {};
copy_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
@@ -202,7 +175,7 @@ namespace mlx
copy_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
copy_barrier.subresourceRange.levelCount = 1;
copy_barrier.subresourceRange.layerCount = 1;
vkCmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &copy_barrier);
vkCmdPipelineBarrier(_transfer_cmd.get(), VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &copy_barrier);
VkBufferImageCopy region = {};
region.bufferOffset = 0;
@@ -212,13 +185,10 @@ namespace mlx
region.imageSubresource.mipLevel = 0;
region.imageSubresource.baseArrayLayer = 0;
region.imageSubresource.layerCount = 1;
region.imageOffset = {0, 0, 0};
region.imageExtent = {
_width,
_height,
1
};
vkCmdCopyImageToBuffer(cmdBuffer, _image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, buffer.get(), 1, &region);
region.imageOffset = { 0, 0, 0 };
region.imageExtent = { _width, _height, 1 };
vkCmdCopyImageToBuffer(_transfer_cmd.get(), _image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, buffer.get(), 1, &region);
VkImageMemoryBarrier use_barrier = {};
use_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
@@ -232,21 +202,10 @@ namespace mlx
use_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
use_barrier.subresourceRange.levelCount = 1;
use_barrier.subresourceRange.layerCount = 1;
vkCmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &use_barrier);
vkCmdPipelineBarrier(_transfer_cmd.get(), VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &use_barrier);
vkEndCommandBuffer(cmdBuffer);
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &cmdBuffer;
auto graphicsQueue = Render_Core::get().getQueue().getGraphic();
vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
vkQueueWaitIdle(graphicsQueue);
cmdpool.destroy();
_transfer_cmd.endRecord();
_transfer_cmd.submitIdle();
}
void Image::destroy() noexcept
@@ -258,6 +217,9 @@ namespace mlx
vkFreeMemory(Render_Core::get().getDevice().get(), _memory, nullptr);
vkDestroyImage(Render_Core::get().getDevice().get(), _image, nullptr);
if(_transfer_cmd.isInit())
_transfer_cmd.destroy();
_pool.destroy();
}
uint32_t formatSize(VkFormat format)

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/25 11:54:21 by maldavid #+# #+# */
/* Updated: 2023/04/13 10:59:04 by maldavid ### ########.fr */
/* Updated: 2023/04/23 14:17:11 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,6 +16,8 @@
#include <volk.h>
#include <cstddef>
#include <vector>
#include <renderer/command/vk_cmd_buffer.h>
#include <renderer/command/vk_cmd_pool.h>
namespace mlx
{
@@ -26,7 +28,7 @@ namespace mlx
public:
Image() = default;
void create(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties);
void create(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, std::vector<VkMemoryPropertyFlags> properties);
void createImageView(VkImageViewType type, VkImageAspectFlags aspectFlags) noexcept;
void createSampler() noexcept;
void copyFromBuffer(class Buffer& buffer);
@@ -38,6 +40,7 @@ namespace mlx
inline VkDeviceMemory getDeviceMemory() noexcept { return _memory; }
inline VkImageView getImageView() noexcept { return _image_view; }
inline VkFormat getFormat() noexcept { return _format; }
inline VkImageTiling getTiling() noexcept { return _tiling; }
inline VkSampler getSampler() noexcept { return _sampler; }
inline uint32_t getWidth() const noexcept { return _width; }
inline uint32_t getHeight() const noexcept { return _height; }
@@ -45,11 +48,14 @@ namespace mlx
virtual ~Image() = default;
private:
CmdBuffer _transfer_cmd;
CmdPool _pool;
VkImage _image = VK_NULL_HANDLE;
VkDeviceMemory _memory = VK_NULL_HANDLE;
VkImageView _image_view = VK_NULL_HANDLE;
VkSampler _sampler = VK_NULL_HANDLE;
VkFormat _format;
VkImageTiling _tiling;
uint32_t _width = 0;
uint32_t _height = 0;
};

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/31 15:14:50 by maldavid #+# #+# */
/* Updated: 2023/04/13 14:51:37 by maldavid ### ########.fr */
/* Updated: 2023/08/02 05:28:49 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,35 +21,23 @@ namespace mlx
_texture.setDescriptor(renderer.getFragDescriptorSet().duplicate());
_buffer.create(Buffer::kind::dynamic, sizeof(uint32_t) * (width * height), VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
_buffer.mapMem(&_buffer_map);
_cpu_map = std::vector<uint32_t>(height * width, 0);
_width = width;
_height = height;
}
VkDescriptorSet PixelPutPipeline::getDescriptorSet() noexcept
{
return _texture.getSet();
}
void PixelPutPipeline::setPixel(uint32_t x, uint32_t y, int color) noexcept
void PixelPutPipeline::setPixel(uint32_t x, uint32_t y, uint32_t color) noexcept
{
if(x < 0 || y < 0 || x > _width || y > _height)
return;
if(!_buffer.isMapped())
_buffer.mapMem(&_map);
unsigned char* mem = static_cast<unsigned char*>(_map) + (y * _width * sizeof(uint32_t)) + (x * sizeof(uint32_t));
uint32_t new_color = color & 0xFFFFFF00;
new_color >>= 8;
new_color |= (color << 24) & 0xFF000000;
*reinterpret_cast<uint32_t*>(mem) = new_color;
_cpu_map[(y * _width) + x] = color;
_has_been_modified = true;
}
void PixelPutPipeline::clear()
{
if(!_buffer.isMapped())
_buffer.mapMem(&_map);
unsigned char* mem = static_cast<unsigned char*>(_map);
std::memset(mem, 0, sizeof(uint32_t) * (_width * _height));
_cpu_map.assign(_width * _height, 0);
_has_been_modified = true;
}
@@ -57,6 +45,7 @@ namespace mlx
{
if(_has_been_modified)
{
std::memcpy(_buffer_map, _cpu_map.data(), sizeof(uint32_t) * _cpu_map.size());
_texture.copyFromBuffer(_buffer);
_has_been_modified = false;
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/31 13:18:50 by maldavid #+# #+# */
/* Updated: 2023/04/03 14:22:35 by maldavid ### ########.fr */
/* Updated: 2023/08/02 05:27:27 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -25,13 +25,12 @@ namespace mlx
void init(uint32_t width, uint32_t height, class Renderer& renderer) noexcept;
void setPixel(uint32_t x, uint32_t y, int color) noexcept;
void setPixel(uint32_t x, uint32_t y, uint32_t color) noexcept;
void present() noexcept;
void render(class Renderer& renderer) noexcept;
VkDescriptorSet getDescriptorSet() noexcept;
inline VkDescriptorSet getDescriptorSet() noexcept { return _texture.getSet(); }
void clear();
void destroy() noexcept;
~PixelPutPipeline();
@@ -39,7 +38,9 @@ namespace mlx
private:
Texture _texture;
Buffer _buffer;
void* _map = nullptr;
// using vector as CPU map and not directly writting to mapped buffer to improve performances
std::vector<uint32_t> _cpu_map;
void* _buffer_map = nullptr;
uint32_t _width = 0;
uint32_t _height = 0;
bool _has_been_modified = true;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/18 17:25:16 by maldavid #+# #+# */
/* Updated: 2023/04/02 18:10:01 by maldavid ### ########.fr */
/* Updated: 2023/04/21 20:56:51 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/10 11:59:57 by maldavid #+# #+# */
/* Updated: 2023/04/12 13:24:19 by maldavid ### ########.fr */
/* Updated: 2023/04/19 13:43:43 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,7 +14,6 @@
#include <core/errors.h>
#include <renderer/renderer.h>
#include <algorithm>
#include <iostream>
namespace mlx
{

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */
/* Updated: 2023/04/13 10:49:04 by maldavid ### ########.fr */
/* Updated: 2023/04/19 13:43:49 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,8 +19,6 @@
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>
#include <iostream>
namespace mlx
{
TextDrawData::TextDrawData(std::string _text, int _color, int _x, int _y) :

View File

@@ -1,54 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* texture_library.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 14:24:00 by maldavid #+# #+# */
/* Updated: 2023/04/12 13:26:59 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <core/errors.h>
#include <algorithm>
#include <renderer/texture_library.h>
namespace mlx
{
std::shared_ptr<Texture> TextureLibrary::getTexture(TextureID id)
{
if(!_cache.count(id))
core::error::report(e_kind::fatal_error, "Texture Library : wrong texture ID '%d'", id);
return _cache[id];
}
TextureID TextureLibrary::addTextureToLibrary(std::shared_ptr<Texture> texture)
{
auto it = std::find_if(_cache.begin(), _cache.end(), [=](const std::pair<TextureID, std::shared_ptr<Texture>>& v)
{
return v.second.get() == texture.get();
});
if(it != _cache.end())
return it->first;
_cache[_current_id] = texture;
_current_id++;
return _current_id - 1;
}
void TextureLibrary::removeTextureFromLibrary(TextureID id)
{
if(_cache.count(id))
{
_cache[id]->destroy();
_cache.erase(id);
}
}
void TextureLibrary::clearLibrary()
{
for(auto [id, texture] : _cache)
texture->destroy();
_cache.clear();
}
}

View File

@@ -1,46 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* texture_library.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 14:16:13 by maldavid #+# #+# */
/* Updated: 2023/04/01 14:42:39 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_TEXTURE_LIBRARY__
#define __MLX_TEXTURE_LIBRARY__
#include <vector>
#include <memory>
#include <unordered_map>
#include <filesystem>
#include <renderer/images/texture.h>
namespace mlx
{
using TextureID = uint32_t;
constexpr TextureID nulltexture = 0;
class TextureLibrary
{
public:
TextureLibrary() = default;
std::shared_ptr<Texture> getTexture(TextureID id);
TextureID addTextureToLibrary(std::shared_ptr<Texture> texture);
void removeTextureFromLibrary(TextureID id);
void clearLibrary();
~TextureLibrary() = default;
private:
std::unordered_map<TextureID, std::shared_ptr<Texture>> _cache;
TextureID _current_id = 1;
};
}
#endif