mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-11 14:43:34 +00:00
adding image optimizations control over make call
This commit is contained in:
9
Makefile
9
Makefile
@@ -3,10 +3,10 @@
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ #
|
||||
# By: vvaas <vvaas@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/10/04 16:43:41 by maldavid #+# #+# #
|
||||
# Updated: 2023/06/06 15:59:27 by maldavid ### ########.fr #
|
||||
# Updated: 2023/08/09 13:44:58 by maldavid ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@@ -21,6 +21,7 @@ OBJS = $(SRCS:.cpp=.o)
|
||||
|
||||
DEBUG ?= false
|
||||
TOOLCHAIN ?= clang
|
||||
IMAGES_OPTIMIZED ?= true
|
||||
|
||||
CXX = clang++
|
||||
|
||||
@@ -35,6 +36,10 @@ ifeq ($(DEBUG), true)
|
||||
CXXFLAGS += -g -D DEBUG
|
||||
endif
|
||||
|
||||
ifeq ($(IMAGES_OPTIMIZED), true)
|
||||
CXXFLAGS += -D IMAGE_OPTIMIZED
|
||||
endif
|
||||
|
||||
RM = rm -f
|
||||
|
||||
%.o: %.cpp
|
||||
|
||||
10
README.md
10
README.md
@@ -36,3 +36,13 @@ Dependances :
|
||||
```bash
|
||||
clang myApp.c MacroLibX/libmlx.so -lSDL2
|
||||
```
|
||||
|
||||
## /!\ If you run into glitches when writing or reading pixels from images /!\
|
||||
|
||||
You need to add `IMAGES_OPTIMIZED=false` to your make mlx command
|
||||
|
||||
```bash
|
||||
~ git clone https://github.com/420verfl0w/MacroLibX.git
|
||||
~ cd MacroLibX
|
||||
~ make IMAGES_OPTIMIZED=false
|
||||
```
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/31 18:03:35 by maldavid #+# #+# */
|
||||
/* Updated: 2023/06/06 16:05:02 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/08/09 13:42:48 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -14,15 +14,24 @@
|
||||
#include <renderer/images/texture.h>
|
||||
#include <renderer/buffers/vk_buffer.h>
|
||||
#include <renderer/renderer.h>
|
||||
#include <cstring>
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#ifdef IMAGE_OPTIMIZED
|
||||
#define TILING VK_IMAGE_TILING_OPTIMAL
|
||||
#else
|
||||
#define TILING VK_IMAGE_TILING_LINEAR
|
||||
#endif
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void Texture::create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format)
|
||||
{
|
||||
Image::create(width, height, format, VK_IMAGE_TILING_OPTIMAL,
|
||||
Image::create(width, height, format, TILING,
|
||||
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
|
||||
{ VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT }
|
||||
);
|
||||
@@ -56,10 +65,9 @@ namespace mlx
|
||||
{
|
||||
if(x < 0 || y < 0 || x > getWidth() || y > getHeight())
|
||||
return;
|
||||
if(_cpu_map == nullptr)
|
||||
if(_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;
|
||||
_cpu_map[(y * getWidth()) + x] = color;
|
||||
_has_been_modified = true;
|
||||
}
|
||||
|
||||
@@ -67,18 +75,15 @@ namespace mlx
|
||||
{
|
||||
if(x < 0 || y < 0 || x > getWidth() || y > getHeight())
|
||||
return 0;
|
||||
if(_cpu_map == nullptr)
|
||||
if(_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;
|
||||
uint32_t color = _cpu_map[(y * getWidth()) + x];
|
||||
return (color);
|
||||
}
|
||||
|
||||
void Texture::openCPUmap()
|
||||
{
|
||||
if(_cpu_map != nullptr)
|
||||
if(_map != nullptr)
|
||||
return;
|
||||
|
||||
#ifdef DEBUG
|
||||
@@ -89,7 +94,9 @@ namespace mlx
|
||||
_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);
|
||||
_buf_map->mapMem(&_map);
|
||||
_cpu_map = std::vector<uint32_t>(getWidth() * getHeight(), 0);
|
||||
std::memcpy(_cpu_map.data(), _map, size);
|
||||
#ifdef DEBUG
|
||||
core::error::report(e_kind::message, "Texture : mapped CPU memory using staging buffer");
|
||||
#endif
|
||||
@@ -99,6 +106,7 @@ namespace mlx
|
||||
{
|
||||
if(_has_been_modified)
|
||||
{
|
||||
std::memcpy(_map, _cpu_map.data(), _cpu_map.size() * formatSize(getFormat()));
|
||||
Image::copyFromBuffer(*_buf_map);
|
||||
_has_been_modified = false;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 02:24:58 by maldavid #+# #+# */
|
||||
/* Updated: 2023/06/06 15:54:44 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/08/02 12:32:27 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -51,8 +51,9 @@ namespace mlx
|
||||
C_VBO _vbo;
|
||||
C_IBO _ibo;
|
||||
DescriptorSet _set;
|
||||
std::vector<uint32_t> _cpu_map;
|
||||
std::optional<Buffer> _buf_map = std::nullopt;
|
||||
void* _cpu_map = nullptr;
|
||||
void* _map = nullptr;
|
||||
bool _has_been_modified = false;
|
||||
bool _has_been_updated = false;
|
||||
};
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/31 15:14:50 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/23 15:24:37 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/08/02 05:28:49 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -21,29 +21,23 @@ namespace mlx
|
||||
_texture.setDescriptor(renderer.getFragDescriptorSet().duplicate());
|
||||
|
||||
_buffer.create(Buffer::kind::dynamic, sizeof(uint32_t) * (width * height), VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
|
||||
_buffer.mapMem(&_map);
|
||||
_buffer.mapMem(&_buffer_map);
|
||||
_cpu_map = std::vector<uint32_t>(height * width, 0);
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
|
||||
VkDescriptorSet PixelPutPipeline::getDescriptorSet() noexcept
|
||||
{
|
||||
return _texture.getSet();
|
||||
}
|
||||
|
||||
void PixelPutPipeline::setPixel(uint32_t x, uint32_t y, uint32_t color) noexcept
|
||||
{
|
||||
if(x < 0 || y < 0 || x > _width || y > _height)
|
||||
return;
|
||||
unsigned char* mem = static_cast<unsigned char*>(_map) + (y * _width * sizeof(uint32_t)) + (x * sizeof(uint32_t));
|
||||
*reinterpret_cast<uint32_t*>(mem) = color;
|
||||
_cpu_map[(y * _width) + x] = color;
|
||||
_has_been_modified = true;
|
||||
}
|
||||
|
||||
void PixelPutPipeline::clear()
|
||||
{
|
||||
unsigned char* mem = static_cast<unsigned char*>(_map);
|
||||
std::memset(mem, 0, sizeof(uint32_t) * (_width * _height));
|
||||
_cpu_map.assign(_width * _height, 0);
|
||||
_has_been_modified = true;
|
||||
}
|
||||
|
||||
@@ -51,6 +45,7 @@ namespace mlx
|
||||
{
|
||||
if(_has_been_modified)
|
||||
{
|
||||
std::memcpy(_buffer_map, _cpu_map.data(), sizeof(uint32_t) * _cpu_map.size());
|
||||
_texture.copyFromBuffer(_buffer);
|
||||
_has_been_modified = false;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/31 13:18:50 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/19 11:32:34 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/08/02 05:27:27 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -28,10 +28,9 @@ namespace mlx
|
||||
void setPixel(uint32_t x, uint32_t y, uint32_t color) noexcept;
|
||||
void present() noexcept;
|
||||
void render(class Renderer& renderer) noexcept;
|
||||
VkDescriptorSet getDescriptorSet() noexcept;
|
||||
inline VkDescriptorSet getDescriptorSet() noexcept { return _texture.getSet(); }
|
||||
|
||||
void clear();
|
||||
|
||||
void destroy() noexcept;
|
||||
|
||||
~PixelPutPipeline();
|
||||
@@ -39,7 +38,9 @@ namespace mlx
|
||||
private:
|
||||
Texture _texture;
|
||||
Buffer _buffer;
|
||||
void* _map = nullptr;
|
||||
// using vector as CPU map and not directly writting to mapped buffer to improve performances
|
||||
std::vector<uint32_t> _cpu_map;
|
||||
void* _buffer_map = nullptr;
|
||||
uint32_t _width = 0;
|
||||
uint32_t _height = 0;
|
||||
bool _has_been_modified = true;
|
||||
|
||||
12
test/main.c
12
test/main.c
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */
|
||||
/* Updated: 2023/06/06 16:22:26 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/08/02 12:36:11 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -57,12 +57,12 @@ void *create_image(t_mlx *mlx)
|
||||
{
|
||||
if (i[0] < 10000 || i[0] > 20000)
|
||||
{
|
||||
pixel[i[0] + 0] = 0xFF;
|
||||
pixel[i[0] + 1] = i[0];
|
||||
pixel[i[0] + 2] = 0x00;
|
||||
pixel[i[0] + 3] = 0xFF;
|
||||
pixel[0] = i[0];
|
||||
pixel[1] = i[1];
|
||||
pixel[2] = i[2];
|
||||
pixel[3] = 0xFF;
|
||||
mlx_set_image_pixel(mlx->mlx, img, i[1], i[2], *((int *)pixel));
|
||||
}
|
||||
mlx_set_image_pixel(mlx->mlx, img, i[1], i[2], *((int *)pixel));
|
||||
i[0] += 4;
|
||||
i[1]++;
|
||||
if (i[1] >= 100)
|
||||
|
||||
@@ -1 +1 @@
|
||||
clang main.c ../libmlx.so `/nfs/homes/maldavid/.xmake/packages/l/libsdl/2.26.4/8dfbcb8049e744a597cd5333e1b399cd/bin/sdl2-config --cflags --libs` -g && ./a.out
|
||||
clang main.c ../libmlx.so -lSDL2 -g && ./a.out
|
||||
|
||||
Reference in New Issue
Block a user