From 6a360ef618543d7aa9c7f733fa12972bb593345b Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Fri, 1 Nov 2024 18:44:16 +0100 Subject: [PATCH 1/3] Use PixelUnpackData in tex_image_2d Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- src/lib.rs | 2 +- src/native.rs | 7 +++-- src/web_sys.rs | 76 +++++++++++++++++++++++++++++++------------------- 3 files changed, 54 insertions(+), 31 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2b1c286..bdaa00a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1094,7 +1094,7 @@ pub trait HasContext: __private::Sealed { border: i32, format: u32, ty: u32, - pixels: Option<&[u8]>, + pixels: PixelUnpackData, ); unsafe fn tex_image_2d_multisample( diff --git a/src/native.rs b/src/native.rs index 967e311..3fbcde4 100644 --- a/src/native.rs +++ b/src/native.rs @@ -2482,7 +2482,7 @@ impl HasContext for Context { border: i32, format: u32, ty: u32, - pixels: Option<&[u8]>, + pixels: PixelUnpackData, ) { let gl = &self.raw; gl.TexImage2D( @@ -2494,7 +2494,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 42c9cdf..06cc056 100644 --- a/src/web_sys.rs +++ b/src/web_sys.rs @@ -3968,39 +3968,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? } } } From dd9150bdff73d63a6f8ddbc8822ea13174bb5bff Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Fri, 1 Nov 2024 18:52:09 +0100 Subject: [PATCH 2/3] Use PixelUnpackData in tex_image_1d Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- src/lib.rs | 2 +- src/native.rs | 7 +++++-- src/web_sys.rs | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bdaa00a..0cd2514 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1070,7 +1070,7 @@ pub trait HasContext: __private::Sealed { border: i32, format: u32, ty: u32, - pixels: Option<&[u8]>, + pixels: PixelUnpackData, ); unsafe fn compressed_tex_image_1d( diff --git a/src/native.rs b/src/native.rs index 3fbcde4..dd487d9 100644 --- a/src/native.rs +++ b/src/native.rs @@ -2435,7 +2435,7 @@ impl HasContext for Context { border: i32, format: u32, ty: u32, - pixels: Option<&[u8]>, + pixels: PixelUnpackData, ) { let gl = &self.raw; gl.TexImage1D( @@ -2446,7 +2446,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 06cc056..d8c138b 100644 --- a/src/web_sys.rs +++ b/src/web_sys.rs @@ -3940,7 +3940,7 @@ impl HasContext for Context { _border: i32, _format: u32, _ty: u32, - _pixels: Option<&[u8]>, + _pixels: PixelUnpackData, ) { panic!("Tex image 1D is not supported"); } From b8f4020f38ca0d14bc0afb09677c62360a3c30a7 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Fri, 1 Nov 2024 19:00:16 +0100 Subject: [PATCH 3/3] Use PixelUnpackData for tex_image_3d Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- src/lib.rs | 2 +- src/native.rs | 7 +++++-- src/web_sys.rs | 47 +++++++++++++++++++++++++++++++---------------- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0cd2514..f7bf75d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1130,7 +1130,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 dd487d9..1218af2 100644 --- a/src/native.rs +++ b/src/native.rs @@ -2559,7 +2559,7 @@ impl HasContext for Context { border: i32, format: u32, ty: u32, - pixels: Option<&[u8]>, + pixels: PixelUnpackData, ) { let gl = &self.raw; gl.TexImage3D( @@ -2572,7 +2572,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 d8c138b..9e74283 100644 --- a/src/web_sys.rs +++ b/src/web_sys.rs @@ -4084,26 +4084,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? } } }