cl3/
dx9_media_sharing.rs

1// Copyright (c) 2021-2025 Via Technology Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! FFI bindings for `cl_dx9_media_sharing.h`
16//!
17//! `cl_ecl_dx9_media_sharing.h` contains `OpenCL` extensions that provide interoperability with `Direct3D` 9.
18//! `OpenCL` extensions are documented in the [OpenCL-Registry](https://github.com/KhronosGroup/OpenCL-Registry)
19
20#![allow(non_camel_case_types)]
21#![allow(clippy::missing_safety_doc)]
22
23pub use opencl_sys::cl_dx9_media_sharing::*;
24pub use opencl_sys::{
25    CL_INVALID_VALUE, CL_SUCCESS, cl_context, cl_int, cl_mem_flags, cl_mem_object_type, cl_uint,
26};
27
28#[allow(unused_imports)]
29use libc::c_void;
30#[allow(unused_imports)]
31use std::ptr;
32
33#[cfg(any(feature = "cl_intel_dx9_media_sharing", feature = "dynamic"))]
34pub unsafe fn get_device_ids_from_dx9_intel(
35    platform: cl_platform_id,
36    dx9_device_source: cl_dx9_device_source_intel,
37    dx9_object: *mut c_void,
38    dx9_device_set: cl_dx9_device_set_intel,
39) -> Result<Vec<cl_device_id>, cl_int> {
40    let mut count: cl_uint = 0;
41    let status: cl_int = cl_call!(cl_dx9_media_sharing::clGetDeviceIDsFromDX9INTEL(
42        platform,
43        dx9_device_source,
44        dx9_object,
45        dx9_device_set,
46        0,
47        ptr::null_mut(),
48        &raw mut count,
49    ));
50    if CL_SUCCESS != status {
51        Err(status)
52    } else if 0 < count {
53        // Get the device ids.
54        let len = count as usize;
55        let mut ids: Vec<cl_device_id> = Vec::with_capacity(len);
56        let status: cl_int = cl_call!(cl_dx9_media_sharing::clGetDeviceIDsFromDX9INTEL(
57            platform,
58            dx9_device_source,
59            dx9_object,
60            dx9_device_set,
61            count,
62            ids.as_mut_ptr(),
63            ptr::null_mut(),
64        ));
65        if CL_SUCCESS == status {
66            Ok(ids)
67        } else {
68            Err(status)
69        }
70    } else {
71        Ok(Vec::default())
72    }
73}
74
75#[cfg(any(feature = "cl_intel_dx9_media_sharing", feature = "dynamic"))]
76pub unsafe fn create_from_dx9_media_surface_intel(
77    context: cl_context,
78    flags: cl_mem_flags,
79    resource: IDirect3DSurface9_ptr,
80    shared_handle: HANDLE,
81    plane: cl_uint,
82) -> Result<cl_mem, cl_int> {
83    let mut status: cl_int = CL_INVALID_VALUE;
84    let mem = cl_call!(cl_dx9_media_sharing::clCreateFromDX9MediaSurfaceINTEL(
85        context,
86        flags,
87        resource,
88        shared_handle,
89        plane,
90        &raw mut status,
91    ));
92    if CL_SUCCESS == status {
93        Ok(mem)
94    } else {
95        Err(status)
96    }
97}
98
99#[cfg(any(feature = "cl_intel_dx9_media_sharing", feature = "dynamic"))]
100pub unsafe fn enqueue_acquire_dx9_objects_intel(
101    command_queue: cl_command_queue,
102    num_objects: cl_uint,
103    mem_objects: *const cl_mem,
104    num_events_in_wait_list: cl_uint,
105    event_wait_list: *const cl_event,
106) -> Result<cl_event, cl_int> {
107    let mut event: cl_event = ptr::null_mut();
108    let status: cl_int = cl_call!(cl_dx9_media_sharing::clEnqueueAcquireDX9ObjectsINTEL(
109        command_queue,
110        num_objects,
111        mem_objects,
112        num_events_in_wait_list,
113        event_wait_list,
114        &raw mut event,
115    ));
116    if CL_SUCCESS == status {
117        Ok(event)
118    } else {
119        Err(status)
120    }
121}
122
123#[cfg(any(feature = "cl_intel_dx9_media_sharing", feature = "dynamic"))]
124pub unsafe fn enqueue_release_dx9_objects_intel(
125    command_queue: cl_command_queue,
126    num_objects: cl_uint,
127    mem_objects: *const cl_mem,
128    num_events_in_wait_list: cl_uint,
129    event_wait_list: *const cl_event,
130) -> Result<cl_event, cl_int> {
131    let mut event: cl_event = ptr::null_mut();
132    let status: cl_int = cl_call!(cl_dx9_media_sharing::clEnqueueReleaseDX9ObjectsINTEL(
133        command_queue,
134        num_objects,
135        mem_objects,
136        num_events_in_wait_list,
137        event_wait_list,
138        &raw mut event,
139    ));
140    if CL_SUCCESS == status {
141        Ok(event)
142    } else {
143        Err(status)
144    }
145}
146
147#[cfg(any(
148    feature = "cl_khr_dx9_media_sharing",
149    feature = "cl_intel_dx9_media_sharing",
150    feature = "dynamic"
151))]
152pub unsafe fn get_supported_dx9_media_surface_formats_intel(
153    context: cl_context,
154    flags: cl_mem_flags,
155    image_type: cl_mem_object_type,
156    plane: cl_uint,
157) -> Result<Vec<cl_uint>, cl_int> {
158    let mut count: cl_uint = 0;
159    let status: cl_int = cl_call!(
160        cl_dx9_media_sharing::clGetSupportedDX9MediaSurfaceFormatsINTEL(
161            context,
162            flags,
163            image_type,
164            plane,
165            0,
166            ptr::null_mut(),
167            &raw mut count,
168        )
169    );
170    if CL_SUCCESS != status {
171        Err(status)
172    } else if 0 < count {
173        // Get the dx9 formats.
174        let len = count as usize;
175        let mut ids: Vec<cl_uint> = Vec::with_capacity(len);
176        let status: cl_int = cl_call!(
177            cl_dx9_media_sharing::clGetSupportedDX9MediaSurfaceFormatsINTEL(
178                context,
179                flags,
180                image_type,
181                plane,
182                count,
183                ids.as_mut_ptr(),
184                ptr::null_mut(),
185            )
186        );
187        if CL_SUCCESS == status {
188            Ok(ids)
189        } else {
190            Err(status)
191        }
192    } else {
193        Ok(Vec::default())
194    }
195}