rusticl/mesa: add PipeSamplerView wrapper

Reviewed-by: @LingMan
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/32903>
This commit is contained in:
Karol Herbst
2025-01-06 01:53:09 +01:00
committed by Marge Bot
parent b94d4f90f2
commit 80b81ed58e
2 changed files with 62 additions and 0 deletions

View File

@@ -51,6 +51,10 @@ impl PipeContext {
Some(s)
}
pub(crate) fn pipe(&self) -> NonNull<pipe_context> {
self.pipe
}
pub fn buffer_subdata(
&self,
res: &PipeResource,

View File

@@ -1,10 +1,13 @@
use mesa_rust_gen::*;
use std::{
marker::PhantomData,
mem,
ptr::{self, NonNull},
};
use super::context::PipeContext;
#[derive(PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct PipeResource {
@@ -214,3 +217,58 @@ impl Drop for PipeResource {
unsafe { pipe_resource_reference(&mut self.pipe.as_ptr(), ptr::null_mut()) }
}
}
/// Wrapper around gallium's pipe_sampler_view.
///
/// It deals with the refcounting and frees the object automatically if not needed anymore.
#[repr(transparent)]
pub struct PipeSamplerView<'c, 'r> {
view: NonNull<pipe_sampler_view>,
// the pipe_sampler_view object references both a context and a resource.
_ctx: PhantomData<&'c PipeContext>,
_res: PhantomData<&'r PipeResource>,
}
impl<'c, 'r> PipeSamplerView<'c, 'r> {
pub fn new(
ctx: &'c PipeContext,
res: &'r PipeResource,
format: pipe_format,
size: u32,
app_img_info: Option<&AppImgInfo>,
) -> Option<Self> {
let template = res.pipe_sampler_view_template(format, size, app_img_info);
let view = unsafe {
ctx.pipe().as_ref().create_sampler_view.unwrap()(
ctx.pipe().as_ptr(),
res.pipe(),
&template,
)
};
let view = NonNull::new(view)?;
unsafe {
debug_assert_eq!(view.as_ref().context, ctx.pipe().as_ptr());
debug_assert_eq!(view.as_ref().texture, res.pipe());
}
Some(Self {
view: view,
_ctx: PhantomData,
_res: PhantomData,
})
}
pub(crate) fn as_pipe(views: &mut [Self]) -> *mut *mut pipe_sampler_view {
// We are transparent over *mut pipe_sample_view, so this is sound.
views.as_mut_ptr().cast()
}
}
impl Drop for PipeSamplerView<'_, '_> {
fn drop(&mut self) {
unsafe {
pipe_sampler_view_reference(&mut ptr::null_mut(), self.view.as_ptr());
}
}
}