1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-25 19:25:07 +00:00
serenity/Userland/Libraries/LibGPU/ImageFormat.h
Jelle Raaijmakers eb7c3d16fb LibGL+LibGPU+LibSoftGPU: Implement flexible pixel format conversion
A GPU (driver) is now responsible for reading and writing pixels from
and to user data. The client (LibGL) is responsible for specifying how
the user data must be interpreted or written to.

This allows us to centralize all pixel format conversion in one class,
`LibSoftGPU::PixelConverter`. For both the input and output image, it
takes a specification containing the image dimensions, the pixel type
and the selection (basically a clipping rect), and converts the pixels
from the input image to the output image.

Effectively this means we now support almost all OpenGL 1.5 formats,
and all custom logic has disappeared from:
  - `glDrawPixels`
  - `glReadPixels`
  - `glTexImage2D`
  - `glTexSubImage2D`

The new logic is still unoptimized, but on my machine I experienced no
noticeable slowdown. :^)
2022-08-27 12:28:05 +02:00

176 lines
4.2 KiB
C++

/*
* Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
* Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Array.h>
namespace GPU {
// The pixel data's representation
enum class PixelFormat {
Alpha,
BGR,
BGRA,
Blue,
ColorIndex,
DepthComponent,
Green,
Luminance,
LuminanceAlpha,
Red,
RGB,
RGBA,
StencilIndex,
};
// Bit width assigned to individual components within a single pixel's value
enum class PixelComponentBits {
AllBits,
B1_5_5_5,
B2_3_3,
B2_10_10_10,
B3_3_2,
B4_4_4_4,
B5_5_5_1,
B5_6_5,
B8_8_8_8,
B10_10_10_2,
};
// The base data type used as pixel storage
enum class PixelDataType {
Bitmap,
Byte,
Float,
HalfFloat,
Int,
Short,
UnsignedByte,
UnsignedInt,
UnsignedShort,
};
// Order of components within a single pixel
enum class ComponentsOrder {
Normal,
Reversed,
};
struct PixelType final {
PixelFormat format;
PixelComponentBits bits;
PixelDataType data_type;
ComponentsOrder components_order { ComponentsOrder::Normal };
};
static constexpr int number_of_components(PixelFormat format)
{
switch (format) {
case PixelFormat::Alpha:
case PixelFormat::Blue:
case PixelFormat::ColorIndex:
case PixelFormat::DepthComponent:
case PixelFormat::Green:
case PixelFormat::Luminance:
case PixelFormat::Red:
case PixelFormat::StencilIndex:
return 1;
case PixelFormat::LuminanceAlpha:
return 2;
case PixelFormat::BGR:
case PixelFormat::RGB:
return 3;
case PixelFormat::BGRA:
case PixelFormat::RGBA:
return 4;
}
VERIFY_NOT_REACHED();
}
static constexpr int number_of_components(PixelComponentBits bits)
{
switch (bits) {
case PixelComponentBits::AllBits:
return 1;
case PixelComponentBits::B2_3_3:
case PixelComponentBits::B3_3_2:
case PixelComponentBits::B5_6_5:
return 3;
case PixelComponentBits::B1_5_5_5:
case PixelComponentBits::B2_10_10_10:
case PixelComponentBits::B4_4_4_4:
case PixelComponentBits::B5_5_5_1:
case PixelComponentBits::B8_8_8_8:
case PixelComponentBits::B10_10_10_2:
return 4;
}
VERIFY_NOT_REACHED();
}
static constexpr Array<u8, 4> pixel_component_bitfield_lengths(PixelComponentBits bits)
{
switch (bits) {
case PixelComponentBits::AllBits:
VERIFY_NOT_REACHED();
case PixelComponentBits::B1_5_5_5:
return { 1, 5, 5, 5 };
case PixelComponentBits::B2_3_3:
return { 2, 3, 3 };
case PixelComponentBits::B2_10_10_10:
return { 2, 10, 10, 10 };
case PixelComponentBits::B3_3_2:
return { 3, 3, 2 };
case PixelComponentBits::B4_4_4_4:
return { 4, 4, 4, 4 };
case PixelComponentBits::B5_5_5_1:
return { 5, 5, 5, 1 };
case PixelComponentBits::B5_6_5:
return { 5, 6, 5 };
case PixelComponentBits::B8_8_8_8:
return { 8, 8, 8, 8 };
case PixelComponentBits::B10_10_10_2:
return { 10, 10, 10, 2 };
}
VERIFY_NOT_REACHED();
}
static constexpr size_t pixel_data_type_size_in_bytes(PixelDataType data_type)
{
switch (data_type) {
case PixelDataType::Bitmap:
return sizeof(u8);
case PixelDataType::Byte:
return sizeof(u8);
case PixelDataType::Float:
return sizeof(float);
case PixelDataType::HalfFloat:
return sizeof(float) / 2;
case PixelDataType::Int:
return sizeof(i32);
case PixelDataType::Short:
return sizeof(i16);
case PixelDataType::UnsignedByte:
return sizeof(u8);
case PixelDataType::UnsignedInt:
return sizeof(u32);
case PixelDataType::UnsignedShort:
return sizeof(u16);
}
VERIFY_NOT_REACHED();
}
static constexpr u8 pixel_size_in_bytes(PixelType pixel_type)
{
auto component_size_in_bytes = pixel_data_type_size_in_bytes(pixel_type.data_type);
if (pixel_type.bits == PixelComponentBits::AllBits)
return component_size_in_bytes * number_of_components(pixel_type.format);
return component_size_in_bytes;
}
}