This commit is contained in:
2023-06-06 16:35:08 +02:00
parent 6a8d55dbe8
commit f6fcb214e5
6 changed files with 122 additions and 46 deletions

View File

@@ -6,7 +6,7 @@
# By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ # # By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2022/10/04 16:43:41 by maldavid #+# #+# # # Created: 2022/10/04 16:43:41 by maldavid #+# #+# #
# Updated: 2023/04/22 20:28:48 by maldavid ### ########.fr # # Updated: 2023/06/06 15:59:27 by maldavid ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@@ -29,7 +29,7 @@ ifeq ($(TOOLCHAIN), gcc)
endif endif
CXXFLAGS = -std=c++17 -O3 -fPIC CXXFLAGS = -std=c++17 -O3 -fPIC
INCLUDES = -I./includes -I./src -I./third_party INCLUDES = -I./includes -I./src -I./third_party -I ~/.xmake/packages/l/libsdl/2.26.4/8dfbcb8049e744a597cd5333e1b399cd/include
ifeq ($(DEBUG), true) ifeq ($(DEBUG), true)
CXXFLAGS += -g -D DEBUG CXXFLAGS += -g -D DEBUG

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/31 18:03:35 by maldavid #+# #+# */ /* Created: 2023/03/31 18:03:35 by maldavid #+# #+# */
/* Updated: 2023/04/25 22:13:45 by maldavid ### ########.fr */ /* Updated: 2023/06/06 16:05:02 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -52,49 +52,56 @@ namespace mlx
} }
} }
void Texture::setPixel() void Texture::setPixel(int x, int y, uint32_t color) noexcept
{
if(x < 0 || y < 0 || x > getWidth() || y > getHeight())
return;
if(_cpu_map == nullptr)
openCPUmap();
unsigned char* mem = static_cast<unsigned char*>(_cpu_map) + (y * getWidth() * formatSize(getFormat())) + (x * formatSize(getFormat()));
*reinterpret_cast<uint32_t*>(mem) = color;
_has_been_modified = true;
}
void* Texture::openCPUmap() int Texture::getPixel(int x, int y) noexcept
{
if(x < 0 || y < 0 || x > getWidth() || y > getHeight())
return 0;
if(_cpu_map == nullptr)
openCPUmap();
uint32_t color = 0;
unsigned char* mem = static_cast<unsigned char*>(_cpu_map) + (y * getWidth() * formatSize(getFormat())) + (x * formatSize(getFormat()));
color = *reinterpret_cast<uint32_t*>(mem);
color >>= 8;
return (color);
}
void Texture::openCPUmap()
{ {
if(_cpu_map != nullptr) if(_cpu_map != nullptr)
return _cpu_map; return;
#ifdef DEBUG #ifdef DEBUG
core::error::report(e_kind::message, "Texture : enabling CPU mapping"); core::error::report(e_kind::message, "Texture : enabling CPU mapping");
#endif #endif
Buffer staging_buffer;
std::size_t size = getWidth() * getHeight() * formatSize(getFormat()); std::size_t size = getWidth() * getHeight() * formatSize(getFormat());
staging_buffer.create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, nullptr); _buf_map.emplace();
Image::copyToBuffer(staging_buffer); _buf_map->create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
Image::copyToBuffer(*_buf_map);
this->destroy(); _buf_map->mapMem(&_cpu_map);
this->create(nullptr, getWidth(), getHeight(), getFormat(), true);
Image::copyFromBuffer(staging_buffer);
staging_buffer.destroy();
if(vkMapMemory(Render_Core::get().getDevice().get(), getDeviceMemory(), 0, VK_WHOLE_SIZE, 0, &_cpu_map) != VK_SUCCESS)
{
_buf_map.emplace();
_buf_map->create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
Image::copyToBuffer(*_buf_map);
_buf_map->mapMem(&_cpu_map);
#ifdef DEBUG
core::error::report(e_kind::message, "Texture : mapped CPU memory using staging buffer");
#endif
}
#ifdef DEBUG #ifdef DEBUG
else core::error::report(e_kind::message, "Texture : mapped CPU memory using staging buffer");
core::error::report(e_kind::message, "Texture : mapped CPU memory using direct memory mapping");
#endif #endif
return _cpu_map;
} }
void Texture::render(Renderer& renderer, int x, int y) void Texture::render(Renderer& renderer, int x, int y)
{ {
if(_buf_map.has_value()) if(_has_been_modified)
{
Image::copyFromBuffer(*_buf_map); Image::copyFromBuffer(*_buf_map);
_has_been_modified = false;
}
auto cmd = renderer.getActiveCmdBuffer().get(); auto cmd = renderer.getActiveCmdBuffer().get();
_vbo.bind(renderer); _vbo.bind(renderer);
_ibo.bind(renderer); _ibo.bind(renderer);

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 02:24:58 by maldavid #+# #+# */ /* Created: 2023/03/08 02:24:58 by maldavid #+# #+# */
/* Updated: 2023/04/25 20:10:44 by maldavid ### ########.fr */ /* Updated: 2023/06/06 15:54:44 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -44,12 +44,16 @@ namespace mlx
~Texture() = default; ~Texture() = default;
private:
void openCPUmap();
private: private:
C_VBO _vbo; C_VBO _vbo;
C_IBO _ibo; C_IBO _ibo;
DescriptorSet _set; DescriptorSet _set;
std::optional<Buffer> _buf_map = std::nullopt; std::optional<Buffer> _buf_map = std::nullopt;
void* _cpu_map = nullptr; void* _cpu_map = nullptr;
bool _has_been_modified = false;
bool _has_been_updated = false; bool _has_been_updated = false;
}; };

59
test/.gdb_history git.filemode.normal_file
View File

@@ -0,0 +1,59 @@
run
a
q
run
bt
q
run
a
q
run
bt
a
y
q
run
p (i / 400) * 100
q
run
bt
q
run
bt
q
b create_image
run
tui n
n
n
n
n
n
n
n
n
s
n
n
s
s
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
s
s
q

View File

@@ -6,11 +6,12 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */ /* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */
/* Updated: 2023/04/19 11:54:51 by maldavid ### ########.fr */ /* Updated: 2023/06/06 16:22:26 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "../includes/mlx.h" #include "../includes/mlx.h"
typedef struct s_mlx typedef struct s_mlx
@@ -46,24 +47,29 @@ int update(t_mlx *mlx)
void *create_image(t_mlx *mlx) void *create_image(t_mlx *mlx)
{ {
int ignore[3]; unsigned char pixel[4];
void *img; int i[3];
char *addr; void *img;
int i;
memset(i, 0, sizeof(int) * 3);
img = mlx_new_image(mlx->mlx, 100, 100); img = mlx_new_image(mlx->mlx, 100, 100);
addr = mlx_get_data_addr(mlx->mlx, img, &ignore[0], &ignore[1], &ignore[2]); while (i[0] < (100 * 100) * 4)
i = 0;
while (i < (100 * 100) * 4)
{ {
if (i < 10000 || i > 20000) if (i[0] < 10000 || i[0] > 20000)
{ {
addr[i + 0] = 0xFF; pixel[i[0] + 0] = 0xFF;
addr[i + 1] = i; pixel[i[0] + 1] = i[0];
addr[i + 2] = 0x00; pixel[i[0] + 2] = 0x00;
addr[i + 3] = 0xFF; pixel[i[0] + 3] = 0xFF;
}
mlx_set_image_pixel(mlx->mlx, img, i[1], i[2], *((int *)pixel));
i[0] += 4;
i[1]++;
if (i[1] >= 100)
{
i[1] = 0;
i[2]++;
} }
i += 4;
} }
return (img); return (img);
} }

View File

@@ -1 +1 @@
clang main.c ../libmlx.so -lSDL2 -g && ./a.out clang main.c ../libmlx.so `/nfs/homes/maldavid/.xmake/packages/l/libsdl/2.26.4/8dfbcb8049e744a597cd5333e1b399cd/bin/sdl2-config --cflags --libs` -g && ./a.out