Skip to content

Truly independent web browser

LibWeb: Apply WebGL unpack flags to ArrayBufferView uploads

Aliaksandr Kalenik1706fd08e265
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/buffer_source.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/buffer_source.rs
index 134a60bf3c8..dac325adcb8 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/buffer_source.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/buffer_source.rs
@@ -17,6 +17,7 @@ use super::byte_ranges::{
upload_range, upload_range_of_2d_texture,
};
use super::canvas::{is_valid_2d_pixel_unpack_state, required_2d_texture_data_size};
+use super::pixel_unpacking::ImageUnpackConversion;
use super::proxy::recorder::{PlainNumber, bytes_of};
use super::state::ContextState;
use super::types::Float32List;
@@ -83,6 +84,24 @@ impl SourceBytes {
unsafe { with_bytes_of_buffer(&self.buffer, self.range.bytes_in_buffer(), function) }
.expect("nothing has run since the bytes were found")
}
+
+ fn copy(&self) -> Vec<u8> {
+ copy_of_bytes_of_buffer(&self.buffer, self.range.bytes_in_buffer())
+ .expect("nothing has run since the bytes were found")
+ }
+}
+
+/// The pixels that texImage2D or texSubImage2D upload from an ArrayBufferView.
+pub(crate) struct TextureData {
+ bytes: SourceBytes,
+ /// `None` when GL takes the pixels as they are.
+ conversion: Option<ImageUnpackConversion>,
+}
+
+impl TextureData {
+ pub(crate) fn gl_size(&self) -> i32 {
+ self.bytes.gl_size()
+ }
}
impl ContextState {
@@ -122,7 +141,7 @@ impl ContextState {
height: i32,
format: u32,
r#type: u32,
- ) -> Result<SourceBytes, OutOfRange> {
+ ) -> Result<TextureData, OutOfRange> {
let unpack_state = self.unpack_state.get();
if !is_valid_2d_pixel_unpack_state(width, &unpack_state) {
return Err(OutOfRange);
@@ -130,7 +149,43 @@ impl ContextState {
let size_that_the_image_takes = required_2d_texture_data_size(width, height, format, r#type, &unpack_state);
let (buffer, extent) = buffer_and_extent_of(&BufferSource::from(source));
let range = upload_range_of_2d_texture(&extent, src_offset, size_that_the_image_takes)?;
- Ok(SourceBytes { buffer, range })
+
+ let flip_y = self.unpack_flip_y.get();
+ let premultiply_alpha = self.unpack_premultiply_alpha.get();
+ let conversion = (flip_y || premultiply_alpha).then_some(ImageUnpackConversion {
+ width,
+ height,
+ format,
+ r#type,
+ unpack_state,
+ flip_y,
+ premultiply_alpha,
+ });
+ Ok(TextureData {
+ bytes: SourceBytes { buffer, range },
+ conversion,
+ })
+ }
+
+ /// `record` gets `None` when the source has no bytes from the offset of the upload on, and otherwise the pixels,
+ /// flipped and premultiplied as UNPACK_FLIP_Y_WEBGL and UNPACK_PREMULTIPLY_ALPHA_WEBGL ask. Converted pixels are
+ /// a copy, which leaves the memory of script as it is.
+ ///
+ /// # Safety
+ ///
+ /// As for upload().
+ pub(crate) unsafe fn upload_texture_data<R>(
+ &self,
+ texture_data: &TextureData,
+ record: impl FnOnce(Option<&[u8]>) -> R,
+ ) -> R {
+ let Some(conversion) = texture_data.conversion else {
+ // SAFETY: The caller keeps `record` from running script and from allocating.
+ return unsafe { self.upload_through_nullable_pointer(&texture_data.bytes, record) };
+ };
+ let mut pixels = texture_data.bytes.copy();
+ conversion.convert(&mut pixels);
+ record(texture_data.bytes.range.source_has_bytes().then_some(&pixels))
}
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/canvas.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/canvas.rs
index b2e28de50d5..2627d595a04 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/canvas.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/canvas.rs
@@ -14,29 +14,9 @@ use core::ptr::NonNull;
use libjs_rust::api::{Gc, Object, Realm};
use super::host::Frame;
+use super::pixel_unpacking::PixelUnpackState;
use crate::bindings::conversions::CppInterface;
-/// Mirrors `Compositing::WebGL::PixelUnpackState`.
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-#[repr(C)]
-pub(crate) struct PixelUnpackState {
- pub(crate) alignment: usize,
- pub(crate) row_length: usize,
- pub(crate) skip_pixels: usize,
- pub(crate) skip_rows: usize,
-}
-
-impl Default for PixelUnpackState {
- fn default() -> Self {
- Self {
- alignment: 4,
- row_length: 0,
- skip_pixels: 0,
- skip_rows: 0,
- }
- }
-}
-
/// In the order of the C++ enum of the same name.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/mod.rs
index 052f65f6e77..4a659b30116 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/mod.rs
@@ -10,6 +10,7 @@
pub(crate) mod buffer_kind;
pub(crate) mod byte_ranges;
pub(crate) mod constants;
+pub(crate) mod pixel_unpacking;
pub(crate) mod proxy;
// What follows runs on the JS runtime, which the tests of the crate do not link.
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/pixel_unpacking.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/pixel_unpacking.rs
new file mode 100644
index 00000000000..f578bca9df3
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/pixel_unpacking.rs
@@ -0,0 +1,866 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! Where the pixels of a 2D texture image are in the client memory that GL unpacks it from, and what
+//! UNPACK_FLIP_Y_WEBGL and UNPACK_PREMULTIPLY_ALPHA_WEBGL do to them, as arithmetic on plain bytes that the tests of
+//! the crate cover without the JS runtime.
+
+use core::ops::Range;
+
+use super::constants::{
+ GL_ALPHA, GL_BYTE, GL_DEPTH_COMPONENT, GL_FLOAT, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, GL_HALF_FLOAT, GL_INT,
+ GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RED, GL_RED_INTEGER, GL_RG, GL_RG_INTEGER, GL_RGB, GL_RGB_INTEGER, GL_RGBA,
+ GL_RGBA_INTEGER, GL_SHORT, GL_UNSIGNED_BYTE, GL_UNSIGNED_INT, GL_UNSIGNED_INT_2_10_10_10_REV,
+ GL_UNSIGNED_INT_5_9_9_9_REV, GL_UNSIGNED_INT_10F_11F_11F_REV, GL_UNSIGNED_INT_24_8, GL_UNSIGNED_SHORT,
+ GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_SHORT_5_5_5_1, GL_UNSIGNED_SHORT_5_6_5,
+};
+
+/// Mirrors `Compositing::WebGL::PixelUnpackState`.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+#[repr(C)]
+pub(crate) struct PixelUnpackState {
+ pub(crate) alignment: usize,
+ pub(crate) row_length: usize,
+ pub(crate) skip_pixels: usize,
+ pub(crate) skip_rows: usize,
+}
+
+impl Default for PixelUnpackState {
+ fn default() -> Self {
+ Self {
+ alignment: 4,
+ row_length: 0,
+ skip_pixels: 0,
+ skip_rows: 0,
+ }
+ }
+}
+
+/// https://registry.khronos.org/webgl/specs/latest/1.0/#PIXEL_STORAGE_PARAMETERS
+///
+/// A 2D image that texImage2D or texSubImage2D unpacks from an ArrayBufferView, and how it is flipped and
+/// premultiplied on the way.
+#[derive(Clone, Copy, Debug)]
+pub(crate) struct ImageUnpackConversion {
+ pub(crate) width: i32,
+ pub(crate) height: i32,
+ pub(crate) format: u32,
+ pub(crate) r#type: u32,
+ pub(crate) unpack_state: PixelUnpackState,
+ pub(crate) flip_y: bool,
+ pub(crate) premultiply_alpha: bool,
+}
+
+impl ImageUnpackConversion {
+ /// Converts the pixels where they are, so that GL unpacks the converted image with the same pixel unpack state.
+ /// The bytes between the rows and before the first pixel stay as they are, and bytes that do not hold the whole
+ /// image are left for GL to report the error of.
+ pub(crate) fn convert(&self, bytes: &mut [u8]) {
+ let Some(texel_layout) = texel_layout(self.format, self.r#type) else {
+ return;
+ };
+ let Some(rows) = ImageRows::new(self, texel_layout.bytes_per_pixel) else {
+ return;
+ };
+ if rows.end > bytes.len() {
+ return;
+ }
+
+ if self.premultiply_alpha && texel_layout.alpha_premultiplication != AlphaPremultiplication::Unneeded {
+ for row in 0..rows.count {
+ premultiply_alpha_of_row(&mut bytes[rows.bytes_of(row)], texel_layout);
+ }
+ }
+ if self.flip_y {
+ flip_rows(bytes, &rows);
+ }
+ }
+}
+
+/// Where the rows of the pixels of an image are. A row is followed by the padding that UNPACK_ALIGNMENT asks for, and
+/// by the pixels past the image when UNPACK_ROW_LENGTH is wider than the image.
+#[derive(Debug, PartialEq, Eq)]
+struct ImageRows {
+ start_of_first_row: usize,
+ stride: usize,
+ byte_length: usize,
+ count: usize,
+ end: usize,
+}
+
+impl ImageRows {
+ /// `None` for an image without pixels, and for one whose rows overlap.
+ fn new(image: &ImageUnpackConversion, bytes_per_pixel: usize) -> Option<Self> {
+ let width = usize::try_from(image.width).ok().filter(|&width| width > 0)?;
+ let count = usize::try_from(image.height).ok().filter(|&height| height > 0)?;
+ let unpack_state = &image.unpack_state;
+
+ let pixels_per_row = if unpack_state.row_length == 0 {
+ width
+ } else {
+ unpack_state.row_length
+ };
+ let stride = pixels_per_row
+ .checked_mul(bytes_per_pixel)?
+ .checked_next_multiple_of(unpack_state.alignment)?;
+ let byte_length = width.checked_mul(bytes_per_pixel)?;
+ if byte_length > stride {
+ return None;
+ }
+ let start_of_first_row = unpack_state
+ .skip_rows
+ .checked_mul(stride)?
+ .checked_add(unpack_state.skip_pixels.checked_mul(bytes_per_pixel)?)?;
+ let end = stride
+ .checked_mul(count - 1)?
+ .checked_add(start_of_first_row)?
+ .checked_add(byte_length)?;
+ Some(Self {
+ start_of_first_row,
+ stride,
+ byte_length,
+ count,
+ end,
+ })
+ }
+
+ fn bytes_of(&self, row: usize) -> Range<usize> {
+ let start = self.start_of_first_row + row * self.stride;
+ start..start + self.byte_length
+ }
+}
+
+fn flip_rows(bytes: &mut [u8], rows: &ImageRows) {
+ for upper_row in 0..rows.count / 2 {
+ let lower_row = rows.count - 1 - upper_row;
+ let (bytes_up_to_lower_row, bytes_from_lower_row) = bytes.split_at_mut(rows.bytes_of(lower_row).start);
+ bytes_up_to_lower_row[rows.bytes_of(upper_row)].swap_with_slice(&mut bytes_from_lower_row[..rows.byte_length]);
+ }
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+enum ComponentType {
+ UnsignedByte,
+ Byte,
+ UnsignedShort,
+ Short,
+ UnsignedInt,
+ Int,
+ HalfFloat,
+ Float,
+}
+
+impl ComponentType {
+ fn size(self) -> usize {
+ match self {
+ Self::UnsignedByte | Self::Byte => 1,
+ Self::UnsignedShort | Self::Short | Self::HalfFloat => 2,
+ Self::UnsignedInt | Self::Int | Self::Float => 4,
+ }
+ }
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+enum AlphaPremultiplication {
+ /// The texel has no alpha, or no color for alpha to be multiplied into.
+ Unneeded,
+ /// Each component is a number of the type, and alpha is the last of them.
+ IntoComponentsBeforeAlpha(ComponentType),
+ UnsignedShort4444,
+ UnsignedShort5551,
+ UnsignedInt2101010Rev,
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+struct TexelLayout {
+ bytes_per_pixel: usize,
+ alpha_premultiplication: AlphaPremultiplication,
+}
+
+/// Formats and types that no upload takes together may have a layout: GL rejects the upload whatever its pixels are.
+fn texel_layout(format: u32, r#type: u32) -> Option<TexelLayout> {
+ let format_is_rgba = matches!(format, GL_RGBA | GL_RGBA_INTEGER);
+ let packed_texel = |bytes_per_pixel, alpha_premultiplication_of_rgba| TexelLayout {
+ bytes_per_pixel,
+ alpha_premultiplication: if format_is_rgba {
+ alpha_premultiplication_of_rgba
+ } else {
+ AlphaPremultiplication::Unneeded
+ },
+ };
+ match r#type {
+ GL_UNSIGNED_SHORT_4_4_4_4 => return Some(packed_texel(2, AlphaPremultiplication::UnsignedShort4444)),
+ GL_UNSIGNED_SHORT_5_5_5_1 => return Some(packed_texel(2, AlphaPremultiplication::UnsignedShort5551)),
+ GL_UNSIGNED_INT_2_10_10_10_REV => return Some(packed_texel(4, AlphaPremultiplication::UnsignedInt2101010Rev)),
+ GL_UNSIGNED_SHORT_5_6_5 => return Some(packed_texel(2, AlphaPremultiplication::Unneeded)),
+ GL_UNSIGNED_INT_10F_11F_11F_REV | GL_UNSIGNED_INT_5_9_9_9_REV | GL_UNSIGNED_INT_24_8 => {
+ return Some(packed_texel(4, AlphaPremultiplication::Unneeded));
+ }
+ GL_FLOAT_32_UNSIGNED_INT_24_8_REV => return Some(packed_texel(8, AlphaPremultiplication::Unneeded)),
+ _ => {}
+ }
+
+ let component_type = match r#type {
+ GL_UNSIGNED_BYTE => ComponentType::UnsignedByte,
+ GL_BYTE => ComponentType::Byte,
+ GL_UNSIGNED_SHORT => ComponentType::UnsignedShort,
+ GL_SHORT => ComponentType::Short,
+ GL_UNSIGNED_INT => ComponentType::UnsignedInt,
+ GL_INT => ComponentType::Int,
+ GL_HALF_FLOAT => ComponentType::HalfFloat,
+ GL_FLOAT => ComponentType::Float,
+ _ => return None,
+ };
+ let (component_count, alpha_premultiplication) = match format {
+ GL_ALPHA | GL_LUMINANCE | GL_RED | GL_RED_INTEGER | GL_DEPTH_COMPONENT => (1, AlphaPremultiplication::Unneeded),
+ GL_RG | GL_RG_INTEGER => (2, AlphaPremultiplication::Unneeded),
+ GL_RGB | GL_RGB_INTEGER => (3, AlphaPremultiplication::Unneeded),
+ GL_LUMINANCE_ALPHA => (2, AlphaPremultiplication::IntoComponentsBeforeAlpha(component_type)),
+ GL_RGBA | GL_RGBA_INTEGER => (4, AlphaPremultiplication::IntoComponentsBeforeAlpha(component_type)),
+ _ => return None,
+ };
+ Some(TexelLayout {
+ bytes_per_pixel: component_count * component_type.size(),
+ alpha_premultiplication,
+ })
+}
+
+fn premultiply_alpha_of_row(row: &mut [u8], texel_layout: TexelLayout) {
+ let bytes_per_pixel = texel_layout.bytes_per_pixel;
+ match texel_layout.alpha_premultiplication {
+ AlphaPremultiplication::Unneeded => {}
+ AlphaPremultiplication::IntoComponentsBeforeAlpha(component_type) => match component_type {
+ ComponentType::UnsignedByte => premultiply_components_before_alpha::<1, u8>(row, bytes_per_pixel),
+ ComponentType::Byte => premultiply_components_before_alpha::<1, i8>(row, bytes_per_pixel),
+ ComponentType::UnsignedShort => premultiply_components_before_alpha::<2, u16>(row, bytes_per_pixel),
+ ComponentType::Short => premultiply_components_before_alpha::<2, i16>(row, bytes_per_pixel),
+ ComponentType::UnsignedInt => premultiply_components_before_alpha::<4, u32>(row, bytes_per_pixel),
+ ComponentType::Int => premultiply_components_before_alpha::<4, i32>(row, bytes_per_pixel),
+ ComponentType::HalfFloat => premultiply_components_before_alpha::<2, HalfFloat>(row, bytes_per_pixel),
+ ComponentType::Float => premultiply_components_before_alpha::<4, f32>(row, bytes_per_pixel),
+ },
+ AlphaPremultiplication::UnsignedShort4444 => replace_numbers_of_row(row, premultiplied_unsigned_short_4444),
+ AlphaPremultiplication::UnsignedShort5551 => replace_numbers_of_row(row, premultiplied_unsigned_short_5551),
+ AlphaPremultiplication::UnsignedInt2101010Rev => {
+ replace_numbers_of_row(row, premultiplied_unsigned_int_2_10_10_10_rev);
+ }
+ }
+}
+
+/// A number of `SIZE` bytes in the byte order of the platform, which is that of the typed arrays of script and that GL
+/// unpacks.
+trait NativeEndianNumber<const SIZE: usize>: Copy {
+ fn from_bytes(bytes: [u8; SIZE]) -> Self;
+ fn to_bytes(self) -> [u8; SIZE];
+}
+
+trait ColorComponent<const SIZE: usize>: NativeEndianNumber<SIZE> {
+ fn multiplied_by_alpha(self, alpha: Self) -> Self;
+}
+
+macro_rules! native_endian_numbers {
+ ($($number:ty),*) => {$(
+ impl NativeEndianNumber<{ size_of::<$number>() }> for $number {
+ fn from_bytes(bytes: [u8; size_of::<$number>()]) -> Self {
+ Self::from_ne_bytes(bytes)
+ }
+
+ fn to_bytes(self) -> [u8; size_of::<$number>()] {
+ self.to_ne_bytes()
+ }
+ }
+ )*};
+}
+
+native_endian_numbers!(u8, i8, u16, i16, u32, i32, f32);
+
+/// `color` and `alpha` count up to `maximum`, which stands for 1.
+fn product_of_unsigned_normalized(color: u64, alpha: u64, maximum: u64) -> u64 {
+ (color * alpha + maximum / 2) / maximum
+}
+
+/// `color` and `alpha` count from `-maximum` to `maximum`, which stand for -1 and 1. The one number below `-maximum`
+/// stands for -1 as well.
+fn product_of_signed_normalized(color: i64, alpha: i64, maximum: i64) -> i64 {
+ let product = color.max(-maximum) * alpha.max(-maximum);
+ let magnitude = (product.abs() + maximum / 2) / maximum;
+ magnitude * product.signum()
+}
+
+macro_rules! unsigned_normalized_color_components {
+ ($($number:ty),*) => {$(
+ impl ColorComponent<{ size_of::<$number>() }> for $number {
+ fn multiplied_by_alpha(self, alpha: Self) -> Self {
+ let product = product_of_unsigned_normalized(self.into(), alpha.into(), Self::MAX.into());
+ Self::try_from(product).expect("a product of two numbers up to 1 is up to 1")
+ }
+ }
+ )*};
+}
+
+macro_rules! signed_normalized_color_components {
+ ($($number:ty),*) => {$(
+ impl ColorComponent<{ size_of::<$number>() }> for $number {
+ fn multiplied_by_alpha(self, alpha: Self) -> Self {
+ let product = product_of_signed_normalized(self.into(), alpha.into(), Self::MAX.into());
+ Self::try_from(product).expect("a product of two numbers from -1 to 1 is from -1 to 1")
+ }
+ }
+ )*};
+}
+
+// NB: An integer format has no range that stands for 1, so its alpha is premultiplied as though it were normalized.
+unsigned_normalized_color_components!(u8, u16, u32);
+signed_normalized_color_components!(i8, i16, i32);
+
+impl ColorComponent<4> for f32 {
+ fn multiplied_by_alpha(self, alpha: Self) -> Self {
+ self * alpha
+ }
+}
+
+const F32_SIGN_BIT: u32 = 0x8000_0000;
+const F32_EXPONENT_BIAS: u32 = 127;
+const F32_MANTISSA_BITS: u32 = 23;
+const F32_INFINITY_BITS: u32 = 0x7f80_0000;
+
+#[derive(Clone, Copy)]
+struct HalfFloat(u16);
+
+impl NativeEndianNumber<2> for HalfFloat {
+ fn from_bytes(bytes: [u8; 2]) -> Self {
+ Self(u16::from_ne_bytes(bytes))
+ }
+
+ fn to_bytes(self) -> [u8; 2] {
+ self.0.to_ne_bytes()
+ }
+}
+
+impl ColorComponent<2> for HalfFloat {
+ fn multiplied_by_alpha(self, alpha: Self) -> Self {
+ // NB: The product of two half floats is exact in an f32, so it is rounded once.
+ Self::from_f32(self.to_f32() * alpha.to_f32())
+ }
+}
+
+impl HalfFloat {
+ const SIGN_BIT: u16 = 0x8000;
+ const EXPONENT_BIAS: u32 = 15;
+ const MANTISSA_BITS: u32 = 10;
+ const MANTISSA_MASK: u16 = 0x3ff;
+ const MAXIMUM_EXPONENT: u32 = 0x1f;
+ const INFINITY: u16 = 0x7c00;
+ const QUIET_NAN: u16 = 0x7e00;
+
+ fn to_f32(self) -> f32 {
+ const SUBNORMAL_STEP: f32 = 1.0 / 16_777_216.0;
+ let sign = u32::from(self.0 & Self::SIGN_BIT) << 16;
+ let exponent = u32::from(self.0 >> Self::MANTISSA_BITS) & Self::MAXIMUM_EXPONENT;
+ let mantissa = self.0 & Self::MANTISSA_MASK;
+ let mantissa_of_f32 = u32::from(mantissa) << (F32_MANTISSA_BITS - Self::MANTISSA_BITS);
+ let magnitude = match exponent {
+ 0 => (f32::from(mantissa) * SUBNORMAL_STEP).to_bits(),
+ Self::MAXIMUM_EXPONENT => F32_INFINITY_BITS | mantissa_of_f32,
+ _ => ((exponent + F32_EXPONENT_BIAS - Self::EXPONENT_BIAS) << F32_MANTISSA_BITS) | mantissa_of_f32,
+ };
+ f32::from_bits(sign | magnitude)
+ }
+
+ /// Rounds to the nearest half float, and to the one with an even mantissa between two.
+ fn from_f32(value: f32) -> Self {
+ const SMALLEST_MAGNITUDE_ABOVE_THE_RANGE: u32 = (F32_EXPONENT_BIAS + 16) << F32_MANTISSA_BITS;
+ const SMALLEST_NORMAL_MAGNITUDE: u32 = (F32_EXPONENT_BIAS + 1 - HalfFloat::EXPONENT_BIAS) << F32_MANTISSA_BITS;
+ // NB: The step between the f32s from this one to twice it is that of the subnormal half floats, so the f32
+ // addition rounds a subnormal magnitude to one of them.
+ const SUBNORMAL_ROUNDING_ADDEND: u32 = (F32_EXPONENT_BIAS - 1) << F32_MANTISSA_BITS;
+ const DROPPED_MANTISSA_BITS: u32 = F32_MANTISSA_BITS - HalfFloat::MANTISSA_BITS;
+
+ let bits = value.to_bits();
+ let sign = ((bits >> 16) as u16) & Self::SIGN_BIT;
+ let magnitude = bits & !F32_SIGN_BIT;
+ let half_magnitude = if magnitude >= SMALLEST_MAGNITUDE_ABOVE_THE_RANGE {
+ if magnitude > F32_INFINITY_BITS {
+ Self::QUIET_NAN
+ } else {
+ Self::INFINITY
+ }
+ } else if magnitude < SMALLEST_NORMAL_MAGNITUDE {
+ let sum = f32::from_bits(magnitude) + f32::from_bits(SUBNORMAL_ROUNDING_ADDEND);
+ (sum.to_bits() - SUBNORMAL_ROUNDING_ADDEND) as u16
+ } else {
+ let mantissa_is_odd = (magnitude >> DROPPED_MANTISSA_BITS) & 1;
+ let rebiased = magnitude - ((F32_EXPONENT_BIAS - Self::EXPONENT_BIAS) << F32_MANTISSA_BITS);
+ // NB: A carry out of the mantissa increments the exponent, up to that of infinity.
+ let rounded = rebiased + (1 << (DROPPED_MANTISSA_BITS - 1)) - 1 + mantissa_is_odd;
+ (rounded >> DROPPED_MANTISSA_BITS) as u16
+ };
+ Self(sign | half_magnitude)
+ }
+}
+
+fn premultiply_components_before_alpha<const SIZE: usize, C: ColorComponent<SIZE>>(
+ row: &mut [u8],
+ bytes_per_pixel: usize,
+) {
+ for pixel in row.chunks_exact_mut(bytes_per_pixel) {
+ let Some((alpha, colors)) = pixel.as_chunks_mut::<SIZE>().0.split_last_mut() else {
+ continue;
+ };
+ let alpha = C::from_bytes(*alpha);
+ for color in colors {
+ *color = C::from_bytes(*color).multiplied_by_alpha(alpha).to_bytes();
+ }
+ }
+}
+
+fn replace_numbers_of_row<const SIZE: usize, N: NativeEndianNumber<SIZE>>(
+ row: &mut [u8],
+ replacement: impl Fn(N) -> N,
+) {
+ for number in row.as_chunks_mut::<SIZE>().0 {
+ *number = replacement(N::from_bytes(*number)).to_bytes();
+ }
+}
+
+/// Red, green, blue and alpha from the most significant bits on.
+fn premultiplied_unsigned_short_4444(texel: u16) -> u16 {
+ let alpha = u64::from(texel & 0xf);
+ let premultiplied = |shift: u32| {
+ let color = u64::from((texel >> shift) & 0xf);
+ (product_of_unsigned_normalized(color, alpha, 0xf) as u16) << shift
+ };
+ premultiplied(12) | premultiplied(8) | premultiplied(4) | (texel & 0xf)
+}
+
+/// Red, green and blue in five bits each and alpha in the least significant bit.
+fn premultiplied_unsigned_short_5551(texel: u16) -> u16 {
+ if texel & 1 == 0 { 0 } else { texel }
+}
+
+/// Red, green and blue in ten bits each and alpha in the two most significant bits, from the least significant bits on.
+fn premultiplied_unsigned_int_2_10_10_10_rev(texel: u32) -> u32 {
+ let alpha = u64::from(texel >> 30);
+ let premultiplied = |shift: u32| {
+ let color = u64::from((texel >> shift) & 0x3ff);
+ (product_of_unsigned_normalized(color, alpha, 0b11) as u32) << shift
+ };
+ premultiplied(0) | premultiplied(10) | premultiplied(20) | (texel & 0xc000_0000)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::web_gl::constants::GL_DEPTH_STENCIL;
+
+ fn image(width: i32, height: i32, format: u32, r#type: u32) -> ImageUnpackConversion {
+ ImageUnpackConversion {
+ width,
+ height,
+ format,
+ r#type,
+ unpack_state: PixelUnpackState {
+ alignment: 1,
+ ..PixelUnpackState::default()
+ },
+ flip_y: false,
+ premultiply_alpha: false,
+ }
+ }
+
+ fn flipped(image: ImageUnpackConversion) -> ImageUnpackConversion {
+ ImageUnpackConversion { flip_y: true, ..image }
+ }
+
+ fn premultiplied(image: ImageUnpackConversion) -> ImageUnpackConversion {
+ ImageUnpackConversion {
+ premultiply_alpha: true,
+ ..image
+ }
+ }
+
+ fn with_unpack_state(image: ImageUnpackConversion, unpack_state: PixelUnpackState) -> ImageUnpackConversion {
+ ImageUnpackConversion { unpack_state, ..image }
+ }
+
+ fn converted(image: ImageUnpackConversion, bytes: &[u8]) -> Vec<u8> {
+ let mut bytes = bytes.to_vec();
+ image.convert(&mut bytes);
+ bytes
+ }
+
+ fn converted_numbers<const SIZE: usize, N: NativeEndianNumber<SIZE>>(
+ image: ImageUnpackConversion,
+ numbers: &[N],
+ ) -> Vec<N> {
+ let bytes: Vec<u8> = numbers.iter().flat_map(|number| number.to_bytes()).collect();
+ let converted_bytes = converted(image, &bytes);
+ converted_bytes
+ .as_chunks::<SIZE>()
+ .0
+ .iter()
+ .map(|bytes| N::from_bytes(*bytes))
+ .collect()
+ }
+
+ #[test]
+ fn the_rows_of_an_image_follow_the_pixel_unpack_state() {
+ let rows = |width, height, bytes_per_pixel, unpack_state| {
+ let image = with_unpack_state(image(width, height, GL_RGBA, GL_UNSIGNED_BYTE), unpack_state);
+ ImageRows::new(&image, bytes_per_pixel)
+ };
+ let unpack_state = |alignment, row_length, skip_pixels, skip_rows| PixelUnpackState {
+ alignment,
+ row_length,
+ skip_pixels,
+ skip_rows,
+ };
+
+ assert_eq!(
+ rows(3, 2, 3, unpack_state(4, 0, 0, 0)),
+ Some(ImageRows {
+ start_of_first_row: 0,
+ stride: 12,
+ byte_length: 9,
+ count: 2,
+ end: 21,
+ })
+ );
+ assert_eq!(rows(3, 2, 3, unpack_state(1, 0, 0, 0)).unwrap().stride, 9);
+ assert_eq!(rows(3, 2, 3, unpack_state(8, 0, 0, 0)).unwrap().stride, 16);
+ assert_eq!(rows(3, 2, 4, unpack_state(8, 0, 0, 0)).unwrap().stride, 16);
+ assert_eq!(
+ rows(2, 3, 4, unpack_state(4, 5, 2, 1)),
+ Some(ImageRows {
+ start_of_first_row: 28,
+ stride: 20,
+ byte_length: 8,
+ count: 3,
+ end: 76,
+ })
+ );
+
+ assert_eq!(rows(0, 2, 4, unpack_state(4, 0, 0, 0)), None);
+ assert_eq!(rows(2, 0, 4, unpack_state(4, 0, 0, 0)), None);
+ assert_eq!(rows(-1, 2, 4, unpack_state(4, 0, 0, 0)), None);
+ assert_eq!(rows(3, 2, 4, unpack_state(4, 2, 0, 0)), None);
+ assert_eq!(rows(2, 2, 4, unpack_state(0, 0, 0, 0)), None);
+ assert_eq!(rows(2, 2, 4, unpack_state(4, 0, 0, usize::MAX)), None);
+ assert_eq!(rows(2, 2, 4, unpack_state(4, 0, usize::MAX, 0)), None);
+ assert_eq!(rows(i32::MAX, i32::MAX, 8, unpack_state(8, usize::MAX / 8, 0, 0)), None);
+ }
+
+ #[test]
+ fn flipping_swaps_the_pixels_of_rows_and_keeps_padding_and_skipped_bytes() {
+ let rgb = image(2, 3, GL_RGB, GL_UNSIGNED_BYTE);
+ let aligned_to_four = with_unpack_state(rgb, PixelUnpackState::default());
+ #[rustfmt::skip]
+ let bytes = [
+ 1, 2, 3, 4, 5, 6, 0xa0, 0xa1,
+ 7, 8, 9, 10, 11, 12, 0xa2, 0xa3,
+ 13, 14, 15, 16, 17, 18,
+ ];
+ #[rustfmt::skip]
+ let expected = [
+ 13, 14, 15, 16, 17, 18, 0xa0, 0xa1,
+ 7, 8, 9, 10, 11, 12, 0xa2, 0xa3,
+ 1, 2, 3, 4, 5, 6,
+ ];
+ assert_eq!(converted(flipped(aligned_to_four), &bytes), expected);
+ assert_eq!(converted(premultiplied(aligned_to_four), &bytes), bytes);
+ assert_eq!(converted(aligned_to_four, &bytes), bytes);
+
+ let one_byte_pixels = image(1, 4, GL_ALPHA, GL_UNSIGNED_BYTE);
+ assert_eq!(converted(flipped(one_byte_pixels), &[1, 2, 3, 4]), [4, 3, 2, 1]);
+ assert_eq!(
+ converted(flipped(image(4, 1, GL_ALPHA, GL_UNSIGNED_BYTE)), &[1, 2, 3, 4]),
+ [1, 2, 3, 4]
+ );
+ }
+
+ #[test]
+ fn flipping_and_premultiplying_leave_the_pixels_around_a_sub_image_as_they_are() {
+ let sub_image = with_unpack_state(
+ image(2, 2, GL_RGBA, GL_UNSIGNED_BYTE),
+ PixelUnpackState {
+ alignment: 4,
+ row_length: 3,
+ skip_pixels: 1,
+ skip_rows: 1,
+ },
+ );
+ let outside = [9, 9, 9, 0];
+ let transparent = [255, 255, 255, 0];
+ let one_fifth_opaque = [200, 100, 50, 51];
+ let opaque = [10, 20, 30, 255];
+ let bytes = [
+ outside,
+ outside,
+ outside,
+ outside,
+ one_fifth_opaque,
+ transparent,
+ outside,
+ opaque,
+ opaque,
+ ]
+ .concat();
+ let expected = [
+ outside,
+ outside,
+ outside,
+ outside,
+ opaque,
+ opaque,
+ outside,
+ [40, 20, 10, 51],
+ [0, 0, 0, 0],
+ ]
+ .concat();
+ assert_eq!(converted(flipped(premultiplied(sub_image)), &bytes), expected);
+ }
+
+ #[test]
+ fn bytes_that_do_not_hold_the_whole_image_are_left_as_they_are() {
+ let rgba = flipped(premultiplied(image(1, 2, GL_RGBA, GL_UNSIGNED_BYTE)));
+ let too_few_bytes = [255, 255, 255, 0, 1, 2, 3];
+ assert_eq!(converted(rgba, &too_few_bytes), too_few_bytes);
+
+ let more_bytes = [255, 255, 255, 0, 1, 2, 3, 255, 7];
+ assert_eq!(converted(rgba, &more_bytes), [1, 2, 3, 255, 0, 0, 0, 0, 7]);
+
+ let unknown_type = flipped(image(1, 2, GL_RGBA, 0x1234));
+ assert_eq!(converted(unknown_type, &[1, 2]), [1, 2]);
+ let unknown_format = flipped(image(1, 2, 0x1234, GL_UNSIGNED_BYTE));
+ assert_eq!(converted(unknown_format, &[1, 2]), [1, 2]);
+ }
+
+ #[test]
+ fn unsigned_bytes_are_premultiplied_where_the_format_has_alpha() {
+ let premultiplied_pixel =
+ |format, pixel: &[u8]| converted(premultiplied(image(1, 1, format, GL_UNSIGNED_BYTE)), pixel);
+ assert_eq!(premultiplied_pixel(GL_RGBA, &[200, 100, 50, 51]), [40, 20, 10, 51]);
+ assert_eq!(premultiplied_pixel(GL_RGBA, &[255, 255, 255, 0]), [0, 0, 0, 0]);
+ assert_eq!(premultiplied_pixel(GL_RGBA, &[1, 128, 255, 255]), [1, 128, 255, 255]);
+ assert_eq!(premultiplied_pixel(GL_RGBA, &[255, 1, 3, 128]), [128, 1, 2, 128]);
+ assert_eq!(
+ premultiplied_pixel(GL_RGBA_INTEGER, &[200, 100, 50, 51]),
+ [40, 20, 10, 51]
+ );
+ assert_eq!(premultiplied_pixel(GL_LUMINANCE_ALPHA, &[200, 51]), [40, 51]);
+ assert_eq!(premultiplied_pixel(GL_ALPHA, &[51]), [51]);
+ assert_eq!(premultiplied_pixel(GL_RGB, &[200, 100, 51]), [200, 100, 51]);
+ assert_eq!(premultiplied_pixel(GL_RG, &[200, 51]), [200, 51]);
+ assert_eq!(premultiplied_pixel(GL_LUMINANCE, &[51]), [51]);
+ }
+
+ #[test]
+ fn wider_and_signed_components_are_premultiplied_as_normalized_numbers() {
+ let rgba = |r#type| premultiplied(image(1, 1, GL_RGBA, r#type));
+ assert_eq!(
+ converted_numbers::<2, u16>(rgba(GL_UNSIGNED_SHORT), &[65535, 1000, 3, 13107]),
+ [13107, 200, 1, 13107]
+ );
+ assert_eq!(
+ converted_numbers::<4, u32>(rgba(GL_UNSIGNED_INT), &[u32::MAX, 1000, 0, u32::MAX / 5]),
+ [u32::MAX / 5, 200, 0, u32::MAX / 5]
+ );
+ assert_eq!(
+ converted_numbers::<1, i8>(rgba(GL_BYTE), &[127, -127, -128, 64]),
+ [64, -64, -64, 64]
+ );
+ assert_eq!(
+ converted_numbers::<1, i8>(rgba(GL_BYTE), &[100, -100, 0, -127]),
+ [-100, 100, 0, -127]
+ );
+ assert_eq!(
+ converted_numbers::<2, i16>(rgba(GL_SHORT), &[32767, -32767, 1000, 6553]),
+ [6553, -6553, 200, 6553]
+ );
+ assert_eq!(
+ converted_numbers::<4, i32>(rgba(GL_INT), &[i32::MAX, i32::MIN, 1000, i32::MAX / 5]),
+ [i32::MAX / 5, -(i32::MAX / 5), 200, i32::MAX / 5]
+ );
+ assert_eq!(
+ converted_numbers::<2, u16>(
+ premultiplied(image(1, 1, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT)),
+ &[65535, 0, 0, 0]
+ ),
+ [0, 0, 0, 0]
+ );
+ }
+
+ #[test]
+ fn floats_are_premultiplied_by_multiplication() {
+ assert_eq!(
+ converted_numbers::<4, f32>(premultiplied(image(1, 1, GL_RGBA, GL_FLOAT)), &[1.0, 0.5, -2.0, 0.25]),
+ [0.25, 0.125, -0.5, 0.25]
+ );
+ assert_eq!(
+ converted_numbers::<4, f32>(premultiplied(image(1, 1, GL_LUMINANCE_ALPHA, GL_FLOAT)), &[0.75, 0.5]),
+ [0.375, 0.5]
+ );
+ assert_eq!(
+ converted_numbers::<4, f32>(premultiplied(image(1, 1, GL_RGB, GL_FLOAT)), &[0.75, 0.5, 0.25]),
+ [0.75, 0.5, 0.25]
+ );
+
+ let one = 0x3c00;
+ let half = 0x3800;
+ let quarter = 0x3400;
+ let three_quarters = 0x3a00;
+ let three_sixteenths = 0x3200;
+ let eighth = 0x3000;
+ assert_eq!(
+ converted_numbers::<2, u16>(
+ premultiplied(image(1, 1, GL_RGBA, GL_HALF_FLOAT)),
+ &[one, three_quarters, half, quarter]
+ ),
+ [quarter, three_sixteenths, eighth, quarter]
+ );
+ assert_eq!(
+ converted_numbers::<2, u16>(
+ premultiplied(image(1, 1, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT)),
+ &[one, half]
+ ),
+ [half, half]
+ );
+ }
+
+ #[test]
+ fn half_floats_convert_to_and_from_f32s() {
+ let to_f32 = |bits| HalfFloat(bits).to_f32();
+ assert_eq!(to_f32(0x0000).to_bits(), 0.0f32.to_bits());
+ assert_eq!(to_f32(0x8000).to_bits(), (-0.0f32).to_bits());
+ assert_eq!(to_f32(0x3c00), 1.0);
+ assert_eq!(to_f32(0xc000), -2.0);
+ assert_eq!(to_f32(0x7bff), 65504.0);
+ assert_eq!(to_f32(0x0400), 1.0 / 16384.0);
+ assert_eq!(to_f32(0x0001), 1.0 / 16_777_216.0);
+ assert_eq!(to_f32(0x03ff), 1023.0 / 16_777_216.0);
+ assert_eq!(to_f32(0x7c00), f32::INFINITY);
+ assert_eq!(to_f32(0xfc00), f32::NEG_INFINITY);
+ assert!(to_f32(0x7e00).is_nan());
+
+ let from_f32 = |value| HalfFloat::from_f32(value).0;
+ assert_eq!(from_f32(0.0), 0x0000);
+ assert_eq!(from_f32(-0.0), 0x8000);
+ assert_eq!(from_f32(1.0), 0x3c00);
+ assert_eq!(from_f32(-2.0), 0xc000);
+ assert_eq!(from_f32(65504.0), 0x7bff);
+ assert_eq!(from_f32(65519.0), 0x7bff);
+ assert_eq!(from_f32(65520.0), 0x7c00);
+ assert_eq!(from_f32(1.0e10), 0x7c00);
+ assert_eq!(from_f32(f32::INFINITY), 0x7c00);
+ assert_eq!(from_f32(f32::NEG_INFINITY), 0xfc00);
+ assert_eq!(from_f32(f32::NAN) & 0x7e00, 0x7e00);
+ assert_eq!(from_f32(1.0 / 16384.0), 0x0400);
+ assert_eq!(from_f32(1.0 / 16_777_216.0), 0x0001);
+ assert_eq!(from_f32(1.0 / 33_554_432.0), 0x0000);
+ assert_eq!(from_f32(3.0 / 33_554_432.0), 0x0002);
+ assert_eq!(from_f32(1.0 / 1.0e10), 0x0000);
+ // Between two half floats, the one with the even mantissa.
+ assert_eq!(from_f32(1.0 + 1.0 / 2048.0), 0x3c00);
+ assert_eq!(from_f32(1.0 + 3.0 / 2048.0), 0x3c02);
+ assert_eq!(from_f32(1.0 + 1.0 / 2048.0 + 1.0 / 65536.0), 0x3c01);
+
+ for bits in (0..=0xffff_u16).filter(|&bits| !HalfFloat(bits).to_f32().is_nan()) {
+ assert_eq!(from_f32(to_f32(bits)), bits);
+ }
+ }
+
+ #[test]
+ fn packed_texels_are_premultiplied_by_their_alpha_bits() {
+ assert_eq!(premultiplied_unsigned_short_4444(0xfa56), 0x6426);
+ assert_eq!(premultiplied_unsigned_short_4444(0xfff0), 0x0000);
+ assert_eq!(premultiplied_unsigned_short_4444(0x123f), 0x123f);
+ assert_eq!(premultiplied_unsigned_short_4444(0xf008), 0x8008);
+
+ assert_eq!(premultiplied_unsigned_short_5551(0xfc01), 0xfc01);
+ assert_eq!(premultiplied_unsigned_short_5551(0xfffe), 0x0000);
+
+ let texel = |red: u32, green: u32, blue: u32, alpha: u32| (alpha << 30) | (blue << 20) | (green << 10) | red;
+ assert_eq!(
+ premultiplied_unsigned_int_2_10_10_10_rev(texel(1023, 300, 0, 2)),
+ texel(682, 200, 0, 2)
+ );
+ assert_eq!(
+ premultiplied_unsigned_int_2_10_10_10_rev(texel(1023, 1023, 1023, 0)),
+ texel(0, 0, 0, 0)
+ );
+ assert_eq!(
+ premultiplied_unsigned_int_2_10_10_10_rev(texel(5, 6, 7, 3)),
+ texel(5, 6, 7, 3)
+ );
+ assert_eq!(
+ premultiplied_unsigned_int_2_10_10_10_rev(texel(1000, 2, 1, 1)),
+ texel(333, 1, 0, 1)
+ );
+
+ let premultiplied_texels = |format, r#type, texels: &[u16]| {
+ converted_numbers(premultiplied(image(texels.len() as i32, 1, format, r#type)), texels)
+ };
+ assert_eq!(
+ premultiplied_texels(GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, &[0xfa56, 0xfff0]),
+ [0x6426, 0x0000]
+ );
+ assert_eq!(
+ premultiplied_texels(GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, &[0xfc01, 0xfffe]),
+ [0xfc01, 0x0000]
+ );
+ assert_eq!(
+ premultiplied_texels(GL_RGB, GL_UNSIGNED_SHORT_5_6_5, &[0xfffe, 0x0000]),
+ [0xfffe, 0x0000]
+ );
+ assert_eq!(
+ converted_numbers::<4, u32>(
+ premultiplied(image(1, 1, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV)),
+ &[texel(1023, 300, 0, 2)]
+ ),
+ [texel(682, 200, 0, 2)]
+ );
+ assert_eq!(
+ converted_numbers::<4, u32>(
+ premultiplied(image(1, 1, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV)),
+ &[texel(1023, 300, 0, 2)]
+ ),
+ [texel(1023, 300, 0, 2)]
+ );
+ }
+
+ #[test]
+ fn every_texel_layout_takes_as_many_bytes_as_gl_unpacks() {
+ let bytes_per_pixel = |format, r#type| texel_layout(format, r#type).map(|layout| layout.bytes_per_pixel);
+ assert_eq!(bytes_per_pixel(GL_RGBA, GL_UNSIGNED_BYTE), Some(4));
+ assert_eq!(bytes_per_pixel(GL_RGB, GL_UNSIGNED_BYTE), Some(3));
+ assert_eq!(bytes_per_pixel(GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE), Some(2));
+ assert_eq!(bytes_per_pixel(GL_LUMINANCE, GL_UNSIGNED_BYTE), Some(1));
+ assert_eq!(bytes_per_pixel(GL_ALPHA, GL_UNSIGNED_BYTE), Some(1));
+ assert_eq!(bytes_per_pixel(GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4), Some(2));
+ assert_eq!(bytes_per_pixel(GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1), Some(2));
+ assert_eq!(bytes_per_pixel(GL_RGB, GL_UNSIGNED_SHORT_5_6_5), Some(2));
+ assert_eq!(bytes_per_pixel(GL_RGBA, GL_FLOAT), Some(16));
+ assert_eq!(bytes_per_pixel(GL_LUMINANCE_ALPHA, GL_HALF_FLOAT), Some(4));
+ assert_eq!(bytes_per_pixel(GL_RGBA_INTEGER, GL_UNSIGNED_INT), Some(16));
+ assert_eq!(bytes_per_pixel(GL_RGB_INTEGER, GL_SHORT), Some(6));
+ assert_eq!(bytes_per_pixel(GL_RG, GL_HALF_FLOAT), Some(4));
+ assert_eq!(bytes_per_pixel(GL_RG_INTEGER, GL_INT), Some(8));
+ assert_eq!(bytes_per_pixel(GL_RED, GL_BYTE), Some(1));
+ assert_eq!(bytes_per_pixel(GL_RED_INTEGER, GL_UNSIGNED_SHORT), Some(2));
+ assert_eq!(bytes_per_pixel(GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV), Some(4));
+ assert_eq!(bytes_per_pixel(GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV), Some(4));
+ assert_eq!(bytes_per_pixel(GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV), Some(4));
+ assert_eq!(bytes_per_pixel(GL_DEPTH_COMPONENT, GL_UNSIGNED_INT), Some(4));
+ assert_eq!(bytes_per_pixel(GL_DEPTH_COMPONENT, GL_FLOAT), Some(4));
+ assert_eq!(bytes_per_pixel(GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8), Some(4));
+ assert_eq!(
+ bytes_per_pixel(GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV),
+ Some(8)
+ );
+ assert_eq!(bytes_per_pixel(GL_RGBA, 0x1234), None);
+ assert_eq!(bytes_per_pixel(0x1234, GL_UNSIGNED_BYTE), None);
+ }
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/state.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/state.rs
index 0f01ed9ea1c..53972018e11 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/state.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/state.rs
@@ -14,12 +14,13 @@ use core::ops::Deref;
use libjs_rust::api::{ForeignCellSlot, Gc, Realm, Trace, Visitor, Vm};
-use super::canvas::{Canvas, PixelUnpackState, TexImageSourceFrame, TexImageSourceKind, read_tex_image_source};
+use super::canvas::{Canvas, TexImageSourceFrame, TexImageSourceKind, read_tex_image_source};
use super::constants::{
GL_BROWSER_DEFAULT_WEBGL, GL_CONTEXT_LOST_WEBGL, GL_INVALID_OPERATION, GL_INVALID_VALUE, GL_NO_ERROR,
};
use super::extensions::{EnabledEmptyExtension, EnabledExtension};
use super::host::{CppTransport, Frame};
+use super::pixel_unpacking::PixelUnpackState;
use super::proxy::custom::FrameUpload;
use super::proxy::recorder::Proxy;
use super::web_gl_buffer::WebGLBuffer;
@@ -380,6 +381,18 @@ impl ContextState {
Some((frame, upload))
}
+ /// https://registry.khronos.org/webgl/specs/latest/2.0/#PIXEL_STORE_PARAM_CONSTRAINTS
+ /// If UNPACK_FLIP_Y_WEBGL or UNPACK_PREMULTIPLY_ALPHA_WEBGL is set to true, texImage3D and texSubImage3D generate
+ /// an INVALID_OPERATION error if they upload data from a PIXEL_UNPACK_BUFFER or a non-null client side
+ /// ArrayBufferView.
+ pub(crate) fn unpack_flags_allow_3d_upload_from_array_buffer_view(&self) -> bool {
+ if self.unpack_flip_y.get() || self.unpack_premultiply_alpha.get() {
+ self.set_error(GL_INVALID_OPERATION);
+ return false;
+ }
+ true
+ }
+
pub(crate) fn uniform_location_handle(&self, location: Ref<WebGLUniformLocation>) -> Option<i32> {
let handle = location.handle(self.current_program.get()).ok();
if handle.is_none() {
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_base.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_base.rs
index d62807ba2a4..ee973415c12 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_base.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_base.rs
@@ -332,6 +332,9 @@ impl RenderingContext<WebGL2> {
return Ok(());
};
+ if !self.unpack_flags_allow_3d_upload_from_array_buffer_view() {
+ return Ok(());
+ }
let Ok(bytes) = source_bytes(src_data.into(), 0, 0) else {
self.set_error(GL_INVALID_OPERATION);
return Ok(());
@@ -373,6 +376,9 @@ impl RenderingContext<WebGL2> {
src_data: ArrayBufferView,
src_offset: u64,
) -> WebIdlResult<()> {
+ if !self.unpack_flags_allow_3d_upload_from_array_buffer_view() {
+ return Ok(());
+ }
let Ok(bytes) = source_bytes(src_data.into(), src_offset, 0) else {
self.set_error(GL_INVALID_OPERATION);
return Ok(());
@@ -447,6 +453,9 @@ impl RenderingContext<WebGL2> {
return Ok(());
};
+ if !self.unpack_flags_allow_3d_upload_from_array_buffer_view() {
+ return Ok(());
+ }
let Ok(bytes) = source_bytes(src_data.into(), src_offset, 0) else {
self.set_error(GL_INVALID_OPERATION);
return Ok(());
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_overloads.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_overloads.rs
index 3bf839d98e8..5cdc6620e8a 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_overloads.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl2_rendering_context_overloads.rs
@@ -165,7 +165,7 @@ impl RenderingContext<WebGL2> {
let buf_size = texture_data.gl_size();
// SAFETY: Recording the command neither runs script nor allocates a cell.
unsafe {
- self.upload_through_nullable_pointer(&texture_data, |texture_data| {
+ self.upload_texture_data(&texture_data, |texture_data| {
self.proxy.tex_image2d_robust_angle(
target,
level,
@@ -252,7 +252,7 @@ impl RenderingContext<WebGL2> {
let buf_size = texture_data.gl_size();
// SAFETY: Recording the command neither runs script nor allocates a cell.
unsafe {
- self.upload_through_nullable_pointer(&texture_data, |texture_data| {
+ self.upload_texture_data(&texture_data, |texture_data| {
self.proxy.tex_image2d_robust_angle(
target,
level,
@@ -300,7 +300,7 @@ impl RenderingContext<WebGL2> {
let buf_size = texture_data.gl_size();
// SAFETY: Recording the command neither runs script nor allocates a cell.
unsafe {
- self.upload_through_nullable_pointer(&texture_data, |texture_data| {
+ self.upload_texture_data(&texture_data, |texture_data| {
self.proxy.tex_sub_image2d_robust_angle(
target,
level,
@@ -381,7 +381,7 @@ impl RenderingContext<WebGL2> {
let buf_size = texture_data.gl_size();
// SAFETY: Recording the command neither runs script nor allocates a cell.
unsafe {
- self.upload_through_nullable_pointer(&texture_data, |texture_data| {
+ self.upload_texture_data(&texture_data, |texture_data| {
self.proxy.tex_sub_image2d_robust_angle(
target,
level,
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl_rendering_context_overloads.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl_rendering_context_overloads.rs
index 473098343b5..47306f5bce7 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl_rendering_context_overloads.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/web_gl_rendering_context_overloads.rs
@@ -232,7 +232,7 @@ impl RenderingContext<WebGL1> {
let buf_size = texture_data.gl_size();
// SAFETY: Recording the command neither runs script nor allocates a cell.
unsafe {
- self.upload_through_nullable_pointer(&texture_data, |texture_data| {
+ self.upload_texture_data(&texture_data, |texture_data| {
self.proxy.tex_image2d_robust_angle(
target,
level,
@@ -331,7 +331,7 @@ impl RenderingContext<WebGL1> {
let buf_size = texture_data.gl_size();
// SAFETY: Recording the command neither runs script nor allocates a cell.
unsafe {
- self.upload_through_nullable_pointer(&texture_data, |texture_data| {
+ self.upload_texture_data(&texture_data, |texture_data| {
self.proxy.tex_sub_image2d_robust_angle(
target,
level,
diff --git a/Tests/LibWeb/Text/expected/canvas/webgl-array-buffer-view-unpack-flip-y-premultiply-alpha.txt b/Tests/LibWeb/Text/expected/canvas/webgl-array-buffer-view-unpack-flip-y-premultiply-alpha.txt
new file mode 100644
index 00000000000..dd44d01b868
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/canvas/webgl-array-buffer-view-unpack-flip-y-premultiply-alpha.txt
@@ -0,0 +1,30 @@
+RGBA/UNSIGNED_BYTE, texImage2D, no flags: NO_ERROR, texture rows: 200,100,50,51 | 255,255,255,0
+RGBA/UNSIGNED_BYTE, texImage2D, flipY: NO_ERROR, texture rows: 255,255,255,0 | 200,100,50,51
+RGBA/UNSIGNED_BYTE, texImage2D, premultiplyAlpha: NO_ERROR, texture rows: 40,20,10,51 | 0,0,0,0
+RGBA/UNSIGNED_BYTE, texImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 0,0,0,0 | 40,20,10,51
+RGBA/UNSIGNED_BYTE, texSubImage2D, no flags: NO_ERROR, texture rows: 200,100,50,51 | 255,255,255,0
+RGBA/UNSIGNED_BYTE, texSubImage2D, flipY: NO_ERROR, texture rows: 255,255,255,0 | 200,100,50,51
+RGBA/UNSIGNED_BYTE, texSubImage2D, premultiplyAlpha: NO_ERROR, texture rows: 40,20,10,51 | 0,0,0,0
+RGBA/UNSIGNED_BYTE, texSubImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 0,0,0,0 | 40,20,10,51
+RGB/UNSIGNED_BYTE, texImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 70,80,90,255 100,110,120,255 | 10,20,30,255 40,50,60,255
+RGB/UNSIGNED_BYTE, texSubImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 70,80,90,255 100,110,120,255 | 10,20,30,255 40,50,60,255
+LUMINANCE_ALPHA/UNSIGNED_BYTE, texImage2D, premultiplyAlpha: NO_ERROR, texture rows: 40,40,40,51 | 100,100,100,255
+LUMINANCE_ALPHA/UNSIGNED_BYTE, texImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 100,100,100,255 | 40,40,40,51
+LUMINANCE_ALPHA/UNSIGNED_BYTE, texSubImage2D, premultiplyAlpha: NO_ERROR, texture rows: 40,40,40,51 | 100,100,100,255
+LUMINANCE_ALPHA/UNSIGNED_BYTE, texSubImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 100,100,100,255 | 40,40,40,51
+ALPHA/UNSIGNED_BYTE, texImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 0,0,0,255 | 0,0,0,51
+ALPHA/UNSIGNED_BYTE, texSubImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 0,0,0,255 | 0,0,0,51
+LUMINANCE/UNSIGNED_BYTE, texImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 255,255,255,255 | 51,51,51,255
+LUMINANCE/UNSIGNED_BYTE, texSubImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 255,255,255,255 | 51,51,51,255
+RGBA/UNSIGNED_SHORT_4_4_4_4, texImage2D, premultiplyAlpha: NO_ERROR, texture rows: 102,68,34,102 | 0,0,0,0
+RGBA/UNSIGNED_SHORT_4_4_4_4, texImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 0,0,0,0 | 102,68,34,102
+RGBA/UNSIGNED_SHORT_4_4_4_4, texSubImage2D, premultiplyAlpha: NO_ERROR, texture rows: 102,68,34,102 | 0,0,0,0
+RGBA/UNSIGNED_SHORT_4_4_4_4, texSubImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 0,0,0,0 | 102,68,34,102
+RGBA/UNSIGNED_SHORT_5_5_5_1, texImage2D, premultiplyAlpha: NO_ERROR, texture rows: 255,132,0,255 | 0,0,0,0
+RGBA/UNSIGNED_SHORT_5_5_5_1, texImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 0,0,0,0 | 255,132,0,255
+RGBA/UNSIGNED_SHORT_5_5_5_1, texSubImage2D, premultiplyAlpha: NO_ERROR, texture rows: 255,132,0,255 | 0,0,0,0
+RGBA/UNSIGNED_SHORT_5_5_5_1, texSubImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 0,0,0,0 | 255,132,0,255
+RGB/UNSIGNED_SHORT_5_6_5, texImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 0,255,255,255 | 255,0,0,255
+RGB/UNSIGNED_SHORT_5_6_5, texSubImage2D, flipY+premultiplyAlpha: NO_ERROR, texture rows: 0,255,255,255 | 255,0,0,255
+too short source, texImage2D, flipY+premultiplyAlpha: INVALID_OPERATION
+too short source, texSubImage2D, flipY+premultiplyAlpha: INVALID_OPERATION
diff --git a/Tests/LibWeb/Text/expected/canvas/webgl2-array-buffer-view-unpack-flip-y-premultiply-alpha.txt b/Tests/LibWeb/Text/expected/canvas/webgl2-array-buffer-view-unpack-flip-y-premultiply-alpha.txt
new file mode 100644
index 00000000000..c762b7d23da
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/canvas/webgl2-array-buffer-view-unpack-flip-y-premultiply-alpha.txt
@@ -0,0 +1,34 @@
+row length 3, skip 1 pixel and 1 row, flipY=false, premultiplyAlpha=false
+ texImage2D: NO_ERROR, texture rows: 200,100,50,51 255,255,255,0 | 10,20,30,255 10,20,30,255
+ texSubImage2D: NO_ERROR, texture rows: 200,100,50,51 255,255,255,0 | 10,20,30,255 10,20,30,255
+row length 3, skip 1 pixel and 1 row, flipY=true, premultiplyAlpha=false
+ texImage2D: NO_ERROR, texture rows: 10,20,30,255 10,20,30,255 | 200,100,50,51 255,255,255,0
+ texSubImage2D: NO_ERROR, texture rows: 10,20,30,255 10,20,30,255 | 200,100,50,51 255,255,255,0
+row length 3, skip 1 pixel and 1 row, flipY=false, premultiplyAlpha=true
+ texImage2D: NO_ERROR, texture rows: 40,20,10,51 0,0,0,0 | 10,20,30,255 10,20,30,255
+ texSubImage2D: NO_ERROR, texture rows: 40,20,10,51 0,0,0,0 | 10,20,30,255 10,20,30,255
+row length 3, skip 1 pixel and 1 row, flipY=true, premultiplyAlpha=true
+ texImage2D: NO_ERROR, texture rows: 10,20,30,255 10,20,30,255 | 40,20,10,51 0,0,0,0
+ texSubImage2D: NO_ERROR, texture rows: 10,20,30,255 10,20,30,255 | 40,20,10,51 0,0,0,0
+source data unchanged: true
+texImage2D with source offset, flipY+premultiplyAlpha: NO_ERROR, texture rows: 0,0,0,0 | 40,20,10,51
+texSubImage2D with source offset, flipY+premultiplyAlpha: NO_ERROR, texture rows: 40,20,10,51 | 10,20,30,255
+RGBA32F/FLOAT, flipY+premultiplyAlpha: NO_ERROR, texture rows: 0.375,0.5,0.25,0.5 | 0.25,0.1875,0.125,0.25
+RGBA16F/HALF_FLOAT, flipY+premultiplyAlpha: NO_ERROR, texture rows: 0.375,0.5,0.25,0.5 | 0.25,0.1875,0.125,0.25
+RGB10_A2/UNSIGNED_INT_2_10_10_10_REV, premultiplyAlpha: NO_ERROR, texture rows: 682,200,0,2 | 0,0,1023,3
+texImage3D from an ArrayBufferView, flipY=false, premultiplyAlpha=false: NO_ERROR
+texImage3D from an ArrayBufferView with source offset, flipY=false, premultiplyAlpha=false: NO_ERROR
+texImage3D from null, flipY=false, premultiplyAlpha=false: NO_ERROR
+texSubImage3D from an ArrayBufferView, flipY=false, premultiplyAlpha=false: NO_ERROR
+texImage3D from an ArrayBufferView, flipY=true, premultiplyAlpha=false: INVALID_OPERATION
+texImage3D from an ArrayBufferView with source offset, flipY=true, premultiplyAlpha=false: INVALID_OPERATION
+texImage3D from null, flipY=true, premultiplyAlpha=false: NO_ERROR
+texSubImage3D from an ArrayBufferView, flipY=true, premultiplyAlpha=false: INVALID_OPERATION
+texImage3D from an ArrayBufferView, flipY=false, premultiplyAlpha=true: INVALID_OPERATION
+texImage3D from an ArrayBufferView with source offset, flipY=false, premultiplyAlpha=true: INVALID_OPERATION
+texImage3D from null, flipY=false, premultiplyAlpha=true: NO_ERROR
+texSubImage3D from an ArrayBufferView, flipY=false, premultiplyAlpha=true: INVALID_OPERATION
+texImage3D from an ArrayBufferView, flipY=true, premultiplyAlpha=true: INVALID_OPERATION
+texImage3D from an ArrayBufferView with source offset, flipY=true, premultiplyAlpha=true: INVALID_OPERATION
+texImage3D from null, flipY=true, premultiplyAlpha=true: NO_ERROR
+texSubImage3D from an ArrayBufferView, flipY=true, premultiplyAlpha=true: INVALID_OPERATION
diff --git a/Tests/LibWeb/Text/input/canvas/webgl-array-buffer-view-unpack-flip-y-premultiply-alpha.html b/Tests/LibWeb/Text/input/canvas/webgl-array-buffer-view-unpack-flip-y-premultiply-alpha.html
new file mode 100644
index 00000000000..6de58e8d99a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/canvas/webgl-array-buffer-view-unpack-flip-y-premultiply-alpha.html
@@ -0,0 +1,212 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script src="webgl-helpers.js"></script>
+<script>
+ const vertexShaderSource = `
+ attribute vec2 position;
+ varying vec2 textureCoordinate;
+ void main() {
+ textureCoordinate = position * 0.5 + 0.5;
+ gl_Position = vec4(position, 0.0, 1.0);
+ }`;
+ const fragmentShaderSource = `
+ precision highp float;
+ uniform sampler2D sampler;
+ varying vec2 textureCoordinate;
+ void main() {
+ gl_FragColor = texture2D(sampler, textureCoordinate);
+ }`;
+
+ function compileShader(gl, type, source) {
+ const shader = gl.createShader(type);
+ gl.shaderSource(shader, source);
+ gl.compileShader(shader);
+ return shader;
+ }
+
+ function setUpTextureDrawing(gl) {
+ const program = gl.createProgram();
+ gl.attachShader(program, compileShader(gl, gl.VERTEX_SHADER, vertexShaderSource));
+ gl.attachShader(program, compileShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource));
+ gl.bindAttribLocation(program, 0, "position");
+ gl.linkProgram(program);
+ gl.useProgram(program);
+
+ const buffer = gl.createBuffer();
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1]), gl.STATIC_DRAW);
+ gl.enableVertexAttribArray(0);
+ gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
+ }
+
+ function createTexture(gl) {
+ const texture = gl.createTexture();
+ gl.bindTexture(gl.TEXTURE_2D, texture);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
+ return texture;
+ }
+
+ // Draws the texture into an RGBA framebuffer of its size, so that texels of every format read back as RGBA bytes.
+ // The rows come back in the order in which they are in the texture, the first uploaded row first.
+ function readTexture(gl, texture, width, height) {
+ const target = createTexture(gl);
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
+ const framebuffer = gl.createFramebuffer();
+ gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, target, 0);
+
+ gl.bindTexture(gl.TEXTURE_2D, texture);
+ gl.viewport(0, 0, width, height);
+ gl.drawArrays(gl.TRIANGLES, 0, 6);
+
+ const pixels = new Uint8Array(width * height * 4);
+ gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
+
+ const rows = [];
+ for (let row = 0; row < height; ++row) {
+ const texels = [];
+ for (let column = 0; column < width; ++column) {
+ const offset = (row * width + column) * 4;
+ texels.push(Array.from(pixels.slice(offset, offset + 4)).join(","));
+ }
+ rows.push(texels.join(" "));
+ }
+ return rows.join(" | ");
+ }
+
+ function setUnpackFlags(gl, flipY, premultiplyAlpha) {
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha);
+ }
+
+ function flagsLabel(flipY, premultiplyAlpha) {
+ const flags = [];
+ if (flipY)
+ flags.push("flipY");
+ if (premultiplyAlpha)
+ flags.push("premultiplyAlpha");
+ return flags.length ? flags.join("+") : "no flags";
+ }
+
+ const flagCombinations = [
+ [false, false],
+ [true, false],
+ [false, true],
+ [true, true],
+ ];
+
+ function uploadAndRead(gl, { format, type, width, height, data }, useTexSubImage2D) {
+ const texture = createTexture(gl);
+ if (useTexSubImage2D) {
+ gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, format, type, new data.constructor(data.length));
+ gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
+ } else {
+ gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, format, type, data);
+ }
+ const error = errorName(gl, gl.getError());
+ setUnpackFlags(gl, false, false);
+ return `${error}, texture rows: ${readTexture(gl, texture, width, height)}`;
+ }
+
+ function runCase(gl, label, image, flagCombinationsToRun = flagCombinations) {
+ for (const useTexSubImage2D of [false, true]) {
+ for (const [flipY, premultiplyAlpha] of flagCombinationsToRun) {
+ const dataBefore = Array.from(image.data).join(",");
+ setUnpackFlags(gl, flipY, premultiplyAlpha);
+ const result = uploadAndRead(gl, image, useTexSubImage2D);
+ const dataIsUnchanged = Array.from(image.data).join(",") === dataBefore;
+ println(`${label}, ${useTexSubImage2D ? "texSubImage2D" : "texImage2D"}, ${flagsLabel(flipY, premultiplyAlpha)}: ${result}${dataIsUnchanged ? "" : " (source data changed)"}`);
+ }
+ }
+ }
+
+ test(() => {
+ const canvas = document.createElement("canvas");
+ const gl = canvas.getContext("webgl");
+ setUpTextureDrawing(gl);
+ clearErrors(gl);
+
+ // A pixel with one fifth alpha, and a transparent one with white color.
+ runCase(gl, "RGBA/UNSIGNED_BYTE", {
+ format: gl.RGBA,
+ type: gl.UNSIGNED_BYTE,
+ width: 1,
+ height: 2,
+ data: new Uint8Array([200, 100, 50, 51, 255, 255, 255, 0]),
+ });
+
+ // Rows of six bytes padded to eight by the default UNPACK_ALIGNMENT of 4. The padding is not a pixel.
+ runCase(gl, "RGB/UNSIGNED_BYTE", {
+ format: gl.RGB,
+ type: gl.UNSIGNED_BYTE,
+ width: 2,
+ height: 2,
+ data: new Uint8Array([10, 20, 30, 40, 50, 60, 99, 99, 70, 80, 90, 100, 110, 120]),
+ }, [[true, true]]);
+
+ runCase(gl, "LUMINANCE_ALPHA/UNSIGNED_BYTE", {
+ format: gl.LUMINANCE_ALPHA,
+ type: gl.UNSIGNED_BYTE,
+ width: 1,
+ height: 2,
+ data: new Uint8Array([200, 51, 99, 99, 100, 255]),
+ }, [[false, true], [true, true]]);
+
+ runCase(gl, "ALPHA/UNSIGNED_BYTE", {
+ format: gl.ALPHA,
+ type: gl.UNSIGNED_BYTE,
+ width: 1,
+ height: 2,
+ data: new Uint8Array([51, 99, 99, 99, 255]),
+ }, [[true, true]]);
+
+ runCase(gl, "LUMINANCE/UNSIGNED_BYTE", {
+ format: gl.LUMINANCE,
+ type: gl.UNSIGNED_BYTE,
+ width: 1,
+ height: 2,
+ data: new Uint8Array([51, 99, 99, 99, 255]),
+ }, [[true, true]]);
+
+ // Red 15, green 10, blue 5 and alpha 6 of 15, then transparent white.
+ runCase(gl, "RGBA/UNSIGNED_SHORT_4_4_4_4", {
+ format: gl.RGBA,
+ type: gl.UNSIGNED_SHORT_4_4_4_4,
+ width: 1,
+ height: 2,
+ data: new Uint16Array([0xfa56, 0x9999, 0xfff0]),
+ }, [[false, true], [true, true]]);
+
+ // Opaque red with half green, then transparent white.
+ runCase(gl, "RGBA/UNSIGNED_SHORT_5_5_5_1", {
+ format: gl.RGBA,
+ type: gl.UNSIGNED_SHORT_5_5_5_1,
+ width: 1,
+ height: 2,
+ data: new Uint16Array([0xfc01, 0x9999, 0xfffe]),
+ }, [[false, true], [true, true]]);
+
+ // Red, then cyan.
+ runCase(gl, "RGB/UNSIGNED_SHORT_5_6_5", {
+ format: gl.RGB,
+ type: gl.UNSIGNED_SHORT_5_6_5,
+ width: 1,
+ height: 2,
+ data: new Uint16Array([0xf800, 0x9999, 0x07ff]),
+ }, [[true, true]]);
+
+ // A source too short for the image is an error, whatever the flags.
+ createTexture(gl);
+ setUnpackFlags(gl, true, true);
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 2, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array(7));
+ printError("too short source, texImage2D, flipY+premultiplyAlpha", gl);
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 2, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array(8));
+ gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 1, 2, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array(7));
+ printError("too short source, texSubImage2D, flipY+premultiplyAlpha", gl);
+ setUnpackFlags(gl, false, false);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/canvas/webgl2-array-buffer-view-unpack-flip-y-premultiply-alpha.html b/Tests/LibWeb/Text/input/canvas/webgl2-array-buffer-view-unpack-flip-y-premultiply-alpha.html
new file mode 100644
index 00000000000..d6a525cadff
--- /dev/null
+++ b/Tests/LibWeb/Text/input/canvas/webgl2-array-buffer-view-unpack-flip-y-premultiply-alpha.html
@@ -0,0 +1,162 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script src="webgl-helpers.js"></script>
+<script>
+ function createTexture(gl, target = gl.TEXTURE_2D) {
+ const texture = gl.createTexture();
+ gl.bindTexture(target, texture);
+ gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
+ gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
+ return texture;
+ }
+
+ function setUnpackFlags(gl, flipY, premultiplyAlpha) {
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha);
+ }
+
+ // The rows come back in the order in which they are in the texture, the first uploaded row first.
+ function readTexture(gl, texture, width, height, format, type, ArrayType) {
+ const framebuffer = gl.createFramebuffer();
+ gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
+ const pixels = new ArrayType(width * height * 4);
+ gl.readPixels(0, 0, width, height, format, type, pixels);
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
+ gl.deleteFramebuffer(framebuffer);
+
+ const rows = [];
+ for (let row = 0; row < height; ++row) {
+ const texels = [];
+ for (let column = 0; column < width; ++column) {
+ const offset = (row * width + column) * 4;
+ texels.push(Array.from(pixels.slice(offset, offset + 4)).join(","));
+ }
+ rows.push(texels.join(" "));
+ }
+ return rows.join(" | ");
+ }
+
+ function readRGBA8Texture(gl, texture, width, height) {
+ return readTexture(gl, texture, width, height, gl.RGBA, gl.UNSIGNED_BYTE, Uint8Array);
+ }
+
+ function readFloatTexture(gl, texture, width, height) {
+ return readTexture(gl, texture, width, height, gl.RGBA, gl.FLOAT, Float32Array);
+ }
+
+ const transparentWhite = [255, 255, 255, 0];
+ const oneFifthOpaque = [200, 100, 50, 51];
+ const opaque = [10, 20, 30, 255];
+ const outside = [9, 9, 9, 9];
+
+ function runSubImageOfDataStoreTest(gl) {
+ // A data store of three by three pixels, of which the image is the two by two at the bottom right.
+ gl.pixelStorei(gl.UNPACK_ROW_LENGTH, 3);
+ gl.pixelStorei(gl.UNPACK_SKIP_PIXELS, 1);
+ gl.pixelStorei(gl.UNPACK_SKIP_ROWS, 1);
+ const data = new Uint8Array([
+ outside, outside, outside,
+ outside, oneFifthOpaque, transparentWhite,
+ outside, opaque, opaque,
+ ].flat());
+
+ for (const [flipY, premultiplyAlpha] of [[false, false], [true, false], [false, true], [true, true]]) {
+ const texture = createTexture(gl);
+ setUnpackFlags(gl, flipY, premultiplyAlpha);
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, 2, 2, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
+ const texImageError = errorName(gl, gl.getError());
+ const texImageRows = readRGBA8Texture(gl, texture, 2, 2);
+
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, 2, 2, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
+ gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 2, 2, gl.RGBA, gl.UNSIGNED_BYTE, data);
+ const texSubImageError = errorName(gl, gl.getError());
+ const texSubImageRows = readRGBA8Texture(gl, texture, 2, 2);
+
+ println(`row length 3, skip 1 pixel and 1 row, flipY=${flipY}, premultiplyAlpha=${premultiplyAlpha}`);
+ println(` texImage2D: ${texImageError}, texture rows: ${texImageRows}`);
+ println(` texSubImage2D: ${texSubImageError}, texture rows: ${texSubImageRows}`);
+ }
+ setUnpackFlags(gl, false, false);
+ gl.pixelStorei(gl.UNPACK_ROW_LENGTH, 0);
+ gl.pixelStorei(gl.UNPACK_SKIP_PIXELS, 0);
+ gl.pixelStorei(gl.UNPACK_SKIP_ROWS, 0);
+ println(`source data unchanged: ${data.join(",") === [outside, outside, outside, outside, oneFifthOpaque, transparentWhite, outside, opaque, opaque].flat().join(",")}`);
+ }
+
+ function runSourceOffsetTest(gl) {
+ // The image starts at the third element of the source.
+ const data = new Uint8Array([outside, outside, oneFifthOpaque, transparentWhite].flat());
+ const texture = createTexture(gl);
+ setUnpackFlags(gl, true, true);
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, 1, 2, 0, gl.RGBA, gl.UNSIGNED_BYTE, data, 8);
+ println(`texImage2D with source offset, flipY+premultiplyAlpha: ${errorName(gl, gl.getError())}, texture rows: ${readRGBA8Texture(gl, texture, 1, 2)}`);
+
+ setUnpackFlags(gl, true, true);
+ gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 1, 2, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([outside, opaque, oneFifthOpaque].flat()), 4);
+ println(`texSubImage2D with source offset, flipY+premultiplyAlpha: ${errorName(gl, gl.getError())}, texture rows: ${readRGBA8Texture(gl, texture, 1, 2)}`);
+ setUnpackFlags(gl, false, false);
+ }
+
+ function runFloatTest(gl, label, internalformat, type, data) {
+ const texture = createTexture(gl);
+ setUnpackFlags(gl, true, true);
+ gl.texImage2D(gl.TEXTURE_2D, 0, internalformat, 1, 2, 0, gl.RGBA, type, data);
+ const error = errorName(gl, gl.getError());
+ setUnpackFlags(gl, false, false);
+ println(`${label}, flipY+premultiplyAlpha: ${error}, texture rows: ${readFloatTexture(gl, texture, 1, 2)}`);
+ }
+
+ function runPackedTest(gl) {
+ // Red 1023, green 300, blue 0 and alpha 2 of 3, then opaque blue.
+ const data = new Uint32Array([(2 << 30) | (300 << 10) | 1023, (3 << 30) | (1023 << 20)]);
+ const texture = createTexture(gl);
+ setUnpackFlags(gl, false, true);
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB10_A2, 1, 2, 0, gl.RGBA, gl.UNSIGNED_INT_2_10_10_10_REV, data);
+ const error = errorName(gl, gl.getError());
+ setUnpackFlags(gl, false, false);
+
+ const framebuffer = gl.createFramebuffer();
+ gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
+ const texels = new Uint32Array(2);
+ gl.readPixels(0, 0, 1, 2, gl.RGBA, gl.UNSIGNED_INT_2_10_10_10_REV, texels);
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
+ const components = Array.from(texels).map(texel => [texel & 0x3ff, (texel >>> 10) & 0x3ff, (texel >>> 20) & 0x3ff, texel >>> 30].join(","));
+ println(`RGB10_A2/UNSIGNED_INT_2_10_10_10_REV, premultiplyAlpha: ${error}, texture rows: ${components.join(" | ")}`);
+ }
+
+ function run3DUploadTests(gl) {
+ const texture = createTexture(gl, gl.TEXTURE_2D_ARRAY);
+ const data = new Uint8Array(oneFifthOpaque);
+ for (const [flipY, premultiplyAlpha] of [[false, false], [true, false], [false, true], [true, true]]) {
+ setUnpackFlags(gl, flipY, premultiplyAlpha);
+ const label = `flipY=${flipY}, premultiplyAlpha=${premultiplyAlpha}`;
+
+ gl.texImage3D(gl.TEXTURE_2D_ARRAY, 0, gl.RGBA8, 1, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
+ printError(`texImage3D from an ArrayBufferView, ${label}`, gl);
+ gl.texImage3D(gl.TEXTURE_2D_ARRAY, 0, gl.RGBA8, 1, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, data, 0);
+ printError(`texImage3D from an ArrayBufferView with source offset, ${label}`, gl);
+ gl.texImage3D(gl.TEXTURE_2D_ARRAY, 0, gl.RGBA8, 1, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
+ printError(`texImage3D from null, ${label}`, gl);
+ gl.texSubImage3D(gl.TEXTURE_2D_ARRAY, 0, 0, 0, 0, 1, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, data);
+ printError(`texSubImage3D from an ArrayBufferView, ${label}`, gl);
+ }
+ setUnpackFlags(gl, false, false);
+ }
+
+ test(() => {
+ const canvas = document.createElement("canvas");
+ const gl = canvas.getContext("webgl2");
+ gl.getExtension("EXT_color_buffer_float");
+ clearErrors(gl);
+
+ runSubImageOfDataStoreTest(gl);
+ runSourceOffsetTest(gl);
+ runFloatTest(gl, "RGBA32F/FLOAT", gl.RGBA32F, gl.FLOAT, new Float32Array([1, 0.75, 0.5, 0.25, 0.75, 1, 0.5, 0.5]));
+ // The same values as half floats.
+ runFloatTest(gl, "RGBA16F/HALF_FLOAT", gl.RGBA16F, gl.HALF_FLOAT, new Uint16Array([0x3c00, 0x3a00, 0x3800, 0x3400, 0x3a00, 0x3c00, 0x3800, 0x3800]));
+ runPackedTest(gl);
+ run3DUploadTests(gl);
+ });
+</script>