From 2fef79767fb064d63b13d078f9d072ddcf9542a9 Mon Sep 17 00:00:00 2001 From: Karol Herbst Date: Tue, 6 Aug 2024 09:22:11 +0200 Subject: [PATCH] rusticl/image: take pitches into account when allocating memory for maps This is more correct than the previous code and the CL CTS relies on edge case behavior here, e.g. for 1Dbuffer images. I think part of that is not actually required by the spec, but whatever. Fixes: 7b22bc617bf ("rusticl/memory: complete rework on how mapping is implemented") Part-of: (cherry picked from commit 2484331e8282146a89f371a69b3f91f81cb882ed) --- .pick_status.json | 2 +- src/gallium/frontends/rusticl/core/memory.rs | 29 ++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/.pick_status.json b/.pick_status.json index fbc3d5b4feb..9cef3f985a7 100644 --- a/.pick_status.json +++ b/.pick_status.json @@ -3634,7 +3634,7 @@ "description": "rusticl/image: take pitches into account when allocating memory for maps", "nominated": true, "nomination_type": 1, - "resolution": 0, + "resolution": 1, "main_sha": null, "because_sha": "7b22bc617bf2db4120a438c1ad5e45992f638d82", "notes": null diff --git a/src/gallium/frontends/rusticl/core/memory.rs b/src/gallium/frontends/rusticl/core/memory.rs index 586f4edc32a..75eeba42994 100644 --- a/src/gallium/frontends/rusticl/core/memory.rs +++ b/src/gallium/frontends/rusticl/core/memory.rs @@ -1329,8 +1329,33 @@ impl Image { *row_pitch = self.image_desc.row_pitch()? as usize; *slice_pitch = self.image_desc.slice_pitch(); - let (offset, size) = - CLVec::calc_offset_size(origin, region, [pixel_size, *row_pitch, *slice_pitch]); + let offset = CLVec::calc_offset(origin, [pixel_size, *row_pitch, *slice_pitch]); + + // From the CL Spec: + // + // The pointer returned maps a 1D, 2D or 3D region starting at origin and is at least + // region[0] pixels in size for a 1D image, 1D image buffer or 1D image array, + // (image_row_pitch × region[1]) pixels in size for a 2D image or 2D image array, and + // (image_slice_pitch × region[2]) pixels in size for a 3D image. The result of a memory + // access outside this region is undefined. + // + // It's not guaranteed that the row_pitch is taken into account for 1D images, but the CL + // CTS relies on this behavior. + // + // Also note, that the spec wording is wrong in regards to arrays, which need to take the + // image_slice_pitch into account. + let size = if self.image_desc.is_array() || self.image_desc.dims() == 3 { + debug_assert_ne!(*slice_pitch, 0); + // the slice count is in region[1] for 1D array images + if self.mem_type == CL_MEM_OBJECT_IMAGE1D_ARRAY { + region[1] * *slice_pitch + } else { + region[2] * *slice_pitch + } + } else { + debug_assert_ne!(*row_pitch, 0); + region[1] * *row_pitch + }; let layout; unsafe {