diff --git a/.gitignore b/.gitignore index 6504cdb..f368165 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ *.a *.so *.out +.cache/ diff --git a/src/core/application.cpp b/src/core/application.cpp index 9a39781..adb3ccf 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ #include #include #include +#include namespace mlx::core { diff --git a/src/core/profile.h b/src/core/profile.h new file mode 100644 index 0000000..c5e45a2 --- /dev/null +++ b/src/core/profile.h @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* profile.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include + +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 diff --git a/src/renderer/buffers/vk_buffer.cpp b/src/renderer/buffers/vk_buffer.cpp index e8ba944..21bdeff 100644 --- a/src/renderer/buffers/vk_buffer.cpp +++ b/src/renderer/buffers/vk_buffer.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -140,5 +140,6 @@ namespace mlx void Buffer::flush(VkDeviceSize size, VkDeviceSize offset) { + Render_Core::get().getAllocator().flush(_allocation, size, offset); } } diff --git a/src/renderer/buffers/vk_ibo.h b/src/renderer/buffers/vk_ibo.h index 7312060..d986fa3 100644 --- a/src/renderer/buffers/vk_ibo.h +++ b/src/renderer/buffers/vk_ibo.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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); } }; } diff --git a/src/renderer/buffers/vk_ubo.cpp b/src/renderer/buffers/vk_ubo.cpp index f8d0a55..48506e4 100644 --- a/src/renderer/buffers/vk_ubo.cpp +++ b/src/renderer/buffers/vk_ubo.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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(); diff --git a/src/renderer/buffers/vk_ubo.h b/src/renderer/buffers/vk_ubo.h index b31e63d..f527dd4 100644 --- a/src/renderer/buffers/vk_ubo.h +++ b/src/renderer/buffers/vk_ubo.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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(); } diff --git a/src/renderer/buffers/vk_vbo.cpp b/src/renderer/buffers/vk_vbo.cpp index c20cb47..1b2a0d3 100644 --- a/src/renderer/buffers/vk_vbo.cpp +++ b/src/renderer/buffers/vk_vbo.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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"); diff --git a/src/renderer/buffers/vk_vbo.h b/src/renderer/buffers/vk_vbo.h index ebadecf..d4c9a61 100644 --- a/src/renderer/buffers/vk_vbo.h +++ b/src/renderer/buffers/vk_vbo.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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); } }; } diff --git a/src/renderer/core/memory.cpp b/src/renderer/core/memory.cpp index a442414..3a36be9 100644 --- a/src/renderer/core/memory.cpp +++ b/src/renderer/core/memory.cpp @@ -6,14 +6,27 @@ /* By: kbz_8 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 +#include + #define VMA_STATIC_VULKAN_FUNCTIONS 0 #define VMA_DYNAMIC_VULKAN_FUNCTIONS 0 #define VMA_VULKAN_VERSION 1002000 -#include +#define VMA_IMPLEMENTATION + +#ifdef MLX_COMPILER_CLANG + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wnullability-completeness" + #include + #pragma clang diagnostic pop +#else + #include +#endif + #include namespace mlx @@ -53,7 +66,7 @@ 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) @@ -61,6 +74,7 @@ namespace mlx #ifdef DEBUG core::error::report(e_kind::message, "Graphics Allocator : created new buffer"); #endif + vmaGetAllocationInfo(_allocator, allocation, &allocinfo); return allocation; } @@ -69,7 +83,7 @@ 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) @@ -77,6 +91,7 @@ namespace mlx #ifdef DEBUG core::error::report(e_kind::message, "Graphics Allocator : created new image"); #endif + vmaGetAllocationInfo(_allocator, allocation, &allocinfo); return allocation; } @@ -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 { diff --git a/src/renderer/core/memory.h b/src/renderer/core/memory.h index 3df2887..20a62a7 100644 --- a/src/renderer/core/memory.h +++ b/src/renderer/core/memory.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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: diff --git a/src/renderer/images/vk_image.cpp b/src/renderer/images/vk_image.cpp index ac2ae70..890ddb0 100644 --- a/src/renderer/images/vk_image.cpp +++ b/src/renderer/images/vk_image.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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(); } diff --git a/src/renderer/images/vk_image.h b/src/renderer/images/vk_image.h index cb7bae0..a4b315a 100644 --- a/src/renderer/images/vk_image.h +++ b/src/renderer/images/vk_image.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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;