rusticl: rework CLVec helper function to calculate bounds

We kinda need three things:
1. offset of a point in linear memory
2. size of access for a region
3. a mix of both

Signed-off-by: Karol Herbst <kherbst@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22449>
This commit is contained in:
Karol Herbst 2023-04-12 17:52:56 +02:00 committed by Marge Bot
parent 0a52002a1c
commit 3e118e8910
2 changed files with 24 additions and 16 deletions

View file

@ -1220,12 +1220,7 @@ pub fn enqueue_read_buffer_rect(
// CL_INVALID_VALUE if the region being read or written specified by (buffer_origin, region,
// buffer_row_pitch, buffer_slice_pitch) is out of bounds.
if !CLVec::is_in_bound(
r,
buf_ori,
[1, buffer_row_pitch, buffer_slice_pitch],
buf.size,
) {
if CLVec::calc_size(r + buf_ori, [1, buffer_row_pitch, buffer_slice_pitch]) > buf.size {
return Err(CL_INVALID_VALUE);
}
@ -1347,12 +1342,7 @@ pub fn enqueue_write_buffer_rect(
// CL_INVALID_VALUE if the region being read or written specified by (buffer_origin, region,
// buffer_row_pitch, buffer_slice_pitch) is out of bounds.
if !CLVec::is_in_bound(
r,
buf_ori,
[1, buffer_row_pitch, buffer_slice_pitch],
buf.size,
) {
if CLVec::calc_size(r + buf_ori, [1, buffer_row_pitch, buffer_slice_pitch]) > buf.size {
return Err(CL_INVALID_VALUE);
}
@ -1470,8 +1460,8 @@ pub fn enqueue_copy_buffer_rect(
// CL_INVALID_VALUE if (src_origin, region, src_row_pitch, src_slice_pitch) or (dst_origin,
// region, dst_row_pitch, dst_slice_pitch) require accessing elements outside the src_buffer
// and dst_buffer buffer objects respectively.
if !CLVec::is_in_bound(r, src_ori, [1, src_row_pitch, src_slice_pitch], src.size)
|| !CLVec::is_in_bound(r, dst_ori, [1, dst_row_pitch, dst_slice_pitch], dst.size)
if CLVec::calc_size(r + src_ori, [1, src_row_pitch, src_slice_pitch]) > src.size
|| CLVec::calc_size(r + dst_ori, [1, dst_row_pitch, dst_slice_pitch]) > dst.size
{
return Err(CL_INVALID_VALUE);
}

View file

@ -1,5 +1,6 @@
use rusticl_opencl_gen::*;
use std::borrow::Borrow;
use std::iter::Product;
#[macro_export]
@ -101,8 +102,25 @@ impl<T: Copy> CLVec<T> {
}
impl CLVec<usize> {
pub fn is_in_bound(base: Self, offset: Self, pitch: [usize; 3], size: usize) -> bool {
(base + offset - [1, 1, 1]) * pitch < size
/// returns the offset of point in linear memory.
pub fn calc_offset<T: Borrow<Self>>(point: T, pitch: [usize; 3]) -> usize {
*point.borrow() * pitch
}
/// returns the scalar size of the described region in linear memory.
pub fn calc_size<T: Borrow<Self>>(region: T, pitch: [usize; 3]) -> usize {
(*region.borrow() - [0, 1, 1]) * pitch
}
pub fn calc_offset_size<T1: Borrow<Self>, T2: Borrow<Self>>(
base: T1,
region: T2,
pitch: [usize; 3],
) -> (usize, usize) {
(
Self::calc_offset(base, pitch),
Self::calc_size(region, pitch),
)
}
}