rusticl: rework CLVec helper function to calculate bounds
authorKarol Herbst <kherbst@redhat.com>
Wed, 12 Apr 2023 15:52:56 +0000 (17:52 +0200)
committerMarge Bot <emma+marge@anholt.net>
Thu, 13 Apr 2023 20:23:44 +0000 (20:23 +0000)
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>

src/gallium/frontends/rusticl/api/memory.rs
src/gallium/frontends/rusticl/api/types.rs

index e23012eaa3f0d35844dbf27137d0279671c97f9d..0cb59b213347a8171297b9c362f657a164290469 100644 (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);
     }
index 954d0df533a09f636e2196a2a0ba616a4803953c..a4b7a32f8015a997883d5736e18b66d670654a25 100644 (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),
+        )
     }
 }