Skip to content

Commit ad60a17

Browse files
committed
fixing rendering issues
1 parent d29cd56 commit ad60a17

25 files changed

+171
-117
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ #
77
# +#+#+#+#+#+ +#+ #
88
# Created: 2022/10/04 16:43:41 by maldavid #+# #+# #
9-
# Updated: 2023/11/10 23:55:12 by maldavid ### ########.fr #
9+
# Updated: 2023/11/13 06:08:16 by maldavid ### ########.fr #
1010
# #
1111
# **************************************************************************** #
1212

@@ -24,6 +24,7 @@ DEBUG ?= false
2424
TOOLCHAIN ?= clang
2525
IMAGES_OPTIMIZED ?= true
2626
FORCE_INTEGRATED_GPU ?= false
27+
GRAPHICS_MEMORY_DUMP ?= false
2728

2829
CXX = clang++
2930

@@ -52,6 +53,10 @@ ifeq ($(IMAGES_OPTIMIZED), true)
5253
CXXFLAGS += -D IMAGE_OPTIMIZED
5354
endif
5455

56+
ifeq ($(GRAPHICS_MEMORY_DUMP), true)
57+
CXXFLAGS += -D GRAPHICS_MEMORY_DUMP
58+
endif
59+
5560
RM = rm -f
5661

5762
%.o: %.cpp

