diff --git a/src/gallium/frontends/rusticl/core.rs b/src/gallium/frontends/rusticl/core.rs index d4631f3b163..a39c4174dbb 100644 --- a/src/gallium/frontends/rusticl/core.rs +++ b/src/gallium/frontends/rusticl/core.rs @@ -2,6 +2,7 @@ pub mod context; pub mod device; pub mod event; pub mod format; +pub mod gl; pub mod kernel; pub mod memory; pub mod platform; diff --git a/src/gallium/frontends/rusticl/core/gl.rs b/src/gallium/frontends/rusticl/core/gl.rs new file mode 100644 index 00000000000..bf986470e16 --- /dev/null +++ b/src/gallium/frontends/rusticl/core/gl.rs @@ -0,0 +1,91 @@ +use crate::api::icd::*; + +use libc_rust_gen::dlsym; +use rusticl_opencl_gen::*; + +use std::ffi::CString; +use std::mem; +use std::ptr; + +pub struct XPlatManager { + glx_get_proc_addr: PFNGLXGETPROCADDRESSPROC, + egl_get_proc_addr: PFNEGLGETPROCADDRESSPROC, +} + +impl Default for XPlatManager { + fn default() -> Self { + Self::new() + } +} + +impl XPlatManager { + pub fn new() -> Self { + Self { + glx_get_proc_addr: Self::get_proc_address_func("glXGetProcAddress"), + egl_get_proc_addr: Self::get_proc_address_func("eglGetProcAddress"), + } + } + + fn get_proc_address_func(name: &str) -> T { + let cname = CString::new(name).unwrap(); + unsafe { + let pfn = dlsym(ptr::null_mut(), cname.as_ptr()); + mem::transmute_copy(&pfn) + } + } + + fn get_func(&self, name: &str) -> CLResult { + let cname = CString::new(name).unwrap(); + unsafe { + let raw_func = if name.starts_with("glX") { + self.glx_get_proc_addr + .ok_or(CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR)?( + cname.as_ptr().cast() + ) + } else if name.starts_with("egl") { + self.egl_get_proc_addr + .ok_or(CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR)?( + cname.as_ptr().cast() + ) + } else { + panic!(); + }; + + Ok(mem::transmute_copy(&raw_func)) + } + } + + #[allow(non_snake_case)] + pub fn MesaGLInteropEGLQueryDeviceInfo( + &self, + ) -> CLResult { + self.get_func::("eglGLInteropQueryDeviceInfoMESA") + } + + #[allow(non_snake_case)] + pub fn MesaGLInteropGLXQueryDeviceInfo( + &self, + ) -> CLResult { + self.get_func::("glXGLInteropQueryDeviceInfoMESA") + } + + #[allow(non_snake_case)] + pub fn MesaGLInteropEGLExportObject(&self) -> CLResult { + self.get_func::("eglGLInteropExportObjectMESA") + } + + #[allow(non_snake_case)] + pub fn MesaGLInteropGLXExportObject(&self) -> CLResult { + self.get_func::("glXGLInteropExportObjectMESA") + } + + #[allow(non_snake_case)] + pub fn MesaGLInteropEGLFlushObjects(&self) -> CLResult { + self.get_func::("eglGLInteropFlushObjectsMESA") + } + + #[allow(non_snake_case)] + pub fn MesaGLInteropGLXFlushObjects(&self) -> CLResult { + self.get_func::("glXGLInteropFlushObjectsMESA") + } +} diff --git a/src/gallium/frontends/rusticl/meson.build b/src/gallium/frontends/rusticl/meson.build index 4b179ec4433..bdcc0d15c44 100644 --- a/src/gallium/frontends/rusticl/meson.build +++ b/src/gallium/frontends/rusticl/meson.build @@ -73,6 +73,7 @@ rusticl_files = files( 'core/queue.rs', 'core/util.rs', 'core/version.rs', + 'core/gl.rs', ) rusticl_args = [