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