rusticl/mem: add functions to create sampler and image views to Image

This allows us to use more contextual information of the image to create
the pipe_sampler_view properly instead of passing all the required
properties via function arguments.

Reviewed-by: @LingMan
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/32903>
This commit is contained in:
Karol Herbst
2025-01-06 06:57:13 +01:00
committed by Marge Bot
parent b759618d55
commit 26df5938cf
2 changed files with 50 additions and 44 deletions

View File

@@ -1398,54 +1398,14 @@ impl Kernel {
add_global(q, &mut input, &mut resource_info, res, buffer.offset);
}
KernelArgValue::Image(image) => {
let res = image.get_res_of_dev(q.device)?;
// If resource is a buffer, the image was created from a buffer. Use
// strides and dimensions of the image then.
let app_img_info = if res.as_ref().is_buffer()
&& image.mem_type == CL_MEM_OBJECT_IMAGE2D
{
Some(AppImgInfo::new(
image.image_desc.row_pitch()?
/ image.image_elem_size as u32,
image.image_desc.width()?,
image.image_desc.height()?,
))
} else {
None
};
let format = image.pipe_format;
let size =
image.size.try_into().map_err(|_| CL_OUT_OF_RESOURCES)?;
let (formats, orders) = if api_arg.kind == KernelArgType::Image {
iviews.push(res.pipe_image_view(
format,
false,
image.pipe_image_host_access(),
size,
app_img_info.as_ref(),
));
iviews.push(image.image_view(q.device, false)?);
(&mut img_formats, &mut img_orders)
} else if api_arg.kind == KernelArgType::RWImage {
iviews.push(res.pipe_image_view(
format,
true,
image.pipe_image_host_access(),
size,
app_img_info.as_ref(),
));
iviews.push(image.image_view(q.device, true)?);
(&mut img_formats, &mut img_orders)
} else {
let sview = PipeSamplerView::new(
ctx,
res,
format,
size,
app_img_info.as_ref(),
)
.ok_or(CL_OUT_OF_HOST_MEMORY)?;
sviews.push(sview);
sviews.push(image.sampler_view(ctx)?);
(&mut tex_formats, &mut tex_orders)
};

View File

@@ -1384,7 +1384,7 @@ impl Image {
)
}
pub fn pipe_image_host_access(&self) -> u16 {
fn pipe_image_host_access(&self) -> u16 {
// those flags are all mutually exclusive
(if bit_check(self.flags, CL_MEM_HOST_READ_ONLY) {
PIPE_IMAGE_ACCESS_READ
@@ -1585,6 +1585,52 @@ impl Image {
}
Ok(())
}
/// Creates metadata when an 2D image or sampler view is created over a buffer resource.
fn buffer_2d_info(&self) -> CLResult<AppImgInfo> {
Ok(AppImgInfo::new(
self.image_desc.row_pitch()? / self.image_elem_size as u32,
self.image_desc.width()?,
self.image_desc.height()?,
))
}
pub fn sampler_view<'c>(&self, ctx: &'c QueueContext) -> CLResult<PipeSamplerView<'c, '_>> {
let res = self.get_res_of_dev(ctx.dev)?;
let size = self.size.try_into().map_err(|_| CL_OUT_OF_RESOURCES)?;
// If resource is a buffer, the image was created from a buffer. Use
// strides and dimensions of the image then.
let app_img_info = if res.is_buffer() && self.mem_type == CL_MEM_OBJECT_IMAGE2D {
Some(self.buffer_2d_info()?)
} else {
None
};
PipeSamplerView::new(ctx, res, self.pipe_format, size, app_img_info.as_ref())
.ok_or(CL_OUT_OF_HOST_MEMORY)
}
pub fn image_view(&self, dev: &Device, read_write: bool) -> CLResult<PipeImageView> {
let res = self.get_res_of_dev(dev)?;
let size = self.size.try_into().map_err(|_| CL_OUT_OF_RESOURCES)?;
// If resource is a buffer, the image was created from a buffer. Use
// strides and dimensions of the image then.
let app_img_info = if res.is_buffer() && self.mem_type == CL_MEM_OBJECT_IMAGE2D {
Some(self.buffer_2d_info()?)
} else {
None
};
Ok(res.pipe_image_view(
self.pipe_format,
read_write,
self.pipe_image_host_access(),
size,
app_img_info.as_ref(),
))
}
}
pub struct Sampler {