1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:57:44 +00:00

LibGL+LibGPU+LibSoftGPU: Implement texture pixel format support

In OpenGL this is called the (base) internal format which is an
expectation expressed by the client for the minimum supported texel
storage format in the GPU for textures.

Since we store everything as RGBA in a `FloatVector4`, the only thing
we do in this patch is remember the expected internal format, and when
we write new texels we fixate the value for the alpha channel to 1 for
two formats that require it.

`PixelConverter` has learned how to transform pixels during transfer to
support this.
This commit is contained in:
Jelle Raaijmakers 2022-08-26 15:59:51 +02:00 committed by Andreas Kling
parent 6c80d12111
commit 84c4b66721
13 changed files with 141 additions and 32 deletions

View file

@ -1399,7 +1399,7 @@ void Device::blit_from_color_buffer(void* output_data, Vector2<i32> input_offset
PixelConverter converter { input_layout, output_layout };
auto const* input_data = m_frame_buffer->color_buffer()->scanline(0);
auto conversion_result = converter.convert(input_data, output_data);
auto conversion_result = converter.convert(input_data, output_data, {});
if (conversion_result.is_error())
dbgln("Pixel conversion failed: {}", conversion_result.error().string_literal());
}
@ -1411,7 +1411,7 @@ void Device::blit_from_depth_buffer(void* output_data, Vector2<i32> input_offset
PixelConverter converter { input_layout, output_layout };
auto const* input_data = m_frame_buffer->depth_buffer()->scanline(0);
auto conversion_result = converter.convert(input_data, output_data);
auto conversion_result = converter.convert(input_data, output_data, {});
if (conversion_result.is_error())
dbgln("Pixel conversion failed: {}", conversion_result.error().string_literal());
}
@ -1432,7 +1432,7 @@ void Device::blit_to_color_buffer_at_raster_position(void const* input_data, GPU
PixelConverter converter { input_layout, output_layout };
auto* output_data = m_frame_buffer->color_buffer()->scanline(0);
auto conversion_result = converter.convert(input_data, output_data);
auto conversion_result = converter.convert(input_data, output_data, {});
if (conversion_result.is_error())
dbgln("Pixel conversion failed: {}", conversion_result.error().string_literal());
}
@ -1450,7 +1450,7 @@ void Device::blit_to_depth_buffer_at_raster_position(void const* input_data, GPU
PixelConverter converter { input_layout, output_layout };
auto* output_data = m_frame_buffer->depth_buffer()->scanline(0);
auto conversion_result = converter.convert(input_data, output_data);
auto conversion_result = converter.convert(input_data, output_data, {});
if (conversion_result.is_error())
dbgln("Pixel conversion failed: {}", conversion_result.error().string_literal());
}
@ -1533,19 +1533,15 @@ void Device::set_light_model_params(GPU::LightModelParameters const& lighting_mo
m_lighting_model = lighting_model;
}
NonnullRefPtr<GPU::Image> Device::create_image(GPU::PixelType const& pixel_type, u32 width, u32 height, u32 depth, u32 levels, u32 layers)
NonnullRefPtr<GPU::Image> Device::create_image(GPU::PixelFormat const& pixel_format, u32 width, u32 height, u32 depth, u32 levels, u32 layers)
{
VERIFY(pixel_type.format == GPU::PixelFormat::RGBA
&& pixel_type.bits == GPU::PixelComponentBits::AllBits
&& pixel_type.data_type == GPU::PixelDataType::Float
&& pixel_type.components_order == GPU::ComponentsOrder::Normal);
VERIFY(width > 0);
VERIFY(height > 0);
VERIFY(depth > 0);
VERIFY(levels > 0);
VERIFY(layers > 0);
return adopt_ref(*new Image(this, width, height, depth, levels, layers));
return adopt_ref(*new Image(this, pixel_format, width, height, depth, levels, layers));
}
void Device::set_sampler_config(unsigned sampler, GPU::SamplerConfig const& config)

