rusticl/mem: split Buffer::copy_to into Buffer and Image versions

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/27376>
This commit is contained in:
Karol Herbst 2024-01-30 13:06:24 +01:00 committed by Marge Bot
parent 28bff68d98
commit c0c6eca095
2 changed files with 75 additions and 77 deletions

View file

@ -1171,16 +1171,7 @@ fn enqueue_copy_buffer(
evs,
event,
false,
Box::new(move |q, ctx| {
src.copy_to(
q,
ctx,
&dst,
CLVec::new([src_offset, 0, 0]),
CLVec::new([dst_offset, 0, 0]),
&CLVec::new([size, 1, 1]),
)
}),
Box::new(move |q, ctx| src.copy_to_buffer(q, ctx, &dst, src_offset, dst_offset, size)),
)
// TODO
@ -1980,7 +1971,6 @@ fn enqueue_copy_buffer_to_image(
}
let region = unsafe { CLVec::from_raw(region) };
let src_origin = CLVec::new([src_offset, 0, 0]);
let dst_origin = unsafe { CLVec::from_raw(dst_origin) };
// CL_INVALID_VALUE if values in dst_origin and region do not follow rules described in the
@ -1995,7 +1985,7 @@ fn enqueue_copy_buffer_to_image(
evs,
event,
false,
Box::new(move |q, ctx| src.copy_to(q, ctx, &dst, src_origin, dst_origin, &region)),
Box::new(move |q, ctx| src.copy_to_image(q, ctx, &dst, src_offset, dst_origin, &region)),
)
//• CL_INVALID_MEM_OBJECT if src_buffer is not a valid buffer object or dst_image is not a valid image object or if dst_image is a 1D image buffer object created from src_buffer.

View file

@ -942,81 +942,89 @@ impl Buffer {
Ok(())
}
pub fn copy_to(
pub fn copy_to_buffer(
&self,
q: &Queue,
ctx: &PipeContext,
dst: &MemBase,
mut src_origin: CLVec<usize>,
mut dst_origin: CLVec<usize>,
dst: &Buffer,
src_offset: usize,
dst_offset: usize,
size: usize,
) -> CLResult<()> {
let src_offset = self.apply_offset(src_offset)?;
let dst_offset = dst.apply_offset(dst_offset)?;
let src_res = self.get_res_of_dev(q.device)?;
let dst_res = dst.get_res_of_dev(q.device)?;
let bx = create_pipe_box(
[src_offset, 0, 0].into(),
[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,
];
ctx.resource_copy_region(src_res, dst_res, &dst_origin, &bx);
Ok(())
}
pub fn copy_to_image(
&self,
q: &Queue,
ctx: &PipeContext,
dst: &Image,
src_offset: usize,
dst_origin: CLVec<usize>,
region: &CLVec<usize>,
) -> CLResult<()> {
let src_parent = self.to_parent(&mut src_origin[0]);
let dst_parent = dst.to_parent(&mut dst_origin[0]);
let src_res = src_parent.get_res_of_dev(q.device)?;
let dst_res = dst_parent.get_res_of_dev(q.device)?;
let src_offset = self.apply_offset(src_offset)?;
let bpp = dst.pixel_size().unwrap().into();
let src_pitch = [bpp, bpp * region[0], bpp * region[0] * region[1]];
let size = CLVec::calc_size(region, src_pitch);
let tx_src = self.tx(q, ctx, src_offset, size, RWFlags::RD)?;
// We just want to use sw_copy if mem objects have different types
// or if copy can have custom strides (image2d from buff/images)
if !dst.is_buffer() {
let bpp = dst.pixel_size().unwrap().into();
let mut dst_pitch = [0, 0, 0];
let src_pitch = [bpp, bpp * region[0], bpp * region[0] * region[1]];
// If image is created from a buffer, use image's slice and row pitch instead
let tx_dst;
let dst_pitch;
if dst.is_parent_buffer() {
dst_pitch = [
bpp,
dst.image_desc.row_pitch()? as usize,
dst.image_desc.slice_pitch(),
];
let (offset, size) = CLVec::calc_offset_size(src_origin, region, src_pitch);
let tx_src = src_parent.tx(q, ctx, offset, size, RWFlags::RD)?;
let tx_dst;
if dst_parent.is_buffer() {
// If image is created from a buffer, use image's slice and row pitch instead
dst_pitch[0] = bpp;
if dst.is_image_from_buffer() {
dst_pitch[1] = dst.image_desc.row_pitch()? as usize;
dst_pitch[2] = dst.image_desc.slice_pitch();
} else {
dst_pitch[1] = region[0] * bpp;
dst_pitch[2] = region[0] * region[1] * bpp;
}
let (offset, size) = CLVec::calc_offset_size(dst_origin, region, dst_pitch);
tx_dst = dst_parent.tx(q, ctx, offset, size, RWFlags::WR)?;
} else {
tx_dst = dst_parent.tx_image(
q,
ctx,
&create_pipe_box(dst_origin, *region, dst_parent.mem_type)?,
RWFlags::WR,
)?;
dst_pitch = [1, tx_dst.row_pitch() as usize, tx_dst.slice_pitch()];
}
// Those pitch values cannot have 0 value in its coordinates
debug_assert!(src_pitch[0] != 0 && src_pitch[1] != 0 && src_pitch[2] != 0);
debug_assert!(dst_pitch[0] != 0 && dst_pitch[1] != 0 && dst_pitch[2] != 0);
sw_copy(
tx_src.ptr(),
tx_dst.ptr(),
region,
&CLVec::default(),
src_pitch[1],
src_pitch[2],
&CLVec::default(),
dst_pitch[1],
dst_pitch[2],
bpp as u8,
)
let (offset, size) = CLVec::calc_offset_size(dst_origin, region, dst_pitch);
tx_dst = dst.tx(q, ctx, offset, size, RWFlags::WR)?;
} else {
let bx = create_pipe_box(src_origin, *region, src_parent.mem_type)?;
let mut dst_origin: [u32; 3] = dst_origin.try_into()?;
tx_dst = dst.tx_image(
q,
ctx,
&create_pipe_box(dst_origin, *region, dst.mem_type)?,
RWFlags::WR,
)?;
if src_parent.mem_type == CL_MEM_OBJECT_IMAGE1D_ARRAY {
(dst_origin[1], dst_origin[2]) = (dst_origin[2], dst_origin[1]);
}
ctx.resource_copy_region(src_res, dst_res, &dst_origin, &bx);
dst_pitch = [1, tx_dst.row_pitch() as usize, tx_dst.slice_pitch()];
}
// Those pitch values cannot have 0 value in its coordinates
debug_assert!(src_pitch[0] != 0 && src_pitch[1] != 0 && src_pitch[2] != 0);
debug_assert!(dst_pitch[0] != 0 && dst_pitch[1] != 0 && dst_pitch[2] != 0);
sw_copy(
tx_src.ptr(),
tx_dst.ptr(),
region,
&CLVec::default(),
src_pitch[1],
src_pitch[2],
&CLVec::default(),
dst_pitch[1],
dst_pitch[2],
bpp as u8,
);
Ok(())
}