fixing rendering issues

This commit is contained in:
Kbz-8
2023-11-14 07:07:43 +01:00
parent d29cd56044
commit ad60a17da9
25 changed files with 171 additions and 117 deletions

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/11/11 03:27:31 by maldavid ### ########.fr */
/* Updated: 2023/11/14 07:06:17 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,11 +19,9 @@
namespace mlx
{
void Buffer::create(Buffer::kind type, VkDeviceSize size, VkBufferUsageFlags usage, const void* data)
void Buffer::create(Buffer::kind type, VkDeviceSize size, VkBufferUsageFlags usage, const char* name, const void* data)
{
_usage = usage;
VmaAllocationCreateInfo alloc_info{};
alloc_info.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT;
if(type == Buffer::kind::constant)
{
if(data == nullptr)
@@ -32,22 +30,20 @@ namespace mlx
return;
}
_usage |= VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
alloc_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST;
}
else if(type == Buffer::kind::uniform)
alloc_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST;
else
alloc_info.usage = VMA_MEMORY_USAGE_AUTO;
createBuffer(_usage, alloc_info, size);
VmaAllocationCreateInfo alloc_info{};
alloc_info.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
alloc_info.usage = VMA_MEMORY_USAGE_AUTO;
if(type == Buffer::kind::constant || data != nullptr)
createBuffer(_usage, alloc_info, size, name);
if(data != nullptr)
{
void* mapped = nullptr;
mapMem(&mapped);
std::memcpy(mapped, data, size);
unmapMem();
if(type == Buffer::kind::constant)
pushToGPU();
}
@@ -60,7 +56,7 @@ namespace mlx
Render_Core::get().getAllocator().destroyBuffer(_allocation, _buffer);
}
void Buffer::createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size)
void Buffer::createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size, const char* name)
{
VkBufferCreateInfo bufferInfo{};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
@@ -68,7 +64,15 @@ namespace mlx
bufferInfo.usage = usage;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
_allocation = Render_Core::get().getAllocator().createBuffer(&bufferInfo, &info, _buffer, _alloc_infos);
_name = name;
if(usage & VK_BUFFER_USAGE_INDEX_BUFFER_BIT)
_name.append("_index_buffer");
else if(usage & VK_BUFFER_USAGE_VERTEX_BUFFER_BIT)
_name.append("_vertex_buffer");
else if((usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) != 1)
_name.append("_buffer");
_allocation = Render_Core::get().getAllocator().createBuffer(&bufferInfo, &info, _buffer, _name.c_str());
_size = size;
}
void Buffer::pushToGPU() noexcept
@@ -76,9 +80,11 @@ namespace mlx
VmaAllocationCreateInfo alloc_info{};
alloc_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
std::string new_name = _name + "_GPU";
Buffer newBuffer;
newBuffer._usage = (_usage & 0xFFFFFFFC) | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
newBuffer.createBuffer(newBuffer._usage, alloc_info, _alloc_infos.size);
newBuffer.createBuffer(newBuffer._usage, alloc_info, _size, new_name.c_str());
CmdPool cmdpool;
cmdpool.init();
@@ -100,7 +106,7 @@ namespace mlx
vkBeginCommandBuffer(commandBuffer, &beginInfo);
VkBufferCopy copyRegion{};
copyRegion.size = _alloc_infos.size;
copyRegion.size = _size;
vkCmdCopyBuffer(commandBuffer, _buffer, newBuffer._buffer, 1, &copyRegion);
vkEndCommandBuffer(commandBuffer);
@@ -128,9 +134,17 @@ namespace mlx
_buffer = buffer._buffer;
buffer._buffer = temp_b;
VmaAllocationInfo temp_i = _alloc_infos;
_alloc_infos = buffer._alloc_infos;
buffer._alloc_infos = temp_i;
VmaAllocation temp_a = buffer._allocation;
buffer._allocation = _allocation;
_allocation = temp_a;
VkDeviceSize temp_size = buffer._size;
buffer._size = _size;
_size = temp_size;
VkDeviceSize temp_offset = buffer._offset;
buffer._offset = _offset;
_offset = temp_offset;
VkBufferUsageFlags temp_u = _usage;
_usage = buffer._usage;

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/11/11 03:29:40 by maldavid ### ########.fr */
/* Updated: 2023/11/14 03:24:12 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,18 +23,18 @@ namespace mlx
public:
enum class kind { dynamic, uniform, constant };
void create(kind type, VkDeviceSize size, VkBufferUsageFlags usage, const void* data = nullptr);
void create(kind type, VkDeviceSize size, VkBufferUsageFlags usage, const char* name, const void* data = nullptr);
void destroy() noexcept;
inline void mapMem(void** data = nullptr) noexcept { Render_Core::get().getAllocator().mapMemory(_allocation, data); _is_mapped = true; }
inline void mapMem(void** data) noexcept { Render_Core::get().getAllocator().mapMemory(_allocation, data); _is_mapped = true; }
inline bool isMapped() const noexcept { return _is_mapped; }
inline void unmapMem() noexcept { Render_Core::get().getAllocator().unmapMemory(_allocation);_is_mapped = false; }
inline void unmapMem() noexcept { Render_Core::get().getAllocator().unmapMemory(_allocation); _is_mapped = false; }
void flush(VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0);
inline VkBuffer& operator()() noexcept { return _buffer; }
inline VkBuffer& get() noexcept { return _buffer; }
inline VkDeviceSize getSize() const noexcept { return _alloc_infos.size; }
inline VkDeviceSize getSize() const noexcept { return _size; }
inline VkDeviceSize getOffset() const noexcept { return _offset; }
protected:
@@ -43,14 +43,15 @@ namespace mlx
protected:
VmaAllocation _allocation;
VmaAllocationInfo _alloc_infos;
VkBuffer _buffer = VK_NULL_HANDLE;
VkDeviceSize _offset = 0;
VkDeviceSize _size = 0;
private:
void createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size);
void createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size, const char* name);
private:
std::string _name;
VkBufferUsageFlags _usage = 0;
bool _is_mapped = false;
};

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/11/11 03:08:10 by maldavid ### ########.fr */
/* Updated: 2023/11/14 03:25:59 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,8 +22,8 @@ namespace mlx
class C_IBO : public Buffer
{
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, 0, VK_INDEX_TYPE_UINT16); }
inline void create(uint32_t size, const uint16_t* data, const char* name) { Buffer::create(Buffer::kind::constant, size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, name, data); }
inline void bind(Renderer& renderer) noexcept { vkCmdBindIndexBuffer(renderer.getActiveCmdBuffer().get(), _buffer, _offset, VK_INDEX_TYPE_UINT16); }
};
}

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/11/10 07:54:48 by maldavid ### ########.fr */
/* Updated: 2023/11/14 03:27:08 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,13 +16,15 @@
namespace mlx
{
void UBO::create(Renderer* renderer, uint32_t size)
void UBO::create(Renderer* renderer, uint32_t size, const char* name)
{
_renderer = renderer;
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
{
_buffers[i].create(Buffer::kind::uniform, size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
std::string name_frame = name;
name_frame.append(std::to_string(i));
_buffers[i].create(Buffer::kind::uniform, size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, name_frame.c_str());
_buffers[i].mapMem(&_maps[i]);
if(_maps[i] == nullptr)
core::error::report(e_kind::fatal_error, "Vulkan : unable to map a uniform buffer");

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/11/10 07:54:29 by maldavid ### ########.fr */
/* Updated: 2023/11/14 03:26:10 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,7 +22,7 @@ namespace mlx
class UBO
{
public:
void create(class Renderer* renderer, uint32_t size);
void create(class Renderer* renderer, uint32_t size, const char* name);
void setData(uint32_t size, const void* data);
void setDynamicData(uint32_t size, const void* data);

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/11/11 03:22:44 by maldavid ### ########.fr */
/* Updated: 2023/11/14 04:53:36 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,7 +21,7 @@ namespace mlx
class VBO : public Buffer
{
public:
inline void create(uint32_t size) { Buffer::create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); }
inline void create(uint32_t size, const char* name) { Buffer::create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name); }
void setData(uint32_t size, const void* data);
inline void bind(Renderer& renderer) noexcept { vkCmdBindVertexBuffers(renderer.getActiveCmdBuffer().get(), 0, 1, &_buffer, &_offset); }
};
@@ -29,7 +29,7 @@ namespace mlx
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 create(uint32_t size, const void* data, const char* name) { Buffer::create(Buffer::kind::constant, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name, data); }
inline void bind(Renderer& renderer) noexcept { vkCmdBindVertexBuffers(renderer.getActiveCmdBuffer().get(), 0, 1, &_buffer, &_offset); }
};
}