Module: Mesa
Branch: main
Commit: c4c58aa26f39ce19d7d84a02b5f4568f57e87bd0
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=c4c58aa26f39ce19d7d84a02b5f4568f57e87bd0

Author: Antonio Gomes <antoniospg...@gmail.com>
Date:   Fri Apr 21 22:07:53 2023 -0300

rusticl: Add xplat helpers to dynamic link interop functions

Reviewed-by: Karol Herbst <kher...@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21305>

---

 src/gallium/frontends/rusticl/core.rs     |  1 +
 src/gallium/frontends/rusticl/core/gl.rs  | 91 +++++++++++++++++++++++++++++++
 src/gallium/frontends/rusticl/meson.build |  1 +
 3 files changed, 93 insertions(+)

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<T>(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<T>(&self, name: &str) -> CLResult<T> {
+        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<PFNMESAGLINTEROPEGLQUERYDEVICEINFOPROC> {
+        
self.get_func::<PFNMESAGLINTEROPEGLQUERYDEVICEINFOPROC>("eglGLInteropQueryDeviceInfoMESA")
+    }
+
+    #[allow(non_snake_case)]
+    pub fn MesaGLInteropGLXQueryDeviceInfo(
+        &self,
+    ) -> CLResult<PFNMESAGLINTEROPGLXQUERYDEVICEINFOPROC> {
+        
self.get_func::<PFNMESAGLINTEROPGLXQUERYDEVICEINFOPROC>("glXGLInteropQueryDeviceInfoMESA")
+    }
+
+    #[allow(non_snake_case)]
+    pub fn MesaGLInteropEGLExportObject(&self) -> 
CLResult<PFNMESAGLINTEROPEGLEXPORTOBJECTPROC> {
+        
self.get_func::<PFNMESAGLINTEROPEGLEXPORTOBJECTPROC>("eglGLInteropExportObjectMESA")
+    }
+
+    #[allow(non_snake_case)]
+    pub fn MesaGLInteropGLXExportObject(&self) -> 
CLResult<PFNMESAGLINTEROPGLXEXPORTOBJECTPROC> {
+        
self.get_func::<PFNMESAGLINTEROPGLXEXPORTOBJECTPROC>("glXGLInteropExportObjectMESA")
+    }
+
+    #[allow(non_snake_case)]
+    pub fn MesaGLInteropEGLFlushObjects(&self) -> 
CLResult<PFNMESAGLINTEROPEGLFLUSHOBJECTSPROC> {
+        
self.get_func::<PFNMESAGLINTEROPEGLFLUSHOBJECTSPROC>("eglGLInteropFlushObjectsMESA")
+    }
+
+    #[allow(non_snake_case)]
+    pub fn MesaGLInteropGLXFlushObjects(&self) -> 
CLResult<PFNMESAGLINTEROPGLXFLUSHOBJECTSPROC> {
+        
self.get_func::<PFNMESAGLINTEROPGLXFLUSHOBJECTSPROC>("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 = [

Reply via email to