This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_ph_gl.c
408 lines (341 loc) · 11 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2009 Sam Lantinga
4
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
14
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
20
21
Sam Lantinga
slouken@libsdl.org
*/
22
#include "SDL_config.h"
23
24
25
26
#include <dlfcn.h>
#include "SDL_ph_gl.h"
27
#if SDL_VIDEO_OPENGL
28
29
30
#if (_NTO_VERSION >= 630)
/* PhotonGL functions */
31
GLPH_DECLARE_FUNCS;
32
33
34
#endif /* 6.3.0 */
#if (_NTO_VERSION < 630)
35
36
void
ph_GL_SwapBuffers(_THIS)
37
38
39
40
41
{
PgSetRegion(PtWidgetRid(window));
PdOpenGLContextSwapBuffers(oglctx);
}
#else
42
43
void
ph_GL_SwapBuffers(_THIS)
44
45
46
47
48
{
qnxgl_swap_buffers(oglbuffers);
}
#endif /* 6.3.0 */
49
50
int
ph_GL_GetAttribute(_THIS, SDL_GLattr attrib, int *value)
51
{
52
53
54
55
56
57
58
59
60
61
switch (attrib) {
case SDL_GL_DOUBLEBUFFER:
*value = this->gl_config.double_buffer;
break;
case SDL_GL_STENCIL_SIZE:
*value = this->gl_config.stencil_size;
break;
case SDL_GL_DEPTH_SIZE:
*value = this->gl_config.depth_size;
break;
62
#if (_NTO_VERSION >= 630)
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
case SDL_GL_RED_SIZE:
*value = this->gl_config.red_size;
break;
case SDL_GL_GREEN_SIZE:
*value = this->gl_config.green_size;
break;
case SDL_GL_BLUE_SIZE:
*value = this->gl_config.blue_size;
break;
case SDL_GL_ALPHA_SIZE:
*value = this->gl_config.alpha_size;
break;
case SDL_GL_ACCUM_RED_SIZE:
*value = this->gl_config.accum_red_size;
break;
case SDL_GL_ACCUM_GREEN_SIZE:
*value = this->gl_config.accum_green_size;
break;
case SDL_GL_ACCUM_BLUE_SIZE:
*value = this->gl_config.accum_blue_size;
break;
case SDL_GL_ACCUM_ALPHA_SIZE:
*value = this->gl_config.accum_alpha_size;
break;
case SDL_GL_STEREO:
*value = this->gl_config.stereo;
break;
90
#endif /* 6.3.0 */
91
92
93
default:
*value = 0;
return (-1);
94
95
96
97
98
}
return 0;
}
#if (_NTO_VERSION < 630)
99
100
int
ph_GL_LoadLibrary(_THIS, const char *path)
101
{
102
/* if code compiled with SDL_VIDEO_OPENGL, that mean that library already linked */
103
104
105
106
107
this->gl_config.driver_loaded = 1;
return 0;
}
#else
108
109
int
ph_GL_LoadLibrary(_THIS, const char *path)
110
{
111
112
void *handle;
int dlopen_flags = RTLD_WORLD | RTLD_GROUP;
113
114
if (this->gl_config.dll_handle != NULL) {
115
116
117
118
119
return 0;
}
handle = dlopen(path, dlopen_flags);
120
if (handle == NULL) {
121
122
123
124
125
126
127
SDL_SetError("ph_GL_LoadLibrary(): Could not load OpenGL library");
return -1;
}
this->gl_config.dll_handle = handle;
this->gl_config.driver_loaded = 1;
128
129
SDL_strlcpy(this->gl_config.driver_path, path,
SDL_arraysize(this->gl_config.driver_path));
130
131
132
133
134
135
return 0;
}
#endif /* 6.3.0 */
#if (_NTO_VERSION < 630)
136
137
void *
ph_GL_GetProcAddress(_THIS, const char *proc)
138
139
140
141
{
return NULL;
}
#else
142
143
void *
ph_GL_GetProcAddress(_THIS, const char *proc)
144
{
145
void *function;
146
147
if (this->gl_config.dll_handle == NULL) {
148
ph_GL_LoadLibrary(this, DEFAULT_OPENGL);
149
if (this->gl_config.dll_handle == NULL) {
150
151
152
return NULL;
}
}
153
154
155
156
function = qnxgl_get_func(proc, oglctx, 0);
if (function == NULL) {
function = dlsym(this->gl_config.dll_handle, proc);
157
158
159
160
161
162
163
}
return function;
}
#endif /* 6.3.0 */
#if (_NTO_VERSION < 630)
164
165
int
ph_GL_MakeCurrent(_THIS)
166
167
168
{
PgSetRegion(PtWidgetRid(window));
169
if (oglctx != NULL) {
170
171
172
173
174
175
PhDCSetCurrent(oglctx);
}
return 0;
}
#else
176
177
int
ph_GL_MakeCurrent(_THIS)
178
179
180
{
PgSetRegion(PtWidgetRid(window));
181
182
183
if (oglctx != NULL) {
if (qnxgl_set_current(oglctx) == -1) {
return -1;
184
185
186
187
188
189
190
191
192
193
194
}
}
return 0;
}
#endif /* 6.3.0 */
#if (_NTO_VERSION < 630)
/* This code is actual for the Photon3D Runtime which was available prior to 6.3 only */
195
196
int
ph_SetupOpenGLContext(_THIS, int width, int height, int bpp, Uint32 flags)
197
198
199
{
PhDim_t dim;
uint64_t OGLAttrib[PH_OGL_MAX_ATTRIBS];
200
int exposepost = 0;
201
202
int OGLargc;
203
204
205
206
207
208
209
210
211
212
213
214
215
216
dim.w = width;
dim.h = height;
if ((oglctx != NULL) && (oglflags == flags) && (oglbpp == bpp)) {
PdOpenGLContextResize(oglctx, &dim);
PhDCSetCurrent(oglctx);
return 0;
} else {
if (oglctx != NULL) {
PhDCSetCurrent(NULL);
PhDCRelease(oglctx);
oglctx = NULL;
exposepost = 1;
}
217
218
}
219
220
221
222
OGLargc = 0;
if (this->gl_config.depth_size) {
OGLAttrib[OGLargc++] = PHOGL_ATTRIB_DEPTH_BITS;
OGLAttrib[OGLargc++] = this->gl_config.depth_size;
223
}
224
225
226
if (this->gl_config.stencil_size) {
OGLAttrib[OGLargc++] = PHOGL_ATTRIB_STENCIL_BITS;
OGLAttrib[OGLargc++] = this->gl_config.stencil_size;
227
}
228
229
230
231
232
233
OGLAttrib[OGLargc++] = PHOGL_ATTRIB_FORCE_SW;
if (flags & SDL_FULLSCREEN) {
OGLAttrib[OGLargc++] = PHOGL_ATTRIB_FULLSCREEN;
OGLAttrib[OGLargc++] = PHOGL_ATTRIB_DIRECT;
OGLAttrib[OGLargc++] = PHOGL_ATTRIB_FULLSCREEN_BEST;
OGLAttrib[OGLargc++] = PHOGL_ATTRIB_FULLSCREEN_CENTER;
234
}
235
OGLAttrib[OGLargc++] = PHOGL_ATTRIB_NONE;
236
237
238
239
240
if (this->gl_config.double_buffer) {
oglctx = PdCreateOpenGLContext(2, &dim, 0, OGLAttrib);
} else {
oglctx = PdCreateOpenGLContext(1, &dim, 0, OGLAttrib);
241
242
}
243
244
245
if (oglctx == NULL) {
SDL_SetError
("ph_SetupOpenGLContext(): cannot create OpenGL context !\n");
246
247
248
249
250
251
252
return -1;
}
PhDCSetCurrent(oglctx);
PtFlush();
253
254
oglflags = flags;
oglbpp = bpp;
255
256
if (exposepost != 0) {
257
258
259
260
261
262
263
264
265
266
267
/* OpenGL context has been recreated, so report about this fact */
SDL_PrivateExpose();
}
return 0;
}
#else /* _NTO_VERSION */
/* This code is actual for the built-in PhGL support, which became available since 6.3 */
268
269
int
ph_SetupOpenGLContext(_THIS, int width, int height, int bpp, Uint32 flags)
270
271
{
qnxgl_buf_attrib_t qnxgl_attribs[PH_OGL_MAX_ATTRIBS];
272
qnxgl_buf_attrib_t *qnxgl_attribs_slide;
273
274
275
276
277
278
279
int num_interfaces = 0;
int num_buffers = 0;
/* Initialize the OpenGL subsystem */
num_interfaces = qnxgl_init(NULL, NULL, 0);
280
281
282
if (num_interfaces < 0) {
SDL_SetError
("ph_SetupOpenGLContext(): cannot initialize OpenGL subsystem !\n");
283
284
return -1;
}
285
286
287
if (num_interfaces == 0) {
SDL_SetError
("ph_SetupOpenGLContext(): there are no available OpenGL renderers was found !\n");
288
289
290
291
return -1;
}
/* Driver is linked */
292
this->gl_config.driver_loaded = 1;
293
294
/* Initialize the OpenGL context attributes */
295
qnxgl_attribs_slide = qnxgl_attribs;
296
297
/* Depth size */
298
if (this->gl_config.depth_size) {
299
fprintf(stderr, "setted depth size %d\n", this->gl_config.depth_size);
300
301
302
qnxgl_attribs_slide =
qnxgl_attrib_set_depth(qnxgl_attribs_slide,
this->gl_config.depth_size);
303
304
305
}
/* Stencil size */
306
307
308
309
if (this->gl_config.stencil_size) {
qnxgl_attribs_slide =
qnxgl_attrib_set_stencil(qnxgl_attribs_slide,
this->gl_config.stencil_size);
310
311
312
}
/* The sum of the accum bits of each channel */
313
314
315
if ((this->gl_config.accum_red_size != 0)
&& (this->gl_config.accum_blue_size != 0)
&& (this->gl_config.accum_green_size != 0)) {
316
qnxgl_attribs_slide = qnxgl_attrib_set_accum(qnxgl_attribs_slide,
317
318
this->
gl_config.accum_red_size
319
320
+
this->
321
322
323
324
325
326
327
gl_config.accum_blue_size
+
this->
gl_config.
accum_green_size +
this->gl_config.
accum_alpha_size);
328
}
329
330
/* Stereo mode */
331
if (this->gl_config.stereo) {
332
333
334
335
qnxgl_attribs_slide = qnxgl_attrib_set_stereo(qnxgl_attribs_slide);
}
/* Fullscreen mode */
336
337
338
if ((flags & SDL_FULLSCREEN) == SDL_FULLSCREEN) {
qnxgl_attribs_slide =
qnxgl_attrib_set_hint_fullscreen(qnxgl_attribs_slide);
339
}
340
341
/* Double buffering mode */
342
343
344
345
if (this->gl_config.double_buffer) {
num_buffers = 2;
} else {
num_buffers = 1;
346
347
348
349
350
351
}
/* Loading the function pointers so we can use the extensions */
GLPH_LOAD_FUNCS_GC(oglctx);
/* Set the buffers region to be that of our window's region */
352
353
qnxgl_attribs_slide =
glph_attrib_set_region(qnxgl_attribs_slide, PtWidgetRid(window));
354
355
356
/* End of the attributes array */
qnxgl_attribs_slide = qnxgl_attrib_set_end(qnxgl_attribs_slide);
357
358
/* Create the buffers with the specified color model */
359
360
361
362
fprintf(stderr, "ARGB: %d, %d, %d, %d\n", this->gl_config.alpha_size,
this->gl_config.red_size, this->gl_config.green_size,
this->gl_config.blue_size);
oglbuffers = qnxgl_buffers_create(QNXGL_FORMAT_BEST_RGB,
363
364
/* __QNXGL_BUILD_FORMAT(0, __QNXGL_COLOR_MODEL_RGB, this->gl_config.alpha_size,
this->gl_config.red_size, this->gl_config.green_size, this->gl_config.blue_size), */
365
366
num_buffers, width, height,
qnxgl_attribs, -1);
367
368
369
370
371
if (oglbuffers == NULL) {
SDL_SetError
("ph_SetupOpenGLContext(): failed to create OpenGL buffers !\n");
372
373
374
375
376
377
378
qnxgl_finish();
return -1;
}
/* Create a QNXGL context for the previously created buffer */
oglctx = qnxgl_context_create(oglbuffers, NULL);
379
380
381
if (oglctx == NULL) {
SDL_SetError
("ph_SetupOpenGLContext(): failed to create OpenGL context !\n");
382
383
384
385
386
387
qnxgl_buffers_destroy(oglbuffers);
qnxgl_finish();
return -1;
}
/* Attempt to make the context current so we can use OpenGL commands */
388
389
390
if (qnxgl_set_current(oglctx) == -1) {
SDL_SetError
("ph_SetupOpenGLContext(): failed to make the OpenGL context current !\n");
391
392
393
394
395
396
397
398
qnxgl_context_destroy(oglctx);
qnxgl_buffers_destroy(oglbuffers);
qnxgl_finish();
return -1;
}
PtFlush();
399
400
oglflags = flags;
oglbpp = bpp;
401
402
403
404
405
406
return 0;
}
#endif /* _NTO_VERSION */
407
#endif /* SDL_VIDEO_OPENGL */
408
/* vi: set ts=4 sw=4 expandtab: */