From 4e0643ae9785cb2ee6851344460a903ef948cb38 Mon Sep 17 00:00:00 2001 From: Stephan Unverwerth Date: Tue, 15 Mar 2022 13:19:03 +0100 Subject: LibGL+LibGPU+LibSoftGPU: Move ImageFormat.h to LibGPU --- Userland/Libraries/LibGL/GLContext.cpp | 4 +- Userland/Libraries/LibGL/Tex/Texture2D.cpp | 10 +- Userland/Libraries/LibGPU/ImageFormat.h | 43 +++++++ Userland/Libraries/LibSoftGPU/Device.cpp | 4 +- Userland/Libraries/LibSoftGPU/Device.h | 4 +- Userland/Libraries/LibSoftGPU/Image.h | 119 +++++++++++++++++- Userland/Libraries/LibSoftGPU/ImageDataLayout.h | 4 +- Userland/Libraries/LibSoftGPU/ImageFormat.h | 156 ------------------------ 8 files changed, 172 insertions(+), 172 deletions(-) create mode 100644 Userland/Libraries/LibGPU/ImageFormat.h delete mode 100644 Userland/Libraries/LibSoftGPU/ImageFormat.h (limited to 'Userland') 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 #include #include +#include #include #include #include #include -#include __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 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 + * Copyright (c) 2022, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +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 Device::create_image(ImageFormat format, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers) +NonnullRefPtr 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 #include #include +#include #include #include #include @@ -24,7 +25,6 @@ #include #include #include -#include #include #include #include @@ -130,7 +130,7 @@ public: ColorType get_color_buffer_pixel(int x, int y); DepthType get_depthbuffer_value(int x, int y); - NonnullRefPtr create_image(ImageFormat format, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers); + NonnullRefPtr 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 #include #include +#include #include #include #include #include #include -#include 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(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(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(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(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(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(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(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(clamp(color.x(), 0.0f, 1.0f) * 255); + auto g = static_cast(clamp(color.y(), 0.0f, 1.0f) * 255); + auto b = static_cast(clamp(color.z(), 0.0f, 1.0f) * 255); + auto a = static_cast(clamp(color.w(), 0.0f, 1.0f) * 255); + + switch (format) { + case GPU::ImageFormat::RGB888: + reinterpret_cast(ptr)[0] = r; + reinterpret_cast(ptr)[1] = g; + reinterpret_cast(ptr)[2] = b; + return; + case GPU::ImageFormat::BGR888: + reinterpret_cast(ptr)[2] = b; + reinterpret_cast(ptr)[1] = g; + reinterpret_cast(ptr)[0] = r; + return; + case GPU::ImageFormat::RGBA8888: + *reinterpret_cast(ptr) = r | (g << 8) | (b << 16) | (a << 24); + return; + case GPU::ImageFormat::BGRA8888: + *reinterpret_cast(ptr) = b | (g << 8) | (r << 16) | (a << 24); + return; + case GPU::ImageFormat::RGB565: + *reinterpret_cast(ptr) = (r & 0x1f) | ((g & 0x3f) << 5) | ((b & 0x1f) << 11); + return; + case GPU::ImageFormat::L8: + *reinterpret_cast(ptr) = r; + return; + case GPU::ImageFormat::L8A8: + reinterpret_cast(ptr)[0] = r; + reinterpret_cast(ptr)[1] = a; + return; + default: + VERIFY_NOT_REACHED(); + } +} + class Image final : public RefCounted { 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 const& offset, Vector3 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 +#include 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 - * Copyright (c) 2022, the SerenityOS developers. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include - -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(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(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(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(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(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(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(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(clamp(color.x(), 0.0f, 1.0f) * 255); - auto g = static_cast(clamp(color.y(), 0.0f, 1.0f) * 255); - auto b = static_cast(clamp(color.z(), 0.0f, 1.0f) * 255); - auto a = static_cast(clamp(color.w(), 0.0f, 1.0f) * 255); - - switch (format) { - case ImageFormat::RGB888: - reinterpret_cast(ptr)[0] = r; - reinterpret_cast(ptr)[1] = g; - reinterpret_cast(ptr)[2] = b; - return; - case ImageFormat::BGR888: - reinterpret_cast(ptr)[2] = b; - reinterpret_cast(ptr)[1] = g; - reinterpret_cast(ptr)[0] = r; - return; - case ImageFormat::RGBA8888: - *reinterpret_cast(ptr) = r | (g << 8) | (b << 16) | (a << 24); - return; - case ImageFormat::BGRA8888: - *reinterpret_cast(ptr) = b | (g << 8) | (r << 16) | (a << 24); - return; - case ImageFormat::RGB565: - *reinterpret_cast(ptr) = (r & 0x1f) | ((g & 0x3f) << 5) | ((b & 0x1f) << 11); - return; - case ImageFormat::L8: - *reinterpret_cast(ptr) = r; - return; - case ImageFormat::L8A8: - reinterpret_cast(ptr)[0] = r; - reinterpret_cast(ptr)[1] = a; - return; - default: - VERIFY_NOT_REACHED(); - } -} - -} -- cgit v1.2.3