View file

@ -62,7 +62,7 @@ public:
virtual GPU::RasterizerOptions options() const override { return m_options; }
virtual GPU::LightModelParameters light_model() const override { return m_lighting_model; }
virtual NonnullRefPtr<GPU::Image> create_image(GPU::PixelType const&, u32 width, u32 height, u32 depth, u32 levels, u32 layers) override;
virtual NonnullRefPtr<GPU::Image> create_image(GPU::PixelFormat const&, u32 width, u32 height, u32 depth, u32 levels, u32 layers) override;
virtual void set_sampler_config(unsigned, GPU::SamplerConfig const&) override;
virtual void set_light_state(unsigned, GPU::Light const&) override;

View file

@ -10,11 +10,18 @@
namespace SoftGPU {
Image::Image(void const* ownership_token, u32 width, u32 height, u32 depth, u32 max_levels, u32 layers)
Image::Image(void const* ownership_token, GPU::PixelFormat const& pixel_format, u32 width, u32 height, u32 depth, u32 max_levels, u32 layers)
: GPU::Image(ownership_token)
, m_num_layers(layers)
, m_pixel_format(pixel_format)
, m_mipmap_buffers(FixedArray<RefPtr<Typed3DBuffer<FloatVector4>>>::must_create_but_fixme_should_propagate_errors(layers * max_levels))
{
VERIFY(pixel_format == GPU::PixelFormat::Alpha
|| pixel_format == GPU::PixelFormat::Intensity
|| pixel_format == GPU::PixelFormat::Luminance
|| pixel_format == GPU::PixelFormat::LuminanceAlpha
|| pixel_format == GPU::PixelFormat::RGB
|| pixel_format == GPU::PixelFormat::RGBA);
VERIFY(width > 0);
VERIFY(height > 0);
VERIFY(depth > 0);
@ -70,20 +77,30 @@ GPU::ImageDataLayout Image::image_data_layout(u32 level, Vector3<i32> offset) co
};
}
void Image::write_texels(u32 layer, u32 level, Vector3<i32> const& output_offset, void const* data, GPU::ImageDataLayout const& input_layout)
void Image::write_texels(u32 layer, u32 level, Vector3<i32> const& output_offset, void const* input_data, GPU::ImageDataLayout const& input_layout)
{
VERIFY(layer < num_layers());
VERIFY(level < num_levels());
auto output_layout = image_data_layout(level, output_offset);
auto texel_data = texel_pointer(layer, level, 0, 0, 0);
PixelConverter converter { input_layout, output_layout };
auto conversion_result = converter.convert(data, texel_pointer(layer, level, 0, 0, 0));
ErrorOr<void> conversion_result;
switch (m_pixel_format) {
case GPU::PixelFormat::Luminance:
case GPU::PixelFormat::RGB:
// Both Luminance and RGB set the alpha to 1, regardless of the source texel
conversion_result = converter.convert(input_data, texel_data, [](auto& components) { components[3] = 1.f; });
break;
default:
conversion_result = converter.convert(input_data, texel_data, {});
}
if (conversion_result.is_error())
dbgln("Pixel conversion failed: {}", conversion_result.error().string_literal());
}
void Image::read_texels(u32 layer, u32 level, Vector3<i32> const& input_offset, void* data, GPU::ImageDataLayout const& output_layout) const
void Image::read_texels(u32 layer, u32 level, Vector3<i32> const& input_offset, void* output_data, GPU::ImageDataLayout const& output_layout) const
{
VERIFY(layer < num_layers());
VERIFY(level < num_levels());
@ -91,7 +108,7 @@ void Image::read_texels(u32 layer, u32 level, Vector3<i32> const& input_offset,
auto input_layout = image_data_layout(level, input_offset);
PixelConverter converter { input_layout, output_layout };
auto conversion_result = converter.convert(texel_pointer(layer, level, 0, 0, 0), data);
auto conversion_result = converter.convert(texel_pointer(layer, level, 0, 0, 0), output_data, {});
if (conversion_result.is_error())
dbgln("Pixel conversion failed: {}", conversion_result.error().string_literal());
}

View file

@ -11,6 +11,7 @@
#include <AK/RefPtr.h>
#include <LibGPU/Image.h>
#include <LibGPU/ImageDataLayout.h>
#include <LibGPU/ImageFormat.h>
#include <LibGfx/Vector3.h>
#include <LibGfx/Vector4.h>
#include <LibSoftGPU/Buffer/Typed3DBuffer.h>
@ -19,7 +20,7 @@ namespace SoftGPU {
class Image final : public GPU::Image {
public:
Image(void const* ownership_token, u32 width, u32 height, u32 depth, u32 max_levels, u32 layers);
Image(void const* ownership_token, GPU::PixelFormat const&, u32 width, u32 height, u32 depth, u32 max_levels, u32 layers);
u32 level_width(u32 level) const { return m_mipmap_buffers[level]->width(); }
u32 level_height(u32 level) const { return m_mipmap_buffers[level]->height(); }
@ -40,8 +41,8 @@ public:
*texel_pointer(layer, level, x, y, z) = color;
}
virtual void write_texels(u32 layer, u32 level, Vector3<i32> const& output_offset, void const* data, GPU::ImageDataLayout const&) override;
virtual void read_texels(u32 layer, u32 level, Vector3<i32> const& input_offset, void* data, GPU::ImageDataLayout const&) const override;
virtual void write_texels(u32 layer, u32 level, Vector3<i32> const& output_offset, void const* input_data, GPU::ImageDataLayout const&) override;
virtual void read_texels(u32 layer, u32 level, Vector3<i32> const& input_offset, void* output_data, GPU::ImageDataLayout const&) const override;
virtual void copy_texels(GPU::Image const& source, u32 source_layer, u32 source_level, Vector3<u32> const& source_offset, Vector3<u32> const& size, u32 destination_layer, u32 destination_level, Vector3<u32> const& destination_offset) override;
private:
@ -61,6 +62,7 @@ private:
u32 m_num_levels { 0 };
u32 m_num_layers { 0 };
GPU::PixelFormat m_pixel_format;
FixedArray<RefPtr<Typed3DBuffer<FloatVector4>>> m_mipmap_buffers;
bool m_width_is_power_of_two { false };

View file

@ -359,7 +359,7 @@ static constexpr GPU::ImageSelection restrain_selection_within_dimensions(GPU::I
return selection;
}
ErrorOr<void> PixelConverter::convert(void const* input_data, void* output_data)
ErrorOr<void> PixelConverter::convert(void const* input_data, void* output_data, Function<void(FloatVector4&)> transform)
{
// Verify pixel data specifications
auto validate_image_data_layout = [](GPU::ImageDataLayout const& specification) -> ErrorOr<void> {
@ -428,6 +428,8 @@ ErrorOr<void> PixelConverter::convert(void const* input_data, void* output_data)
+ output_selection.offset_x * output_pixel_size_in_bytes];
for (u32 input_x = input_selection.offset_x; input_x < input_selection.offset_x + input_selection.width; ++input_x) {
auto pixel_components = read_pixel(&input_scanline);
if (transform)
transform(pixel_components);
write_pixel(&output_scanline, pixel_components);
}
++output_y;

View file

@ -7,6 +7,7 @@
#pragma once
#include <AK/Error.h>
#include <AK/Function.h>
#include <LibGPU/ImageDataLayout.h>
#include <LibGfx/Vector4.h>
@ -20,7 +21,7 @@ public:
{
}
ErrorOr<void> convert(void const* input_data, void* output_data);
ErrorOr<void> convert(void const* input_data, void* output_data, Function<void(FloatVector4&)> transform);
private:
FloatVector4 read_pixel(u8 const**);