diff --git a/src/gallium/frontends/rusticl/api/types.rs b/src/gallium/frontends/rusticl/api/types.rs index 6a8fc477b1b..fb094227138 100644 --- a/src/gallium/frontends/rusticl/api/types.rs +++ b/src/gallium/frontends/rusticl/api/types.rs @@ -6,6 +6,7 @@ use crate::core::memory::MemBase; use crate::core::program::Program; use crate::core::queue::Queue; +use mesa_rust_util::conversion::*; use rusticl_opencl_gen::*; use std::borrow::Borrow; @@ -341,9 +342,9 @@ where let vec: Result, _> = self .vals .iter() - .map(|v| T::try_from(*v).map_err(|_| CL_OUT_OF_HOST_MEMORY)) + .map(|v| T::try_from_with_err(*v, CL_OUT_OF_HOST_MEMORY)) .collect(); - vec?.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY) + vec?.try_into_with_err(CL_OUT_OF_HOST_MEMORY) } } diff --git a/src/gallium/frontends/rusticl/core/context.rs b/src/gallium/frontends/rusticl/core/context.rs index 07e4506f8da..09d68bb6813 100644 --- a/src/gallium/frontends/rusticl/core/context.rs +++ b/src/gallium/frontends/rusticl/core/context.rs @@ -10,6 +10,7 @@ use crate::impl_cl_type_trait; use mesa_rust::pipe::resource::*; use mesa_rust::pipe::screen::ResourceType; use mesa_rust_gen::*; +use mesa_rust_util::conversion::*; use mesa_rust_util::properties::Properties; use mesa_rust_util::ptr::TrackedPointers; use rusticl_opencl_gen::*; @@ -56,7 +57,7 @@ impl Context { copy: bool, res_type: ResourceType, ) -> CLResult>> { - let adj_size: u32 = size.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?; + let adj_size: u32 = size.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?; let mut res = HashMap::new(); for &dev in &self.devs { let mut resource = None; @@ -102,22 +103,12 @@ impl Context { ) -> CLResult>> { let pipe_format = format.to_pipe_format().unwrap(); - let width = desc - .image_width - .try_into() - .map_err(|_| CL_OUT_OF_HOST_MEMORY)?; - let height = desc - .image_height - .try_into() - .map_err(|_| CL_OUT_OF_HOST_MEMORY)?; - let depth = desc - .image_depth - .try_into() - .map_err(|_| CL_OUT_OF_HOST_MEMORY)?; + let width = desc.image_width.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?; + let height = desc.image_height.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?; + let depth = desc.image_depth.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?; let array_size = desc .image_array_size - .try_into() - .map_err(|_| CL_OUT_OF_HOST_MEMORY)?; + .try_into_with_err(CL_OUT_OF_HOST_MEMORY)?; let target = cl_mem_type_to_texture_target(desc.image_type); let mut res = HashMap::new(); diff --git a/src/gallium/frontends/rusticl/core/memory.rs b/src/gallium/frontends/rusticl/core/memory.rs index cca9c6caa28..2a966761d3e 100644 --- a/src/gallium/frontends/rusticl/core/memory.rs +++ b/src/gallium/frontends/rusticl/core/memory.rs @@ -16,6 +16,7 @@ use mesa_rust::pipe::resource::*; use mesa_rust::pipe::screen::ResourceType; use mesa_rust::pipe::transfer::*; use mesa_rust_gen::*; +use mesa_rust_util::conversion::*; use mesa_rust_util::properties::Properties; use mesa_rust_util::ptr::AllocSize; use mesa_rust_util::ptr::TrackedPointers; @@ -689,8 +690,7 @@ impl CLImageDescInfo for cl_image_desc { fn row_pitch(&self) -> CLResult { self.image_row_pitch - .try_into() - .map_err(|_| CL_OUT_OF_HOST_MEMORY) + .try_into_with_err(CL_OUT_OF_HOST_MEMORY) } fn slice_pitch(&self) -> usize { @@ -698,15 +698,11 @@ impl CLImageDescInfo for cl_image_desc { } fn width(&self) -> CLResult { - self.image_width - .try_into() - .map_err(|_| CL_OUT_OF_HOST_MEMORY) + self.image_width.try_into_with_err(CL_OUT_OF_HOST_MEMORY) } fn height(&self) -> CLResult { - self.image_height - .try_into() - .map_err(|_| CL_OUT_OF_HOST_MEMORY) + self.image_height.try_into_with_err(CL_OUT_OF_HOST_MEMORY) } } @@ -1186,11 +1182,7 @@ impl Buffer { [size, 1, 1].into(), CL_MEM_OBJECT_BUFFER, )?; - let dst_origin: [u32; 3] = [ - dst_offset.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?, - 0, - 0, - ]; + let dst_origin: [u32; 3] = [dst_offset.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?, 0, 0]; ctx.resource_copy_region(src_res, dst_res, &dst_origin, &bx); Ok(()) @@ -1265,8 +1257,8 @@ impl Buffer { ctx.clear_buffer( res, pattern, - offset.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?, - size.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?, + offset.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?, + size.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?, ); Ok(()) } @@ -1406,8 +1398,8 @@ impl Buffer { ctx.buffer_map( r, - offset.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?, - size.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?, + offset.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?, + size.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?, rw, ) .ok_or(CL_OUT_OF_RESOURCES) @@ -1440,9 +1432,9 @@ impl Buffer { ctx.buffer_subdata( r, - offset.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?, + offset.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?, ptr, - size.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?, + size.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?, ); Ok(()) } @@ -1946,9 +1938,7 @@ impl Image { res, &bx, src, - src_row_pitch - .try_into() - .map_err(|_| CL_OUT_OF_HOST_MEMORY)?, + src_row_pitch.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?, src_slice_pitch, ); } @@ -1971,7 +1961,7 @@ impl Image { res.pipe_sampler_view_template_2d_buffer(self.pipe_format, &self.buffer_2d_info()?) } else if res.is_buffer() { // we need to pass in the size of the buffer, not the width. - let size = self.size.try_into().map_err(|_| CL_OUT_OF_RESOURCES)?; + let size = self.size.try_into_with_err(CL_OUT_OF_RESOURCES)?; res.pipe_sampler_view_template_1d_buffer(self.pipe_format, size) } else { res.pipe_sampler_view_template() @@ -1992,7 +1982,7 @@ impl Image { &self.buffer_2d_info()?, )) } else if res.is_buffer() { - let size = self.size.try_into().map_err(|_| CL_OUT_OF_RESOURCES)?; + let size = self.size.try_into_with_err(CL_OUT_OF_RESOURCES)?; Ok(res.pipe_image_view_1d_buffer( self.pipe_format, read_write, diff --git a/src/gallium/frontends/rusticl/core/util.rs b/src/gallium/frontends/rusticl/core/util.rs index ffa10af058e..e14725f709c 100644 --- a/src/gallium/frontends/rusticl/core/util.rs +++ b/src/gallium/frontends/rusticl/core/util.rs @@ -1,6 +1,7 @@ use crate::api::{icd::CLResult, types::CLVec}; use mesa_rust_gen::*; +use mesa_rust_util::conversion::*; use rusticl_opencl_gen::*; use super::gl::is_cube_map_face; @@ -61,11 +62,11 @@ pub fn create_pipe_box( }; Ok(pipe_box { - x: base[0].try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?, - y: base[1].try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?, - z: base[2].try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?, - width: region[0].try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?, - height: region[1].try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?, - depth: region[2].try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?, + x: base[0].try_into_with_err(CL_OUT_OF_HOST_MEMORY)?, + y: base[1].try_into_with_err(CL_OUT_OF_HOST_MEMORY)?, + z: base[2].try_into_with_err(CL_OUT_OF_HOST_MEMORY)?, + width: region[0].try_into_with_err(CL_OUT_OF_HOST_MEMORY)?, + height: region[1].try_into_with_err(CL_OUT_OF_HOST_MEMORY)?, + depth: region[2].try_into_with_err(CL_OUT_OF_HOST_MEMORY)?, }) } diff --git a/src/gallium/frontends/rusticl/util/conversion.rs b/src/gallium/frontends/rusticl/util/conversion.rs new file mode 100644 index 00000000000..9064e63ec15 --- /dev/null +++ b/src/gallium/frontends/rusticl/util/conversion.rs @@ -0,0 +1,25 @@ +pub trait TryFromWithErr: Sized { + fn try_from_with_err(value: T, error: E) -> Result; +} + +impl TryFromWithErr for T +where + T: TryFrom, +{ + fn try_from_with_err(value: U, error: E) -> Result { + T::try_from(value).map_err(|_| error) + } +} + +pub trait TryIntoWithErr: Sized { + fn try_into_with_err(self, error: E) -> Result; +} + +impl TryIntoWithErr for U +where + T: TryFromWithErr, +{ + fn try_into_with_err(self, error: E) -> Result { + T::try_from_with_err(self, error) + } +} diff --git a/src/gallium/frontends/rusticl/util/lib.rs b/src/gallium/frontends/rusticl/util/lib.rs index a55f0fd0a43..74783832710 100644 --- a/src/gallium/frontends/rusticl/util/lib.rs +++ b/src/gallium/frontends/rusticl/util/lib.rs @@ -1,5 +1,6 @@ pub mod assert; pub mod bitset; +pub mod conversion; pub mod feature; pub mod math; pub mod properties;