diff options
author | Stephan Unverwerth <s.unverwerth@serenityos.org> | 2022-03-15 13:19:03 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-04-06 11:32:24 +0200 |
commit | 4e0643ae9785cb2ee6851344460a903ef948cb38 (patch) | |
tree | a7fbb3d5f86f6a6c8de81df66f7c370a0e47d75c /Userland | |
parent | 24d420312c6a8a2d1488b54c712e625a1d6b02e2 (diff) | |
download | serenity-4e0643ae9785cb2ee6851344460a903ef948cb38.zip |
LibGL+LibGPU+LibSoftGPU: Move ImageFormat.h to LibGPU
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibGL/GLContext.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibGL/Tex/Texture2D.cpp | 10 | ||||
-rw-r--r-- | Userland/Libraries/LibGPU/ImageFormat.h | 43 | ||||
-rw-r--r-- | Userland/Libraries/LibSoftGPU/Device.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibSoftGPU/Device.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibSoftGPU/Image.h | 119 | ||||
-rw-r--r-- | Userland/Libraries/LibSoftGPU/ImageDataLayout.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibSoftGPU/ImageFormat.h | 156 |
8 files changed, 172 insertions, 172 deletions
diff --git a/Userland/Libraries/LibGL/GLContext.cpp b/Userland/Libraries/LibGL/GLContext.cpp index 3c08102143..4712fed66c 100644 --- a/Userland/Libraries/LibGL/GLContext.cpp +++ b/Userland/Libraries/LibGL/GLContext.cpp @@ -16,11 +16,11 @@ #include <AK/Vector.h> #include <LibGL/GLContext.h> #include <LibGPU/Enums.h> +#include <LibGPU/ImageFormat.h> #include <LibGfx/Bitmap.h> #include <LibGfx/Painter.h> #include <LibGfx/Vector4.h> #include <LibSoftGPU/Device.h> -#include <LibSoftGPU/ImageFormat.h> __attribute__((visibility("hidden"))) GL::GLContext* g_gl_context; @@ -927,7 +927,7 @@ void GLContext::gl_tex_image_2d(GLenum target, GLint level, GLint internal_forma // that constructing GL textures in any but the default mipmap order, going from level 0 upwards will cause mip levels to stay uninitialized. // To be spec compliant we should create the device image once the texture has become complete and is used for rendering the first time. // All images that were attached before the device image was created need to be stored somewhere to be used to initialize the device image once complete. - texture_2d->set_device_image(m_rasterizer.create_image(SoftGPU::ImageFormat::BGRA8888, width, height, 1, 999, 1)); + texture_2d->set_device_image(m_rasterizer.create_image(GPU::ImageFormat::BGRA8888, width, height, 1, 999, 1)); m_sampler_config_is_dirty = true; } diff --git a/Userland/Libraries/LibGL/Tex/Texture2D.cpp b/Userland/Libraries/LibGL/Tex/Texture2D.cpp index dd33c3a104..662a938bf1 100644 --- a/Userland/Libraries/LibGL/Tex/Texture2D.cpp +++ b/Userland/Libraries/LibGL/Tex/Texture2D.cpp @@ -66,16 +66,16 @@ void Texture2D::replace_sub_texture_data(GLuint lod, GLint xoffset, GLint yoffse layout.depth_stride = 0; if (type == GL_UNSIGNED_SHORT_5_6_5) { - layout.format = SoftGPU::ImageFormat::RGB565; + layout.format = GPU::ImageFormat::RGB565; } else if (type == GL_UNSIGNED_BYTE) { if (format == GL_RGB) - layout.format = SoftGPU::ImageFormat::RGB888; + layout.format = GPU::ImageFormat::RGB888; else if (format == GL_BGR) - layout.format = SoftGPU::ImageFormat::BGR888; + layout.format = GPU::ImageFormat::BGR888; else if (format == GL_RGBA) - layout.format = SoftGPU::ImageFormat::RGBA8888; + layout.format = GPU::ImageFormat::RGBA8888; else if (format == GL_BGRA) - layout.format = SoftGPU::ImageFormat::BGRA8888; + layout.format = GPU::ImageFormat::BGRA8888; } Vector3<unsigned> offset { diff --git a/Userland/Libraries/LibGPU/ImageFormat.h b/Userland/Libraries/LibGPU/ImageFormat.h new file mode 100644 index 0000000000..acae63d96a --- /dev/null +++ b/Userland/Libraries/LibGPU/ImageFormat.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org> + * Copyright (c) 2022, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/Types.h> + +namespace GPU { + +enum class ImageFormat { + RGB565, + RGB888, + BGR888, + RGBA8888, + BGRA8888, + L8, + L8A8, +}; + +static constexpr size_t element_size(ImageFormat format) +{ + switch (format) { + case ImageFormat::L8: + return 1; + case ImageFormat::RGB565: + case ImageFormat::L8A8: + return 2; + case ImageFormat::RGB888: + case ImageFormat::BGR888: + return 3; + case ImageFormat::RGBA8888: + case ImageFormat::BGRA8888: + return 4; + default: + VERIFY_NOT_REACHED(); + } +} + +} diff --git a/Userland/Libraries/LibSoftGPU/Device.cpp b/Userland/Libraries/LibSoftGPU/Device.cpp index d6e287c6ff..abcf4df7c3 100644 --- a/Userland/Libraries/LibSoftGPU/Device.cpp +++ b/Userland/Libraries/LibSoftGPU/Device.cpp @@ -1219,9 +1219,9 @@ DepthType Device::get_depthbuffer_value(int x, int y) return m_frame_buffer->depth_buffer()->scanline(y)[x]; } -NonnullRefPtr<Image> Device::create_image(ImageFormat format, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers) +NonnullRefPtr<Image> Device::create_image(GPU::ImageFormat format, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers) { - VERIFY(format == ImageFormat::BGRA8888); + VERIFY(format == GPU::ImageFormat::BGRA8888); VERIFY(width > 0); VERIFY(height > 0); VERIFY(depth > 0); diff --git a/Userland/Libraries/LibSoftGPU/Device.h b/Userland/Libraries/LibSoftGPU/Device.h index 0551a9903e..451a347431 100644 --- a/Userland/Libraries/LibSoftGPU/Device.h +++ b/Userland/Libraries/LibSoftGPU/Device.h @@ -13,6 +13,7 @@ #include <AK/Vector.h> #include <LibGPU/DeviceInfo.h> #include <LibGPU/Enums.h> +#include <LibGPU/ImageFormat.h> #include <LibGfx/Bitmap.h> #include <LibGfx/Matrix3x3.h> #include <LibGfx/Matrix4x4.h> @@ -24,7 +25,6 @@ #include <LibSoftGPU/Clipper.h> #include <LibSoftGPU/Config.h> #include <LibSoftGPU/Image.h> -#include <LibSoftGPU/ImageFormat.h> #include <LibSoftGPU/Light/Light.h> #include <LibSoftGPU/Light/Material.h> #include <LibSoftGPU/Sampler.h> @@ -130,7 +130,7 @@ public: ColorType get_color_buffer_pixel(int x, int y); DepthType get_depthbuffer_value(int x, int y); - NonnullRefPtr<Image> create_image(ImageFormat format, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers); + NonnullRefPtr<Image> create_image(GPU::ImageFormat format, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers); void set_sampler_config(unsigned, SamplerConfig const&); void set_light_state(unsigned, Light const&); diff --git a/Userland/Libraries/LibSoftGPU/Image.h b/Userland/Libraries/LibSoftGPU/Image.h index 8c31007d06..23f53f6a0d 100644 --- a/Userland/Libraries/LibSoftGPU/Image.h +++ b/Userland/Libraries/LibSoftGPU/Image.h @@ -11,15 +11,128 @@ #include <AK/RefCounted.h> #include <AK/RefPtr.h> #include <LibGPU/Enums.h> +#include <LibGPU/ImageFormat.h> #include <LibGfx/Vector3.h> #include <LibGfx/Vector4.h> #include <LibSoftGPU/Buffer/Typed3DBuffer.h> #include <LibSoftGPU/Config.h> #include <LibSoftGPU/ImageDataLayout.h> -#include <LibSoftGPU/ImageFormat.h> namespace SoftGPU { +inline static FloatVector4 unpack_color(void const* ptr, GPU::ImageFormat format) +{ + constexpr auto one_over_255 = 1.0f / 255; + switch (format) { + case GPU::ImageFormat::RGB888: { + auto rgb = reinterpret_cast<u8 const*>(ptr); + return { + rgb[0] * one_over_255, + rgb[1] * one_over_255, + rgb[2] * one_over_255, + 1.0f, + }; + } + case GPU::ImageFormat::BGR888: { + auto bgr = reinterpret_cast<u8 const*>(ptr); + return { + bgr[2] * one_over_255, + bgr[1] * one_over_255, + bgr[0] * one_over_255, + 1.0f, + }; + } + case GPU::ImageFormat::RGBA8888: { + auto rgba = *reinterpret_cast<u32 const*>(ptr); + return { + (rgba & 0xff) * one_over_255, + ((rgba >> 8) & 0xff) * one_over_255, + ((rgba >> 16) & 0xff) * one_over_255, + ((rgba >> 24) & 0xff) * one_over_255, + }; + } + case GPU::ImageFormat::BGRA8888: { + auto bgra = *reinterpret_cast<u32 const*>(ptr); + return { + ((bgra >> 16) & 0xff) * one_over_255, + ((bgra >> 8) & 0xff) * one_over_255, + (bgra & 0xff) * one_over_255, + ((bgra >> 24) & 0xff) * one_over_255, + }; + } + case GPU::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 + }; + } + case GPU::ImageFormat::L8: { + auto luminance = *reinterpret_cast<u8 const*>(ptr); + auto clamped_luminance = luminance * one_over_255; + return { + clamped_luminance, + clamped_luminance, + clamped_luminance, + 1.0f, + }; + } + case GPU::ImageFormat::L8A8: { + auto luminance_and_alpha = reinterpret_cast<u8 const*>(ptr); + auto clamped_luminance = luminance_and_alpha[0] * one_over_255; + return { + clamped_luminance, + clamped_luminance, + clamped_luminance, + luminance_and_alpha[1] * one_over_255, + }; + } + default: + VERIFY_NOT_REACHED(); + } +} + +inline static void pack_color(FloatVector4 const& color, void* ptr, GPU::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 GPU::ImageFormat::RGB888: + reinterpret_cast<u8*>(ptr)[0] = r; + reinterpret_cast<u8*>(ptr)[1] = g; + reinterpret_cast<u8*>(ptr)[2] = b; + return; + case GPU::ImageFormat::BGR888: + reinterpret_cast<u8*>(ptr)[2] = b; + reinterpret_cast<u8*>(ptr)[1] = g; + reinterpret_cast<u8*>(ptr)[0] = r; + return; + case GPU::ImageFormat::RGBA8888: + *reinterpret_cast<u32*>(ptr) = r | (g << 8) | (b << 16) | (a << 24); + return; + case GPU::ImageFormat::BGRA8888: + *reinterpret_cast<u32*>(ptr) = b | (g << 8) | (r << 16) | (a << 24); + return; + case GPU::ImageFormat::RGB565: + *reinterpret_cast<u16*>(ptr) = (r & 0x1f) | ((g & 0x3f) << 5) | ((b & 0x1f) << 11); + return; + case GPU::ImageFormat::L8: + *reinterpret_cast<u8*>(ptr) = r; + return; + case GPU::ImageFormat::L8A8: + reinterpret_cast<u8*>(ptr)[0] = r; + reinterpret_cast<u8*>(ptr)[1] = a; + return; + default: + VERIFY_NOT_REACHED(); + } +} + class Image final : public RefCounted<Image> { public: Image(unsigned width, unsigned height, unsigned depth, unsigned max_levels, unsigned layers); @@ -35,12 +148,12 @@ public: FloatVector4 texel(unsigned layer, unsigned level, int x, int y, int z) const { - return unpack_color(texel_pointer(layer, level, x, y, z), ImageFormat::BGRA8888); + return unpack_color(texel_pointer(layer, level, x, y, z), GPU::ImageFormat::BGRA8888); } void set_texel(unsigned layer, unsigned level, int x, int y, int z, FloatVector4 const& color) { - pack_color(color, texel_pointer(layer, level, x, y, z), ImageFormat::BGRA8888); + pack_color(color, texel_pointer(layer, level, x, y, z), GPU::ImageFormat::BGRA8888); } void write_texels(unsigned layer, unsigned level, Vector3<unsigned> const& offset, Vector3<unsigned> const& size, void const* data, ImageDataLayout const& layout); diff --git a/Userland/Libraries/LibSoftGPU/ImageDataLayout.h b/Userland/Libraries/LibSoftGPU/ImageDataLayout.h index 86b069d43b..30e65ed7cc 100644 --- a/Userland/Libraries/LibSoftGPU/ImageDataLayout.h +++ b/Userland/Libraries/LibSoftGPU/ImageDataLayout.h @@ -6,12 +6,12 @@ #pragma once -#include <LibSoftGPU/ImageFormat.h> +#include <LibGPU/ImageFormat.h> namespace SoftGPU { struct ImageDataLayout final { - ImageFormat format; + GPU::ImageFormat format; size_t column_stride; size_t row_stride; size_t depth_stride; diff --git a/Userland/Libraries/LibSoftGPU/ImageFormat.h b/Userland/Libraries/LibSoftGPU/ImageFormat.h deleted file mode 100644 index 56c88eae49..0000000000 --- a/Userland/Libraries/LibSoftGPU/ImageFormat.h +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org> - * Copyright (c) 2022, the SerenityOS developers. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include <LibGfx/Vector4.h> - -namespace SoftGPU { - -enum class ImageFormat { - RGB565, - RGB888, - BGR888, - RGBA8888, - BGRA8888, - L8, - L8A8, -}; - -static constexpr size_t element_size(ImageFormat format) -{ - switch (format) { - case ImageFormat::L8: - return 1; - case ImageFormat::RGB565: - case ImageFormat::L8A8: - 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) -{ - constexpr auto one_over_255 = 1.0f / 255; - switch (format) { - case ImageFormat::RGB888: { - auto rgb = reinterpret_cast<u8 const*>(ptr); - return { - rgb[0] * one_over_255, - rgb[1] * one_over_255, - rgb[2] * one_over_255, - 1.0f, - }; - } - case ImageFormat::BGR888: { - auto bgr = reinterpret_cast<u8 const*>(ptr); - return { - bgr[2] * one_over_255, - bgr[1] * one_over_255, - bgr[0] * one_over_255, - 1.0f, - }; - } - case ImageFormat::RGBA8888: { - auto rgba = *reinterpret_cast<u32 const*>(ptr); - return { - (rgba & 0xff) * one_over_255, - ((rgba >> 8) & 0xff) * one_over_255, - ((rgba >> 16) & 0xff) * one_over_255, - ((rgba >> 24) & 0xff) * one_over_255, - }; - } - case ImageFormat::BGRA8888: { - auto bgra = *reinterpret_cast<u32 const*>(ptr); - return { - ((bgra >> 16) & 0xff) * one_over_255, - ((bgra >> 8) & 0xff) * one_over_255, - (bgra & 0xff) * one_over_255, - ((bgra >> 24) & 0xff) * one_over_255, - }; - } - 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 - }; - } - case ImageFormat::L8: { - auto luminance = *reinterpret_cast<u8 const*>(ptr); - auto clamped_luminance = luminance * one_over_255; - return { - clamped_luminance, - clamped_luminance, - clamped_luminance, - 1.0f, - }; - } - case ImageFormat::L8A8: { - auto luminance_and_alpha = reinterpret_cast<u8 const*>(ptr); - auto clamped_luminance = luminance_and_alpha[0] * one_over_255; - return { - clamped_luminance, - clamped_luminance, - clamped_luminance, - luminance_and_alpha[1] * one_over_255, - }; - } - 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; - case ImageFormat::L8: - *reinterpret_cast<u8*>(ptr) = r; - return; - case ImageFormat::L8A8: - reinterpret_cast<u8*>(ptr)[0] = r; - reinterpret_cast<u8*>(ptr)[1] = a; - return; - default: - VERIFY_NOT_REACHED(); - } -} - -} |