Skip to content

Commit

Permalink
Merge pull request #16 from 420verfl0w/multiple-fonts
Browse files Browse the repository at this point in the history
merge Multiple fonts in dev
  • Loading branch information
Kbz-8 authored Dec 14, 2023
2 parents ac91253 + 57b27ad commit cebadef
Show file tree
Hide file tree
Showing 25 changed files with 361 additions and 141 deletions.
6 changes: 3 additions & 3 deletions includes/mlx.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */
/* Updated: 2023/12/11 20:35:41 by kbz_8 ### ########.fr */
/* Updated: 2023/12/14 16:27:44 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -282,7 +282,7 @@ MLX_API int mlx_string_put(void* mlx, void* win, int x, int y, int color, char*
*
* @param mlx Internal MLX application
* @param win Internal window
* @param filepath Filepath to the font
* @param filepath Filepath to the font or "default" to reset to the embedded font
*
* @return (void)
*/
Expand All @@ -293,7 +293,7 @@ MLX_API void mlx_set_font(void* mlx, void* win, char* filepath);
*
* @param mlx Internal MLX application
* @param win Internal window
* @param filepath Filepath to the font
* @param filepath Filepath to the font or "default" to reset to the embedded font
* @param scale Scale to apply to the font
*
* @return (void)
Expand Down
6 changes: 3 additions & 3 deletions src/core/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
/* Updated: 2023/12/11 15:56:18 by kbz_8 ### ########.fr */
/* Updated: 2023/12/14 17:47:17 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -178,7 +178,7 @@ extern "C"
void mlx_set_font(void* mlx, void* win, char* filepath)
{
std::filesystem::path file(filepath);
if(file.extension() != ".ttf" && file.extension() != ".tte")
if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte")
{
mlx::core::error::report(e_kind::error, "TTF loader : not a truetype font file '%s'", filepath);
return;
Expand All @@ -189,7 +189,7 @@ extern "C"
void mlx_set_font_scale(void* mlx, void* win, char* filepath, float scale)
{
std::filesystem::path file(filepath);
if(file.extension() != ".ttf" && file.extension() != ".tte")
if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte")
{
mlx::core::error::report(e_kind::error, "TTF loader : not a truetype font file '%s'", filepath);
return;
Expand Down
8 changes: 2 additions & 6 deletions src/core/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */
/* Updated: 2023/12/10 22:20:38 by kbz_8 ### ########.fr */
/* Updated: 2023/12/14 17:14:30 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -51,11 +51,7 @@ namespace mlx
sets[1] = _pixel_put_pipeline.getDescriptorSet();
vkCmdBindDescriptorSets(cmd_buff, VK_PIPELINE_BIND_POINT_GRAPHICS, _renderer->getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr);
_pixel_put_pipeline.render(*_renderer);

sets[1] = _text_put_pipeline->getDescriptorSet();
vkCmdBindDescriptorSets(cmd_buff, VK_PIPELINE_BIND_POINT_GRAPHICS, _renderer->getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr);
_text_put_pipeline->render();

_text_put_pipeline->render(sets);
_renderer->endFrame();

for(auto& data : _textures_to_render)
Expand Down
2 changes: 0 additions & 2 deletions src/core/graphics.inl
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ namespace mlx
void GraphicsSupport::texturePut(Texture* texture, int x, int y)
{
_textures_to_render.emplace_back(texture, x, y);

auto it = std::find(_textures_to_render.begin(), _textures_to_render.end() - 1, _textures_to_render.back());

if(it != _textures_to_render.end() - 1)
_textures_to_render.erase(it);
}
Expand Down
53 changes: 35 additions & 18 deletions src/renderer/buffers/vk_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 18:55:57 by maldavid #+# #+# */
/* Updated: 2023/12/10 23:05:14 by kbz_8 ### ########.fr */
/* Updated: 2023/12/12 22:11:47 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,9 +23,9 @@ namespace mlx
void Buffer::create(Buffer::kind type, VkDeviceSize size, VkBufferUsageFlags usage, const char* name, const void* data)
{
_usage = usage;
if(type == Buffer::kind::constant)
if(type == Buffer::kind::constant || type == Buffer::kind::dynamic_device_local)
{
if(data == nullptr)
if(data == nullptr && type == Buffer::kind::constant)
{
core::error::report(e_kind::warning, "Vulkan : trying to create constant buffer without data (constant buffers cannot be modified after creation)");
return;
Expand All @@ -45,7 +45,7 @@ namespace mlx
mapMem(&mapped);
std::memcpy(mapped, data, size);
unmapMem();
if(type == Buffer::kind::constant)
if(type == Buffer::kind::constant || type == Buffer::kind::dynamic_device_local)
pushToGPU();
}
}
Expand Down Expand Up @@ -83,20 +83,20 @@ namespace mlx
_size = size;
}

void Buffer::pushToGPU() noexcept
bool Buffer::copyFromBuffer(const Buffer& buffer) noexcept
{
VmaAllocationCreateInfo alloc_info{};
alloc_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;

Buffer newBuffer;
newBuffer._usage = (_usage & 0xFFFFFFFC) | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
#ifdef DEBUG
std::string new_name = _name + "_GPU";
newBuffer.createBuffer(newBuffer._usage, alloc_info, _size, new_name.c_str());
#else
newBuffer.createBuffer(newBuffer._usage, alloc_info, _size, nullptr);
#endif
if(!(_usage & VK_BUFFER_USAGE_TRANSFER_DST_BIT))
{
core::error::report(e_kind::error, "Vulkan : buffer cannot be the destination of a copy because it does not have the correct usage flag");
return false;
}
if(!(buffer._usage & VK_BUFFER_USAGE_TRANSFER_SRC_BIT))
{
core::error::report(e_kind::error, "Vulkan : buffer cannot be the source of a copy because it does not have the correct usage flag");
return false;
}

// TODO, use global cmd buffer pool to manage resources
CmdPool cmdpool;
cmdpool.init();
CmdBuffer cmdBuffer;
Expand All @@ -106,16 +106,33 @@ namespace mlx

VkBufferCopy copyRegion{};
copyRegion.size = _size;
vkCmdCopyBuffer(cmdBuffer.get(), _buffer, newBuffer._buffer, 1, &copyRegion);
vkCmdCopyBuffer(cmdBuffer.get(), buffer._buffer, _buffer, 1, &copyRegion);

cmdBuffer.endRecord();
cmdBuffer.submitIdle();

cmdBuffer.destroy();
cmdpool.destroy();

this->swap(newBuffer);
return true;
}

void Buffer::pushToGPU() noexcept
{
VmaAllocationCreateInfo alloc_info{};
alloc_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;

Buffer newBuffer;
newBuffer._usage = (_usage & 0xFFFFFFFC) | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
#ifdef DEBUG
std::string new_name = _name + "_GPU";
newBuffer.createBuffer(newBuffer._usage, alloc_info, _size, new_name.c_str());
#else
newBuffer.createBuffer(newBuffer._usage, alloc_info, _size, nullptr);
#endif

if(newBuffer.copyFromBuffer(*this)) // if the copy succeded we swap the buffers, else the new one is deleted
this->swap(newBuffer);
newBuffer.destroy();
}

Expand Down
5 changes: 3 additions & 2 deletions src/renderer/buffers/vk_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 23:18:52 by maldavid #+# #+# */
/* Updated: 2023/12/11 19:47:39 by kbz_8 ### ########.fr */
/* Updated: 2023/12/12 21:12:44 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,7 +22,7 @@ namespace mlx
class Buffer
{
public:
enum class kind { dynamic, uniform, constant };
enum class kind { dynamic, dynamic_device_local, uniform, constant };

void create(kind type, VkDeviceSize size, VkBufferUsageFlags usage, const char* name, const void* data = nullptr);
void destroy() noexcept;
Expand All @@ -32,6 +32,7 @@ namespace mlx
inline void unmapMem() noexcept { Render_Core::get().getAllocator().unmapMemory(_allocation); _is_mapped = false; }

void flush(VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0);
bool copyFromBuffer(const Buffer& buffer) noexcept;

inline VkBuffer& operator()() noexcept { return _buffer; }
inline VkBuffer& get() noexcept { return _buffer; }
Expand Down
26 changes: 25 additions & 1 deletion src/renderer/buffers/vk_vbo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:28:08 by maldavid #+# #+# */
/* Updated: 2023/11/10 08:33:52 by maldavid ### ########.fr */
/* Updated: 2023/12/12 22:17:14 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,7 +18,10 @@ namespace mlx
void VBO::setData(uint32_t size, const void* data)
{
if(size > getSize())
{
core::error::report(e_kind::error, "Vulkan : trying to store to much data in a vertex buffer (%d bytes in %d bytes)", size, getSize());
return;
}

if(data == nullptr)
core::error::report(e_kind::warning, "Vulkan : mapping null data in a vertex buffer");
Expand All @@ -28,4 +31,25 @@ namespace mlx
std::memcpy(temp, data, static_cast<size_t>(size));
unmapMem();
}

void D_VBO::setData(uint32_t size, const void* data)
{
if(size > getSize())
{
core::error::report(e_kind::error, "Vulkan : trying to store to much data in a vertex buffer (%d bytes in %d bytes)", size, getSize());
return;
}

if(data == nullptr)
core::error::report(e_kind::warning, "Vulkan : mapping null data in a vertex buffer");

Buffer tmp_buf;
#ifdef DEBUG
tmp_buf.create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, "tmp_buffer", data);
#else
tmp_buf.create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, nullptr, data);
#endif
copyFromBuffer(tmp_buf);
tmp_buf.destroy();
}
}
12 changes: 10 additions & 2 deletions src/renderer/buffers/vk_vbo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:27:38 by maldavid #+# #+# */
/* Updated: 2023/12/08 19:06:45 by kbz_8 ### ########.fr */
/* Updated: 2023/12/12 22:46:21 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,7 +22,15 @@ namespace mlx
class VBO : public Buffer
{
public:
inline void create(uint32_t size, const char* name) { Buffer::create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name); }
inline void create(uint32_t size, const void* data, const char* name) { Buffer::create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name, data); }
void setData(uint32_t size, const void* data);
inline void bind(Renderer& renderer) noexcept { vkCmdBindVertexBuffers(renderer.getActiveCmdBuffer().get(), 0, 1, &_buffer, &_offset); }
};

class D_VBO : public Buffer
{
public:
inline void create(uint32_t size, const void* data, const char* name) { Buffer::create(Buffer::kind::dynamic_device_local, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name, data); }
void setData(uint32_t size, const void* data);
inline void bind(Renderer& renderer) noexcept { vkCmdBindVertexBuffers(renderer.getActiveCmdBuffer().get(), 0, 1, &_buffer, &_offset); }
};
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/descriptors/vk_descriptor_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/23 18:40:44 by maldavid #+# #+# */
/* Updated: 2023/12/07 20:00:13 by kbz_8 ### ########.fr */
/* Updated: 2023/12/14 16:45:11 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -42,7 +42,7 @@ namespace mlx
#endif
}

void DescriptorSet::writeDescriptor(int binding, UBO* ubo) noexcept
void DescriptorSet::writeDescriptor(int binding, UBO* ubo) const noexcept
{
auto device = Render_Core::get().getDevice().get();

Expand All @@ -66,7 +66,7 @@ namespace mlx
}
}

void DescriptorSet::writeDescriptor(int binding, VkImageView view, VkSampler sampler) noexcept
void DescriptorSet::writeDescriptor(int binding, VkImageView view, VkSampler sampler) const noexcept
{
auto device = Render_Core::get().getDevice().get();

Expand Down
8 changes: 4 additions & 4 deletions src/renderer/descriptors/vk_descriptor_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/23 18:39:36 by maldavid #+# #+# */
/* Updated: 2023/12/08 19:09:31 by kbz_8 ### ########.fr */
/* Updated: 2023/12/14 17:12:49 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -25,10 +25,10 @@ namespace mlx
public:
void init(class Renderer* renderer, class DescriptorPool* pool, class DescriptorSetLayout* layout);

void writeDescriptor(int binding, class UBO* ubo) noexcept;
void writeDescriptor(int binding, VkImageView view, VkSampler sampler) noexcept;
void writeDescriptor(int binding, class UBO* ubo) const noexcept;
void writeDescriptor(int binding, VkImageView view, VkSampler sampler) const noexcept;

inline bool isInit() noexcept { return _pool != nullptr && _renderer != nullptr; }
inline bool isInit() const noexcept { return _pool != nullptr && _renderer != nullptr; }

DescriptorSet duplicate();

Expand Down
Loading

0 comments on commit cebadef

Please sign in to comment.