UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
gl_context.c
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2012-2023 CESNET, z. s. p. o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#ifdef HAVE_CONFIG_H
39#include "config.h"
40#include "config_unix.h"
41#include "config_win32.h"
42#endif
43
44#include <stdio.h>
45
46#ifdef __APPLE__
47#include "mac_gl_common.h"
48#elif defined __linux__
49#include "x11_common.h"
50#include "glx_common.h"
51#else // _WIN32
52#include "win32_gl_common.h"
53#endif
54
55#include "debug.h"
56#include "gl_context.h"
57#include "utils/macros.h"
58
69bool init_gl_context(struct gl_context *context, int which) {
70 context->context = NULL;
71#ifdef __linux__
72 if(which == GL_CONTEXT_ANY) {
73 debug_msg("Trying OpenGL 3.1 first.\n");
74 context->context = glx_init(MK_OPENGL_VERSION(3,1));
75 context->legacy = FALSE;
76 }
77 if(!context->context) {
78 if(which != GL_CONTEXT_LEGACY) {
79 debug_msg("OpenGL 3.1 profile failed to initialize, falling back to legacy profile.\n");
80 }
82 context->legacy = TRUE;
83 }
84 if(context->context) {
85 glx_validate(context->context);
86 }
87#elif defined __APPLE__
88 if(which == GL_CONTEXT_ANY) {
90 debug_msg("Mac 10.7 or latter detected. Trying OpenGL 3.2 Core profile first.\n");
92 if(!context->context) {
93 debug_msg("OpenGL 3.2 Core profile failed to initialize, falling back to legacy profile.\n");
94 } else {
95 context->legacy = FALSE;
96 }
97 }
98 }
99
100 if(!context->context) {
102 context->legacy = TRUE;
103 }
104#else // _WIN32
105 if(which == GL_CONTEXT_ANY) {
107 } else if(which == GL_CONTEXT_LEGACY) {
109 }
110
111 context->legacy = TRUE;
112#endif
113
114 {
115 char *save_ptr;
116 const char *gl_ver = (const char *) glGetString(GL_VERSION);
117 if (!gl_ver) {
118 fprintf(stderr, "Unable to determine OpenGL version!\n");
119 return false;
120 }
121
122 char *tmp = strdup(gl_ver);
123 char *item = strtok_r(tmp, ".", &save_ptr);
124 if (!item) {
125 fprintf(stderr, "Unable to determine OpenGL version!\n");
126 free(tmp);
127 return false;
128 }
129 context->gl_major = atoi(item);
130 item = strtok_r(NULL, ".", &save_ptr);
131 if (!item) {
132 fprintf(stderr, "Unable to determine OpenGL version!\n");
133 free(tmp);
134 return false;
135 }
136 context->gl_minor = atoi(item);
137 free(tmp);
138 }
139
140 if(context->context) {
141 return true;
142 } else {
143 return false;
144 }
145}
146
147GLuint glsl_compile_link(const char *vprogram, const char *fprogram)
148{
149 char log[32768];
150 GLuint vhandle = 0;
151 GLuint fhandle = 0;
152 GLint status;
153
154 log_msg(LOG_LEVEL_DEBUG, "Compiling program:\nVertex:%s\nShader:\n%s\n", IF_NOT_NULL_ELSE(vprogram, "(none)"), IF_NOT_NULL_ELSE(fprogram, "(none)"));
155
156 /* compile */
157 /* fragmemt */
158 if (fprogram) {
159 fhandle = glCreateShader(GL_FRAGMENT_SHADER);
160 glShaderSource(fhandle, 1, &fprogram, NULL);
161 glCompileShader(fhandle);
162 glGetShaderiv(fhandle, GL_COMPILE_STATUS, &status);
163 /* Print compile log */
164 glGetShaderInfoLog(fhandle, sizeof log, NULL, log);
165 if (status == GL_FALSE || (strlen(log) > 0 && log_level >= LOG_LEVEL_VERBOSE)) {
166 log_msg(status == GL_FALSE ? LOG_LEVEL_ERROR : LOG_LEVEL_VERBOSE, "Fragment compile log: %s\n", log);
167 }
168 if (status != GL_TRUE) {
169 glDeleteShader(fhandle);
170 return 0;
171 }
172 }
173 /* vertex */
174 if (vprogram) {
175 vhandle = glCreateShader(GL_VERTEX_SHADER);
176 glShaderSource(vhandle, 1, &vprogram, NULL);
177 glCompileShader(vhandle);
178 glGetShaderiv(vhandle, GL_COMPILE_STATUS, &status);
179 /* Print compile log */
180 glGetShaderInfoLog(vhandle, sizeof log, NULL, log);
181 if (status == GL_FALSE || (strlen(log) > 0 && log_level >= LOG_LEVEL_VERBOSE)) {
182 log_msg(status == GL_FALSE ? LOG_LEVEL_ERROR : LOG_LEVEL_VERBOSE, "Vertex compile log: %s\n", log);
183 }
184 if (status != GL_TRUE) {
185 glDeleteShader(fhandle);
186 glDeleteShader(vhandle);
187 return 0;
188 }
189 }
190
191 GLuint phandle = glCreateProgram();
192
193 /* attach and link */
194 if (vprogram) {
195 glAttachShader(phandle, vhandle);
196 }
197 if (fprogram) {
198 glAttachShader(phandle, fhandle);
199 }
200 glLinkProgram(phandle);
201
202 // mark shaders for deletion when program is deleted
203 glDeleteShader(vhandle);
204 glDeleteShader(fhandle);
205
206 glGetProgramiv(phandle, GL_LINK_STATUS, &status);
207 glGetProgramInfoLog(phandle, sizeof log, NULL, (GLchar*) log);
208 if (status == GL_FALSE || (strlen(log) > 0 && log_level >= LOG_LEVEL_VERBOSE)) {
209 log_msg(status == GL_FALSE ? LOG_LEVEL_ERROR : LOG_LEVEL_VERBOSE, "Link log: %s\n", log);
210 }
211 if (status != GL_TRUE) {
212 glDeleteProgram(phandle);
213 return 0;
214 }
215
216 // check GL errors
218
219 return phandle;
220}
221
222
223void destroy_gl_context(struct gl_context *context) {
224 if (context == NULL || context->context == NULL) {
225 return;
226 }
227#ifdef __APPLE__
228 mac_gl_free(context->context);
229#elif defined __linux__
230 glx_free(context->context);
231#else
232 win32_context_free(context->context);
233#endif
234}
235
237{
238 void *context_state = NULL;
239 if(context) {
240 context_state = context->context;
241 }
242#ifdef __linux__
243 glx_make_current(context_state);
244#elif defined __APPLE__
245 mac_gl_make_current(context_state);
246#else
247 win32_context_make_current(context_state);
248#endif
249}
250
#define TRUE
#define FALSE
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
#define LOG_LEVEL_VERBOSE
Definition debug.h:63
#define debug_msg(...)
#define debug_msg(...) log_msg(LOG_LEVEL_DEBUG, "[pid/%d +%d %s] ", getpid(), LINE,...
Definition debug.h:92
bool init_gl_context(struct gl_context *context, int which)
initializes specified OpenGL context
Definition gl_context.c:69
void destroy_gl_context(struct gl_context *context)
Definition gl_context.c:223
void gl_context_make_current(struct gl_context *context)
Definition gl_context.c:236
GLuint glsl_compile_link(const char *vprogram, const char *fprogram)
Definition gl_context.c:147
#define gl_check_error()
Definition gl_context.h:82
#define GL_CONTEXT_LEGACY
Definition gl_context.h:73
#define GL_CONTEXT_ANY
Definition gl_context.h:72
void glx_validate(void *arg)
Definition glx_common.c:81
void glx_free(void *arg)
Definition glx_common.c:90
void glx_make_current(void *arg)
Definition glx_common.c:434
void * glx_init(glx_opengl_version_t version)
Definition glx_common.c:169
#define OPENGL_VERSION_UNSPECIFIED
Definition glx_common.h:39
#define MK_OPENGL_VERSION(major, minor)
Definition glx_common.h:40
#define LOG_LEVEL_DEBUG
Definition kernels.cu:48
volatile int log_level
Definition debug.cpp:65
int get_mac_kernel_version_major(void)
void * mac_gl_init(mac_opengl_version_t)
void mac_gl_free(void *)
#define MAC_GL_PROFILE_LEGACY
void mac_gl_make_current(void *)
#define MAC_GL_PROFILE_3_2
#define IF_NOT_NULL_ELSE(val, default_val)
Definition macros.h:51
void * context
Definition gl_context.h:67
unsigned legacy
Definition gl_context.h:66
Definition hd-rum.c:27
static const char * vprogram
Definition swmix.c:468
void win32_context_make_current(void *arg)
void win32_context_free(void *arg)
void * win32_context_init(win32_opengl_version_t version)
#define OPENGL_VERSION_LEGACY