UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
gl_context.c File Reference
#include <stdio.h>
#include "win32_gl_common.h"
#include "debug.h"
#include "gl_context.h"
#include "utils/macros.h"
Include dependency graph for gl_context.c:

Go to the source code of this file.

Functions

void destroy_gl_context (struct gl_context *context)
 
void gl_context_make_current (struct gl_context *context)
 
GLuint glsl_compile_link (const char *vprogram, const char *fprogram)
 
bool init_gl_context (struct gl_context *context, int which)
 initializes specified OpenGL context
 

Detailed Description

Function Documentation

◆ destroy_gl_context()

void destroy_gl_context ( struct gl_context * context)

Definition at line 223 of file gl_context.c.

223 {
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}
void glx_free(void *arg)
Definition glx_common.c:90
void mac_gl_free(void *)
void * context
Definition gl_context.h:67
void win32_context_free(void *arg)

References gl_context::context, glx_free(), mac_gl_free(), and win32_context_free().

Referenced by dxt_glsl_decompress_done(), dxt_glsl_decompress_reconfigure(), scale_done(), vidcap_spout_done(), vidcap_swmix_done(), and vidcap_syphon_done().

Here is the call graph for this function:

◆ gl_context_make_current()

void gl_context_make_current ( struct gl_context * context)

Definition at line 236 of file gl_context.c.

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}
void glx_make_current(void *arg)
Definition glx_common.c:434
void mac_gl_make_current(void *)
void win32_context_make_current(void *arg)

References gl_context::context, glx_make_current(), mac_gl_make_current(), and win32_context_make_current().

Referenced by dxt_glsl_decompress(), dxt_glsl_decompress_done(), dxt_glsl_decompress_reconfigure(), master_worker(), oneshot_init(), scale_init(), scale_postprocess(), scale_reconfigure(), vidcap_spout_done(), vidcap_spout_grab(), vidcap_spout_init(), vidcap_swmix_done(), and vidcap_swmix_init().

Here is the call graph for this function:

◆ glsl_compile_link()

GLuint glsl_compile_link ( const char * vprogram,
const char * fprogram )

compiles and links specified program shaders

Returns
shader ID
Return values
0on error

Definition at line 147 of file gl_context.c.

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}
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 gl_check_error()
Definition gl_context.h:82
#define LOG_LEVEL_DEBUG
Definition kernels.cu:48
volatile int log_level
Definition debug.cpp:65
#define IF_NOT_NULL_ELSE(val, default_val)
Definition macros.h:51
static const char * vprogram
Definition swmix.c:468

References gl_check_error, IF_NOT_NULL_ELSE, log_level, LOG_LEVEL_DEBUG, LOG_LEVEL_ERROR, LOG_LEVEL_VERBOSE, log_msg(), and vprogram.

Referenced by gl_substitute_compile_link(), master_worker(), vidcap_swmix_init(), and vidcap_syphon_init_common().

Here is the call graph for this function:

◆ init_gl_context()

bool init_gl_context ( struct gl_context * context,
int which )

initializes specified OpenGL context

Note
After this call, context is current for calling thread.
Parameters
[out]contextnewly created context
whichOpenGL version specifier
Returns
info if context creation succeeded or failed

Definition at line 69 of file gl_context.c.

69 {
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}
#define TRUE
#define FALSE
#define debug_msg(...)
#define debug_msg(...) log_msg(LOG_LEVEL_DEBUG, "[pid/%d +%d %s] ", getpid(), LINE,...
Definition debug.h:92
#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_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
int get_mac_kernel_version_major(void)
void * mac_gl_init(mac_opengl_version_t)
#define MAC_GL_PROFILE_LEGACY
#define MAC_GL_PROFILE_3_2
unsigned legacy
Definition gl_context.h:66
Definition hd-rum.c:27
void * win32_context_init(win32_opengl_version_t version)
#define OPENGL_VERSION_LEGACY

References gl_context::context, debug_msg, FALSE, get_mac_kernel_version_major(), GL_CONTEXT_ANY, GL_CONTEXT_LEGACY, gl_context::gl_major, gl_context::gl_minor, glx_init(), glx_validate(), gl_context::legacy, mac_gl_init(), MAC_GL_PROFILE_3_2, MAC_GL_PROFILE_LEGACY, MK_OPENGL_VERSION, OPENGL_VERSION_LEGACY, OPENGL_VERSION_UNSPECIFIED, TRUE, and win32_context_init().

Referenced by configure_with(), scale_init(), vidcap_spout_init(), vidcap_swmix_init(), and vidcap_syphon_init_common().

Here is the call graph for this function: