working on endianness with vulkan images

This commit is contained in:
2024-12-17 00:11:21 +01:00
parent a85ad6378e
commit c636229875
12 changed files with 162 additions and 109 deletions

View File

@@ -126,7 +126,7 @@ extern "C"
gs->GetWindow()->GetSize(x, y);
}
void mlx_clear_window(mlx_context mlx, mlx_window win, int color)
void mlx_clear_window(mlx_context mlx, mlx_window win, mlx_color color)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
@@ -201,7 +201,7 @@ extern "C"
mlx->app->OnEvent(win, static_cast<int>(event), funct_ptr, param);
}
void mlx_pixel_put(mlx_context mlx, mlx_window win, int x, int y, int color)
void mlx_pixel_put(mlx_context mlx, mlx_window win, int x, int y, mlx_color color)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
@@ -248,16 +248,16 @@ extern "C"
mlx->app->DestroyTexture(image);
}
int mlx_get_image_pixel(mlx_context mlx, mlx_image image, int x, int y)
mlx_color mlx_get_image_pixel(mlx_context mlx, mlx_image image, int x, int y)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
if(!texture)
return 0;
return { .rgba = 0x00000000 };
return texture->GetPixel(x, y);
}
void mlx_set_image_pixel(mlx_context mlx, mlx_image image, int x, int y, int color)
void mlx_set_image_pixel(mlx_context mlx, mlx_image image, int x, int y, mlx_color color)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
@@ -278,7 +278,7 @@ extern "C"
gs->TexturePut(texture, x, y, 1.0f, 1.0f, 0.0f);
}
void mlx_string_put(mlx_context mlx, mlx_window win, int x, int y, int color, char* str)
void mlx_string_put(mlx_context mlx, mlx_window win, int x, int y, mlx_color color, char* str)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
@@ -381,7 +381,7 @@ extern "C"
gs->GetWindow()->Restore();
}
void mlx_pixel_put_array(mlx_context mlx, mlx_window win, int x, int y, int* pixels, size_t pixels_size)
void mlx_pixel_put_array(mlx_context mlx, mlx_window win, int x, int y, mlx_color* pixels, size_t pixels_size)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
@@ -390,7 +390,7 @@ extern "C"
gs->PixelPutArray(x, y, pixels, pixels_size);
}
void mlx_pixel_put_region(mlx_context mlx, mlx_window win, int x, int y, int w, int h, int* pixels)
void mlx_pixel_put_region(mlx_context mlx, mlx_window win, int x, int y, int w, int h, mlx_color* pixels)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
@@ -399,7 +399,7 @@ extern "C"
gs->PixelPutRegion(x, y, w, h, pixels);
}
void mlx_get_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, int* dst)
void mlx_get_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, mlx_color* dst)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
@@ -408,7 +408,7 @@ extern "C"
texture->GetRegion(x, y, w, h, dst);
}
void mlx_set_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, int* pixels)
void mlx_set_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, mlx_color* pixels)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);

View File

