ocl_interop/
lib.rs

1#[cfg(target_os = "macos")]
2extern crate cgl;
3extern crate ocl;
4
5#[cfg(target_os = "linux")]
6#[allow(improper_ctypes)]
7mod glx {
8    include!(concat!(env!("OUT_DIR"), "/glx_bindings.rs"));
9}
10
11#[cfg(target_os = "windows")]
12#[allow(improper_ctypes)]
13mod wgl {
14    include!(concat!(env!("OUT_DIR"), "/wgl_bindings.rs"));
15}
16
17#[cfg(target_os = "android")]
18#[allow(improper_ctypes)]
19mod egl {
20    include!(concat!(env!("OUT_DIR"), "/egl_bindings.rs"));
21}
22
23pub fn get_properties_list() -> ocl::builders::ContextProperties {
24    let mut properties = ocl::builders::ContextProperties::new();
25
26    #[cfg(target_os = "linux")]
27    unsafe {
28        properties.set_gl_context(glx::GetCurrentContext() as *mut _);
29        properties.set_glx_display(glx::GetCurrentDisplay() as *mut _);
30    }
31
32    #[cfg(target_os = "windows")]
33    unsafe {
34        properties.set_gl_context(wgl::GetCurrentContext() as *mut _);
35        properties.set_wgl_hdc(wgl::GetCurrentDC() as *mut _);
36    }
37
38    #[cfg(target_os = "macos")]
39    unsafe {
40        let gl_context = cgl::CGLGetCurrentContext();
41        let share_group = cgl::CGLGetShareGroup(gl_context);
42        properties = properties.cgl_sharegroup(share_group);
43    }
44
45    #[cfg(target_os = "android")]
46    unsafe {
47        properties.set_gl_display(egl::GetCurrentContext() as *mut _);
48        properties.set_egl_display(egl::GetCurrentDisplay() as *mut _);
49    }
50
51    return properties;
52}
53
54pub fn get_context() -> std::option::Option<ocl::Context> {
55    ocl::Platform::list()
56        .iter()
57        .map(|plat| {
58            //println!("Plat: {}",plat);
59            ocl::Device::list(plat, Some(ocl::flags::DeviceType::new().gpu()))
60                .unwrap()
61                .iter()
62                .map(|dev| {
63                    let ctx = ocl::Context::builder()
64                        .properties(get_properties_list().platform(plat))
65                        .platform(*plat)
66                        .devices(dev)
67                        .build();
68                    //println!("- Dev: {:?} Ctx: {:?}",dev,ctx);
69                    ctx
70                })
71                .find(|t| t.is_ok())
72        })
73        .find(|t| t.is_some())
74        .map(|ctx| ctx.unwrap().unwrap())
75}
76
77#[cfg(test)]
78mod tests {
79    use get_properties_list;
80    #[test]
81    fn it_doesnt_crash() {
82        get_properties_list();
83    }
84}