opengl: use all GL symbols from the vtable
[vlc.git] / modules / video_output / opengl / converter.h
blob42ba005160ce191fb8729efb376ae258e8cfe2c7
1 /*****************************************************************************
2 * converter.h: OpenGL internal header
3 *****************************************************************************
4 * Copyright (C) 2016 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifndef VLC_OPENGL_CONVERTER_H
22 #define VLC_OPENGL_CONVERTER_H
24 #ifdef HAVE_LIBPLACEBO
25 #include <libplacebo/shaders.h>
26 #endif
28 #include "vout_helper.h"
29 #include <vlc_plugin.h>
31 #define VLCGL_PICTURE_MAX 128
33 #ifndef GL_TEXTURE_RECTANGLE
34 # define GL_TEXTURE_RECTANGLE 0x84F5
35 #endif
37 #ifndef APIENTRY
38 # define APIENTRY
39 #endif
41 /* Core OpenGL/OpenGLES functions: the following functions pointers typedefs
42 * are not defined. */
43 #if !defined(_WIN32) /* Already defined on Win32 */
44 typedef void (*PFNGLACTIVETEXTUREPROC) (GLenum texture);
45 #endif
46 typedef void (APIENTRY *PFNGLBINDTEXTUREPROC) (GLenum target, GLuint texture);
47 typedef void (APIENTRY *PFNGLBLENDFUNCPROC) (GLenum sfactor, GLenum dfactor);
48 typedef void (APIENTRY *PFNGLBUFFERSTORAGEPROC) (GLenum target, GLsizeiptr size, const void *data, GLbitfield flags);
49 typedef void (APIENTRY *PFNGLCLEARCOLORPROC) (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
50 typedef void (APIENTRY *PFNGLCLEARPROC) (GLbitfield mask);
51 typedef void (APIENTRY *PFNGLDELETETEXTURESPROC) (GLsizei n, const GLuint *textures);
52 typedef void (APIENTRY *PFNGLDEPTHMASKPROC) (GLboolean flag);
53 typedef void (APIENTRY *PFNGLDISABLEPROC) (GLenum cap);
54 typedef void (APIENTRY *PFNGLDRAWARRAYSPROC) (GLenum mode, GLint first, GLsizei count);
55 typedef void (APIENTRY *PFNGLDRAWELEMENTSPROC) (GLenum mode, GLsizei count, GLenum type, const void *indices);
56 typedef void (APIENTRY *PFNGLENABLEPROC) (GLenum cap);
57 typedef void (APIENTRY *PFNGLFINISHPROC)(void);
58 typedef void (APIENTRY *PFNGLFLUSHPROC)(void);
59 typedef void (APIENTRY *PFNGLGENTEXTURESPROC) (GLsizei n, GLuint *textures);
60 typedef GLenum (APIENTRY *PFNGLGETERRORPROC) (void);
61 typedef void (APIENTRY *PFNGLGETINTEGERVPROC) (GLenum pname, GLint *data);
62 typedef const GLubyte *(APIENTRY *PFNGLGETSTRINGPROC) (GLenum name);
63 typedef void (APIENTRY *PFNGLGETTEXLEVELPARAMETERIVPROC) (GLenum target, GLint level, GLenum pname, GLint *params);
64 typedef void (APIENTRY *PFNGLPIXELSTOREIPROC) (GLenum pname, GLint param);
65 typedef void (APIENTRY *PFNGLTEXENVFPROC)(GLenum target, GLenum pname, GLfloat param);
66 typedef void (APIENTRY *PFNGLTEXIMAGE2DPROC) (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels);
67 typedef void (APIENTRY *PFNGLTEXPARAMETERFPROC) (GLenum target, GLenum pname, GLfloat param);
68 typedef void (APIENTRY *PFNGLTEXPARAMETERIPROC) (GLenum target, GLenum pname, GLint param);
69 typedef void (APIENTRY *PFNGLTEXSUBIMAGE2DPROC) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels);
70 typedef void (APIENTRY *PFNGLVIEWPORTPROC) (GLint x, GLint y, GLsizei width, GLsizei height);
72 /* The following are defined in glext.h but not for GLES2 or on Apple systems */
73 #if defined(USE_OPENGL_ES2) || defined(__APPLE__)
74 # define PFNGLGETPROGRAMIVPROC typeof(glGetProgramiv)*
75 # define PFNGLGETPROGRAMINFOLOGPROC typeof(glGetProgramInfoLog)*
76 # define PFNGLGETSHADERIVPROC typeof(glGetShaderiv)*
77 # define PFNGLGETSHADERINFOLOGPROC typeof(glGetShaderInfoLog)*
78 # define PFNGLGETUNIFORMLOCATIONPROC typeof(glGetUniformLocation)*
79 # define PFNGLGETATTRIBLOCATIONPROC typeof(glGetAttribLocation)*
80 # define PFNGLVERTEXATTRIBPOINTERPROC typeof(glVertexAttribPointer)*
81 # define PFNGLENABLEVERTEXATTRIBARRAYPROC typeof(glEnableVertexAttribArray)*
82 # define PFNGLUNIFORMMATRIX4FVPROC typeof(glUniformMatrix4fv)*
83 # define PFNGLUNIFORMMATRIX3FVPROC typeof(glUniformMatrix3fv)*
84 # define PFNGLUNIFORMMATRIX2FVPROC typeof(glUniformMatrix2fv)*
85 # define PFNGLUNIFORM4FVPROC typeof(glUniform4fv)*
86 # define PFNGLUNIFORM4FPROC typeof(glUniform4f)*
87 # define PFNGLUNIFORM3FPROC typeof(glUniform3f)*
88 # define PFNGLUNIFORM2FPROC typeof(glUniform2f)*
89 # define PFNGLUNIFORM1FPROC typeof(glUniform1f)*
90 # define PFNGLUNIFORM1IPROC typeof(glUniform1i)*
91 # define PFNGLCREATESHADERPROC typeof(glCreateShader)*
92 # define PFNGLSHADERSOURCEPROC typeof(glShaderSource)*
93 # define PFNGLCOMPILESHADERPROC typeof(glCompileShader)*
94 # define PFNGLDELETESHADERPROC typeof(glDeleteShader)*
95 # define PFNGLCREATEPROGRAMPROC typeof(glCreateProgram)*
96 # define PFNGLLINKPROGRAMPROC typeof(glLinkProgram)*
97 # define PFNGLUSEPROGRAMPROC typeof(glUseProgram)*
98 # define PFNGLDELETEPROGRAMPROC typeof(glDeleteProgram)*
99 # define PFNGLATTACHSHADERPROC typeof(glAttachShader)*
100 # define PFNGLGENBUFFERSPROC typeof(glGenBuffers)*
101 # define PFNGLBINDBUFFERPROC typeof(glBindBuffer)*
102 # define PFNGLBUFFERDATAPROC typeof(glBufferData)*
103 # define PFNGLBUFFERSUBDATAPROC typeof(glBufferSubData)*
104 # define PFNGLDELETEBUFFERSPROC typeof(glDeleteBuffers)*
105 #if defined(__APPLE__)
106 # import <CoreFoundation/CoreFoundation.h>
107 #endif
108 #endif
110 /* The following are defined in glext.h but doesn't exist in GLES2 */
111 #if defined(USE_OPENGL_ES2) || defined(__APPLE__)
112 typedef struct __GLsync *GLsync;
113 typedef uint64_t GLuint64;
114 typedef void *(APIENTRY *PFNGLMAPBUFFERRANGEPROC) (GLenum target, GLintptr offset, GLsizeiptr length, GLbitfield access);
115 typedef void (APIENTRY *PFNGLFLUSHMAPPEDBUFFERRANGEPROC) (GLenum target, GLintptr offset, GLsizeiptr length);
116 typedef GLboolean (APIENTRY *PFNGLUNMAPBUFFERPROC) (GLenum target);
117 typedef GLsync (APIENTRY *PFNGLFENCESYNCPROC) (GLenum condition, GLbitfield flags);
118 typedef void (APIENTRY *PFNGLDELETESYNCPROC) (GLsync sync);
119 typedef GLenum (APIENTRY *PFNGLCLIENTWAITSYNCPROC) (GLsync sync, GLbitfield flags, GLuint64 timeout);
120 #endif
123 * Structure containing function pointers to shaders commands
125 typedef struct {
127 * GL / GLES core functions
129 PFNGLBINDTEXTUREPROC BindTexture;
130 PFNGLBLENDFUNCPROC BlendFunc;
131 PFNGLCLEARCOLORPROC ClearColor;
132 PFNGLCLEARPROC Clear;
133 PFNGLDELETETEXTURESPROC DeleteTextures;
134 PFNGLDEPTHMASKPROC DepthMask;
135 PFNGLDISABLEPROC Disable;
136 PFNGLDRAWARRAYSPROC DrawArrays;
137 PFNGLDRAWELEMENTSPROC DrawElements;
138 PFNGLENABLEPROC Enable;
139 PFNGLFINISHPROC Finish;
140 PFNGLFLUSHPROC Flush;
141 PFNGLGENTEXTURESPROC GenTextures;
142 PFNGLGETERRORPROC GetError;
143 PFNGLGETINTEGERVPROC GetIntegerv;
144 PFNGLGETSTRINGPROC GetString;
145 PFNGLPIXELSTOREIPROC PixelStorei;
146 PFNGLTEXIMAGE2DPROC TexImage2D;
147 PFNGLTEXPARAMETERFPROC TexParameterf;
148 PFNGLTEXPARAMETERIPROC TexParameteri;
149 PFNGLTEXSUBIMAGE2DPROC TexSubImage2D;
150 PFNGLVIEWPORTPROC Viewport;
152 /* GL only core functions: NULL for GLES2 */
153 PFNGLGETTEXLEVELPARAMETERIVPROC GetTexLevelParameteriv; /* Can be NULL */
154 PFNGLTEXENVFPROC TexEnvf; /* Can be NULL */
157 * GL / GLES extensions
160 /* Shader commands */
161 PFNGLCREATESHADERPROC CreateShader;
162 PFNGLSHADERSOURCEPROC ShaderSource;
163 PFNGLCOMPILESHADERPROC CompileShader;
164 PFNGLATTACHSHADERPROC AttachShader;
165 PFNGLDELETESHADERPROC DeleteShader;
167 /* Shader log commands */
168 PFNGLGETPROGRAMIVPROC GetProgramiv;
169 PFNGLGETSHADERIVPROC GetShaderiv;
170 PFNGLGETPROGRAMINFOLOGPROC GetProgramInfoLog;
171 PFNGLGETSHADERINFOLOGPROC GetShaderInfoLog;
173 /* Shader variables commands */
174 PFNGLGETUNIFORMLOCATIONPROC GetUniformLocation;
175 PFNGLGETATTRIBLOCATIONPROC GetAttribLocation;
176 PFNGLVERTEXATTRIBPOINTERPROC VertexAttribPointer;
177 PFNGLENABLEVERTEXATTRIBARRAYPROC EnableVertexAttribArray;
178 PFNGLUNIFORMMATRIX4FVPROC UniformMatrix4fv;
179 PFNGLUNIFORMMATRIX3FVPROC UniformMatrix3fv;
180 PFNGLUNIFORMMATRIX2FVPROC UniformMatrix2fv;
181 PFNGLUNIFORM4FVPROC Uniform4fv;
182 PFNGLUNIFORM4FPROC Uniform4f;
183 PFNGLUNIFORM3FPROC Uniform3f;
184 PFNGLUNIFORM2FPROC Uniform2f;
185 PFNGLUNIFORM1FPROC Uniform1f;
186 PFNGLUNIFORM1IPROC Uniform1i;
188 /* Program commands */
189 PFNGLCREATEPROGRAMPROC CreateProgram;
190 PFNGLLINKPROGRAMPROC LinkProgram;
191 PFNGLUSEPROGRAMPROC UseProgram;
192 PFNGLDELETEPROGRAMPROC DeleteProgram;
194 /* Texture commands */
195 PFNGLACTIVETEXTUREPROC ActiveTexture;
197 /* Buffers commands */
198 PFNGLGENBUFFERSPROC GenBuffers;
199 PFNGLBINDBUFFERPROC BindBuffer;
200 PFNGLBUFFERDATAPROC BufferData;
201 PFNGLDELETEBUFFERSPROC DeleteBuffers;
203 /* Commands used for PBO and/or Persistent mapping */
204 PFNGLBUFFERSUBDATAPROC BufferSubData; /* can be NULL */
205 PFNGLBUFFERSTORAGEPROC BufferStorage; /* can be NULL */
206 PFNGLMAPBUFFERRANGEPROC MapBufferRange; /* can be NULL */
207 PFNGLFLUSHMAPPEDBUFFERRANGEPROC FlushMappedBufferRange; /* can be NULL */
208 PFNGLUNMAPBUFFERPROC UnmapBuffer; /* can be NULL */
209 PFNGLFENCESYNCPROC FenceSync; /* can be NULL */
210 PFNGLDELETESYNCPROC DeleteSync; /* can be NULL */
211 PFNGLCLIENTWAITSYNCPROC ClientWaitSync; /* can be NULL */
212 } opengl_vtable_t;
215 * Structure that is filled by "glhw converter" module probe function
216 * The implementation should initialize every members of the struct that are
217 * not set by the caller
219 typedef struct opengl_tex_converter_t opengl_tex_converter_t;
220 struct opengl_tex_converter_t
222 VLC_COMMON_MEMBERS
224 module_t *p_module;
226 /* Pointer to object gl, set by the caller */
227 vlc_gl_t *gl;
229 #ifdef HAVE_LIBPLACEBO
230 /* libplacebo context, created by the caller (optional) */
231 struct pl_context *pl_ctx;
232 #endif
234 /* Function pointers to OpenGL functions, set by the caller */
235 const opengl_vtable_t *vt;
237 /* True to dump shaders, set by the caller */
238 bool b_dump_shaders;
240 /* Function pointer to the shader init command, set by the caller, see
241 * opengl_fragment_shader_init() documentation. */
242 GLuint (*pf_fragment_shader_init)(opengl_tex_converter_t *, GLenum,
243 vlc_fourcc_t, video_color_space_t);
245 /* Available gl extensions (from GL_EXTENSIONS) */
246 const char *glexts;
248 /* True if the current API is OpenGL ES, set by the caller */
249 bool is_gles;
250 /* GLSL version, set by the caller. 100 for GLSL ES, 120 for desktop GLSL */
251 unsigned glsl_version;
252 /* Precision header, set by the caller. In OpenGLES, the fragment language
253 * has no default precision qualifier for floating point types. */
254 const char *glsl_precision_header;
256 /* Can only be changed from the module open function */
257 video_format_t fmt;
259 /* Fragment shader, must be set from the module open function. It will be
260 * deleted by the caller. */
261 GLuint fshader;
263 /* Number of textures, cannot be 0 */
264 unsigned tex_count;
266 /* Texture mapping (usually: GL_TEXTURE_2D), cannot be 0 */
267 GLenum tex_target;
269 /* Set to true if textures are generated from pf_update() */
270 bool handle_texs_gen;
272 struct opengl_tex_cfg {
273 /* Texture scale factor, cannot be 0 */
274 vlc_rational_t w;
275 vlc_rational_t h;
277 /* The following is used and filled by the opengl_fragment_shader_init
278 * function. */
279 GLint internal;
280 GLenum format;
281 GLenum type;
282 } texs[PICTURE_PLANE_MAX];
284 /* The following is used and filled by the opengl_fragment_shader_init
285 * function. */
286 struct {
287 GLint Texture[PICTURE_PLANE_MAX];
288 GLint TexSize[PICTURE_PLANE_MAX]; /* for GL_TEXTURE_RECTANGLE */
289 GLint Coefficients;
290 GLint FillColor;
291 GLint *pl_vars; /* for pl_sh_res */
292 } uloc;
293 bool yuv_color;
294 GLfloat yuv_coefficients[16];
296 #ifdef HAVE_LIBPLACEBO
297 struct pl_shader *pl_sh;
298 const struct pl_shader_res *pl_sh_res;
299 #endif
301 /* Private context */
302 void *priv;
305 * Callback to allocate data for bound textures
307 * This function pointer can be NULL. Software converters should call
308 * glTexImage2D() to allocate textures data (it will be deallocated by the
309 * caller when calling glDeleteTextures()). Won't be called if
310 * handle_texs_gen is true.
312 * \param fc OpenGL tex converter
313 * \param textures array of textures to bind (one per plane)
314 * \param tex_width array of tex width (one per plane)
315 * \param tex_height array of tex height (one per plane)
316 * \return VLC_SUCCESS or a VLC error
318 int (*pf_allocate_textures)(const opengl_tex_converter_t *tc, GLuint *textures,
319 const GLsizei *tex_width, const GLsizei *tex_height);
322 * Callback to allocate a picture pool
324 * This function pointer *can* be NULL. If NULL, A generic pool with
325 * pictures allocated from the video_format_t will be used.
327 * \param fc OpenGL tex converter
328 * \param requested_count number of pictures to allocate
329 * \return the picture pool or NULL in case of error
331 picture_pool_t *(*pf_get_pool)(const opengl_tex_converter_t *fc,
332 unsigned requested_count);
335 * Callback to update a picture
337 * This function pointer cannot be NULL. The implementation should upload
338 * every planes of the picture.
340 * \param fc OpenGL tex converter
341 * \param textures array of textures to bind (one per plane)
342 * \param tex_width array of tex width (one per plane)
343 * \param tex_height array of tex height (one per plane)
344 * \param pic picture to update
345 * \param plane_offset offsets of each picture planes to read data from
346 * (one per plane, can be NULL)
347 * \return VLC_SUCCESS or a VLC error
349 int (*pf_update)(const opengl_tex_converter_t *fc, GLuint *textures,
350 const GLsizei *tex_width, const GLsizei *tex_height,
351 picture_t *pic, const size_t *plane_offset);
354 * Callback to fetch locations of uniform or attributes variables
356 * This function pointer cannot be NULL. This callback is called one time
357 * after init.
359 * \param fc OpenGL tex converter
360 * \param program linked program that will be used by this tex converter
361 * \return VLC_SUCCESS or a VLC error
363 int (*pf_fetch_locations)(opengl_tex_converter_t *fc, GLuint program);
366 * Callback to prepare the fragment shader
368 * This function pointer cannot be NULL. This callback can be used to
369 * specify values of uniform variables.
371 * \param fc OpenGL tex converter
372 * \param tex_width array of tex width (one per plane)
373 * \param tex_height array of tex height (one per plane)
374 * \param alpha alpha value, used only for RGBA fragment shader
376 void (*pf_prepare_shader)(const opengl_tex_converter_t *fc,
377 const GLsizei *tex_width, const GLsizei *tex_height,
378 float alpha);
382 * Generate a fragment shader
384 * This utility function can be used by hw opengl tex converters that need a
385 * generic fragment shader. It will compile a fragment shader generated from
386 * the chroma and the tex target. This will initialize all elements of the
387 * opengl_tex_converter_t struct except for priv, pf_allocate_texture,
388 * pf_get_pool, pf_update
390 * \param tc OpenGL tex converter
391 * \param tex_target GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE
392 * \param chroma chroma used to generate the fragment shader
393 * \param if not COLOR_SPACE_UNDEF, YUV planes will be converted to RGB
394 * according to the color space
395 * \return the compiled fragment shader or 0 in case of error
397 static inline GLuint
398 opengl_fragment_shader_init(opengl_tex_converter_t *tc, GLenum tex_target,
399 vlc_fourcc_t chroma, video_color_space_t yuv_space)
401 return tc->pf_fragment_shader_init(tc, tex_target, chroma, yuv_space);
404 #endif /* include-guard */