/* * Copyright (c) 2021, Stephan Unverwerth * Copyright (c) 2022, Jelle Raaijmakers * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #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 GPU::Image { public: Image(void* const ownership_token, unsigned width, unsigned height, unsigned depth, unsigned max_levels, unsigned layers); unsigned level_width(unsigned level) const { return m_mipmap_buffers[level]->width(); } unsigned level_height(unsigned level) const { return m_mipmap_buffers[level]->height(); } unsigned level_depth(unsigned level) const { return m_mipmap_buffers[level]->depth(); } unsigned num_levels() const { return m_num_levels; } unsigned num_layers() const { return m_num_layers; } bool width_is_power_of_two() const { return m_width_is_power_of_two; } bool height_is_power_of_two() const { return m_height_is_power_of_two; } bool depth_is_power_of_two() const { return m_depth_is_power_of_two; } FloatVector4 texel(unsigned layer, unsigned level, int x, int y, int z) const { 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), GPU::ImageFormat::BGRA8888); } virtual void write_texels(unsigned layer, unsigned level, Vector3 const& offset, Vector3 const& size, void const* data, GPU::ImageDataLayout const& layout) override; virtual void read_texels(unsigned layer, unsigned level, Vector3 const& offset, Vector3 const& size, void* data, GPU::ImageDataLayout const& layout) const override; virtual void copy_texels(GPU::Image const& source, unsigned source_layer, unsigned source_level, Vector3 const& source_offset, Vector3 const& size, unsigned destination_layer, unsigned destination_level, Vector3 const& destination_offset) override; private: void const* texel_pointer(unsigned layer, unsigned level, int x, int y, int z) const { return m_mipmap_buffers[layer * m_num_layers + level]->buffer_pointer(x, y, z); } void* texel_pointer(unsigned layer, unsigned level, int x, int y, int z) { return m_mipmap_buffers[layer * m_num_layers + level]->buffer_pointer(x, y, z); } private: unsigned m_num_levels { 0 }; unsigned m_num_layers { 0 }; FixedArray>> m_mipmap_buffers; bool m_width_is_power_of_two { false }; bool m_height_is_power_of_two { false }; bool m_depth_is_power_of_two { false }; }; }