b76bd7ec2f509f2863b8f642a79f937f6571772b
[mesa.git] / include / GL / mesa_glinterop.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright 2016 Advanced Micro Devices, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /* Mesa OpenGL inter-driver interoperability interface designed for but not
26 * limited to OpenCL.
27 *
28 * This is a driver-agnostic, backward-compatible interface. The structures
29 * are only allowed to grow. They can never shrink and their members can
30 * never be removed, renamed, or redefined.
31 *
32 * The interface doesn't return a lot of static texture parameters like
33 * width, height, etc. It mainly returns mutable buffer and texture view
34 * parameters that can't be part of the texture allocation (because they are
35 * mutable). If drivers want to return more data or want to return static
36 * allocation parameters, they can do it in one of these two ways:
37 * - attaching the data to the DMABUF handle in a driver-specific way
38 * - passing the data via "out_driver_data" in the "in" structure.
39 *
40 * Mesa is expected to do a lot of error checking on behalf of OpenCL, such
41 * as checking the target, miplevel, and texture completeness.
42 *
43 * OpenCL, on the other hand, needs to check if the display+context combo
44 * is compatible with the OpenCL driver by querying the device information.
45 * It also needs to check if the texture internal format and channel ordering
46 * (returned in a driver-specific way) is supported by OpenCL, among other
47 * things.
48 */
49
50 #ifndef MESA_GLINTEROP_H
51 #define MESA_GLINTEROP_H
52
53 #include <stddef.h>
54 #include <EGL/egl.h>
55
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59
60 /* Forward declarations to avoid inclusion of GL/glx.h */
61 typedef struct _XDisplay Display;
62 typedef struct __GLXcontextRec *GLXContext;
63
64 /** Returned error codes. */
65 enum {
66 MESA_GLINTEROP_SUCCESS = 0,
67 MESA_GLINTEROP_OUT_OF_RESOURCES,
68 MESA_GLINTEROP_OUT_OF_HOST_MEMORY,
69 MESA_GLINTEROP_INVALID_OPERATION,
70 MESA_GLINTEROP_INVALID_VALUE,
71 MESA_GLINTEROP_INVALID_DISPLAY,
72 MESA_GLINTEROP_INVALID_CONTEXT,
73 MESA_GLINTEROP_INVALID_TARGET,
74 MESA_GLINTEROP_INVALID_OBJECT,
75 MESA_GLINTEROP_INVALID_MIP_LEVEL,
76 MESA_GLINTEROP_UNSUPPORTED
77 };
78
79 /** Access flags. */
80 enum {
81 MESA_GLINTEROP_ACCESS_READ_WRITE = 0,
82 MESA_GLINTEROP_ACCESS_READ_ONLY,
83 MESA_GLINTEROP_ACCESS_WRITE_ONLY
84 };
85
86 #define MESA_GLINTEROP_DEVICE_INFO_VERSION 1
87
88 /**
89 * Device information returned by Mesa.
90 */
91 typedef struct _mesa_glinterop_device_info {
92 /* The caller should set this to: MESA_GLINTEROP_DEVICE_INFO_VERSION */
93 uint32_t struct_version;
94
95 /* PCI location */
96 uint32_t pci_segment_group;
97 uint32_t pci_bus;
98 uint32_t pci_device;
99 uint32_t pci_function;
100
101 /* Device identification */
102 uint32_t vendor_id;
103 uint32_t device_id;
104
105 /* The interop version determines what behavior the caller should expect
106 * out of all functions.
107 *
108 * Interop version 1:
109 * - mesa_glinterop_export_in is not read beyond "out_driver_data"
110 * - mesa_glinterop_export_out is not written beyond "out_driver_data_written"
111 * - mesa_glinterop_device_info is not written beyond "interop_version"
112 */
113 uint32_t interop_version;
114 /* Structure version 1 ends here. */
115 } mesa_glinterop_device_info;
116
117 #define MESA_GLINTEROP_EXPORT_IN_VERSION 1
118
119 /**
120 * Input parameters to Mesa interop export functions.
121 */
122 typedef struct _mesa_glinterop_export_in {
123 /* The caller should set this to: MESA_GLINTEROP_EXPORT_IN_VERSION */
124 uint32_t struct_version;
125
126 /* One of the following:
127 * - GL_TEXTURE_BUFFER
128 * - GL_TEXTURE_1D
129 * - GL_TEXTURE_2D
130 * - GL_TEXTURE_3D
131 * - GL_TEXTURE_RECTANGLE
132 * - GL_TEXTURE_1D_ARRAY
133 * - GL_TEXTURE_2D_ARRAY
134 * - GL_TEXTURE_CUBE_MAP_ARRAY
135 * - GL_TEXTURE_CUBE_MAP
136 * - GL_TEXTURE_CUBE_MAP_POSITIVE_X
137 * - GL_TEXTURE_CUBE_MAP_NEGATIVE_X
138 * - GL_TEXTURE_CUBE_MAP_POSITIVE_Y
139 * - GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
140 * - GL_TEXTURE_CUBE_MAP_POSITIVE_Z
141 * - GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
142 * - GL_TEXTURE_2D_MULTISAMPLE
143 * - GL_TEXTURE_2D_MULTISAMPLE_ARRAY
144 * - GL_TEXTURE_EXTERNAL_OES
145 * - GL_RENDERBUFFER
146 * - GL_ARRAY_BUFFER
147 */
148 unsigned target;
149
150 /* If target is GL_ARRAY_BUFFER, it's a buffer object.
151 * If target is GL_RENDERBUFFER, it's a renderbuffer object.
152 * If target is GL_TEXTURE_*, it's a texture object.
153 */
154 unsigned obj;
155
156 /* Mipmap level. Ignored for non-texture objects. */
157 unsigned miplevel;
158
159 /* One of MESA_GLINTEROP_ACCESS_* flags. This describes how the exported
160 * object is going to be used.
161 */
162 uint32_t access;
163
164 /* Size of memory pointed to by out_driver_data. */
165 uint32_t out_driver_data_size;
166
167 /* If the caller wants to query driver-specific data about the OpenGL
168 * object, this should point to the memory where that data will be stored.
169 * This is expected to be a temporary staging memory. The pointer is not
170 * allowed to be saved for later use by Mesa.
171 */
172 void *out_driver_data;
173 /* Structure version 1 ends here. */
174 } mesa_glinterop_export_in;
175
176 #define MESA_GLINTEROP_EXPORT_OUT_VERSION 1
177
178 /**
179 * Outputs of Mesa interop export functions.
180 */
181 typedef struct _mesa_glinterop_export_out {
182 /* The caller should set this to: MESA_GLINTEROP_EXPORT_OUT_VERSION */
183 uint32_t struct_version;
184
185 /* The DMABUF handle. It must be closed by the caller using the POSIX
186 * close() function when it's not needed anymore. Mesa is not responsible
187 * for closing the handle.
188 *
189 * Not closing the handle by the caller will lead to a resource leak,
190 * will prevent releasing the GPU buffer, and may prevent creating new
191 * DMABUF handles within the process.
192 */
193 int dmabuf_fd;
194
195 /* The mutable OpenGL internal format specified by glTextureView or
196 * glTexBuffer. If the object is not one of those, the original internal
197 * format specified by glTexStorage, glTexImage, or glRenderbufferStorage
198 * will be returned.
199 */
200 unsigned internal_format;
201
202 /* Buffer offset and size for GL_ARRAY_BUFFER and GL_TEXTURE_BUFFER.
203 * This allows interop with suballocations (a buffer allocated within
204 * a larger buffer).
205 *
206 * Parameters specified by glTexBufferRange for GL_TEXTURE_BUFFER are
207 * applied to these and can shrink the range further.
208 */
209 ptrdiff_t buf_offset;
210 ptrdiff_t buf_size;
211
212 /* Parameters specified by glTextureView. If the object is not a texture
213 * view, default parameters covering the whole texture will be returned.
214 */
215 unsigned view_minlevel;
216 unsigned view_numlevels;
217 unsigned view_minlayer;
218 unsigned view_numlayers;
219
220 /* The number of bytes written to out_driver_data. */
221 uint32_t out_driver_data_written;
222 /* Structure version 1 ends here. */
223 } mesa_glinterop_export_out;
224
225
226 /**
227 * Query device information.
228 *
229 * \param dpy GLX display
230 * \param context GLX context
231 * \param out where to return the information
232 *
233 * \return MESA_GLINTEROP_SUCCESS or MESA_GLINTEROP_* != 0 on error
234 */
235 int
236 MesaGLInteropGLXQueryDeviceInfo(Display *dpy, GLXContext context,
237 mesa_glinterop_device_info *out);
238
239
240 /**
241 * Same as MesaGLInteropGLXQueryDeviceInfo except that it accepts EGLDisplay
242 * and EGLContext.
243 */
244 int
245 MesaGLInteropEGLQueryDeviceInfo(EGLDisplay dpy, EGLContext context,
246 mesa_glinterop_device_info *out);
247
248
249 /**
250 * Create and return a DMABUF handle corresponding to the given OpenGL
251 * object, and return other parameters about the OpenGL object.
252 *
253 * \param dpy GLX display
254 * \param context GLX context
255 * \param in input parameters
256 * \param out return values
257 *
258 * \return MESA_GLINTEROP_SUCCESS or MESA_GLINTEROP_* != 0 on error
259 */
260 int
261 MesaGLInteropGLXExportObject(Display *dpy, GLXContext context,
262 const mesa_glinterop_export_in *in,
263 mesa_glinterop_export_out *out);
264
265
266 /**
267 * Same as MesaGLInteropGLXExportObject except that it accepts
268 * EGLDisplay and EGLContext.
269 */
270 int
271 MesaGLInteropEGLExportObject(EGLDisplay dpy, EGLContext context,
272 const mesa_glinterop_export_in *in,
273 mesa_glinterop_export_out *out);
274
275
276 typedef int (PFNMESAGLINTEROPGLXQUERYDEVICEINFOPROC)(Display *dpy, GLXContext context,
277 mesa_glinterop_device_info *out);
278 typedef int (PFNMESAGLINTEROPEGLQUERYDEVICEINFOPROC)(EGLDisplay dpy, EGLContext context,
279 mesa_glinterop_device_info *out);
280 typedef int (PFNMESAGLINTEROPGLXEXPORTOBJECTPROC)(Display *dpy, GLXContext context,
281 const mesa_glinterop_export_in *in,
282 mesa_glinterop_export_out *out);
283 typedef int (PFNMESAGLINTEROPEGLEXPORTOBJECTPROC)(EGLDisplay dpy, EGLContext context,
284 const mesa_glinterop_export_in *in,
285 mesa_glinterop_export_out *out);
286
287 #ifdef __cplusplus
288 }
289 #endif
290
291 #endif /* MESA_GLINTEROP_H */