From a0ba3b756a46f39f0c7410af3e8cc18ca12352ff Mon Sep 17 00:00:00 2001 From: Samson <16504129+sagudev@users.noreply.github.com> Date: Tue, 5 Nov 2024 16:38:48 +0100 Subject: [PATCH] Use `PixelUnpackData` in tex_image_{1,2,3}d (#322) * Use PixelUnpackData in tex_image_2d Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> * Use PixelUnpackData in tex_image_1d Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> * Use PixelUnpackData for tex_image_3d Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --------- Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- src/lib.rs | 6 +-- src/native.rs | 21 ++++++--- src/web_sys.rs | 125 +++++++++++++++++++++++++++++++------------------ 3 files changed, 98 insertions(+), 54 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e06c7ac..d2d0318 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1115,7 +1115,7 @@ pub trait HasContext: __private::Sealed { border: i32, format: u32, ty: u32, - pixels: Option<&[u8]>, + pixels: PixelUnpackData, ); unsafe fn compressed_tex_image_1d( @@ -1139,7 +1139,7 @@ pub trait HasContext: __private::Sealed { border: i32, format: u32, ty: u32, - pixels: Option<&[u8]>, + pixels: PixelUnpackData, ); unsafe fn tex_image_2d_multisample( @@ -1175,7 +1175,7 @@ pub trait HasContext: __private::Sealed { border: i32, format: u32, ty: u32, - pixels: Option<&[u8]>, + pixels: PixelUnpackData, ); unsafe fn compressed_tex_image_3d( diff --git a/src/native.rs b/src/native.rs index adbe111..85f839f 100644 --- a/src/native.rs +++ b/src/native.rs @@ -2465,7 +2465,7 @@ impl HasContext for Context { border: i32, format: u32, ty: u32, - pixels: Option<&[u8]>, + pixels: PixelUnpackData, ) { let gl = &self.raw; gl.TexImage1D( @@ -2476,7 +2476,10 @@ impl HasContext for Context { border, format, ty, - pixels.map(|p| p.as_ptr()).unwrap_or(std::ptr::null()) as *const std::ffi::c_void, + match pixels { + PixelUnpackData::BufferOffset(offset) => offset as *const std::ffi::c_void, + PixelUnpackData::Slice(data) => data.as_ptr() as *const std::ffi::c_void, + }, ); } @@ -2512,7 +2515,7 @@ impl HasContext for Context { border: i32, format: u32, ty: u32, - pixels: Option<&[u8]>, + pixels: PixelUnpackData, ) { let gl = &self.raw; gl.TexImage2D( @@ -2524,7 +2527,10 @@ impl HasContext for Context { border, format, ty, - pixels.map(|p| p.as_ptr()).unwrap_or(std::ptr::null()) as *const std::ffi::c_void, + match pixels { + PixelUnpackData::BufferOffset(offset) => offset as *const std::ffi::c_void, + PixelUnpackData::Slice(data) => data.as_ptr() as *const std::ffi::c_void, + }, ); } @@ -2583,7 +2589,7 @@ impl HasContext for Context { border: i32, format: u32, ty: u32, - pixels: Option<&[u8]>, + pixels: PixelUnpackData, ) { let gl = &self.raw; gl.TexImage3D( @@ -2596,7 +2602,10 @@ impl HasContext for Context { border, format, ty, - pixels.map(|p| p.as_ptr()).unwrap_or(std::ptr::null()) as *const std::ffi::c_void, + match pixels { + PixelUnpackData::BufferOffset(offset) => offset as *const std::ffi::c_void, + PixelUnpackData::Slice(data) => data.as_ptr() as *const std::ffi::c_void, + }, ); } diff --git a/src/web_sys.rs b/src/web_sys.rs index 95d8821..d819174 100644 --- a/src/web_sys.rs +++ b/src/web_sys.rs @@ -3960,7 +3960,7 @@ impl HasContext for Context { _border: i32, _format: u32, _ty: u32, - _pixels: Option<&[u8]>, + _pixels: PixelUnpackData, ) { panic!("Tex image 1D is not supported"); } @@ -3988,39 +3988,59 @@ impl HasContext for Context { border: i32, format: u32, ty: u32, - pixels: Option<&[u8]>, + pixels: PixelUnpackData, ) { - let pixels = pixels.map(|bytes| texture_data_view(ty, bytes)); match self.raw { RawRenderingContext::WebGl1(ref gl) => { - // TODO: Handle return value? - gl.tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_opt_array_buffer_view( - target, - level, - internal_format, - width, - height, - border, - format, - ty, - pixels.as_ref(), - ) - .unwrap(); + match pixels { + PixelUnpackData::BufferOffset(_offset) => panic!("Tex image 2D with offset is not supported"), + PixelUnpackData::Slice(data) => { + let data = texture_data_view(ty, data); + gl.tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_opt_array_buffer_view( + target, + level, + internal_format, + width, + height, + border, + format, + ty, + Some(&data), + ) + } + } + .unwrap(); // TODO: Handle return value? } RawRenderingContext::WebGl2(ref gl) => { - // TODO: Handle return value? - gl.tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_opt_array_buffer_view( - target, - level, - internal_format, - width, - height, - border, - format, - ty, - pixels.as_ref(), - ) - .unwrap(); + match pixels { + PixelUnpackData::BufferOffset(offset) => gl + .tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_i32( + target, + level, + internal_format, + width, + height, + border, + format, + ty, + offset as i32, + ), + PixelUnpackData::Slice(data) => { + let data = texture_data_view(ty, data); + gl.tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_opt_array_buffer_view( + target, + level, + internal_format, + width, + height, + border, + format, + ty, + Some(&data), + ) + } + } + .unwrap(); // TODO: Handle return value? } } } @@ -4084,26 +4104,41 @@ impl HasContext for Context { border: i32, format: u32, ty: u32, - pixels: Option<&[u8]>, + pixels: PixelUnpackData, ) { match self.raw { RawRenderingContext::WebGl1(ref _gl) => panic!("3d textures are not supported"), RawRenderingContext::WebGl2(ref gl) => { - let pixels = pixels.map(|bytes| texture_data_view(ty, bytes)); - // TODO: Handle return value? - gl.tex_image_3d_with_opt_array_buffer_view( - target, - level, - internal_format, - width, - height, - depth, - border, - format, - ty, - pixels.as_ref(), - ) - .unwrap(); + match pixels { + PixelUnpackData::BufferOffset(offset) => gl.tex_image_3d_with_i32( + target, + level, + internal_format, + width, + height, + border, + depth, + format, + ty, + offset as i32, + ), + PixelUnpackData::Slice(data) => { + let data = texture_data_view(ty, data); + gl.tex_image_3d_with_opt_array_buffer_view( + target, + level, + internal_format, + width, + height, + border, + depth, + format, + ty, + Some(&data), + ) + } + } + .unwrap(); // TODO: Handle return value? } } }