src/core/application.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */
9-
/* Updated: 2023/11/10 09:06:44 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 03:20:40 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -44,7 +44,7 @@ namespace mlx::core
4444

4545
void* Application::newTexture(int w, int h)
4646
{
47-
_textures.emplace_front().create(nullptr, w, h, VK_FORMAT_R8G8B8A8_UNORM);
47+
_textures.emplace_front().create(nullptr, w, h, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_unamed_user_texture");
4848
return &_textures.front();
4949
}
5050

src/core/graphics.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */
9-
/* Updated: 2023/11/08 21:02:22 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 06:59:12 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -54,11 +54,21 @@ namespace mlx
5454
sets[1] = _text_put_pipeline->getDescriptorSet();
5555
vkCmdBindDescriptorSets(cmd_buff, VK_PIPELINE_BIND_POINT_GRAPHICS, _renderer->getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr);
5656
_text_put_pipeline->render();
57-
57+
5858
_renderer->endFrame();
5959

6060
for(auto& data : _textures_to_render)
6161
data.texture->resetUpdate();
62+
63+
#ifdef GRAPHICS_MEMORY_DUMP
64+
// dump memory to file every two seconds
65+
static uint64_t timer = SDL_GetTicks64();
66+
if(SDL_GetTicks64() - timer > 2000)
67+
{
68+
Render_Core::get().getAllocator().dumpMemoryToJson();
69+
timer += 2000;
70+
}
71+
#endif
6272
}
6373

6474
GraphicsSupport::~GraphicsSupport()

src/renderer/buffers/vk_buffer.cpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/08 18:55:57 by maldavid #+# #+# */
9-
/* Updated: 2023/11/11 03:27:31 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 07:06:17 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -19,11 +19,9 @@
1919

2020
namespace mlx
2121
{
22-
void Buffer::create(Buffer::kind type, VkDeviceSize size, VkBufferUsageFlags usage, const void* data)
22+
void Buffer::create(Buffer::kind type, VkDeviceSize size, VkBufferUsageFlags usage, const char* name, const void* data)
2323
{
2424
_usage = usage;
25-
VmaAllocationCreateInfo alloc_info{};
26-
alloc_info.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT;
2725
if(type == Buffer::kind::constant)
2826
{
2927
if(data == nullptr)
@@ -32,22 +30,20 @@ namespace mlx
3230
return;
3331
}
3432
_usage |= VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
35-
alloc_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST;
3633
}
37-
else if(type == Buffer::kind::uniform)
38-
alloc_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST;
39-
else
40-
alloc_info.usage = VMA_MEMORY_USAGE_AUTO;
4134

42-
createBuffer(_usage, alloc_info, size);
35+
VmaAllocationCreateInfo alloc_info{};
36+
alloc_info.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
37+
alloc_info.usage = VMA_MEMORY_USAGE_AUTO;
38+
39+
createBuffer(_usage, alloc_info, size, name);
4340

44-
if(type == Buffer::kind::constant || data != nullptr)
41+
if(data != nullptr)
4542
{
4643
void* mapped = nullptr;
4744
mapMem(&mapped);
4845
std::memcpy(mapped, data, size);
4946
unmapMem();
50-
5147
if(type == Buffer::kind::constant)
5248
pushToGPU();
5349
}
@@ -60,25 +56,35 @@ namespace mlx
6056
Render_Core::get().getAllocator().destroyBuffer(_allocation, _buffer);
6157
}
6258

63-
void Buffer::createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size)
59+
void Buffer::createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size, const char* name)
6460
{
6561
VkBufferCreateInfo bufferInfo{};
6662
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
6763
bufferInfo.size = size;
6864
bufferInfo.usage = usage;
6965
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
7066

71-
_allocation = Render_Core::get().getAllocator().createBuffer(&bufferInfo, &info, _buffer, _alloc_infos);
67+
_name = name;
68+
if(usage & VK_BUFFER_USAGE_INDEX_BUFFER_BIT)
69+
_name.append("_index_buffer");
70+
else if(usage & VK_BUFFER_USAGE_VERTEX_BUFFER_BIT)
71+
_name.append("_vertex_buffer");
72+
else if((usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) != 1)
73+
_name.append("_buffer");
74+
_allocation = Render_Core::get().getAllocator().createBuffer(&bufferInfo, &info, _buffer, _name.c_str());
75+
_size = size;
7276
}
7377

7478
void Buffer::pushToGPU() noexcept
7579
{
7680
VmaAllocationCreateInfo alloc_info{};
7781
alloc_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
7882

83+
std::string new_name = _name + "_GPU";
84+
7985
Buffer newBuffer;
8086
newBuffer._usage = (_usage & 0xFFFFFFFC) | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
81-
newBuffer.createBuffer(newBuffer._usage, alloc_info, _alloc_infos.size);
87+
newBuffer.createBuffer(newBuffer._usage, alloc_info, _size, new_name.c_str());
8288

8389
CmdPool cmdpool;
8490
cmdpool.init();
@@ -100,7 +106,7 @@ namespace mlx
100106
vkBeginCommandBuffer(commandBuffer, &beginInfo);
101107

102108
VkBufferCopy copyRegion{};
103-
copyRegion.size = _alloc_infos.size;
109+
copyRegion.size = _size;
104110
vkCmdCopyBuffer(commandBuffer, _buffer, newBuffer._buffer, 1, &copyRegion);
105111

106112
vkEndCommandBuffer(commandBuffer);
@@ -128,9 +134,17 @@ namespace mlx
128134
_buffer = buffer._buffer;
129135
buffer._buffer = temp_b;
130136

131-
VmaAllocationInfo temp_i = _alloc_infos;
132-
_alloc_infos = buffer._alloc_infos;
133-
buffer._alloc_infos = temp_i;
137+
VmaAllocation temp_a = buffer._allocation;
138+
buffer._allocation = _allocation;
139+
_allocation = temp_a;
140+
141+
VkDeviceSize temp_size = buffer._size;
142+
buffer._size = _size;
143+
_size = temp_size;
144+
145+
VkDeviceSize temp_offset = buffer._offset;
146+
buffer._offset = _offset;
147+
_offset = temp_offset;
134148

135149
VkBufferUsageFlags temp_u = _usage;
136150
_usage = buffer._usage;

src/renderer/buffers/vk_buffer.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/06 23:18:52 by maldavid #+# #+# */
9-
/* Updated: 2023/11/11 03:29:40 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 03:24:12 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -23,18 +23,18 @@ namespace mlx
2323
public:
2424
enum class kind { dynamic, uniform, constant };
2525

26-
void create(kind type, VkDeviceSize size, VkBufferUsageFlags usage, const void* data = nullptr);
26+
void create(kind type, VkDeviceSize size, VkBufferUsageFlags usage, const char* name, const void* data = nullptr);
2727
void destroy() noexcept;
2828

29-
inline void mapMem(void** data = nullptr) noexcept { Render_Core::get().getAllocator().mapMemory(_allocation, data); _is_mapped = true; }
29+
inline void mapMem(void** data) noexcept { Render_Core::get().getAllocator().mapMemory(_allocation, data); _is_mapped = true; }
3030
inline bool isMapped() const noexcept { return _is_mapped; }
31-
inline void unmapMem() noexcept { Render_Core::get().getAllocator().unmapMemory(_allocation);_is_mapped = false; }
31+
inline void unmapMem() noexcept { Render_Core::get().getAllocator().unmapMemory(_allocation); _is_mapped = false; }
3232

3333
void flush(VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0);
3434

3535
inline VkBuffer& operator()() noexcept { return _buffer; }
3636
inline VkBuffer& get() noexcept { return _buffer; }
37-
inline VkDeviceSize getSize() const noexcept { return _alloc_infos.size; }
37+
inline VkDeviceSize getSize() const noexcept { return _size; }
3838
inline VkDeviceSize getOffset() const noexcept { return _offset; }
3939

4040
protected:
@@ -43,14 +43,15 @@ namespace mlx
4343

4444
protected:
4545
VmaAllocation _allocation;
46-
VmaAllocationInfo _alloc_infos;
4746
VkBuffer _buffer = VK_NULL_HANDLE;
4847
VkDeviceSize _offset = 0;
48+
VkDeviceSize _size = 0;
4949

5050
private:
51-
void createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size);
51+
void createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size, const char* name);
5252

5353
private:
54+
std::string _name;
5455
VkBufferUsageFlags _usage = 0;
5556
bool _is_mapped = false;
5657
};

src/renderer/buffers/vk_ibo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/25 15:05:05 by maldavid #+# #+# */
9-
/* Updated: 2023/11/11 03:08:10 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 03:25:59 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -22,8 +22,8 @@ namespace mlx
2222
class C_IBO : public Buffer
2323
{
2424
public:
25-
inline void create(uint32_t size, const uint16_t* data) { Buffer::create(Buffer::kind::constant, size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, data); }
26-
inline void bind(Renderer& renderer) noexcept { vkCmdBindIndexBuffer(renderer.getActiveCmdBuffer().get(), _buffer, 0, VK_INDEX_TYPE_UINT16); }
25+
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); }
26+
inline void bind(Renderer& renderer) noexcept { vkCmdBindIndexBuffer(renderer.getActiveCmdBuffer().get(), _buffer, _offset, VK_INDEX_TYPE_UINT16); }
2727
};
2828
}
2929

src/renderer/buffers/vk_ubo.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/06 18:45:52 by maldavid #+# #+# */
9-
/* Updated: 2023/11/10 07:54:48 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 03:27:08 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -16,13 +16,15 @@
1616

1717
namespace mlx
1818
{
19-
void UBO::create(Renderer* renderer, uint32_t size)
19+
void UBO::create(Renderer* renderer, uint32_t size, const char* name)
2020
{
2121
_renderer = renderer;
2222

2323
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
2424
{
25-
_buffers[i].create(Buffer::kind::uniform, size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
25+
std::string name_frame = name;
26+
name_frame.append(std::to_string(i));
27+
_buffers[i].create(Buffer::kind::uniform, size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, name_frame.c_str());
2628
_buffers[i].mapMem(&_maps[i]);
2729
if(_maps[i] == nullptr)
2830
core::error::report(e_kind::fatal_error, "Vulkan : unable to map a uniform buffer");

src/renderer/buffers/vk_ubo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/06 18:45:29 by maldavid #+# #+# */
9-
/* Updated: 2023/11/10 07:54:29 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 03:26:10 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -22,7 +22,7 @@ namespace mlx
2222
class UBO
2323
{
2424
public:
25-
void create(class Renderer* renderer, uint32_t size);
25+
void create(class Renderer* renderer, uint32_t size, const char* name);
2626

2727
void setData(uint32_t size, const void* data);
2828
void setDynamicData(uint32_t size, const void* data);

src/renderer/buffers/vk_vbo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/06 18:27:38 by maldavid #+# #+# */
9-
/* Updated: 2023/11/11 03:22:44 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/14 04:53:36 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -21,15 +21,15 @@ namespace mlx
2121
class VBO : public Buffer
2222
{
2323
public:
24-
inline void create(uint32_t size) { Buffer::create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); }
24+
inline void create(uint32_t size, const char* name) { Buffer::create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name); }
2525
void setData(uint32_t size, const void* data);
2626
inline void bind(Renderer& renderer) noexcept { vkCmdBindVertexBuffers(renderer.getActiveCmdBuffer().get(), 0, 1, &_buffer, &_offset); }
2727
};
2828

2929
class C_VBO : public Buffer
3030
{
3131
public:
32-
inline void create(uint32_t size, const void* data) { Buffer::create(Buffer::kind::constant, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, data); }
32+
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); }
3333
inline void bind(Renderer& renderer) noexcept { vkCmdBindVertexBuffers(renderer.getActiveCmdBuffer().get(), 0, 1, &_buffer, &_offset); }
3434
};
3535
}

0 commit comments

Comments
 (0)