summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--opengl-bindings/Cargo.toml1
-rw-r--r--opengl-bindings/src/texture.rs72
2 files changed, 71 insertions, 2 deletions
diff --git a/opengl-bindings/Cargo.toml b/opengl-bindings/Cargo.toml
index 081dd92..4d9d4e0 100644
--- a/opengl-bindings/Cargo.toml
+++ b/opengl-bindings/Cargo.toml
@@ -71,4 +71,5 @@ gl_commands = [
"BlendEquation",
"Scissor",
"DepthFunc",
+ "PixelStorei"
]
diff --git a/opengl-bindings/src/texture.rs b/opengl-bindings/src/texture.rs
index 6bb576f..ae5b2c3 100644
--- a/opengl-bindings/src/texture.rs
+++ b/opengl-bindings/src/texture.rs
@@ -1,5 +1,3 @@
-use std::borrow::Cow;
-
use crate::data_types::{Dimens, Vec2};
use crate::MaybeCurrentContextWithFns;
@@ -320,6 +318,16 @@ impl Texture
image: &[u8],
)
{
+ let mut is_pixel_unpack_alignment_changed = false;
+
+ if let Some(new_pixel_unpack_alignment) =
+ pixel_data_format.requires_adjust_pixel_unpack_alignment()
+ {
+ set_pixel_unpack_alignment(current_context, new_pixel_unpack_alignment);
+
+ is_pixel_unpack_alignment_changed = true;
+ }
+
unsafe {
current_context.fns().TextureSubImage2D(
self.texture,
@@ -333,6 +341,10 @@ impl Texture
image.as_ptr().cast(),
);
}
+
+ if is_pixel_unpack_alignment_changed {
+ set_pixel_unpack_alignment(current_context, PixelAlignment::default());
+ }
}
fn sub_image_3d(
@@ -345,6 +357,16 @@ impl Texture
image: &[u8],
)
{
+ let mut is_pixel_unpack_alignment_changed = false;
+
+ if let Some(new_pixel_unpack_alignment) =
+ pixel_data_format.requires_adjust_pixel_unpack_alignment()
+ {
+ set_pixel_unpack_alignment(current_context, new_pixel_unpack_alignment);
+
+ is_pixel_unpack_alignment_changed = true;
+ }
+
unsafe {
current_context.fns().TextureSubImage3D(
self.texture,
@@ -360,6 +382,10 @@ impl Texture
image.as_ptr().cast(),
);
}
+
+ if is_pixel_unpack_alignment_changed {
+ set_pixel_unpack_alignment(current_context, PixelAlignment::default());
+ }
}
}
@@ -484,6 +510,19 @@ impl PixelDataFormat
Self::DepthComponent(_) => 1,
}
}
+
+ fn requires_adjust_pixel_unpack_alignment(&self) -> Option<PixelAlignment>
+ {
+ if matches!(
+ self,
+ PixelDataFormat::Rgb(RgbDataType::UnsignedByte)
+ | PixelDataFormat::Srgb(SrgbDataType::UnsignedByte)
+ ) {
+ return Some(PixelAlignment::Byte);
+ }
+
+ None
+ }
}
impl Default for PixelDataFormat
@@ -593,3 +632,32 @@ fn check_image_buffer_len_correct_for_size(
Ok(())
}
+
+fn set_pixel_unpack_alignment(
+ current_context: &MaybeCurrentContextWithFns,
+ alignment: PixelAlignment,
+)
+{
+ let alignment = match alignment {
+ PixelAlignment::Byte => 1,
+ // PixelAlignment::TwoBytes => 2,
+ PixelAlignment::FourBytes => 4,
+ // PixelAlignment::EightBytes => 8,
+ };
+
+ unsafe {
+ current_context
+ .fns()
+ .PixelStorei(crate::sys::UNPACK_ALIGNMENT, alignment);
+ }
+}
+
+#[derive(Debug, Default, Clone, Copy)]
+enum PixelAlignment
+{
+ Byte,
+ // TwoBytes,
+ #[default]
+ FourBytes,
+ // EightBytes,
+}