@@ -2,21 +2,10 @@
#include <Core/SDLManager.h>
#include <Core/Memory.h>
#include <Embedded/IconMlx.h>
#include <Utils/Bits.h>
namespace mlx
{
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
constexpr const std::uint32_t rmask = 0xff000000;
constexpr const std::uint32_t gmask = 0x00ff0000;
constexpr const std::uint32_t bmask = 0x0000ff00;
constexpr const std::uint32_t amask = 0x000000ff;
#else
constexpr const std::uint32_t rmask = 0x000000ff;
constexpr const std::uint32_t gmask = 0x0000ff00;
constexpr const std::uint32_t bmask = 0x00ff0000;
constexpr const std::uint32_t amask = 0xff000000;
#endif
namespace Internal
{
struct WindowInfos
@@ -65,7 +54,7 @@ namespace mlx
infos->window = SDL_CreateWindow(info->title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, info->width, info->height, flags);
if(!infos->window)
FatalError("SDL: unable to open a new window; %", SDL_GetError());
infos->icon = SDL_CreateRGBSurfaceFrom(static_cast<void*>(logo_mlx), logo_mlx_width, logo_mlx_height, 32, 4 * logo_mlx_width, rmask, gmask, bmask, amask);
infos->icon = SDL_CreateRGBSurfaceFrom(static_cast<void*>(logo_mlx), logo_mlx_width, logo_mlx_height, 32, 4 * logo_mlx_width, Rmask(), Gmask(), Bmask(), Amask());
SDL_SetWindowIcon(infos->window, infos->icon);
m_windows_registry.insert(infos);

View File

@@ -2,10 +2,11 @@
#include <Graphics/PutPixelManager.h>
#include <Renderer/Renderer.h>
#include <Utils/Bits.h>
namespace mlx
{
NonOwningPtr<Texture> PutPixelManager::DrawPixel(int x, int y, std::uint64_t draw_layer, int color)
NonOwningPtr<Texture> PutPixelManager::DrawPixel(int x, int y, std::uint64_t draw_layer, mlx_color color)
{
MLX_PROFILE_FUNCTION();
bool is_newlayer;
@@ -16,7 +17,7 @@ namespace mlx
return (is_newlayer ? layer : nullptr);
}
NonOwningPtr<Texture> PutPixelManager::DrawPixelsArray(int x, int y, std::uint64_t draw_layer, int* pixels, std::size_t pixels_size)
NonOwningPtr<Texture> PutPixelManager::DrawPixelsArray(int x, int y, std::uint64_t draw_layer, mlx_color* pixels, std::size_t pixels_size)
{
MLX_PROFILE_FUNCTION();
bool is_newlayer;
@@ -27,7 +28,7 @@ namespace mlx
return (is_newlayer ? layer : nullptr);
}
NonOwningPtr<Texture> PutPixelManager::DrawPixelsRegion(int x, int y, int w, int h, std::uint64_t draw_layer, int* pixels)
NonOwningPtr<Texture> PutPixelManager::DrawPixelsRegion(int x, int y, int w, int h, std::uint64_t draw_layer, mlx_color* pixels)
{
MLX_PROFILE_FUNCTION();
bool is_newlayer;
@@ -70,7 +71,7 @@ namespace mlx
try
{
m_placements[draw_layer] = m_textures.at(m_current_texture_index).get();
m_textures.at(m_current_texture_index)->Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
m_textures.at(m_current_texture_index)->Clear(VK_NULL_HANDLE, Vec4f{ 0.f });
NonOwningPtr<Texture> texture = m_textures.at(m_current_texture_index).get();
m_current_texture_index++;
return texture;

View File

@@ -5,7 +5,6 @@
#include <Renderer/RenderCore.h>
#include <Utils/CallOnExit.h>
#include <Core/Memory.h>
#include <Utils/Bits.h>
#define STB_IMAGE_IMPLEMENTATION
@@ -200,20 +199,21 @@ namespace mlx
Image::Destroy();
}
void Texture::SetPixel(int x, int y, int color) noexcept
void Texture::SetPixel(int x, int y, mlx_color color) noexcept
{
MLX_PROFILE_FUNCTION();
if(x < 0 || y < 0 || static_cast<std::uint32_t>(x) > m_width || static_cast<std::uint32_t>(y) > m_height)
return;
if(!m_staging_buffer.has_value())
OpenCPUBuffer();
// Needs to reverse bytes order because why not
color = ByteSwap(color);
m_cpu_buffer[(y * m_width) + x] = color;
if constexpr(std::endian::native == std::endian::little)
m_cpu_buffer[(y * m_width) + x] = mlx_color{ .r = color.a, .g = color.b, .b = color.g, .a = color.r };
else
m_cpu_buffer[(y * m_width) + x] = color;
m_has_been_modified = true;
}
void Texture::SetRegion(int x, int y, int w, int h, int* pixels) noexcept
void Texture::SetRegion(int x, int y, int w, int h, mlx_color* pixels) noexcept
{
MLX_PROFILE_FUNCTION();
if(x < 0 || y < 0 || static_cast<std::uint32_t>(x) > m_width || static_cast<std::uint32_t>(y) > m_height)
@@ -231,40 +231,48 @@ namespace mlx
break;
moving_y++;
}
// Needs to reverse bytes order because why not
int color = ByteSwap(pixels[i]);
m_cpu_buffer[(moving_y * m_width) + moving_x] = color;
if constexpr(std::endian::native == std::endian::little)
m_cpu_buffer[(moving_y * m_width) + moving_x] = mlx_color{ .r = pixels[i].a, .g = pixels[i].b, .b = pixels[i].g, .a = pixels[i].r };
else
m_cpu_buffer[(moving_y * m_width) + moving_x] = pixels[i];
}
m_has_been_modified = true;
}
void Texture::SetLinearRegion(int x, int y, std::size_t len, int* pixels) noexcept
void Texture::SetLinearRegion(int x, int y, std::size_t len, mlx_color* pixels) noexcept
{
MLX_PROFILE_FUNCTION();
if(x < 0 || y < 0 || static_cast<std::uint32_t>(x) > m_width || static_cast<std::uint32_t>(y) > m_height)
return;
if(!m_staging_buffer.has_value())
OpenCPUBuffer();
for(std::size_t i = 0; i < len; i++)
if constexpr(std::endian::native == std::endian::little)
for(std::size_t i = 0; i < len; i++)
m_cpu_buffer[(y * m_width) + x + i] = mlx_color{ .r = pixels[i].a, .g = pixels[i].b, .b = pixels[i].g, .a = pixels[i].r };
else
{
// Needs to reverse bytes order because why not
int color = ByteSwap(pixels[i]);
m_cpu_buffer[(y * m_width) + x + i] = color;
std::memcpy(&m_cpu_buffer[(y * m_width) + x], pixels, len);
}
m_has_been_modified = true;
}
int Texture::GetPixel(int x, int y) noexcept
mlx_color Texture::GetPixel(int x, int y) noexcept
{
MLX_PROFILE_FUNCTION();
if(x < 0 || y < 0 || static_cast<std::uint32_t>(x) > m_width || static_cast<std::uint32_t>(y) > m_height)
return 0;
return { .rgba = 0x00000000 };
if(!m_staging_buffer.has_value())
OpenCPUBuffer();
return m_cpu_buffer[(y * m_width) + x];
if constexpr(std::endian::native == std::endian::little)
{
mlx_color color = m_cpu_buffer[(y * m_width) + x];
return { .r = color.a, .g = color.b, .b = color.g, .a = color.r };
}
else
return m_cpu_buffer[(y * m_width) + x];
}
void Texture::GetRegion(int x, int y, int w, int h, int* dst) noexcept
void Texture::GetRegion(int x, int y, int w, int h, mlx_color* dst) noexcept
{
MLX_PROFILE_FUNCTION();
if(x < 0 || y < 0 || static_cast<std::uint32_t>(x) > m_width || static_cast<std::uint32_t>(y) > m_height)
@@ -280,8 +288,13 @@ namespace mlx
break;
moving_y++;
}
// Needs to reverse bytes order because why not
dst[i] = ByteSwap(m_cpu_buffer[(moving_y * m_width) + moving_x]);
if constexpr(std::endian::native == std::endian::little)
{
mlx_color color = m_cpu_buffer[(moving_y * m_width) + moving_x];
dst[i] = mlx_color{ .r = color.a, .g = color.b, .b = color.g, .a = color.r };
}
else
dst[i] = m_cpu_buffer[(moving_y * m_width) + moving_x];
}
}
@@ -291,12 +304,13 @@ namespace mlx
Image::Clear(cmd, std::move(color));
if(m_staging_buffer.has_value())
{
std::uint8_t color_bytes[4];
color_bytes[0] = static_cast<std::uint8_t>(color.r * 255.f);
color_bytes[1] = static_cast<std::uint8_t>(color.g * 255.f);
color_bytes[2] = static_cast<std::uint8_t>(color.b * 255.f);
color_bytes[3] = static_cast<std::uint8_t>(color.a * 255.f);
std::fill(m_cpu_buffer.begin(), m_cpu_buffer.end(), *reinterpret_cast<int*>(color_bytes));
mlx_color processed_color{
.r = static_cast<std::uint8_t>(color.r * 255.f),
.g = static_cast<std::uint8_t>(color.g * 255.f),
.b = static_cast<std::uint8_t>(color.b * 255.f),
.a = static_cast<std::uint8_t>(color.a * 255.f)
};
std::fill(m_cpu_buffer.begin(), m_cpu_buffer.end(), processed_color);
}
}