Skip to content

Commit

Permalink
fixing compilation issues, adding profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Nov 10, 2023
1 parent e6ed390 commit 85c43da
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*.a
*.so
*.out
.cache/
3 changes: 2 additions & 1 deletion src/core/application.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 22:10:52 by maldavid #+# #+# */
/* Updated: 2023/08/28 10:19:53 by maldavid ### ########.fr */
/* Updated: 2023/11/10 09:06:44 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,6 +16,7 @@
#include <array>
#include <core/errors.h>
#include <utils/endian.h>
#include <core/profile.h>

namespace mlx::core
{
Expand Down
60 changes: 60 additions & 0 deletions src/core/profile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* profile.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/10 08:49:17 by maldavid #+# #+# */
/* Updated: 2023/11/10 09:05:56 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef __MLX_PROFILE__
#define __MLX_PROFILE__

// Try to identify the compiler
#if defined(__BORLANDC__)
#define MLX_COMPILER_BORDLAND
#elif defined(__clang__)
#define MLX_COMPILER_CLANG
#ifdef __MINGW32__
#define MLX_COMPILER_MINGW
#ifdef __MINGW64_VERSION_MAJOR
#define MLX_COMPILER_MINGW_W64
#endif
#endif
#elif defined(__GNUC__) || defined(__MINGW32__)
#define MLX_COMPILER_GCC
#ifdef __MINGW32__
#define MLX_COMPILER_MINGW
#ifdef __MINGW64_VERSION_MAJOR
#define MLX_COMPILER_MINGW_W64
#endif
#endif
#elif defined(__INTEL_COMPILER) || defined(__ICL)
#define MLX_COMPILER_INTEL
#elif defined(_MSC_VER)
#define MLX_COMPILER_MSVC
#else
#define MLX_COMPILER_UNKNOWN
#warning "This compiler is not fully supported"
#endif

// Checking common assumptions
#include <climits>
#include <cstdint>

static_assert(CHAR_BIT == 8, "CHAR_BIT is expected to be 8");

static_assert(sizeof(int8_t) == 1, "int8_t is not of the correct size" );
static_assert(sizeof(int16_t) == 2, "int16_t is not of the correct size");
static_assert(sizeof(int32_t) == 4, "int32_t is not of the correct size");
static_assert(sizeof(int64_t) == 8, "int64_t is not of the correct size");

static_assert(sizeof(uint8_t) == 1, "uint8_t is not of the correct size" );
static_assert(sizeof(uint16_t) == 2, "uint16_t is not of the correct size");
static_assert(sizeof(uint32_t) == 4, "uint32_t is not of the correct size");
static_assert(sizeof(uint64_t) == 8, "uint64_t is not of the correct size");

#endif
3 changes: 2 additions & 1 deletion 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/11/09 20:02:14 by maldavid ### ########.fr */
/* Updated: 2023/11/10 08:14:42 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -140,5 +140,6 @@ namespace mlx

void Buffer::flush(VkDeviceSize size, VkDeviceSize offset)
{
Render_Core::get().getAllocator().flush(_allocation, size, offset);
}
}
4 changes: 2 additions & 2 deletions src/renderer/buffers/vk_ibo.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/25 15:05:05 by maldavid #+# #+# */
/* Updated: 2023/04/22 19:51:54 by maldavid ### ########.fr */
/* Updated: 2023/11/10 07:53:26 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,7 +23,7 @@ namespace mlx
{
public:
inline void create(uint32_t size, const uint16_t* data) { Buffer::create(Buffer::kind::constant, size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, data); }
inline void bind(Renderer& renderer) noexcept { vkCmdBindIndexBuffer(renderer.getActiveCmdBuffer().get(), _buffer, _offset, VK_INDEX_TYPE_UINT16); }
inline void bind(Renderer& renderer) noexcept { vkCmdBindIndexBuffer(renderer.getActiveCmdBuffer().get(), _buffer, getOffset(), VK_INDEX_TYPE_UINT16); }
};
}

Expand Down
7 changes: 1 addition & 6 deletions src/renderer/buffers/vk_ubo.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:45:52 by maldavid #+# #+# */
/* Updated: 2023/04/22 19:51:56 by maldavid ### ########.fr */
/* Updated: 2023/11/10 07:54:48 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -50,11 +50,6 @@ namespace mlx
return _buffers[_renderer->getActiveImageIndex()].getOffset();
}

VkDeviceMemory UBO::getDeviceMemory() noexcept
{
return _buffers[_renderer->getActiveImageIndex()].getDeviceMemory();
}

VkBuffer& UBO::operator()() noexcept
{
return _buffers[_renderer->getActiveImageIndex()].get();
Expand Down
3 changes: 1 addition & 2 deletions src/renderer/buffers/vk_ubo.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:45:29 by maldavid #+# #+# */
/* Updated: 2023/04/22 19:51:59 by maldavid ### ########.fr */
/* Updated: 2023/11/10 07:54:29 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -37,7 +37,6 @@ namespace mlx

inline unsigned int getSize(int i) noexcept { return _buffers[i].getSize(); }
inline unsigned int getOffset(int i) noexcept { return _buffers[i].getOffset(); }
inline VkDeviceMemory getDeviceMemory(int i) noexcept { return _buffers[i].getDeviceMemory(); }
inline VkBuffer& operator()(int i) noexcept { return _buffers[i].get(); }
inline VkBuffer& get(int i) noexcept { return _buffers[i].get(); }

Expand Down
6 changes: 3 additions & 3 deletions 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: 2022/12/19 15:38:45 by maldavid ### ########.fr */
/* Updated: 2023/11/10 08:33:52 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,8 +17,8 @@ namespace mlx
{
void VBO::setData(uint32_t size, const void* data)
{
if(size > _size)
core::error::report(e_kind::error, "Vulkan : trying to store to much data in a vertex buffer (%d bytes in %d bytes)", size, _size);
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());

if(data == nullptr)
core::error::report(e_kind::warning, "Vulkan : mapping null data in a vertex buffer");
Expand Down
6 changes: 3 additions & 3 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/04/22 19:52:05 by maldavid ### ########.fr */
/* Updated: 2023/11/10 07:54:15 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -25,14 +25,14 @@ namespace mlx

void setData(uint32_t size, const void* data);

inline void bind(Renderer& renderer) noexcept { vkCmdBindVertexBuffers(renderer.getActiveCmdBuffer().get(), 0, 1, &_buffer, &_offset); }
inline void bind(Renderer& renderer) noexcept { vkCmdBindVertexBuffers(renderer.getActiveCmdBuffer().get(), 0, 1, &_buffer, &_alloc_infos.offset); }
};

class C_VBO : public Buffer
{
public:
inline void create(uint32_t size, const void* data) { Buffer::create(Buffer::kind::constant, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, data); }
inline void bind(Renderer& renderer) noexcept { vkCmdBindVertexBuffers(renderer.getActiveCmdBuffer().get(), 0, 1, &_buffer, &_offset); }
inline void bind(Renderer& renderer) noexcept { vkCmdBindVertexBuffers(renderer.getActiveCmdBuffer().get(), 0, 1, &_buffer, &_alloc_infos.offset); }
};
}

Expand Down
28 changes: 24 additions & 4 deletions src/renderer/core/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@
/* By: kbz_8 <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/20 22:02:37 by kbz_8 #+# #+# */
/* Updated: 2023/11/08 22:31:27 by maldavid ### ########.fr */
/* Updated: 2023/11/10 09:07:33 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

#include <core/profile.h>
#include <cstdio>

#define VMA_STATIC_VULKAN_FUNCTIONS 0
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0
#define VMA_VULKAN_VERSION 1002000
#include <renderer/core/memory.h>
#define VMA_IMPLEMENTATION

#ifdef MLX_COMPILER_CLANG
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnullability-completeness"
#include <renderer/core/memory.h>
#pragma clang diagnostic pop
#else
#include <renderer/core/memory.h>
#endif

#include <renderer/core/render_core.h>

namespace mlx
Expand Down Expand Up @@ -53,14 +66,15 @@ namespace mlx
#endif
}

VmaAllocation GPUallocator::createBuffer(const VkBufferCreateInfo* binfo, const VmaAllocationCreateInfo* vinfo, VkBuffer& buffer) noexcept
VmaAllocation GPUallocator::createBuffer(const VkBufferCreateInfo* binfo, const VmaAllocationCreateInfo* vinfo, VkBuffer& buffer, VmaAllocationInfo& allocinfo) noexcept
{
VmaAllocation allocation;
if(vmaCreateBuffer(_allocator, binfo, vinfo, &buffer, &allocation, nullptr) != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan : failed to allocate a buffer");
#ifdef DEBUG
core::error::report(e_kind::message, "Graphics Allocator : created new buffer");
#endif
vmaGetAllocationInfo(_allocator, allocation, &allocinfo);
return allocation;
}

Expand All @@ -69,14 +83,15 @@ namespace mlx
vmaDestroyBuffer(_allocator, buffer, allocation);
}

VmaAllocation GPUallocator::createImage(const VkImageCreateInfo* iminfo, const VmaAllocationCreateInfo* vinfo, VkImage& image) noexcept
VmaAllocation GPUallocator::createImage(const VkImageCreateInfo* iminfo, const VmaAllocationCreateInfo* vinfo, VkImage& image, VmaAllocationInfo& allocinfo) noexcept
{
VmaAllocation allocation;
if(vmaCreateImage(_allocator, iminfo, vinfo, &image, &allocation, nullptr) != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan : failed to allocate an image");
#ifdef DEBUG
core::error::report(e_kind::message, "Graphics Allocator : created new image");
#endif
vmaGetAllocationInfo(_allocator, allocation, &allocinfo);
return allocation;
}

Expand All @@ -95,6 +110,11 @@ namespace mlx
{
vmaUnmapMemory(_allocator, allocation);
}

void GPUallocator::flush(VmaAllocation allocation, VkDeviceSize size, VkDeviceSize offset) noexcept
{
vmaFlushAllocation(_allocator, allocation, offset, size);
}

void GPUallocator::destroy() noexcept
{
Expand Down
10 changes: 6 additions & 4 deletions src/renderer/core/memory.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/10/20 02:13:03 by maldavid #+# #+# */
/* Updated: 2023/11/09 19:58:06 by maldavid ### ########.fr */
/* Updated: 2023/11/10 08:14:06 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,16 +26,18 @@ namespace mlx

void init() noexcept;
void destroy() noexcept;

VmaAllocation createBuffer(const VkBufferCreateInfo* binfo, const VmaAllocationCreateInfo* vinfo, VkBuffer& buffer, VmaAllocationInfo& allocinfo) noexcept;
void destroyBuffer(VmaAllocation allocation, VkBuffer buffer) noexcept;

VmaAllocation createImage(const VkImageCreateInfo* iminfo, const VmaAllocationCreateInfo* vinfo, VkImage& image, VmaAllocationInfo& allocinfo) noexcept;
void destroyImage(VmaAllocation allocation, VkImage image) noexcept;

void mapMemory(VmaAllocation allocation, void** data) noexcept;
void unmapMemory(VmaAllocation allocation) noexcept;

void flush(VmaAllocation allocation, VkDeviceSize size, VkDeviceSize offset) noexcept;

~GPUallocator() = default;

private:
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/images/vk_image.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/25 11:59:07 by maldavid #+# #+# */
/* Updated: 2023/11/09 19:35:26 by maldavid ### ########.fr */
/* Updated: 2023/11/10 08:24:11 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -43,7 +43,7 @@ namespace mlx
VmaAllocationCreateInfo alloc_info{};
alloc_info.usage = VMA_MEMORY_USAGE_GPU_ONLY;

_allocation = Render_Core::get().getAllocator().createImage(&imageInfo, &alloc_info, _image);
_allocation = Render_Core::get().getAllocator().createImage(&imageInfo, &alloc_info, _image, _alloc_infos);

_pool.init();
}
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/images/vk_image.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/25 11:54:21 by maldavid #+# #+# */
/* Updated: 2023/11/09 19:29:54 by maldavid ### ########.fr */
/* Updated: 2023/11/10 08:23:59 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -50,6 +50,7 @@ namespace mlx
CmdBuffer _transfer_cmd;
CmdPool _pool;
VmaAllocation _allocation;
VmaAllocationInfo _alloc_infos;
VkImage _image = VK_NULL_HANDLE;
VkImageView _image_view = VK_NULL_HANDLE;
VkSampler _sampler = VK_NULL_HANDLE;
Expand Down

0 comments on commit 85c43da

Please sign in to comment.