diff options
author | Stephan Unverwerth <s.unverwerth@serenityos.org> | 2021-12-18 23:48:07 +0100 |
---|---|---|
committer | Brian Gianforcaro <b.gianfo@gmail.com> | 2021-12-24 05:10:28 -0800 |
commit | 91ccf9958ff2be4026b9bfa21a2a3fa52fc7f265 (patch) | |
tree | bce564e602f67851a5f37e6569c381819d0a985a | |
parent | a9e27b9a0fe1415803bbdf735d4fd99e554e8d95 (diff) | |
download | serenity-91ccf9958ff2be4026b9bfa21a2a3fa52fc7f265.zip |
LibSoftGPU: Add Image class
This serves as the storage for all image types. 1D, 2D, 3D, Cube and
image arrays.
Upon construction a full mipmap chain is generated and the image is
immutable afterwards with respect to its layout.
-rw-r--r-- | Userland/Libraries/LibSoftGPU/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibSoftGPU/Image.cpp | 44 | ||||
-rw-r--r-- | Userland/Libraries/LibSoftGPU/Image.h | 68 | ||||
-rw-r--r-- | Userland/Libraries/LibSoftGPU/ImageFormat.h | 122 |
4 files changed, 235 insertions, 0 deletions
diff --git a/Userland/Libraries/LibSoftGPU/CMakeLists.txt b/Userland/Libraries/LibSoftGPU/CMakeLists.txt index 5fab5680d7..b2870d3cb2 100644 --- a/Userland/Libraries/LibSoftGPU/CMakeLists.txt +++ b/Userland/Libraries/LibSoftGPU/CMakeLists.txt @@ -2,6 +2,7 @@ set(SOURCES Clipper.cpp DepthBuffer.cpp Device.cpp + Image.cpp ) serenity_lib(LibSoftGPU softgpu) diff --git a/Userland/Libraries/LibSoftGPU/Image.cpp b/Userland/Libraries/LibSoftGPU/Image.cpp new file mode 100644 index 0000000000..147e7d9a53 --- /dev/null +++ b/Userland/Libraries/LibSoftGPU/Image.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibSoftGPU/Image.h> + +namespace SoftGPU { + +Image::Image(ImageFormat format, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers) + : m_format(format) + , m_width(width) + , m_height(height) + , m_depth(depth) + , m_num_layers(layers) +{ + VERIFY(width > 0); + VERIFY(height > 0); + VERIFY(depth > 0); + VERIFY(levels > 0); + VERIFY(layers > 0); + + m_mipmap_sizes.append({ width, height, depth }); + m_mipmap_offsets.append(0); + + m_mipchain_size += width * height * depth * element_size(format); + + while (--levels && (width > 1 || height > 1 || depth > 1)) { + width = max(width / 2, 1); + height = max(height / 2, 1); + depth = max(depth / 2, 1); + m_mipmap_sizes.append({ width, height, depth }); + m_mipmap_offsets.append(m_mipchain_size); + + m_mipchain_size += width * height * depth * element_size(format); + } + + m_num_levels = m_mipmap_sizes.size(); + + m_data.resize(m_mipchain_size * m_num_layers); +} + +} diff --git a/Userland/Libraries/LibSoftGPU/Image.h b/Userland/Libraries/LibSoftGPU/Image.h new file mode 100644 index 0000000000..f46d3f601c --- /dev/null +++ b/Userland/Libraries/LibSoftGPU/Image.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/RefCounted.h> +#include <AK/Vector.h> +#include <LibGfx/Vector3.h> +#include <LibGfx/Vector4.h> +#include <LibSoftGPU/ImageFormat.h> + +namespace SoftGPU { + +class Image final : public RefCounted<Image> { +public: + Image(ImageFormat format, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers); + + ImageFormat format() const { return m_format; } + unsigned width() const { return m_width; } + unsigned height() const { return m_height; } + unsigned depth() const { return m_depth; } + unsigned level_width(unsigned level) const { return m_mipmap_sizes[level].x(); } + unsigned level_height(unsigned level) const { return m_mipmap_sizes[level].y(); } + unsigned level_depth(unsigned level) const { return m_mipmap_sizes[level].z(); } + unsigned num_levels() const { return m_num_levels; } + unsigned num_layers() const { return m_num_layers; } + + FloatVector4 texel(unsigned layer, unsigned level, unsigned x, unsigned y, unsigned z) const + { + return unpack_color(texel_pointer(layer, level, x, y, z), m_format); + } + + void set_texel(unsigned layer, unsigned level, unsigned x, unsigned y, unsigned z, FloatVector4 const& color) + { + pack_color(color, texel_pointer(layer, level, x, y, z), m_format); + } + +private: + void const* texel_pointer(unsigned layer, unsigned level, unsigned x, unsigned y, unsigned z) const + { + auto size = m_mipmap_sizes[level]; + return &m_data[m_mipchain_size * layer + m_mipmap_offsets[level] + (z * size.x() * size.y() + y * size.x() + x) * element_size(m_format)]; + } + + void* texel_pointer(unsigned layer, unsigned level, unsigned x, unsigned y, unsigned z) + { + auto size = m_mipmap_sizes[level]; + return &m_data[m_mipchain_size * layer + m_mipmap_offsets[level] + (z * size.x() * size.y() + y * size.x() + x) * element_size(m_format)]; + } + +private: + ImageFormat m_format { ImageFormat::RGBA8888 }; + unsigned m_width { 0 }; + unsigned m_height { 0 }; + unsigned m_depth { 0 }; + unsigned m_num_levels { 0 }; + unsigned m_num_layers { 0 }; + + size_t m_mipchain_size { 0 }; + Vector<size_t, 16> m_mipmap_offsets; + Vector<Vector3<unsigned>, 16> m_mipmap_sizes; + Vector<u8> m_data; +}; + +} diff --git a/Userland/Libraries/LibSoftGPU/ImageFormat.h b/Userland/Libraries/LibSoftGPU/ImageFormat.h new file mode 100644 index 0000000000..933627a10b --- /dev/null +++ b/Userland/Libraries/LibSoftGPU/ImageFormat.h @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <LibGfx/Vector4.h> + +namespace SoftGPU { + +enum class ImageFormat { + RGB565, + RGB888, + BGR888, + RGBA8888, + BGRA8888, +}; + +inline static constexpr size_t element_size(ImageFormat format) +{ + switch (format) { + case ImageFormat::RGB565: + return 2; + case ImageFormat::RGB888: + case ImageFormat::BGR888: + return 3; + case ImageFormat::RGBA8888: + case ImageFormat::BGRA8888: + return 4; + default: + VERIFY_NOT_REACHED(); + } +} + +inline static FloatVector4 unpack_color(void const* ptr, ImageFormat format) +{ + switch (format) { + case ImageFormat::RGB888: { + auto rgb = reinterpret_cast<u8 const*>(ptr); + return { + rgb[0] / 255.f, + rgb[1] / 255.f, + rgb[2] / 255.f, + 1.0f + }; + } + case ImageFormat::BGR888: { + auto bgr = reinterpret_cast<u8 const*>(ptr); + return { + bgr[2] / 255.f, + bgr[1] / 255.f, + bgr[0] / 255.f, + 1.0f + }; + } + case ImageFormat::RGBA8888: { + auto rgba = *reinterpret_cast<u32 const*>(ptr); + return { + (rgba & 0xff) / 255.f, + ((rgba >> 8) & 0xff) / 255.f, + ((rgba >> 16) & 0xff) / 255.f, + ((rgba >> 24) & 0xff) / 255.f + }; + } + case ImageFormat::BGRA8888: { + auto bgra = *reinterpret_cast<u32 const*>(ptr); + return { + ((bgra >> 16) & 0xff) / 255.f, + ((bgra >> 8) & 0xff) / 255.f, + (bgra & 0xff) / 255.f, + ((bgra >> 24) & 0xff) / 255.f + }; + } + case ImageFormat::RGB565: { + auto rgb = *reinterpret_cast<u16 const*>(ptr); + return { + ((rgb >> 11) & 0x1f) / 31.f, + ((rgb >> 5) & 0x3f) / 63.f, + (rgb & 0x1f) / 31.f, + 1.0f + }; + } + default: + VERIFY_NOT_REACHED(); + } +} + +inline static void pack_color(FloatVector4 const& color, void* ptr, ImageFormat format) +{ + auto r = static_cast<u8>(clamp(color.x(), 0.0f, 1.0f) * 255); + auto g = static_cast<u8>(clamp(color.y(), 0.0f, 1.0f) * 255); + auto b = static_cast<u8>(clamp(color.z(), 0.0f, 1.0f) * 255); + auto a = static_cast<u8>(clamp(color.w(), 0.0f, 1.0f) * 255); + + switch (format) { + case ImageFormat::RGB888: + reinterpret_cast<u8*>(ptr)[0] = r; + reinterpret_cast<u8*>(ptr)[1] = g; + reinterpret_cast<u8*>(ptr)[2] = b; + return; + case ImageFormat::BGR888: + reinterpret_cast<u8*>(ptr)[2] = b; + reinterpret_cast<u8*>(ptr)[1] = g; + reinterpret_cast<u8*>(ptr)[0] = r; + return; + case ImageFormat::RGBA8888: + *reinterpret_cast<u32*>(ptr) = r | (g << 8) | (b << 16) | (a << 24); + return; + case ImageFormat::BGRA8888: + *reinterpret_cast<u32*>(ptr) = b | (g << 8) | (r << 16) | (a << 24); + return; + case ImageFormat::RGB565: + *reinterpret_cast<u16*>(ptr) = (r & 0x1f) | ((g & 0x3f) << 5) | ((b & 0x1f) << 11); + return; + default: + VERIFY_NOT_REACHED(); + } +} + +} |